Substrate.NET.API icon indicating copy to clipboard operation
Substrate.NET.API copied to clipboard

U256 type can get negative value

Open RostislavLitovkin opened this issue 1 year ago • 2 comments

var address = "unixuHLc4UoAjwLpkQHUWy2NpT5LV4tUqJFnFVNyLaeqBfq22";

var publicKey = Utils.GetPublicKeyFrom(address);

var value = new U256();

int p = 0;

value.Decode(publicKey.Reverse().ToArray(), ref p);

Console.WriteLine(value.Value); // -26843990603843339315797176730311565364345939525783601353920003531657203886725

U256 should never be negative.

RostislavLitovkin avatar Oct 11 '24 21:10 RostislavLitovkin

Workaround I found for getting the correct value:

Console.WriteLine(new BigInteger(publicKey, true, true));

RostislavLitovkin avatar Oct 11 '24 21:10 RostislavLitovkin

NetStandard 2.0 doesn't support the isUnsigned option. The trade-off is to mimic a larger array to achieve a BigInteger with the most significant bit not set. However, this leads to an inconsistency between the BigInteger value and the byte representation.

I'm unsure if we should address this, as the use case seems quite constructed and likely irrelevant.

https://github.com/SubstrateGaming/Substrate.NET.API/commit/a47945da924b4b6bda88b8b098a4d8e6a7cab565#diff-d9432c6f40e795d58bd62982772a902b84fac034ad1ef8c55ac543a4d4f8086eR80

darkfriend77 avatar Oct 14 '24 14:10 darkfriend77