<p>You can't find the address from the scriptSig or input in general, because you don't know what the output was. Your question makes the assumption that every output contains a (common, but not only) P2PKH script.</p>

<p>If you would know the output being spent was a pay-to-pubkey-hash scriptPubKey, which is spent by revealing the full public key and a signature with it, you can indeed compute the address as the P2PKH address of that public key. But you don't know that without access to the output.</p>

<p>If you would know the output being spent was a P2SH scriptPubKey, which is spent by revealing the full script and its inputs, you can compute the address as the P2SH address of that script. But you don't know that without access to the output.</p>

<p>If you would know the output being spent was a segwit P2WPKH or P2WSH scriptPubKey, which are spent with an input that contains the public key resp. script as the last witness stack element, you can compute the address as the Bech32 address corresponding to that key or script. But you don't know that without access to the output.</p>

<p>If the scriptPubKey of the output being spent is not one of these specific templates (P2PKH, P2SH, segwit), the answer is simply that no address for that output exists. Addresses are a convenient human-readable form for specific types of transaction outputs that are common im payments. But they are by no means the only things supported by the Bitcoin protocol.</p>

CC BY-SA 4.0
2019-06-23T17:49:10.127Z
2019-06-23T17:49:10.127Z
Pieter Wuille
<blockquote>
  <p><strong>we can't generate previous output address from the public key inside the ScriptSig?</strong></p>
</blockquote>

<p>AFAIK you can. For example see <a href="https://medium.com/coinmonks/how-to-generate-a-bitcoin-address-step-by-step-9d7fcbf1ad0b" rel="nofollow noreferrer">How to Generate a Bitcoin Address — Step by Step</a> by Jordan Baczuk</p>

<p>However, being able to generate an address from a public key is of no benefit unless you know the private-key which allows you to spend the BTC associated with that address.</p>

<p>Furthermore, if I understand correctly, with an HD wallet, a third party cannot predict the addresses that will be generated because they cannot predict the public keys that will be generated. Since HD wallets usually do not reuse addresses, knowledge of a single public key does not allow you to do anything useful or discover anything interesting.</p>

CC BY-SA 4.0
2019-06-17T13:51:28.303Z
thank you. I want to generate previous output address just to save a transaction history and no more. and the link you sent it, just explain how to generate destination address which is obtain from ScriptPubKey  and not ScriptSig.
I want previous address not next destination address.
Saeed
CC BY-SA 4.0
2019-06-17T14:00:10.103Z
As I've commented before, what you're trying to do, accounting via packet captures, is utterly idiotic. Do not build systems like this if you want to retain control of your money.
Claris
CC BY-SA 4.0
2019-06-17T14:12:39.767Z
@Anonymous I don't have any bitcoin and also i don't want to extract address from packet capture. if you read my question correctly, I just want to find an algorithm to extract previous address from ScriptSig in input, like the algorithm which is used to extract destination address from ScriptPubKey  in ouput.
if you don't understand my question or don't know my answer you are not compelled to answer me. thank you.
Saeed
CC BY-SA 4.0
2019-06-18T11:09:05.717Z
You literally asked how to do that in the previous question.
Claris
CC BY-SA 4.0
2019-06-18T11:11:41.443Z
It’s rather funny that you don’t think people would make the connection between the two questions, or are you claiming that it’s a total coincidence?
Claris
CC BY-SA 4.0
2019-06-22T06:13:03.183Z
@Anonymous I'm just a programmer who intrested in Bitcoin and extract useful information from tx messages. so you should not judge me.
Saeed
CC BY-SA 4.0
2019-06-17T13:33:10.407Z
2019-06-17T13:33:10.407Z
RedGrittyBrick
<p>Recently I read some articles about BITCOIN protocol and it's transaction.
I understand(maybe it is incorrect) these rules:</p>

<ul>
<li>An input is what we call an output when you're spending it in a
transaction.</li>
<li>A public key is the early form of an address.</li>
<li>It seem's that an "input" has two jobs: Select an Output and Unlock it.</li>
<li>Every transaction has a unique TXID, so by using that with a specific
output number(VOUT), we can refer to any output in the blockchain.</li>
<li>Each output is set with a locking script. So if we want to spend
one, we need to supply an unlocking script(ScriptSig).</li>
<li>ScriptSig in an input is a script that unlocks the input and
ScriptPubKey is a A script that locks the output.</li>
</ul>

