<p>In F#:</p>

<pre><code>let address = BitcoinAddress.Create("1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1", 
                                    NBitcoin.Network.Main)
let sha = NBitcoin.Crypto.Hashes.SHA256(address.ScriptPubKey.ToBytes())
let reversedSha = sha.Reverse().ToArray() // add `open System.Linq` at the top
NBitcoin.DataEncoders.Encoders.Hex.EncodeData reversedSha
</code></pre>

CC BY-SA 4.0
2019-01-29T07:26:19.487Z
2019-01-29T07:26:19.487Z
knocte
<p>Electrum server expects scripts to be queried by their sha256 hash, regardless of script type.  The name <code>script hash</code> is a bit confusing - it actually just means <em>take the sha256 of the script</em>.  You should be using a bare <code>sha256</code> method instead of the <code>Hash</code> one, which seems to hash the data for use with bitcoin transactions.
I'm not sure which endianness is used by NBitcoin, but if you do end up needing to reverse it, you shouldn't reverse the individual characters, rather you should be reversing the raw bytes, or in case it's a string, then in groups of two characters.</p>

CC BY-SA 4.0
2019-01-13T18:17:06.167Z
I've tried both `WitScriptId(account.PublicKey.WitHash.ScriptPubKey).ToString()` (because WitScriptId constructor seems to call sha256) and also reversed via `NBitcoin.DataEncoders.Encoders.Hex.EncodeData(WitScriptId(account.PublicKey.WitHash.ScriptPubKey).ToBytes().Reverse().ToArray())`, but electrum servers return a balance of zero for these two options (but I know it's not zero)
knocte
CC BY-SA 4.0
2019-01-13T18:31:05.897Z
You shouldn't be using any bitcoin-specific hashes to hash the scriptpubkey.  Try something like : https://github.com/MetacoSA/NBitcoin/blob/master/NBitcoin/Crypto/Hashes.cs#L621
arubi
CC BY-SA 4.0
2019-01-13T18:52:11.043Z
now I've tried `NBitcoin.DataEncoders.Encoders.Hex.EncodeData(NBitcoin.Crypto.Hashes.SHA256(account.PublicKey.ScriptPubKey.ToBytes()))`, and reversing it; and `NBitcoin.DataEncoders.Encoders.Hex.EncodeData(NBitcoin.Crypto.Hashes.SHA256(account.PublicKey.WitHash.ScriptPubKey.ToBytes()))` and reversing it, still zero balance
knocte
CC BY-SA 4.0
2019-01-13T19:00:28.407Z
Maybe post the contents of an example `ccount.PublicKey.ScriptPubKey` in your original question.  Hard to know if you're hashing the correct blob.
arubi
CC BY-SA 4.0
2019-01-14T04:02:35.663Z
updated the question to reflect what you just asked
knocte
CC BY-SA 4.0
2019-01-15T18:47:40.470Z
Ok, so really the address `39PtejNZn8sTGSFrefYUWxp7WdEyMLobkQ` is for the script `A9145483DD3179B74D410D474669AD5ED63A55EAB5A387`, which isn't listed.  The "reversed" sha256 of this script is `8ECFA8BA033A3332C73BBC6368DFA99D0FA2BD7035A92A4D78BBF0197E1FE44B`.  The two scrips you did list are a p2pk and p2wpkh, but not the p2sh-p2wpkh which the address actually references.  The last script which is the bare p2wpk script has address `bc1qfd6pj3fttk7caqsqpauvr7khy9lwpmgsf4thv2` and rev. sha256 `46731F1C49B8E024E92702C8CE87764C0C0F8BCF675C90B8E26BC38FF9043179`
arubi
CC BY-SA 4.0
2019-01-13T17:48:00.503Z
2019-01-13T17:48:00.503Z
arubi
<p>I've come across how scripts are hashed in the electrum (stratum) protocol implementation by looking <a href="https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes" rel="nofollow noreferrer">at the docs</a>.</p>

<p>I'm using NBitcoin, and I've tried to generate this hash from a segwit address, first this way:</p>

<pre><code>myPublicKey.WitHash.ScriptPubKey.Hash.ToString()
</code></pre>

<p>However, electrum servers throw an error saying that this hash is not valid. I'm guessing because this hash is of type HASH160 instead of SHA256 like the docs claim? (As I see that the hash generated by this technique is the same hash that appears below the address in a page <a href="https://www.blockchain.com/btc/address/3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8" rel="nofollow noreferrer">like this</a>.) </p>

