0%

创建具有找零地址的交易

之前的文章讲了构建裸交易的过程,但是上文直接构建裸交易是没有创建找零地址的,所以输入和输出的差值都会成为矿工的交易费。极容易造成高额的交易费,所以这篇文章在之前的基础上使用 fundrawtransaction 来创建具有找零地址的交易。

先列出主干

  1. 创建一笔没有输入的交易
  2. 添加充足的未签名的的交易来满足交易需求
  3. 签名
  4. 广播

具体过程

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
// 启动私链
1. bitcoind -printtoconsole -regtest // 其余参数全部参照原来的设计,其余几个项目都去 .bitcoin/bitcoin.conf 下去掉

////先测试私链起来了没有
2. bitcoin-cli -regtest -getinfo

//在查询一下
3. bitcoin-cli -regtest getbestblockhash

//查询当前用户信息 不出所料的话都应该没有币的
4. bitcoin-cli -regtest getbalance

//生成一个地址作为初始地址 2N9nBBNFP4BFFZWzvQTwQKF96RxidHx4p8K
5. bitcoin-cli -regtest getnewaddress

//验证一下这个地址
6. bitcoin-cli -regtest validateaddress 2N9nBBNFP4BFFZWzvQTwQKF96RxidHx4p8K

// 输出
{
"isvalid": true,
"address": "2N9nBBNFP4BFFZWzvQTwQKF96RxidHx4p8K",
"scriptPubKey": "a914b55c9006466ed8bad87d9f7af0aed405764eb0a887",
"isscript": true,
"iswitness": false
}

//尝试导出这个私钥 cQUSPugSndqrg9UkVKvgeowJpptyyxHFEE5TcstjJnw8NzeUKPCg
7. bitcoin-cli -regtest dumpprivkey 2N9nBBNFP4BFFZWzvQTwQKF96RxidHx4p8K

//开始挖矿 注意这里的 api 发生了变化 以前是直接generate 现在推荐下面的方法 直接100个区块到之前生成的地址上
8. bitcoin-cli -regtest generatetoaddress 101 2N9nBBNFP4BFFZWzvQTwQKF96RxidHx4p8K

//接下来 获取当前账户下的信息 确认一百个块 得到50个比特币
9. bitcoin-cli -regtest getbalance

//再生成第二个地址 用于接受转账 2NByxgfmQxVuQXB16GY1J2ru6RGfNEdgPk3
10. bitcoin-cli -regtest getnewaddress

//先创建一笔没有输入的交易 转账给我们的目标地址35个比特币 02000000000100c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000
11. bitcoin-cli -regtest createrawtransaction '[]' '{"2NByxgfmQxVuQXB16GY1J2ru6RGfNEdgPk3":35}'

//接下来decode
12. bitcoin-cli -regtest decoderawtransaction 02000000000100c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000

输出

{
"txid": "776b54a0b65d3af5fd13ab4d947ab72e563ba7a2aa43cd37ebdd820dc50ec9ca",
"hash": "776b54a0b65d3af5fd13ab4d947ab72e563ba7a2aa43cd37ebdd820dc50ec9ca",
"version": 2,
"size": 42,
"vsize": 42,
"weight": 168,
"locktime": 0,
"vin": [
],
"vout": [
{
"value": 35.00000000,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 cd876025d3d13aa7ed999128d40100a9f26a65ea OP_EQUAL",
"hex": "a914cd876025d3d13aa7ed999128d40100a9f26a65ea87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NByxgfmQxVuQXB16GY1J2ru6RGfNEdgPk3"
]
}
}
]
}

//使用 fundrawtransaction 计算出交易费 输入为刚才得到的 raw transaction 02000000000100c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000
13. bitcoin-cli -regtest fundrawtransaction 02000000000100c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000
{
"hex": "02000000019e667f70450d80e036c5763b4ece68479a047a67efcf483f8bf2e56c50ca5f140000000000feffffff02082268590000000017a914ebec149b57c6c84ab9a5302dae8150369d9c51fd8700c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000",
"fee": 0.00003320,
"changepos": 0
}

// decoderawtransaction
14. bitcoin-cli -regtest decoderawtransaction 02000000019e667f70450d80e036c5763b4ece68479a047a67efcf483f8bf2e56c50ca5f140000000000feffffff02082268590000000017a914ebec149b57c6c84ab9a5302dae8150369d9c51fd8700c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000

{
"txid": "704bd8b218ab5dc934a6388f139659f0626a93af1d77814d34a8123590641500",
"hash": "704bd8b218ab5dc934a6388f139659f0626a93af1d77814d34a8123590641500",
"version": 2,
"size": 115,
"vsize": 115,
"weight": 460,
"locktime": 0,
"vin": [
{
"txid": "145fca506ce5f28b3f48cfef677a049a4768ce4e3b76c536e0800d45707f669e",
"vout": 0,
"scriptSig": {
"asm": "",
"hex": ""
},
"sequence": 4294967294
}
],
"vout": [
{
"value": 14.99996680,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 ebec149b57c6c84ab9a5302dae8150369d9c51fd OP_EQUAL",
"hex": "a914ebec149b57c6c84ab9a5302dae8150369d9c51fd87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NEkfbXNcp3iQtQjh4RemfTGeGQ6S8uX5kC"
]
}
},
{
"value": 35.00000000,
"n": 1,
"scriptPubKey": {
"asm": "OP_HASH160 cd876025d3d13aa7ed999128d40100a9f26a65ea OP_EQUAL",
"hex": "a914cd876025d3d13aa7ed999128d40100a9f26a65ea87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NByxgfmQxVuQXB16GY1J2ru6RGfNEdgPk3"
]
}
}
]
}

