0

Suppose I have following object

console.log(bitcoinjs.Transaction.fromHex(rawTx));

The output

Transaction {
  version: 2,
  locktime: 1655960,
  ins: [
    {
      hash: <Buffer dc 4d aa 10 db 80 36 52 24 49 7c c5 7c bc c5 bc df be e1 2d 58 61 70 e1 0c 7b 76 a7 35 cc 7c 3a>,
      index: 21,
      script: <Buffer 16 00 14 5f f3 9b e6 42 af a9 f3 59 25 34 e7 6a dd b2 ee e3 41 82 15>,
      sequence: 4294967294,
      witness: [Array]
    }
  ],
  outs: [
    {
      value: 1000000,
      script: <Buffer a9 14 14 ea 69 2e 87 76 ec c6 2e 6a aa a1 7d cc d2 8f f3 30 b7 46 87>
    },
    {
      value: 3299775,
      script: <Buffer a9 14 73 e2 99 78 43 31 1e cc 32 b5 ac 38 ef c9 9b b3 ae 54 bc 7c 87>
    }
  ]
}

How can I decode transfer amount (in BTC) from this object?

2
  • 2
    What do you mean by "transfer amount"? You have the output values there...
    – pinhead
    Commented Jan 9, 2020 at 15:07
  • You have the output value, which is the funds that get sent somewhere. That's probably enough for most applications. But you don't know how the value of any of the inputs, and therefore you also can't tell how much was paid in fees. If you can look up the inputs of this transaction, you'll have all the information you want!
    – karimkorun
    Commented Feb 3, 2021 at 18:42

2 Answers 2

1

The transfer amount (BTC) is, (somewhat dependant on the library used), most commonly denominated as "value:" Read: the outs => value: *

 {
      value: 1000000,
      script: <Buffer a9 14 14 ea 69 2e 87 76 ec c6 2e 6a aa a1 7d cc d2 8f f3 30 b7 46 87>
    },
    {
      value: 3299775,
      script: <Buffer a9 14 73 e2 99 78 43 31 1e cc 32 b5 ac 38 ef c9 9b b3 ae 54 bc 7c 87>
    }

Where the value is most often denominated in satoshis (a single unit.). But, some block explorers (blockchair) or other libraries will denominate in single units of Bitcoin, as is done below. (10^8 satoshis - 1 BTC ( 100 mil))

1000000 = 0.01BTC 3299775 = 0.03299775 BTC

0

I'm assuming you mean you want to know how many sats were sent from previous outputs (and how much was paid in fees).

This is not encoded in the tx itself. You need to get that information from your Bitcoin node (or from an explorer API like Blockstream's). You need to retrieve all previous input transactions and then look at the values in the outputs that your tx consumed. The tx contains the ids of the previous tx. You just have to go get that information.

This will do the trick:

import { Transaction } from 'bitcoinjs-lib';
import fetch from 'cross-fetch';

//I took a random recent tx from the blockchain:
const rawTx =
  '01000000000102f1950b9b14422ccb4214c0411a722d436f6b78aa35135ab87da42ec20a9ebf2900000000171600148e2d687d3dc2d5f2505809f212d90b3725bb78c0fdffffffc5e2cf7beaf2a14e57d530a88224e48673e6b8aa9f70d1c785ffc4618a0f89f001000000171600147606ee140cae2a5a47ad580e37a8e2557c05d12efdffffff02844402000000000017a914ec4c86acc0023962981f9d9585cab245e12b0859872eca0f0000000000160014330692d03439fe8ddb88df7b97fa834b975ea41002483045022100b6eced5373a7c37e9be7e4cbe070b880406fae7eba94519e8ddb35510889df4d02205cb95d1b9333f8713ea416f1a2f5d7d685418cad66d4b5c6a5f6b28ddecc955b012102d105008f859c9e9388c56fbe3432c077c731549c6f2807e44136b7de44abffe30248304502210085726ec64c7a614de6997705e86efdb78463d99a6be81128db2151e1869c6bbf022067e42c75755ea5ea922e29fae4a5a0b963de1f17faf8e5b9e4f767115a62e726012102555dabf7c0ecf054f1a0ae1a80980851d9b3b57060c59048c887709c62c7542900000000';

const outputValue = Transaction.fromHex(rawTx)
  .outs.map(out => out.value)
  .reduce((sum, value) => sum + value, 0);

let inputValue = 0;
for (const input of Transaction.fromHex(rawTx).ins) {
  const txid = Buffer.from(input.hash).reverse().toString('hex');
  const response = await fetch(`https://blockstream.info/api/tx/${txid}/hex`);
  const rawTx = await response.text();
  inputValue += Transaction.fromHex(rawTx).outs[input.index].value;
}

console.log({ inputValue, outputValue, fees: inputValue - outputValue });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.