From 4111122cba81a4fa9a6007f2f40364bcc7cbb1d4 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 24 Sep 2020 08:23:41 +0200 Subject: [PATCH] JSON pair creation without addition (#691) * JSON pair creation without addition * Linting * create test --- CHANGELOG.md | 1 + packages/keyring/src/index.spec.ts | 10 ++++++++++ packages/keyring/src/keyring.ts | 32 +++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 486e02dbb..d7c882e8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Changes: - Use cross-environment TextDecoder & TextEncoder from the polkadot-js polyfills - Add `isBase{32, 58, 64}` to `@polkadot/util-crypto` - Add `base64{Pad, Trim}` to add/remove padding characters +- Add `createFromJSON(json)` to keyring (create pair without adding) ## 3.4.1 Aug 31, 2020 diff --git a/packages/keyring/src/index.spec.ts b/packages/keyring/src/index.spec.ts index eece6dca6..3ba04f317 100644 --- a/packages/keyring/src/index.spec.ts +++ b/packages/keyring/src/index.spec.ts @@ -209,6 +209,16 @@ describe('keypair', (): void => { ).toEqual('5DzMsaYFhmpRdErWrP6K6PD7UXzYoeETToSBUrZSvxasqWRz'); }); + it('allows creation from JSON', (): void => { + keyring.setSS58Format(2); + const pair = keyring.createFromJSON( + JSON.parse('{"address":"0x02fde629668eb2bcc7d748f40a7e597f7c7b363498ff3db31f03ce4854937883ad","encoded":"qIhAhKqtf2iyEoWEr8nmBdksSI8EHHCpgJHToqd6Pl8AgAAAAQAAAAgAAADDZ//fj/BRRj+0+bl1KAlYgoPJp6nEUwiw0fVqO2BW4mjEgQ+iWwJEgDf1JUtecbzOlfhTXBzqX/dIYzLgUADrF4EFEPpboCWiU1iN7W/3DM1cOTRVvTGcbdIqW//z3axhz961qzeJVUIFgllwGe/euLUPIlKbIkiN/CsRYdQ=","encoding":{"content":["pkcs8","ecdsa"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{"genesisHash":"0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe","name":"ecdsa","tags":[],"whenCreated":1600925898271}}') + ); + + expect(pair.address).toEqual('DHL8HKFuTTR55JzzLmkJRCAfPBbuevKaT9cXikxbEV97Ko8'); + expect(pair.publicKey).toEqual(hexToU8a('0x02fde629668eb2bcc7d748f40a7e597f7c7b363498ff3db31f03ce4854937883ad')); + }); + it('signs and verifies', (): void => { const MESSAGE = stringToU8a('this is a message'); const pair = keyring.getPair(addressKeyOne); diff --git a/packages/keyring/src/keyring.ts b/packages/keyring/src/keyring.ts index e12dbf939..2552358f8 100644 --- a/packages/keyring/src/keyring.ts +++ b/packages/keyring/src/keyring.ts @@ -103,15 +103,8 @@ export default class Keyring implements KeyringInstance { * of an account backup), and then generates a keyring pair from it that it passes to * `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value. */ - public addFromJson ({ address, encoded, encoding: { content, type, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair { - const cryptoType = version === '0' || !Array.isArray(content) - ? this.type - : content[1]; - const encType = !Array.isArray(type) - ? [type] - : type; - - return this.addFromAddress(address, meta, isHex(encoded) ? hexToU8a(encoded) : base64Decode(encoded), cryptoType, ignoreChecksum, encType); + public addFromJson (json: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair { + return this.addPair(this.createFromJSON(json, ignoreChecksum)); } /** @@ -148,6 +141,27 @@ export default class Keyring implements KeyringInstance { ); } + /** + * @name createFromJSON + * @description Creates a pair from a JSON keyfile + */ + public createFromJSON ({ address, encoded, encoding: { content, type, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair { + const cryptoType = version === '0' || !Array.isArray(content) + ? this.type + : content[1]; + const encType = !Array.isArray(type) + ? [type] + : type; + const publicKey = isHex(address) + ? hexToU8a(address) + : this.decodeAddress(address, ignoreChecksum); + const decoded = isHex(encoded) + ? hexToU8a(encoded) + : base64Decode(encoded); + + return createPair({ toSS58: this.encodeAddress, type: cryptoType }, { publicKey, secretKey: new Uint8Array() }, meta, decoded, encType); + } + /** * @name createFromUri * @summary Creates a Keypair from an suri