Blockchain Nanodegree Notes 20181103

版权声明:本栏目下的所有文章均为个人学习笔记,部分内容为直接搬运,供学习分享。如有版权问题请联系作者删除。 https://blog.csdn.net/xiaozhenliu/article/details/83929232

Term 1 S3 Lesson 4: Blockchain Data

Create Raw Transaction

Motivation for Creating Transactions

To really leverage the full power of the protocol, we need to bypass the GUI and work directly with the protocol.

Motivation for the Project

  • Step 1 - View all unspent confirmed UTXO in the wallet
  • Step 2 - View Details about a Specific UTXO
  • Step 3 - Create a Raw Transaction
  • Step 4 - Decode the Raw Transaction (to doublecheck it went through correctly)
  • Step 5 - Sign the Raw Transaction
  • Step 6 - Submit the Raw Transaction to the Network
  • Step 7 - Query the TxID of the Transaction we sent

“Transaction life cycle”: Transaction - Wallet - Signature- Mempool - Network- Consensus - Hashing - block - Blockchain

Step 1

listunspent

  • txid - This shows us that there is a Transaction with this id.
  • vout - The Transaction has one output (vout at index 0).
  • address - The Transaction is assigned to this address.
  • account - This is a deprecated. It’s set to “ “ as a default.
  • scriptPubKey - The hash of the Locking Script
  • amount - Transaction amount in BTC
  • confirmations - At the time of viewing this Tx, it has been confirmed 7 times (6 blocks added after it was added to the blockchain).

Step 2

gettxout

gettxout "txid" n ( include_mempool )

Arguments:

  1. “txid” (string, required) The transaction id
  2. “n” (numeric, required) vout number
  3. “include_mempool” (boolean, optional) Whether to include the mempool. Default: true. Note that an unspent output that is spent in the mempool won’t appear.

Step 3

createrawtransaction

createrawtransaction [{"txid":"id","vout":n},...] {"address":amount,"data":"hex",...} ( locktime ) ( replaceable )

Arguments

1. "inputs"                (array, required) A json array of json objects
     [
       {
         "txid":"id",    (string, required) The transaction id
         "vout":n,         (numeric, required) The output number
         "sequence":n      (numeric, optional) The sequence number
       } 
       ,...
     ]
2. "outputs"               (object, required) a json object with outputs
    {
      "address": x.xxx,    (numeric or string, required) The key is the bitcoin address, the numeric value (can be string) is the BTC amount
      "data": "hex"      (string, required) The key is "data", the value is hex encoded data
      ,...
    }
3. locktime                  (numeric, optional, default=0) Raw locktime. Non-0 value also locktime-activates inputs
4. replaceable               (boolean, optional, default=false) Marks this transaction as BIP125 replaceable.
                             Allows this transaction to be replaced by a transaction with higher fees. If provided, it is an error if explicit sequence numbers are incompatible.
