Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoderawtransaction() possible? #1606

Closed
ghost opened this issue Jul 27, 2020 · 4 comments
Closed

decoderawtransaction() possible? #1606

ghost opened this issue Jul 27, 2020 · 4 comments

Comments

@ghost
Copy link

ghost commented Jul 27, 2020

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
Copy link
Member

junderw commented Jul 27, 2020

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
Copy link
Author

ghost commented Jul 28, 2020

Thank you

@ghost ghost closed this as completed Jul 28, 2020
@junderw
Copy link
Member

junderw commented Jul 28, 2020

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'),
            },
        })),
    }
}

@iamnivekx
Copy link

const bitcoinjs = require('bitcoinjs-lib')

function decoderawtransaction(serialized) {
  const tx = bitcoinjs.Transaction.fromHex(serialized);
  return {
    txid: tx.getId(),
    hash: tx.getHash(true).reverse().toString('hex'), // should reverse the hash
    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')
      },
    })),
  };
}

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants