← 返回列表

走进区块链的第一步:钱包服务

单站 零鉴科技 发布 Wed, 07 Sep 2022 18:47:00 +0800 入库 2026-11-18T12:54:32

原创 零鉴科技 2022-09-07 18:47 湖南

本文主要关注区块链上使用较广泛的钱包服务:多签钱包和分层确定性钱包。

多签钱包

_singleton: 0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552 initializer: 0xb63e800d0000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000f48f2b2d2a534e402487b3ee7c18c33aec0fe5e400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000005ce0b4d552a80ca79ef391a1d87be7350f3e09ab0000000000000000000000000000000000000000000000000000000000000000 saltNonce: 1661943294934

3、创建合约使用的是内联汇编的方式,通过 call 调用 proxy 合约内的函数,具体需要调用的初始化函数由参数 initializer 来实现。

4、回到创建钱包的交易。通过解析 initializer 参数可以发现,调用的是 proxy 合约的 setup 函数进行初始化。

>>> sha3.keccak_256('setup(address[],uint256,address,bytes,address,address,uint256,address)'.encode('utf-8')).hexdigest()``>>> 'b63e800d34f56a9238e5b86a8547a23865164bec371bede606e899182da118bf'``>>> initializer[:8]``>>> 'b63e800d'

setup 函数会将用户设置的多签规则(owner, threshold)设置到 proxy 合约中,完成用户对于多签钱包的特定需求。

M <Public Key 1> <Public Key 2> ... <Public Key N> N CHECKMULTISIG``00 <Signature 1> <Signature 2> ... <Signature M> M <Public Key 1> <Public Key 2> ... <Public Key N> N CHECKMULTISIG``<Script> OP_HASH160 <Script Hash> OP_EQUAL
但如果设置的多签规则中阈值未超过验证者数量的一半,那么意味着少数人的共识就能够代表大多数人,又或者如果验证者的数量过少,那么恶意的验证者仅需要花费较小的成本(策反部分验证者)就能够控制多签钱包。

例如前段时间跨链桥项目 Harmony 被攻击,损失了将近1亿美元,其中有一部分原因就是多签规则的设置存在问题(2/5的多签对则),导致攻击者仅需要获取其中两个的私钥就可以控制多签钱包。

因此,使用了多签钱包并不意味着绝对的安全和去中心化(社区自治)。同时还需要设置好多签规则,分配好对应的私钥,并确保拥有验证者私钥的主体之间不存在直接的利益关系。

分层确定性钱包

1、助记词转换为 512 位 seed 。使用 PBKDF2 算法对初始密钥进行延伸(key streching),以此来降低初始密钥被暴力破解的概率。

passphrase =""  # default value is ""``salt = bytes("mnemonic"+ passphrase, 'utf8')``seed = hashlib.pbkdf2_hmac('sha512', mnemonic, salt, 2048)

2、seed 生成 Master Private Key 和 Master Chain Code 。使用 HMAC-SHA512 算法对 seed 进行单向哈希得到 512 位二进制字符串,左边 256 位为 Master Private Key ,右边 256 位为 Master Chain Code 。

h = hmac.new(b'Bitcoin seed', seed, hashlib.sha512).digest()``master_private_key, master_chain_code = h[:32], h[32:]

3、使用 Master Private Key , Master Chain Code 和 index 作为第一轮导出函数的输入,不断地进行推导,得到 index 位置上的子私钥。

child_private_key = HMAC-SHA512(Key=parent_chain_code, Data=(parent_private_key*secp256k1.G||index)) + parent_private_key
(https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)。

public_key = private_key * secp256k1.G
(1) child_private_key = HMAC-SHA512(Key=parent_chain_code, Data=(parent_private_key*secp256k1.G||index)) + parent_private_key``(2) public_key = private_key * secp256k1.G``由 (1) 和 (2) 公式可以推导出:``child_public_key = HMAC-SHA512(Key=parent_chain_code, Data=(parent_public_key||index))*secp256k1.G + parent_public_key

扩展密钥包含6种信息,分别为version, depth, parent key fingerprint, child number, chain code, public key。

已知:parent_extended_public_key 和 child_private_key``(1) 解析parent_extended_public_key,得到parent_chain_code和parent_public_key``(2) 由公钥推导函数可以通过爆破计算出index``(3) 变换私钥推导函数为parent_private_key = child_private_key - hmac_sha512(parent_chain_code, parent_private_key*G || index) ,计算出parent_private_key

Hardened Derivation:``child_private_key = HMAC-SHA512(Key=parent_chain_code, Data=(0x00||parent_private_key||index)) + parent_private_key``Non-Hardened Derivation:``child_private_key = HMAC-SHA512(Key=parent_chain_code, Data=(parent_private_key*secp256k1.G||index)) + parent_private_key

m / purpose' / coin_type' / account' / change / address_index``m / 44'/ 0' / 0' / 0 / 0``m / 44' / 60' / 0'/ 0 / 0
References

参考

比特币多签钱包

[1]  https://blog.csdn.net/BitTribeLab/article/details/102696746

[2]  https://aaron67.cc/2018/12/29/bitcoin-transaction-p2ms-p2sh/

[3]  https://www.youtube.com/watch?v=6Fa04MnURhw

[1]  https://www.alibabacloud.com/blog/how-ethereum-bip-32-hardware-digital-wallet-works_597788

[2]  https://github.com/bitcoin/bips/blob/master/bip-0032.mediawik

[3]  http://aandds.com/blog/mnemonic-hd-wallet.html

[5]  https://bitcoin.stackexchange.com/questions/87834/hardened-keys-vs-normal-keys-a-simpler-explanation

「 往期文章 」

原文链接:https://mp.weixin.qq.com/s?__biz=MzkzODE2NjgyNQ==&mid=2247496549&idx=1&sn=d95aa6140d4c136a43c299c57a8be58b