Hard and soft derivation aligned with substrate master (#369)

* I compiles... ship it!?

* Re-enable derivation from path tests

* Update schnorrkel-js (latest w3f version)

* Swap default keyring to sr25519
This commit is contained in:
Jaco Greeff
2019-03-16 11:34:31 +01:00
committed by GitHub
parent 41758416e8
commit 5dc8e87797
38 changed files with 194 additions and 203 deletions
+1 -1
View File
@@ -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"
}
-2
View File
@@ -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';
+6 -21
View File
@@ -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));
}
/**
+15 -8
View File
@@ -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
};
}
+7 -5
View File
@@ -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
};
+3 -3
View File
@@ -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);
+3 -3
View File
@@ -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
);
+12 -11
View File
@@ -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)
};
+4 -4
View File
@@ -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'
}
});
});
+1 -1
View File
@@ -22,7 +22,7 @@ export default function toJson (type: KeypairType, { publicKey, meta }: PairStat
type: isEncrypted
? 'xsalsa20-poly1305'
: 'none',
version: '1'
version: '2'
},
meta
};
+1
View File
@@ -4,5 +4,6 @@
export type PairInfo = {
publicKey: Uint8Array,
secretKey?: Uint8Array,
seed?: Uint8Array | null
};
+17 -15
View File
@@ -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();
});
+4 -2
View File
@@ -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
+5 -4
View File
@@ -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');
});
});
+1 -3
View File
@@ -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<KeyringPair>;
readonly publicKeys: Array<Uint8Array>;
readonly type: KeypairType;
+1 -1
View File
@@ -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",
+9 -7
View File
@@ -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<DeriveJunction>, 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<DeriveJunction>, type: KeypairType): Keypair {
const isEd25519 = type === 'ed25519';
return path.reduce((pair, junction) => {
return isEd25519
? keyHdkdEd15519(pair, junction)
: keyHdkdSr15519(pair, junction);
}, pair);
}
+6 -9
View File
@@ -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)
);
}
+7 -20
View File
@@ -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);
}
@@ -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)
);
}
+1 -1
View File
@@ -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';
@@ -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)
);
}
@@ -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)
);
}
@@ -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);
}
@@ -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)
);
}
+4 -3
View File
@@ -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';
@@ -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';
@@ -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)
);
}
@@ -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)
};
}
@@ -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();
}
@@ -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';
@@ -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;
@@ -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);
}
+2 -1
View File
@@ -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
@@ -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';
@@ -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<number>) => crypto.randomBytes(arr.length)
}
});
}
@@ -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';
/**
+8 -8
View File
@@ -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"