I am trying to extract the exact amount of BTC deposited and withdrawn from BTC address (from hardware wallets like Trezor, ledger nano, ...). So if the address is pay-to-pubkey-hash then I can easily track amount through its transaction id hash -> outputs BUT when the address is pay-to-script-hash then I am having a hard time recognizing which one is Change and which one is withdrawing amount from Txn Hash detail. Is there any way to distinguish change and withdraw amount for P2SH address.
P2SH: https://api.blockcypher.com/v1/btc/main/txs/87d5fa33ae3f1754bb1618c8559b8451f64de1e396627fad65a19b74b2c72b86 ( difficulty finding)

https://api.blockcypher.com/v1/btc/main/txs/d7cdb1f910ac15d99ee98a28c276ccfccd5fd6592cf01528eb57a871e789b709 ( This is easier as output is withdraw amount)


Referring to Transaction - Bitcoin Wiki

As far as I understand, the only way to know for sure if one of the outputs was change (sent back to the same public key as the input) would be to find the pubKey (or pubKey hash) that is common between the inputs and outputs, this would be the change transaction.

The scripts in the first transaction you linked are of the form: OP_HASH160 <scriptHash> OP_EQUAL, which is a P2WPKH nested in BIP16 P2SH


The public key is part of the witness (inputs[0].witness[1]) and is the value: 03985970517aea08ddf21636e1c0fb855c0fa04bb9b11ca7323a2d56bfafdb423d

The pubKey hash is in inputs[0].script: 64cc029cec3dce47ee2b105567cf57e2ceb40872


Unfortunately the outputs do not give you this information, because the script (outputs[n].script) only shows OP_HASH160 <scriptHash> OP_EQUAL. You cannot get the pubKey (hash or the pubkey) from the script hash, because a hash is one-way.


It is not possible to discern with absolute certainty which output is a 'payment' and which is the change address (I put payment in quotations because a transaction does not always represent a transfer of coins between two parties, sometimes it might just be someone moving coins to a new wallet they also control, for example).

Sometimes it may be possible to make an educated guess based on input/output values, but this is not always the case. In fact, some wallets may be designed to intentionally obfuscate this data, in an effort to maintain user privacy.

Unless I am misunderstanding your question, there is no difference between P2PKH and P2SH in this regard. The bitcoin address and payment amount is clearly listed for each output in the examples you provided.

  • In fact, there entire reason to have change (as opposed to models that don't need it) is to hide the amounts being sent. If it is obvious to everyone what is change and what isn't, that has failed. – Pieter Wuille Jul 9 '18 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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