From 7f0ecb3edecb6f500219f7ba16aa18aa8419cc7e Mon Sep 17 00:00:00 2001 From: JinGyeong Jeong Date: Mon, 17 Sep 2018 20:46:39 +0900 Subject: [PATCH] Move the address classes from Key module to Core module --- examples/create-account-with-secret.js | 2 +- examples/set-regular-key.js | 2 +- integration_tests/Rpc.spec.ts | 7 +- integration_tests/helper.ts | 4 +- integration_tests/useRegistrar.spec.ts | 2 +- src/core/Asset.ts | 3 +- src/core/AssetScheme.ts | 4 +- src/core/Block.ts | 3 +- src/core/Parcel.ts | 3 +- src/core/SignedParcel.ts | 2 +- src/core/__test__/Block.spec.ts | 3 +- src/core/__test__/Parcel.spec.ts | 3 +- src/core/__test__/SignedParcel.spec.ts | 2 +- src/core/action/Action.ts | 3 +- src/core/action/Payment.ts | 3 +- src/core/action/SetShardOwners.ts | 2 +- src/core/action/SetShardUsers.ts | 2 +- .../action/__test__/SetShardOwners.spec.ts | 3 +- .../action/__test__/SetShardUsers.spec.ts | 3 +- src/core/classes.ts | 2 + src/core/index.ts | 8 +- src/core/transaction/AssetMintTransaction.ts | 2 +- src/core/transaction/AssetTransferOutput.ts | 2 +- .../transaction/AssetTransferTransaction.ts | 3 +- .../transaction/CreateWorldTransaction.ts | 3 +- .../transaction/SetWorldOwnersTransaction.ts | 3 +- .../transaction/SetWorldUsersTransaction.ts | 3 +- .../__test__/CreateWorldTransaction.spec.ts | 3 +- .../SetWorldOwnersTransaction.spec.ts | 3 +- .../__test__/SetWorldUsersTransaction.spec.ts | 3 +- src/key/AssetTransferAddress.ts | 1 - src/key/P2PKH.ts | 2 +- src/key/P2PKHBurn.ts | 2 +- src/key/PlatformAddress.ts | 1 - src/key/__test__/AssetTransferAddress.spec.ts | 33 --- src/key/__test__/PlatformAddress.spec.ts | 88 -------- src/key/__test__/bech32.spec.ts | 14 -- src/key/bech32.ts | 191 ------------------ src/key/classes.ts | 3 - src/key/index.ts | 6 +- src/rpc/__test__/invalid-response.spec.ts | 3 +- src/rpc/account.ts | 3 +- src/rpc/chain.ts | 3 +- 43 files changed, 63 insertions(+), 378 deletions(-) delete mode 100644 src/key/AssetTransferAddress.ts delete mode 100644 src/key/PlatformAddress.ts delete mode 100644 src/key/__test__/AssetTransferAddress.spec.ts delete mode 100644 src/key/__test__/PlatformAddress.spec.ts delete mode 100644 src/key/__test__/bech32.spec.ts delete mode 100644 src/key/bech32.ts diff --git a/examples/create-account-with-secret.js b/examples/create-account-with-secret.js index 65d2c3a2..33b16cb7 100644 --- a/examples/create-account-with-secret.js +++ b/examples/create-account-with-secret.js @@ -4,5 +4,5 @@ var secret = SDK.util.generatePrivateKey(); console.log("Your secret:", secret); var account = SDK.util.getAccountIdFromPrivate(secret); -var address = SDK.Key.classes.PlatformAddress.fromAccountId(account); +var address = SDK.Core.classes.PlatformAddress.fromAccountId(account); console.log("Your CodeChain address:", address.toString()); diff --git a/examples/set-regular-key.js b/examples/set-regular-key.js index a48ef992..7dd17d00 100644 --- a/examples/set-regular-key.js +++ b/examples/set-regular-key.js @@ -11,7 +11,7 @@ var ACCOUNT_SECRET = const masterSecret = ACCOUNT_SECRET; const masterAccountId = SDK.util.getAccountIdFromPrivate(masterSecret); -const masterAddress = sdk.key.classes.PlatformAddress.fromAccountId( +const masterAddress = sdk.core.classes.PlatformAddress.fromAccountId( masterAccountId ); diff --git a/integration_tests/Rpc.spec.ts b/integration_tests/Rpc.spec.ts index 8b2e2544..32e57cb8 100644 --- a/integration_tests/Rpc.spec.ts +++ b/integration_tests/Rpc.spec.ts @@ -7,9 +7,9 @@ import { H256, Invoice, Parcel, + PlatformAddress, SignedParcel } from "../lib/core/classes"; -import { PlatformAddress } from "../lib/key/classes"; import { generatePrivateKey, getAccountIdFromPrivate, @@ -110,7 +110,7 @@ describe("rpc", () => { test("PlatformAddress", () => { expect( - sdk.key.classes.PlatformAddress.fromAccountId(signerAccount).value + sdk.core.classes.PlatformAddress.fromAccountId(signerAccount).value ).toEqual(signerAddress); }); @@ -162,7 +162,8 @@ describe("rpc", () => { test("PlatformAddress", () => { expect( - sdk.key.classes.PlatformAddress.fromAccountId(account).value + sdk.core.classes.PlatformAddress.fromAccountId(account) + .value ).toEqual(address); }); diff --git a/integration_tests/helper.ts b/integration_tests/helper.ts index 74d21a5b..dcb3b428 100644 --- a/integration_tests/helper.ts +++ b/integration_tests/helper.ts @@ -17,7 +17,7 @@ export const ACCOUNT_ID = sdk.util.getAccountIdFromPrivate(ACCOUNT_SECRET).toString(); // "0xa6594b7196808d161b6fb137e781abbc251385d9" export const ACCOUNT_ADDRESS = process.env.ACCOUNT_ADDRESS || - sdk.key.classes.PlatformAddress.fromAccountId(ACCOUNT_ID).toString(); // "tccqzn9jjm3j6qg69smd7cn0eup4w7z2yu9my9a2k78" + sdk.core.classes.PlatformAddress.fromAccountId(ACCOUNT_ID).toString(); // "tccqzn9jjm3j6qg69smd7cn0eup4w7z2yu9my9a2k78" export const ACCOUNT_PASSPHRASE = process.env.ACCOUNT_PASSPHRASE || "satoshi"; export const sendTransactions = async ({ transactions }: any) => { @@ -48,7 +48,7 @@ export const mintAsset = async ({ amount, registrar }); - const assetAddress = sdk.key.classes.AssetTransferAddress.fromTypeAndPayload( + const assetAddress = sdk.core.classes.AssetTransferAddress.fromTypeAndPayload( 0, lockScriptHash ); diff --git a/integration_tests/useRegistrar.spec.ts b/integration_tests/useRegistrar.spec.ts index 2d636aec..2d28be54 100644 --- a/integration_tests/useRegistrar.spec.ts +++ b/integration_tests/useRegistrar.spec.ts @@ -18,7 +18,7 @@ const masterAddress = ACCOUNT_ADDRESS; const otherSecret = "0000000000000000000000000000000000000000000000000000000000000001"; const otherAccountId = SDK.util.getAccountIdFromPrivate(otherSecret); -const otherAddress = sdk.key.classes.PlatformAddress.fromAccountId( +const otherAddress = sdk.core.classes.PlatformAddress.fromAccountId( otherAccountId ); diff --git a/src/core/Asset.ts b/src/core/Asset.ts index 1234bd04..d1d9f97c 100644 --- a/src/core/Asset.ts +++ b/src/core/Asset.ts @@ -1,6 +1,5 @@ import { Buffer } from "buffer"; - -import { AssetTransferAddress } from "../key/AssetTransferAddress"; +import { AssetTransferAddress } from "codechain-primitives"; import { H256 } from "./H256"; import { AssetOutPoint } from "./transaction/AssetOutPoint"; diff --git a/src/core/AssetScheme.ts b/src/core/AssetScheme.ts index 06ecae68..a68be59e 100644 --- a/src/core/AssetScheme.ts +++ b/src/core/AssetScheme.ts @@ -1,6 +1,4 @@ -// FIXME: Use interface instead of importing key class. -import { AssetTransferAddress } from "../key/AssetTransferAddress"; -import { PlatformAddress } from "../key/classes"; +import { AssetTransferAddress, PlatformAddress } from "codechain-primitives"; import { AssetMintTransaction } from "./transaction/AssetMintTransaction"; import { NetworkId } from "./types"; diff --git a/src/core/Block.ts b/src/core/Block.ts index 51807083..68eaf579 100644 --- a/src/core/Block.ts +++ b/src/core/Block.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { H256 } from "./H256"; import { SignedParcel } from "./SignedParcel"; import { U256 } from "./U256"; diff --git a/src/core/Parcel.ts b/src/core/Parcel.ts index bcb2247a..e37159a2 100644 --- a/src/core/Parcel.ts +++ b/src/core/Parcel.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { blake256, signEcdsa } from "../utils"; import { Action, getActionFromJSON } from "./action/Action"; import { AssetTransactionGroup } from "./action/AssetTransactionGroup"; diff --git a/src/core/SignedParcel.ts b/src/core/SignedParcel.ts index 2b95a6e5..0782cc07 100644 --- a/src/core/SignedParcel.ts +++ b/src/core/SignedParcel.ts @@ -1,6 +1,6 @@ +import { PlatformAddress } from "codechain-primitives"; import * as _ from "lodash"; -import { PlatformAddress } from "../key/classes"; import { blake256, recoverEcdsa, ripemd160 } from "../utils"; import { H160 } from "./H160"; diff --git a/src/core/__test__/Block.spec.ts b/src/core/__test__/Block.spec.ts index 45843eed..aa1bf318 100644 --- a/src/core/__test__/Block.spec.ts +++ b/src/core/__test__/Block.spec.ts @@ -1,8 +1,9 @@ +import { PlatformAddress } from "codechain-primitives"; + import { Block } from "../Block"; import { H256 } from "../H256"; import { U256 } from "../U256"; import { Parcel } from "../Parcel"; -import { PlatformAddress } from "../../key/PlatformAddress"; import { getAccountIdFromPrivate } from "../../utils"; test("toJSON", () => { diff --git a/src/core/__test__/Parcel.spec.ts b/src/core/__test__/Parcel.spec.ts index bbb03038..1c524f40 100644 --- a/src/core/__test__/Parcel.spec.ts +++ b/src/core/__test__/Parcel.spec.ts @@ -1,7 +1,8 @@ +import { PlatformAddress } from "codechain-primitives"; + import { H256 } from "../H256"; import { U256 } from "../U256"; import { Parcel } from "../Parcel"; -import { PlatformAddress } from "../../key/PlatformAddress"; test("rlp", () => { const t = Parcel.transactions("tc"); diff --git a/src/core/__test__/SignedParcel.spec.ts b/src/core/__test__/SignedParcel.spec.ts index 5454bdb1..93805eed 100644 --- a/src/core/__test__/SignedParcel.spec.ts +++ b/src/core/__test__/SignedParcel.spec.ts @@ -1,4 +1,4 @@ -import { PlatformAddress } from "../../key/classes"; +import { PlatformAddress } from "codechain-primitives"; import { H256 } from "../H256"; import { U256 } from "../U256"; diff --git a/src/core/action/Action.ts b/src/core/action/Action.ts index b3260325..0f5b99e5 100644 --- a/src/core/action/Action.ts +++ b/src/core/action/Action.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { H512 } from "../H512"; import { getTransactionFromJSON } from "../transaction/Transaction"; import { U256 } from "../U256"; diff --git a/src/core/action/Payment.ts b/src/core/action/Payment.ts index baa10719..7c13e9fd 100644 --- a/src/core/action/Payment.ts +++ b/src/core/action/Payment.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { U256 } from "../U256"; export class Payment { diff --git a/src/core/action/SetShardOwners.ts b/src/core/action/SetShardOwners.ts index f959644d..5eb890b3 100644 --- a/src/core/action/SetShardOwners.ts +++ b/src/core/action/SetShardOwners.ts @@ -1,4 +1,4 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; export class SetShardOwners { public readonly shardId: number; diff --git a/src/core/action/SetShardUsers.ts b/src/core/action/SetShardUsers.ts index 06eb0a84..2f6d5ebd 100644 --- a/src/core/action/SetShardUsers.ts +++ b/src/core/action/SetShardUsers.ts @@ -1,4 +1,4 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; export class SetShardUsers { public readonly shardId: number; diff --git a/src/core/action/__test__/SetShardOwners.spec.ts b/src/core/action/__test__/SetShardOwners.spec.ts index 820f4de3..fd6714c6 100644 --- a/src/core/action/__test__/SetShardOwners.spec.ts +++ b/src/core/action/__test__/SetShardOwners.spec.ts @@ -1,5 +1,6 @@ +import { PlatformAddress } from "codechain-primitives"; + import { SetShardOwners } from "../SetShardOwners"; -import { PlatformAddress } from "../../../key/PlatformAddress"; import { getActionFromJSON } from "../Action"; describe("SetShardOwners", () => { diff --git a/src/core/action/__test__/SetShardUsers.spec.ts b/src/core/action/__test__/SetShardUsers.spec.ts index 29ea4347..880a0ef0 100644 --- a/src/core/action/__test__/SetShardUsers.spec.ts +++ b/src/core/action/__test__/SetShardUsers.spec.ts @@ -1,5 +1,6 @@ +import { PlatformAddress } from "codechain-primitives"; + import { SetShardUsers } from "../SetShardUsers"; -import { PlatformAddress } from "../../../key/PlatformAddress"; import { getActionFromJSON } from "../Action"; describe("SetShardUsers", () => { diff --git a/src/core/classes.ts b/src/core/classes.ts index 12f7ae09..2ee906a7 100644 --- a/src/core/classes.ts +++ b/src/core/classes.ts @@ -36,3 +36,5 @@ export { Asset } from "./Asset"; export { AssetScheme } from "./AssetScheme"; export { Script } from "./Script"; + +export { PlatformAddress, AssetTransferAddress } from "codechain-primitives"; diff --git a/src/core/index.ts b/src/core/index.ts index 4106611a..45beb863 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,5 +1,4 @@ -// FIXME: Use interface instead of importing key class. -import { AssetTransferAddress, PlatformAddress } from "../key/classes"; +import { AssetTransferAddress, PlatformAddress } from "codechain-primitives"; import { AssetTransactionGroup } from "./action/AssetTransactionGroup"; import { CreateShard } from "./action/CreateShard"; @@ -64,7 +63,10 @@ export class Core { Asset, AssetScheme, // Script - Script + Script, + // Addresses + PlatformAddress, + AssetTransferAddress }; public classes = Core.classes; diff --git a/src/core/transaction/AssetMintTransaction.ts b/src/core/transaction/AssetMintTransaction.ts index f53c5ead..bb9ee207 100644 --- a/src/core/transaction/AssetMintTransaction.ts +++ b/src/core/transaction/AssetMintTransaction.ts @@ -1,6 +1,6 @@ import { Buffer } from "buffer"; +import { AssetTransferAddress, PlatformAddress } from "codechain-primitives"; -import { AssetTransferAddress, PlatformAddress } from "../../key/classes"; import { P2PKH } from "../../key/P2PKH"; import { P2PKHBurn } from "../../key/P2PKHBurn"; diff --git a/src/core/transaction/AssetTransferOutput.ts b/src/core/transaction/AssetTransferOutput.ts index 6927ba81..92735e59 100644 --- a/src/core/transaction/AssetTransferOutput.ts +++ b/src/core/transaction/AssetTransferOutput.ts @@ -1,6 +1,6 @@ import { Buffer } from "buffer"; +import { AssetTransferAddress } from "codechain-primitives"; -import { AssetTransferAddress } from "../../key/classes"; import { P2PKH } from "../../key/P2PKH"; import { P2PKHBurn } from "../../key/P2PKHBurn"; diff --git a/src/core/transaction/AssetTransferTransaction.ts b/src/core/transaction/AssetTransferTransaction.ts index 66de46cc..9e318575 100644 --- a/src/core/transaction/AssetTransferTransaction.ts +++ b/src/core/transaction/AssetTransferTransaction.ts @@ -1,7 +1,6 @@ +import { AssetTransferAddress } from "codechain-primitives"; import * as _ from "lodash"; -import { AssetTransferAddress } from "../../key/classes"; - import { blake256, blake256WithKey } from "../../utils"; import { Asset } from "../Asset"; import { H256 } from "../H256"; diff --git a/src/core/transaction/CreateWorldTransaction.ts b/src/core/transaction/CreateWorldTransaction.ts index 4b43f425..8fbdf619 100644 --- a/src/core/transaction/CreateWorldTransaction.ts +++ b/src/core/transaction/CreateWorldTransaction.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { blake256 } from "../../utils"; import { H256 } from "../H256"; import { NetworkId } from "../types"; diff --git a/src/core/transaction/SetWorldOwnersTransaction.ts b/src/core/transaction/SetWorldOwnersTransaction.ts index 328397ad..98070324 100644 --- a/src/core/transaction/SetWorldOwnersTransaction.ts +++ b/src/core/transaction/SetWorldOwnersTransaction.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { blake256 } from "../../utils"; import { H256 } from "../H256"; import { NetworkId } from "../types"; diff --git a/src/core/transaction/SetWorldUsersTransaction.ts b/src/core/transaction/SetWorldUsersTransaction.ts index 0788e360..b254c615 100644 --- a/src/core/transaction/SetWorldUsersTransaction.ts +++ b/src/core/transaction/SetWorldUsersTransaction.ts @@ -1,4 +1,5 @@ -import { PlatformAddress } from "../../key/PlatformAddress"; +import { PlatformAddress } from "codechain-primitives"; + import { blake256 } from "../../utils"; import { H256 } from "../H256"; import { NetworkId } from "../types"; diff --git a/src/core/transaction/__test__/CreateWorldTransaction.spec.ts b/src/core/transaction/__test__/CreateWorldTransaction.spec.ts index 32c7c15c..fe240aad 100644 --- a/src/core/transaction/__test__/CreateWorldTransaction.spec.ts +++ b/src/core/transaction/__test__/CreateWorldTransaction.spec.ts @@ -1,6 +1,7 @@ +import { PlatformAddress } from "codechain-primitives"; + import { CreateWorldTransaction } from "../CreateWorldTransaction"; import { getTransactionFromJSON } from "../Transaction"; -import { PlatformAddress } from "../../../key/classes"; describe("CreateWorldTransaction", () => { test("toJSON", () => { diff --git a/src/core/transaction/__test__/SetWorldOwnersTransaction.spec.ts b/src/core/transaction/__test__/SetWorldOwnersTransaction.spec.ts index 5f9d81ed..0a8a6375 100644 --- a/src/core/transaction/__test__/SetWorldOwnersTransaction.spec.ts +++ b/src/core/transaction/__test__/SetWorldOwnersTransaction.spec.ts @@ -1,6 +1,7 @@ +import { PlatformAddress } from "codechain-primitives"; + import { SetWorldOwnersTransaction } from "../SetWorldOwnersTransaction"; import { getTransactionFromJSON } from "../Transaction"; -import { PlatformAddress } from "../../../key/classes"; describe("SetWorldOwnersTransaction", () => { test("toJSON", () => { diff --git a/src/core/transaction/__test__/SetWorldUsersTransaction.spec.ts b/src/core/transaction/__test__/SetWorldUsersTransaction.spec.ts index 31d8d73b..db963db5 100644 --- a/src/core/transaction/__test__/SetWorldUsersTransaction.spec.ts +++ b/src/core/transaction/__test__/SetWorldUsersTransaction.spec.ts @@ -1,6 +1,7 @@ +import { PlatformAddress } from "codechain-primitives"; + import { SetWorldUsersTransaction } from "../SetWorldUsersTransaction"; import { getTransactionFromJSON } from "../Transaction"; -import { PlatformAddress } from "../../../key/classes"; describe("SetWorldUsersTransaction", () => { test("toJSON", () => { diff --git a/src/key/AssetTransferAddress.ts b/src/key/AssetTransferAddress.ts deleted file mode 100644 index 20b4f83a..00000000 --- a/src/key/AssetTransferAddress.ts +++ /dev/null @@ -1 +0,0 @@ -export { AssetTransferAddress } from "codechain-primitives/lib"; diff --git a/src/key/P2PKH.ts b/src/key/P2PKH.ts index bd266a6e..546a668e 100644 --- a/src/key/P2PKH.ts +++ b/src/key/P2PKH.ts @@ -1,4 +1,5 @@ import { Buffer } from "buffer"; +import { AssetTransferAddress } from "codechain-primitives"; import { H256 } from "../core/H256"; import { Script } from "../core/Script"; @@ -8,7 +9,6 @@ import { } from "../core/transaction/AssetTransferTransaction"; import { NetworkId } from "../core/types"; -import { AssetTransferAddress } from "./AssetTransferAddress"; import { KeyStore } from "./KeyStore"; /** diff --git a/src/key/P2PKHBurn.ts b/src/key/P2PKHBurn.ts index 1691d2b1..6dd430c0 100644 --- a/src/key/P2PKHBurn.ts +++ b/src/key/P2PKHBurn.ts @@ -1,4 +1,5 @@ import { Buffer } from "buffer"; +import { AssetTransferAddress } from "codechain-primitives"; import { H256 } from "../core/H256"; import { Script } from "../core/Script"; @@ -8,7 +9,6 @@ import { } from "../core/transaction/AssetTransferTransaction"; import { NetworkId } from "../core/types"; -import { AssetTransferAddress } from "./AssetTransferAddress"; import { KeyStore } from "./KeyStore"; export class P2PKHBurn implements TransactionBurnSigner { diff --git a/src/key/PlatformAddress.ts b/src/key/PlatformAddress.ts deleted file mode 100644 index 669acda5..00000000 --- a/src/key/PlatformAddress.ts +++ /dev/null @@ -1 +0,0 @@ -export { PlatformAddress } from "codechain-primitives"; diff --git a/src/key/__test__/AssetTransferAddress.spec.ts b/src/key/__test__/AssetTransferAddress.spec.ts deleted file mode 100644 index f505ad8e..00000000 --- a/src/key/__test__/AssetTransferAddress.spec.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { H256 } from "../../core/H256"; - -import { AssetTransferAddress } from "../AssetTransferAddress"; - -test("AssetTransferAddress.fromString - mainnet", () => { - const address = AssetTransferAddress.fromString( - "ccaqqq9pgkq69z488qlkvhkpcxcgfd3cqlkzgxyq9cewxuda8qqz7jtlvcev083x" - ); - expect(address.payload).toEqual( - new H256( - "50a2c0d145539c1fb32f60e0d8425b1c03f6120c40171971b8de9c0017a4bfb3" - ) - ); -}); - -test("AssetTransferAddress.fromString - testnet", () => { - const address = AssetTransferAddress.fromString( - "tcaqqq9pgkq69z488qlkvhkpcxcgfd3cqlkzgxyq9cewxuda8qqz7jtlvctt5eze" - ); - expect(address.payload).toEqual( - new H256( - "50a2c0d145539c1fb32f60e0d8425b1c03f6120c40171971b8de9c0017a4bfb3" - ) - ); -}); - -test("AssetTransferAddress.fromString - invalid checksum", () => { - expect(() => { - AssetTransferAddress.fromString( - "ccaqqq9pgkq69z488qlkvhkpcxcgfd3cqlkzgxyq9cewxuda8qqz7jtlvcqqqqqq" - ); - }).toThrow(); -}); diff --git a/src/key/__test__/PlatformAddress.spec.ts b/src/key/__test__/PlatformAddress.spec.ts deleted file mode 100644 index 6670bf41..00000000 --- a/src/key/__test__/PlatformAddress.spec.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { H160 } from "../../core/H160"; - -import { PlatformAddress } from "../PlatformAddress"; - -describe("fromAccountId", () => { - const accountId = new H160("7b5e0ee8644c6f585fc297364143280a45844502"); - - test("mainnet", () => { - const address = PlatformAddress.fromAccountId(accountId, { - networkId: "cc" - }); - expect(address.value).toMatch(/^ccc[a-z0-9]+$/); - }); - - test("testnet", () => { - const address = PlatformAddress.fromAccountId(accountId); - expect(address.value).toMatch(/^tcc[a-z0-9]+$/); - }); - - test("Valid version", () => { - expect(() => { - PlatformAddress.fromAccountId(accountId, { version: 0 }); - }).not.toThrow(); - }); - - test("Invalid version", done => { - try { - PlatformAddress.fromAccountId(accountId, { version: 99 }); - done.fail(); - } catch (e) { - expect(e.toString()).toContain("version"); - done(); - } - }); - - test("Invalid networkId", done => { - try { - PlatformAddress.fromAccountId(accountId, { - version: 0, - networkId: "x" - }); - done.fail(); - } catch (e) { - expect(e.toString()).toContain("networkId"); - expect(e.toString()).toContain("x"); - done(); - } - }); - - test("Invalid accountId", done => { - try { - PlatformAddress.fromAccountId("xxx"); - done.fail(); - } catch (e) { - expect(e.toString()).toContain("accountId"); - expect(e.toString()).toContain("xxx"); - done(); - } - }); -}); - -describe("fromString", () => { - const accountId = "7b5e0ee8644c6f585fc297364143280a45844502"; - const mainnetAddress = "cccqpa4urhgv3xx7kzlc2tnvs2r9q9ytpz9qg4aw2qm"; - const testnetAddress = "tccqpa4urhgv3xx7kzlc2tnvs2r9q9ytpz9qga6ufnz"; - - test("mainnet", () => { - const address = PlatformAddress.fromString(mainnetAddress); - expect(address.accountId).toEqual(new H160(accountId)); - }); - - test("testnet", () => { - const address = PlatformAddress.fromString(testnetAddress); - expect(address.accountId).toEqual(new H160(accountId)); - }); - - test("Invalid checksum", done => { - const invalidChecksumAddress = - "cccqpa4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgqqqqqq"; - try { - PlatformAddress.fromString(invalidChecksumAddress); - done.fail(); - } catch (e) { - expect(e.toString()).toContain("checksum"); - done(); - } - }); -}); diff --git a/src/key/__test__/bech32.spec.ts b/src/key/__test__/bech32.spec.ts deleted file mode 100644 index 1920d56d..00000000 --- a/src/key/__test__/bech32.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { encode, decode, toWords } from "../bech32"; - -test("encode platform account address version 0", () => { - // words: 0, 1, 2, 3, 4, 5, 6, 7 - const encoded = encode("ccc", toWords([0x00, 0x44, 0x32, 0x14, 0xc7])); - expect(encoded).toEqual("cccqpzry9x848mh92"); -}); - -test("decode platform account address version 0", () => { - const { prefix, words } = decode("cccqpzry9x848mh92", "ccc"); - - expect(prefix).toEqual("ccc"); - expect(words).toEqual([0, 1, 2, 3, 4, 5, 6, 7]); -}); diff --git a/src/key/bech32.ts b/src/key/bech32.ts deleted file mode 100644 index 89c16101..00000000 --- a/src/key/bech32.ts +++ /dev/null @@ -1,191 +0,0 @@ -/** - * @hidden - */ -const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; - -/* tslint:disable:prefer-for-of */ - -// pre-compute lookup table -/** - * @hidden - */ -const ALPHABET_MAP: { [ch: string]: number } = {}; -for (let z = 0; z < ALPHABET.length; z++) { - const x = ALPHABET.charAt(z); - - if (ALPHABET_MAP[x] !== undefined) { - throw new TypeError(x + " is ambiguous"); - } - ALPHABET_MAP[x] = z; -} - -// FIXME: any -/** - * @hidden - */ -function polymodStep(pre: any) { - const b = pre >> 25; - return ( - ((pre & 0x1ffffff) << 5) ^ - (-((b >> 0) & 1) & 0x3b6a57b2) ^ - (-((b >> 1) & 1) & 0x26508e6d) ^ - (-((b >> 2) & 1) & 0x1ea119fa) ^ - (-((b >> 3) & 1) & 0x3d4233dd) ^ - (-((b >> 4) & 1) & 0x2a1462b3) - ); -} - -// FIXME: any -/** - * @hidden - */ -function prefixChk(prefix: any) { - let chk = 1; - for (let i = 0; i < prefix.length; ++i) { - const c = prefix.charCodeAt(i); - if (c < 33 || c > 126) { - throw new Error("Invalid prefix (" + prefix + ")"); - } - - chk = polymodStep(chk) ^ (c >> 5); - } - chk = polymodStep(chk); - - for (let i = 0; i < prefix.length; ++i) { - const v = prefix.charCodeAt(i); - chk = polymodStep(chk) ^ (v & 0x1f); - } - return chk; -} - -// FIXME: any -export function encode(prefix: any, words: any, LIMIT?: any) { - LIMIT = LIMIT || 90; - if (prefix.length + 7 + words.length > LIMIT) { - throw new TypeError("Exceeds length limit"); - } - - prefix = prefix.toLowerCase(); - - // determine chk mod - let chk = prefixChk(prefix); - let result = prefix; - for (let i = 0; i < words.length; ++i) { - const x = words[i]; - if (x >> 5 !== 0) { - throw new Error("Non 5-bit word"); - } - - chk = polymodStep(chk) ^ x; - result += ALPHABET.charAt(x); - } - - for (let i = 0; i < 6; ++i) { - chk = polymodStep(chk); - } - chk ^= 1; - - for (let i = 0; i < 6; ++i) { - const v = (chk >> ((5 - i) * 5)) & 0x1f; - result += ALPHABET.charAt(v); - } - - return result; -} - -// FIXME: any -export function decode(str: string, prefix: string, LIMIT?: number) { - LIMIT = LIMIT || 90; - if (str.length < 8) { - throw new TypeError(str + " too short"); - } - if (str.length > LIMIT) { - throw new TypeError("Exceeds length limit"); - } - - // don't allow mixed case - const lowered = str.toLowerCase(); - const uppered = str.toUpperCase(); - if (str !== lowered && str !== uppered) { - throw new Error("Mixed-case string " + str); - } - str = lowered; - - if (!str.startsWith(prefix)) { - throw new Error("Missing prefix for " + str); - } - const split = prefix.length; - - const wordChars = str.slice(split); - if (wordChars.length < 6) { - throw new Error("Data too short"); - } - - let chk = prefixChk(prefix); - const words = []; - for (let i = 0; i < wordChars.length; ++i) { - const c = wordChars.charAt(i); - const v = ALPHABET_MAP[c]; - if (v === undefined) { - throw new Error("Unknown character " + c); - } - chk = polymodStep(chk) ^ v; - - // not in the checksum? - if (i + 6 >= wordChars.length) { - continue; - } - words.push(v); - } - - if (chk !== 1) { - throw new Error("Invalid checksum for " + str); - } - return { prefix, words }; -} - -// FIXME: any -/** - * @hidden - */ -function convert(data: any, inBits: any, outBits: any, pad: any) { - let value = 0; - let bits = 0; - const maxV = (1 << outBits) - 1; - - const result = []; - for (let i = 0; i < data.length; ++i) { - value = (value << inBits) | data[i]; - bits += inBits; - - while (bits >= outBits) { - bits -= outBits; - result.push((value >> bits) & maxV); - } - } - - if (pad) { - if (bits > 0) { - result.push((value << (outBits - bits)) & maxV); - } - } else { - if (bits >= inBits) { - throw new Error("Excess padding"); - } - if ((value << (outBits - bits)) & maxV) { - throw new Error("Non-zero padding"); - } - } - - return result; -} - -// FIXME: any -export function toWords(bytes: any) { - return convert(bytes, 8, 5, true); -} - -// FIXME: any -export function fromWords(words: any) { - return convert(words, 5, 8, false); -} diff --git a/src/key/classes.ts b/src/key/classes.ts index 19eae3d3..4a6ec0d3 100644 --- a/src/key/classes.ts +++ b/src/key/classes.ts @@ -1,5 +1,2 @@ -export { PlatformAddress } from "./PlatformAddress"; -export { AssetTransferAddress } from "./AssetTransferAddress"; - export { RemoteKeyStore } from "./RemoteKeyStore"; export { LocalKeyStore } from "./LocalKeyStore"; diff --git a/src/key/index.ts b/src/key/index.ts index 69322ec9..9d82b67c 100644 --- a/src/key/index.ts +++ b/src/key/index.ts @@ -1,3 +1,5 @@ +import { AssetTransferAddress, PlatformAddress } from "codechain-primitives"; + import { AssetTransferTransaction, Parcel, @@ -6,13 +8,11 @@ import { } from "../core/classes"; import { NetworkId } from "../core/types"; -import { AssetTransferAddress } from "./AssetTransferAddress"; import { KeyStore } from "./KeyStore"; import { LocalKeyStore } from "./LocalKeyStore"; import { MemoryKeyStore } from "./MemoryKeyStore"; import { P2PKH } from "./P2PKH"; import { P2PKHBurn } from "./P2PKHBurn"; -import { PlatformAddress } from "./PlatformAddress"; import { RemoteKeyStore } from "./RemoteKeyStore"; export type KeyStoreType = @@ -23,8 +23,6 @@ export type KeyStoreType = export class Key { public static classes = { - AssetTransferAddress, - PlatformAddress, RemoteKeyStore, LocalKeyStore }; diff --git a/src/rpc/__test__/invalid-response.spec.ts b/src/rpc/__test__/invalid-response.spec.ts index 18dce9ff..99572b4c 100644 --- a/src/rpc/__test__/invalid-response.spec.ts +++ b/src/rpc/__test__/invalid-response.spec.ts @@ -1,5 +1,6 @@ +import { PlatformAddress } from "codechain-primitives"; + import { Parcel, Payment, U256 } from "../../core/classes"; -import { PlatformAddress } from "../../key/classes"; import { AccountRpc } from "../account"; import { ChainRpc } from "../chain"; diff --git a/src/rpc/account.ts b/src/rpc/account.ts index f47792a9..c1f9a2c6 100644 --- a/src/rpc/account.ts +++ b/src/rpc/account.ts @@ -1,5 +1,6 @@ +import { PlatformAddress } from "codechain-primitives"; + import { H256 } from "../core/classes"; -import { PlatformAddress } from "../key/classes"; import { Rpc } from "."; diff --git a/src/rpc/chain.ts b/src/rpc/chain.ts index 50804d2d..a4480ca2 100644 --- a/src/rpc/chain.ts +++ b/src/rpc/chain.ts @@ -1,3 +1,5 @@ +import { PlatformAddress } from "codechain-primitives"; + import { Rpc } from "."; import { Asset } from "../core/Asset"; import { AssetScheme } from "../core/AssetScheme"; @@ -13,7 +15,6 @@ import { } from "../core/transaction/Transaction"; import { NetworkId } from "../core/types"; import { U256 } from "../core/U256"; -import { PlatformAddress } from "../key/classes"; export class ChainRpc { private rpc: Rpc;