// 利用钱包的功能对这次交易进行签名
15. bitcoin-cli -regtest signrawtransactionwithwallet 02000000019e667f70450d80e036c5763b4ece68479a047a67efcf483f8bf2e56c50ca5f140000000000feffffff02082268590000000017a914ebec149b57c6c84ab9a5302dae8150369d9c51fd8700c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8700000000

{
"hex": "020000000001019e667f70450d80e036c5763b4ece68479a047a67efcf483f8bf2e56c50ca5f1400000000171600145ffd0df9539eaf3036a524bf7c41ed5864cc2334feffffff02082268590000000017a914ebec149b57c6c84ab9a5302dae8150369d9c51fd8700c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8702473044022054074aff080169dc692cb1952eb97205f7c1b7b4a2f14903ee270271fcc9b19102205fdafcafbbb466350e48d891498c1244238866bf17ac11515a1cc4b796fbfdaa0121034824cd575c0c7474fbb49c255ad81892f673f68c2025f5bacfc60bbec910b1ac00000000",
"complete": true
}

// 广播 668911f8a41113ffdbf65fd326e20c107dfd62cf3cc86959444f55be94ab78d7
16. bitcoin-cli -regtest sendrawtransaction 020000000001019e667f70450d80e036c5763b4ece68479a047a67efcf483f8bf2e56c50ca5f1400000000171600145ffd0df9539eaf3036a524bf7c41ed5864cc2334feffffff02082268590000000017a914ebec149b57c6c84ab9a5302dae8150369d9c51fd8700c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8702473044022054074aff080169dc692cb1952eb97205f7c1b7b4a2f14903ee270271fcc9b19102205fdafcafbbb466350e48d891498c1244238866bf17ac11515a1cc4b796fbfdaa0121034824cd575c0c7474fbb49c255ad81892f673f68c2025f5bacfc60bbec910b1ac00000000

//可以查看一下
17. bitcoin-cli -regtest getrawtransaction 668911f8a41113ffdbf65fd326e20c107dfd62cf3cc86959444f55be94ab78d7 1

输出
{
"txid": "668911f8a41113ffdbf65fd326e20c107dfd62cf3cc86959444f55be94ab78d7",
"hash": "53171009d8f8984d72276e6f4f19bad12476d272ba6728e8323a2bb3756c77ac",
"version": 2,
"size": 247,
"vsize": 166,
"weight": 661,
"locktime": 0,
"vin": [
{
"txid": "145fca506ce5f28b3f48cfef677a049a4768ce4e3b76c536e0800d45707f669e",
"vout": 0,
"scriptSig": {
"asm": "00145ffd0df9539eaf3036a524bf7c41ed5864cc2334",
"hex": "1600145ffd0df9539eaf3036a524bf7c41ed5864cc2334"
},
"txinwitness": [
"3044022054074aff080169dc692cb1952eb97205f7c1b7b4a2f14903ee270271fcc9b19102205fdafcafbbb466350e48d891498c1244238866bf17ac11515a1cc4b796fbfdaa01",
"034824cd575c0c7474fbb49c255ad81892f673f68c2025f5bacfc60bbec910b1ac"
],
"sequence": 4294967294
}
],
"vout": [
{
"value": 14.99996680,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 ebec149b57c6c84ab9a5302dae8150369d9c51fd OP_EQUAL",
"hex": "a914ebec149b57c6c84ab9a5302dae8150369d9c51fd87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NEkfbXNcp3iQtQjh4RemfTGeGQ6S8uX5kC"
]
}
},
{
"value": 35.00000000,
"n": 1,
"scriptPubKey": {
"asm": "OP_HASH160 cd876025d3d13aa7ed999128d40100a9f26a65ea OP_EQUAL",
"hex": "a914cd876025d3d13aa7ed999128d40100a9f26a65ea87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NByxgfmQxVuQXB16GY1J2ru6RGfNEdgPk3"
]
}
}
],
"hex": "020000000001019e667f70450d80e036c5763b4ece68479a047a67efcf483f8bf2e56c50ca5f1400000000171600145ffd0df9539eaf3036a524bf7c41ed5864cc2334feffffff02082268590000000017a914ebec149b57c6c84ab9a5302dae8150369d9c51fd8700c39dd00000000017a914cd876025d3d13aa7ed999128d40100a9f26a65ea8702473044022054074aff080169dc692cb1952eb97205f7c1b7b4a2f14903ee270271fcc9b19102205fdafcafbbb466350e48d891498c1244238866bf17ac11515a1cc4b796fbfdaa0121034824cd575c0c7474fbb49c255ad81892f673f68c2025f5bacfc60bbec910b1ac00000000"
}