Re-enable esm modules (#762)
This commit is contained in:
+1
-1
@@ -21,7 +21,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.12.3",
|
||||
"@polkadot/dev": "^0.59.21",
|
||||
"@polkadot/dev": "^0.59.22",
|
||||
"@polkadot/ts": "^0.3.53",
|
||||
"@types/jest": "^26.0.15"
|
||||
},
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"version": "4.0.2-7",
|
||||
"description": "Keyring management",
|
||||
"main": "index.js",
|
||||
"module": "esnext/index.js",
|
||||
"module": "index.mjs",
|
||||
"sideEffects": false,
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
|
||||
@@ -3,15 +3,10 @@
|
||||
|
||||
import './detectPackage';
|
||||
|
||||
import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
import { Keyring } from './keyring';
|
||||
|
||||
export { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
export default Keyring;
|
||||
|
||||
export {
|
||||
Keyring,
|
||||
decodeAddress,
|
||||
encodeAddress,
|
||||
setSS58Format
|
||||
};
|
||||
export { Keyring };
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"version": "4.0.2-7",
|
||||
"description": "A list of all available Substrate networks and their applicable prefixes",
|
||||
"main": "index.js",
|
||||
"module": "esnext/index.js",
|
||||
"module": "index.mjs",
|
||||
"sideEffects": false,
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"version": "4.0.2-7",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"module": "esnext/index.js",
|
||||
"module": "index.mjs",
|
||||
"sideEffects": false,
|
||||
"keywords": [
|
||||
"Polkadot"
|
||||
|
||||
@@ -11,36 +11,44 @@ describe('blake2AsU8a', (): void => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a 64-bit value by default', (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc')
|
||||
).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])
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 64-bit value by default (onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc', undefined, undefined, onlyJs)
|
||||
).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])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a 128-bit value (as specified)', (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc', 128)
|
||||
).toEqual(
|
||||
new Uint8Array([207, 74, 183, 145, 198, 43, 141, 43, 33, 9, 201, 2, 117, 40, 120, 22])
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 128-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc', 128, undefined, onlyJs)
|
||||
).toEqual(
|
||||
new Uint8Array([207, 74, 183, 145, 198, 43, 141, 43, 33, 9, 201, 2, 117, 40, 120, 22])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a 256-bit value (as specified)', (): void => {
|
||||
expect(
|
||||
blake2AsU8a('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])
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 256-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc', 256, undefined, onlyJs)
|
||||
).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])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a 512-bit value (as specified)', (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc', 512)
|
||||
).toEqual(
|
||||
hexToU8a('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923')
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 512-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
blake2AsU8a('abc', 512, undefined, onlyJs)
|
||||
).toEqual(
|
||||
hexToU8a('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('has equivalent Wasm/Js outputs on hex inputs', (): void => {
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import wasmCrypto from '@polkadot/wasm-crypto';
|
||||
import { isReady, waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
export function cryptoIsReady (): boolean {
|
||||
return wasmCrypto.isReady();
|
||||
return isReady();
|
||||
}
|
||||
|
||||
export function cryptoWaitReady (): Promise<boolean> {
|
||||
return wasmCrypto
|
||||
.waitReady()
|
||||
return waitReady()
|
||||
.then((): boolean => true)
|
||||
.catch((error): boolean => {
|
||||
console.error('Unable to initialize @polkadot/util-crypto', error);
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import wasmCrypto from '@polkadot/wasm-crypto';
|
||||
|
||||
export function cryptoIsReady (): boolean {
|
||||
return wasmCrypto.isReady();
|
||||
}
|
||||
|
||||
export function cryptoWaitReady (): Promise<boolean> {
|
||||
return wasmCrypto
|
||||
.waitReady()
|
||||
.then((): boolean => true)
|
||||
.catch((error): boolean => {
|
||||
console.error('Unable to initialize @polkadot/util-crypto', error);
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// start init process immediately
|
||||
cryptoWaitReady().catch((): void => {
|
||||
// shouldn't happen, logged above
|
||||
});
|
||||
@@ -16,25 +16,27 @@ describe('keccakAsU8a', (): void => {
|
||||
'0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'
|
||||
);
|
||||
|
||||
it('returns an hex representation (string)', (): void => {
|
||||
expect(
|
||||
keccakAsU8a(input)
|
||||
).toEqual(output);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns an hex representation (string, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
keccakAsU8a(input, onlyJs)
|
||||
).toEqual(output);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an hex representation (Buffer)', (): void => {
|
||||
expect(
|
||||
keccakAsU8a(
|
||||
Buffer.from(input)
|
||||
)
|
||||
).toEqual(output);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns an hex representation (Buffer, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
keccakAsU8a(Buffer.from(input), onlyJs)
|
||||
).toEqual(output);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an hex representation (Uint8Array)', (): void => {
|
||||
expect(
|
||||
keccakAsU8a(
|
||||
stringToU8a(input)
|
||||
)
|
||||
).toEqual(output);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns an hex representation (Uint8Array, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
keccakAsU8a(stringToU8a(input), onlyJs)
|
||||
).toEqual(output);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import jssha3 from 'js-sha3';
|
||||
import { keccak256 as keccak256Js } from 'js-sha3';
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, keccak256 } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -19,10 +19,10 @@ import { isReady, keccak256 } from '@polkadot/wasm-crypto';
|
||||
* keccakAsU8a('123'); // => Uint8Array
|
||||
* ```
|
||||
*/
|
||||
export function keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array {
|
||||
return isReady()
|
||||
export function keccakAsU8a (value: Buffer | Uint8Array | string, onlyJs = false): Uint8Array {
|
||||
return isReady() && !onlyJs
|
||||
? keccak256(u8aToU8a(value))
|
||||
: new Uint8Array(
|
||||
jssha3.keccak256.update(u8aToU8a(value)).arrayBuffer()
|
||||
keccak256Js.update(u8aToU8a(value)).arrayBuffer()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Keypair } from '../../types';
|
||||
|
||||
import { box } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclBoxKeypairFromSecret
|
||||
@@ -20,5 +20,5 @@ import { box } from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function naclBoxKeypairFromSecret (secret: Uint8Array): Keypair {
|
||||
return box.keyPair.fromSecretKey(secret.slice(0, 32));
|
||||
return nacl.box.keyPair.fromSecretKey(secret.slice(0, 32));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { secretbox } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclDecrypt
|
||||
@@ -18,5 +18,5 @@ import { secretbox } from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function naclDecrypt (encrypted: Uint8Array, nonce: Uint8Array, secret: Uint8Array): Uint8Array | null {
|
||||
return secretbox.open(encrypted, nonce, secret) || null;
|
||||
return nacl.secretbox.open(encrypted, nonce, secret) || null;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { secretbox } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
|
||||
@@ -26,7 +26,7 @@ interface Encrypted {
|
||||
*/
|
||||
export function naclEncrypt (message: Uint8Array, secret: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Encrypted {
|
||||
return {
|
||||
encrypted: secretbox(message, nonce, secret),
|
||||
encrypted: nacl.secretbox(message, nonce, secret),
|
||||
nonce
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Keypair } from '../../types';
|
||||
|
||||
import { sign } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromRandom
|
||||
@@ -20,5 +20,5 @@ import { sign } from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromRandom (): Keypair {
|
||||
return sign.keyPair();
|
||||
return nacl.sign.keyPair();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Keypair } from '../../types';
|
||||
|
||||
import { sign } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromSecret
|
||||
@@ -20,5 +20,5 @@ import { sign } from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromSecret (secret: Uint8Array): Keypair {
|
||||
return sign.keyPair.fromSecretKey(secret);
|
||||
return nacl.sign.keyPair.fromSecretKey(secret);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Keypair } from '../../types';
|
||||
|
||||
import { sign } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
import { isReady, ed25519KeypairFromSeed } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
@@ -30,5 +30,5 @@ export function naclKeypairFromSeed (seed: Uint8Array): Keypair {
|
||||
};
|
||||
}
|
||||
|
||||
return sign.keyPair.fromSeed(seed);
|
||||
return nacl.sign.keyPair.fromSeed(seed);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { box } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclOpen
|
||||
@@ -18,5 +18,5 @@ import { box } from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function naclOpen (sealed: Uint8Array, nonce: Uint8Array, senderBoxPublic: Uint8Array, receiverBoxSecret: Uint8Array): Uint8Array | null {
|
||||
return box.open(sealed, nonce, senderBoxPublic, receiverBoxSecret) || null;
|
||||
return nacl.box.open(sealed, nonce, senderBoxPublic, receiverBoxSecret) || null;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { box } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
|
||||
@@ -27,6 +27,6 @@ interface Sealed {
|
||||
export function naclSeal (message: Uint8Array, senderBoxSecret: Uint8Array, receiverBoxPublic: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Sealed {
|
||||
return {
|
||||
nonce,
|
||||
sealed: box(message, nonce, receiverBoxPublic, senderBoxSecret)
|
||||
sealed: nacl.box(message, nonce, receiverBoxPublic, senderBoxSecret)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,16 +11,19 @@ describe('naclSign', (): void => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a valid signature for the message', (): void => {
|
||||
expect(
|
||||
naclSign(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
naclKeypairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a valid signature for the message (onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
naclSign(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
naclKeypairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
),
|
||||
onlyJs
|
||||
)
|
||||
)
|
||||
).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])
|
||||
);
|
||||
).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])
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Keypair } from '../types';
|
||||
|
||||
import { sign } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, ed25519Sign } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -21,12 +21,12 @@ import { isReady, ed25519Sign } from '@polkadot/wasm-crypto';
|
||||
* naclSign([...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclSign (message: Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
export function naclSign (message: Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>, onlyJs = false): Uint8Array {
|
||||
assert(secretKey, 'Expected a valid secretKey');
|
||||
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return isReady()
|
||||
return isReady() && !onlyJs
|
||||
? ed25519Sign(publicKey as Uint8Array, secretKey.subarray(0, 32), messageU8a)
|
||||
: sign.detached(messageU8a, secretKey);
|
||||
: nacl.sign.detached(messageU8a, secretKey);
|
||||
}
|
||||
|
||||
@@ -19,14 +19,17 @@ describe('naclVerify', (): void => {
|
||||
signature = 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]);
|
||||
});
|
||||
|
||||
it('validates a correctly signed message', (): void => {
|
||||
expect(
|
||||
naclVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
signature,
|
||||
publicKey
|
||||
)
|
||||
).toEqual(true);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`validates a correctly signed message (onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
naclVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
signature,
|
||||
publicKey,
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('fails a correctly signed message (message changed)', (): void => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { sign } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, ed25519Verify } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -19,7 +19,7 @@ import { isReady, ed25519Verify } from '@polkadot/wasm-crypto';
|
||||
* naclVerify([...], [...], [...]); // => true/false
|
||||
* ```
|
||||
*/
|
||||
export function naclVerify (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string): boolean {
|
||||
export function naclVerify (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, onlyJs = false): boolean {
|
||||
const messageU8a = u8aToU8a(message);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
@@ -27,7 +27,7 @@ export function naclVerify (message: Uint8Array | string, signature: Uint8Array
|
||||
assert(publicKeyU8a.length === 32, `Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
|
||||
assert(signatureU8a.length === 64, `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
|
||||
return isReady()
|
||||
return isReady() && !onlyJs
|
||||
? ed25519Verify(signatureU8a, messageU8a, publicKeyU8a)
|
||||
: sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
|
||||
: nacl.sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
|
||||
}
|
||||
|
||||
@@ -10,15 +10,15 @@ describe('sha512AsU8a', (): void => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('creates a sha-512 hash', (): void => {
|
||||
expect(
|
||||
sha512AsU8a(
|
||||
Uint8Array.from([0x61, 0x62, 0x63, 0x64])
|
||||
)
|
||||
).toEqual(
|
||||
Uint8Array.from([
|
||||
216, 2, 47, 32, 96, 173, 110, 253, 41, 122, 183, 61, 204, 83, 85, 201, 178, 20, 5, 75, 13, 23, 118, 161, 54, 166, 105, 210, 106, 125, 59, 20, 247, 58, 160, 208, 235, 255, 25, 238, 51, 51, 104, 240, 22, 75, 100, 25, 169, 109, 164, 158, 62, 72, 23, 83, 231, 233, 107, 113, 107, 220, 203, 111
|
||||
])
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`creates a sha-512 hash (onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
sha512AsU8a(Uint8Array.from([0x61, 0x62, 0x63, 0x64]), onlyJs)
|
||||
).toEqual(
|
||||
Uint8Array.from([
|
||||
216, 2, 47, 32, 96, 173, 110, 253, 41, 122, 183, 61, 204, 83, 85, 201, 178, 20, 5, 75, 13, 23, 118, 161, 54, 166, 105, 210, 106, 125, 59, 20, 247, 58, 160, 208, 235, 255, 25, 238, 51, 51, 104, 240, 22, 75, 100, 25, 169, 109, 164, 158, 62, 72, 23, 83, 231, 233, 107, 113, 107, 220, 203, 111
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hash } from 'tweetnacl';
|
||||
import * as nacl from 'tweetnacl';
|
||||
import { isReady, sha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
@@ -18,8 +18,8 @@ import { isReady, sha512 } from '@polkadot/wasm-crypto';
|
||||
* sha512AsU8a(Uint8Array.from([...])); // => Uint8Array([...])
|
||||
* ```
|
||||
*/
|
||||
export function sha512AsU8a (data: Uint8Array): Uint8Array {
|
||||
return isReady()
|
||||
export function sha512AsU8a (data: Uint8Array, onlyJs = false): Uint8Array {
|
||||
return isReady() && !onlyJs
|
||||
? sha512(data)
|
||||
: hash(data);
|
||||
: nacl.hash(data);
|
||||
}
|
||||
|
||||
@@ -11,27 +11,33 @@ describe('xxhashAsU8a', (): void => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('returns a 64-bit value by default', (): void => {
|
||||
expect(
|
||||
xxhashAsU8a('abc')
|
||||
).toEqual(
|
||||
hexToU8a('0x990977adf52cbc44')
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 64-bit value by default (onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
xxhashAsU8a('abc', undefined, onlyJs)
|
||||
).toEqual(
|
||||
hexToU8a('0x990977adf52cbc44')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a 128-bit value (as specified)', (): void => {
|
||||
expect(
|
||||
xxhashAsU8a('abc', 128)
|
||||
).toEqual(
|
||||
hexToU8a('0x990977adf52cbc440889329981caa9be')
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 128-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
xxhashAsU8a('abc', 128, onlyJs)
|
||||
).toEqual(
|
||||
hexToU8a('0x990977adf52cbc440889329981caa9be')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a 256-bit value (as specified)', (): void => {
|
||||
expect(
|
||||
xxhashAsU8a('abc', 256)
|
||||
).toEqual(
|
||||
hexToU8a('0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b')
|
||||
);
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
it(`returns a 256-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
expect(
|
||||
xxhashAsU8a('abc', 256, onlyJs)
|
||||
).toEqual(
|
||||
hexToU8a('0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b')
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,10 +20,10 @@ import xxhash64AsBn from './xxhash64/asBn';
|
||||
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
||||
* ```
|
||||
*/
|
||||
export function xxhashAsU8a (data: Buffer | Uint8Array | string, bitLength = 64): Uint8Array {
|
||||
export function xxhashAsU8a (data: Buffer | Uint8Array | string, bitLength = 64, onlyJs = false): Uint8Array {
|
||||
const iterations = Math.ceil(bitLength / 64);
|
||||
|
||||
if (isReady()) {
|
||||
if (isReady() && !onlyJs) {
|
||||
return twox(u8aToU8a(data), iterations);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"version": "4.0.2-7",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"module": "esnext/index.js",
|
||||
"module": "index.mjs",
|
||||
"sideEffects": false,
|
||||
"keywords": [
|
||||
"Polkadot"
|
||||
|
||||
@@ -1733,9 +1733,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/dev@npm:^0.59.21":
|
||||
version: 0.59.21
|
||||
resolution: "@polkadot/dev@npm:0.59.21"
|
||||
"@polkadot/dev@npm:^0.59.22":
|
||||
version: 0.59.22
|
||||
resolution: "@polkadot/dev@npm:0.59.22"
|
||||
dependencies:
|
||||
"@babel/cli": ^7.12.1
|
||||
"@babel/core": ^7.12.3
|
||||
@@ -1762,6 +1762,7 @@ __metadata:
|
||||
"@typescript-eslint/parser": 4.6.1
|
||||
"@vue/component-compiler-utils": ^3.2.0
|
||||
babel-jest: ^26.6.3
|
||||
babel-plugin-module-extension-resolver: ^0.3.0
|
||||
babel-plugin-module-resolver: ^4.0.0
|
||||
babel-plugin-styled-components: ^1.11.1
|
||||
browserslist: ^4.14.6
|
||||
@@ -1821,7 +1822,7 @@ __metadata:
|
||||
polkadot-exec-typedoc: scripts/polkadot-exec-typedoc.js
|
||||
polkadot-exec-vuepress: scripts/polkadot-exec-vuepress.js
|
||||
polkadot-exec-webpack: scripts/polkadot-exec-webpack.js
|
||||
checksum: c853d9077c256e68074fc2c08ff8e7e140f01e547e71a8021c71373c3b0fedf7a0e013e9e91eef463f3be60031ca5a1e5c7b490740d5eba5cc5e96b337369a2f
|
||||
checksum: cdd11d3f5c5a3bb525be7a954b1d9679771e627719b703fd799fcc4e32694b519260607dc0df7c2ec98450cf61f711f61bed6f9801a6792be84547ff6cf75c43
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -2857,6 +2858,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"babel-plugin-module-extension-resolver@npm:^0.3.0":
|
||||
version: 0.3.0
|
||||
resolution: "babel-plugin-module-extension-resolver@npm:0.3.0"
|
||||
checksum: d7522f7f17f27747aee35ca0ac0e5af6286065c67210e0e038ff5065e8173d5bdaef8a4f5916dd169fda5abcc96c8fb8175dececc1758210675144c6d48c9f32
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"babel-plugin-module-resolver@npm:^4.0.0":
|
||||
version: 4.0.0
|
||||
resolution: "babel-plugin-module-resolver@npm:4.0.0"
|
||||
@@ -9091,7 +9099,7 @@ fsevents@^1.2.7:
|
||||
resolution: "root-workspace-0b6124@workspace:."
|
||||
dependencies:
|
||||
"@babel/core": ^7.12.3
|
||||
"@polkadot/dev": ^0.59.21
|
||||
"@polkadot/dev": ^0.59.22
|
||||
"@polkadot/ts": ^0.3.53
|
||||
"@types/jest": ^26.0.15
|
||||
languageName: unknown
|
||||
|
||||
Reference in New Issue
Block a user