简体   繁体   English

以下是如何使用 Bitcoin-cli 命令发送原始交易 BTC

[英]Here's how to send raw transaction BTC using Bitcoin-cli command

Scenario:场景:

Michael receives 0.05000000 BTC from Pablo and another 0.01000000 BTC from Kuradang. Michael 从 Pablo 收到 0.05000000 BTC,从 Kuradang 收到另外 0.01000000 BTC。 Michael also wants to send 0.02500000 BTC to Berteng. Michael 还想向 Berteng 发送 0.02500000 BTC。 Each amount that Michael receives has the corresponding txid and other details. Michael 收到的每笔金额都有相应的 txid 和其他详细信息。 Lets check that out using listunspent command then create a raw transaction using createrawtransaction after that sign it using signrawtransaction and send that raw transaction using sendrawtransaction .让我们检查出使用listunspent命令然后使用创建一个原始交易createrawtransaction用它那标志后signrawtransaction和使用发送原始交易sendrawtransaction

Given:鉴于:

  • Michael's Bitcoin Address: mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe Michael 的比特币地址:mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe
  • Berteng's Bitcoin Address: mxh3H416KCRoBDiweSESew5YJyAk1nxLrN Berteng 的比特币地址:mxh3H416KCRoBDiweSESew5YJyAk1nxLrN
  • Send to Berteng: 0.02500000 mBTC发送到 Berteng:0.02500000 mBTC

Step 1)步骤 1)

listunspent

Format:格式:

$ bitcoin-cli listunspent [misconf=1] [max_number_confirmation=99999999] '''["<wallet_address>"]'''

Lets check Michael's list of unspent using with his address.让我们用他的地址检查迈克尔的未使用清单。

Execute:执行:

$ bitcoin-cli listunspent 1 99999999 '''["mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe"]'''

Result:结果:

[{
    "txid": "12b8e7ede4992f4d30f93idj3085746951d945e39f40becebd7c290af8c2e7ad",
    "vout": 1,
    "address": "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe",
    "account": "micz",
    "scriptPubKey": "76a9143aa28e1740a6a5a2190975b6e7f1ad67aaec9a3988ac",
    "amount": 0.05000000,
    "confirmations": 94,
    "spendable": true
}, {
    "txid": "8443bc63b65d569ff9ekwm37sy3b67b9c7c6f8f386c3cdf372b260961b64ec9fc",
    "vout": 1,
    "address": "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe",
    "account": "micz",
    "scriptPubKey": "76a9143aa28e1740a6a5a2190975b6e7f1ad67aaec9a3988ac",
    "amount": 0.01000000,
    "confirmations": 93,
    "spendable": true
}]

What we see here is the results that assigned 50 and 10 mBTC to our address mkrz….我们在这里看到的是将 50 和 10 mBTC 分配给我们的地址 mkrz 的结果...... To spend this output we will create a new transaction.为了花费这个输出,我们将创建一个新的交易。

Step 2)步骤 2)

createrawtransaction

We need to choose some blocks with sufficient amount from the result of listunspent .我们需要从listunspent的结果中选择一些足够数量的listunspent Since we only need to send 0.02500000 mBTC I think the first block has the enough amount to make the transaction.由于我们只需要发送 0.02500000 mBTC,我认为第一个区块有足够的金额进行交易。

Format:格式:

$ bitcoin-cli createrawtransaction
    '[{
        "txid" : "<txid_of_selected_block>",
        "vout" : <vout>
    }]'
    '{"<recipient_address>": <amount_to_send>, "<sender_address>": <amount_change>}'

To pay the fee, we will reduce the change output by 0.5 millibits as you can see below.为了支付费用,我们将更改输出减少 0.5 毫比特,如下所示。

Execute:执行:

$ bitcoin-cli createrawtransaction
    '[{
        "txid" : "12b8e7ede4992f4d30f93idj3085746951d945e39f40becebd7c290af8c2e7ad",
        "vout" : 0
    }]'
    '{"mxh3H416KCRoBDiweSESew5YJyAk1nxLrN": 0.025, "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe": 0.0245}'

Result结果

0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c0000000000ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000

