Bitcoin Core上实现多签交易

本文最后更新于 2022年11月21日 晚上

建立一个 4 节点的回归测试网络,其中任意 3 节点产生一个 2of3 的多签交易付给第四个节点

Bitcoin Core上实现多签交易

配置 Regtest.conf

  • %APPDATA%\Bitcoin 下面新建四个文件夹,分别命名为 alice, bob, networkfour
  • 在这四个文件夹下分别新建 bitcoin.conf 文件,其内容如下:

配置文件

  • netstat -ano|findstr "port" 查看端口是否被占用,若没有则可设置为 portrpcport

alice

  • bitcoin
1
2
3
4
5
6
7
8
9
regtest=1
port=18445
addnode=127.0.0.1:18446
addnode=127.0.0.1:18447
addnode=127.0.0.1:18451
server=1
rpcport=18448
rpcusedr=alice
rpcpassword=alice

bob

  • bitcoin
1
2
3
4
5
6
7
8
9
regtest=1
port=18446
addnode=127.0.0.1:18445
addnode=127.0.0.1:18447
addnode=127.0.0.1:18451
server=1
rpcport=18449
rpcusedr=bob
rpcpassword=bob

network

  • bitcoin
1
2
3
4
5
6
7
8
9
regtest=1
port=18447
addnode=127.0.0.1:18445
addnode=127.0.0.1:18446
addnode=127.0.0.1:18451
server=1
rpcport=18450
rpcusedr=network
rpcpassword=network

four

  • bitcoin
1
2
3
4
5
6
7
8
9
regtest=1
port=18451
addnode=127.0.0.1:18445
addnode=127.0.0.1:18446
addnode=127.0.0.1:18447
server=1
rpcport=18452
rpcusedr=four
rpcpassword=four

通过批处理简化命令行输入

  • 在 %APPDATA%\Bitcoin 添加 regtest.bat 文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
@DOSKEY alice-d=bitcoind -regtest -datadir=%appdata%\bitcoin\alice $* 
@DOSKEY bob-d=bitcoind -regtest -datadir=%appdata%\bitcoin\bob $*
@DOSKEY network-d=bitcoind -regtest -datadir=%appdata%\bitcoin\network $*
@DOSKEY four-d=bitcoind -regtest -datadir=%appdata%\bitcoin\four $*
@DOSKEY alice-cli=bitcoin-cli -regtest -datadir=%appdata%\bitcoin\alice $*
@DOSKEY bob-cli=bitcoin-cli -regtest -datadir=%appdata%\bitcoin\bob $*
@DOSKEY network-cli=bitcoin-cli -regtest -datadir=%appdata%\bitcoin\network $*
@DOSKEY four-cli=bitcoin-cli -regtest -datadir=%appdata%\bitcoin\four $*
@DOSKEY alice-qt=bitcoin-qt -regtest -datadir=%appdata%\bitcoin\alice $*
@DOSKEY bob-qt=bitcoin-qt -regtest -datadir=%appdata%\bitcoin\bob $*
@DOSKEY network-qt=bitcoin-qt -regtest -datadir=%appdata%\bitcoin\network $*
@DOSKEY four-qt=bitcoin-qt -regtest -datadir=%appdata%\bitcoin\four $*
  • 在注册表中找到 HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor , 右键新建,选择字符串值,名为 AutoRun,值为 regtest.bat 的绝对路径。保存退出,即可完成对指令的优化。

检查节点连接情况

  • 分别在四个命令行窗口输入下列命令,启动各个节点
1
bitcoind -datadir=%APPDATA%\Bitcoin\alice
1
bitcoind -datadir=%APPDATA%\Bitcoin\bob
1
bitcoind -datadir=%APPDATA%\Bitcoin\network
1
bitcoind -datadir=%APPDATA%\Bitcoin\four
  • 打开命令行窗口,输入下列命令,查看节点连接情况
1
alice-cli getpeerinfo
1
bob-cli getpeerinfo
1
network-cli getpeerinfo
1
four-cli getpeerinfo
  • 如果节点数大于 4 即成功

打开 qt 客户端

  • 关闭之前的四个命令行窗口,然后在命令行中输入下列命令
1
2
3
4
alice-qt
bob-qt
network-qt
four-qt
  • qt 客户端可能会显示落后XX年,在任意 qt 客户端从 帮助>调试窗口>控制台 输入指令
1
generate 1

​ 然后等待一段时间即可

  • 也可以从 帮助>调试窗口>同伴检查节点连接情况——应该是 连接数 6 (输入: 3 / 输出: 3)

获取公钥

  • 从 qt 客户端的 文件>正在接收地址 中复制当前钱包的一个地址(不是标签),然后运用下列命令在 qt 控制台中获取对应公钥和私钥
1
2
validateaddress <address>
dumpprivatekey <address>

​ 得到的四个节点的对应公钥、私钥、地址如下:

