diff --git a/lerna.json b/lerna.json index 7a4c08738..472f4bd56 100644 --- a/lerna.json +++ b/lerna.json @@ -9,5 +9,5 @@ "packages": [ "packages/*" ], - "version": "0.29.33" + "version": "0.30.0" } diff --git a/package.json b/package.json index b5b3b77c6..d7101cb9f 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ }, "devDependencies": { "@polkadot/dev": "^0.20.19", - "@polkadot/ts": "^0.1.25" + "@polkadot/ts": "^0.1.27" } } diff --git a/packages/db/package.json b/packages/db/package.json index 22650340b..30d22a7d8 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/db", - "version": "0.29.33", + "version": "0.30.0", "description": "Implementation of a basic sync in-memory and on-disk databases with overlays", "main": "index.js", "keywords": [ @@ -29,8 +29,8 @@ "homepage": "https://github.com/polkadot-js/common/tree/master/packages/db#readme", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/trie-hash": "^0.29.33", - "@polkadot/util": "^0.29.33", + "@polkadot/trie-hash": "^0.30.0", + "@polkadot/util": "^0.30.0", "@types/mkdirp": "^0.5.2", "@types/snappy": "^6.0.0", "lru_map": "^0.3.3", diff --git a/packages/trie-db/package.json b/packages/trie-db/package.json index de7b132e0..34ea01d83 100644 --- a/packages/trie-db/package.json +++ b/packages/trie-db/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/trie-db", - "version": "0.29.33", + "version": "0.30.0", "description": "This is an implementation of the modified merkle patricia tree as speficed in Ethereum's yellow paper, adpated for Polkadot", "main": "index.js", "keywords": [ @@ -29,13 +29,13 @@ "homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-db#readme", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/trie-hash": "^0.29.33", - "@polkadot/util": "^0.29.33", - "@polkadot/util-crypto": "^0.29.33", - "@polkadot/util-rlp": "^0.29.33" + "@polkadot/trie-hash": "^0.30.0", + "@polkadot/util": "^0.30.0", + "@polkadot/util-crypto": "^0.30.0", + "@polkadot/util-rlp": "^0.30.0" }, "devDependencies": { - "@polkadot/db": "^0.29.33", + "@polkadot/db": "^0.30.0", "ethereumjs-testing": "1.0.4" } } diff --git a/packages/trie-hash/package.json b/packages/trie-hash/package.json index 3249f6599..85ae6066c 100644 --- a/packages/trie-hash/package.json +++ b/packages/trie-hash/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/trie-hash", - "version": "0.29.33", + "version": "0.30.0", "description": "Creation of triehash values", "main": "index.js", "keywords": [ @@ -29,8 +29,8 @@ "homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/util": "^0.29.33", - "@polkadot/util-crypto": "^0.29.33", - "@polkadot/util-rlp": "^0.29.33" + "@polkadot/util": "^0.30.0", + "@polkadot/util-crypto": "^0.30.0", + "@polkadot/util-rlp": "^0.30.0" } } diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index a81eaf9f6..d439c31d1 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-crypto", - "version": "0.29.33", + "version": "0.30.0", "description": "A collection of useful crypto utilities for @polkadot", "main": "index.js", "keywords": [ @@ -29,7 +29,7 @@ "homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/util": "^0.29.33", + "@polkadot/util": "^0.30.0", "blakejs": "^1.1.0", "js-sha3": "^0.8.0", "tweetnacl": "^1.0.0", diff --git a/packages/util-keyring/package.json b/packages/util-keyring/package.json index cef576e7e..22759b729 100644 --- a/packages/util-keyring/package.json +++ b/packages/util-keyring/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-keyring", - "version": "0.29.33", + "version": "0.30.0", "description": "Keyring management", "main": "index.js", "engines": { @@ -31,8 +31,8 @@ "homepage": "https://github.com/polkadot-js/client/tree/master/packages/util-keyring#readme", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/util": "^0.29.33", - "@polkadot/util-crypto": "^0.29.33", + "@polkadot/util": "^0.30.0", + "@polkadot/util-crypto": "^0.30.0", "@types/bs58": "^3.0.30", "bs58": "^4.0.1" } diff --git a/packages/util-keyring/src/index.spec.js b/packages/util-keyring/src/index.spec.js index 90a984ce2..2fcd5cfc2 100644 --- a/packages/util-keyring/src/index.spec.js +++ b/packages/util-keyring/src/index.spec.js @@ -5,7 +5,7 @@ import hexToU8a from '@polkadot/util/hex/toU8a'; import u8aFromString from '@polkadot/util/u8a/fromString'; -import index from './index'; +import Keyring from './index'; describe('keypair', () => { const publicKeyOne = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]); @@ -15,7 +15,7 @@ describe('keypair', () => { let keypair; beforeEach(() => { - keypair = index(); + keypair = new Keyring(); keypair.addFromSeed(seedOne); }); diff --git a/packages/util-keyring/src/index.ts b/packages/util-keyring/src/index.ts index 0a9bfca6d..144a3f737 100644 --- a/packages/util-keyring/src/index.ts +++ b/packages/util-keyring/src/index.ts @@ -9,28 +9,49 @@ import hexToU8a from '@polkadot/util/hex/toU8a'; import addressDecode from './address/decode'; import createPair from './pair'; -import createPairs from './pairs'; +import Pairs from './pairs'; -export default function keyring (): KeyringInstance { - const pairs = createPairs(); - const addFromAddress = (address: string | Uint8Array, meta?: KeyringPair$Meta, defaultEncoded?: Uint8Array) => +export default class Keyring implements KeyringInstance { + private _pairs: Pairs; + + constructor () { + this._pairs = new Pairs(); + } + + addFromAddress (address: string | Uint8Array, meta?: KeyringPair$Meta, defaultEncoded?: Uint8Array): KeyringPair { // @ts-ignore no secretKey - cannot unlock - pairs.add(createPair({ publicKey: addressDecode(address) }, meta, defaultEncoded)); + return this._pairs.add(createPair({ publicKey: addressDecode(address) }, meta, defaultEncoded)); + } - return { - addFromAddress, - addFromJson: ({ address, encoded, meta }: KeyringPair$Json): KeyringPair => - addFromAddress(address, meta, hexToU8a(encoded)), - addFromSeed: (seed: Uint8Array, meta?: KeyringPair$Meta): KeyringPair => - pairs.add(createPair(naclKeypairFromSeed(seed), meta)), - getPair: (address: string | Uint8Array): KeyringPair => - pairs.get(address), - getPairs: pairs.all, - getPublicKeys: (): Array => - pairs.all().map(({ publicKey }) => publicKey()), - removePair: (address: string | Uint8Array): void => - pairs.remove(address), - toJson: (address: string | Uint8Array, passphrase?: string): KeyringPair$Json => - pairs.get(address).toJson(passphrase) - }; + addFromJson ({ address, encoded, meta }: KeyringPair$Json): KeyringPair { + return this.addFromAddress(address, meta, hexToU8a(encoded)); + } + + addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta): KeyringPair { + return this._pairs.add(createPair(naclKeypairFromSeed(seed), meta)); + } + + getPair (address: string | Uint8Array): KeyringPair { + return this._pairs.get(address); + } + + getPairs (): Array { + return this._pairs.all(); + } + + getPublicKeys (): Array { + return this._pairs + .all() + .map(({ publicKey }) => + publicKey() + ); + } + + removePair (address: string | Uint8Array): void { + this._pairs.remove(address); + } + + toJson (address: string | Uint8Array, passphrase?: string): KeyringPair$Json { + return this._pairs.get(address).toJson(passphrase); + } } diff --git a/packages/util-keyring/src/pairs.ts b/packages/util-keyring/src/pairs.ts index c795667db..12413c564 100644 --- a/packages/util-keyring/src/pairs.ts +++ b/packages/util-keyring/src/pairs.ts @@ -17,35 +17,41 @@ type KeyringPairMap = { [index: Uint8Array]: KeyringPair }; -export default function pairs (): KeyringPairs { - const self: KeyringPairMap = {}; +export default class Pairs implements KeyringPairs { + private _map: KeyringPairMap; - return { - add: (pair: KeyringPair): KeyringPair => { - // @ts-ignore we use coercion :( - self[pair.publicKey()] = pair; + constructor () { + this._map = {}; + } - return pair; - }, - all: (): Array => - Object.values(self), - get: (address: string | Uint8Array): KeyringPair => { - // @ts-ignore we use coercion :( - const pair = self[addressDecode(address)]; + add (pair: KeyringPair): KeyringPair { + // @ts-ignore we use coercion :( + this._map[pair.publicKey()] = pair; - assert(pair, () => { - const formatted: string = isU8a(address) || isHex(address) - ? u8aToHex(u8aToU8a(address)) - : address; + return pair; + } - return `Unable to retrieve keypair '${formatted}'`; - }); + all (): Array { + return Object.values(this._map); + } - return pair; - }, - remove: (address: string | Uint8Array): void => { - // @ts-ignore we use coercion :( - delete self[addressDecode(address)]; - } - }; + get (address: string | Uint8Array): KeyringPair { + // @ts-ignore we use coercion :( + const pair = this._map[addressDecode(address)]; + + assert(pair, () => { + const formatted: string = isU8a(address) || isHex(address) + ? u8aToHex(u8aToU8a(address)) + : address; + + return `Unable to retrieve keypair '${formatted}'`; + }); + + return pair; + } + + remove (address: string | Uint8Array): void { + // @ts-ignore we use coercion :( + delete this._map[addressDecode(address)]; + } } diff --git a/packages/util-keyring/src/testing.ts b/packages/util-keyring/src/testing.ts index ef51d00c6..0f05dff5d 100644 --- a/packages/util-keyring/src/testing.ts +++ b/packages/util-keyring/src/testing.ts @@ -7,7 +7,7 @@ import { KeyringInstance } from './types'; import hexToU8a from '@polkadot/util/hex/toU8a'; import u8aFromString from '@polkadot/util/u8a/fromString'; -import createKeyring from './index'; +import Keyring from './index'; function padSeed (seed: string): Uint8Array { return u8aFromString(seed.padEnd(32, ' ')); @@ -33,7 +33,7 @@ const SEEDS: { [index: string ]: Uint8Array } = { }; export default function testKeyring (): KeyringInstance { - const keyring = createKeyring(); + const keyring = new Keyring(); Object .keys(SEEDS) diff --git a/packages/util-keyring/src/types.d.ts b/packages/util-keyring/src/types.d.ts index 20e2004b6..dfbfd028d 100644 --- a/packages/util-keyring/src/types.d.ts +++ b/packages/util-keyring/src/types.d.ts @@ -31,14 +31,14 @@ export type KeyringPair = { verify (message: Uint8Array, signature: Uint8Array): boolean }; -export type KeyringPairs = { +export interface KeyringPairs { add: (pair: KeyringPair) => KeyringPair, all: () => Array, get: (address: string | Uint8Array) => KeyringPair, remove: (address: string | Uint8Array) => void -}; +} -export type KeyringInstance = { +export interface KeyringInstance { addFromAddress (address: string | Uint8Array, meta?: KeyringPair$Meta): KeyringPair, addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta): KeyringPair, addFromJson (pair: KeyringPair$Json): KeyringPair, @@ -47,4 +47,4 @@ export type KeyringInstance = { getPublicKeys (): Array, removePair (address: string | Uint8Array): void, toJson (address: string | Uint8Array, passphrase?: string): KeyringPair$Json -}; +} diff --git a/packages/util-rlp/package.json b/packages/util-rlp/package.json index 4554ab485..ac0bc0224 100644 --- a/packages/util-rlp/package.json +++ b/packages/util-rlp/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-rlp", - "version": "0.29.33", + "version": "0.30.0", "description": "RLP encoding and decoding", "main": "index.js", "keywords": [ @@ -31,7 +31,7 @@ "homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/util": "^0.29.33" + "@polkadot/util": "^0.30.0" }, "devDependencies": { "ethereumjs-testing": "1.0.4" diff --git a/packages/util/package.json b/packages/util/package.json index 3d012ece7..9fe277046 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.29.33", + "version": "0.30.0", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ diff --git a/yarn.lock b/yarn.lock index ac33adbed..d9180010f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1210,9 +1210,9 @@ typedoc-plugin-markdown "^1.1.15" typescript "^3.0.1" -"@polkadot/ts@^0.1.25": - version "0.1.25" - resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.25.tgz#a88c5242ae02975acce7128b5a84a6df8ca9d7b7" +"@polkadot/ts@^0.1.27": + version "0.1.27" + resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.27.tgz#88ddd9e90f4c5c98996ecd433670839eaf530d73" "@types/base-x@*": version "1.0.29"