<p>Also I'm wondering if I need to reverse the chain of characters myself or if NBitcoin's SHA256 methods would do it for me? All in all, how to get this hash with NBitcoin API so that Electrum servers are happy?</p>

<electrum><hash><segregated-witness><sha256><nbitcoin>
Electrum's "script hash" form with NBitcoin?
knocte
# Electrum's "script hash" form with NBitcoin?

(Asked by: username_0 on 2019-01-13)

I've come across how scripts are hashed in the electrum (stratum) protocol implementation by looking [at the docs](https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes).

I'm using NBitcoin, and I've tried to generate this hash from a segwit address, first this way:

```
myPublicKey.WitHash.ScriptPubKey.Hash.ToString()

```

However, electrum servers throw an error saying that this hash is not valid. I'm guessing because this hash is of type HASH160 instead of SHA256 like the docs claim? (As I see that the hash generated by this technique is the same hash that appears below the address in a page [like this](https://www.blockchain.com/btc/address/3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8).)

Also I'm wondering if I need to reverse the chain of characters myself or if NBitcoin's SHA256 methods would do it for me? All in all, how to get this hash with NBitcoin API so that Electrum servers are happy?

**Tags:** electrum, hash, segregated-witness, sha256, nbitcoin


---

## 2 answers

### Answer by username_0 on 2019-01-29. Score: 1

In F#:

```
let address = BitcoinAddress.Create("1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1", 
                                    NBitcoin.Network.Main)
let sha = NBitcoin.Crypto.Hashes.SHA256(address.ScriptPubKey.ToBytes())
let reversedSha = sha.Reverse().ToArray() // add `open System.Linq` at the top
NBitcoin.DataEncoders.Encoders.Hex.EncodeData reversedSha

```


---

### Answer by username_1 on 2019-01-13. Score: -1

Electrum server expects scripts to be queried by their sha256 hash, regardless of script type. The name `script hash` is a bit confusing - it actually just means *take the sha256 of the script*. You should be using a bare `sha256` method instead of the `Hash` one, which seems to hash the data for use with bitcoin transactions.
I'm not sure which endianness is used by NBitcoin, but if you do end up needing to reverse it, you shouldn't reverse the individual characters, rather you should be reversing the raw bytes, or in case it's a string, then in groups of two characters.

> **Comments:**
> **username_0**: I've tried both `WitScriptId(account.PublicKey.WitHash.ScriptPubKey).ToString()` (because WitScriptId constructor seems to call sha256) and also reversed via `NBitcoin.DataEncoders.Encoders.Hex.EncodeData(WitScriptId(account.PublicKey.WitHash.ScriptPubKey).ToBytes().Reverse().ToArray())`, but electrum servers return a balance of zero for these two options (but I know it's not zero)  
> **username_1**: You shouldn't be using any bitcoin-specific hashes to hash the scriptpubkey.  Try something like : https://github.com/MetacoSA/NBitcoin/blob/master/NBitcoin/Crypto/Hashes.cs#L621  
> **username_0**: now I've tried `NBitcoin.DataEncoders.Encoders.Hex.EncodeData(NBitcoin.Crypto.Hashes.SHA256(account.PublicKey.ScriptPubKey.ToBytes()))`, and reversing it; and `NBitcoin.DataEncoders.Encoders.Hex.EncodeData(NBitcoin.Crypto.Hashes.SHA256(account.PublicKey.WitHash.ScriptPubKey.ToBytes()))` and reversing it, still zero balance  
> **username_1**: Maybe post the contents of an example `ccount.PublicKey.ScriptPubKey` in your original question.  Hard to know if you're hashing the correct blob.  
> **username_0**: updated the question to reflect what you just asked  
> **username_1**: Ok, so really the address `39PtejNZn8sTGSFrefYUWxp7WdEyMLobkQ` is for the script `A9145483DD3179B74D410D474669AD5ED63A55EAB5A387`, which isn't listed.  The "reversed" sha256 of this script is `8ECFA8BA033A3332C73BBC6368DFA99D0FA2BD7035A92A4D78BBF0197E1FE44B`.  The two scrips you did list are a p2pk and p2wpkh, but not the p2sh-p2wpkh which the address actually references.  The last script which is the bare p2wpk script has address `bc1qfd6pj3fttk7caqsqpauvr7khy9lwpmgsf4thv2` and rev. sha256 `46731F1C49B8E024E92702C8CE87764C0C0F8BCF675C90B8E26BC38FF9043179`  

---