节点 正在接收地址 公钥 私钥
alice mo7QSvzFhrmSob8Nc4oezMrKFG4kgXfpaQ 03c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247 cQ9Z4BSY4657dqgfSEPhKVVrYrcGmWZiSBhrSW9cbYXGiDMdeNBc
bob mgWnSjEDz1hCBryhkhQ1gXvGtK8aujiCL2 0338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb cVkpuL8fJtdqbWjxTkPVsPS7Lgm35DJBv3FaJkiKcHmAFFHqvP4Y
network mx7mERwyydMSYFTzPifQwFBhnmTH2kDcn6 02bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d1 cPBWwcJU3jEsvYdsSCkMshAd5sugk3y3LBXbAfh5GzYcoJGLxBBn
four mrntHkXT2ua8rkqXx4SVJoeGY24VxDo8og 02186ce1b7f4f33d2e2284111c1442eeb36801424517b15f25410c9089cbe2fcc6 cTdnyoEahTfrXffKyDQ3rDGJg9DmWWkCtZwfmeybAb8CFjQ7YHDp

生成多重签名

  • alice 的控制台中输入以下命令,生成 2 of 3 多重签名
1
createmultisig n <'["key1","key2",...,"keyn"]'>

​ 在这里,使用 alicebobnetwork 的公钥生成,命令如下:

1
createmultisig 2 '["03c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247","0338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb","02bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d1"]'
  • 返回信息如下:
1
2
3
4
{
"address": "2NDuscm8JWy7jVgQfcCSyEbLzsf1uwxtfi8",
"redeemScript": "522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae"
}
  • 将上述生成的地址加入到 bob 的钱包
1
addmultisigaddress 2 '["03c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247","0338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb","02bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d1"]'
  • 得到地址,加入到 bob 的正在接收地址中
1
2NDuscm8JWy7jVgQfcCSyEbLzsf1uwxtfi8

挖矿并向多签地址转钱

  • alice 生成 101 个区块,从而获得 50个 BTC
1
generate 100
  • alice2NDuscm8JWy7jVgQfcCSyEbLzsf1uwxtfi8 转账 12 个 BTC
1
sendaddress 2NDuscm8JWy7jVgQfcCSyEbLzsf1uwxtfi8 12
  • alice 生成 6个区块从而确认交易
1
generate 6
  • bob 的钱包可以看到交易成功

多签地址转账成功

构建从多签地址向第四节点转账账单

  • alice 控制台中输入如下命令,从多签地址向 four 转账 6 个 BTC

※:要给出找零地址,否则会报错 absurdly-high-fee (code -26)

1
createrawtransaction '[{ "txid": "8b1365335f9ea88fdc9aaba0aae89503cadb607a583a4215a2a157fb09ca53f2", "vout": 1,"scriptPubKey": "a914e2b1bbb779c898d24ce4695b5773ec0233ee138087","redeemScript": "522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae"}]' '{"mrntHkXT2ua8rkqXx4SVJoeGY24VxDo8og":6,"mgWnSjEDz1hCBryhkhQ1gXvGtK8aujiCL2":5.9998}' 
  • 其中,txidscriptPubKey,可以直接在 bob 的交易记录中查询,或者使用一下命令
1
listunspent

生成了 16 进制的交易数据

1
0200000001f253ca09fb57a1a215423a587a60dbca0395e8aaa0ab9adc8fa89e5f3365138b0100000000ffffffff020046c323000000001976a9147bacb21432c1e521319387833b87fe704dfe6afc88ace0f7c223000000001976a9140aefe72eaf26dae31397c46bfa821781406541be88ac00000000

对交易进行签名

  • alice 签名
1
signrawtransaction "0200000001f253ca09fb57a1a215423a587a60dbca0395e8aaa0ab9adc8fa89e5f3365138b0100000000ffffffff020046c323000000001976a9147bacb21432c1e521319387833b87fe704dfe6afc88ace0f7c223000000001976a9140aefe72eaf26dae31397c46bfa821781406541be88ac00000000" '[{ "txid": "8b1365335f9ea88fdc9aaba0aae89503cadb607a583a4215a2a157fb09ca53f2", "vout": 1,"scriptPubKey": "a914e2b1bbb779c898d24ce4695b5773ec0233ee138087","redeemScript": "522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae"}]' '["cQ9Z4BSY4657dqgfSEPhKVVrYrcGmWZiSBhrSW9cbYXGiDMdeNBc"]' 

​ 得到一次签名后的交易数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"hex": "0200000001f253ca09fb57a1a215423a587a60dbca0395e8aaa0ab9adc8fa89e5f3365138b01000000b500483045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d014c69522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153aeffffffff020046c323000000001976a9147bacb21432c1e521319387833b87fe704dfe6afc88ace0f7c223000000001976a9140aefe72eaf26dae31397c46bfa821781406541be88ac00000000",
"complete": false,
"errors": [
{
"txid": "8b1365335f9ea88fdc9aaba0aae89503cadb607a583a4215a2a157fb09ca53f2",
"vout": 1,
"witness": [
],
"scriptSig": "00483045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d014c69522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae",
"sequence": 4294967295,
"error": "Operation not valid with the current stack size"
}
]
}

alice签名

  • network 签名
