<p>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 <code>scriptPubKey</code> (locking script) and unlocking assets via <code>scriptSig</code> (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).</p>
<p>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.</p>
<p>Working with that hash in the locking script (scriptPubKey) can be considered as <strong>sending funds to that address (destination address)</strong>, while working with the same hash in the unlocking script (scriptSig) can be considered as <strong>sending funds from that address (source address)</strong>.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2023-10-23T18:11:04.017Z
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
CC BY-SA 4.0
2023-10-23T20:18:09.393Z
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.
Glenn Willen
CC BY-SA 4.0
2023-10-23T20:19:13.890Z
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.
Glenn Willen
CC BY-SA 4.0
2023-10-23T11:08:40.570Z
2023-10-23T11:08:40.570Z
dassd
<p>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.</p>
<p>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.</p>
<p>prevtxid :</p>
<pre><code>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
</code></pre>
<p>sigscript :</p>
<pre><code>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
</code></pre>

CC BY-SA 4.0
2023-10-23T11:58:26.327Z
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.
RedGrittyBrick
CC BY-SA 4.0
2023-10-23T20:21:16.113Z
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.
Glenn Willen
CC BY-SA 4.0
2023-10-23T21:00:17.173Z
@GlennWillen Perhaps it makes sense to turn that into an additional answer?
Pieter Wuille
CC BY-SA 4.0
2023-10-23T21:07:45.583Z
@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
Glenn Willen
CC BY-SA 4.0
2023-10-24T05:33:32.373Z
@GlennWillen so all API's that gives you a source address for a transaction, they can be wrong?
Monte Conte
CC BY-SA 4.0
2023-10-24T16:12:26.360Z
@MonteConte They will accurately display an address some coins were previously sent to. Not all coins will have such an address, and for coins which do, that address may not be able to receive more coins, or if it can, they may not go to the person who sent the transaction.
Glenn Willen
<address><raw-transaction><p2pk>
get source address of a transaction
Monte Conte
# get source address of a transaction

(Asked by: username_0 on 2023-10-23)

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

```

**Tags:** address, raw-transaction, p2pk

> **Comments:**
> **username_1**: 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.  
> **username_2**: 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.  
> **username_3**: @username_2 Perhaps it makes sense to turn that into an additional answer?  
> **username_2**: @username_3 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  
> **username_0**: @username_2 so all API's that gives you a source address for a transaction, they can be wrong?  
> **username_2**: @username_0 They will accurately display an address some coins were previously sent to. Not all coins will have such an address, and for coins which do, that address may not be able to receive more coins, or if it can, they may not go to the person who sent the transaction.  

---

## 1 answers

### Answer by username_4 on 2023-10-23. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_5**: 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.  
> **username_2**: Aside from what username_5 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.  
> **username_2**: 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.  

---

