Skip to content

Commit eb9cdc0

Browse files
Seulgi Kimsgkim126
authored andcommitted
Make RPC use PlatformAddress
1 parent d3111d7 commit eb9cdc0

File tree

8 files changed

+44
-28
lines changed

8 files changed

+44
-28
lines changed

examples/set-regular-key.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const SDK = require("codechain-sdk");
22

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

78
const regularSecret = SDK.util.generatePrivateKey();
89
const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
@@ -28,7 +29,7 @@ const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
2829

2930
const nonce2 = await sdk.rpc.chain.getNonce(masterAddress);
3031
const p2 = sdk.core.createPaymentParcel({
31-
recipient: sdk.key.classes.PlatformAddress.fromAccountId(masterAddress),
32+
recipient: masterAddress,
3233
amount: 10,
3334
});
3435
// We can sign a parcel with our `regularSecret`.

integration_tests/Rpc.spec.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ describe("rpc", () => {
99
const invalidHash = "0x0000000000000000000000000000000000000000000000000000000000000000";
1010
const signerSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
1111
const signerAccount = "0xa6594b7196808d161b6fb137e781abbc251385d9";
12+
const signerAddress = "cccqzn9jjm3j6qg69smd7cn0eup4w7z2yu9myd6c4d7";
1213

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

18+
test("PlatformAddress", () => {
19+
expect(sdk.key.classes.PlatformAddress.fromAccountId(signerAccount).value).toEqual(signerAddress);
20+
});
21+
1722
describe("node", () => {
1823
test("ping", async () => {
1924
expect(await sdk.rpc.node.ping()).toBe("pong");
@@ -50,18 +55,23 @@ describe("rpc", () => {
5055

5156
describe("with account", () => {
5257
const account = "0xa6594b7196808d161b6fb137e781abbc251385d9";
58+
const address = "cccqzn9jjm3j6qg69smd7cn0eup4w7z2yu9myd6c4d7";
59+
60+
test("PlatformAddress", () => {
61+
expect(sdk.key.classes.PlatformAddress.fromAccountId(account).value).toEqual(address);
62+
});
5363

5464
test("getBalance", async () => {
55-
expect(await sdk.rpc.chain.getBalance(account)).toEqual(expect.any(U256));
65+
expect(await sdk.rpc.chain.getBalance(address)).toEqual(expect.any(U256));
5666
});
5767

5868
test("getNonce", async () => {
59-
expect(await sdk.rpc.chain.getNonce(account)).toEqual(expect.any(U256));
69+
expect(await sdk.rpc.chain.getNonce(address)).toEqual(expect.any(U256));
6070
});
6171

6272
// FIXME: setRegularKey action isn't implemented.
6373
test.skip("getRegularKey", async () => {
64-
expect(await sdk.rpc.chain.getRegularKey(account)).toEqual(expect.any(H512));
74+
expect(await sdk.rpc.chain.getRegularKey(address)).toEqual(expect.any(H512));
6575
});
6676
});
6777

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

7383
beforeAll(async () => {
7484
const parcel = sdk.core.createPaymentParcel({
75-
recipient: sdk.key.classes.PlatformAddress.fromAccountId(signerAccount),
85+
recipient: signerAddress,
7686
amount: 10,
7787
});
7888
const signedParcel = parcel.sign({
7989
secret: signerSecret,
8090
fee: 10,
81-
nonce: await sdk.rpc.chain.getNonce(signerAccount),
91+
nonce: await sdk.rpc.chain.getNonce(signerAddress),
8292
});
8393
parcelHash = await sdk.rpc.chain.sendSignedParcel(signedParcel);
8494
});
@@ -120,7 +130,7 @@ describe("rpc", () => {
120130
});
121131
await sdk.rpc.chain.sendSignedParcel(parcel.sign({
122132
secret: signerSecret,
123-
nonce: await sdk.rpc.chain.getNonce(signerAccount),
133+
nonce: await sdk.rpc.chain.getNonce(signerAddress),
124134
fee: 10
125135
}));
126136
});
@@ -183,7 +193,7 @@ describe("rpc", () => {
183193
const parcel = sdk.core.createChangeShardStateParcel({ transactions: [mintTransaction, transferTransaction] });
184194
await sdk.rpc.chain.sendSignedParcel(parcel.sign({
185195
secret: signerSecret,
186-
nonce: await sdk.rpc.chain.getNonce(signerAccount),
196+
nonce: await sdk.rpc.chain.getNonce(signerAddress),
187197
fee: 10
188198
}));
189199
blockNumber = await sdk.rpc.chain.getBestBlockNumber();

integration_tests/SignedParcel.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ test("getSignerAccountId", async () => {
44
const sdk = new SDK({ server: process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080" });
55
const secret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
66
const accountId = sdk.util.getAccountIdFromPrivate(secret);
7-
const nonce = await sdk.rpc.chain.getNonce(accountId);
7+
const accountAddress = sdk.key.classes.PlatformAddress.fromAccountId(accountId);
8+
const nonce = await sdk.rpc.chain.getNonce(accountAddress);
89
const parcelToSend = sdk.core.createPaymentParcel({
910
amount: 10,
10-
recipient: sdk.key.classes.PlatformAddress.fromAccountId(accountId),
11+
recipient: accountAddress,
1112
}).sign({
1213
secret,
1314
fee: 10,

integration_tests/helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
44
const sdk = new SDK({ server: SERVER_URL });
55

66
const secret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
7-
const address = sdk.util.getAccountIdFromPrivate(secret);
7+
const accountId = sdk.util.getAccountIdFromPrivate(secret);
8+
const address = sdk.key.classes.PlatformAddress.fromAccountId(accountId);
89

910
export const sendTransactions = async ({ transactions }) => {
1011
const parcel = sdk.core.createChangeShardStateParcel({

integration_tests/sendSignedParcel.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { SDK } from "../";
33
const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
44
const sdk = new SDK({ server: SERVER_URL });
55
const secret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
6-
const address = SDK.util.getAccountIdFromPrivate(secret);
6+
const accountId = SDK.util.getAccountIdFromPrivate(secret);
7+
const address = sdk.key.classes.PlatformAddress.fromAccountId(accountId);
78

89
test("sendSignedParcel", async () => {
910
const nonce = await sdk.rpc.chain.getNonce(address);
1011
const p = sdk.core.createPaymentParcel({
11-
recipient: sdk.key.classes.PlatformAddress.fromAccountId(address),
12+
recipient: address,
1213
amount: 0,
1314
});
1415
const hash = await sdk.rpc.chain.sendSignedParcel(p.sign({

integration_tests/setRegularKey.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const U256 = SDK.Core.classes.U256;
55
const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
66
const sdk = new SDK({ server: SERVER_URL });
77
const masterSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
8-
const masterAddress = SDK.util.getAccountIdFromPrivate(masterSecret);
8+
const masterAccountId = SDK.util.getAccountIdFromPrivate(masterSecret);
9+
const masterAddress = sdk.key.classes.PlatformAddress.fromAccountId(masterAccountId);
910

1011
const regularSecret = SDK.util.generatePrivateKey();
1112
const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
@@ -30,7 +31,7 @@ test("setRegularKey", async () => {
3031

3132
const nonce2 = await sdk.rpc.chain.getNonce(masterAddress);
3233
const p2 = sdk.core.createPaymentParcel({
33-
recipient: sdk.key.classes.PlatformAddress.fromAccountId(masterAddress),
34+
recipient: masterAddress,
3435
amount: 10,
3536
});
3637
const hash2 = await sdk.rpc.chain.sendSignedParcel(p2.sign({

integration_tests/useRegistrar.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { SDK } from "../";
33
const SERVER_URL = process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080";
44
const sdk = new SDK({ server: SERVER_URL });
55
const masterSecret = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
6-
const masterAddress = SDK.util.getAccountIdFromPrivate(masterSecret);
6+
const masterAccountId = SDK.util.getAccountIdFromPrivate(masterSecret);
7+
const masterAddress = sdk.key.classes.PlatformAddress.fromAccountId(masterAccountId);
78

89
const otherSecret = "0000000000000000000000000000000000000000000000000000000000000001";
9-
const otherAddress = SDK.util.getAccountIdFromPrivate(otherSecret);
10+
const otherAccountId = SDK.util.getAccountIdFromPrivate(otherSecret);
11+
const otherAddress = sdk.key.classes.PlatformAddress.fromAccountId(otherAccountId);
1012

1113
const regularSecret = SDK.util.generatePrivateKey();
1214
const regularPublic = SDK.util.getPublicFromPrivate(regularSecret);
@@ -44,7 +46,7 @@ async function setRegularKey() {
4446
async function sendCCCToOther() {
4547
const nonce = await sdk.rpc.chain.getNonce(masterAddress);
4648
const p = sdk.core.createPaymentParcel({
47-
recipient: sdk.key.classes.PlatformAddress.fromAccountId(otherAddress),
49+
recipient: otherAddress,
4850
amount: 100,
4951
});
5052
const hash = await sdk.rpc.chain.sendSignedParcel(p.sign({
@@ -67,7 +69,7 @@ async function mintAssetUsingMaster(p2pkh, aliceAddress, bobAddress) {
6769
icon_url: "https://gold.image/",
6870
}),
6971
amount: 10000,
70-
registrar: sdk.key.classes.PlatformAddress.fromAccountId(masterAddress),
72+
registrar: masterAddress,
7173
});
7274

7375
const mintTx = sdk.core.createAssetMintTransaction({

src/rpc/chain.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Rpc } from ".";
22
import { H256 } from "../core/H256";
33
import { SignedParcel } from "../core/SignedParcel";
4-
import { H160 } from "../core/H160";
54
import { U256 } from "../core/U256";
65
import { AssetScheme } from "../core/AssetScheme";
76
import { Block } from "../core/Block";
@@ -55,7 +54,7 @@ export class ChainRpc {
5554
* @throws When the given passphrase does not match
5655
*/
5756
async sendParcel(parcel: Parcel, options?: {
58-
account?: PlatformAddress | H160 | string,
57+
account?: PlatformAddress | string,
5958
passphrase?: string,
6059
nonce?: U256 | string | number,
6160
fee?: U256 | string | number,
@@ -128,10 +127,10 @@ export class ChainRpc {
128127
* @param blockNumber The specific block number to get account's regular key at given address.
129128
* @returns The regular key of account at specified block, or null when address was not found.
130129
*/
131-
getRegularKey(address: PlatformAddress | H160 | string, blockNumber?: number): Promise<H512 | null> {
130+
getRegularKey(address: PlatformAddress | string, blockNumber?: number): Promise<H512 | null> {
132131
return this.rpc.sendRpcRequest(
133132
"chain_getRegularKey",
134-
[`0x${PlatformAddress.ensureAccount(address).value}`, blockNumber || null]
133+
[`${PlatformAddress.ensure(address).value}`, blockNumber || null]
135134
).then(result => result === null ? null : new H512(result));
136135
}
137136

@@ -176,10 +175,10 @@ export class ChainRpc {
176175
* @param blockNumber The specific block number to get account's balance at given address.
177176
* @returns Balance of account at specified block, or null when address was not found.
178177
*/
179-
getBalance(address: PlatformAddress | H160 | string, blockNumber?: number): Promise<U256 | null> {
178+
getBalance(address: PlatformAddress | string, blockNumber?: number): Promise<U256 | null> {
180179
return this.rpc.sendRpcRequest(
181180
"chain_getBalance",
182-
[`0x${PlatformAddress.ensureAccount(address).value}`, blockNumber]
181+
[`${PlatformAddress.ensure(address).value}`, blockNumber]
183182
).then(result => result ? new U256(result) : null);
184183
}
185184

@@ -189,10 +188,10 @@ export class ChainRpc {
189188
* @param blockNumber The specific block number to get account's nonce at given address.
190189
* @returns Nonce of account at specified block, or null when address was not found.
191190
*/
192-
getNonce(address: PlatformAddress | H160 | string, blockNumber?: number): Promise<U256 | null> {
191+
getNonce(address: PlatformAddress | string, blockNumber?: number): Promise<U256 | null> {
193192
return this.rpc.sendRpcRequest(
194193
"chain_getNonce",
195-
[`0x${PlatformAddress.ensureAccount(address).value}`, blockNumber]
194+
[`${PlatformAddress.ensure(address).value}`, blockNumber]
196195
).then(result => result ? new U256(result) : null);
197196
}
198197

0 commit comments

Comments
 (0)