比特币钱包详解 分层确定性密钥 扩展密钥代码示例
- 2025-09-23 21:06:00
- admin
- 原创 259
一、比特币钱包详解
1、全功能钱包同时包含三个功能:公钥分发、花费签名、交易查询和广播;
2、全功能钱包非常便于使用,缺点是因为连接网络,可能被攻击而泄露私钥;
3、仅签名钱包:只有签名功能,需要配合网络钱包使用,用于提升私钥安全性;
4、仅签名钱包:生成父私钥和父公钥,网络钱包使用父公钥生成子公钥,用于接收比特币;
5、仅签名钱包:网络钱包生成未签名的交易数据,仅签名钱包对未签名的交易数据签名;
6、仅签名钱包-离线钱包:两个全功能钱包,分别安装在两台设备,其中一台使用离线模式;
7、仅签名钱包-硬件钱包:专用硬件设备生成父私钥和父公钥,可以连接到网络钱包进行签名;
8、仅分发公钥钱包只能分发公钥,需要使用额外钱包支持花费签名、交易查询和广播;
比特币资源:
1、比特币软件:https://en.bitcoin.it/wiki/Software
2、比特币客户端:https://bitcoinwiki.org/category/bitcoin-infrastructure-and-clients
公私钥详解:
1、私钥长度是256比特,私钥取值范围规范:https://www.secg.org/sec2-v2.pdf
2、公钥不压缩使用点坐标表示,公钥压缩使用x坐标加一个方向位表示,节约一半字节数量;
3、secp256k1椭圆曲线一个x坐标仅包含2个点,一个点在曲线上面,一个点在曲线下面;
4、比特币地址前缀:https://en.bitcoin.it/wiki/List_of_address_prefixes
5、比特币地址工具:https://www.rfctools.com/bitcoin-address-test-tool
私钥格式:
1、WIF,Wallet Import Format,包含字段:网络标识、私钥、公钥是否压缩、校验码;
2、WIF私钥格式包含校验码,可以快速检验错误,使用base58编码,私钥长度更短;
3、WIF私钥格式工具:https://learnmeabitcoin.com/technical/keys/private-key/wif
4、WIF私钥格式工具:https://secretscan.org/PrivateKeyWif
5、MPKF,Mini Private Key Format,迷你私钥格式使用小于等于30个字符编码私钥;
6、MPKF私钥格式用于将私钥承载于实物场景,使用sha256(mini_key)导出私钥;
余额查询:
1、余额查询:https://btc.tokenview.io/cn
2、余额查询:https://bitinfocharts.com/zh/bitcoin/explorer
3、余额查询:https://www.blockexplorer.com/bitcoin/transactions
4、余额排行:https://bitinfocharts.com/zh/top-100-richest-bitcoin-addresses.html
5、余额排行:https://www.528btc.com/tool/rich/btc.html
6、交易所余额:https://history.btc126.com/balance
二、分层确定性密钥
1、分层确定性密钥使用一个种子,长度是128bits、256bits、512bits,生成大量有层级的私钥和公钥;
2、第一层:output=hmac-sha512(seed),prikey=left-half(output),chain_code=right-half(output);
3、第二层:output2=hmac-sha512(parent_pubkey+parent_chain_code+index_number);
4、第二层:pubkey=parent_pubkey+point(left-half(output2));
5、第二层:prikey=(parent_prikey+left-half(output2)) % G;
6、第二层:chain_code=right-half(output2);
7、chain_code增加下一层私钥和公钥的随机性;
8、index_number表示同一层级不同的私钥和公钥;
9、index_number>=0x00 && index_number<=0x7fffffff;
强化扩展密钥:
1、分层确定性密钥存在一个问题,只要一个私钥泄露,通过pubkey+chain_node可能计算所有父私钥和子私钥;
2、分层确定性密钥使用公钥导出子公钥,强化扩展密钥使用私钥导出子公钥,强化扩展密钥可以避免上述问题;
3、第二层:output=hmac-sha512(parent_prikey+parent_chain_code+index_number);
4、第二层:pubkey=parent_pubkey+point(left-half(output));
5、第二层:prikey=(parent_prikey+left-half(output)) % G;
6、第二层:chain_code=right-half(output);
7、index_number>=0x80000000 && index_number<=0xffffffff;
扩展密钥规范:
1、扩展密钥规范:https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
2、助记词规范:https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
3、助记词:通过种子和助记词相互转换,使用base2048编码,方便记录种子;
4、助记词:通过助记词+可选密码生成种子,方便记录种子;
5、助记词:种子=times(hmac-sha512(words,password), 2048);
6、助记词:推荐种子和助记词相互转换,不推荐使用助记词生成种子;
7、扩展密钥工具:https://github.com/darosior/python-bip32,pip install bip32
8、助记词工具:https://github.com/trezor/python-mnemonic,pip install mnemonic
三、扩展密钥代码示例
扩展密钥:
from bip32 import BIP32, HARDENED_INDEX
bip32 = BIP32.from_seed(bytes.fromhex("1010007ad7ce1b0534de9d05c3aa7cdf"), "main")
s = bip32.get_xpriv_from_path([1, HARDENED_INDEX, 999])
print(s)
s = bip32.get_xpriv_from_path("m/1/0'/999")
print(s)
s = bip32.get_xpriv_from_path("m/1/0h/999")
print(s)
种子和助记词相互转换:
mnemonic = Mnemonic("english")
seed = bytes.fromhex("1010007ad7ce1b0534de9d05c3aa7cdf")
words = mnemonic.to_mnemonic(seed)
print("助記詞:", words)
seed = mnemonic.to_entropy(words)
print("種子:", seed.hex())
助记词生成种子:
mnemonic = Mnemonic("english")
words = mnemonic.generate(strength=128)
print("助記詞:", words)
seed = mnemonic.to_seed(words, passphrase="")
print("種子:", seed.hex())