<p>The <code>scriptSig</code> needed for spending a P2PKH output (that is, an output of a standard address starting with '1...') consists of two parts, namely the signature and the public key. Looking at your example transaction <a href="https://blockchain.info/en/tx/10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0" rel="noreferrer">10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0</a>, the public key is <code>03d7b3bc2d0b4b72a845c469c9fee3c8cf475a2f237e379d7f75a4f463f7bd6ebd</code>:</p>

<p><img src="https://i.sstatic.net/VTXTE.png" alt="enter image description here"></p>

<p>From this you can derive the address by first applying the HASH-160 (where HASH-160(x) = RIPEMD-160(SHA-256(x))), then adding network byte (prefix) and checksum (postfix), and finally converting the whole data into Base58 format. The detailed process of converting a public key to an address is described here: <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="noreferrer">https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses</a></p>

<p>Though you probably have Bitcoin libraries doing the dirty work, the following small Python script converts the public key to the address:</p>

<pre><code> #!/usr/bin/env python
 from hashlib import *
 from base58 import *

def SHA256D(bstr):
    return sha256(sha256(bstr).digest()).digest()

def ConvertPKHToAddress(prefix, addr):
    data = prefix + addr
    return b58encode(data + SHA256D(data)[:4])

def PubkeyToAddress(pubkey_hex):
    pubkey = bytearray.fromhex(pubkey_hex)
    round1 = sha256(pubkey).digest()
    h = new('ripemd160')
    h.update(round1)
    pubkey_hash = h.digest()
    return ConvertPKHToAddress(b'\x00', pubkey_hash)

pubkey = "03d7b3bc2d0b4b72a845c469c9fee3c8cf475a2f237e379d7f75a4f463f7bd6ebd"
print("Address: %s" % PubkeyToAddress(pubkey))
</code></pre>

<p>This leads to the following expected result:</p>

<pre><code>$ ./pubkey_to_script.py
Address: 1J16FUpZsS6My1FqHXpka5fztAnyFsBPf1
</code></pre>

CC BY-SA 3.0
2017-03-06T14:49:36.347Z
Thanks! I've confirmed it in PHP with Bitwasp library and posted the source code above. You are awesome!
zono
CC BY-SA 3.0
2017-03-06T16:09:24.627Z
I'm glad I could help! Note that this approach only works when the output of a _P2PKH address_ is used as input. For P2SH addresses (that is, starting with '3...', mostly used for multisignature purposes) the sigScript can look quite different. Though very unlikely, there could be cases where you can't exactly tell from the scriptSig alone whether it was spent from a P2PKH or P2SH address and hence can't decide which prefix to use. Maybe this is the reason why in most answers people suggest to lookup the previous tx output.
theStack
CC BY-SA 3.0
2017-03-06T17:01:25.290Z
Thank you for letting me know important things. I will consider about that. I think I will add checking whether P2PKH or P2SH before getting address. I guess most locking script nowadays are P2PKH. I will lookup bitcoind only non-P2PKH address so that I can reduce cost of access to bitcoind.
zono
CC BY-SA 4.0
2022-03-06T17:25:42.997Z
I translated this code from python to golang: https://gist.github.com/IvRRimum/2457e18092292e89eb7ee218c7dc70da
IvRRimUm
CC BY-SA 3.0
2017-03-06T14:02:59.797Z
2017-03-06T14:02:59.797Z
theStack
<p>I'm looking for a way to get address from scriptSig. I want to know input address. Can you share how to do it?
I prefer PHP library but any ways would be appreciated.</p>

<p>e.g.</p>

<p>I use bitwasp, a PHP library. I can see a scriptSig ("483...767") in vin. I want to decode it so that I can get the source addresses.</p>

<p>PHP sample</p>

<pre><code>&lt;?php

require_once 'vendor/autoload.php';

use BitWasp\BitcoinLib\RawTransaction;

$hex = '0100000001e7bb795fa90136f4052a99a433a16d71406261dec7eaff2a2a03ce7254bb4159010000006b483045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401210396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767ffffffff035f150000000000001976a9143e37de641b25616304415a72bc556824f442004588ac00000000000000001e6a1c54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6313d1300000000001976a9142f7dcdf7cda481fe0f123d4c62bf830605e9f54d88ac00000000';

$tx_data = RawTransaction::decode($hex);
print(json_encode($tx_data));
</code></pre>