The createrawtransaction command produces a raw hex string that encodes the transaction details we supplied. createrawtransaction 命令生成一个原始的十六进制字符串,对我们提供的交易细节进行编码。 If you want to decode the hex just use decoderawtransaction command.如果您想解码十六进制,只需使用decodeawtransaction 命令。

Step 3)步骤 3)

signrawtransaction

signs the transaction in the serialized transaction format using private keys stored in the wallet or provided in the call.使用存储在钱包中或在调用中提供的私钥以序列化交易格式签署交易。

Format:格式:

$ bitcoin-cli signrawtransaction <hex_createrawtransaction>

Execute:执行:

$ bitcoin-cli signrawtransaction 0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c0000000000ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000

Result:结果:

{
   "hex" : "0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c000000006a47304402203e8a16522da80cef66bacfbc0c800c6d52c4a26d1d86a54e0a1b76d661f020c9022010397f00149f2a8fb2bc5bca52f2d7a7f87e3897a273ef54b277e4af52051a06012103c9700559f690c4a9182faa8bed88ad8a0c563777ac1d3f00fd44ea6c71dc5127ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000",
   "complete" : true
}

Now the signrawtransaction command returns another hex-encoded raw transaction.现在 signrawtransaction 命令返回另一个十六进制编码的原始交易。

Step 4)步骤 4)

sendrawtransaction

RPC validates a transaction and broadcasts it to the peer-to-peer network. RPC 验证交易并将其广播到对等网络。

Format:格式:

$ bitcoin-cli sendrawtransaction <hex_signrawtransaction>

Execute:执行:

$ bitcoin-cli sendrawtransaction 0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c000000006a47304402203e8a16522da80cef66bacfbc0c800c6d52c4a26d1d86a54e0a1b76d661f020c9022010397f00149f2a8fb2bc5bca52f2d7a7f87e3897a273ef54b277e4af52051a06012103c9700559f690c4a9182faa8bed88ad8a0c563777ac1d3f00fd44ea6c71dc5127ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000

Result:结果:

ae74538baa914f3799081ba78429d5d84f36a0127438e9f721dff584ac17b346

The command sendrawtransaction returns a transaction hash (txid) as it submits the transaction on the network.命令 sendrawtransaction 在网络上提交交易时返回交易哈希 (txid)。 To check the transaction hash you can go to tbtc.blockr.io要检查交易哈希,您可以访问tbtc.blockr.io

For more info see this site mastering bitcoin有关更多信息,请参阅此网站 掌握比特币

Multisig implementation of send rawtransaction in regtest environment) regtest环境下send rawtransaction的Multisig实现)

1)Create a rawtransaction 1)创建一个原始交易

syntax:语法:

bitcoin-cli -regtest createrawtransaction '[{"txid":"","vout":}]' '{"receive_address":amount}' bitcoin-cli -regtest createrawtransaction '[{"txid":"","vout":}]' '{"receive_address":amount}'

Example:示例:

 > bitcoin-cli -regtest createrawtransaction
    > '[{"txid":"eee0de90e9878c039f87c9eedbdf5b9a5da157b19e5354a51ff3b2f84c8a901b","vout":0},]'
    > '{"2MxieCJNTKiiBj6U3SjghQaatZYbM7Qn6GW":30}'

2)Sign the raw transaction 2)签署原始交易

syntax语法

<hexstring> [{"txid":txid,"vout":n,"scriptPubKey":hex},...] [<privatekey1>,...]
bitcoin-cli -regtest signrawtransaction "0200000001a1c33ebb12d94f96effc70c9dd4488faf32dc15269d6a41d115ac956033cadbb0100000000ffffffff0100e1f505000000001976a9145768a869521bb01af8bdd787e6e1e65ec3d9870e88ac00000000" '''
        [
          {
            "txid":"bbad3c0356c95a111da4d66952c12df3fa8844ddc970fcef964fd912bb3ec3a1",
            "vout":1,
            "scriptPubKey":"a91401a8f0509da2396f58d3f9948a76331964524c9687",
            "redeemScript": "5221022d6f957dd76773432d2493edb5601b2d0791286e31c83483ab299672c3d44fc921022dd9c36eece99cc308986a8334c3a0bf24d7ef8b842dacebf56f2477b0f6daab21030544c9613bf27c9773e6fcd79c7786a297188a2647d346da0c3305e22d3e28da53ae"
          }
        ] 
    ''' '''
    [
    "cUdhE3NvePyjqksgJTc5BQfXGDJckPgEKfoRT72x2BRoG2uMS81H"
    ]
    '''

