自定義BTC找零地址

使用命令 fundrawtransaction 設置changeAddress

fundrawtransaction "hexstring" ( options )

Add inputs to a transaction until it has enough in value to meet its out value.
This will not modify existing inputs, and will add at most one change output to the outputs.
No existing outputs will be modified unless "subtractFeeFromOutputs" is specified.
Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.
The inputs added will not be signed, use signrawtransaction for that.
Note that all existing inputs must have their previous output transaction be in the wallet.
Note that all inputs selected must be of standard form and P2SH scripts must be
in the wallet using importaddress or addmultisigaddress (to calculate fees).
You can see whether this is the case by checking the "solvable" field in the listunspent output.
Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only

Arguments:
1. "hexstring"           (string, required) The hex string of the raw transaction
2. options                 (object, optional)
   {
     "changeAddress"          (string, optional, default pool address) The bitcoin address to receive the change
     "changePosition"         (numeric, optional, default random) The index of the change output
     "includeWatching"        (boolean, optional, default false) Also select inputs which are watch only
     "lockUnspents"           (boolean, optional, default false) Lock selected unspent outputs
     "feeRate"                (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (BTC per KB)
     "subtractFeeFromOutputs" (array, optional) A json array of integers.
                              The fee will be equally deducted from the amount of each specified output.
                              The outputs are specified by their zero-based index, before any change output is added.
                              Those recipients will receive less bitcoins than you enter in their corresponding amount field.
                              If no outputs are specified here, the sender pays the fee.
                                  [vout_index,...]
     "replaceable"            (boolean, optional) Marks this transaction as BIP125 replaceable.
                              Allows this transaction to be replaced by a transaction with higher fees
     "conf_target"            (numeric, optional) Confirmation target (in blocks)
     "estimate_mode"          (string, optional, default=UNSET) The fee estimate mode, must be one of:
         "UNSET"
         "ECONOMICAL"
         "CONSERVATIVE"
   }
                         for backward compatibility: passing in a true instead of an object will result in {"includeWatching":true}

Result:
{
  "hex":       "value", (string)  The resulting raw transaction (hex-encoded string)
  "fee":       n,         (numeric) Fee in BTC the resulting transaction pays
  "changepos": n          (numeric) The position of the added change output, or -1
}

Examples:

Create a transaction with no inputs
> bitcoin-cli createrawtransaction "[]" "{\"myaddress\":0.01}"

Add sufficient unsigned inputs to meet the output value
> bitcoin-cli fundrawtransaction "rawtransactionhex"

Sign the transaction
> bitcoin-cli signrawtransaction "fundedtransactionhex"

Send the transaction
> bitcoin-cli sendrawtransaction "signedtransactionhex"
 (code -1)
 

源碼:

https://github.com/bitcoin/bitcoin/blob/d799efe21432ad55c41d5315f24c002bc8b3d119/src/wallet/rpcwallet.cpp

 

bitcoin rpc命令、源碼剖析:

https://mistydew.github.io/blog/category.html#Blockchain-ref

 

用法示例:

先創建一筆沒有輸入的交易,
添加充足的未簽名的輸入用以滿足輸出金額,
簽名交易,
發送交易。

$ bitcoin-cli createrawtransaction "[]" "{\"1Mcg7MDBD38sSScsX3USbsCnkcMbPnLyTV\":0.01}"
01000000000140420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000
$ bitcoin-cli decoderawtransaction 01000000000140420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000
{
  "txid": "b88e6b7777cbcca8f316fde34ab27917f62734c127a218887f032884751a7a94",
  "size": 44,
  "version": 1,
  "locktime": 0,
  "vin": [
  ],
  "vout": [
    {
      "value": 0.01000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 e221b8a504199bec7c5fe8081edd011c36531182 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914e221b8a504199bec7c5fe8081edd011c3653118288ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1Mcg7MDBD38sSScsX3USbsCnkcMbPnLyTV"
        ]
      }
    }
  ]
}
$ bitcoin-cli fundrawtransaction 01000000000140420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000
{
  "hex": "01000000014412ee17ce40921719443ac884702a5100db85a98fa0eea49fe185c36aab546e0100000000feffffff02188de605000000001976a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac40420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000",
  "changepos": 0,
  "fee": 0.00004520
}
$ bitcoin-cli decoderawtransaction 01000000014412ee17ce40921719443ac884702a5100db85a98fa0eea49fe185c36aab546e0100000000feffffff02188de605000000001976a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac40420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000
{
  "txid": "17b65e7c94c81e71e03e763051c85e94dc74f44f385b61bb0c60391717ee5cdb",
  "size": 119,
  "version": 1,
  "locktime": 0,
  "vin": [
    {
      "txid": "6e54ab6ac385e19fa4eea08fa985db00512a7084c83a4419179240ce17ee1244",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.98995480,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 41068d02c7c981b7a7ac4f4c2f28b480a76a66c1 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "16vpmdSDaX3Nv9UMuk2vSecMrdstjjSP4R"
        ]
      }
    }, 
    {
      "value": 0.01000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 e221b8a504199bec7c5fe8081edd011c36531182 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914e221b8a504199bec7c5fe8081edd011c3653118288ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1Mcg7MDBD38sSScsX3USbsCnkcMbPnLyTV"
        ]
      }
    }
  ]
}
$ bitcoin-cli signrawtransaction 01000000014412ee17ce40921719443ac884702a5100db85a98fa0eea49fe185c36aab546e0100000000feffffff02188de605000000001976a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac40420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000
{
  "hex": "01000000014412ee17ce40921719443ac884702a5100db85a98fa0eea49fe185c36aab546e010000006b483045022100c49f5b0ff43f2b35266c6944b042bbaef667c3504c556a915636930d857465e1022041421645d9fb25b154c5356508d08f69054a57cfd0f9f9a60975855de62167430121029add8d65b91a60990c1ca7e45d8239631511a217cd23521054727c63ff1ff7c0feffffff02188de605000000001976a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac40420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000",
  "complete": true
}
$ bitcoin-cli sendrawtransaction 01000000014412ee17ce40921719443ac884702a5100db85a98fa0eea49fe185c36aab546e010000006b483045022100c49f5b0ff43f2b35266c6944b042bbaef667c3504c556a915636930d857465e1022041421645d9fb25b154c5356508d08f69054a57cfd0f9f9a60975855de62167430121029add8d65b91a60990c1ca7e45d8239631511a217cd23521054727c63ff1ff7c0feffffff02188de605000000001976a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac40420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000
cd92e2a951d5624355fff82288d28cd4d213a711f7ddb10fcdd22bc6fa16801f
$ bitcoin-cli getrawtransaction cd92e2a951d5624355fff82288d28cd4d213a711f7ddb10fcdd22bc6fa16801f 1
{
  "hex": "01000000014412ee17ce40921719443ac884702a5100db85a98fa0eea49fe185c36aab546e010000006b483045022100c49f5b0ff43f2b35266c6944b042bbaef667c3504c556a915636930d857465e1022041421645d9fb25b154c5356508d08f69054a57cfd0f9f9a60975855de62167430121029add8d65b91a60990c1ca7e45d8239631511a217cd23521054727c63ff1ff7c0feffffff02188de605000000001976a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac40420f00000000001976a914e221b8a504199bec7c5fe8081edd011c3653118288ac00000000",
  "txid": "cd92e2a951d5624355fff82288d28cd4d213a711f7ddb10fcdd22bc6fa16801f",
  "size": 226,
  "version": 1,
  "locktime": 0,
  "vin": [
    {
      "txid": "6e54ab6ac385e19fa4eea08fa985db00512a7084c83a4419179240ce17ee1244",
      "vout": 1,
      "scriptSig": {
        "asm": "3045022100c49f5b0ff43f2b35266c6944b042bbaef667c3504c556a915636930d857465e1022041421645d9fb25b154c5356508d08f69054a57cfd0f9f9a60975855de6216743[ALL] 029add8d65b91a60990c1ca7e45d8239631511a217cd23521054727c63ff1ff7c0",
        "hex": "483045022100c49f5b0ff43f2b35266c6944b042bbaef667c3504c556a915636930d857465e1022041421645d9fb25b154c5356508d08f69054a57cfd0f9f9a60975855de62167430121029add8d65b91a60990c1ca7e45d8239631511a217cd23521054727c63ff1ff7c0"
      },
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.98995480,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 41068d02c7c981b7a7ac4f4c2f28b480a76a66c1 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a91441068d02c7c981b7a7ac4f4c2f28b480a76a66c188ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "16vpmdSDaX3Nv9UMuk2vSecMrdstjjSP4R"
        ]
      }
    }, 
    {
      "value": 0.01000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 e221b8a504199bec7c5fe8081edd011c36531182 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914e221b8a504199bec7c5fe8081edd011c3653118288ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1Mcg7MDBD38sSScsX3USbsCnkcMbPnLyTV"
        ]
      }
    }
  ]
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章