<p>Output</p>

<pre><code>{
   "txid":"55a8f0d5852745ab2d80358ff4ae6fdbfeab0e4dc94b3474d1c5538e0579bb95",
   "version":"1",
   "vin":[
      {
         "txid":"5941bb5472ce032a2affeac7de616240716da133a4992a05f43601a95f79bbe7",
         "vout":1,
         "scriptSig":{
            "asm":"3045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401 0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767",
            "hex":"483045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401210396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767"
         },
         "sequence":4294967295
      }
   ],
   "vout":[
      {
         "value":"0.00005471",
         "vout":0,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 3e37de641b25616304415a72bc556824f4420045 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9143e37de641b25616304415a72bc556824f442004588ac",
            "type":"pubkeyhash",
            "reqSigs":1,
            "hash160":"3e37de641b25616304415a72bc556824f4420045",
            "addresses":[
               "16fypikbjmmfroCHUhDLhczngCfL6BgccH"
            ]
         }
      },
      {
         "value":"0.00000000",
         "vout":1,
         "scriptPubKey":{
            "asm":"OP_RETURN 54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6",
            "hex":"6a1c54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6",
            "message":"unable to decode tx type!"
         }
      },
      {
         "value":"0.01260849",
         "vout":2,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 2f7dcdf7cda481fe0f123d4c62bf830605e9f54d OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9142f7dcdf7cda481fe0f123d4c62bf830605e9f54d88ac",
            "type":"pubkeyhash",
            "reqSigs":1,
            "hash160":"2f7dcdf7cda481fe0f123d4c62bf830605e9f54d",
            "addresses":[
               "15L7U55PAsHLEpQkZqz62e3eqWd9AHb2DH"
            ]
         }
      }
   ],
   "locktime":"0"
}
</code></pre>

<hr>

<p>Update 1</p>

<p>The following is a screenshot of blockchain.info</p>

<p><a href="https://i.sstatic.net/TjG1F.png" rel="noreferrer"><img src="https://i.sstatic.net/TjG1F.png" alt="enter image description here"></a></p>

<p><a href="https://blockchain.info/ja/tx/10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0" rel="noreferrer">https://blockchain.info/ja/tx/10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0</a></p>

<p>I'm wondering how I can get the source address "1J16FUpZsS6My1FqHXpka5fztAnyFsBPf1".</p>

<hr>

<p>Update 2</p>

<p>I found a similar question and retrieving previous transaction was proposed. It may work but I want to decode hex so that it can reduce cost to access bitcoind.</p>

<p><a href="https://bitcoin.stackexchange.com/questions/25540/how-to-get-the-input-address-from-vin-where-only-a-txid-is-given-insight-api">How to get the Input Address from vin where only a txid is given (Insight-Api)</a></p>

<hr>

<p>Update 3</p>

<p>This is also taking same approach. It may impossible to decode the scriptsig...</p>

<p><a href="https://bitcoin.stackexchange.com/questions/3896/how-to-findout-the-sender-of-a-transaction">How to findout the sender of a transaction</a></p>

<hr>

<p>Update 4</p>

<p>This explains why it is impossible. Okay I give up decoding scriptsig. I'll retrieve it by txid. </p>

<blockquote>
  <p>Due to the nature of the pay-to-pubkey script, the script sig does not contain the pubkey anymore (just the signature).</p>
</blockquote>

<p><a href="https://stackoverflow.com/questions/29350766/extract-input-address-from-a-namecoin-transaction-given-a-name-operation">https://stackoverflow.com/questions/29350766/extract-input-address-from-a-namecoin-transaction-given-a-name-operation</a></p>

<hr>

<p>Update 5 (Solved)</p>

<p>theStack gave me the best answer. Following the answer, I tried it in PHP with Bitwasp.</p>

<pre><code>&lt;?php

require_once 'vendor/autoload.php';

use BitWasp\BitcoinLib\BitcoinLib;
use BitWasp\BitcoinLib\RawTransaction;

$hex = '0100000001e7bb795fa90136f4052a99a433a16d71406261dec7eaff2a2a03ce7254bb4159010000006b483045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401210396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767ffffffff035f150000000000001976a9143e37de641b25616304415a72bc556824f442004588ac00000000000000001e6a1c54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6313d1300000000001976a9142f7dcdf7cda481fe0f123d4c62bf830605e9f54d88ac00000000';

