Bitcoin-Core

是否存在一些用於解碼/編碼原始事務的庫?

  • September 2, 2019

我想將原始事務解析為 JSON,然後更改一些值並編碼回十六進制。像這樣的JSON:

{
"addresses": [
   "346vcZZ6QV4nr68fybMEuA7qxxQeyKWoPS"
], 
"block_height": -1, 
"block_index": -1, 
"confirmations": 0, 
"double_spend": false, 
"fees": 0, 
"hash": "e09c37cca11208a8c372a1e397dcff0382b4d78cdde157ce8824d028dc185a80", 
"inputs": [
   {
       "age": 0, 
       "output_index": 1, 
       "prev_hash": "c15bb773cdd5bb63a65766f5680684ce991c126698db0aa020e3058081742874", 
       "script_type": "empty", 
       "sequence": 4294967295
   }
], 
"outputs": [
   {
       "addresses": [
           "346vcZZ6QV4nr68fybMEuA7qxxQeyKWoPS"
       ], 
       "script": "a9141a75cd53e9740caadbf83cbefdd087cdceb978a487", 
       "script_type": "pay-to-script-hash", 
       "value": 14000000
   }
], 
"preference": "low", 
"received": "2018-08-21T08:05:02.841875145Z", 
"relayed_by": "54.160.159.32", 
"size": 83, 
"total": 14000000, 
"ver": 2, 
"vin_sz": 1, 
"vout_sz": 1
}

我嘗試使用 bitcoinj,但它返回非常簡單的對象,沒有很多欄位。

試試這個Python 庫(免責聲明:我寫的)

from btctools import Transaction

>>> tx = Transaction.from_hex('0100000000010153159b2a077d8dfbe4dca4b6b3a9e0ccedb4962ca73280526e6a1eeb1e8e9adc2a00000000ffffffff04a08f3e00000000001976a914de755835002260891962f1e671a2bf7605788d0f88ac0046c3230000000017a914c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c872052a6000000000017a91469f376599f0ffcaacd6a79854b1ee99513bb7b35870006f21600000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004830450221009360af3ec3f9d4ae8acc3ab2213f3f9344cb322b4ea447490bb297a9a4c1d207022067027b032e54b3efb9e885604349ec4f1b6446eed8ed208c6fe6eeae9ace921a0148304502210097cf932eb37513c201b577472d70f1553f338d6439e9181607ec75f2e83fa148022052368c88085838521efc64cf509b95dd53d8c825089acd3ff681e74d3a2b4959016952210266edd4ef2953675faf0662c088a7f620935807d200d65387290b31648e51e253210372ce38027ee95c98cdc54172964fa3aecf9f24b85c139d3d203365d6b691d0502103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000')
>>> tx
Transaction(inputs=1, outputs=4)

>>> tx.json()
{'locktime': 0,
'size': 448,
'txid': '0f6787fe25b631c73db598769eff70d616645eb61ad8c6f7408bc4341917aca5',
'version': 1,
'vin': [{'scriptSig': {'hex': ''},
  'sequence': 4294967295,
  'txid': 'dc9a8e1eeb1e6a6e528032a72c96b4edcce0a9b3b6a4dce4fb8d7d072a9b1553',
  'vout': 42,
  'witness': ['',
   '30450221009360af3ec3f9d4ae8acc3ab2213f3f9344cb322b4ea447490bb297a9a4c1d207022067027b032e54b3efb9e885604349ec4f1b6446eed8ed208c6fe6eeae9ace921a01',
   '304502210097cf932eb37513c201b577472d70f1553f338d6439e9181607ec75f2e83fa148022052368c88085838521efc64cf509b95dd53d8c825089acd3ff681e74d3a2b495901',
   '52210266edd4ef2953675faf0662c088a7f620935807d200d65387290b31648e51e253210372ce38027ee95c98cdc54172964fa3aecf9f24b85c139d3d203365d6b691d0502103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae']}],
'vout': [{'scriptPubKey': {'asm': 'OP_DUP OP_HASH160 de755835002260891962f1e671a2bf7605788d0f OP_EQUALVERIFY OP_CHECKSIG',
   'hex': '76a914de755835002260891962f1e671a2bf7605788d0f88ac'},
  'value': 0.041},
 {'n': 1,
  'scriptPubKey': {'asm': 'OP_HASH160 c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c OP_EQUAL',
   'hex': 'a914c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c87'},
  'value': 6.0},
 {'n': 2,
  'scriptPubKey': {'asm': 'OP_HASH160 69f376599f0ffcaacd6a79854b1ee99513bb7b35 OP_EQUAL',
   'hex': 'a91469f376599f0ffcaacd6a79854b1ee99513bb7b3587'},
  'value': 0.109},
 {'n': 3,
  'scriptPubKey': {'asm': 'OP_0 701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d',
   'hex': '0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d'},
  'value': 3.8496}]}

引用自:https://bitcoin.stackexchange.com/questions/78452