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
+3 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-crypto",
"version": "0.33.36",
"version": "0.34.0",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -29,8 +29,9 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36",
"@polkadot/util": "^0.34.0",
"@types/bip39": "^2.4.1",
"@types/webassembly-js-api": "^0.0.2",
"bip39": "^2.5.0",
"blakejs": "^1.1.0",
"js-sha3": "^0.8.0",
+1
View File
@@ -7,5 +7,6 @@ export * from './keccak';
export * from './mnemonic';
export * from './nacl';
export * from './random';
export * from './schnorrkel';
export * from './sha512';
export * from './xxhash';
@@ -2,10 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../../types';
import { naclKeypairFromRandom } from '../index';
describe('naclKeypairFromRandom', () => {
let keypair;
let keypair: KeypairType;
beforeEach(() => {
keypair = naclKeypairFromRandom();
@@ -7,7 +7,7 @@ import { stringToU8a } from '@polkadot/util/index';
import { naclSign, naclKeypairFromSeed } from './index';
describe('naclSign', () => {
let secretKey;
let secretKey: Uint8Array;
beforeEach(() => {
secretKey = naclKeypairFromSeed(
@@ -19,7 +19,7 @@ describe('naclSign', () => {
expect(
naclSign(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
secretKey
{ secretKey }
)
).toEqual(
new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7])
+7 -2
View File
@@ -2,7 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../types';
import nacl from 'tweetnacl';
import { assert } from '@polkadot/util/index';
/**
* @name naclSign
@@ -19,6 +22,8 @@ import nacl from 'tweetnacl';
* naclSign([...], [...]); // => [...]
* ```
*/
export default function naclSign (message: Uint8Array, secretKey: Uint8Array): Uint8Array {
return nacl.sign.detached(message, secretKey);
export default function naclSign (message: Uint8Array, { secretKey }: Partial<KeypairType>): Uint8Array {
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64 bytes');
return nacl.sign.detached(message, secretKey as Uint8Array);
}
@@ -7,8 +7,8 @@ import { stringToU8a } from '@polkadot/util/index';
import { naclVerify, naclKeypairFromSeed } from './index';
describe('naclSign', () => {
let publicKey;
let signature;
let publicKey: Uint8Array;
let signature: Uint8Array;
beforeEach(() => {
publicKey = naclKeypairFromSeed(
@@ -0,0 +1,8 @@
// 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.
export { default as schnorrkelKeypairFromRandom } from './keypair/fromRandom';
export { default as schnorrkelKeypairFromSeed } from './keypair/fromSeed';
export { default as schnorrkelSign } from './sign';
export { default as schnorrkelVerify } from './verify';
@@ -0,0 +1,19 @@
// 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 { schnorrkelKeypairFromRandom } from '../index';
describe('schnorrkelKeypairFromRandom', () => {
it('generates a valid publicKey', () => {
expect(
schnorrkelKeypairFromRandom().publicKey
).toHaveLength(32);
});
it('generates a valid secretKey', () => {
expect(
schnorrkelKeypairFromRandom().secretKey
).toHaveLength(64);
});
});
@@ -0,0 +1,16 @@
// 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 { KeypairType } from '../../types';
import randomAsU8a from '../../random/asU8a';
import fromSeed from './fromSeed';
/**
* @name schnorrkelKeypairFromRandom
* @description Returns a new generate object containing a `publicKey` & `secretKey`.
*/
export default function schnorrkelKeypairFromRandom (): KeypairType {
return fromSeed(randomAsU8a(32));
}
@@ -0,0 +1,21 @@
// 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 { stringToU8a } from '@polkadot/util/index';
import { schnorrkelKeypairFromSeed } from '../index';
describe('schnorrkelKeypairFromSeed', () => {
const TEST = stringToU8a('12345678901234567890123456789012');
const RESULT = {
publicKey: new Uint8Array([116, 28, 8, 160, 111, 65, 197, 150, 96, 143, 103, 116, 37, 155, 217, 4, 51, 4, 173, 250, 93, 62, 234, 98, 118, 11, 217, 190, 151, 99, 77, 99]),
secretKey: new Uint8Array([240, 16, 102, 96, 195, 221, 162, 63, 22, 218, 169, 172, 91, 129, 27, 150, 48, 119, 245, 188, 10, 248, 159, 133, 128, 79, 13, 232, 228, 36, 240, 80, 249, 141, 102, 243, 148, 66, 80, 111, 249, 71, 253, 145, 31, 24, 199, 167, 165, 218, 99, 154, 99, 232, 211, 180, 226, 51, 247, 65, 67, 217, 81, 193])
};
it('generates a valid publicKey/secretKey pair (u8a)', () => {
expect(
schnorrkelKeypairFromSeed(TEST)
).toEqual(RESULT);
});
});
@@ -0,0 +1,23 @@
// 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 { KeypairType } from '../../types';
import * as schnorrkel from '../schnorrkel-js';
const SEC_LEN = 64;
const PUB_LEN = 32;
/**
* @name schnorrkelKeypairFromSeed
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
*/
export default function schnorrkelKeypairFromSeed (seed: Uint8Array): KeypairType {
const full = schnorrkel.keypair_from_seed(seed);
return {
publicKey: full.slice(SEC_LEN, SEC_LEN + PUB_LEN),
secretKey: full.slice(0, SEC_LEN)
};
}
@@ -0,0 +1,5 @@
# schnorrkel-js
These are build outputs from https://github.com/paritytech/schnorrkel-js
To be removed once the above is published with Node/Browser/React-Native compatible versions
@@ -0,0 +1,3 @@
import './polyfill';
export * from './schnorrkel_js';
@@ -0,0 +1,13 @@
// 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)
}
});
}
@@ -0,0 +1,5 @@
/* tslint:disable */
export function sign(arg0: Uint8Array, arg1: Uint8Array, arg2: Uint8Array): Uint8Array;
export function verify(arg0: Uint8Array, arg1: Uint8Array, arg2: Uint8Array): boolean;
export function secret_from_seed(arg0: Uint8Array): Uint8Array;
export function keypair_from_seed(arg0: Uint8Array): Uint8Array;
@@ -0,0 +1,256 @@
/* tslint:disable */
var wasm;
let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm(arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8Memory().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function getArrayU8FromWasm(ptr, len) {
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
}
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}
return cachedGlobalArgumentPtr;
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
/**
* Sign a message
*
* The combination of both public and private key must be provided.
* This is effectively equivalent to a keypair.
*
* * public: UIntArray with 32 element
* * private: UIntArray with 64 element
* * message: Arbitrary length UIntArray
*
* * returned vector is the signature consisting of 64 bytes.
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @param {Uint8Array} arg2
* @returns {Uint8Array}
*/
module.exports.sign = function(arg0, arg1, arg2) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm(arg1);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm(arg2);
const len2 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.sign(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
wasm.__wbindgen_free(ptr2, len2 * 1);
}
};
/**
* Verify a message and its corresponding against a public key;
*
* * signature: UIntArray with 64 element
* * message: Arbitrary length UIntArray
* * pubkey: UIntArray with 32 element
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @param {Uint8Array} arg2
* @returns {boolean}
*/
module.exports.verify = function(arg0, arg1, arg2) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm(arg1);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm(arg2);
const len2 = WASM_VECTOR_LEN;
try {
return (wasm.verify(ptr0, len0, ptr1, len1, ptr2, len2)) !== 0;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
wasm.__wbindgen_free(ptr2, len2 * 1);
}
};
/**
* Generate a secret key (aka. private key) from a seed phrase.
*
* * seed: UIntArray with 32 element
*
* returned vector is the private key consisting of 64 bytes.
* @param {Uint8Array} arg0
* @returns {Uint8Array}
*/
module.exports.secret_from_seed = function(arg0) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.secret_from_seed(retptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
};
/**
* Generate a key pair. .
*
* * seed: UIntArray with 32 element
*
* returned vector is the concatenation of first the private key (64 bytes)
* followed by the public key (32) bytes.
* @param {Uint8Array} arg0
* @returns {Uint8Array}
*/
module.exports.keypair_from_seed = function(arg0) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.keypair_from_seed(retptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
};
const TextDecoder = require('util').TextDecoder;
let cachedTextDecoder = new TextDecoder('utf-8');
function getStringFromWasm(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}
const heap = new Array(32);
heap.fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
module.exports.__wbg_new_886f15c1b20b061b = function(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
return addHeapObject(new Function(varg0));
};
function getObject(idx) { return heap[idx]; }
module.exports.__wbg_call_a2b503e0ee1234e4 = function(arg0, arg1) {
return addHeapObject(getObject(arg0).call(getObject(arg1)));
};
module.exports.__wbg_self_ddd2d80076091e5f = function(arg0) {
return addHeapObject(getObject(arg0).self);
};
module.exports.__wbg_crypto_4b7669ff1793d881 = function(arg0) {
return addHeapObject(getObject(arg0).crypto);
};
module.exports.__wbg_getRandomValues_6de85818bd2ad699 = function(arg0) {
return addHeapObject(getObject(arg0).getRandomValues);
};
module.exports.__wbg_getRandomValues_95cef5eed1acafda = function(arg0, arg1, arg2) {
let varg1 = getArrayU8FromWasm(arg1, arg2);
getObject(arg0).getRandomValues(varg1);
};
module.exports.__wbg_require_86edd37cfda5f13d = function(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
return addHeapObject(require(varg0));
};
module.exports.__wbg_randomFillSync_571502126f344d60 = function(arg0, arg1, arg2) {
let varg1 = getArrayU8FromWasm(arg1, arg2);
getObject(arg0).randomFillSync(varg1);
};
function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}
module.exports.__wbindgen_object_drop_ref = function(i) { dropObject(i); };
module.exports.__wbindgen_is_undefined = function(idx) {
return getObject(idx) === undefined ? 1 : 0;
};
module.exports.__wbindgen_jsval_eq = function(a, b) {
return getObject(a) === getObject(b) ? 1 : 0;
};
wasm = require('./schnorrkel_js_bg');
@@ -0,0 +1,9 @@
/* tslint:disable */
export const memory: WebAssembly.Memory;
export function sign(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
export function verify(a: number, b: number, c: number, d: number, e: number, f: number): number;
export function secret_from_seed(a: number, b: number, c: number): void;
export function keypair_from_seed(a: number, b: number, c: number): void;
export function __wbindgen_global_argument_ptr(): number;
export function __wbindgen_malloc(a: number): number;
export function __wbindgen_free(a: number, b: number): void;
File diff suppressed because one or more lines are too long
@@ -0,0 +1,20 @@
// 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 { KeypairType } from '../types';
import { assert } from '@polkadot/util/index';
import * as schnorrkel from './schnorrkel-js';
/**
* @name schnorrkelSign
* @description Returns message signature of `message`, using the supplied pair
*/
export default function schnorrkelSign (message: Uint8Array, { publicKey, secretKey }: Partial<KeypairType>): Uint8Array {
assert(publicKey && publicKey.length === 32, 'Expected valid publicKey, 32-bytes');
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 364-bytes');
return schnorrkel.sign(publicKey as Uint8Array, secretKey as Uint8Array, message);
}
@@ -0,0 +1,26 @@
// 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 { stringToU8a } from '@polkadot/util/index';
import pairFromRandom from './keypair/fromRandom';
import sign from './sign';
import verify from './verify';
const MESSAGE = stringToU8a('this is a message');
describe('sign and verify', () => {
it('has 64-byte signatures', () => {
const pair = pairFromRandom();
expect(sign(MESSAGE, pair)).toHaveLength(64);
});
it('can sign and verify a message', () => {
const pair = pairFromRandom();
const signature = sign(MESSAGE, pair);
expect(verify(MESSAGE, signature, pair.publicKey)).toBe(true);
});
});
@@ -0,0 +1,13 @@
// 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 * as schnorrkel from './schnorrkel-js';
/**
* @name schnorrkelVerify
* @description Verifies the signature of `message`, using the supplied pair
*/
export default function schnorrkelVerify (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {
return schnorrkel.verify(signature, message, publicKey);
}
+4 -3
View File
@@ -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 { SignKeyPair } from 'tweetnacl';
export type KeypairType = SignKeyPair;
export type KeypairType = {
publicKey: Uint8Array;
secretKey: Uint8Array;
};