0%

构建离线交易

本文是在之前的复杂交易的基础上进行的,离线签名主要是为了满足以下场景

我们都知道,正常的交易需要确认6次以后才可以被视作可信(confirm)交易。当我们需要从A到B再到C连续交易时,如果离线签名不存在,则必须在A到B的交易被确认后,再发起一次B到C的交易。而离线签名可以使我们做到A到B的交易在广播之前就立刻发起一个下一步交易,之后再进行广播。

按照之前惯例,列出离线签名的主干步骤

  1. 发起第一步交易,完成对交易的签名
  2. 在第一步交易未广播之前,发起第二笔交易
  3. 依次广播第一次交易和第二交易,完成整个交易过程

以上的交易中,提取UXTO,发起交易,签名的步骤和之前列出的交易类型是一致的,不可省略。下面列出发起离线签名的步骤,其中18步之前和前文的构建复杂交易是一致的,之后的步骤是发起离线签名的过程

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
1. 启动测试链
bitcoind -printtoconsole -regtest

2. 查询链信息
bitcoin-cli -regtest -getinfo

3. 生成地址 2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9
bitcoin-cli -regtest getnewaddress

4. 导出私钥备用 cUBZgraadC4HdjCf5px1QT9cLENgF6RPGkXfqaS7TivpCtAUctMb
bitcoin-cli -regtest dumpprivkey 2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9

5. 给这个地址挖矿 101块
bitcoin-cli -regtest generatetoaddress 101 2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9

6. 生成第二个地址 2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n
bitcoin-cli -regtest getnewaddress

7. 导出私钥 cQne7yVqswhrC43N3DushL6XzzejHBkBwzrEM8ydeRcR7MTBmPV9
bitcoin-cli -regtest dumpprivkey 2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n

8. 挖矿 102块
bitcoin-cli -regtest generatetoaddress 102 2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n

9. 生成地址作为输出 2NDkJxccs9Wd6cctvKSBtfk2Nza2EeavS1Y
bitcoin-cli -regtest getnewaddress

10. 继续生成 2MxXvWg5KqLUHWQfm4z8rZhGj16aAqPWF6y
bitcoin-cli -regtest getnewaddress

11. 提取UXTO
bitcoin-cli -regtest listunspent 6 9999999 "[\"2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9\"]"

{
"txid": "eef9c597e33f507bc8fd8a9e883bfd5843986b5cc4fb56ff688735f44b2e8539",
"vout": 0,
"address": "2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9",
"label": "",
"redeemScript": "001482d55d9dff1765974f91aa49ca8390bc3da3d761",
"scriptPubKey": "a91420efe7d679e1ebfa932860265e6ae69ac0087b3987",
"amount": 50.00000000,
"confirmations": 182,
"spendable": true,
"solvable": true,
"desc": "sh(wpkh([bec83093/0'/0'/29']02c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a88))#x6wqjynz",
"safe": true
},
{
"txid": "33043fc71a74c6436f5672696e8bf0c4b809fd72f5d69d698bf406415a8d6f3b",
"vout": 0,
"address": "2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9",
"label": "",
"redeemScript": "001482d55d9dff1765974f91aa49ca8390bc3da3d761",
"scriptPubKey": "a91420efe7d679e1ebfa932860265e6ae69ac0087b3987",
"amount": 50.00000000,
"confirmations": 196,
"spendable": true,
"solvable": true,
"desc": "sh(wpkh([bec83093/0'/0'/29']02c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a88))#x6wqjynz",
"safe": true
}


14. 提取UTXO 只列举一小部分
bitcoin-cli -regtest listunspent 6 9999999 "[\"2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n\"]"

[
{
"txid": "be12c06ce23a174d29a507b09a5bc86ef38fc0ad5467e2083c9d371f0928a5d5",
"vout": 0,
"address": "2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n",
"label": "",
"redeemScript": "0014044267290a1ed9e1b363bfdec43aac1df8f85bfb",
"scriptPubKey": "a914aa848392f25d6672385271ef62b80fc03ced47d287",
"amount": 50.00000000,
"confirmations": 101,
"spendable": true,
"solvable": true,
"desc": "sh(wpkh([bec83093/0'/0'/30']030541bdfff7b3e4deb1e0addf5715cbbb13407e7b06a9793a1be36233b7d49fdc))#px0ff4zh",
"safe": true
},
{
"txid": "a3ef346a4f86ea82ac4b78c2b60f5be3b6c2b21ab0fbcecdfd87acc3b5c439e2",
"vout": 0,
"address": "2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n",
"label": "",
"redeemScript": "0014044267290a1ed9e1b363bfdec43aac1df8f85bfb",
"scriptPubKey": "a914aa848392f25d6672385271ef62b80fc03ced47d287",
"amount": 50.00000000,
"confirmations": 102,
"spendable": true,
"solvable": true,
"desc": "sh(wpkh([bec83093/0'/0'/30']030541bdfff7b3e4deb1e0addf5715cbbb13407e7b06a9793a1be36233b7d49fdc))#px0ff4zh",
"safe": true
}
]


