Skip to content

Commit

Permalink
Merge pull request #106 from casper-ecosystem/develop-next
Browse files Browse the repository at this point in the history
Fix for U128, U256 and U512
  • Loading branch information
hoffmannjan authored Sep 22, 2021
2 parents 7d33be4 + 2d87888 commit 8bb9da3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ByteConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion test/lib/byterepr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8bb9da3

Please sign in to comment.