$tx = RawTransaction::decode($hex);

$asm = explode(' ', $tx['vin'][0]['scriptSig']['asm']);
$address = BitcoinLib::public_key_to_address($asm[1], '00');

print($address); // 15L7U55PAsHLEpQkZqz62e3eqWd9AHb2DHyusaku
</code></pre>

<script>
How to get address from scriptSig?
zono
# How to get address from scriptSig?

(Asked by: username_0 on 2017-03-06)

I'm looking for a way to get address from scriptSig. I want to know input address. Can you share how to do it?
I prefer PHP library but any ways would be appreciated.

e.g.

I use bitwasp, a PHP library. I can see a scriptSig ("483...767") in vin. I want to decode it so that I can get the source addresses.

PHP sample

```
<?php

require_once 'vendor/autoload.php';

use BitWasp\BitcoinLib\RawTransaction;

$hex = '0100000001e7bb795fa90136f4052a99a433a16d71406261dec7eaff2a2a03ce7254bb4159010000006b483045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401210396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767ffffffff035f150000000000001976a9143e37de641b25616304415a72bc556824f442004588ac00000000000000001e6a1c54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6313d1300000000001976a9142f7dcdf7cda481fe0f123d4c62bf830605e9f54d88ac00000000';

$tx_data = RawTransaction::decode($hex);
print(json_encode($tx_data));

```

Output

```
{
   "txid":"55a8f0d5852745ab2d80358ff4ae6fdbfeab0e4dc94b3474d1c5538e0579bb95",
   "version":"1",
   "vin":[
      {
         "txid":"5941bb5472ce032a2affeac7de616240716da133a4992a05f43601a95f79bbe7",
         "vout":1,
         "scriptSig":{
            "asm":"3045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401 0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767",
            "hex":"483045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401210396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767"
         },
         "sequence":4294967295
      }
   ],
   "vout":[
      {
         "value":"0.00005471",
         "vout":0,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 3e37de641b25616304415a72bc556824f4420045 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9143e37de641b25616304415a72bc556824f442004588ac",
            "type":"pubkeyhash",
            "reqSigs":1,
            "hash160":"3e37de641b25616304415a72bc556824f4420045",
            "addresses":[
               "16fypikbjmmfroCHUhDLhczngCfL6BgccH"
            ]
         }
      },
      {
         "value":"0.00000000",
         "vout":1,
         "scriptPubKey":{
            "asm":"OP_RETURN 54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6",
            "hex":"6a1c54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6",
            "message":"unable to decode tx type!"
         }
      },
      {
         "value":"0.01260849",
         "vout":2,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 2f7dcdf7cda481fe0f123d4c62bf830605e9f54d OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9142f7dcdf7cda481fe0f123d4c62bf830605e9f54d88ac",
            "type":"pubkeyhash",
            "reqSigs":1,
            "hash160":"2f7dcdf7cda481fe0f123d4c62bf830605e9f54d",
            "addresses":[
               "15L7U55PAsHLEpQkZqz62e3eqWd9AHb2DH"
            ]
         }
      }
   ],
   "locktime":"0"
}

```

---

Update 1

The following is a screenshot of blockchain.info

