<p>If you are using C# with NBitcoin</p>

<pre><code>var address = new Script("OP_DUP OP_HASH160 ... OP_EQUALVERIFY OP_CHECKSIG").GetDestinationAddress(Network.Main);
</code></pre>

<p>It has the nice effect to work with P2SH addresses too.</p>

CC BY-SA 3.0
2015-03-28T21:43:19.003Z
2015-03-28T21:43:19.003Z
Nicolas Dorier
<p>Addresses are kept in <a href="https://en.bitcoin.it/wiki/Base58Check_encoding" rel="nofollow">Base58Check</a> format. Here's how you decode it.</p>

<ol>
<li>Decode the base58 encoding (similar to Base64). You should have 25 bytes.</li>
<li>Check that the 1st byte is 0x00 (the version byte of Bitcoin)</li>
<li><p>Check that the last 4 bytes are a correct checksum of the rest. This is done (in Python) by:</p>

<pre><code>sha256(sha256(data[0:21]))[:4] == data[-4:]
</code></pre>

<p>(Or, "take the first 4 bytes of a double-SHA256 of the first 21 bytes of the decoded data, then compare to the last 4 bytes of the decoded data.")</p></li>
<li><p>Take the middle 20 bytes (<code>data[1:21]</code>) and insert it into the following scriptPubKey.</p>

<pre><code>OP_DUP OP_HASH160 &lt;x&gt; OP_EQUALVERIFY OP_CHECKSIG
</code></pre></li>
</ol>

<p>Here are some libraries that will do steps 1-3:</p>

<ul>
<li><a href="https://github.com/luke-jr/libbase58" rel="nofollow">libbasse58</a> (C)</li>
<li><a href="http://lenschulwitz.com/b58/base58perl.txt" rel="nofollow">base58perl</a> (Perl)</li>
<li><a href="https://github.com/petertodd/python-bitcoinlib" rel="nofollow">python-bitcoinlib</a> (Python)</li>
<li><p><a href="https://github.com/bitcoinj/bitcoinj" rel="nofollow">bitcoinj</a> (Java)</p>

<pre><code>byte[] hash160 = new Address(NetworkParameters.prodNet(),
    &lt;addr&gt;).getHash160();
</code></pre></li>
<li><p><a href="https://github.com/cryptocoinjs/coinstring" rel="nofollow">coinstring.js</a> (node.js)</p>

<pre><code>require('coinstring').decode(&lt;addr&gt;)
</code></pre></li>
<li><a href="http://rosettacode.org/wiki/Bitcoin/address_validation" rel="nofollow">Various examples on rosetta code</a></li>
</ul>

CC BY-SA 3.0
2015-03-28T21:02:18.663Z
Worked well accept 
"Check that the last four bytes are a correct checksum" , of what ?
Haddar Macdasi
CC BY-SA 3.0
2015-03-28T21:13:12.110Z
@HaddarMacdasi Does the edit make it more clear?
Nick ODell
CC BY-SA 3.0
2015-03-28T21:18:44.763Z
I'm trying with an online tool. can't seem to work it out. The checksum part.
Haddar Macdasi
CC BY-SA 3.0
2015-03-28T20:45:05.200Z
2016-01-03T17:20:05.280Z
2016-01-03T17:20:05.280Z
Nick ODell
<p>How do I get to the value in the 'scriptPubKey' part of the transaction ? 
<img src="https://i.sstatic.net/eK9UA.png" alt="decoded transaction"></p>

<p>The sender knows only the address (1A3XjcuZmcszX2tGoVn1TrMNchuHwdAxZV) so how did he get into 63339ebf4914d964fbc729b0dc4c1a44c4fb8f80 ?
I've tried ripemd160(sha256(address)) didn't work .
I've tried sha256(sha256(address)) didn't work. </p>

<transactions><base58check>
How do I get to the value in the 'scriptPubKey' part of the transaction ?
Haddar Macdasi
# How do I get to the value in the 'scriptPubKey' part of the transaction ?

(Asked by: username_0 on 2015-03-28)

How do I get to the value in the 'scriptPubKey' part of the transaction ?
![decoded transaction](https://i.sstatic.net/eK9UA.png)

The sender knows only the address (1A3XjcuZmcszX2tGoVn1TrMNchuHwdAxZV) so how did he get into 63339ebf4914d964fbc729b0dc4c1a44c4fb8f80 ?
I've tried ripemd160(sha256(address)) didn't work .
I've tried sha256(sha256(address)) didn't work.

**Tags:** transactions, base58check


---

## 2 answers

### Answer by username_2 on 2015-03-28. Score: 6 | Accepted answer

Addresses are kept in Base58Check format. Here's how you decode it.

Decode the base58 encoding (similar to Base64). You should have 25 bytes.
Check that the 1st byte is 0x00 (the version byte of Bitcoin)
Check that the last 4 bytes are a correct checksum of the rest. This is done (in Python) by:
sha256(sha256(data[0:21]))[:4] == data[-4:]

(Or, "take the first 4 bytes of a double-SHA256 of the first 21 bytes of the decoded data, then compare to the last 4 bytes of the decoded data.")
Take the middle 20 bytes (data[1:21]) and insert it into the following scriptPubKey.
OP_DUP OP_HASH160  OP_EQUALVERIFY OP_CHECKSIG


Here are some libraries that will do steps 1-3:

libbasse58 (C)
base58perl (Perl)
python-bitcoinlib (Python)
bitcoinj (Java)
byte[] hash160 = new Address(NetworkParameters.prodNet(),
    ).getHash160();

coinstring.js (node.js)
require('coinstring').decode()

Various examples on rosetta code

> **Comments:**
> **username_0**: Worked well accept  "Check that the last four bytes are a correct checksum" , of what ?  
> **username_2**: @HaddarMacdasi Does the edit make it more clear?  
> **username_0**: I'm trying with an online tool. can't seem to work it out. The checksum part.  

---

### Answer by username_1 on 2015-03-28. Score: 1

If you are using C# with NBitcoin
var address = new Script("OP_DUP OP_HASH160 ... OP_EQUALVERIFY OP_CHECKSIG").GetDestinationAddress(Network.Main);

It has the nice effect to work with P2SH addresses too.


---