// CREATE RAW TRANSACTION
TXID = Get this from listunspent command
VOUT = Get this from listunspent command
ADDRESS = Where will this amount be sent
AMOUNT = How much BTC will be sent
createrawtransaction '[{"txid":"TXID","vout": VOUT}]''{"ADDRESS":AMOUNT}'
createrawtransaction '[{"txid": "50126e2761333ce429792e57bb756c9eaf5f51039242721316ee6e88ec10d8ac", "vout": 1}]' '{"2Mz2AhntMWhKoRiwSKRtt77XgJXstQREJZb":0.00008, "2MzJNqq3pRJuacB58wVCmuwWpse1nHUeAgQ":0.00851634}'`

0200000001acd810ec886eee161372429203515faf9e6c75bb572e7929e43c3361276e12500100000000ffffffff02401f00000000000017a9144a50bb39c22ba309f0ac1b81ad373f4886519f6c87b2fe0c000000000017a9144d6184fe9c6198131fd9020d729c53f32ef79f308700000000

(raw hex string of the tx)

Step 4. Decode Raw Transaction

decoderawtransaction"hexstring"

Step 5. Sign Raw Transaction

signrawtransaction "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex","redeemScript":"hex"},...] ["privatekey1",...] sighashtype )

Arguments

1. "hexstring"     (string, required) The transaction hex string
2. "prevtxs"       (string, optional) An json array of previous dependent transaction outputs
     [               (json array of json objects, or 'null' if none provided)
       {
         "txid":"id",             (string, required) The transaction id
         "vout":n,                  (numeric, required) The output number
         "scriptPubKey": "hex",   (string, required) script key
         "redeemScript": "hex",   (string, required for P2SH or P2WSH) redeem script
         "amount": value            (numeric, required) The amount spent
       }
       ,...
    ]
3. "privkeys"     (string, optional) A json array of base58-encoded private keys for signing
    [                  (json array of strings, or 'null' if none provided)
      "privatekey"   (string) private key in base58-encoding
      ,...
    ]
4. "sighashtype"     (string, optional, default=ALL) The signature hash type. Must be one of
       "ALL"
       "NONE"
       "SINGLE"
       "ALL|ANYONECANPAY"
       "NONE|ANYONECANPAY"
       "SINGLE|ANYONECANPAY"

Build Out Command

// SIGN RAW TRANSACTION
TRANSACTION HASH = Get from result of createrawtransaction command
TXID = Get from listunspent command
VOUT (optional) = Get from listunspent command
SCRIPT PUB KEY (optional) = Get from createrawtransaction command
PRIVATE KEY (optional) = Get from wallet
// Use the values you’ve collected above to fill in the signrawtransaction command
signrawtransaction 'TRANSACTION_HASH_HERE''[{"txid":"TXID_HERE","vout":VOUT_HERE,"scriptPubKey":"SCRIPT_PUB_KEY_HERE"}]''["PRIVATE_KEY"]'

signrawtransaction is deprecated and will be fully removed in v0.18. To use signrawtransaction in v0.17, restart bitcoind with -deprecatedrpc=signrawtransaction.
Projects should transition to using signrawtransactionwithkey and signrawtransactionwithwallet before upgrading to v0.18 (code -32)

Step 6. Send Raw Transaction

sendrawtransaction "hexstring" ( allowhighfees )

Arguments

1. "hexstring"    (string, required) The hex string of the raw transaction)
2. allowhighfees    (boolean, optional, default=false) Allow high fees

Result

"hex"             (string) The transaction hash in hex

sendrawtransaction 02000000000101acd810ec886eee161372429203515faf9e6c75bb572e7929e43c3361276e12500100000017160014ece4a6d95ac9f7cd0ace4784f66e20cb15f24be9ffffffff02401f00000000000017a9144a50bb39c22ba309f0ac1b81ad373f4886519f6c87b2fe0c000000000017a9144d6184fe9c6198131fd9020d729c53f32ef79f30870247304402203cb459580529475124f0906c0c482c2fb99e0ad1e47596f5dc03789938ad5c14022067714a9589a2e09d9362df06c807fea540e3961b7e3dc42a7ff25f604b1158f4012103a852c77aabfa72c821bffda3ba83c6c91416f874ea638cc7ff8ec4c5e65e5b9a00000000

got:

2873e08edc78cbe1b9dbf2183ac625ac946d0f17e05401d6dfc01a6ddbb403d8

(txid)

Step 7. Query the TxID

gettransaction "txid" ( include_watchonly )

1. "txid"                  (string, required) The transaction id
2. "include_watchonly"     (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[]

Summary

  • Step 1 - View all unspent confirmed UTXO in the wallet
    • listunspent - Show all the unspent confirmed outputs in our wallet)
  • Step 2 - View Details about a Specific UTXO
    • gettxout - Get the details of this unspent output above
  • Step 3 - Create a Raw Transaction
  • Step 4 - Decode the Raw Transaction (to double-check it went through correctly)
  • Step 5 - Sign the Raw Transaction
  • Step 6 - Submit the Raw Transaction to the Network
    • sendrawtransction - Takes the raw hex string produced by signrawtransaction and returns a transaction hash (txid) as it submits the transaction on the network.
  • Step 7 - Query the TxID of the Transaction we sent
    • gettransaction - Query the TxID and view details. Similar to online block explorer

Embedding Data in Bitcoin Core

OP_RETURN

  • Stores up to 40 bytes
  • Recorded in Blockchain
  • Does not bloat UTXO memory pool

猜你喜欢

转载自blog.csdn.net/xiaozhenliu/article/details/83929232