Handle U128 and U256 value when bytes are at the maximum by Apolixit · Pull Request #99 · SubstrateGaming/Substrate.NET.API · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Substrate.NetApi.Test/TypeConverters/PrimitiveTypesTest.cs
11 changes: 9 additions & 2 deletions Substrate.NetApi/Model/Types/Primitive/U128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,20 @@ public override void Create(BigInteger value)

var byteArray = value.ToByteArray();

if (byteArray.Length > TypeSize)
if (byteArray.Length > TypeSize + 1)
{
throw new NotSupportedException($"Wrong byte array size for {TypeName()}, max. {TypeSize} bytes!");
}

var bytes = new byte[TypeSize];
byteArray.CopyTo(bytes, 0);
if (byteArray.Length == TypeSize + 1)
{
Array.Copy(byteArray, 0, bytes, 0, TypeSize);
}
else
{
byteArray.CopyTo(bytes, 0);
}
Bytes = bytes;
Value = value;
}
Expand Down
12 changes: 9 additions & 3 deletions Substrate.NetApi/Model/Types/Primitive/U256.cs