Bitcoinjs-lib: Parse raw transaction to get output address?

Created on 22 Dec 2016  路  7Comments  路  Source: bitcoinjs/bitcoinjs-lib

Hello!

Is it possible to use this module in order to parse raw transaction data and get destination Bitcoin addresses from it?

I've managed to parse raw transaction from hex string:


const bitcoinJs = require('bitcoinjs-lib');

const rawTx = '010...000';

const tx = bitcoinJs.Transaction.fromHex(rawTx);

console.log(JSON.stringify(tx, null, 4));

But how do I get destinations addresses from it?

Thank you.

how to / question / docs

Most helpful comment

let bitcoinjs = require('bitcoinjs-lib')

// ...
let tx = bitcoinjs.Transaction.fromHex('010...000')

tx.outs.forEach((out) => {
  let address
  try {
    address = bitcoinjs.address.fromOutputScript(out.script)
  } catch (e) {}

  if (address) console.log(address)
})

@afk11 to be honest, I do the above so often, I'd rather just have no error thrown and instead return null...

Maybe fromOutputScriptUnsafe

All 7 comments

Well, looks like I've finally managed to extract address from script output.

Here's my implementation:


const _ = require('lodash');
const bitcoinJs = require('bitcoinjs-lib');
const Buffer = require('buffer').Buffer;

const rawTx = '010...000';

const tx = bitcoinJs.Transaction.fromHex(rawTx);

_.forEach(tx.outs, out => {
  if (bitcoinJs.script.isPubKeyHashOutput(out.script)) {
    const pubKey = Buffer.from(out.script, 3, 20);
    const address = bitcoinJs.address.fromOutputScript(pubKey);
    console.log(address);
  }
});

Is this a most optimal way to do this? Do you have any suggestions of how I can improve it?

Is it enough to only check for PubKeyHashOutput for general transactions? Or do I need to implement some other script types also?


The library is very well written, it was a pleasure reading it's source code. Great job!

Thanks.

Don't forget about script-hash outputs, they also have an address!

let bitcoinjs = require('bitcoinjs-lib')

// ...
let tx = bitcoinjs.Transaction.fromHex('010...000')

tx.outs.forEach((out) => {
  let address
  try {
    address = bitcoinjs.address.fromOutputScript(out.script)
  } catch (e) {}

  if (address) console.log(address)
})

@afk11 to be honest, I do the above so often, I'd rather just have no error thrown and instead return null...

Maybe fromOutputScriptUnsafe

Oh, thank you @dcousens . Is bitcoinjs.address.fromOutputScript(out.script) enough to parse all commonly used transaction types?

@maxsummers it is enough to obtain an "address" from the 2 commonly used script types that have addresses: P2PKH, P2SH.
It does not support stealth addresses, or other complex ECDH protocols.

Thank you, this really helps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhaozhiming picture zhaozhiming  路  3Comments

Mr-Mondragon picture Mr-Mondragon  路  3Comments

tuyennvtb picture tuyennvtb  路  3Comments

ishwarchandratiwari picture ishwarchandratiwari  路  3Comments

ghost picture ghost  路  3Comments