Question NBitcoin Library Issue

Blopa

New member
Joined
Feb 18, 2021
Messages
2
Programming Experience
Beginner
I'm using the NBitcoin library, I would like to add a bitcoin address to a Key object:

This is the Key class definition:
C#:
namespace NBitcoin
{
    public class Key : IDestination, IDisposable, IBitcoinSerializable
    {
        public Key(byte[] data, int count = -1, bool fCompressedIn = true);
I started out asigning the decoder to a byte array:
C#:
byte[] byteArray = Encoders.Base58.DecodeData("15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe");
Then converted it to hex:
C#:
string hex = Encoders.Hex.EncodeData(byteArray);
And finally printed that out: Console.WriteLine(hex); It seems to be printing out the hash correctly.

Then I create a HashSet:
C#:
var keys = new HashSet<Key>();
Call the constructor of the class like this:
C#:
var key = new Key(byteArray, -1, true); (according to the constructor signature)
And add key to the HashSet:
C#:
keys.Add(key);
But var key = new Key(byteArray, -1, true); is thowing an error: "the size of an ec key should be 32"

I really have no idea how to fix this.

Any suggestions , please?
 
Last edited by a moderator:
There is a debugger tutorial in my signature. I suggest whether you are new to debugging or not, that you read through it and learn how to debug your code. Why? (And not to discourage questioning).

But this should have been the first thing to do when you got an error like this. Step into your code to look for irregularities using your debugger. Only then when something doesn't make sense, or something doesn't function as intended, should you open a topic on any public forum, not just this one. Had you used a debugger, you would have identified your problem.

From what I can see, your byteArray does not contain enough chars to make up the required 32 bytes. Try adding : TRVWbTDSY, it should bring you up to 32, and see if that works. Remember, debugging is very important and every developer should know how to do it.
 
The code is not actually his. He needs to rebuild the NBitcoin library from source in github to have symbols and trace into the library to see why it is throwing that exception.
 
If 32 bytes is required then I would have assumed that that would have been his issue that the string that was attached would be too short. At least that's what it looks like, perhaps I'm wrong.
 
Yeah, running the string through the decoder only returned something like 15 bytes as I recall.
 
Back
Top Bottom