<p>Change</p>
<pre><code>sha256.Write([]byte(scriptHex))
</code></pre>
<p>to</p>
<pre><code>s, err := hex.DecodeString(scriptHex)
if err != nil {
    log.Fatal(err) // or t.Fatalf(...) in unit test 
}
sha256.Write(s)
</code></pre>
<p>You must hash the binary data, not the hex string representation of it</p>
<p>See <a href="https://go.dev/play/p/iKHNKy118zd" rel="nofollow noreferrer">https://go.dev/play/p/iKHNKy118zd</a></p>

CC BY-SA 4.0
2022-06-02T03:40:36.470Z
You saved my head from an explosion. Thanks, this is exactly what I need! I gave you the status of the correct answer, but I can not give you a plus, due to the lack of 15 reputation units. Thank you.
Timofey Belanenko
CC BY-SA 4.0
2022-06-01T14:41:19.437Z
2022-06-01T14:51:16.130Z
2022-06-01T14:51:16.130Z
RedGrittyBrick
<p>I broke my head looking for a solution.<br />
I want use bitcoin address for electrumx</p>
<p>Take bitcoin address from docs example: <code>1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa</code><br />
And i need convert to <code>8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161</code></p>
<p>Example from docs <a href="https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes" rel="nofollow noreferrer">https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes</a></p>
<pre><code>For example, the legacy Bitcoin address from the genesis block:
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

has P2PKH script:
76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac

with SHA256 hash:
6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b

which is sent to the server reversed as:
8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161

By subscribing to this hash you can find P2PKH payments to that address.
</code></pre>
<p>my code:</p>
<pre><code>func AddressToScriptHex(addressStr string) string {
    address, err := btcutil.DecodeAddress(addressStr, &amp;chaincfg.MainNetParams)
    if err != nil {
        return &quot;&quot;
    }
    script, err := txscript.PayToAddrScript(address)
    if err != nil {
        return &quot;&quot;
    }
    return fmt.Sprintf(&quot;%x&quot;, script)
}

func TestBtcUtil_AddressToScriptHex(t *testing.T) {
    addressStr := &quot;1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa&quot;
    scriptHex := &quot;76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac&quot;

    assert.Equal(t, AddressToScriptHex(addressStr), scriptHex)
}

func TestBtcUtil_ScriptHexToSHA256(t *testing.T) {
    scriptHex := &quot;76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac&quot;
    exampleHash := &quot;6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b&quot;
    sha256 := sha256.New()
    sha256.Write([]byte(scriptHex))

    actual := fmt.Sprintf(&quot;%x&quot;, sha256.Sum(nil))

    assert.Equal(t, exampleHash, actual) // ERROR
}
</code></pre>
<p>why actual != example</p>
<hr />
<p>i was find other question, but they use other language
<a href="https://bitcoin.stackexchange.com/questions/68565/scripthash-from-bitcoin-address-with-bitcoinjs">ScriptHash from bitcoin address with BitcoinJS</a></p>
<p>Help me please</p>

<electrum><hash><source-code><go-lang>
Scripthash from bitcoin address with Golang
Timofey Belanenko
# Scripthash from bitcoin address with Golang

(Asked by: username_0 on 2022-06-01)

I broke my head looking for a solution.  
I want use bitcoin address for electrumx

Take bitcoin address from docs example: `1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa`  
And i need convert to `8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161`

Example from docs <https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes>

```
For example, the legacy Bitcoin address from the genesis block:
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

has P2PKH script:
76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac

with SHA256 hash:
6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b

which is sent to the server reversed as:
8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161

By subscribing to this hash you can find P2PKH payments to that address.

```

my code:

```
func AddressToScriptHex(addressStr string) string {
    address, err := btcutil.DecodeAddress(addressStr, &chaincfg.MainNetParams)
    if err != nil {
        return ""
    }
    script, err := txscript.PayToAddrScript(address)
    if err != nil {
        return ""
    }
    return fmt.Sprintf("%x", script)
}

func TestBtcUtil_AddressToScriptHex(t *testing.T) {
    addressStr := "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
    scriptHex := "76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac"

    assert.Equal(t, AddressToScriptHex(addressStr), scriptHex)
}

func TestBtcUtil_ScriptHexToSHA256(t *testing.T) {
    scriptHex := "76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac"
    exampleHash := "6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b"
    sha256 := sha256.New()
    sha256.Write([]byte(scriptHex))

    actual := fmt.Sprintf("%x", sha256.Sum(nil))

    assert.Equal(t, exampleHash, actual) // ERROR
}

```

why actual != example

---

i was find other question, but they use other language
[ScriptHash from bitcoin address with BitcoinJS](https://bitcoin.stackexchange.com/questions/68565/scripthash-from-bitcoin-address-with-bitcoinjs)

Help me please

**Tags:** electrum, hash, source-code, go-lang


---

## 1 answers

### Answer by username_1 on 2022-06-01. Score: 1 | Accepted answer

Change

```
sha256.Write([]byte(scriptHex))

```

to

```
s, err := hex.DecodeString(scriptHex)
if err != nil {
    log.Fatal(err) // or t.Fatalf(...) in unit test 
}
sha256.Write(s)

```

You must hash the binary data, not the hex string representation of it

See <https://go.dev/play/p/iKHNKy118zd>

> **Comments:**
> **username_0**: You saved my head from an explosion. Thanks, this is exactly what I need! I gave you the status of the correct answer, but I can not give you a plus, due to the lack of 15 reputation units. Thank you.  

---

