<p>Referring to <a href="https://en.bitcoin.it/wiki/Transaction#Pay-to-Script-Hash" rel="nofollow noreferrer">Transaction - Bitcoin Wiki</a></p>

<p>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.</p>

<p>The scripts in the first transaction you linked are of the form:
<code>OP_HASH160 &lt;scriptHash&gt; OP_EQUAL</code>, which is a <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh-nested-in-bip16-p2sh" rel="nofollow noreferrer">P2WPKH nested in BIP16 P2SH</a></p>

<p><strong>Input</strong></p>

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

<p>The pubKey hash is in <code>inputs[0].script</code>: <code>64cc029cec3dce47ee2b105567cf57e2ceb40872</code></p>

<p><strong>Output</strong></p>

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

CC BY-SA 4.0
2018-07-09T21:44:01.550Z
2018-07-09T21:50:35.637Z
2018-07-09T21:50:35.637Z
JBaczuk
<p>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).</p>

<p>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.</p>

<p>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. </p>

CC BY-SA 4.0
2018-07-09T21:09:26.563Z
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
CC BY-SA 4.0
2018-07-09T21:04:28.347Z
2018-07-09T21:04:28.347Z
chytrik
<p>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.<br>
eg:<br>
P2SH: <a href="https://api.blockcypher.com/v1/btc/main/txs/87d5fa33ae3f1754bb1618c8559b8451f64de1e396627fad65a19b74b2c72b86" rel="nofollow noreferrer">https://api.blockcypher.com/v1/btc/main/txs/87d5fa33ae3f1754bb1618c8559b8451f64de1e396627fad65a19b74b2c72b86</a> ( difficulty finding)</p>

<p><a href="https://api.blockcypher.com/v1/btc/main/txs/d7cdb1f910ac15d99ee98a28c276ccfccd5fd6592cf01528eb57a871e789b709" rel="nofollow noreferrer">https://api.blockcypher.com/v1/btc/main/txs/d7cdb1f910ac15d99ee98a28c276ccfccd5fd6592cf01528eb57a871e789b709</a> ( This is easier as output is withdraw amount)</p>

<p2sh>
Hard time distinguishing change and withdraw amount for P2SH address
M.Roe
# Hard time distinguishing change and withdraw amount for P2SH address

(Asked by: username_0 on 2018-07-09)

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.  
eg:  
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)

**Tags:** p2sh


---

## 2 answers

### Answer by username_1 on 2018-07-09. Score: 0 | Accepted answer

Referring to [Transaction - Bitcoin Wiki](https://en.bitcoin.it/wiki/Transaction#Pay-to-Script-Hash)

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](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh-nested-in-bip16-p2sh)

**Input**

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`

**Output**

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.


---

### Answer by username_2 on 2018-07-09. Score: 2

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.

> **Comments:**
> **username_3**: 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.  

---