第一组
txid: eef9c597e33f507bc8fd8a9e883bfd5843986b5cc4fb56ff688735f44b2e8539
vout: 0
address: 2MvFP1Mf8Y7v3VsoUtPi4FqA39eNySyKhK9
privkey: cUBZgraadC4HdjCf5px1QT9cLENgF6RPGkXfqaS7TivpCtAUctMb

第二组:
txid: be12c06ce23a174d29a507b09a5bc86ef38fc0ad5467e2083c9d371f0928a5d5
vout: 0
address: 2N8nqc8HxKaziqbJR3T9KotZkb5unYq7R5n
privkey: cQne7yVqswhrC43N3DushL6XzzejHBkBwzrEM8ydeRcR7MTBmPV9

目标地址
2NDkJxccs9Wd6cctvKSBtfk2Nza2EeavS1Y

2MxXvWg5KqLUHWQfm4z8rZhGj16aAqPWF6y

15 构建交易 给每个目标49.99个比特币
bitcoin-cli -regtest createrawtransaction '''
[
{
"txid": "eef9c597e33f507bc8fd8a9e883bfd5843986b5cc4fb56ff688735f44b2e8539",
"vout": 0
},
{
"txid": "be12c06ce23a174d29a507b09a5bc86ef38fc0ad5467e2083c9d371f0928a5d5",
"vout": 0
}
]
''' '''
{
"2NDkJxccs9Wd6cctvKSBtfk2Nza2EeavS1Y": 49.99,
"2MxXvWg5KqLUHWQfm4z8rZhGj16aAqPWF6y": 49.99
}'''

020000000239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee0000000000ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000000ffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48700000000

16. 现在一次进行签名 这里使用私钥进行签名
bitcoin-cli -regtest signrawtransactionwithkey "020000000239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee0000000000ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000000ffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48700000000" "[\"cUBZgraadC4HdjCf5px1QT9cLENgF6RPGkXfqaS7TivpCtAUctMb\"]"

{
"hex": "0200000000010239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee000000001716001482d55d9dff1765974f91aa49ca8390bc3da3d761ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000000ffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48702473044022074136f51a9f5dfc5b27e4e0b2a5b576f895f3e872f16bb74441b921ed670f7dd0220276d9f4fc40a1c0349008ae82135651077932010b5e9077bf62f8bc0b9142c33012102c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a880000000000",
"complete": false,
"errors": [
{
"txid": "be12c06ce23a174d29a507b09a5bc86ef38fc0ad5467e2083c9d371f0928a5d5",
"vout": 0,
"witness": [
],
"scriptSig": "",
"sequence": 4294967295,
"error": "Unable to sign input, invalid stack size (possibly missing key)"
}
]
}

17 2签
bitcoin-cli -regtest signrawtransactionwithkey "0200000000010239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee000000001716001482d55d9dff1765974f91aa49ca8390bc3da3d761ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000000ffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48702473044022074136f51a9f5dfc5b27e4e0b2a5b576f895f3e872f16bb74441b921ed670f7dd0220276d9f4fc40a1c0349008ae82135651077932010b5e9077bf62f8bc0b9142c33012102c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a880000000000" "[\"cQne7yVqswhrC43N3DushL6XzzejHBkBwzrEM8ydeRcR7MTBmPV9\"]"

{
"hex": "0200000000010239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee000000001716001482d55d9dff1765974f91aa49ca8390bc3da3d761ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000017160014044267290a1ed9e1b363bfdec43aac1df8f85bfbffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48702473044022074136f51a9f5dfc5b27e4e0b2a5b576f895f3e872f16bb74441b921ed670f7dd0220276d9f4fc40a1c0349008ae82135651077932010b5e9077bf62f8bc0b9142c33012102c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a8802473044022053aaaa8f122a8430342d274fdd57ea8dc746cce269e099d78ce2778656406c48022003e3788c5694ce17fcd842b6685f44afe6a7151dcfa214d4198d5503134d2d210121030541bdfff7b3e4deb1e0addf5715cbbb13407e7b06a9793a1be36233b7d49fdc00000000",
"complete": true
}


