diff --git a/packages/keyring/src/pair/index.spec.ts b/packages/keyring/src/pair/index.spec.ts index de657cc05..23962b49d 100644 --- a/packages/keyring/src/pair/index.spec.ts +++ b/packages/keyring/src/pair/index.spec.ts @@ -93,4 +93,12 @@ describe('pair', (): void => { expect(stash.publicKey).toEqual(PAIRS[1].publicKey); expect(soft.address).toEqual('5ECQNn7UueWHPFda5qUi4fTmTtyCnPvGnuoyVVSj5CboJh9J'); }); + + it('fails to sign when locked', (): void => { + const pair = createPair({ toSS58, type: 'sr25519' }, { publicKey: keyring.alice.publicKey }); + expect(pair.isLocked).toEqual(true); + expect((): Uint8Array => + pair.sign(new Uint8Array([0])) + ).toThrow('Cannot sign with a locked key pair'); + }); }); diff --git a/packages/keyring/src/pair/index.ts b/packages/keyring/src/pair/index.ts index 8a736649b..a0b660627 100644 --- a/packages/keyring/src/pair/index.ts +++ b/packages/keyring/src/pair/index.ts @@ -6,7 +6,7 @@ import { Keypair, KeypairType } from '@polkadot/util-crypto/types'; import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types'; import { PairInfo } from './types'; -import { u8aConcat, assert } from '@polkadot/util'; +import { assert, u8aConcat } from '@polkadot/util'; import { keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify } from '@polkadot/util-crypto'; import decode from './decode'; @@ -62,6 +62,10 @@ function verify (type: KeypairType, message: Uint8Array, signature: Uint8Array, : naclVerify(message, signature, publicKey); } +function isLocked (secretKey?: Uint8Array): secretKey is Uint8Array { + return !secretKey || secretKey.length === 0 || isEmpty(secretKey); +} + /** * @name pair * @summary Creates a keyring pair object @@ -102,7 +106,7 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret return meta; }, get isLocked (): boolean { - return !secretKey || secretKey.length === 0 || isEmpty(secretKey); + return isLocked(secretKey); }, get publicKey (): Uint8Array { return publicKey; @@ -124,10 +128,10 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret } }, derive: (suri: string, meta?: KeyringPair$Meta): KeyringPair => { - assert(secretKey, 'Cannot derive on a locked keypair'); + assert(!isLocked(secretKey), 'Cannot derive on a locked keypair'); const { path } = keyExtractPath(suri); - const derived = keyFromPath({ publicKey, secretKey: secretKey }, path, type); + const derived = keyFromPath({ publicKey, secretKey: secretKey! }, path, type); return createPair({ toSS58, type }, derived, meta, null); }, @@ -139,8 +143,10 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret setMeta: (additional: KeyringPair$Meta): void => { meta = { ...meta, ...additional }; }, - sign: (message: Uint8Array, options?: SignOptions): Uint8Array => - sign(type, message, { publicKey, secretKey }, options), + sign: (message: Uint8Array, options?: SignOptions): Uint8Array => { + assert(!isLocked(secretKey), 'Cannot sign with a locked key pair'); + return sign(type, message, { publicKey, secretKey }, options); + }, toJson: (passphrase?: string): KeyringPair$Json => toJson(type, { meta, publicKey }, encode({ publicKey, secretKey }, passphrase), !!passphrase), verify: (message: Uint8Array, signature: Uint8Array): boolean =>