Schnorrkel support (#297)

* Initial schnorrkel support

* Start conversion of actual keyring

* Wrap encoding/decoding (needs another check)

* Manage encoding version & types with passthrough

* Update to version with working sign

* Cleanup pair interface (with seed)

* Fix failing tests

* Align naclSign signature

* Update tests for nacl (with TS conversion)

* Bump version with CHANGELOG

* Remove unneeded dep

* Use KeyringOptions instead of PairType

* Do isNumber check on addressPrefix
This commit is contained in:
Jaco Greeff
2019-02-13 16:46:33 +01:00
committed by GitHub
parent 0f53f0ebf0
commit 2aba82e569
60 changed files with 693 additions and 293 deletions
@@ -4,7 +4,7 @@
import testingPairs from '../testingPairs';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('decode', () => {
it('fails when no data provided', () => {
@@ -14,9 +14,11 @@ describe('decode', () => {
});
it('returns correct publicKey from encoded', () => {
const PASS = 'testing';
expect(
() => keyring.alice.decodePkcs8(
'testing', keyring.alice.encodePkcs8('testing')
PASS, keyring.alice.encodePkcs8(PASS)
)
).not.toThrow();
});
+19 -16
View File
@@ -2,44 +2,47 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert, stringToU8a, u8aConcat, u8aFixLength } from '@polkadot/util/index';
import { naclDecrypt, naclKeypairFromSeed } from '@polkadot/util-crypto/index';
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util/index';
import { naclDecrypt } from '@polkadot/util-crypto/index';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
import { KEY_LENGTH, NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
const KEY_LENGTH = 32;
const SEED_OFFSET = PKCS8_HEADER.length;
const DIV_OFFSET = SEED_OFFSET + KEY_LENGTH;
const PUBLIC_OFFSET = SEED_OFFSET + KEY_LENGTH + PKCS8_DIVIDER.length;
const PUBLIC_OFFSET = DIV_OFFSET + PKCS8_DIVIDER.length;
export default function decode (passphrase?: string, _encrypted?: Uint8Array) {
type DecodeResult = {
publicKey: Uint8Array,
seed: Uint8Array
};
export default function decode (passphrase?: string, _encrypted?: Uint8Array | null): DecodeResult {
assert(_encrypted, `No encrypted data available to decode`);
const encrypted = (_encrypted as Uint8Array);
const encoded = passphrase
? (naclDecrypt(
encrypted.subarray(24),
encrypted.subarray(0, 24),
u8aFixLength(stringToU8a(passphrase), 256, true)) as Uint8Array)
encrypted.subarray(NONCE_LENGTH),
encrypted.subarray(0, NONCE_LENGTH),
u8aFixLength(stringToU8a(passphrase), 256, true)
) as Uint8Array)
: encrypted;
assert(encoded, `Unable to unencrypt using the supplied passphrase`);
const header = encoded.subarray(0, PKCS8_HEADER.length);
const divider = encoded.subarray(DIV_OFFSET, DIV_OFFSET + PKCS8_DIVIDER.length);
assert(header.toString() === PKCS8_HEADER.toString(), 'Invalid Pkcs8 header found in body');
const divider = encoded.subarray(DIV_OFFSET, DIV_OFFSET + PKCS8_DIVIDER.length);
assert(divider.toString() === PKCS8_DIVIDER.toString(), 'Invalid Pkcs8 divider found in body');
const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH);
const seed = encoded.subarray(SEED_OFFSET, SEED_OFFSET + KEY_LENGTH);
const secretKey = u8aConcat(seed, publicKey);
const validate = naclKeypairFromSeed(seed);
assert(validate.publicKey.toString() === publicKey.toString(), 'Pkcs8 decoded publicKeys are not matching');
const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH);
return {
publicKey,
secretKey
seed
};
}
+6
View File
@@ -4,8 +4,14 @@
const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]);
const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
const KEY_LENGTH = 32;
const SEC_LENGTH = 64;
const NONCE_LENGTH = 24;
export {
KEY_LENGTH,
SEC_LENGTH,
NONCE_LENGTH,
PKCS8_DIVIDER,
PKCS8_HEADER
};
@@ -8,7 +8,7 @@ import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
const PKCS8_LENGTH = PKCS8_DIVIDER.length + PKCS8_HEADER.length + 64;
const ENCODED_LENGTH = 125;
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('encode', () => {
it('returns PKCS8 when no passphrase supplied', () => {
+6 -4
View File
@@ -2,17 +2,19 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { stringToU8a, u8aConcat, u8aFixLength } from '@polkadot/util/index';
import { stringToU8a, u8aConcat, u8aFixLength, assert } from '@polkadot/util/index';
import { naclEncrypt } from '@polkadot/util-crypto/index';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
export default function encode (secretKey: Uint8Array, passphrase?: string): Uint8Array {
export default function encode (publicKey: Uint8Array, seed?: Uint8Array | null, passphrase?: string): Uint8Array {
assert(seed, 'Expected a valid seed to be passed to encode');
const encoded = u8aConcat(
PKCS8_HEADER,
secretKey.subarray(0, 32),
seed as Uint8Array,
PKCS8_DIVIDER,
secretKey.subarray(32, 64)
publicKey
);
if (!passphrase) {
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Meta } from '../types';
import { PairState } from './types';
export default function getMeta (state: PairState): KeyringPair$Meta {
return state.meta;
}
@@ -4,7 +4,7 @@
import testingPairs from '../testingPairs';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('pair', () => {
const SIGNATURE = new Uint8Array([80, 191, 198, 147, 225, 207, 75, 88, 126, 39, 129, 109, 191, 38, 72, 181, 75, 254, 81, 143, 244, 79, 237, 38, 236, 141, 28, 252, 134, 26, 169, 234, 79, 33, 153, 158, 151, 34, 175, 188, 235, 20, 35, 135, 83, 120, 139, 211, 233, 130, 1, 208, 201, 215, 73, 80, 56, 98, 185, 196, 11, 8, 193, 14]);
+33 -23
View File
@@ -3,21 +3,18 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types';
import { PairState } from './types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, PairType } from '../types';
import { PairInfo } from './types';
import { naclSign, naclVerify } from '@polkadot/util-crypto/index';
import { naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify } from '@polkadot/util-crypto/index';
import { encodeAddress } from '../address';
import decode from './decode';
import encode from './encode';
import getMeta from './getMeta';
import setMeta from './setMeta';
import toJson from './toJson';
/**
* @name pair
* @signature pair ({ publicKey, secretKey }: KeypairType, meta: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair
* @summary Creates a keyring pair object
* @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
* The keyring pair stores the account state including the encoded address and associated metadata.
@@ -47,25 +44,37 @@ import toJson from './toJson';
* an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
* property that indicates whether the public key value of the `encoded` property is encoded or not.
*/
export default function pair ({ publicKey, secretKey }: KeypairType, meta: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair {
const state: PairState = {
meta: { ...meta },
publicKey
};
export default function pair (type: PairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null): KeyringPair {
const fromSeed = (seed: Uint8Array) =>
type === 'sr25519'
? schnorrkelFromSeed(seed)
: naclFromSeed(seed);
const sign = (message: Uint8Array, pair: Partial<KeypairType>) =>
type === 'sr25519'
? schnorrkelSign(message, pair)
: naclSign(message, pair);
const verify = (message: Uint8Array, signature: Uint8Array) =>
type === 'sr25519'
? schnorrkelVerify(message, signature, publicKey)
: naclVerify(message, signature, publicKey);
let secretKey: Uint8Array | undefined = seed
? fromSeed(seed).secretKey
: undefined;
return {
address: (): string =>
encodeAddress(state.publicKey),
decodePkcs8: (passphrase?: string, encoded?: Uint8Array): void => {
const decoded = decode(passphrase, encoded || defaultEncoded);
encodeAddress(publicKey),
decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => {
const decoded = decode(passphrase, _encoded || encoded);
state.publicKey = decoded.publicKey;
secretKey = decoded.secretKey;
publicKey = decoded.publicKey;
seed = decoded.seed;
secretKey = fromSeed(seed).secretKey;
},
encodePkcs8: (passphrase?: string): Uint8Array =>
encode(secretKey, passphrase),
encode(publicKey, seed, passphrase),
getMeta: (): KeyringPair$Meta =>
getMeta(state),
meta,
isLocked: (): boolean =>
(!secretKey || secretKey.length === 0),
lock: (): void => {
@@ -73,13 +82,14 @@ export default function pair ({ publicKey, secretKey }: KeypairType, meta: Keyri
},
publicKey: (): Uint8Array =>
publicKey,
setMeta: (meta: KeyringPair$Meta): void =>
setMeta(state, meta),
setMeta: (_meta: KeyringPair$Meta): void => {
meta = meta;
},
sign: (message: Uint8Array): Uint8Array =>
naclSign(message, secretKey),
sign(message, { publicKey, secretKey }),
toJson: (passphrase?: string): KeyringPair$Json =>
toJson(state, encode(secretKey, passphrase), !!passphrase),
toJson({ meta, publicKey }, encode(publicKey, seed, passphrase), !!passphrase),
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
naclVerify(message, signature, publicKey)
verify(message, signature)
};
}
-13
View File
@@ -1,13 +0,0 @@
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Meta } from '../types';
import { PairState } from './types';
export default function setMeta (state: PairState, meta: KeyringPair$Meta): void {
state.meta = {
...state.meta,
...meta
};
}
@@ -4,7 +4,7 @@
import testingPairs from '../testingPairs';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('toJson', () => {
it('creates an unencoded output with no passphrase', () => {
@@ -16,7 +16,7 @@ describe('toJson', () => {
encoding: {
content: 'pkcs8',
type: 'none',
version: '0'
version: '1'
},
meta: {
name: 'alice',
@@ -34,7 +34,7 @@ describe('toJson', () => {
encoding: {
content: 'pkcs8',
type: 'xsalsa20-poly1305',
version: '0'
version: '1'
}
});
});
+7 -4
View File
@@ -2,14 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Json } from '../types';
import { PairState } from './types';
import { KeyringPair$Json, KeyringPair$Meta } from '../types';
import { u8aToHex } from '@polkadot/util/index';
import { encodeAddress } from '../address';
export default function toJson ({ publicKey, meta }: PairState, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
type PairStateJson = KeyringPair$Meta & {
publicKey: Uint8Array
};
export default function toJson ({ publicKey, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
return {
address: encodeAddress(publicKey),
encoded: u8aToHex(encoded),
@@ -18,7 +21,7 @@ export default function toJson ({ publicKey, meta }: PairState, encoded: Uint8Ar
type: isEncrypted
? 'xsalsa20-poly1305'
: 'none',
version: '0'
version: '1'
},
meta
};
+3 -5
View File
@@ -2,9 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Meta } from '../types';
export type PairState = {
meta: KeyringPair$Meta,
publicKey: Uint8Array
export type PairInfo = {
publicKey: Uint8Array,
seed?: Uint8Array
};