18 接下来进行离线签署

1. decode
bitcoin-cli -regtest decoderawtransaction 0200000000010239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee000000001716001482d55d9dff1765974f91aa49ca8390bc3da3d761ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000017160014044267290a1ed9e1b363bfdec43aac1df8f85bfbffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48702473044022074136f51a9f5dfc5b27e4e0b2a5b576f895f3e872f16bb74441b921ed670f7dd0220276d9f4fc40a1c0349008ae82135651077932010b5e9077bf62f8bc0b9142c33012102c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a8802473044022053aaaa8f122a8430342d274fdd57ea8dc746cce269e099d78ce2778656406c48022003e3788c5694ce17fcd842b6685f44afe6a7151dcfa214d4198d5503134d2d210121030541bdfff7b3e4deb1e0addf5715cbbb13407e7b06a9793a1be36233b7d49fdc00000000

{
"txid": "593bc8b6149fc64d31d70d58d365764fc362fd2aecf6adabb6c2be3964682389",
"hash": "47d4757e05dffdd2a24ca98134ee4aa87d00521ac5237cdd8cbfbf475b6ef870",
"version": 2,
"size": 418,
"vsize": 256,
"weight": 1024,
"locktime": 0,
"vin": [
{
"txid": "eef9c597e33f507bc8fd8a9e883bfd5843986b5cc4fb56ff688735f44b2e8539",
"vout": 0,
"scriptSig": {
"asm": "001482d55d9dff1765974f91aa49ca8390bc3da3d761",
"hex": "16001482d55d9dff1765974f91aa49ca8390bc3da3d761"
},
"txinwitness": [
"3044022074136f51a9f5dfc5b27e4e0b2a5b576f895f3e872f16bb74441b921ed670f7dd0220276d9f4fc40a1c0349008ae82135651077932010b5e9077bf62f8bc0b9142c3301",
"02c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a88"
],
"sequence": 4294967295
},
{
"txid": "be12c06ce23a174d29a507b09a5bc86ef38fc0ad5467e2083c9d371f0928a5d5",
"vout": 0,
"scriptSig": {
"asm": "0014044267290a1ed9e1b363bfdec43aac1df8f85bfb",
"hex": "160014044267290a1ed9e1b363bfdec43aac1df8f85bfb"
},
"txinwitness": [
"3044022053aaaa8f122a8430342d274fdd57ea8dc746cce269e099d78ce2778656406c48022003e3788c5694ce17fcd842b6685f44afe6a7151dcfa214d4198d5503134d2d2101",
"030541bdfff7b3e4deb1e0addf5715cbbb13407e7b06a9793a1be36233b7d49fdc"
],
"sequence": 4294967295
}
],
"vout": [
{
"value": 49.99000000,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 e0e2ba743a50d6c51176cf4006a73b07c29df3cf OP_EQUAL",
"hex": "a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NDkJxccs9Wd6cctvKSBtfk2Nza2EeavS1Y"
]
}
},
{
"value": 49.99000000,
"n": 1,
"scriptPubKey": {
"asm": "OP_HASH160 3a011cad3e72170448f61524c490ce1ec3c7e8a4 OP_EQUAL",
"hex": "a9143a011cad3e72170448f61524c490ce1ec3c7e8a487",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2MxXvWg5KqLUHWQfm4z8rZhGj16aAqPWF6y"
]
}
}
]
}


从以上信息中提取一组有效信息

从vout中选择第二组数据

UTXO_TXID = 593bc8b6149fc64d31d70d58d365764fc362fd2aecf6adabb6c2be3964682389
UTXO_VOUT = 1
UTXO_VALUE = 49.99000000
UTXO_OUTPUT_SCRIPT = a9143a011cad3e72170448f61524c490ce1ec3c7e8a487

2. 生成一个新的目标地址 2N7hMdx8XoKMyBJd4PLXZUvEa3szWLK7c4j
bitcoin-cli -regtest getnewaddress

3. 从以上的有效信息中构建一笔交易给新的地址49.98个比特币
bitcoin-cli -regtest createrawtransaction '''
[
{
"txid": "593bc8b6149fc64d31d70d58d365764fc362fd2aecf6adabb6c2be3964682389",
"vout": '1'
}
]
''' '''
{
"2N7hMdx8XoKMyBJd4PLXZUvEa3szWLK7c4j": 49.98
}'''

02000000018923686439bec2b6abadf6ec2afd62c34f7665d3580dd7314dc69f14b6c83b590100000000ffffffff01806de7290100000017a9149e82f61edb11d83f62a60368868a7a16bcf55a888700000000

4. 离线签名
bitcoin-cli -regtest signrawtransactionwithwallet 02000000018923686439bec2b6abadf6ec2afd62c34f7665d3580dd7314dc69f14b6c83b590100000000ffffffff01806de7290100000017a9149e82f61edb11d83f62a60368868a7a16bcf55a888700000000 '''

{
"hex": "020000000001018923686439bec2b6abadf6ec2afd62c34f7665d3580dd7314dc69f14b6c83b59010000001716001490c66a16ddd8405227e11efc87f7b93fce151373ffffffff01806de7290100000017a9149e82f61edb11d83f62a60368868a7a16bcf55a88870247304402203665a815716ed7c99a627acf896806101e851a9122a05a9dd337a7c70792800d022070b1a1c1947bbb81aff11e966392a4b05e3d4b2785a8170f70b4d0250f960e72012103d678d4c116ea3bff2b18760a45d86fd19692c213b217f88a6815006387290cfc00000000",
"complete": true
}

5. 广播之前的交易 593bc8b6149fc64d31d70d58d365764fc362fd2aecf6adabb6c2be3964682389
bitcoin-cli -regtest sendrawtransaction 0200000000010239852e4bf4358768ff56fbc45c6b984358fd3b889e8afdc87b503fe397c5f9ee000000001716001482d55d9dff1765974f91aa49ca8390bc3da3d761ffffffffd5a528091f379d3c08e26754adc08ff36ec85b9ab007a5294d173ae26cc012be0000000017160014044267290a1ed9e1b363bfdec43aac1df8f85bfbffffffff02c0aff6290100000017a914e0e2ba743a50d6c51176cf4006a73b07c29df3cf87c0aff6290100000017a9143a011cad3e72170448f61524c490ce1ec3c7e8a48702473044022074136f51a9f5dfc5b27e4e0b2a5b576f895f3e872f16bb74441b921ed670f7dd0220276d9f4fc40a1c0349008ae82135651077932010b5e9077bf62f8bc0b9142c33012102c7a49c3f29e63cf274819490e6e0bdc115756397ca60a86091f822286f5e9a8802473044022053aaaa8f122a8430342d274fdd57ea8dc746cce269e099d78ce2778656406c48022003e3788c5694ce17fcd842b6685f44afe6a7151dcfa214d4198d5503134d2d210121030541bdfff7b3e4deb1e0addf5715cbbb13407e7b06a9793a1be36233b7d49fdc00000000

6. 广播离线签名后的交易 9447820947c7fa1198a6342bbd06c22459a94e663a11aa2933d89f76e8597a80
bitcoin-cli -regtest sendrawtransaction 020000000001018923686439bec2b6abadf6ec2afd62c34f7665d3580dd7314dc69f14b6c83b59010000001716001490c66a16ddd8405227e11efc87f7b93fce151373ffffffff01806de7290100000017a9149e82f61edb11d83f62a60368868a7a16bcf55a88870247304402203665a815716ed7c99a627acf896806101e851a9122a05a9dd337a7c70792800d022070b1a1c1947bbb81aff11e966392a4b05e3d4b2785a8170f70b4d0250f960e72012103d678d4c116ea3bff2b18760a45d86fd19692c213b217f88a6815006387290cfc00000000


以上的过程已经非常详尽了,现在对以上的过程进一步说明。

文章列出的步骤是满足以下场景的:

有两组地址,分别属于不同的私钥,从这两组地址中提取两个UTXO,输入到两个目标地址中,给每个目标地址49.9个比特币。这个过程称为第一次交易,在第一次交易广播之前,立刻发起第二笔交易。对第二笔交易签名完成之后,依次对这两个交易进行广播。

在步骤18之前,和前面文章中的构建复杂交易是完全一样的。为了简单降低文章的复杂度,并没有设置找零,如需设置找零,参考之前的文章,在创建交易之后设置找零地址和计算交易费即可。

再强调一遍,离线签署的重点是两个

  1. 在第一次交易未广播之前发起第二笔交易
  2. 在交易的最后,依次广播

额外提示,如果最后发现交易无法广播,请回头查阅签名问题。