比特币RAW方式转账

一步一步实现比特币转账,已经在比特币私链进行测试

Posted by HonorJoey on March 2, 2020

介绍

转账一共分为以下几步:

  • 查询发送地址的utxo
  • 创建交易
  • 签名交易
  • 发送交易

首先我们要从地址A给地址B转比特币, 地址A

bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr

地址B

bcrt1qp9gq52muvdzdmlhdd9xtg22vz7rfd033zewz4v

PS:这里为比特币私链地址

发送方地址要是在本钱包创建的地址,如果不是,就要导入到钱包中。

导入地址
./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 importaddress bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr "test label" true
导入私钥
./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 importprivkey            cU8Q2jGeX3GNKNa5etiC8mgEgFSeVUTRQfWE2ZCzszyqYNK4Mepy "test label" true

进行转账

查询发送地址的UTXO

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 listunspent 6 9999999 '["bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr"]'

PS:6为查询的最近区块,9999999为最远区块,因为比特币网络的确认区块数为6,查询的区块号与最新区块号的距离要大于6个区块才能算作被确认的区块。上面的地址即为发送方地址,发送方地址要是在本钱包创建的地址,如果不是,就要导入到钱包中。

返回信息

[
  {
    "txid": "cd76be7e039384750e88acfb3c3a51d9b25b51252aa3c266663a6cac642373f4",
    "vout": 0,
    "address": "bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr",
    "label": "",
    "scriptPubKey": "0014f654168823a3f295f36a2dca35b13897b51bb80f",
    "amount": 50.00000000,
    "confirmations": 149,
    "spendable": true,
    "solvable": true,
    "desc": "wpkh([df77b868/0'/0'/0']0366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb176)#clkfw4rc",
    "safe": true
  },
  ...
]

选择一个余额大于转账金额的,因为还要考虑矿工费。选择一个utxo后要用到txidvoutamount

创建交易

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 createrawtransaction '''[{"txid":"cd76be7e039384750e88acfb3c3a51d9b25b51252aa3c266663a6cac642373f4","vout" : 0}]''' '''[{"bcrt1qp9gq52muvdzdmlhdd9xtg22vz7rfd033zewz4v": 10.0 },{"bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr": 39.99999 }]'''

这里的txidvout为上一步查询的utxo中的txidvout,后面分为两组地址和金额,一个是接收方地址和转账金额,另一个是找零地址和金额,这里两部分金额加起来是49.99999,比上一步得到的amount 50.000000少的0.00001即为预留的矿工费。

返回如下

0200000001f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f00000000

签名交易

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 signrawtransactionwithwallet 0200000001f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f00000000

signrawtransactionwithwallet后面就是上一笔创建交易得到的。

返回如下

{
  "hex": "02000000000101f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f0247304402202401e626f7f00f4e2cc3e5a11f846c05cc319c48f2c76dd570cf7cd72dbd7d350220352a8e524a7cfa0e9da2e5f3a24e7d9dc554e8bc545f24d058e4009622c394ea01210366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb17600000000",
  "complete": true
}

这里的hex用于后面发送交易。

发送交易

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 sendrawtransaction 02000000000101f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f0247304402202401e626f7f00f4e2cc3e5a11f846c05cc319c48f2c76dd570cf7cd72dbd7d350220352a8e524a7cfa0e9da2e5f3a24e7d9dc554e8bc545f24d058e4009622c394ea01210366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb17600000000

这里用到了上一步签名交易得到的hex

返回如下

0648acd662854e0bb6f231ccc8888207c1318e3f2b67fd2bb09f580d028c511e

返回为交易的txid

查询交易

可以查询交易的信息

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 gettransaction 0648acd662854e0bb6f231ccc8888207c1318e3f2b67fd2bb09f580d028c511e

返回如下

