1
I've come across how scripts are hashed in the electrum (stratum) protocol implementation by looking at the docs.
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.)
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?
I've tried both
WitScriptId(account.PublicKey.WitHash.ScriptPubKey).ToString()(because WitScriptId constructor seems to call sha256) and also reversed viaNBitcoin.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 – 2019-01-13T18:17:06.167You 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 – 2019-01-13T18:31:05.897now I've tried
NBitcoin.DataEncoders.Encoders.Hex.EncodeData(NBitcoin.Crypto.Hashes.SHA256(account.PublicKey.ScriptPubKey.ToBytes())), and reversing it; andNBitcoin.DataEncoders.Encoders.Hex.EncodeData(NBitcoin.Crypto.Hashes.SHA256(account.PublicKey.WitHash.ScriptPubKey.ToBytes()))and reversing it, still zero balance – knocte – 2019-01-13T18:52:11.043Maybe post the contents of an example
ccount.PublicKey.ScriptPubKeyin your original question. Hard to know if you're hashing the correct blob. – arubi – 2019-01-13T19:00:28.407updated the question to reflect what you just asked – knocte – 2019-01-14T04:02:35.663
Ok, so really the address
39PtejNZn8sTGSFrefYUWxp7WdEyMLobkQis for the scriptA9145483DD3179B74D410D474669AD5ED63A55EAB5A387, which isn't listed. The "reversed" sha256 of this script is8ECFA8BA033A3332C73BBC6368DFA99D0FA2BD7035A92A4D78BBF0197E1FE44B. 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 addressbc1qfd6pj3fttk7caqsqpauvr7khy9lwpmgsf4thv2and rev. sha25646731F1C49B8E024E92702C8CE87764C0C0F8BCF675C90B8E26BC38FF9043179– arubi – 2019-01-15T18:47:40.470