<p>if all rules that I wrote is correct, why we can generate an address which is obtained from public key in ScriptPubKey but we can't generate previous output address from the public key inside the ScriptSig?</p>

<address><public-key><transaction-id><input-script>
getting address from public of ScriptPubKey vs ScriptSig
Saeed
# getting address from public of ScriptPubKey vs ScriptSig

(Asked by: username_0 on 2019-06-17)

Recently I read some articles about BITCOIN protocol and it's transaction.
I understand(maybe it is incorrect) these rules:

* An input is what we call an output when you're spending it in a
  transaction.
* A public key is the early form of an address.
* It seem's that an "input" has two jobs: Select an Output and Unlock it.
* Every transaction has a unique TXID, so by using that with a specific
  output number(VOUT), we can refer to any output in the blockchain.
* Each output is set with a locking script. So if we want to spend
  one, we need to supply an unlocking script(ScriptSig).
* ScriptSig in an input is a script that unlocks the input and
  ScriptPubKey is a A script that locks the output.

if all rules that I wrote is correct, why we can generate an address which is obtained from public key in ScriptPubKey but we can't generate previous output address from the public key inside the ScriptSig?

**Tags:** address, public-key, transaction-id, input-script


---

## 2 answers

### Answer by username_1 on 2019-06-23. Score: 2

You can't find the address from the scriptSig or input in general, because you don't know what the output was. Your question makes the assumption that every output contains a (common, but not only) P2PKH script.

If you would know the output being spent was a pay-to-pubkey-hash scriptPubKey, which is spent by revealing the full public key and a signature with it, you can indeed compute the address as the P2PKH address of that public key. But you don't know that without access to the output.

If you would know the output being spent was a P2SH scriptPubKey, which is spent by revealing the full script and its inputs, you can compute the address as the P2SH address of that script. But you don't know that without access to the output.

If you would know the output being spent was a segwit P2WPKH or P2WSH scriptPubKey, which are spent with an input that contains the public key resp. script as the last witness stack element, you can compute the address as the Bech32 address corresponding to that key or script. But you don't know that without access to the output.

If the scriptPubKey of the output being spent is not one of these specific templates (P2PKH, P2SH, segwit), the answer is simply that no address for that output exists. Addresses are a convenient human-readable form for specific types of transaction outputs that are common im payments. But they are by no means the only things supported by the Bitcoin protocol.


---

### Answer by username_2 on 2019-06-17. Score: 1

> **we can't generate previous output address from the public key inside the ScriptSig?**

AFAIK you can. For example see [How to Generate a Bitcoin Address — Step by Step](https://medium.com/coinmonks/how-to-generate-a-bitcoin-address-step-by-step-9d7fcbf1ad0b) by Jordan Baczuk

However, being able to generate an address from a public key is of no benefit unless you know the private-key which allows you to spend the BTC associated with that address.

Furthermore, if I understand correctly, with an HD wallet, a third party cannot predict the addresses that will be generated because they cannot predict the public keys that will be generated. Since HD wallets usually do not reuse addresses, knowledge of a single public key does not allow you to do anything useful or discover anything interesting.

> **Comments:**
> **username_0**: thank you. I want to generate previous output address just to save a transaction history and no more. and the link you sent it, just explain how to generate destination address which is obtain from ScriptPubKey  and not ScriptSig. I want previous address not next destination address.  
> **username_3**: As I've commented before, what you're trying to do, accounting via packet captures, is utterly idiotic. Do not build systems like this if you want to retain control of your money.  
> **username_0**: @Anonymous I don't have any bitcoin and also i don't want to extract address from packet capture. if you read my question correctly, I just want to find an algorithm to extract previous address from ScriptSig in input, like the algorithm which is used to extract destination address from ScriptPubKey  in ouput. if you don't understand my question or don't know my answer you are not compelled to answer me. thank you.  
> **username_3**: You literally asked how to do that in the previous question.  
> **username_3**: It’s rather funny that you don’t think people would make the connection between the two questions, or are you claiming that it’s a total coincidence?  
> **username_0**: @Anonymous I'm just a programmer who intrested in Bitcoin and extract useful information from tx messages. so you should not judge me.  

---