{
  "amount": 0.00000000,
  "fee": -0.00010000,
  "confirmations": 0,
  "trusted": true,
  "txid": "0648acd662854e0bb6f231ccc8888207c1318e3f2b67fd2bb09f580d028c511e",
  "walletconflicts": [
  ],
  "time": 1574932189,
  "timereceived": 1574932189,
  "bip125-replaceable": "no",
  "details": [
    {
      "address": "bcrt1qp9gq52muvdzdmlhdd9xtg22vz7rfd033zewz4v",
      "category": "send",
      "amount": -10.00000000,
      "label": "",
      "vout": 0,
      "fee": -0.00010000,
      "abandoned": false
    },
    {
      "address": "bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr",
      "category": "send",
      "amount": -39.99990000,
      "label": "",
      "vout": 1,
      "fee": -0.00010000,
      "abandoned": false
    },
    {
      "address": "bcrt1qp9gq52muvdzdmlhdd9xtg22vz7rfd033zewz4v",
      "category": "receive",
      "amount": 10.00000000,
      "label": "",
      "vout": 0
    },
    {
      "address": "bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr",
      "category": "receive",
      "amount": 39.99990000,
      "label": "",
      "vout": 1
    }
  ],
  "hex": "02000000000101f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f0247304402202401e626f7f00f4e2cc3e5a11f846c05cc319c48f2c76dd570cf7cd72dbd7d350220352a8e524a7cfa0e9da2e5f3a24e7d9dc554e8bc545f24d058e4009622c394ea01210366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb17600000000"
}

这里的confirmations0表示交易未被矿工打包,-1表示交易失败。

gettransaction 只能查询本钱包的交易,要查询其他交易要用getrawtransaction

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 getrawtransaction 0648acd662854e0bb6f231ccc8888207c1318e3f2b67fd2bb09f580d028c511e

需要注意,要查询非本钱包的交易,即链上的其他交易,要在启动节点是开启txindexreindex,开启方法见https://blog.csdn.net/qq_22157427/article/details/103295657 返回如下

02000000000101f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f0247304402202401e626f7f00f4e2cc3e5a11f846c05cc319c48f2c76dd570cf7cd72dbd7d350220352a8e524a7cfa0e9da2e5f3a24e7d9dc554e8bc545f24d058e4009622c394ea01210366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb17600000000

再用decoderawtransaction进行解码得到具体的交易信息。

./bitcoin-cli -rpcuser=btc -rpcpassword=btcbtc -rpcport=18443 decoderawtransaction 02000000000101f4732364ac6c3a6666c2a32a25515bb2d9513a3cfbac880e758493037ebe76cd0000000000ffffffff0200ca9a3b0000000016001409500a2b7c6344ddfeed694cb4294c178696be31f0006bee00000000160014f654168823a3f295f36a2dca35b13897b51bb80f0247304402202401e626f7f00f4e2cc3e5a11f846c05cc319c48f2c76dd570cf7cd72dbd7d350220352a8e524a7cfa0e9da2e5f3a24e7d9dc554e8bc545f24d058e4009622c394ea01210366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb17600000000

返回如下

{
  "txid": "0648acd662854e0bb6f231ccc8888207c1318e3f2b67fd2bb09f580d028c511e",
  "hash": "bd86e1d9357faa7a83707730120764d2d4db1d77a07dd538e9a1824fe4cb8940",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 0,
  "vin": [
    {
      "txid": "cd76be7e039384750e88acfb3c3a51d9b25b51252aa3c266663a6cac642373f4",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "304402202401e626f7f00f4e2cc3e5a11f846c05cc319c48f2c76dd570cf7cd72dbd7d350220352a8e524a7cfa0e9da2e5f3a24e7d9dc554e8bc545f24d058e4009622c394ea01",
        "0366adfb9c537c67710e5f38b1e0d186daad0c020c8adaa21c259bcaa242fdb176"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 10.00000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 09500a2b7c6344ddfeed694cb4294c178696be31",
        "hex": "001409500a2b7c6344ddfeed694cb4294c178696be31",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1qp9gq52muvdzdmlhdd9xtg22vz7rfd033zewz4v"
        ]
      }
    },
    {
      "value": 39.99990000,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 f654168823a3f295f36a2dca35b13897b51bb80f",
        "hex": "0014f654168823a3f295f36a2dca35b13897b51bb80f",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1q7e2pdzpr50eftum29h9rtvfcj763hwq02jjlhr"
        ]
      }
    }
  ]
}