Simplify @polkadot/keyring KeyringPair (#419)
* Simplify @polkadot/keyring KeyringPair * Fix keypair.address not taking prefix changes into account * Merge master, update CHANGELOG * Use getter for keypair.publicKey * Fix import * Fix isLocked; add test
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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])
|
||||
);
|
||||
|
||||
@@ -211,7 +211,7 @@ export default class Keyring implements KeyringInstance {
|
||||
return this._pairs
|
||||
.all()
|
||||
.map(({ publicKey }) =>
|
||||
publicKey()
|
||||
publicKey
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 };
|
||||
},
|
||||
|
||||
@@ -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 =>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,7 +17,7 @@ describe('decode', () => {
|
||||
expect(
|
||||
decode('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
|
||||
).toEqual(
|
||||
keyring.alice.publicKey()
|
||||
keyring.alice.publicKey
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user