Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Merge #110
Browse files Browse the repository at this point in the history
110: CLValue.asList() r=zie1ony a=zie1ony

#

Co-authored-by: Maciej Zielinski <maciek.s.zielinski@gmail.com>
  • Loading branch information
bors[bot] and zie1ony authored Feb 9, 2021
2 parents b9935be + 37ff810 commit 5d4b036
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 25 deletions.
10 changes: 10 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to casper-client-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).

## [1.0.19]

### Added

- `CLValue.isList` and `CLValue.asList`.

### Fixed

- BytesArrayValue's fromBytes.

## [1.0.18]

### Added
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-client-sdk",
"version": "1.0.18",
"version": "1.0.19",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"main": "dist/index.js",
Expand Down Expand Up @@ -38,7 +38,7 @@
"husky": "^2.7.0",
"lint-staged": "^8.2.1",
"minimist": ">=1.2.3",
"mocha": "^6.1.4",
"mocha": "^6.2.3",
"nodemon": "^2.0.2",
"nyc": "^15.0.1",
"prettier": "^1.18.2",
Expand Down
36 changes: 26 additions & 10 deletions packages/sdk/src/lib/CLValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ const fromBytesSimpleType = (
};

export class List<T extends CLTypedAndToBytes> extends CLTypedAndToBytes {
constructor(private vec: T[]) {
constructor(public vec: T[]) {
super();
if (vec.length === 0) {
throw new Error("Can't create instance for empty list");
Expand Down Expand Up @@ -811,7 +811,7 @@ class ByteArrayValue extends CLTypedAndToBytes {
}

public static fromBytes(bytes: Uint8Array): Result<ByteArrayValue> {
const b = new ByteArrayValue(bytes);
const b = new ByteArrayValue(bytes.subarray(0, 32));
return Result.Ok(b, bytes.subarray(32));
}
}
Expand Down Expand Up @@ -1655,7 +1655,7 @@ export class CLValue implements ToBytes {
const numberCoder = this.value as NumberCoder;
return BigNumber.from(numberCoder.val);
} else {
throw new Error("The CLValue can't convert to BigNumber");
throw new Error('The CLValue is not an instance of BigNumber');
}
}

Expand All @@ -1665,7 +1665,7 @@ export class CLValue implements ToBytes {

public asBoolean() {
if (!this.isBoolean()) {
throw new Error("The CLValue can't convert to Boolean");
throw new Error('The CLValue is not an instance of Boolean');
}
return (this.value as Bool).val;
}
Expand All @@ -1676,7 +1676,7 @@ export class CLValue implements ToBytes {

public asString() {
if (!this.isString()) {
throw new Error("The CLValue can't convert to String");
throw new Error('The CLValue is not an instance of String');
}
return (this.value as StringValue).val;
}
Expand All @@ -1687,7 +1687,7 @@ export class CLValue implements ToBytes {

public asPublicKey(): PublicKey {
if (!this.isPublicKey()) {
throw new Error("The CLValue can't convert to PublicKey");
throw new Error('The CLValue is not an instance of PublicKey');
}
return this.value as PublicKey;
}
Expand All @@ -1698,7 +1698,7 @@ export class CLValue implements ToBytes {

public asKey() {
if (!this.isKey()) {
throw new Error("The CLValue can't convert to Key");
throw new Error('The CLValue is not an instance of Key');
}
return this.value as KeyValue;
}
Expand All @@ -1709,7 +1709,7 @@ export class CLValue implements ToBytes {

public asURef() {
if (!this.isURef()) {
throw new Error("The CLValue can't convert to URef");
throw new Error('The CLValue is not an instance of URef');
}
return this.value as URef;
}
Expand All @@ -1720,7 +1720,7 @@ export class CLValue implements ToBytes {

public asBytesArray() {
if (!this.isBytesArray()) {
throw new Error("The CLValue can't convert to BytesArray");
throw new Error('The CLValue is not an instance of BytesArray');
}
return (this.value as ByteArrayValue).toBytes();
}
Expand All @@ -1731,10 +1731,26 @@ export class CLValue implements ToBytes {

public asOption() {
if (!this.isOption()) {
throw new Error("The CLValue can't convert to Option");
throw new Error('The CLValue is not an instance of Option');
}
return this.value as Option;
}

public isList() {
return this.clType instanceof ListType;
}

public asList() {
if (!this.isList()) {
throw new Error('The CLValue is not an instance of List');
}
const innerType = this.clType as ListType;
const list = List.fromBytes(innerType, this.clValueBytes());
if (list.hasError()) {
throw new Error('The CLValue can not be parsed to list.');
}
return list.value().vec.map(e => CLValue.fromT(e));
}
}

export enum KeyVariant {
Expand Down
75 changes: 66 additions & 9 deletions packages/sdk/test/lib/DeployUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Keys, DeployUtil, CLValue } from '../../src/lib';
import { TypedJSON } from 'typedjson';

describe('DeployUtil', () => {
it('should stringify/parse DeployHeader correctly', function () {
it('should stringify/parse DeployHeader correctly', function() {
const ed25519Key = Keys.Ed25519.new();
const deployHeader = new DeployUtil.DeployHeader(
ed25519Key.publicKey,
Expand All @@ -20,7 +20,7 @@ describe('DeployUtil', () => {
expect(deployHeader1).to.deep.equal(deployHeader);
});

it('should allow to extract data from Transfer', function () {
it('should allow to extract data from Transfer', function() {
const senderKey = Keys.Ed25519.new();
const recipientKey = Keys.Ed25519.new();
const networkName = 'test-network';
Expand Down Expand Up @@ -53,11 +53,17 @@ describe('DeployUtil', () => {
assert.isTrue(deploy.isStandardPayment());
assert.deepEqual(deploy.header.account, senderKey.publicKey);
assert.deepEqual(
deploy.payment.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.payment
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
paymentAmount
);
assert.deepEqual(
deploy.session.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.session
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
transferAmount
);
assert.deepEqual(
Expand All @@ -77,7 +83,7 @@ describe('DeployUtil', () => {
assert.deepEqual(deploy.approvals[1].signer, recipientKey.accountHex());
});

it('should allow to add arg to Deploy', function () {
it('should allow to add arg to Deploy', function() {
const senderKey = Keys.Ed25519.new();
const recipientKey = Keys.Ed25519.new();
const networkName = 'test-network';
Expand Down Expand Up @@ -111,18 +117,27 @@ describe('DeployUtil', () => {
deploy = DeployUtil.deployFromJson(json)!;

assert.deepEqual(
deploy.session.getArgByName('custom_id')!.asBigNumber().toNumber(),
deploy.session
.getArgByName('custom_id')!
.asBigNumber()
.toNumber(),
customId
);
assert.isTrue(deploy.isTransfer());
assert.isTrue(deploy.isStandardPayment());
assert.deepEqual(deploy.header.account, senderKey.publicKey);
assert.deepEqual(
deploy.payment.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.payment
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
paymentAmount
);
assert.deepEqual(
deploy.session.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.session
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
transferAmount
);
assert.deepEqual(
Expand All @@ -143,7 +158,7 @@ describe('DeployUtil', () => {
assert.notEqual(oldDeploy.header.bodyHash, deploy.header.bodyHash);
});

it('should not allow to add arg to a signed Deploy', function () {
it('should not allow to add arg to a signed Deploy', function() {
const senderKey = Keys.Ed25519.new();
const recipientKey = Keys.Ed25519.new();
const networkName = 'test-network';
Expand Down Expand Up @@ -171,4 +186,46 @@ describe('DeployUtil', () => {
DeployUtil.addArgToDeploy(deploy, 'custom_id', CLValue.u32(customId));
}).to.throw('Can not add argument to already signed deploy.');
});

it('should allow to extract additional args from Transfer.', function() {
const from = Keys.Ed25519.new();
const to = Keys.Ed25519.new();
const networkName = 'test-network';
const paymentAmount = 10000000000000;
const transferAmount = 10;
const id = 34;

let deployParams = new DeployUtil.DeployParams(from.publicKey, networkName);
let session = DeployUtil.ExecutableDeployItem.newTransfer(
transferAmount,
to.publicKey,
undefined,
id
);
let payment = DeployUtil.standardPayment(paymentAmount);
let deploy = DeployUtil.makeDeploy(deployParams, session, payment);
let fromRawPK = from.publicKey.rawPublicKey;

let transferDeploy = DeployUtil.addArgToDeploy(
deploy,
'fromPublicKey',
CLValue.publicKey(fromRawPK)
);

assert.deepEqual(
transferDeploy.session.getArgByName('fromPublicKey')?.asPublicKey()
.rawPublicKey,
fromRawPK
);

let newTransferDeploy = DeployUtil.deployFromJson(
DeployUtil.deployToJson(transferDeploy)
);

assert.deepEqual(
newTransferDeploy?.session.getArgByName('fromPublicKey')?.asPublicKey()
.rawPublicKey,
fromRawPK
);
});
});
38 changes: 34 additions & 4 deletions packages/sdk/test/lib/RuntimeArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { expect, assert } from 'chai';
import { CLValue, RuntimeArgs, CLTypedAndToBytesHelper } from '../../src/lib';
import {
CLValue,
RuntimeArgs,
CLTypedAndToBytesHelper,
Keys
} from '../../src/lib';
import { decodeBase16 } from '../../src';
import { TypedJSON } from 'typedjson';

Expand Down Expand Up @@ -56,8 +61,14 @@ describe(`RuntimeArgs`, () => {
let str = serializer.stringify(value);
let parsed = serializer.parse(str)!;
assert.deepEqual(
value.asOption().getSome().asBigNumber(),
parsed.asOption().getSome().asBigNumber()
value
.asOption()
.getSome()
.asBigNumber(),
parsed
.asOption()
.getSome()
.asBigNumber()
);
});

Expand All @@ -69,6 +80,25 @@ describe(`RuntimeArgs`, () => {
let serializer = new TypedJSON(RuntimeArgs);
let str = serializer.stringify(runtimeArgs);
let value = serializer.parse(str)!;
assert.isTrue(value.args.get('a')!.asOption().isNone());
assert.isTrue(
value.args
.get('a')!
.asOption()
.isNone()
);
});

it('should allow to extract lists of account hashes.', () => {
const account0 = Keys.Ed25519.new().accountHash();
const account1 = Keys.Ed25519.new().accountHash();
let runtimeArgs = RuntimeArgs.fromMap({
accounts: CLValue.list([
CLTypedAndToBytesHelper.bytes(account0),
CLTypedAndToBytesHelper.bytes(account1)
])
});
let accounts = runtimeArgs.args.get('accounts')!.asList();
assert.deepEqual(accounts[0].asBytesArray(), account0);
assert.deepEqual(accounts[1].asBytesArray(), account1);
});
});

0 comments on commit 5d4b036

Please sign in to comment.