1

I have been conducting research on the web and Bitcoin Stack Exchange to find a method for obtaining the source address of a transaction. I have come across multiple answers, some suggesting the use of APIs, while others focus solely on P2PKH sigscript.

In my case, the sigscript is a P2PK signature. I have utilized online transaction decoders, which parse the hexstring in a similar manner to how I have implemented it in my code. However, I have noticed that these decoders do not provide the source address. Interestingly, block explorers do display the source address. I am curious to know how block explorers obtain this information and how to calculate or retrieve the source address using the Bitcoin protocol itself, without relying on block explorer APIs.

prevtxid :

201,151,165,229,110,16,65,2,250,32,156,106,133,45,217,6,96,162,11,45,156,53,36,35,237,206,37,133,127,205,55,4

sigscript :

71,48,68,2,32,78,69,225,105,50,184,175,81,73,97,161,211,
                    161,162,95,223,63,79,119,50,233,214,36,198,198,21,72,171,
                    95,184,205,65,2,32,24,21,34,236,142,202,7,222,72,96,164,
                    172,221,18,144,157,131,28,197,108,187,172,70,34,8,34,33,
                    168,118,141,29,9,1
6
  • 2
    I would display data as hex rather than a decimal list, using either a printf, format of "%x" or hex.Encode(). It is shorter and often easier for readers to recognise. Commented Oct 23, 2023 at 11:58
  • 1
    The most pedantic answer is "there is no such thing as a source address; the block explorers are lying". The less pedantic answer is "it depends very much on what purpose you want it for". You must never ever send coins to a "source address" you found on the blockchain. It may not go to the actual sender of the transaction, and in some cases it may destroy the coins. Commented Oct 23, 2023 at 20:21
  • @GlennWillen Perhaps it makes sense to turn that into an additional answer? Commented Oct 23, 2023 at 21:00
  • @PieterWuille I did start writing one, but I didn't feel like I could properly do it justice. Don't you have a boilerplate one you can paste at this point? XD Commented Oct 23, 2023 at 21:07
  • @GlennWillen so all API's that gives you a source address for a transaction, they can be wrong? Commented Oct 24, 2023 at 5:33

1 Answer 1

3

Bitcoin itself does not know the concept of an address. The term address arose from the desire to bring end users working with bitcoin closer to the way they manage their bank account. This is precisely why you cannot find either the source or destination address in the serialized form of the transaction, nor in the bitcoin protocol at all. It just doesn't exist. Everything is based on locking assets via scriptPubKey (locking script) and unlocking assets via scriptSig (unlocking script). The content of locking and unlocking scripts can be whatever you want. Of course, everything in accordance with consensus rules (and standard rules if we don't work directly with miners).

What confuses you is that there are certain standard types of transactions, that is, standard types of output, such as P2PKH, P2WPKH, P2SH, etc.. Within them, you work with the hash of the public key, that is, the hash of the script. That hash is denoted (outside the bitcoin protocol) as an address.

Working with that hash in the locking script (scriptPubKey) can be considered as sending funds to that address (destination address), while working with the same hash in the unlocking script (scriptSig) can be considered as sending funds from that address (source address).

In order to see what the source address is, it is most correct to look at the referenced transaction and the referenced output of some input. That way, you will see which destination address (or more correctly hash) was used in the output that locked the funds, so that address (hash) becomes the source address in the input that unlocks it.

You should not look in scriptSig in search of an address, because it often happens that it is not there, at least not directly. For example, in P2SH the address (more correctly say the hash) is obtained by doing a hash on the last element (push) in the scriptSig. With P2WPKH, the address is not even in scriptSig, but in the witness part of the transaction.

Block explorers are applications intended for end users, so many of the things they display are adapted to that. Many of the things they show are not taken directly from the bitcoin protocol. One of those things is the concept of an address.

3
  • 3
    It might be worth mentioning that the source address is not necessarily controlled by the sender. E.g. if a transaction is sent from a custodial wallet service, the “source address” may be the address of another user of that service which the omnibus wallet happened to use to fund this payment.
    – Murch
    Commented Oct 23, 2023 at 18:11
  • 1
    Aside from what Murch said, the source of funds for a transaction may not be associated with any address at all (or it may be associated with multiple addresses.) Very old outputs may have been sent by the original Bitcoin software without using an address. Outputs made by software like Lightning may be sent to complex scripts that don't have any associated address. And a transaction might have inputs from multiple distinct senders (e.g. if it's some kind of CoinJoin), with no way to know who's who. Commented Oct 23, 2023 at 20:18
  • 1
    While it makes some sense for block explorers to display "sender addresses", it can be very misleading. You must absolutely never send funds to an address discovered in this way. If you do that, you will sooner or later find yourself being sued, when the funds end up destroyed, or sent to the wrong place. Commented Oct 23, 2023 at 20:19

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.