diff --git a/package.json b/package.json index abadf93f9..66a8f9a79 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ }, "devDependencies": { "@babel/core": "^7.3.4", - "@polkadot/dev": "^0.26.1", + "@polkadot/dev": "^0.27.1", "@polkadot/ts": "^0.1.56", "gh-pages": "^2.0.1" } diff --git a/packages/keyring/src/index.spec.ts b/packages/keyring/src/index.spec.ts index 3da0e758f..efd6c4aeb 100644 --- a/packages/keyring/src/index.spec.ts +++ b/packages/keyring/src/index.spec.ts @@ -2,8 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import '@polkadot/util-crypto/schnorrkel/test-polyfill'; - import { hexToU8a, stringToU8a } from '@polkadot/util'; import { cryptoWaitReady } from '@polkadot/util-crypto'; diff --git a/packages/keyring/src/index.ts b/packages/keyring/src/index.ts index 2c85cca87..d1fafe1dc 100644 --- a/packages/keyring/src/index.ts +++ b/packages/keyring/src/index.ts @@ -33,7 +33,7 @@ export default class Keyring implements KeyringInstance { private _type: KeypairType; constructor (options: KeyringOptions = {}) { - options.type = options.type || 'ed25519'; + options.type = options.type || 'sr25519'; assert(options && ['ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519' or 'sr25519', found '${options.type}`); @@ -47,20 +47,6 @@ export default class Keyring implements KeyringInstance { encodeAddress = encodeAddress; setAddressPrefix = setAddressPrefix; - /** - * @description True for Ed25519 keyring - */ - get isEd25519 (): boolean { - return this.type === 'ed25519'; - } - - /** - * @description True for Ed25519 keyring - */ - get isSr25519 (): boolean { - return this.type === 'sr25519'; - } - /** * @description retrieve the pairs (alias for getPairs) */ @@ -136,11 +122,11 @@ export default class Keyring implements KeyringInstance { * `addPair` to store in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value. */ addFromSeed (seed: Uint8Array, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair { - const keypair = this.isSr25519 + const keypair = type === 'sr25519' ? schnorrkelFromSeed(seed) : naclFromSeed(seed); - return this.addPair(createPair(type, { ...keypair, seed }, meta, null)); + return this.addPair(createPair(type, keypair, meta, null)); } /** @@ -165,13 +151,12 @@ export default class Keyring implements KeyringInstance { } } - const { publicKey } = this.isSr25519 + const keypair = type === 'sr25519' ? schnorrkelFromSeed(seed) : naclFromSeed(seed); + const derived = keyFromPath(keypair, path, type); - return this.addPair( - createPair(type, keyFromPath({ publicKey, seed }, path, type), meta, null) - ); + return this.addPair(createPair(type, derived, meta, null)); } /** diff --git a/packages/keyring/src/pair/decode.ts b/packages/keyring/src/pair/decode.ts index e104932a3..78ef18507 100644 --- a/packages/keyring/src/pair/decode.ts +++ b/packages/keyring/src/pair/decode.ts @@ -6,14 +6,12 @@ import { assert, stringToU8a, u8aFixLength } from '@polkadot/util'; import { naclDecrypt } from '@polkadot/util-crypto'; import { PairInfo } from './types'; -import { KEY_LENGTH, NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER } from './defaults'; +import { NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH } from './defaults'; const SEED_OFFSET = PKCS8_HEADER.length; -const DIV_OFFSET = SEED_OFFSET + KEY_LENGTH; -const PUBLIC_OFFSET = DIV_OFFSET + PKCS8_DIVIDER.length; type DecodeResult = PairInfo & { - seed: Uint8Array + secretKey: Uint8Array }; export default function decode (passphrase?: string, _encrypted?: Uint8Array | null): DecodeResult { @@ -34,15 +32,24 @@ export default function decode (passphrase?: string, _encrypted?: Uint8Array | n assert(header.toString() === PKCS8_HEADER.toString(), 'Invalid Pkcs8 header found in body'); - const divider = encoded.subarray(DIV_OFFSET, DIV_OFFSET + PKCS8_DIVIDER.length); + let secretKey = encoded.subarray(SEED_OFFSET, SEED_OFFSET + SEC_LENGTH); + let divOffset = SEED_OFFSET + SEC_LENGTH; + let divider = encoded.subarray(divOffset, divOffset + PKCS8_DIVIDER.length); + + // old-style, we have the seed here + if (divider.toString() !== PKCS8_DIVIDER.toString()) { + divOffset = SEED_OFFSET + SEED_LENGTH; + secretKey = encoded.subarray(SEED_OFFSET, divOffset); + divider = encoded.subarray(divOffset, divOffset + PKCS8_DIVIDER.length); + } assert(divider.toString() === PKCS8_DIVIDER.toString(), 'Invalid Pkcs8 divider found in body'); - const seed = encoded.subarray(SEED_OFFSET, SEED_OFFSET + KEY_LENGTH); - const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH); + const pubOffset = divOffset + PKCS8_DIVIDER.length; + const publicKey = encoded.subarray(pubOffset, pubOffset + PUB_LENGTH); return { publicKey, - seed + secretKey }; } diff --git a/packages/keyring/src/pair/defaults.ts b/packages/keyring/src/pair/defaults.ts index ed57460d2..1be12dc16 100644 --- a/packages/keyring/src/pair/defaults.ts +++ b/packages/keyring/src/pair/defaults.ts @@ -2,16 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +const NONCE_LENGTH = 24; 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 PUB_LENGTH = 32; const SEC_LENGTH = 64; -const NONCE_LENGTH = 24; +const SEED_LENGTH = 32; export { - KEY_LENGTH, - SEC_LENGTH, NONCE_LENGTH, PKCS8_DIVIDER, - PKCS8_HEADER + PKCS8_HEADER, + PUB_LENGTH, + SEC_LENGTH, + SEED_LENGTH }; diff --git a/packages/keyring/src/pair/encode.spec.ts b/packages/keyring/src/pair/encode.spec.ts index f24d25ab2..7e9befc6a 100644 --- a/packages/keyring/src/pair/encode.spec.ts +++ b/packages/keyring/src/pair/encode.spec.ts @@ -3,10 +3,10 @@ // of the Apache-2.0 license. See the LICENSE file for details. import testingPairs from '../testingPairs'; -import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults'; +import { PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH } from './defaults'; -const PKCS8_LENGTH = PKCS8_DIVIDER.length + PKCS8_HEADER.length + 64; -const ENCODED_LENGTH = 125; +const PKCS8_LENGTH = PKCS8_DIVIDER.length + PKCS8_HEADER.length + PUB_LENGTH + SEC_LENGTH; +const ENCODED_LENGTH = 157; const keyring = testingPairs({ type: 'ed25519' }, false); diff --git a/packages/keyring/src/pair/encode.ts b/packages/keyring/src/pair/encode.ts index 8db408f9e..0577d607d 100644 --- a/packages/keyring/src/pair/encode.ts +++ b/packages/keyring/src/pair/encode.ts @@ -9,12 +9,12 @@ import { naclEncrypt } from '@polkadot/util-crypto'; import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults'; -export default function encode ({ publicKey, seed }: PairInfo, passphrase?: string): Uint8Array { - assert(seed, 'Expected a valid seed to be passed to encode'); +export default function encode ({ publicKey, secretKey }: PairInfo, passphrase?: string): Uint8Array { + assert(secretKey, 'Expected a valid secretKey to be passed to encode'); const encoded = u8aConcat( PKCS8_HEADER, - seed as Uint8Array, + secretKey as Uint8Array, PKCS8_DIVIDER, publicKey ); diff --git a/packages/keyring/src/pair/index.ts b/packages/keyring/src/pair/index.ts index 635d64365..3aee03a9d 100644 --- a/packages/keyring/src/pair/index.ts +++ b/packages/keyring/src/pair/index.ts @@ -62,13 +62,7 @@ const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, p * 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 createPair (type: KeypairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null): KeyringPair { - let secretKey: Uint8Array | undefined; - - if (seed) { - secretKey = fromSeed(type, seed).secretKey; - } - +export default function createPair (type: KeypairType, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null): KeyringPair { return { type, address: (): string => @@ -76,11 +70,18 @@ export default function createPair (type: KeypairType, { publicKey, seed }: Pair decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => { const decoded = decode(passphrase, _encoded || encoded); - publicKey = decoded.publicKey; - secretKey = fromSeed(type, decoded.seed).secretKey; + if (decoded.secretKey.length === 64) { + publicKey = decoded.publicKey; + secretKey = decoded.secretKey; + } else { + const pair = fromSeed(type, decoded.secretKey); + + publicKey = pair.publicKey; + secretKey = pair.secretKey; + } }, encodePkcs8: (passphrase?: string): Uint8Array => - encode({ publicKey, seed }, passphrase), + encode({ publicKey, secretKey }, passphrase), getMeta: (): KeyringPair$Meta => meta, isLocked: (): boolean => @@ -96,7 +97,7 @@ export default function createPair (type: KeypairType, { publicKey, seed }: Pair sign: (message: Uint8Array): Uint8Array => sign(type, message, { publicKey, secretKey }), toJson: (passphrase?: string): KeyringPair$Json => - toJson(type, { meta, publicKey }, encode({ publicKey, seed }, passphrase), !!passphrase), + toJson(type, { meta, publicKey }, encode({ publicKey, secretKey }, passphrase), !!passphrase), verify: (message: Uint8Array, signature: Uint8Array): boolean => verify(type, message, signature, publicKey) }; diff --git a/packages/keyring/src/pair/toJson.spec.ts b/packages/keyring/src/pair/toJson.spec.ts index 3a252acf6..834324b28 100644 --- a/packages/keyring/src/pair/toJson.spec.ts +++ b/packages/keyring/src/pair/toJson.spec.ts @@ -12,11 +12,11 @@ describe('toJson', () => { keyring.alice.toJson() ).toMatchObject({ address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ', - encoded: '0x3053020101300506032b657004220420416c696365202020202020202020202020202020202020202020202020202020a123032100d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f', + encoded: '0x3053020101300506032b657004220420416c696365202020202020202020202020202020202020202020202020202020d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4fa123032100d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f', encoding: { content: ['pkcs8', 'ed25519'], type: 'none', - version: '1' + version: '2' }, meta: { name: 'alice', @@ -28,13 +28,13 @@ describe('toJson', () => { it('creates an encoded output with passphrase', () => { const json = keyring.alice.toJson('testing'); - expect(json.encoded).toHaveLength(252); + expect(json.encoded).toHaveLength(316); expect(json).toMatchObject({ address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ', encoding: { content: ['pkcs8', 'ed25519'], type: 'xsalsa20-poly1305', - version: '1' + version: '2' } }); }); diff --git a/packages/keyring/src/pair/toJson.ts b/packages/keyring/src/pair/toJson.ts index 8ea9e43c6..1ebdcb3d4 100644 --- a/packages/keyring/src/pair/toJson.ts +++ b/packages/keyring/src/pair/toJson.ts @@ -22,7 +22,7 @@ export default function toJson (type: KeypairType, { publicKey, meta }: PairStat type: isEncrypted ? 'xsalsa20-poly1305' : 'none', - version: '1' + version: '2' }, meta }; diff --git a/packages/keyring/src/pair/types.ts b/packages/keyring/src/pair/types.ts index 62fea5a98..23dee4b6f 100644 --- a/packages/keyring/src/pair/types.ts +++ b/packages/keyring/src/pair/types.ts @@ -4,5 +4,6 @@ export type PairInfo = { publicKey: Uint8Array, + secretKey?: Uint8Array, seed?: Uint8Array | null }; diff --git a/packages/keyring/src/suri.spec.ts b/packages/keyring/src/suri.spec.ts index 0efdf58d4..5070153c3 100644 --- a/packages/keyring/src/suri.spec.ts +++ b/packages/keyring/src/suri.spec.ts @@ -28,39 +28,41 @@ const TESTS = [ uri: `${PHRASE}/foo` }, { - pk: '0x545ecfd16d2ccbece3c3d8e284ba21b624d53a010d19673285a02aca92a62724', - ss: '5DyL3XpLxWKJgeUYkFFhepm9DWFMHjvW6JKyVst6F6bX1DQw', + pk: '0x547d4a55642ec7ebadc0bd29b6e570b8c926059b3c0655d4948075e9a7e6f31e', + ss: '5DyV6fZuvPemWrUqBgWwTSgoV86w6xms3KhkFU6cQcWxTwuv', uri: `${PHRASE}//foo` }, { - pk: '0xc6755aae1c6a172dc06a8bcf46d119a56ec04b22d7c673255dfc6fb5d7afd250', - ss: '5GYvCYSJxHTNRMqtkLX5AGpTYjSiaHeCdVgBQJWp6Jb1eWZR', + pk: '0x3841947ffcde6f5fef26fb68b59bb8665637e30e32ec2051f99cf6b9c674fe09', + ss: '5DLU27is5iViNopQb2KxsTyPx6j4vCu8X3sk3j3NNLkPCsLj', uri: `${PHRASE}//foo/bar` }, { - pk: '0xbed7420abfb5398eaa5c1e5ce484bf200c735ee0606ed97048dae9a6e7b1aa59', - ss: '5GNvuAdKoC5mHzmSSctsZAC4WkrhZC5W7FBB4G3N5XMCBhMD', + pk: '0xdc142f7476a7b0aa262aeccf207f1d18daa90762db393006741e8a31f39dbc53', + ss: '5H3GPTqDSpjkfDwbHy12PD6BWm8jvGSX4xYC8UMprHpTPhQg', uri: `${PHRASE}/foo//bar` }, { - pk: '0xd0c0cb1e54a54222058552d7bf94a8abaaafd778adae57a30a9bba5afb01f17a', - ss: '5GnR66wpdL4hnArhDRyyBCqchgVYXVMQDDSZQWegfSSnAEFh', + pk: '0xa2e56b06407a6d1e819d2fc33fa0ec604b29c2e868b70b3696bb049b8725934b', + ss: '5FkHmNgbg64MwStgCyDi2Uw3ufFu11mqQgmWT9uwK4Lgi3SE', uri: `${PHRASE}//foo/bar//42/69` }, { - pk: '0x12dccbb4d851a7bc36bb4f5f6f84a1caaf04c542509b1e3b84f1f47445c1bf08', - ss: '5CVSJt82L8jYodrwYKCoP6LtC6V2VYPHYMHtHzjhdNr1KDdu', + pk: '0x0e0d24e3e1ff2c07f269c99e2e0df8681fda1851ac42fc846ca2daaa90cd8f14', + ss: '5CP8S23JBNXYNpJsL7ESPJBNnUZE6itcfM4EnDxEhaVEUAWL', uri: `${PHRASE}//foo/bar//42/69///password` + }, + { + pk: '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', + ss: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKv3gB', + uri: `${PHRASE}//Alice` } ]; -// TODO Enable once we have proper schnorrkel support -describe.skip('keyring.addFromUri', () => { - let keyring: Keyring; +describe('keyring.addFromUri', () => { + const keyring = new Keyring({ type: 'sr25519' }); beforeEach(async () => { - keyring = new Keyring({ type: 'sr25519' }); - await cryptoWaitReady(); }); diff --git a/packages/keyring/src/testing.ts b/packages/keyring/src/testing.ts index 064856ca1..d81adf357 100644 --- a/packages/keyring/src/testing.ts +++ b/packages/keyring/src/testing.ts @@ -18,7 +18,9 @@ const SEEDS = ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve', 'Ferdie']; * @description The test accounts (i.e. alice, bob, dave, eve, ferdie) * are available on the dev chain and each test account is initialised with DOT funds. */ -export default function testKeyring (options?: KeyringOptions, isDerived: boolean = true): KeyringInstance { +export default function testKeyring (options: KeyringOptions = {}, isDerived: boolean = true): KeyringInstance { + options.type = options.type || (isDerived ? 'sr25519' : 'ed25519'); + const keyring = new Keyring(options); SEEDS.forEach((entry) => { @@ -28,7 +30,7 @@ export default function testKeyring (options?: KeyringOptions, isDerived: boolea const pair = keyring.addFromUri(phrase, { isTesting: true, name: entry.toLowerCase() - }, isDerived ? 'sr25519' : 'ed25519'); + }, isDerived ? 'sr25519' : options.type); pair.lock = () => { // we don't have lock/unlock functionality here diff --git a/packages/keyring/src/testingPairs.spec.ts b/packages/keyring/src/testingPairs.spec.ts index b0f423886..23b2e7763 100644 --- a/packages/keyring/src/testingPairs.spec.ts +++ b/packages/keyring/src/testingPairs.spec.ts @@ -2,15 +2,16 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { cryptoWaitReady } from '@polkadot/util-crypto/index'; +import { cryptoWaitReady } from '@polkadot/util-crypto'; + import testingPairs from './testingPairs'; -describe('testing', () => { +describe('testingPairs', () => { beforeEach(async () => { await cryptoWaitReady(); }); - it.skip('creates without failing', () => { + it('creates without failing', () => { expect( Object.keys(testingPairs()) ).toHaveLength(1 + 6); @@ -25,6 +26,6 @@ describe('testing', () => { it('has the correct address for Alice (HDKD)', () => { expect( testingPairs({ type: 'ed25519' }, true).alice.address() - ).toEqual('5CfbEZ9EvFsdZ2yHPt5BsSsGpM9mZPkahsZm6WeZE87mJKKT'); + ).toEqual('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKv3gB'); }); }); diff --git a/packages/keyring/src/types.ts b/packages/keyring/src/types.ts index 518e16be6..e88d8511a 100644 --- a/packages/keyring/src/types.ts +++ b/packages/keyring/src/types.ts @@ -14,7 +14,7 @@ export type KeyringPair$Meta = { [index: string]: any }; -export type KeyringPair$JsonVersion = '0' | '1'; +export type KeyringPair$JsonVersion = '0' | '1' | '2'; export type KeyringPair$JsonEncoding = { content: ['pkcs8', KeypairType], @@ -53,8 +53,6 @@ export interface KeyringPairs { } export interface KeyringInstance { - readonly isEd25519: boolean; - readonly isSr25519: boolean; readonly pairs: Array; readonly publicKeys: Array; readonly type: KeypairType; diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index 350c4160c..7ca1490f3 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -29,7 +29,7 @@ "homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme", "dependencies": { "@babel/runtime": "^7.3.4", - "@polkadot/schnorrkel-js": "^0.2.0-3", + "@polkadot/schnorrkel-js": "^0.2.0-12", "@polkadot/util": "^0.38.0-beta.2", "@types/bip39": "^2.4.2", "@types/pbkdf2": "^3.0.0", diff --git a/packages/util-crypto/src/key/fromPath.ts b/packages/util-crypto/src/key/fromPath.ts index 8e7c6577c..5aca2ba49 100644 --- a/packages/util-crypto/src/key/fromPath.ts +++ b/packages/util-crypto/src/key/fromPath.ts @@ -2,16 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { KeypairType, Seedpair } from '../types'; +import { KeypairType, Keypair } from '../types'; import DeriveJunction from './DeriveJunction'; import keyHdkdEd15519 from './hdkdEd25519'; import keyHdkdSr15519 from './hdkdSr25519'; -export default function keyFromPath (seedpair: Seedpair, path: Array, type: KeypairType): Seedpair { - return path.reduce((seedpair, junction) => { - return type === 'ed25519' - ? keyHdkdEd15519(seedpair, junction) - : keyHdkdSr15519(seedpair, junction); - }, seedpair); +export default function keyFromPath (pair: Keypair, path: Array, type: KeypairType): Keypair { + const isEd25519 = type === 'ed25519'; + + return path.reduce((pair, junction) => { + return isEd25519 + ? keyHdkdEd15519(pair, junction) + : keyHdkdSr15519(pair, junction); + }, pair); } diff --git a/packages/util-crypto/src/key/hdkdEd25519.ts b/packages/util-crypto/src/key/hdkdEd25519.ts index aa817ec46..b0f370ca6 100644 --- a/packages/util-crypto/src/key/hdkdEd25519.ts +++ b/packages/util-crypto/src/key/hdkdEd25519.ts @@ -2,21 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Seedpair } from '../types'; +import { Keypair } from '../types'; import { assert } from '@polkadot/util'; -import naclDerivePrivate from '../nacl/derivePrivate'; +import naclDeriveHard from '../nacl/deriveHard'; import naclKeypairFromSeed from '../nacl/keypair/fromSeed'; import DeriveJunction from './DeriveJunction'; -export default function keyHdkdEd25519 ({ seed }: Seedpair, { chainCode, isHard }: DeriveJunction): Seedpair { +export default function keyHdkdEd25519 (keypair: Keypair, { chainCode, isHard }: DeriveJunction): Keypair { assert(isHard, 'A soft key was found in the path (and is unsupported)'); - const derived = naclDerivePrivate(seed, chainCode); - - return { - publicKey: naclKeypairFromSeed(derived).publicKey, - seed: derived - }; + return naclKeypairFromSeed( + naclDeriveHard(keypair.secretKey.subarray(0, 32), chainCode) + ); } diff --git a/packages/util-crypto/src/key/hdkdSr25519.ts b/packages/util-crypto/src/key/hdkdSr25519.ts index e881be7d4..59fc664f7 100644 --- a/packages/util-crypto/src/key/hdkdSr25519.ts +++ b/packages/util-crypto/src/key/hdkdSr25519.ts @@ -2,27 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Seedpair } from '../types'; +import { Keypair } from '../types'; -import schnorrkelKeypairFromSeed from '../schnorrkel/keypair/fromSeed'; -import schnorrkelDerivePrivate from '../schnorrkel/derivePrivate'; -import schnorrkelDerivePublic from '../schnorrkel/derivePublic'; -import schnorrkelSecretFromSeed from '../schnorrkel/secretFromSeed'; +import schnorrkelDeriveHard from '../schnorrkel/deriveHard'; +import schnorrkelDeriveSoft from '../schnorrkel/deriveSoft'; import DeriveJunction from './DeriveJunction'; -export default function keyHdkdSr25519 ({ seed, publicKey }: Seedpair, { chainCode, isSoft }: DeriveJunction): Seedpair { - if (isSoft) { - return { - publicKey: schnorrkelDerivePublic(publicKey, chainCode), - seed - }; - } - - const secretKey = schnorrkelSecretFromSeed(seed); - const derived = schnorrkelDerivePrivate(secretKey, chainCode); - - return { - publicKey: schnorrkelKeypairFromSeed(derived).publicKey, - seed: derived - }; +export default function keyHdkdSr25519 (keypair: Keypair, { chainCode, isSoft }: DeriveJunction): Keypair { + return isSoft + ? schnorrkelDeriveSoft(keypair, chainCode) + : schnorrkelDeriveHard(keypair, chainCode); } diff --git a/packages/util-crypto/src/nacl/derivePrivate.ts b/packages/util-crypto/src/nacl/deriveHard.ts similarity index 74% rename from packages/util-crypto/src/nacl/derivePrivate.ts rename to packages/util-crypto/src/nacl/deriveHard.ts index 3f24bc499..c312d45e5 100644 --- a/packages/util-crypto/src/nacl/derivePrivate.ts +++ b/packages/util-crypto/src/nacl/deriveHard.ts @@ -8,8 +8,8 @@ import blake2AsU8a from '../blake2/asU8a'; const HDKD = compactAddLength(stringToU8a('Ed25519HDKD')); -export default function derivePrivate (secretKey: Uint8Array, chainCode: Uint8Array): Uint8Array { +export default function deriveHard (seed: Uint8Array, chainCode: Uint8Array): Uint8Array { return blake2AsU8a( - u8aConcat(HDKD, secretKey, chainCode) + u8aConcat(HDKD, seed, chainCode) ); } diff --git a/packages/util-crypto/src/nacl/index.ts b/packages/util-crypto/src/nacl/index.ts index 9f61b6c37..e246737d3 100644 --- a/packages/util-crypto/src/nacl/index.ts +++ b/packages/util-crypto/src/nacl/index.ts @@ -6,7 +6,7 @@ * @summary Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures */ export { default as naclDecrypt } from './decrypt'; -export { default as derivePrivate } from './derivePrivate'; +export { default as deriveHard } from './deriveHard'; export { default as naclEncrypt } from './encrypt'; export { default as naclKeypairFromRandom } from './keypair/fromRandom'; export { default as naclKeypairFromSecret } from './keypair/fromSecret'; diff --git a/packages/util-crypto/src/schnorrkel/deriveHard.ts b/packages/util-crypto/src/schnorrkel/deriveHard.ts new file mode 100644 index 000000000..5654230e8 --- /dev/null +++ b/packages/util-crypto/src/schnorrkel/deriveHard.ts @@ -0,0 +1,18 @@ +// Copyright 2017-2019 @polkadot/util-crypto 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 { Keypair } from '../types'; + +import './polyfill'; + +import schnorrkel from '@polkadot/schnorrkel-js'; + +import keypairFromU8a from './keypair/fromU8a'; +import keypairToU8a from './keypair/toU8a'; + +export default function deriveHard (keypair: Keypair, chainCode: Uint8Array): Keypair { + return keypairFromU8a( + schnorrkel.deriveKeypairHard(keypairToU8a(keypair), chainCode) + ); +} diff --git a/packages/util-crypto/src/schnorrkel/derivePrivate.ts b/packages/util-crypto/src/schnorrkel/derivePrivate.ts deleted file mode 100644 index 834770d15..000000000 --- a/packages/util-crypto/src/schnorrkel/derivePrivate.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2017-2019 @polkadot/util-crypto 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 { compactAddLength, stringToU8a, u8aConcat } from '@polkadot/util'; - -import blake2AsU8a from '../blake2/asU8a'; - -const HDKD = compactAddLength(stringToU8a('SchnorrRistrettoHDKD')); - -export default function derivePrivate (secretKey: Uint8Array, chainCode: Uint8Array): Uint8Array { - return blake2AsU8a( - u8aConcat(HDKD, compactAddLength(secretKey), chainCode) - ); -} diff --git a/packages/util-crypto/src/schnorrkel/derivePublic.ts b/packages/util-crypto/src/schnorrkel/derivePublic.ts deleted file mode 100644 index e5439f9fd..000000000 --- a/packages/util-crypto/src/schnorrkel/derivePublic.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2017-2019 @polkadot/util-crypto 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 schnorrkel from '@polkadot/schnorrkel-js'; - -export default function derivePublic (publicKey: Uint8Array, chainCode: Uint8Array): Uint8Array { - return schnorrkel.softDerivePublic(publicKey, chainCode); -} diff --git a/packages/util-crypto/src/schnorrkel/deriveSoft.ts b/packages/util-crypto/src/schnorrkel/deriveSoft.ts new file mode 100644 index 000000000..eb70c4243 --- /dev/null +++ b/packages/util-crypto/src/schnorrkel/deriveSoft.ts @@ -0,0 +1,18 @@ +// Copyright 2017-2019 @polkadot/util-crypto 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 { Keypair } from '../types'; + +import './polyfill'; + +import schnorrkel from '@polkadot/schnorrkel-js'; + +import keypairFromU8a from './keypair/fromU8a'; +import keypairToU8a from './keypair/toU8a'; + +export default function deriveSoft (keypair: Keypair, chainCode: Uint8Array): Keypair { + return keypairFromU8a( + schnorrkel.deriveKeypairSoft(keypairToU8a(keypair), chainCode) + ); +} diff --git a/packages/util-crypto/src/schnorrkel/index.ts b/packages/util-crypto/src/schnorrkel/index.ts index 1f5c5f261..8c4e6ca04 100644 --- a/packages/util-crypto/src/schnorrkel/index.ts +++ b/packages/util-crypto/src/schnorrkel/index.ts @@ -2,10 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -export { default as schnorrkelDerivePrivate } from './derivePrivate'; -export { default as schnorrkelDerivePublic } from './derivePublic'; +import './polyfill'; + +export { default as schnorrkelDeriveHard } from './deriveHard'; +export { default as schnorrkelDeriveSoft } from './deriveSoft'; export { default as schnorrkelKeypairFromSeed } from './keypair/fromSeed'; export { schnorrkelIsReady, default as schnorrkelWaitReady } from './ready'; -export { default as schnorrkelSecretFromSeed } from './secretFromSeed'; export { default as schnorrkelSign } from './sign'; export { default as schnorrkelVerify } from './verify'; diff --git a/packages/util-crypto/src/schnorrkel/keypair/fromSeed.spec.ts b/packages/util-crypto/src/schnorrkel/keypair/fromSeed.spec.ts index 9823c7635..b569901d9 100644 --- a/packages/util-crypto/src/schnorrkel/keypair/fromSeed.spec.ts +++ b/packages/util-crypto/src/schnorrkel/keypair/fromSeed.spec.ts @@ -2,8 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import '../test-polyfill'; - import { stringToU8a, u8aToHex } from '@polkadot/util'; import { mnemonicToMiniSecret } from '../../mnemonic'; diff --git a/packages/util-crypto/src/schnorrkel/keypair/fromSeed.ts b/packages/util-crypto/src/schnorrkel/keypair/fromSeed.ts index 32e5195be..398e9afbf 100644 --- a/packages/util-crypto/src/schnorrkel/keypair/fromSeed.ts +++ b/packages/util-crypto/src/schnorrkel/keypair/fromSeed.ts @@ -4,20 +4,18 @@ import { Keypair } from '../../types'; +import '../polyfill'; + import schnorrkel from '@polkadot/schnorrkel-js'; -const SEC_LEN = 64; -const PUB_LEN = 32; +import keypairFromU8a from './fromU8a'; /** * @name schnorrkelKeypairFromSeed * @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed. */ export default function schnorrkelKeypairFromSeed (seed: Uint8Array): Keypair { - const full = schnorrkel.keypairFromSeed(seed); - - return { - publicKey: full.slice(SEC_LEN, SEC_LEN + PUB_LEN), - secretKey: full.slice(0, SEC_LEN) - }; + return keypairFromU8a( + schnorrkel.keypairFromSeed(seed) + ); } diff --git a/packages/util-crypto/src/schnorrkel/keypair/fromU8a.ts b/packages/util-crypto/src/schnorrkel/keypair/fromU8a.ts new file mode 100644 index 000000000..bdf4de5bc --- /dev/null +++ b/packages/util-crypto/src/schnorrkel/keypair/fromU8a.ts @@ -0,0 +1,15 @@ +// Copyright 2017-2019 @polkadot/util-crypto 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 { Keypair } from '../../types'; + +const SEC_LEN = 64; +const PUB_LEN = 32; + +export default function schnorrkelKeypairFromU8a (full: Uint8Array): Keypair { + return { + publicKey: full.slice(SEC_LEN, SEC_LEN + PUB_LEN), + secretKey: full.slice(0, SEC_LEN) + }; +} diff --git a/packages/util-crypto/src/schnorrkel/keypair/toU8a.ts b/packages/util-crypto/src/schnorrkel/keypair/toU8a.ts new file mode 100644 index 000000000..9a20a2e63 --- /dev/null +++ b/packages/util-crypto/src/schnorrkel/keypair/toU8a.ts @@ -0,0 +1,11 @@ +// Copyright 2017-2019 @polkadot/util-crypto 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 { Keypair } from '../../types'; + +import { u8aConcat } from '@polkadot/util'; + +export default function schnorrkelKeypairToU8a ({ publicKey, secretKey }: Keypair): Uint8Array { + return u8aConcat(secretKey, publicKey).slice(); +} diff --git a/packages/util-crypto/src/schnorrkel/polyfill.ts b/packages/util-crypto/src/schnorrkel/polyfill.ts new file mode 100644 index 000000000..5af7775f6 --- /dev/null +++ b/packages/util-crypto/src/schnorrkel/polyfill.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2019 @polkadot/util-crypto 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 '@polkadot/schnorrkel-js/crypto-polyfill'; diff --git a/packages/util-crypto/src/schnorrkel/ready.spec.ts b/packages/util-crypto/src/schnorrkel/ready.spec.ts index c1035c514..05761db7d 100644 --- a/packages/util-crypto/src/schnorrkel/ready.spec.ts +++ b/packages/util-crypto/src/schnorrkel/ready.spec.ts @@ -2,8 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import './test-polyfill'; - describe('ready', () => { let globalWA: typeof WebAssembly; diff --git a/packages/util-crypto/src/schnorrkel/secretFromSeed.ts b/packages/util-crypto/src/schnorrkel/secretFromSeed.ts deleted file mode 100644 index a415ec5d7..000000000 --- a/packages/util-crypto/src/schnorrkel/secretFromSeed.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2019 @polkadot/util-crypto 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 { assert } from '@polkadot/util'; - -import schnorrkel from '@polkadot/schnorrkel-js'; - -/** - * @name schnorrkelSecretFromSeed - * @description Returns a full 64-byte secretKey from the seed - */ -export default function schnorrkelSecretFromSeed (seed: Uint8Array): Uint8Array { - assert(seed && seed.length === 32, 'Expected valid seed, 32-bytes'); - - return schnorrkel.secretFromSeed(seed); -} diff --git a/packages/util-crypto/src/schnorrkel/sign.ts b/packages/util-crypto/src/schnorrkel/sign.ts index 894d8c821..a3f40fbcd 100644 --- a/packages/util-crypto/src/schnorrkel/sign.ts +++ b/packages/util-crypto/src/schnorrkel/sign.ts @@ -4,9 +4,10 @@ import { Keypair } from '../types'; -import { assert } from '@polkadot/util'; +import './polyfill'; import schnorrkel from '@polkadot/schnorrkel-js'; +import { assert } from '@polkadot/util'; /** * @name schnorrkelSign diff --git a/packages/util-crypto/src/schnorrkel/signVerify.spec.ts b/packages/util-crypto/src/schnorrkel/signVerify.spec.ts index 4c30f8abb..de97be069 100644 --- a/packages/util-crypto/src/schnorrkel/signVerify.spec.ts +++ b/packages/util-crypto/src/schnorrkel/signVerify.spec.ts @@ -2,8 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import './test-polyfill'; - import { stringToU8a } from '@polkadot/util'; import randomAsU8a from '../random/asU8a'; diff --git a/packages/util-crypto/src/schnorrkel/test-polyfill.ts b/packages/util-crypto/src/schnorrkel/test-polyfill.ts deleted file mode 100644 index 71b7e5e7d..000000000 --- a/packages/util-crypto/src/schnorrkel/test-polyfill.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2019 @polkadot/util-crypto authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -if (process.env.NODE_ENV === 'test') { - const crypto = require('crypto'); - - Object.defineProperty((global as any).self, 'crypto', { - value: { - getRandomValues: (arr: Array) => crypto.randomBytes(arr.length) - } - }); -} diff --git a/packages/util-crypto/src/schnorrkel/verify.ts b/packages/util-crypto/src/schnorrkel/verify.ts index dba4e1322..8cb10ef1d 100644 --- a/packages/util-crypto/src/schnorrkel/verify.ts +++ b/packages/util-crypto/src/schnorrkel/verify.ts @@ -2,6 +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 './polyfill'; import schnorrkel from '@polkadot/schnorrkel-js'; /** diff --git a/yarn.lock b/yarn.lock index 5421cbe11..6b5aa75e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1393,10 +1393,10 @@ universal-user-agent "^2.0.0" url-template "^2.0.8" -"@polkadot/dev@^0.26.1": - version "0.26.1" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.26.1.tgz#609bd1f43860a7172e45be76a529af24c598f546" - integrity sha512-m1BBk8QlQRGxFouwWNdK4kDHPUQEIbSCAB3VUiHstFeR+zto5II7zytX7wKHRH8Bn7U5CiW3PWpJ85MR4oodDA== +"@polkadot/dev@^0.27.1": + version "0.27.1" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.27.1.tgz#63ef2439bac8131d368ba80bf7dc8d09c0e2e927" + integrity sha512-EN60509R89FNJsoNWH87G3buCd717Mx3wrexLLkd+IURl52shFjv12HqpSdW09mMwxhUZMOmmqwYoFdRnkOFag== dependencies: "@babel/cli" "^7.2.3" "@babel/core" "^7.3.4" @@ -1429,10 +1429,10 @@ typescript "^3.3.3333" vuepress "^1.0.0-alpha.40" -"@polkadot/schnorrkel-js@^0.2.0-3": - version "0.2.0-3" - resolved "https://registry.yarnpkg.com/@polkadot/schnorrkel-js/-/schnorrkel-js-0.2.0-3.tgz#17b8f49f6c3b60dc2f64dc98d664447881d5a100" - integrity sha512-D+ITK9rfYLWnrDtvFJSXUujmWbxIUeyjy75QY/Vf9V2lDRO1WhF6ttW6H2NRvlPxxY6Qc5jebLYBZ7Put7AIpA== +"@polkadot/schnorrkel-js@^0.2.0-12": + version "0.2.0-12" + resolved "https://registry.yarnpkg.com/@polkadot/schnorrkel-js/-/schnorrkel-js-0.2.0-12.tgz#fd70f3b79d2c7a97874c26ae41bf331ef8763b77" + integrity sha512-S/tbdME58VDzP5nXE4kWseZnrWGkaMFFmEdW43KlwGlWvxNMREpgncUdDfTFip6lDahcKNSU98n+QJJlkaSB+A== "@polkadot/ts@^0.1.56": version "0.1.56"