Throws an error when signing with a locked keypair (#578)

* Throws an error when signing with a locked keypair

Fixes #576

* Fix lint errors

* Switch to `assert` and fix nits
This commit is contained in:
h4x3rotab
2020-04-01 20:44:40 +02:00
committed by GitHub
parent 697a452ba6
commit c98e994777
2 changed files with 20 additions and 6 deletions
+8
View File
@@ -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');
});
});
+12 -6
View File
@@ -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 =>