Skip to content
Merged
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
3 changes: 2 additions & 1 deletion examples/set-regular-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const SDK = require("codechain-sdk");

const sdk = new SDK({ server: "http://localhost:8080" });
const masterSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const masterAddress = SDK.util.getAccountIdFromPrivate(masterSecret);
const masterAccountId = SDK.util.getAccountIdFromPrivate(masterSecret);
const masterAddress = sdk.key.classes.PlatformAddress.fromAccountId(masterAccountId);

const regularSecret = SDK.util.generatePrivateKey();
const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
Expand Down
24 changes: 17 additions & 7 deletions integration_tests/Rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ describe("rpc", () => {
const invalidHash = "0x0000000000000000000000000000000000000000000000000000000000000000";
const signerSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const signerAccount = "0xa6594b7196808d161b6fb137e781abbc251385d9";
const signerAddress = "cccqzn9jjm3j6qg69smd7cn0eup4w7z2yu9myd6c4d7";

beforeAll(async () => {
sdk = new SDK({ server: "http://localhost:8080" });
});

test("PlatformAddress", () => {
expect(sdk.key.classes.PlatformAddress.fromAccountId(signerAccount).value).toEqual(signerAddress);
});

describe("node", () => {
test("ping", async () => {
expect(await sdk.rpc.node.ping()).toBe("pong");
Expand Down Expand Up @@ -50,18 +55,23 @@ describe("rpc", () => {

describe("with account", () => {
const account = "0xa6594b7196808d161b6fb137e781abbc251385d9";
const address = "cccqzn9jjm3j6qg69smd7cn0eup4w7z2yu9myd6c4d7";

test("PlatformAddress", () => {
expect(sdk.key.classes.PlatformAddress.fromAccountId(account).value).toEqual(address);
});

test("getBalance", async () => {
expect(await sdk.rpc.chain.getBalance(account)).toEqual(expect.any(U256));
expect(await sdk.rpc.chain.getBalance(address)).toEqual(expect.any(U256));
});

test("getNonce", async () => {
expect(await sdk.rpc.chain.getNonce(account)).toEqual(expect.any(U256));
expect(await sdk.rpc.chain.getNonce(address)).toEqual(expect.any(U256));
});

// FIXME: setRegularKey action isn't implemented.
test.skip("getRegularKey", async () => {
expect(await sdk.rpc.chain.getRegularKey(account)).toEqual(expect.any(H512));
expect(await sdk.rpc.chain.getRegularKey(address)).toEqual(expect.any(H512));
});
});

Expand All @@ -72,13 +82,13 @@ describe("rpc", () => {

beforeAll(async () => {
const parcel = sdk.core.createPaymentParcel({
recipient: signerAccount,
recipient: signerAddress,
amount: 10,
});
const signedParcel = parcel.sign({
secret: signerSecret,
fee: 10,
nonce: await sdk.rpc.chain.getNonce(signerAccount),
nonce: await sdk.rpc.chain.getNonce(signerAddress),
});
parcelHash = await sdk.rpc.chain.sendSignedParcel(signedParcel);
});
Expand Down Expand Up @@ -120,7 +130,7 @@ describe("rpc", () => {
});
await sdk.rpc.chain.sendSignedParcel(parcel.sign({
secret: signerSecret,
nonce: await sdk.rpc.chain.getNonce(signerAccount),
nonce: await sdk.rpc.chain.getNonce(signerAddress),
fee: 10
}));
});
Expand Down Expand Up @@ -183,7 +193,7 @@ describe("rpc", () => {
const parcel = sdk.core.createChangeShardStateParcel({ transactions: [mintTransaction, transferTransaction] });
await sdk.rpc.chain.sendSignedParcel(parcel.sign({
secret: signerSecret,
nonce: await sdk.rpc.chain.getNonce(signerAccount),
nonce: await sdk.rpc.chain.getNonce(signerAddress),
fee: 10
}));
blockNumber = await sdk.rpc.chain.getBestBlockNumber();
Expand Down
5 changes: 3 additions & 2 deletions integration_tests/SignedParcel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ test("getSignerAccountId", async () => {
const sdk = new SDK({ server: process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080" });
const secret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const accountId = sdk.util.getAccountIdFromPrivate(secret);
const nonce = await sdk.rpc.chain.getNonce(accountId);
const accountAddress = sdk.key.classes.PlatformAddress.fromAccountId(accountId);
const nonce = await sdk.rpc.chain.getNonce(accountAddress);
const parcelToSend = sdk.core.createPaymentParcel({
amount: 10,
recipient: accountId,
recipient: accountAddress,
}).sign({
secret,
fee: 10,
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
const sdk = new SDK({ server: SERVER_URL });

const secret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const address = sdk.util.getAccountIdFromPrivate(secret);
const accountId = sdk.util.getAccountIdFromPrivate(secret);
const address = sdk.key.classes.PlatformAddress.fromAccountId(accountId);

export const sendTransactions = async ({ transactions }) => {
const parcel = sdk.core.createChangeShardStateParcel({
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/sendSignedParcel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { SDK } from "../";
const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
const sdk = new SDK({ server: SERVER_URL });
const secret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const address = SDK.util.getAccountIdFromPrivate(secret);
const accountId = SDK.util.getAccountIdFromPrivate(secret);
const address = sdk.key.classes.PlatformAddress.fromAccountId(accountId);

test("sendSignedParcel", async () => {
const nonce = await sdk.rpc.chain.getNonce(address);
Expand Down
4 changes: 3 additions & 1 deletion integration_tests/setRegularKey.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SDK } from "../";

const U256 = SDK.Core.classes.U256;

const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
const sdk = new SDK({ server: SERVER_URL });
const masterSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const masterAddress = SDK.util.getAccountIdFromPrivate(masterSecret);
const masterAccountId = SDK.util.getAccountIdFromPrivate(masterSecret);
const masterAddress = sdk.key.classes.PlatformAddress.fromAccountId(masterAccountId);

const regularSecret = SDK.util.generatePrivateKey();
const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
Expand Down
6 changes: 4 additions & 2 deletions integration_tests/useRegistrar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { SDK } from "../";
const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
const sdk = new SDK({ server: SERVER_URL });
const masterSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
const masterAddress = SDK.util.getAccountIdFromPrivate(masterSecret);
const masterAccountId = SDK.util.getAccountIdFromPrivate(masterSecret);
const masterAddress = sdk.key.classes.PlatformAddress.fromAccountId(masterAccountId);

const otherSecret = "0000000000000000000000000000000000000000000000000000000000000001";
const otherAddress = SDK.util.getAccountIdFromPrivate(otherSecret);
const otherAccountId = SDK.util.getAccountIdFromPrivate(otherSecret);
const otherAddress = sdk.key.classes.PlatformAddress.fromAccountId(otherAccountId);

const regularSecret = SDK.util.generatePrivateKey();
const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
Expand Down
9 changes: 4 additions & 5 deletions src/core/AssetScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { AssetTransferAddress } from "../key/AssetTransferAddress";
import { PlatformAddress } from "../key/classes";

import { H160 } from "./H160";
import { AssetMintTransaction } from "./transaction/AssetMintTransaction";

type NetworkId = string;
Expand All @@ -13,7 +12,7 @@ export type AssetSchemeData = {
worldId: number;
metadata: string;
amount: number;
registrar: PlatformAddress | H160 | string | null;
registrar: PlatformAddress | null;
};
/**
* Object that contains information about the Asset when performing AssetMintTransaction.
Expand All @@ -24,14 +23,14 @@ export class AssetScheme {
worldId: number;
metadata: string;
amount: number;
registrar: H160 | null;
registrar: PlatformAddress | null;

constructor(data: AssetSchemeData) {
this.networkId = data.networkId;
this.shardId = data.shardId;
this.worldId = data.worldId;
this.metadata = data.metadata;
this.registrar = data.registrar === null ? null : PlatformAddress.ensureAccount(data.registrar);
this.registrar = data.registrar;
this.amount = data.amount;
}

Expand All @@ -45,7 +44,7 @@ export class AssetScheme {
networkId,
metadata,
amount,
registrar: registrar === null ? null : registrar.value
registrar: registrar === null ? null : registrar.toString()
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/core/Block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H160 } from "./H160";
import { PlatformAddress } from "../key/PlatformAddress";
import { H256 } from "./H256";
import { U256 } from "./U256";
import { SignedParcel } from "./SignedParcel";
Expand All @@ -7,7 +7,7 @@ export type BlockData = {
parentHash: H256;
timestamp: number;
number: number;
author: H160;
author: PlatformAddress;
extraData: Buffer;
parcelsRoot: H256;
stateRoot: H256;
Expand All @@ -24,7 +24,7 @@ export class Block {
parentHash: H256;
timestamp: number;
number: number;
author: H160;
author: PlatformAddress;
extraData: Buffer;
parcelsRoot: H256;
stateRoot: H256;
Expand Down Expand Up @@ -58,7 +58,7 @@ export class Block {
parentHash: new H256(parentHash),
timestamp,
number,
author: new H160(author),
author: PlatformAddress.fromString(author),
extraData,
parcelsRoot: new H256(parcelsRoot),
stateRoot: new H256(stateRoot),
Expand All @@ -77,7 +77,7 @@ export class Block {
parentHash: parentHash.value,
timestamp,
number,
author: author.value,
author: author.toString(),
extraData,
parcelsRoot: parcelsRoot.value,
stateRoot: stateRoot.value,
Expand Down
4 changes: 2 additions & 2 deletions src/core/Parcel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PlatformAddress } from "../key/PlatformAddress";
import { U256 } from "./U256";
import { H160 } from "./H160";
import { H256 } from "./H256";
import { H512 } from "./H512";
import { SignedParcel } from "./SignedParcel";
Expand Down Expand Up @@ -35,7 +35,7 @@ export class Parcel {
return new Parcel(networkId, action);
}

static payment(networkId: NetworkId, receiver: H160, value: U256): Parcel {
static payment(networkId: NetworkId, receiver: PlatformAddress, value: U256): Parcel {
const action = new Payment(receiver, value);
return new Parcel(networkId, action);
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/__test__/Block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Block } from "../Block";
import { H256 } from "../H256";
import { H160 } from "../H160";
import { U256 } from "../U256";
import { Parcel } from "../Parcel";
import { PlatformAddress } from "../../key/PlatformAddress";
import { getAccountIdFromPrivate } from "../../utils";

test("toJSON", () => {
const secret = new H256("ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd");
const sender = new H160(getAccountIdFromPrivate(secret.value));
const p = Parcel.payment("tc", new H160("0x2222222222222222222222222222222222222222"), new U256(11)).sign({
const sender = PlatformAddress.fromAccountId(getAccountIdFromPrivate(secret.value));
const p = Parcel.payment("tc", PlatformAddress.fromAccountId("0x2222222222222222222222222222222222222222"), new U256(11)).sign({
secret,
fee: 33,
nonce: 44
Expand All @@ -17,7 +17,7 @@ test("toJSON", () => {
parentHash: new H256("0000000000000000000000000000000000000000000000000000000000000000"),
timestamp: 1,
number: 2,
author: new H160("1111111111111111111111111111111111111111"),
author: PlatformAddress.fromAccountId("1111111111111111111111111111111111111111"),
extraData: Buffer.from([]),
parcelsRoot: new H256("1111111111111111111111111111111111111111111111111111111111111111"),
stateRoot: new H256("2222222222222222222222222222222222222222222222222222222222222222"),
Expand Down
4 changes: 2 additions & 2 deletions src/core/__test__/Parcel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { H160 } from "../H160";
import { H256 } from "../H256";
import { U256 } from "../U256";
import { Parcel } from "../Parcel";
import { PlatformAddress } from "../../key/PlatformAddress";

test("rlp", () => {
const t = Parcel.transactions("tc");
Expand Down Expand Up @@ -42,7 +42,7 @@ test("signed hash", () => {
});

test("toJSON", () => {
const p = Parcel.payment("tc", new H160("0x0000000000000000000000000000000000000000"), new U256(11));
const p = Parcel.payment("tc", PlatformAddress.fromAccountId("0x0000000000000000000000000000000000000000"), new U256(11));
p.setFee(33);
p.setNonce(44);
expect(Parcel.fromJSON(p.toJSON())).toEqual(p);
Expand Down
11 changes: 5 additions & 6 deletions src/core/__test__/SignedParcel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PlatformAddress } from "../../key/classes";

import { H160 } from "../H160";
import { H256 } from "../H256";
import { U256 } from "../U256";
import { Parcel } from "../Parcel";
Expand All @@ -9,7 +8,7 @@ import { getAccountIdFromPrivate } from "../../utils";

test("toJSON", () => {
const secret = new H256("ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd");
const p = Parcel.payment("tc", new H160("0x0000000000000000000000000000000000000000"), new U256(11)).sign({
const p = Parcel.payment("tc", PlatformAddress.fromAccountId("0x0000000000000000000000000000000000000000"), new U256(11)).sign({
secret,
fee: 33,
nonce: 33
Expand All @@ -19,8 +18,8 @@ test("toJSON", () => {

test("getSignerAccountId", () => {
const secret = new H256("ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd");
const signerAccountId = new H160(getAccountIdFromPrivate(secret.value));
const p = Parcel.payment("tc", new H160("0x0000000000000000000000000000000000000000"), new U256(11)).sign({
const signerAccountId = PlatformAddress.fromAccountId(getAccountIdFromPrivate(secret.value)).getAccountId();
const p = Parcel.payment("tc", PlatformAddress.fromAccountId("0x0000000000000000000000000000000000000000"), new U256(11)).sign({
secret,
fee: 33,
nonce: 44
Expand All @@ -30,9 +29,9 @@ test("getSignerAccountId", () => {

test("getSignerAddress", () => {
const secret = new H256("ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd");
const signerAccountId = new H160(getAccountIdFromPrivate(secret.value));
const signerAccountId = PlatformAddress.fromAccountId(getAccountIdFromPrivate(secret.value)).getAccountId();
const signerAddress = PlatformAddress.fromAccountId(signerAccountId);
const p = Parcel.payment("tc", new H160("0x0000000000000000000000000000000000000000"), new U256(11)).sign({
const p = Parcel.payment("tc", PlatformAddress.fromAccountId("0x0000000000000000000000000000000000000000"), new U256(11)).sign({
secret,
fee: 33,
nonce: 44
Expand Down
8 changes: 4 additions & 4 deletions src/core/action/Action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PlatformAddress } from "../../key/PlatformAddress";
import { getTransactionFromJSON } from "../transaction/Transaction";
import { H160 } from "../H160";
import { U256 } from "../U256";
import { H512 } from "../H512";

Expand All @@ -20,18 +20,18 @@ export function getActionFromJSON(json: any): Action {
return new ChangeShardState({ transactions: transactions.map(getTransactionFromJSON) });
case "payment":
const { receiver, amount } = json;
return new Payment(new H160(receiver), new U256(amount));
return new Payment(PlatformAddress.ensure(receiver), new U256(amount));
case "setRegularKey":
const { key } = json;
return new SetRegularKey(new H512(key));
case "createShard":
return new CreateShard();
case "changeShardOwners":
const { shardId, owners } = json;
return new ChangeShardOwners({ shardId, owners: owners.map(H160.ensure) });
return new ChangeShardOwners({ shardId, owners: owners.map(PlatformAddress.ensure) });
case "changeShardUsers": {
const { shardId, users } = json;
return new ChangeShardUsers({ shardId, users: users.map(H160.ensure) });
return new ChangeShardUsers({ shardId, users: users.map(PlatformAddress.ensure) });
}
default:
throw new Error(`Unexpected parcel action: ${action}`);
Expand Down
8 changes: 4 additions & 4 deletions src/core/action/ChangeShardOwners.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { H160 } from "../H160";
import { PlatformAddress } from "../../key/PlatformAddress";

export class ChangeShardOwners {
public readonly shardId: number;
public readonly owners: H160[];
constructor(params: { shardId: number, owners: H160[] }) {
public readonly owners: PlatformAddress[];
constructor(params: { shardId: number, owners: PlatformAddress[] }) {
const { shardId, owners } = params;
this.shardId = shardId;
this.owners = owners;
}

toEncodeObject(): Array<any> {
const { shardId, owners } = this;
return [5, shardId, owners.map(owner => owner.toEncodeObject())];
return [5, shardId, owners.map(owner => owner.getAccountId().toEncodeObject())];
}

toJSON() {
Expand Down
Loading