[![enter image description here](https://i.sstatic.net/TjG1F.png)](https://i.sstatic.net/TjG1F.png)

<https://blockchain.info/ja/tx/10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0>

I'm wondering how I can get the source address "1J16FUpZsS6My1FqHXpka5fztAnyFsBPf1".

---

Update 2

I found a similar question and retrieving previous transaction was proposed. It may work but I want to decode hex so that it can reduce cost to access bitcoind.

[How to get the Input Address from vin where only a txid is given (Insight-Api)](https://bitcoin.stackexchange.com/questions/25540/how-to-get-the-input-address-from-vin-where-only-a-txid-is-given-insight-api)

---

Update 3

This is also taking same approach. It may impossible to decode the scriptsig...

[How to findout the sender of a transaction](https://bitcoin.stackexchange.com/questions/3896/how-to-findout-the-sender-of-a-transaction)

---

Update 4

This explains why it is impossible. Okay I give up decoding scriptsig. I'll retrieve it by txid.

> Due to the nature of the pay-to-pubkey script, the script sig does not contain the pubkey anymore (just the signature).

<https://stackoverflow.com/questions/29350766/extract-input-address-from-a-namecoin-transaction-given-a-name-operation>

---

Update 5 (Solved)

username_1 gave me the best answer. Following the answer, I tried it in PHP with Bitwasp.

```
<?php

require_once 'vendor/autoload.php';

use BitWasp\BitcoinLib\BitcoinLib;
use BitWasp\BitcoinLib\RawTransaction;

$hex = '0100000001e7bb795fa90136f4052a99a433a16d71406261dec7eaff2a2a03ce7254bb4159010000006b483045022100f5b10c765ae5116917ddf9b8c4e8d07996b37ad934c005396e7dcda70e7621ee022014cf65f5cc46e229fbcfe7259b3f8c9d0b0c3cd446c4307a8872ad6cc0d538b401210396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767ffffffff035f150000000000001976a9143e37de641b25616304415a72bc556824f442004588ac00000000000000001e6a1c54f55fe6f2a0f349e2921d06e63d58712a906fbb4231f1e943da82d6313d1300000000001976a9142f7dcdf7cda481fe0f123d4c62bf830605e9f54d88ac00000000';

$tx = RawTransaction::decode($hex);

$asm = explode(' ', $tx['vin'][0]['scriptSig']['asm']);
$address = BitcoinLib::public_key_to_address($asm[1], '00');

print($address); // 15L7U55PAsHLEpQkZqz62e3eqWd9AHb2DHyusaku

```

**Tags:** script


---

## 1 answers

### Answer by username_1 on 2017-03-06. Score: 9 | Accepted answer

The `scriptSig` needed for spending a P2PKH output (that is, an output of a standard address starting with '1...') consists of two parts, namely the signature and the public key. Looking at your example transaction [10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0](https://blockchain.info/en/tx/10857ad56f1695559562af8076aea4ccba625ba79a82f75e5658c0c2fa4336c0), the public key is `03d7b3bc2d0b4b72a845c469c9fee3c8cf475a2f237e379d7f75a4f463f7bd6ebd`:

![enter image description here](https://i.sstatic.net/VTXTE.png)

From this you can derive the address by first applying the HASH-160 (where HASH-160(x) = RIPEMD-160(SHA-256(x))), then adding network byte (prefix) and checksum (postfix), and finally converting the whole data into Base58 format. The detailed process of converting a public key to an address is described here: <https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses>

Though you probably have Bitcoin libraries doing the dirty work, the following small Python script converts the public key to the address:

```
 #!/usr/bin/env python
 from hashlib import *
 from base58 import *

def SHA256D(bstr):
    return sha256(sha256(bstr).digest()).digest()

def ConvertPKHToAddress(prefix, addr):
    data = prefix + addr
    return b58encode(data + SHA256D(data)[:4])

def PubkeyToAddress(pubkey_hex):
    pubkey = bytearray.fromhex(pubkey_hex)
    round1 = sha256(pubkey).digest()
    h = new('ripemd160')
    h.update(round1)
    pubkey_hash = h.digest()
    return ConvertPKHToAddress(b'\x00', pubkey_hash)

pubkey = "03d7b3bc2d0b4b72a845c469c9fee3c8cf475a2f237e379d7f75a4f463f7bd6ebd"
print("Address: %s" % PubkeyToAddress(pubkey))

```

This leads to the following expected result:

```
$ ./pubkey_to_script.py
Address: 1J16FUpZsS6My1FqHXpka5fztAnyFsBPf1

```

> **Comments:**
> **username_0**: Thanks! I've confirmed it in PHP with Bitwasp library and posted the source code above. You are awesome!  
> **username_1**: I'm glad I could help! Note that this approach only works when the output of a _P2PKH address_ is used as input. For P2SH addresses (that is, starting with '3...', mostly used for multisignature purposes) the sigScript can look quite different. Though very unlikely, there could be cases where you can't exactly tell from the scriptSig alone whether it was spent from a P2PKH or P2SH address and hence can't decide which prefix to use. Maybe this is the reason why in most answers people suggest to lookup the previous tx output.  
> **username_0**: Thank you for letting me know important things. I will consider about that. I think I will add checking whether P2PKH or P2SH before getting address. I guess most locking script nowadays are P2PKH. I will lookup bitcoind only non-P2PKH address so that I can reduce cost of access to bitcoind.  
> **username_2**: I translated this code from python to golang: https://gist.github.com/IvRRimum/2457e18092292e89eb7ee218c7dc70da  

---

