diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e2b5d06f..937615969 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to casper-js-sdk. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 2.5.2 + +### Fixed + +- Problem when serializing U128, U256 ad U512 `toBytes`. + ## 2.5.1 ### Fixed diff --git a/package.json b/package.json index 98b84553e..795ff96f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "casper-js-sdk", - "version": "2.5.1", + "version": "2.5.2", "license": "Apache 2.0", "description": "SDK to interact with the Casper blockchain", "homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md", diff --git a/src/lib/ByteConverters.ts b/src/lib/ByteConverters.ts index fc68ff991..c23c6e4d9 100644 --- a/src/lib/ByteConverters.ts +++ b/src/lib/ByteConverters.ts @@ -35,6 +35,10 @@ export const toBytesNumber = (bitSize: number, signed: boolean) => ( if (valTwos.gte(0)) { // for positive number, we had to deal with paddings if (bitSize > 64) { + // if zero just return zero + if (valTwos.eq(0)) { + return bytes; + } // for u128, u256, u512, we have to and append extra byte for length return concat([bytes, Uint8Array.from([bytes.length])]) .slice() diff --git a/test/lib/byterepr.test.ts b/test/lib/byterepr.test.ts index ec5bc3d8b..c66abdbf1 100644 --- a/test/lib/byterepr.test.ts +++ b/test/lib/byterepr.test.ts @@ -12,9 +12,21 @@ import { AccessRights, decodeBase16 } from '../../src'; -import { toBytesDeployHash } from '../../src/lib/ByteConverters'; +import { toBytesNumber, toBytesDeployHash } from '../../src/lib/ByteConverters'; describe(`numbers' toBytes`, () => { + it('CLU256 of zero after serialization should be equal to [0]', () => { + const toBytesNum128 = toBytesNumber(128, false); + const toBytesNum256 = toBytesNumber(256, false); + const toBytesNum512= toBytesNumber(512, false); + + const expectedRes = Uint8Array.from([0]); + + expect(toBytesNum128(0)).to.deep.eq(expectedRes) + expect(toBytesNum256(0)).to.deep.eq(expectedRes) + expect(toBytesNum512(0)).to.deep.eq(expectedRes) + }); + it('should be able to serialize/deserialize u8', () => { const validBytes = Uint8Array.from([0x0a]); const clVal = CLValueBuilder.u8(10);