bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.65k stars 2.09k forks source link

decoderawtransaction() possible? #1606

Closed ghost closed 4 years ago

ghost commented 4 years ago

Is there a function available that is equivalent to the bitcoin-cli decoderawtransaction() function?

I am trying to decode raw transaction data.

I have tried using the code below to decode transactions but parts of the transaction are still in the buffer format.

var tx = bitcoin.Transaction.fromHex(rawTx);
junderw commented 4 years ago

This is the example output for decoderawtransaction... I am looking at it and you can get all that information from the Transaction class, though the asm text of the scripts will need to use bitcoinjs.script.toASM().

{                           (json object)
  "txid" : "hex",           (string) The transaction id
  "hash" : "hex",           (string) The transaction hash (differs from txid for witness transactions)
  "size" : n,               (numeric) The transaction size
  "vsize" : n,              (numeric) The virtual transaction size (differs from size for witness transactions)
  "weight" : n,             (numeric) The transaction's weight (between vsize*4 - 3 and vsize*4)
  "version" : n,            (numeric) The version
  "locktime" : xxx,         (numeric) The lock time
  "vin" : [                 (json array)
    {                       (json object)
      "txid" : "hex",       (string) The transaction id
      "vout" : n,           (numeric) The output number
      "scriptSig" : {       (json object) The script
        "asm" : "str",      (string) asm
        "hex" : "hex"       (string) hex
      },
      "txinwitness" : [     (json array)
        "hex",              (string) hex-encoded witness data (if any)
        ...
      ],
      "sequence" : n        (numeric) The script sequence number
    },
    ...
  ],
  "vout" : [                (json array)
    {                       (json object)
      "value" : n,          (numeric) The value in BTC
      "n" : n,              (numeric) index
      "scriptPubKey" : {    (json object)
        "asm" : "str",      (string) the asm
        "hex" : "hex",      (string) the hex
        "reqSigs" : n,      (numeric) The required sigs
        "type" : "str",     (string) The type, eg 'pubkeyhash'
        "addresses" : [     (json array)
          "str",            (string) bitcoin address
          ...
        ]
      }
    },
    ...
  ]
}
ghost commented 4 years ago

Thank you

junderw commented 4 years ago

This is most of the info.

I do not guarantee that this will be 100% the same as bitcoind in every case. Bugs ahoy.

const bitcoinjs = require('bitcoinjs-lib')

function decoderawtransaction(hex) {
    const tx = bitcoinjs.Transaction.fromHex(hex)
    return {
        txid: tx.getId(),
        hash: tx.getHash(true).toString('hex'),
        size: tx.byteLength(),
        vsize: tx.virtualSize(),
        weight: tx.weight(),
        version: tx.version,
        locktime: tx.locktime,
        vin: tx.ins.map(input => ({
            txid: Buffer.from(input.hash).reverse().toString('hex'),
            vout: input.index,
            scriptSig: {
                asm: bitcoinjs.script.toASM(input.script),
                hex: input.script.toString('hex'),
            },
            txinwitness: input.witness.map(b => b.toString('hex')),
            sequence: input.sequence,
        })),
        vout: tx.outs.map((output, i) => ({
            value: output.value,
            n: i,
            scriptPubKey: {
                asm: bitcoinjs.script.toASM(output.script),
                hex: output.script.toString('hex'),
            },
        })),
    }
}