MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 844 forks source link

Help : trying to understand GetScriptPubKey #1074

Closed AsyncLuck closed 2 years ago

AsyncLuck commented 2 years ago

Hi,

For the fun and to understand, I' m trying to query Electrs (Electrum rust server) to find tx history with private keys (with mnemonic).

I'm doing something like that

Mnemonic mnemo = new Mnemonic("12 words here", Wordlist.English);
ExtKey extKey = mnemo.DeriveExtKey();
var privateKey = extKey.PrivateKey;

var shaSegwit = NBitcoin.Crypto.Hashes.SHA256(privateKey.GetScriptPubKey(ScriptPubKeyType.Segwit).ToBytes());
var reverseSegwit = shaSegwit.Reverse().ToArray();
var hexSegwit = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(reverseSegwit);

Electrs needs a reverse SHA256 on the ScriptPubKey. I do it like above for Segwit.

But it doesn't work.

But when I m trying from a public address like this

var add2 = "bc1qxpvx3qcsaq3a3av8lzvdgfldm7w0g9pkyy678ayn99zpet3ax9ksgasuq6";
var add = BitcoinAddress.Create(add2, Network.Main);
var pub = NBitcoin.Crypto.Hashes.SHA256(add.ScriptPubKey.ToBytes());
reverseSegwit = pub.Reverse().ToArray();
hexSegwit = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(reverseSegwit);

It s working ->I can retrieve the txs form Electrs.

I m sure I m missing something in the "from private key" part. Maybe someone can point me where and how to ?

AsyncLuck commented 2 years ago

more tests If I do that

Mnemonic mnemo = new Mnemonic("my 12 words here", Wordlist.English);
ExtKey extKey = mnemo.DeriveExtKey();
var privateKey = extKey.PrivateKey;
var bitcoinPrivateKey = privateKey.GetWif(Network.Main);

And import the Wif in Electrum directly it doesn't work (wallet with no tx), but if I import via my "12 words seed", it works (txs in it). So, I think i miss something in the pk generation, that's why I have a "bad" ScriptPubKey for electrs ?

AsyncLuck commented 2 years ago

or even when I try to import the wif master key in electrum (no success to retrieve the txs)

var masterKey = extKey.GetWif(Network.Main); xprv @knocte I know you had the same kind of issues in the past (found some traces of similar questions from u), do you have an idea ? ;)

AsyncLuck commented 2 years ago

Found this library : (https://github.com/zkSNACKs/WalletWasabi/blob/abebff2b0703b0758ac8b97ba4b4f1cbd3a605eb/WalletWasabi/Extensions/NBitcoinExtensions.cs)

To import the ext key in electrum (zprv format for bip84) working good.

But didn't found a way to generate the correct scriptpubkey from the ext key to query electrs server.

Make some progress...

plasticalligator commented 2 years ago

Any updates private? We're eagerly awaiting the results of your latest scam.

AsyncLuck commented 2 years ago

@plasticalligator I don't understand your comment. "waiting the results of your latest scam" ? I m just trying to understand why I cannot obtain a ScriptPubKey from my ExtKey (mnemonic) to query Electrum server, the correct way. I can close this issue if it's considered as pure polution for this repo. I m not trying to scam anyone ???

knocte commented 2 years ago

Don't listen to @plasticalligator, he must be a troll (he's trolling also on another issue). With regards to your issue, maybe have a look at geewallet's source code? Been a while since I touched the code around this stuff, so I'm not sure.

knocte commented 2 years ago

(PS: geewallet queries Electrum servers.)

AsyncLuck commented 2 years ago

@knocte thx I will try