[WIP] added shards to Account

@types
neeboo 6 years ago
parent 72a1442ec5
commit d49ac9baf5
  1. 4
      examples/testNode.js
  2. 54
      packages/harmony-account/src/account.ts
  3. 2
      packages/harmony-account/src/types.ts
  4. 1
      packages/harmony-crypto/src/bytes.ts
  5. 1
      packages/harmony-crypto/src/keccak256.ts
  6. 2
      tsconfig.json

@ -18,6 +18,6 @@ const c = Account.add(importKey);
// console.log(isPrivateKey(importKey)); // console.log(isPrivateKey(importKey));
console.log(c); c.addShard('newShard');
console.log(getAddressFromPublicKey(c.publicKey)); console.log(c.getShardsCount);

@ -6,12 +6,22 @@ import {
} from '@harmony/crypto'; } from '@harmony/crypto';
import { isPrivateKey } from '@harmony/utils'; import { isPrivateKey } from '@harmony/utils';
import { Shards, ShardId } from './types';
class Account { class Account {
/**
* @function new static method create account
* @return {Account} Account instance
*/
static new(): Account { static new(): Account {
const newAcc = new Account()._new(); const newAcc = new Account()._new();
return newAcc; 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 { static add(key: string): Account {
const newAcc = new Account()._import(key); const newAcc = new Account()._import(key);
return newAcc; return newAcc;
@ -20,16 +30,53 @@ class Account {
privateKey?: string; privateKey?: string;
publicKey?: string; publicKey?: string;
address?: string; address?: string;
shards: Shards = new Map().set('default', '');
/**
* @function checksumAddress checsumAddress getter
* @return {string} get the checksumAddress
*/
get checksumAddress(): string { get checksumAddress(): string {
return this.address ? toChecksumAddress(this.address) : ''; 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) { constructor(key?: string) {
if (key === null) { if (key === null) {
this._new(); 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 { private _new(): Account {
const prv = generatePrivateKey(); const prv = generatePrivateKey();
if (!isPrivateKey(prv)) { if (!isPrivateKey(prv)) {
@ -38,9 +85,15 @@ class Account {
this.privateKey = prv; this.privateKey = prv;
this.publicKey = getPubkeyFromPrivateKey(this.privateKey); this.publicKey = getPubkeyFromPrivateKey(this.privateKey);
this.address = getAddressFromPrivateKey(this.privateKey); this.address = getAddressFromPrivateKey(this.privateKey);
return this; return this;
} }
/**
* @function _import private method import a private Key
* @param {string} key - private key
* @return {Account} Account instance
*/
private _import(key: string): Account { private _import(key: string): Account {
if (!isPrivateKey(key)) { if (!isPrivateKey(key)) {
throw new Error(`${key} is not PrivateKey`); throw new Error(`${key} is not PrivateKey`);
@ -48,6 +101,7 @@ class Account {
this.privateKey = key; this.privateKey = key;
this.publicKey = getPubkeyFromPrivateKey(this.privateKey); this.publicKey = getPubkeyFromPrivateKey(this.privateKey);
this.address = getAddressFromPrivateKey(this.privateKey); this.address = getAddressFromPrivateKey(this.privateKey);
return this; return this;
} }
} }

@ -0,0 +1,2 @@
export type ShardId = string | number;
export type Shards = Map<ShardId, string>;

@ -1,4 +1,5 @@
// This file is ported from ether.js/src.ts/utils/bytes.ts // This file is ported from ether.js/src.ts/utils/bytes.ts
// and done some fixes
import * as errors from './errors'; import * as errors from './errors';

@ -1,3 +1,4 @@
// this file is ported from 'ether.js' and done some fixes
import * as sha3 from 'js-sha3'; import * as sha3 from 'js-sha3';
import { arrayify, Arrayish } from './bytes'; import { arrayify, Arrayish } from './bytes';

@ -2,7 +2,7 @@
"files": [], "files": [],
"references": [ "references": [
{ "path": "packages/harmony-account" }, { "path": "packages/harmony-account" },
{ "path": "packages/harmony-crypto" } { "path": "packages/harmony-crypto" },
{ "path": "packages/harmony-utils" } { "path": "packages/harmony-utils" }
] ]
} }

Loading…
Cancel
Save