Swap crypto to WASM (JS as fallback) (#378)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
- Add additional derivation path extraction mechanisms to util-crypto
|
||||
- Add createFromUri function to keypair (creates, does not add it)
|
||||
- Allow all crypto hashing & ed25519 to use WASM (as available, JS fallbacks)
|
||||
|
||||
# 0.39.1
|
||||
|
||||
|
||||
@@ -30,8 +30,9 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.4.0",
|
||||
"@polkadot/util": "^0.40.0-beta.1",
|
||||
"@polkadot/wasm-crypto": "^0.2.1",
|
||||
"@polkadot/wasm-schnorrkel": "^0.2.1",
|
||||
"@polkadot/wasm-crypto": "^0.3.0-beta.0",
|
||||
"@polkadot/wasm-dalek-ed25519": "^0.3.0-beta.0",
|
||||
"@polkadot/wasm-schnorrkel": "^0.3.0-beta.0",
|
||||
"@types/bip39": "^2.4.2",
|
||||
"@types/pbkdf2": "^3.0.0",
|
||||
"@types/secp256k1": "^3.5.0",
|
||||
|
||||
@@ -2,11 +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 hexToU8a from '@polkadot/util/hex/toU8a';
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { blake2AsHex } from '.';
|
||||
|
||||
describe('blake2AsHex', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a 64-bit value (specified)', () => {
|
||||
expect(
|
||||
blake2AsHex('abc', 64)
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import blake2bAsHex from './blake2b/asHex';
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import blake2AsU8a from './asU8a';
|
||||
|
||||
/**
|
||||
* @name blake2AsHex
|
||||
@@ -19,5 +21,7 @@ import blake2bAsHex from './blake2b/asHex';
|
||||
* ```
|
||||
*/
|
||||
export default function blake2AsHex (data: Uint8Array | string, bitLength: number = 256): string {
|
||||
return blake2bAsHex(data, bitLength);
|
||||
return u8aToHex(
|
||||
blake2AsU8a(data, bitLength)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,11 +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 hexToU8a from '@polkadot/util/hex/toU8a';
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { blake2AsU8a } from '.';
|
||||
|
||||
describe('blake2AsU8a', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a 64-bit value by default', () => {
|
||||
expect(
|
||||
blake2AsU8a('abc')
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import blake2bAsU8a from './blake2b/asU8a';
|
||||
import blakejs from 'blakejs';
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { blake2b, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name blake2AsU8a
|
||||
@@ -18,6 +20,10 @@ import blake2bAsU8a from './blake2b/asU8a';
|
||||
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa53, 0xf98, 0x1c, 0x4d, 0x0d]
|
||||
* ```
|
||||
*/
|
||||
export default function blake2AsU8a (data: Uint8Array | string, bitLength: number = 256, key?: Uint8Array): Uint8Array {
|
||||
return blake2bAsU8a(data, bitLength, key);
|
||||
export default function blake2AsU8a (data: Uint8Array | string, bitLength: number = 256, key: Uint8Array | null = null): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
return isReady()
|
||||
? blake2b(u8aToU8a(data), u8aToU8a(key), byteLength)
|
||||
: blakejs.blake2b(data, key, byteLength);
|
||||
}
|
||||
|
||||
@@ -1,23 +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 hexToU8a from '@polkadot/util/hex/toU8a';
|
||||
|
||||
import { blake2bAsHex } from '.';
|
||||
|
||||
describe('blake2bAsHex', () => {
|
||||
it('creates the correct hash', () => {
|
||||
expect(
|
||||
blake2bAsHex('abc')
|
||||
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923');
|
||||
});
|
||||
|
||||
it('matches the Rust implementation', () => {
|
||||
expect(
|
||||
blake2bAsHex(
|
||||
hexToU8a('0x454545454545454545454545454545454545454545454545454545454545454501000000000000002481853da20b9f4322f34650fea5f240dcbfb266d02db94bfa0153c31f4a29dbdbf025dd4a69a6f4ee6e1577b251b655097e298b692cb34c18d3182cac3de0dc00000000'), 256
|
||||
)
|
||||
).toEqual('0x1025e5db74fdaf4d2818822dccf0e1604ae9ccc62f26cecfde23448ff0248abf');
|
||||
});
|
||||
});
|
||||
@@ -1,27 +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 { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import blake2bAsU8a from './asU8a';
|
||||
|
||||
/**
|
||||
* @name blake2bAsHex
|
||||
* @summary Creates a blake2b hex string from the input.
|
||||
* @description
|
||||
* From a `Uint8Array` input, create the blake2b and return the result as a hex string.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { blake2bAsHex } from '@polkadot/util-crypto';
|
||||
*
|
||||
* blake2bAsHex('abc'); // => '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923'
|
||||
* ```
|
||||
*/
|
||||
export default function blake2bAsHex (data: Uint8Array | string, bitLength: number = 512): string {
|
||||
return u8aToHex(
|
||||
blake2bAsU8a(data, bitLength)
|
||||
);
|
||||
}
|
||||
@@ -1,21 +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 { blake2bAsU8a } from '.';
|
||||
|
||||
describe('blake2bAsU8a', () => {
|
||||
it('creates the correct hash (default length)', () => {
|
||||
expect(
|
||||
blake2bAsU8a('abc')
|
||||
).toEqual(new Uint8Array([186, 128, 165, 63, 152, 28, 77, 13, 106, 39, 151, 182, 159, 18, 246, 233, 76, 33, 47, 20, 104, 90, 196, 183, 75, 18, 187, 111, 219, 255, 162, 209, 125, 135, 197, 57, 42, 171, 121, 45, 194, 82, 213, 222, 69, 51, 204, 149, 24, 211, 138, 168, 219, 241, 146, 90, 185, 35, 134, 237, 212, 0, 153, 35]));
|
||||
});
|
||||
|
||||
it('creates the correct hash', () => {
|
||||
expect(
|
||||
blake2bAsU8a('abc', 256)
|
||||
).toEqual(
|
||||
new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,24 +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.
|
||||
|
||||
// TODO: For Node we can also use node-blake2 (npm blake2)
|
||||
import blakejs from 'blakejs';
|
||||
|
||||
/**
|
||||
* @name blake2bAsU8a
|
||||
* @summary Creates a blake2b Uint8Array from the input.
|
||||
* @description
|
||||
* From a `Uint8Array` input, create the blake2b and return the result as a `Uint8Array`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { blake2bAsU8a } from '@polkadot/util-crypto';
|
||||
*
|
||||
* blake2bAsU8a('abc'); // => Uint8Array('508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982')
|
||||
* ```
|
||||
*/
|
||||
export default function blake2bAsU8a (data: Uint8Array | string, bitLength: number = 512, key: Uint8Array | null = null): Uint8Array {
|
||||
return blakejs.blake2b(data, key, bitLength / 8);
|
||||
}
|
||||
@@ -1,14 +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 blake2bAsHex from './asHex';
|
||||
import blake2bAsU8a from './asU8a';
|
||||
|
||||
/**
|
||||
* @summary Create [Blake2b](https://blake2.net/) values as hex & Uint8Array output
|
||||
*/
|
||||
export {
|
||||
blake2bAsHex,
|
||||
blake2bAsU8a
|
||||
};
|
||||
@@ -4,8 +4,9 @@
|
||||
|
||||
import './polyfill';
|
||||
|
||||
import { wasmWaitReady } from './ready';
|
||||
import { schnorrkelWaitReady } from './schnorrkel';
|
||||
import wasmCrypto from '@polkadot/wasm-crypto';
|
||||
import wasmEd25519 from '@polkadot/wasm-dalek-ed25519';
|
||||
import wasmSr25519 from '@polkadot/wasm-schnorrkel';
|
||||
|
||||
export * from './blake2';
|
||||
export * from './keccak';
|
||||
@@ -23,8 +24,9 @@ export function cryptoWaitReady (): Promise<boolean> {
|
||||
// prepare for multiples, easy to adapt without thinking
|
||||
return Promise
|
||||
.all([
|
||||
wasmWaitReady(),
|
||||
schnorrkelWaitReady()
|
||||
wasmCrypto.waitReady(),
|
||||
wasmEd25519.waitReady(),
|
||||
wasmSr25519.waitReady()
|
||||
])
|
||||
.then(() => true)
|
||||
.catch((error) => {
|
||||
|
||||
@@ -2,12 +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 { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { keccakAsHex } from '.';
|
||||
|
||||
const value = 'test value';
|
||||
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
|
||||
|
||||
describe('keccakAsHex', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsHex(value)
|
||||
|
||||
@@ -3,10 +3,15 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { keccakAsU8a } from '.';
|
||||
|
||||
describe('keccakAsU8a', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
const input = 'test value';
|
||||
const output = hexToU8a(
|
||||
'0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { keccak256 } from 'js-sha3';
|
||||
import jssha3 from 'js-sha3';
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, keccak256 } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name keccakAsU8a
|
||||
@@ -19,7 +21,9 @@ import { keccak256 } from 'js-sha3';
|
||||
* ```
|
||||
*/
|
||||
export default function keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array {
|
||||
return new Uint8Array(
|
||||
keccak256.update(value).arrayBuffer()
|
||||
);
|
||||
return isReady()
|
||||
? keccak256(u8aToU8a(value))
|
||||
: new Uint8Array(
|
||||
jssha3.keccak256.update(value).arrayBuffer()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import ed25519 from '@polkadot/wasm-dalek-ed25519';
|
||||
|
||||
import { naclKeypairFromSeed } from '..';
|
||||
|
||||
@@ -26,6 +27,10 @@ describe('naclKeypairFromSeed', () => {
|
||||
])
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
await ed25519.waitReady();
|
||||
});
|
||||
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', () => {
|
||||
expect(
|
||||
naclKeypairFromSeed(TEST)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import { Keypair } from '../../types';
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
import { isReady, keypairFromSeed } from '@polkadot/wasm-dalek-ed25519';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromSeed
|
||||
@@ -21,5 +22,14 @@ import nacl from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export default function naclKeypairFromSeed (seed: Uint8Array): Keypair {
|
||||
if (isReady()) {
|
||||
const full = keypairFromSeed(seed);
|
||||
|
||||
return {
|
||||
publicKey: full.slice(32),
|
||||
secretKey: full.slice(0, 64)
|
||||
};
|
||||
}
|
||||
|
||||
return nacl.sign.keyPair.fromSeed(seed);
|
||||
}
|
||||
|
||||
@@ -3,23 +3,22 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import ed25519 from '@polkadot/wasm-dalek-ed25519';
|
||||
|
||||
import { naclSign, naclKeypairFromSeed } from '.';
|
||||
|
||||
describe('naclSign', () => {
|
||||
let secretKey: Uint8Array;
|
||||
|
||||
beforeEach(() => {
|
||||
secretKey = naclKeypairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
).secretKey;
|
||||
beforeEach(async () => {
|
||||
await ed25519.waitReady();
|
||||
});
|
||||
|
||||
it('returns a valid signature for the message', () => {
|
||||
expect(
|
||||
naclSign(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
{ secretKey }
|
||||
naclKeypairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
)
|
||||
)
|
||||
).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])
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Keypair } from '../types';
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
import { assert } from '@polkadot/util';
|
||||
import { isReady, sign } from '@polkadot/wasm-dalek-ed25519';
|
||||
|
||||
/**
|
||||
* @name naclSign
|
||||
@@ -21,8 +22,10 @@ import { assert } from '@polkadot/util';
|
||||
* naclSign([...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export default function naclSign (message: Uint8Array, { secretKey }: Partial<Keypair>): Uint8Array {
|
||||
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64 bytes');
|
||||
export default function naclSign (message: Uint8Array, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
assert(secretKey, 'Expected valid secretKey');
|
||||
|
||||
return nacl.sign.detached(message, secretKey as Uint8Array);
|
||||
return isReady()
|
||||
? sign(publicKey as Uint8Array, (secretKey as Uint8Array).subarray(0, 32), message)
|
||||
: nacl.sign.detached(message, secretKey as Uint8Array);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import ed25519 from '@polkadot/wasm-dalek-ed25519';
|
||||
|
||||
import { naclVerify, naclKeypairFromSeed } from '.';
|
||||
|
||||
@@ -10,7 +11,9 @@ describe('naclSign', () => {
|
||||
let publicKey: Uint8Array;
|
||||
let signature: Uint8Array;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
await ed25519.waitReady();
|
||||
|
||||
publicKey = naclKeypairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
).publicKey;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
import { isReady, verify } from '@polkadot/wasm-dalek-ed25519';
|
||||
|
||||
/**
|
||||
* @name naclSign
|
||||
@@ -19,5 +20,7 @@ import nacl from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export default function naclVerify (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {
|
||||
return nacl.sign.detached.verify(message, signature, publicKey);
|
||||
return isReady()
|
||||
? verify(signature, message, publicKey)
|
||||
: nacl.sign.detached.verify(message, signature, publicKey);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
import { isReady, waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
export function wasmWaitReady (): Promise<boolean> {
|
||||
return waitReady();
|
||||
}
|
||||
|
||||
export function wasmIsReady (): boolean {
|
||||
return isReady();
|
||||
}
|
||||
@@ -7,6 +7,5 @@ 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 schnorrkelSign } from './sign';
|
||||
export { default as schnorrkelVerify } from './verify';
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
import schnorrkel from '@polkadot/wasm-schnorrkel';
|
||||
|
||||
import { mnemonicToMiniSecret } from '../../mnemonic';
|
||||
import { schnorrkelKeypairFromSeed, schnorrkelWaitReady } from '..';
|
||||
import { schnorrkelKeypairFromSeed } from '..';
|
||||
import tests from './testing';
|
||||
|
||||
describe('schnorrkelKeypairFromSeed', () => {
|
||||
@@ -16,7 +17,7 @@ describe('schnorrkelKeypairFromSeed', () => {
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
return schnorrkelWaitReady();
|
||||
return schnorrkel.waitReady();
|
||||
});
|
||||
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', () => {
|
||||
|
||||
@@ -1,25 +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.
|
||||
|
||||
describe('ready', () => {
|
||||
let globalWA: typeof WebAssembly;
|
||||
|
||||
beforeEach(() => {
|
||||
globalWA = (global as any).WebAssembly;
|
||||
(global as any).WebAssembly = null;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
(global as any).WebAssembly = globalWA;
|
||||
});
|
||||
|
||||
it('returns false when Webassembly is not available', async () => {
|
||||
(global as any).WebAssembly = null;
|
||||
|
||||
const schnorrkelWaitReady = require('./ready').default;
|
||||
const isReady = await schnorrkelWaitReady();
|
||||
|
||||
expect(isReady).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -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.
|
||||
|
||||
import schnorrkel from '@polkadot/wasm-schnorrkel';
|
||||
|
||||
export default function schnorrkelWaitReady (): Promise<boolean> {
|
||||
return schnorrkel.waitReady();
|
||||
}
|
||||
|
||||
export function schnorrkelIsReady (): boolean {
|
||||
return schnorrkel.isReady();
|
||||
}
|
||||
@@ -3,10 +3,10 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import schnorrkel from '@polkadot/wasm-schnorrkel';
|
||||
|
||||
import randomAsU8a from '../random/asU8a';
|
||||
import pairFromSeed from './keypair/fromSeed';
|
||||
import schnorrkelWaitReady from './ready';
|
||||
import sign from './sign';
|
||||
import verify from './verify';
|
||||
|
||||
@@ -14,7 +14,7 @@ const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
describe('sign and verify', () => {
|
||||
beforeEach(async () => {
|
||||
return schnorrkelWaitReady();
|
||||
return schnorrkel.waitReady();
|
||||
});
|
||||
|
||||
it('has 64-byte signatures', () => {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import schnorrkel from '@polkadot/wasm-schnorrkel';
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,17 +3,14 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import secp256k1 from 'secp256k1';
|
||||
import { bufferToU8a, u8aToBuffer } from '@polkadot/util';
|
||||
import { u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
/**
|
||||
* @name secp256k1Recover
|
||||
* @description Recovers a publicKey from the supplied signature
|
||||
*/
|
||||
export default function secp256k1Recover (_message: Uint8Array, _signature: Uint8Array, recovery: number): Uint8Array {
|
||||
const message = u8aToBuffer(_message);
|
||||
const signature = u8aToBuffer(_signature);
|
||||
|
||||
return bufferToU8a(
|
||||
secp256k1.recover(message, signature, recovery)
|
||||
export default function secp256k1Recover (message: Uint8Array, signature: Uint8Array, recovery: number): Uint8Array {
|
||||
return u8aToU8a(
|
||||
secp256k1.recover(u8aToBuffer(message), u8aToBuffer(signature), recovery)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,15 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { sha512AsU8a } from '.';
|
||||
|
||||
describe('sha512AsU8a', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('creates a sha-512 hash', () => {
|
||||
expect(
|
||||
sha512AsU8a(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
import { isReady, sha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name sha512AsU8a
|
||||
@@ -19,5 +20,7 @@ import nacl from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export default function sha512AsU8a (data: Uint8Array): Uint8Array {
|
||||
return nacl.hash(data);
|
||||
return isReady()
|
||||
? sha512(data)
|
||||
: nacl.hash(data);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,15 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { xxhashAsHex } from '.';
|
||||
|
||||
describe('xxhashAsHex', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a 64-bit value by default', () => {
|
||||
expect(
|
||||
xxhashAsHex('abc')
|
||||
|
||||
@@ -3,10 +3,15 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { xxhashAsU8a } from '.';
|
||||
|
||||
describe('xxhashAsU8a', () => {
|
||||
beforeEach(async () => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a 64-bit value by default', () => {
|
||||
expect(
|
||||
xxhashAsU8a('abc')
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, twox } from '@polkadot/wasm-crypto';
|
||||
|
||||
import xxhash64AsBn from './xxhash64/asBn';
|
||||
|
||||
/**
|
||||
@@ -19,9 +22,13 @@ import xxhash64AsBn from './xxhash64/asBn';
|
||||
* ```
|
||||
*/
|
||||
export default function xxhashAsU8a (data: Buffer | Uint8Array | string, bitLength: number = 64): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
const iterations = Math.ceil(bitLength / 64);
|
||||
const u8a = new Uint8Array(byteLength);
|
||||
|
||||
if (isReady()) {
|
||||
return twox(u8aToU8a(data), iterations);
|
||||
}
|
||||
|
||||
const u8a = new Uint8Array(Math.ceil(bitLength / 8));
|
||||
|
||||
for (let seed = 0; seed < iterations; seed++) {
|
||||
u8a.set(xxhash64AsBn(data, seed).toArray('le', 8), seed * 8);
|
||||
|
||||
@@ -11,9 +11,9 @@ import stringToU8a from '../string/toU8a';
|
||||
|
||||
/**
|
||||
* @name u8aToU8a
|
||||
* @summary Creates a Uint8Array value from a Uint8Array bignumber or hex input.
|
||||
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
|
||||
* @description
|
||||
* `null` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
|
||||
* `null` ior `undefined` nputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
@@ -26,7 +26,7 @@ import stringToU8a from '../string/toU8a';
|
||||
*/
|
||||
export default function u8aToU8a (value?: Array<number> | Buffer | Uint8Array | string | null): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array(0);
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
if (isBuffer(value)) {
|
||||
|
||||
@@ -1584,15 +1584,20 @@
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.56.tgz#ffd6e9c95704a7fb90b918193b9dc5c440114b27"
|
||||
integrity sha512-wnt4zXxZXyz6WaubTO/I+nUElwV2DogFzdl6CrKfVn2PTWp8uHN06W9s40FH57ORtmQfDr9rLRP8Nq+oIyElbg==
|
||||
|
||||
"@polkadot/wasm-crypto@^0.2.1":
|
||||
version "0.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-0.2.1.tgz#ba5eedf02f762d38fd705c57cba31db7f6e7eb1c"
|
||||
integrity sha512-qk2q3B1QWt69g0ToUAQbRWTPhDPpDyS7GDGsPToSs18pbPwn31462EFuP9CvzIV2eDVifJnmwQeD2A0bx+ogMQ==
|
||||
"@polkadot/wasm-crypto@^0.3.0-beta.0":
|
||||
version "0.3.0-beta.0"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-0.3.0-beta.0.tgz#8c574b8014064622abdaa4cf3c8a5bae69c7bfb8"
|
||||
integrity sha512-ELFZBX79aAIPyfqRe8EjqKJRe3zs8vKylEFkajlunmmzcNmP3kmqgLUYqkDlep4IQm/fh/eHcTo/bYvQgYu/og==
|
||||
|
||||
"@polkadot/wasm-schnorrkel@^0.2.1":
|
||||
version "0.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/wasm-schnorrkel/-/wasm-schnorrkel-0.2.1.tgz#d5cdcb3058d29272abc1d0988049415d0c341b3f"
|
||||
integrity sha512-iFqy9jiiBJMkIgouklfOVm55DRNFe4UkhWX3CKoF3XfFSDJsW/QF0StXNMy+StRyKV1IXJ6SWDt5VLBvAU7SEg==
|
||||
"@polkadot/wasm-dalek-ed25519@^0.3.0-beta.0":
|
||||
version "0.3.0-beta.0"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/wasm-dalek-ed25519/-/wasm-dalek-ed25519-0.3.0-beta.0.tgz#8096f85e2ab2a69b6ed0111813c7be4eea846651"
|
||||
integrity sha512-/bk8FPKVZaCSTxL5fRFWw8U3+it50PzVcFPKXgbllJO7uB9aDH4BeWoDy5+dSTzBkyc31VPdqBxgHsKQQ4CL/g==
|
||||
|
||||
"@polkadot/wasm-schnorrkel@^0.3.0-beta.0":
|
||||
version "0.3.0-beta.0"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/wasm-schnorrkel/-/wasm-schnorrkel-0.3.0-beta.0.tgz#0d5166799251af87c013e047a4825f93fd6d7677"
|
||||
integrity sha512-HRCfgjxCTyMdvp2gcX4G2ZNdl5mKTiZ2SD9dKdZPYoH9fog/1nqUMojyPRQughNH8bIzGMXVi9fihAJ4yTGoXQ==
|
||||
|
||||
"@types/babel__core@^7.1.0":
|
||||
version "7.1.0"
|
||||
|
||||
Reference in New Issue
Block a user