Is there a way to create new "Key" object from an existing BigInteger?
Something like this?
var bigInt = new BigInteger(934157136952);
Key key = new Key(bigInt);
I tried searching documentation. But couldn't find.
Update 1
I also tried this. It gave me an exception with the message "Invalid String".
Key key = Key.Parse(bigInt.ToString(), Network.Main);
Seems It needed base58 string as the first argument.
Key key = Key.Parse(new Key().GetWif(Network.Main).ToString(), Network.Main); // Works fine
why don't you create a Key instance using a
byte []
. See theKey
implementation of NBitcoin hereC# supports converting
BigInteger
tobyte
arrays. So your code could hypothetically look like this:The other parameters are optional. However, in the
Key
implementation, you can see that yourKey
must be 32 bytes long. See line 14. So your example will throw anArgumentException
.So creating an instance of
BigInteger
with along
wont work and you should instead parse astring
. The code below gives a 32 byteBigInteger
and compiles successfully on my machine.Update 1
Best thing do to covert a byte array of the BigInteger is to add leading zeros as follows.
Update 2
C# provides a 1-line solution to resizing arrays in-situ. The result will be zero padded by default for byte arrays.