From d49ac9baf55b5e304d42a77c5c422ea039aa096b Mon Sep 17 00:00:00 2001 From: neeboo Date: Mon, 1 Apr 2019 21:33:20 +0800 Subject: [PATCH] [WIP] added shards to Account --- examples/testNode.js | 4 +- packages/harmony-account/src/account.ts | 54 ++++++++++++++++++++++++ packages/harmony-account/src/types.ts | 2 + packages/harmony-crypto/src/bytes.ts | 1 + packages/harmony-crypto/src/keccak256.ts | 1 + tsconfig.json | 2 +- 6 files changed, 61 insertions(+), 3 deletions(-) diff --git a/examples/testNode.js b/examples/testNode.js index 3b64abb..12a92de 100644 --- a/examples/testNode.js +++ b/examples/testNode.js @@ -18,6 +18,6 @@ const c = Account.add(importKey); // console.log(isPrivateKey(importKey)); -console.log(c); +c.addShard('newShard'); -console.log(getAddressFromPublicKey(c.publicKey)); +console.log(c.getShardsCount); diff --git a/packages/harmony-account/src/account.ts b/packages/harmony-account/src/account.ts index add52ff..276681c 100644 --- a/packages/harmony-account/src/account.ts +++ b/packages/harmony-account/src/account.ts @@ -6,12 +6,22 @@ import { } from '@harmony/crypto'; import { isPrivateKey } from '@harmony/utils'; +import { Shards, ShardId } from './types'; class Account { + /** + * @function new static method create account + * @return {Account} Account instance + */ static new(): Account { const newAcc = new Account()._new(); return newAcc; } + /** + * @function add static method add a private key to Account + * @param {string} key - private Key + * @return {Account} Account instance + */ static add(key: string): Account { const newAcc = new Account()._import(key); return newAcc; @@ -20,16 +30,53 @@ class Account { privateKey?: string; publicKey?: string; address?: string; + shards: Shards = new Map().set('default', ''); + /** + * @function checksumAddress checsumAddress getter + * @return {string} get the checksumAddress + */ get checksumAddress(): string { return this.address ? toChecksumAddress(this.address) : ''; } + + /** + * @function getShardsCount getShards number with this Account + * @return {number} shard size + */ + get getShardsCount(): number { + return this.shards.size; + } + constructor(key?: string) { if (key === null) { this._new(); } } + /** + * @function addShard add shard to this Account + * @param {ShardId} id - ShardId to the Account + */ + addShard(id: ShardId): void { + if (this.shards && this.shards.has('default')) { + this.shards.set(id, ''); + } + throw new Error('this account has no default shard or shard is not exist'); + } + + /** + * @function getBalance get Account's balance + * @return {type} {description} + */ + getBalance() { + // console.log() + } + + /** + * @function _new private method create Account + * @return {Account} Account instance + */ private _new(): Account { const prv = generatePrivateKey(); if (!isPrivateKey(prv)) { @@ -38,9 +85,15 @@ class Account { this.privateKey = prv; this.publicKey = getPubkeyFromPrivateKey(this.privateKey); this.address = getAddressFromPrivateKey(this.privateKey); + return this; } + /** + * @function _import private method import a private Key + * @param {string} key - private key + * @return {Account} Account instance + */ private _import(key: string): Account { if (!isPrivateKey(key)) { throw new Error(`${key} is not PrivateKey`); @@ -48,6 +101,7 @@ class Account { this.privateKey = key; this.publicKey = getPubkeyFromPrivateKey(this.privateKey); this.address = getAddressFromPrivateKey(this.privateKey); + return this; } } diff --git a/packages/harmony-account/src/types.ts b/packages/harmony-account/src/types.ts index e69de29..5aa014f 100644 --- a/packages/harmony-account/src/types.ts +++ b/packages/harmony-account/src/types.ts @@ -0,0 +1,2 @@ +export type ShardId = string | number; +export type Shards = Map; diff --git a/packages/harmony-crypto/src/bytes.ts b/packages/harmony-crypto/src/bytes.ts index 73e948f..1887099 100644 --- a/packages/harmony-crypto/src/bytes.ts +++ b/packages/harmony-crypto/src/bytes.ts @@ -1,4 +1,5 @@ // This file is ported from ether.js/src.ts/utils/bytes.ts +// and done some fixes import * as errors from './errors'; diff --git a/packages/harmony-crypto/src/keccak256.ts b/packages/harmony-crypto/src/keccak256.ts index 8c34852..271db87 100644 --- a/packages/harmony-crypto/src/keccak256.ts +++ b/packages/harmony-crypto/src/keccak256.ts @@ -1,3 +1,4 @@ +// this file is ported from 'ether.js' and done some fixes import * as sha3 from 'js-sha3'; import { arrayify, Arrayish } from './bytes'; diff --git a/tsconfig.json b/tsconfig.json index 0393cf1..5ebd1ca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "files": [], "references": [ { "path": "packages/harmony-account" }, - { "path": "packages/harmony-crypto" } + { "path": "packages/harmony-crypto" }, { "path": "packages/harmony-utils" } ] }