diff --git a/CHANGELOG.md b/CHANGELOG.md index 6273a151f..ee7ac8dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 0.92.1 +- Breaking: External pair interface for keyring has changes. Instead of + - `getMeta` use the `meta` getter, i.e. `console.log(pair.meta.name)` + - `address` use the `address` getter, i.e. `console.log(pair.address)` + - `publicKey` use the `publicKey` getter, i.e. `console.log(pair.publicKey)` - Only allow integer input values into formatBalance - Allow formatBalance, formatDecimal & formatNumber to cater for negative numbers - Add 'Bob//stash' to testing keyring diff --git a/docs/examples/keyring/01_create_account/index.js b/docs/examples/keyring/01_create_account/index.js index 79cdd2807..5de9967ca 100755 --- a/docs/examples/keyring/01_create_account/index.js +++ b/docs/examples/keyring/01_create_account/index.js @@ -13,7 +13,7 @@ async function main () { const pairAlice = keyring.addFromSeed(stringToU8a(ALICE_SEED)); console.log(`Created keyring pair for Alice with address: ` + - `${keyring.getPair(pairAlice.address()).address()}`); + `${keyring.getPair(pairAlice.address).address}`); } main().catch(console.error).finally(_ => process.exit()); diff --git a/docs/examples/keyring/02_load_accounts/index.js b/docs/examples/keyring/02_load_accounts/index.js index 664321640..5457b2b53 100755 --- a/docs/examples/keyring/02_load_accounts/index.js +++ b/docs/examples/keyring/02_load_accounts/index.js @@ -21,7 +21,7 @@ async function main () { keyring .getPairs() .forEach((pair, index) => { - const address = pair.address(); + const { address } = pair; console.log(`\nAccount with index #${index} has json: `, JSON.stringify(keyring.toJson(address), null, 2)); diff --git a/packages/keyring/src/index.spec.ts b/packages/keyring/src/index.spec.ts index 24db97347..a787a036b 100644 --- a/packages/keyring/src/index.spec.ts +++ b/packages/keyring/src/index.spec.ts @@ -27,7 +27,7 @@ describe('keypair', () => { it('adds the pair', () => { expect( - keypair.addFromSeed(seedTwo, {}).publicKey() + keypair.addFromSeed(seedTwo, {}).publicKey ).toEqual(publicKeyTwo); }); @@ -35,7 +35,7 @@ describe('keypair', () => { expect( keypair.addFromUri( 'seed sock milk update focus rotate barely fade car face mechanic mercy' - ).address() + ).address ).toEqual('5DkQP32jP4DVJLWWBRBoZF2tpWjqFrcrTBo6H5NcSk7MxKCC'); }); @@ -43,7 +43,7 @@ describe('keypair', () => { setAddressPrefix(68); expect( - keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address() + keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address ).toEqual('7sPsxWPE5DzAyPT3VuoJYw5NTGscx9QYN9oddQx4kALKC3hH'); }); @@ -57,7 +57,7 @@ describe('keypair', () => { it('allows retrieval of a specific item', () => { expect( - keypair.getPair(publicKeyOne).publicKey() + keypair.getPair(publicKeyOne).publicKey ).toEqual(publicKeyOne); }); @@ -65,7 +65,7 @@ describe('keypair', () => { expect( keypair.addFromJson( JSON.parse('{"address":"5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua","encoded":"0xb4a14995d25ab609f3686e9fa45f1fb237cd833f33f00d4b12c51858ca070d96972e47d73aae5eeb0fc06f923826cf0943fdb02c2c2ee30ef52a7912663053940d1da4da66b3a3f520ae07422c1c94b2d95690fca9d1f4a997623bb2923a8833280e19e7f72c3c5cfa343974e60e2b3dc53b404fdaf330756daad5e4e3","encoding":{"content":"pkcs8","type":"xsalsa20-poly1305","version":"0"},"meta":{"isTesting":true,"name":"alice"}}') - ).publicKey() + ).publicKey ).toEqual( new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79]) ); @@ -95,13 +95,13 @@ describe('keypair', () => { it('creates with dev phrase when only path specified', () => { expect( - keypair.createFromUri('//Alice').address() + keypair.createFromUri('//Alice').address ).toEqual('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'); }); it('adds the pair', () => { expect( - keypair.addFromSeed(seedTwo, {}).publicKey() + keypair.addFromSeed(seedTwo, {}).publicKey ).toEqual(publicKeyTwo); }); @@ -109,7 +109,7 @@ describe('keypair', () => { setAddressPrefix(68); expect( - keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address() + keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address ).toEqual('7qQGarA4PWjPPVHG4USn1yuuVZvEHN7XZz8o7EbAp48jayZQ'); }); @@ -123,7 +123,7 @@ describe('keypair', () => { it('allows retrieval of a specific item', () => { expect( - keypair.getPair(publicKeyOne).publicKey() + keypair.getPair(publicKeyOne).publicKey ).toEqual(publicKeyOne); }); @@ -131,7 +131,7 @@ describe('keypair', () => { expect( keypair.addFromJson( JSON.parse('{"address":"5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua","encoded":"0xb4a14995d25ab609f3686e9fa45f1fb237cd833f33f00d4b12c51858ca070d96972e47d73aae5eeb0fc06f923826cf0943fdb02c2c2ee30ef52a7912663053940d1da4da66b3a3f520ae07422c1c94b2d95690fca9d1f4a997623bb2923a8833280e19e7f72c3c5cfa343974e60e2b3dc53b404fdaf330756daad5e4e3","encoding":{"content":"pkcs8","type":"xsalsa20-poly1305","version":"0"},"meta":{"isTesting":true,"name":"alice"}}') - ).publicKey() + ).publicKey ).toEqual( new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79]) ); diff --git a/packages/keyring/src/keyring.ts b/packages/keyring/src/keyring.ts index bc78cbf3e..617f1a172 100644 --- a/packages/keyring/src/keyring.ts +++ b/packages/keyring/src/keyring.ts @@ -211,7 +211,7 @@ export default class Keyring implements KeyringInstance { return this._pairs .all() .map(({ publicKey }) => - publicKey() + publicKey ); } diff --git a/packages/keyring/src/pair/index.spec.ts b/packages/keyring/src/pair/index.spec.ts index cdf2f0d6c..1b00ad5b8 100644 --- a/packages/keyring/src/pair/index.spec.ts +++ b/packages/keyring/src/pair/index.spec.ts @@ -2,7 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { setAddressPrefix } from '@polkadot/util-crypto'; + import testingPairs from '../testingPairs'; +import createPair from '.'; const keyring = testingPairs({ type: 'ed25519' }, false); @@ -11,7 +14,7 @@ describe('pair', () => { it('has a publicKey', () => { expect( - keyring.alice.publicKey() + keyring.alice.publicKey ).toEqual( new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79]) ); @@ -46,10 +49,32 @@ describe('pair', () => { it('allows setting/getting of meta', () => { keyring.bob.setMeta({ foo: 'bar', something: 'else' }); - expect(keyring.bob.getMeta()).toMatchObject({ foo: 'bar', something: 'else' }); + expect(keyring.bob.meta).toMatchObject({ foo: 'bar', something: 'else' }); keyring.bob.setMeta({ something: 'thing' }); - expect(keyring.bob.getMeta()).toMatchObject({ foo: 'bar', something: 'thing' }); + expect(keyring.bob.meta).toMatchObject({ foo: 'bar', something: 'thing' }); + }); + + it('allows encoding of address with different prefixes', () => { + expect(keyring.alice.address).toEqual( + '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua' + ); + + setAddressPrefix(68); + + expect(keyring.alice.address).toEqual( + '7sGUeMak588SPY2YMmmuKUuLz7u2WQpf74F9dCFtSLB2td9d' + ); + }); + + it('allows getting public key after decoding', () => { + const PASS = 'testing'; + const encoded = keyring.alice.encodePkcs8(PASS); + + const pair = createPair('sr25519', { publicKey: keyring.alice.publicKey }); + pair.decodePkcs8(PASS, encoded); + + expect(pair.isLocked).toEqual(false); }); }); diff --git a/packages/keyring/src/pair/index.ts b/packages/keyring/src/pair/index.ts index ada44618b..b26034ab5 100644 --- a/packages/keyring/src/pair/index.ts +++ b/packages/keyring/src/pair/index.ts @@ -46,7 +46,7 @@ const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, p * - `encodePkcs8` function when provided with the correct passphrase associated with the account pair * and when the secret key is in memory (when the account pair is not locked) it returns an encoded * public key of the account. - * - `getMeta` returns the metadata that is stored in the state of the pair, either when it was originally + * - `meta` is the metadata that is stored in the state of the pair, either when it was originally * created or set via `setMeta`. * - `publicKey` returns the public key stored in memory for the pair. * - `sign` may be used to return a signature by signing a provided message with the secret @@ -64,8 +64,9 @@ const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, p export default function createPair (type: KeypairType, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null): KeyringPair { return { type, - address: (): string => - encodeAddress(publicKey), + get address () { + return encodeAddress(publicKey); + }, decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => { const decoded = decode(passphrase, _encoded || encoded); @@ -81,15 +82,18 @@ export default function createPair (type: KeypairType, { publicKey, secretKey }: }, encodePkcs8: (passphrase?: string): Uint8Array => encode({ publicKey, secretKey }, passphrase), - getMeta: (): KeyringPair$Meta => - meta, - isLocked: (): boolean => - (!secretKey || secretKey.length === 0), + get meta () { + return meta; + }, + get isLocked () { + return !secretKey || secretKey.length === 0; + }, lock: (): void => { secretKey = new Uint8Array(0); }, - publicKey: (): Uint8Array => - publicKey, + get publicKey () { + return publicKey; + }, setMeta: (additional: KeyringPair$Meta): void => { meta = { ...meta, ...additional }; }, diff --git a/packages/keyring/src/pair/nobody.ts b/packages/keyring/src/pair/nobody.ts index e51acb44f..0d26ccced 100644 --- a/packages/keyring/src/pair/nobody.ts +++ b/packages/keyring/src/pair/nobody.ts @@ -26,21 +26,17 @@ const json: KeyringPair$Json = { export default function everybody (): KeyringPair { return { type: 'ed25519', - address: (): string => - address, + address, decodePkcs8: (passphrase?: string, encoded?: Uint8Array): void => undefined, encodePkcs8: (passphrase?: string): Uint8Array => new Uint8Array(0), - getMeta: (): KeyringPair$Meta => - meta, - isLocked: (): boolean => - true, + meta, + isLocked: true, lock: (): void => { // no locking, it is always locked }, - publicKey: (): Uint8Array => - publicKey, + publicKey, setMeta: (meta: KeyringPair$Meta): void => undefined, sign: (message: Uint8Array): Uint8Array => diff --git a/packages/keyring/src/pairs.ts b/packages/keyring/src/pairs.ts index 75cbbe215..4d92f25e6 100644 --- a/packages/keyring/src/pairs.ts +++ b/packages/keyring/src/pairs.ts @@ -21,7 +21,7 @@ export default class Pairs implements KeyringPairs { add (pair: KeyringPair): KeyringPair { // @ts-ignore we use coercion :( - this._map[pair.publicKey()] = pair; + this._map[pair.publicKey] = pair; return pair; } diff --git a/packages/keyring/src/suri.spec.ts b/packages/keyring/src/suri.spec.ts index e2dd9bef6..beba15394 100644 --- a/packages/keyring/src/suri.spec.ts +++ b/packages/keyring/src/suri.spec.ts @@ -70,8 +70,8 @@ describe('keyring.addFromUri', () => { it(`creates ${uri}`, () => { const pair = keyring.addFromUri(uri); - expect(u8aToHex(pair.publicKey())).toEqual(pk); - expect(pair.address()).toEqual(ss); + expect(u8aToHex(pair.publicKey)).toEqual(pk); + expect(pair.address).toEqual(ss); }); }); }); diff --git a/packages/keyring/src/testingPairs.spec.ts b/packages/keyring/src/testingPairs.spec.ts index 803c5b033..5d423d763 100644 --- a/packages/keyring/src/testingPairs.spec.ts +++ b/packages/keyring/src/testingPairs.spec.ts @@ -19,13 +19,13 @@ describe('testingPairs', () => { it('has the correct address for Alice (non-HDKD)', () => { expect( - testingPairs({ type: 'ed25519' }, false).alice.address() + testingPairs({ type: 'ed25519' }, false).alice.address ).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua'); }); it('has the correct address for Alice (HDKD)', () => { expect( - testingPairs({ type: 'ed25519' }, true).alice.address() + testingPairs({ type: 'ed25519' }, true).alice.address ).toEqual('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'); }); }); diff --git a/packages/keyring/src/testingPairs.ts b/packages/keyring/src/testingPairs.ts index a5c20b097..4193613cf 100644 --- a/packages/keyring/src/testingPairs.ts +++ b/packages/keyring/src/testingPairs.ts @@ -16,7 +16,7 @@ export default function testKeyringPairs (options?: KeyringOptions, isDerived: b const pairs = keyring.getPairs(); return pairs.reduce((result, pair) => { - const { name } = pair.getMeta(); + const { name } = pair.meta; result[name as string] = pair; diff --git a/packages/keyring/src/types.ts b/packages/keyring/src/types.ts index 47fcf6f92..171eb6c9d 100644 --- a/packages/keyring/src/types.ts +++ b/packages/keyring/src/types.ts @@ -32,13 +32,13 @@ export type KeyringPair$Json = { export interface KeyringPair { readonly type: KeypairType; - address: () => string; + address: string; decodePkcs8: (passphrase?: string, encoded?: Uint8Array) => void; encodePkcs8: (passphrase?: string) => Uint8Array; - getMeta: () => KeyringPair$Meta; - isLocked: () => boolean; + meta: KeyringPair$Meta; + isLocked: boolean; lock: () => void; - publicKey: () => Uint8Array; + publicKey: Uint8Array; setMeta: (meta: KeyringPair$Meta) => void; sign (message: Uint8Array): Uint8Array; toJson (passphrase?: string): KeyringPair$Json; diff --git a/packages/util-crypto/src/address/decode.spec.ts b/packages/util-crypto/src/address/decode.spec.ts index c077968b6..f34ec1d91 100644 --- a/packages/util-crypto/src/address/decode.spec.ts +++ b/packages/util-crypto/src/address/decode.spec.ts @@ -17,7 +17,7 @@ describe('decode', () => { expect( decode('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') ).toEqual( - keyring.alice.publicKey() + keyring.alice.publicKey ); }); diff --git a/packages/util-crypto/src/address/encode.spec.ts b/packages/util-crypto/src/address/encode.spec.ts index cc8edf718..c971220b1 100644 --- a/packages/util-crypto/src/address/encode.spec.ts +++ b/packages/util-crypto/src/address/encode.spec.ts @@ -11,14 +11,14 @@ const keyring = testingPairs({ type: 'ed25519' }, false); describe('encode', () => { it('encodes an address to a valid value', () => { expect( - keyring.alice.address() + keyring.alice.address ).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua'); }); it('fails when non-valid publicKey provided', () => { expect( () => encode( - keyring.alice.publicKey().slice(0, 30) + keyring.alice.publicKey.slice(0, 30) ) ).toThrow(/Expected a valid key/); });