o/p will be a hex which i store as o/p 将是我存储为的十六进制

signed_raw_txn= signed_raw_txn=

3)Send rawtransaction 3)发送原始交易

bitcoin-cli -regtest sendrawtransaction $signed_raw_txn

Yet another simpler way with some extra fundrawtransaction .另一种更简单的方法是一些额外的fundrawtransaction Docs say us what inputs and output all required fields.文档告诉我们什么输入和输出所有必填字段。 But don't forget empty list is object too:但不要忘记空列表也是对象:

Step 1)步骤 1)

$ bitcoin-cli createrawtransaction '[]' \
    '{"mxh3H416KCRoBDiweSESew5YJyAk1nxLrN": 0.025}'

Step 2)步骤 2)

$ bitcoin-cli fundrawtransaction <hex_createrawtransaction> \
    '{"changeAddress": "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe"}'

After second step you can look at the fee generated by your wallet.第二步后,您可以查看钱包产生的费用。 And change it with feeRate option of fundrawtransaction command.并使用fundrawtransaction命令的feeRate选项更改它。 This all can be done before sign and send to network your transaction.这一切都可以在签署并将您的交易发送到网络之前完成。

I don't have 50 rep points to be able to comment to user3931836, but regarding "how does it generate the fee? What's its priority? Does network congestion affect the fee?"我没有 50 个代表点可以对 user3931836 发表评论,但是关于“它如何产生费用?它的优先级是什么?网络拥塞会影响费用吗?”

  1. The fee is whatever amount is left over after you subtract the total output amounts from the total UTXO input amounts:费用是从总 UTXO 输入量中减去总输出量后剩余的任何金额:

fee = (sum of UTXO) - (sum of total being sent)费用 =(UTXO 的总和)-(发送的总和)

ie the fee amount is not overtly specified;即没有明确规定费用金额; the miner simply keeps whatever is unspecified in the output(s), the leftover amount.矿工只是保留输出中未指定的任何内容,即剩余数量。 So you should reduce your "change" output to ensure there is enough to cover the fee.所以你应该减少你的“找零”输出,以确保有足够的钱来支付费用。

  1. The fee is neither high priority nor low priority.费用既不是高优先级也不是低优先级。 The transaction itself become high priority (to miners) by you opting to leave a higher fee amount to the miner (miners are greedy and choose the transactions that pay more - which the payer themselves chooses).通过您选择向矿工留下更高的费用金额(矿工很贪婪并选择支付更多的交易 - 付款人自己选择),交易本身成为(对矿工)的高优先级。
  2. The fee is based on the amount of data you are sending in the transaction.费用基于您在交易中发送的数据量。 The miner looks at the number of bytes in your transaction, then the fee you are paying for the transaction, then computes the fee/byte to prioritise the transaction.矿工查看您交易中的字节数,然后是您为交易支付的费用,然后计算费用/字节以优先处理交易。

Remember, if your transaction uses 50 UTXOs, then it has a LOT of bytes so they will expect a large payment.请记住,如果您的交易使用 50 个 UTXO,那么它有很多字节,因此他们会期待大笔付款。 But if your transaction uses only 1 UTXO, then it uses a LOT fewer bytes, so you can pay less.但是,如果您的交易仅使用 1 个 UTXO,那么它使用的字节数就会少很多,因此您可以支付更少的费用。 Make sure you optimise your selection of UTXOs to reduce the number of bytes in the transaction - then you can provide a smaller fee without impacting its queue "priority".确保优化您对 UTXO 的选择以减少交易中的字节数 - 然后您可以提供较小的费用而不会影响其队列“优先级”。 Currently miners expected around 40 satoshis per byte, but check an online tool to see what the estimated processing times are for the various amounts.目前矿工预计每字节大约 40 satoshis,但请查看在线工具以查看各种数量的估计处理时间。 If you pay them less (per byte), then they'll leave you in the mempool for longer (as a lower "priority" in their context).如果你付给他们更少(每字节),那么他们会让你在内存池中停留更长时间(在他们的上下文中作为较低的“优先级”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM