{address,u8a}Eq functions (#606)

* {addres,u8a}Eq functions

* Remove mnemonicToSeed
This commit is contained in:
Jaco Greeff
2020-05-31 14:19:10 +02:00
committed by GitHub
parent 3351fb1cba
commit 1f3dd60a8d
15 changed files with 1678 additions and 985 deletions
@@ -2,7 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import testingPairs from '../../../keyring/src/testingPairs';
import testingPairs from '@polkadot/keyring/testingPairs';
import encode from './encode';
const keyring = testingPairs({ type: 'ed25519' }, false);
@@ -0,0 +1,46 @@
// Copyright 2017-2020 @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 testingPairs from '@polkadot/keyring/testingPairs';
import { addressEq } from '.';
describe('addressEq', (): void => {
it('returns false with non-equal', (): void => {
expect(
addressEq(
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
'5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG'
)
).toEqual(false);
});
it('returns true for equal, matching prefix', (): void => {
expect(
addressEq(
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
)
).toEqual(true);
});
it('returns true for equal, non-matching prefix', (): void => {
expect(
addressEq(
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
'7sL6eNJj5ZGV5cn3hhV2deRUsivXfBfMH76wCALCqWj1EKzv'
)
).toEqual(true);
});
it('returns true for equal, address vs publicKey', (): void => {
const keyring = testingPairs({ type: 'sr25519' });
expect(
addressEq(
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
keyring.alice.publicKey
)
).toEqual(true);
});
});
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2017-2020 @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 { u8aEq } from '@polkadot/util';
import decode from './decode';
/**
* @name addressEq
* @summary Compares two addresses, either in ss58, Uint8Array or hex format.
* @description
* For the input values, return true is the underlying public keys do match.
* @example
* <BR>
*
* ```javascript
* import { u8aEq } from '@polkadot/util';
*
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
* ```
*/
export default function addressEq (a: string | Uint8Array, b: string | Uint8Array): boolean {
return u8aEq(decode(a), decode(b));
}
@@ -8,10 +8,12 @@ import createKeyMulti from './keyMulti';
import createKeySub from './keySub';
import decodeAddress from './decode';
import encodeAddress from './encode';
import addressEq from './eq';
import setSS58Format from './setSS58Format';
import sortAddresses from './sort';
export {
addressEq,
checkAddress,
checkAddressChecksum,
createKeyMulti,
@@ -11,5 +11,4 @@ import '../polyfill';
export { default as mnemonicGenerate } from './generate';
export { default as mnemonicToEntropy } from './toEntropy';
export { default as mnemonicToMiniSecret } from './toMiniSecret';
export { default as mnemonicToSeed } from './toSeed';
export { default as mnemonicValidate } from './validate';
@@ -1,15 +0,0 @@
// Copyright 2017-2020 @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 toSeed from './toSeed';
describe('mnemonicToSeed', (): void => {
it('generates a valid seed', (): void => {
expect(
u8aToHex(toSeed('seed sock milk update focus rotate barely fade car face mechanic mercy'))
).toEqual('0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027');
});
});
@@ -1,30 +0,0 @@
// Copyright 2017-2020 @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 { mnemonicToSeedSync } from 'bip39';
import { bufferToU8a } from '@polkadot/util';
import { bip39ToSeed, isReady } from '@polkadot/wasm-crypto';
/**
* @name toSeed
* @summary Creates a valid seed from a mnemonic input
* @example
* <BR>
*
* ```javascript
* import { mnemonicGenerate, mnemonicToSeed, mnemonicValidate } from '@polkadot/util-crypto';
*
* const mnemonic = mnemonicGenerate(); // => string
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
*
* if (isValidMnemonic) {
* console.log(`Seed generated from mnemonic: ${mnemonicToSeed(mnemonic)}`); => u8a
* }
* ```
*/
export default function toSeed (mnemonic: string, password = ''): Uint8Array {
return isReady()
? bip39ToSeed(mnemonic, password)
: bufferToU8a(mnemonicToSeedSync(mnemonic, password)).subarray(0, 32);
}
+43
View File
@@ -0,0 +1,43 @@
// Copyright 2017-2020 @polkadot/util 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 { u8aEq } from '.';
describe('u8aEq', (): void => {
it('returns false when the lengths do not match', (): void => {
expect(
u8aEq(
new Uint8Array([1, 2, 3]),
new Uint8Array([1, 2])
)
).toEqual(false);
});
it('returns false when the contents do not match', (): void => {
expect(
u8aEq(
new Uint8Array([1, 2, 3]),
new Uint8Array([1, 2, 4])
)
).toEqual(false);
});
it('returns true when the contents do match', (): void => {
expect(
u8aEq(
new Uint8Array([1, 2, 3]),
new Uint8Array([1, 2, 3])
)
).toEqual(true);
});
it('returns true with equalvalent hex & Uint8array', (): void => {
expect(
u8aEq(
'0x010203',
new Uint8Array([1, 2, 3])
)
).toEqual(true);
});
});
+37
View File
@@ -0,0 +1,37 @@
// Copyright 2017-2020 @polkadot/util 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 u8aToU8a from './toU8a';
function equals (a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
/**
* @name u8aEq
* @summary Compares two Uint8Arrays.
* @description
* For `UInt8Array` (or hex string) input values true if there is a match.
* @example
* <BR>
*
* ```javascript
* import { u8aEq } from '@polkadot/util';
*
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
* ```
*/
export default function u8aEq (a: Uint8Array | string, b: Uint8Array | string): boolean {
return equals(u8aToU8a(a), u8aToU8a(b));
}
+1
View File
@@ -7,6 +7,7 @@
*/
export { default as u8aConcat } from './concat';
export { default as u8aEq } from './eq';
export { default as u8aFixLength } from './fixLength';
export { default as u8aSorted } from './sorted';
export { default as u8aToBn } from './toBn';
+12 -7
View File
@@ -2,7 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const ALPHABET = '0123456789abcdef';
const ALPHABET = new Array(256) as string[];
for (let n = 0; n < 256; ++n) {
ALPHABET[n] = n.toString(16).padStart(2, '0');
}
/**
* @name u8aToHex
@@ -35,10 +39,11 @@ export default function u8aToHex (value?: Uint8Array | null, bitLength = -1, isP
return `${u8aToHex(value.subarray(0, halfLength), -1, isPrefixed)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`;
}
// based on comments in https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex and
// implementation in http://jsben.ch/Vjx2V - optimisation here suggests that a forEach loop is faster
// than reduce as well (clocking at in 90% of the reduce speed with tweaking in the playpen above)
return value.reduce((result, value): string => {
return result + ALPHABET[value >> 4] + ALPHABET[value & 15];
}, prefix);
const result = new Array(value.length) as string[];
for (let i = 0; i < value.length; i++) {
result[i] = ALPHABET[value[i]];
}
return prefix + result.join('');
}