Skip to content

Commit

Permalink
Merge pull request #123 from casper-ecosystem/hotfix/parsing-values
Browse files Browse the repository at this point in the history
Fix for lists and ByteArrays
  • Loading branch information
hoffmannjan authored Jan 10, 2022
2 parents d447349 + 16e9716 commit c4d9f00
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "2.7.4",
"version": "2.7.5",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md",
Expand All @@ -21,6 +21,7 @@
"~build": "tsc --watch",
"test:nctl": "TS_NODE_FILES=true mocha -r ts-node/register test/nctl/*.test.ts --timeout 5000000",
"test": "TS_NODE_FILES=true mocha -r ts-node/register test/**/*.test.ts src/lib/CLValue/*.test.ts",
"test2": "TS_NODE_FILES=true mocha -r ts-node/register src/lib/CLValue/ByteArray.test.ts",
"test:coverage": "nyc npm run test",
"docs": "typedoc --out docs/ src",
"publish-doc": "yarn run docs && gh-pages -d docs -r https://github.com/casper-ecosystem/casper-client-sdk-docs"
Expand Down
12 changes: 12 additions & 0 deletions src/lib/CLValue/ByteArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ describe('CLByteArray', () => {
expect(json).to.deep.eq(expectedJson);
expect(CLValueParsers.fromJSON(expectedJson).unwrap()).to.deep.eq(hash);
});

it('fromJSON() with length more than 32 bytes', () => {
const json = {
bytes:
'7f8d377b97dc7fbf3a777f5ae75eb6edbe79739df9d747f86bbf3b7f7efcd37d7a7b475c7fcefb6f8d3cd7dedcf1a6bd',
cl_type: { ByteArray: 48 }
};

const parsed = CLValueParsers.fromJSON(json).unwrap();

expect(CLValueParsers.toJSON(parsed).unwrap()).to.deep.eq(json);
});
});
17 changes: 4 additions & 13 deletions src/lib/CLValue/ByteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ export class CLByteArrayBytesParser extends CLValueBytesParsers {
}

fromBytesWithRemainder(
bytes: Uint8Array
bytes: Uint8Array,
type: CLByteArrayType
): ResultAndRemainder<CLByteArray, CLErrorCodes> {
const byteArray = new CLByteArray(
bytes.subarray(0, CL_BYTE_ARRAY_MAX_LENGTH)
);
return resultHelper(
Ok(byteArray),
bytes.subarray(CL_BYTE_ARRAY_MAX_LENGTH)
);
const byteArray = new CLByteArray(bytes.subarray(0, type.size));
return resultHelper(Ok(byteArray), bytes.subarray(type.size));
}
}

Expand All @@ -68,11 +64,6 @@ export class CLByteArray extends CLValue {
*/
constructor(v: Uint8Array) {
super();
if (v.length > CL_BYTE_ARRAY_MAX_LENGTH) {
throw new Error(
`Provided value has length ${v.length} which exceeded the limit (${CL_BYTE_ARRAY_MAX_LENGTH})`
);
}
this.data = v;
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/CLValue/Key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ describe('CLKey', () => {
expect(fromExpectedBytes).to.be.deep.eq(myKey);
});

it('toJSON() / fromJSON() with CLByteArray', () => {
const byteArr = new CLByteArray(new Uint8Array([21, 31]));
const myKey = new CLKey(byteArr);
const json = CLValueParsers.toJSON(myKey).unwrap();
const expectedJson = JSON.parse('{"bytes":"01151f","cl_type":"Key"}');

const fromJson = CLValueParsers.fromJSON(expectedJson).unwrap();

expect(json).to.be.deep.eq(expectedJson);
expect(fromJson).to.be.deep.eq(myKey);
});

it('toJSON() / fromJSON() with CLAccountHash', () => {
const hash = new CLAccountHash(Uint8Array.from(Array(32).fill(42)));
const myKey = new CLKey(hash);
Expand Down
11 changes: 8 additions & 3 deletions src/lib/CLValue/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
CLType,
CLValue,
CLByteArray,
CLByteArrayType,
CLByteArrayBytesParser,
CLURef,
CLURefBytesParser,
CLAccountHash,
CLAccountHashBytesParser,
CLErrorCodes,
KeyVariant,
ACCOUNT_HASH_LENGTH,
ResultAndRemainder,
ToBytesResult,
CLValueBytesParsers,
Expand All @@ -23,6 +23,8 @@ import {
} from './index';
import { KEY_ID, CLTypeTag } from './constants';

const HASH_LENGTH = 32;

export class CLKeyType extends CLType {
linksTo = CLKey;
tag = CLTypeTag.Key;
Expand Down Expand Up @@ -80,11 +82,14 @@ export class CLKeyBytesParser extends CLValueBytesParsers {
const tag = bytes[0];

if (tag === KeyVariant.Hash) {
const hashBytes = bytes.subarray(1, ACCOUNT_HASH_LENGTH + 1);
const hashBytes = bytes.subarray(1);
const {
result: hashResult,
remainder: hashRemainder
} = new CLByteArrayBytesParser().fromBytesWithRemainder(hashBytes);
} = new CLByteArrayBytesParser().fromBytesWithRemainder(
hashBytes,
new CLByteArrayType(HASH_LENGTH)
);
const hash = hashResult.unwrap();
const key = new CLKey(hash);
return resultHelper(Ok(key), hashRemainder);
Expand Down
51 changes: 50 additions & 1 deletion src/lib/CLValue/List.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
CLStringType,
CLU8,
CLI32,
CLI32Type
CLI32Type,
CLKey,
CLByteArray
} from './index';

describe('CLValue List implementation', () => {
Expand Down Expand Up @@ -198,6 +200,53 @@ describe('CLValue List implementation', () => {
expect(newList2).to.be.deep.eq(myList);
});

it('Runs toJSON() / fromJSON() properly', () => {
const clKey = new CLKey(
new CLByteArray(
Uint8Array.from([
48,
17,
103,
38,
142,
192,
14,
235,
126,
223,
125,
18,
217,
65,
153,
33,
225,
93,
189,
123,
20,
94,
69,
77,
148,
84,
10,
169,
28,
38,
14,
219
])
)
);
const myList = new CLList([clKey, clKey, clKey]);

const json = CLValueParsers.toJSON(myList).unwrap();
const newList = CLValueParsers.fromJSON(json).unwrap();

expect(newList).to.be.deep.eq(myList);
});

it('toBytesWithCLType() / fromBytesWithCLType()', () => {
const myList = new CLList([
new CLList([new CLBool(true), new CLBool(false)]),
Expand Down
1 change: 1 addition & 0 deletions src/lib/CLValue/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class CLListBytesParser extends CLValueBytesParsers {
remainder,
listType.inner
);

if (!vRes.ok) {
return resultHelper(Err(vRes.val));
}
Expand Down

0 comments on commit c4d9f00

Please sign in to comment.