0

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?

2 Answers 2

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.

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

6
  • 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
    Commented Jun 17, 2019 at 13:51
  • 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
    Commented Jun 17, 2019 at 14:00
  • @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
    Commented Jun 17, 2019 at 14:12
  • You literally asked how to do that in the previous question.
    – Claris
    Commented Jun 18, 2019 at 11:09
  • 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
    Commented Jun 18, 2019 at 11:11

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.