1
signrawtransaction "0200000001f253ca09fb57a1a215423a587a60dbca0395e8aaa0ab9adc8fa89e5f3365138b01000000b500483045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d014c69522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153aeffffffff020046c323000000001976a9147bacb21432c1e521319387833b87fe704dfe6afc88ace0f7c223000000001976a9140aefe72eaf26dae31397c46bfa821781406541be88ac00000000" '[{ "txid": "8b1365335f9ea88fdc9aaba0aae89503cadb607a583a4215a2a157fb09ca53f2", "vout": 1,"scriptPubKey": "a914e2b1bbb779c898d24ce4695b5773ec0233ee138087","redeemScript": "522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae"}]' '["cPBWwcJU3jEsvYdsSCkMshAd5sugk3y3LBXbAfh5GzYcoJGLxBBn"]' 

​ 得到

1
2
3
4
{
"hex": "0200000001f253ca09fb57a1a215423a587a60dbca0395e8aaa0ab9adc8fa89e5f3365138b01000000fdfe0000483045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d01483045022100ceca365ba0d5ec301401f5792fb333fd6ff9b37124afe7b5b484b51d7dfb038f0220614c1adbc3b84991b042f05022bf29eb106669599b0ee8daabbc9656b98a78a6014c69522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153aeffffffff020046c323000000001976a9147bacb21432c1e521319387833b87fe704dfe6afc88ace0f7c223000000001976a9140aefe72eaf26dae31397c46bfa821781406541be88ac00000000",
"complete": true
}

network签名

  • bob 发送交易单
1
sendrawtransaction 0200000001f253ca09fb57a1a215423a587a60dbca0395e8aaa0ab9adc8fa89e5f3365138b01000000fdfe0000483045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d01483045022100ceca365ba0d5ec301401f5792fb333fd6ff9b37124afe7b5b484b51d7dfb038f0220614c1adbc3b84991b042f05022bf29eb106669599b0ee8daabbc9656b98a78a6014c69522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153aeffffffff020046c323000000001976a9147bacb21432c1e521319387833b87fe704dfe6afc88ace0f7c223000000001976a9140aefe72eaf26dae31397c46bfa821781406541be88ac00000000

​ 得到 txid

1
1e58ea5a6d43de28d99cba0f6d9dd8fbf1d9a07187ae4be364f71078ccf529ec

​ 通过 getrawtransactiondecoderawtransaction,得到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

21:52:51

{
"txid": "1e58ea5a6d43de28d99cba0f6d9dd8fbf1d9a07187ae4be364f71078ccf529ec",
"hash": "1e58ea5a6d43de28d99cba0f6d9dd8fbf1d9a07187ae4be364f71078ccf529ec",
"version": 2,
"size": 375,
"vsize": 375,
"locktime": 0,
"vin": [
{
"txid": "8b1365335f9ea88fdc9aaba0aae89503cadb607a583a4215a2a157fb09ca53f2",
"vout": 1,
"scriptSig": {
"asm": "0 3045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d[ALL] 3045022100ceca365ba0d5ec301401f5792fb333fd6ff9b37124afe7b5b484b51d7dfb038f0220614c1adbc3b84991b042f05022bf29eb106669599b0ee8daabbc9656b98a78a6[ALL] 522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae",
"hex": "00483045022100859690df26695e8142b56c4f4e5be62ab9c2101c1c1f63c9b2fa4ac9c7acba7b02204db63f359dd2858609cfca07d9928d1955d441cec86d15e6beddc89344ab0b3d01483045022100ceca365ba0d5ec301401f5792fb333fd6ff9b37124afe7b5b484b51d7dfb038f0220614c1adbc3b84991b042f05022bf29eb106669599b0ee8daabbc9656b98a78a6014c69522103c227dd0d3dc063ba152bca22d08459946131a2a02379eb8113c5bb9263d52247210338c568aff18998f511eabf3c675a3a9596dd71896bc97c939c6a873a43c80dcb2102bf668f096f116ad7a6099227799358a00413467521c9850e688ef9ea18bb86d153ae"
},
"sequence": 4294967295
}
],
"vout": [
{
"value": 6.00000000,
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 7bacb21432c1e521319387833b87fe704dfe6afc OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a9147bacb21432c1e521319387833b87fe704dfe6afc88ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"mrntHkXT2ua8rkqXx4SVJoeGY24VxDo8og"
]
}
},
{
"value": 5.99980000,
"n": 1,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 0aefe72eaf26dae31397c46bfa821781406541be OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a9140aefe72eaf26dae31397c46bfa821781406541be88ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"mgWnSjEDz1hCBryhkhQ1gXvGtK8aujiCL2"
]
}
}
]
}

多签地址转账成功2

  • 找零地址为 network 的地址,可见源地址没有 BTC 了,找零到了 network,支付了 four 地址 6 BTC,实验完成。

network钱包

four钱包


Bitcoin Core上实现多签交易
https://justloseit.top/Bitcoin Core上实现多签交易/
作者
Mobilis In Mobili & zz
发布于
2020年12月14日
更新于
2022年11月21日
许可协议