Re-use pre-define BN_{BE, LE}_* constants (#1175)
* Re-use pre-define BN_{BE, LE}_* constants
* ordering update
* bnMax/bnMin opt
* u8aToHex flatten
This commit is contained in:
@@ -7,6 +7,7 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../blake2/asU8a';
|
||||
import { BN_LE_16_OPTS } from '../bn';
|
||||
import { decodeAddress } from './decode';
|
||||
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
@@ -16,7 +17,7 @@ export function createKeyDerived (who: HexString | Uint8Array | string, index: b
|
||||
u8aConcat(
|
||||
PREFIX,
|
||||
decodeAddress(who),
|
||||
bnToU8a(index, { bitLength: 16, isLe: true })
|
||||
bnToU8a(index, BN_LE_16_OPTS)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../blake2/asU8a';
|
||||
import { BN_LE_16_OPTS } from '../bn';
|
||||
import { addressToU8a } from './util';
|
||||
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
@@ -17,7 +18,7 @@ export function createKeyMulti (who: (HexString | Uint8Array | string)[], thresh
|
||||
PREFIX,
|
||||
compactToU8a(who.length),
|
||||
...u8aSorted(who.map(addressToU8a)),
|
||||
bnToU8a(threshold, { bitLength: 16, isLe: true })
|
||||
bnToU8a(threshold, BN_LE_16_OPTS)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const BN_LE_OPTS = { isLe: true };
|
||||
|
||||
export const BN_LE_16_OPTS = { bitLength: 16, isLe: true };
|
||||
|
||||
export const BN_BE_32_OPTS = { bitLength: 32, isLe: false };
|
||||
export const BN_LE_32_OPTS = { bitLength: 32, isLe: true };
|
||||
|
||||
export const BN_BE_256_OPTS = { bitLength: 256, isLe: false };
|
||||
export const BN_LE_256_OPTS = { bitLength: 256, isLe: true };
|
||||
|
||||
export const BN_LE_512_OPTS = { bitLength: 512, isLe: true };
|
||||
@@ -5,6 +5,7 @@ import type { Keypair } from '../../types';
|
||||
|
||||
import { assert, bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_32_OPTS } from '../../bn';
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
import { secp256k1KeypairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath';
|
||||
@@ -24,7 +25,7 @@ function createCoded (secretKey: Uint8Array, chainCode: Uint8Array): CodedKeypai
|
||||
}
|
||||
|
||||
function deriveChild (hd: CodedKeypair, index: number): CodedKeypair {
|
||||
const indexBuffer = bnToU8a(index, { bitLength: 32, isLe: false });
|
||||
const indexBuffer = bnToU8a(index, BN_BE_32_OPTS);
|
||||
const data = index >= HARDENED
|
||||
? u8aConcat(new Uint8Array(1), hd.secretKey, indexBuffer)
|
||||
: u8aConcat(hd.publicKey, indexBuffer);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import { BN_EIGHT, bnToU8a, u8aConcat, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { BN_LE_32_OPTS, BN_LE_512_OPTS, BN_LE_OPTS } from '../../bn';
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
|
||||
// performs hard-only derivation on the xprv
|
||||
@@ -10,19 +11,19 @@ export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Arra
|
||||
const kl = xprv.subarray(0, 32);
|
||||
const kr = xprv.subarray(32, 64);
|
||||
const cc = xprv.subarray(64, 96);
|
||||
const data = u8aConcat([0], kl, kr, bnToU8a(index, { bitLength: 32, isLe: true }));
|
||||
const data = u8aConcat([0], kl, kr, bnToU8a(index, BN_LE_32_OPTS));
|
||||
const z = hmacSha512(cc, data);
|
||||
|
||||
data[0] = 0x01;
|
||||
|
||||
return u8aConcat(
|
||||
bnToU8a(
|
||||
u8aToBn(kl, { isLe: true }).iadd(u8aToBn(z.subarray(0, 28), { isLe: true }).imul(BN_EIGHT)),
|
||||
{ bitLength: 512, isLe: true }
|
||||
u8aToBn(kl, BN_LE_OPTS).iadd(u8aToBn(z.subarray(0, 28), BN_LE_OPTS).imul(BN_EIGHT)),
|
||||
BN_LE_512_OPTS
|
||||
).subarray(0, 32),
|
||||
bnToU8a(
|
||||
u8aToBn(kr, { isLe: true }).iadd(u8aToBn(z.subarray(32, 64), { isLe: true })),
|
||||
{ bitLength: 512, isLe: true }
|
||||
u8aToBn(kr, BN_LE_OPTS).iadd(u8aToBn(z.subarray(32, 64), BN_LE_OPTS)),
|
||||
BN_LE_512_OPTS
|
||||
).subarray(0, 32),
|
||||
hmacSha512(cc, data).subarray(32, 64)
|
||||
);
|
||||
|
||||
@@ -6,14 +6,11 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { BN, bnToU8a, compactAddLength, hexToU8a, isBigInt, isBn, isHex, isNumber, isString, stringToU8a } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../blake2/asU8a';
|
||||
import { BN_LE_256_OPTS } from '../bn';
|
||||
|
||||
const RE_NUMBER = /^\d+$/;
|
||||
|
||||
const JUNCTION_ID_LEN = 32;
|
||||
const BN_OPTIONS = {
|
||||
bitLength: 256,
|
||||
isLe: true
|
||||
};
|
||||
|
||||
export class DeriveJunction {
|
||||
readonly #chainCode: Uint8Array = new Uint8Array(32);
|
||||
@@ -61,7 +58,7 @@ export class DeriveJunction {
|
||||
|
||||
public soft (value: HexString | number | string | bigint | BN | Uint8Array): DeriveJunction {
|
||||
if (isNumber(value) || isBn(value) || isBigInt(value)) {
|
||||
return this.soft(bnToU8a(value, BN_OPTIONS));
|
||||
return this.soft(bnToU8a(value, BN_LE_256_OPTS));
|
||||
} else if (isHex(value)) {
|
||||
return this.soft(hexToU8a(value));
|
||||
} else if (isString(value)) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
import { bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_32_OPTS } from '../bn';
|
||||
import { hmacSha512 } from '../hmac';
|
||||
|
||||
export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array {
|
||||
@@ -14,7 +15,7 @@ export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: numb
|
||||
|
||||
while (out.length < len) {
|
||||
num++;
|
||||
block.set(bnToU8a(num, { bitLength: 32, isLe: false }), salt.length);
|
||||
block.set(bnToU8a(num, BN_BE_32_OPTS), salt.length);
|
||||
|
||||
let prev = hmacSha512(password, block);
|
||||
const md = prev;
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { Params } from './types';
|
||||
|
||||
import { assert, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { BN_LE_OPTS } from '../bn';
|
||||
import { DEFAULT_PARAMS } from './defaults';
|
||||
|
||||
interface Result {
|
||||
@@ -14,9 +15,9 @@ interface Result {
|
||||
|
||||
export function scryptFromU8a (data: Uint8Array): Result {
|
||||
const salt = data.subarray(0, 32);
|
||||
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), { isLe: true }).toNumber();
|
||||
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), { isLe: true }).toNumber();
|
||||
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), { isLe: true }).toNumber();
|
||||
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), BN_LE_OPTS).toNumber();
|
||||
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), BN_LE_OPTS).toNumber();
|
||||
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), BN_LE_OPTS).toNumber();
|
||||
|
||||
// FIXME At this moment we assume these to be fixed params, this is not a great idea since we lose flexibility
|
||||
// and updates for greater security. However we need some protection against carefully-crafted params that can
|
||||
|
||||
@@ -5,11 +5,13 @@ import type { Params } from './types';
|
||||
|
||||
import { bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_LE_32_OPTS } from '../bn';
|
||||
|
||||
export function scryptToU8a (salt: Uint8Array, { N, p, r }: Params): Uint8Array {
|
||||
return u8aConcat(
|
||||
salt,
|
||||
bnToU8a(N, { bitLength: 32, isLe: true }),
|
||||
bnToU8a(p, { bitLength: 32, isLe: true }),
|
||||
bnToU8a(r, { bitLength: 32, isLe: true })
|
||||
bnToU8a(N, BN_LE_32_OPTS),
|
||||
bnToU8a(p, BN_LE_32_OPTS),
|
||||
bnToU8a(r, BN_LE_32_OPTS)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { EXPAND_OPT, secp256k1 } from './secp256k1';
|
||||
import { BN_BE_256_OPTS } from '../bn';
|
||||
import { secp256k1 } from './secp256k1';
|
||||
|
||||
export function secp256k1Expand (publicKey: Uint8Array): Uint8Array {
|
||||
assert([33, 65].includes(publicKey.length), 'Invalid publicKey provided');
|
||||
@@ -13,7 +14,7 @@ export function secp256k1Expand (publicKey: Uint8Array): Uint8Array {
|
||||
.getPublic();
|
||||
|
||||
return u8aConcat(
|
||||
bnToU8a(expanded.getX(), EXPAND_OPT),
|
||||
bnToU8a(expanded.getY(), EXPAND_OPT)
|
||||
bnToU8a(expanded.getX(), BN_BE_256_OPTS),
|
||||
bnToU8a(expanded.getY(), BN_BE_256_OPTS)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ import type { Keypair } from '../../types';
|
||||
|
||||
import { assert, bnToU8a } from '@polkadot/util';
|
||||
|
||||
import { EXPAND_OPT, secp256k1 } from '../secp256k1';
|
||||
import { BN_BE_256_OPTS } from '../../bn';
|
||||
import { secp256k1 } from '../secp256k1';
|
||||
|
||||
/**
|
||||
* @name secp256k1KeypairFromSeed
|
||||
@@ -18,6 +19,6 @@ export function secp256k1KeypairFromSeed (seed: Uint8Array): Keypair {
|
||||
|
||||
return {
|
||||
publicKey: new Uint8Array(key.getPublic().encodeCompressed()),
|
||||
secretKey: bnToU8a(key.getPrivate(), EXPAND_OPT)
|
||||
secretKey: bnToU8a(key.getPrivate(), BN_BE_256_OPTS)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,8 +3,5 @@
|
||||
|
||||
import elliptic from 'elliptic';
|
||||
|
||||
const EC = elliptic.ec;
|
||||
|
||||
export const secp256k1 = new EC('secp256k1');
|
||||
|
||||
export const EXPAND_OPT = { bitLength: 256, isLe: false };
|
||||
// eslint-disable-next-line new-cap
|
||||
export const secp256k1 = new elliptic.ec('secp256k1');
|
||||
|
||||
@@ -6,8 +6,9 @@ import type { HashType } from './types';
|
||||
|
||||
import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_256_OPTS } from '../bn';
|
||||
import { secp256k1Hasher } from './hasher';
|
||||
import { EXPAND_OPT, secp256k1 } from './secp256k1';
|
||||
import { secp256k1 } from './secp256k1';
|
||||
|
||||
/**
|
||||
* @name secp256k1Sign
|
||||
@@ -20,8 +21,8 @@ export function secp256k1Sign (message: Uint8Array | string, { secretKey }: Part
|
||||
const ecsig = key.sign(secp256k1Hasher(hashType, message));
|
||||
|
||||
return u8aConcat(
|
||||
bnToU8a(ecsig.r, EXPAND_OPT),
|
||||
bnToU8a(ecsig.s, EXPAND_OPT),
|
||||
bnToU8a(ecsig.r, BN_BE_256_OPTS),
|
||||
bnToU8a(ecsig.s, BN_BE_256_OPTS),
|
||||
new Uint8Array([ecsig.recoveryParam || 0])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import { assert, BN, bnToU8a, isU8a } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_256_OPTS } from '../bn';
|
||||
import { secp256k1 } from './secp256k1';
|
||||
|
||||
const ecparams = secp256k1.curve as { n: BN };
|
||||
@@ -23,5 +24,5 @@ export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Arr
|
||||
|
||||
assert(!bn.isZero(), 'Invalid resulting private key');
|
||||
|
||||
return bnToU8a(bn, { bitLength: 256, isLe: false });
|
||||
return bnToU8a(bn, BN_BE_256_OPTS);
|
||||
}
|
||||
|
||||
@@ -15,4 +15,16 @@ describe('bnMax', (): void => {
|
||||
bnMax(new BN(2), new BN(3), new BN(1))
|
||||
).toEqual(new BN(3));
|
||||
});
|
||||
|
||||
it('returns a single item', (): void => {
|
||||
expect(
|
||||
bnMax(new BN(1))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => bnMax()
|
||||
).toThrow(/Must provide one or more BN arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from './bn';
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
import { checkMaxMin } from './util';
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = BN[type](result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
|
||||
@@ -9,4 +9,10 @@ describe('bnMin', (): void => {
|
||||
bnMin(new BN(2), new BN(1), new BN(3))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
|
||||
it('returns a single item', (): void => {
|
||||
expect(
|
||||
bnMin(new BN(1))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { checkMaxMin } from './util';
|
||||
import { checkMaxMin } from './max';
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
|
||||
@@ -9,12 +9,12 @@ import { objectSpread } from '../object/spread';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { bnToU8a } from './toU8a';
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
interface Options extends ToBnOptions {
|
||||
bitLength?: number;
|
||||
}
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
/**
|
||||
* @name bnToHex
|
||||
* @summary Creates a hex value from a BN.js bignumber object.
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (const v of items) {
|
||||
result = BN[type](result, v);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -10,7 +10,7 @@ type HexByte = `${HexDigit}${HexDigit}`;
|
||||
const ALPHABET = arrayRange(256).map((n) => n.toString(16).padStart(2, '0') as HexByte);
|
||||
|
||||
/** @internal */
|
||||
function extract (value: Uint8Array): string {
|
||||
function hex (value: Uint8Array): string {
|
||||
const result = new Array<HexByte>(value.length);
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
@@ -20,20 +20,6 @@ function extract (value: Uint8Array): string {
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function unprefixed (value: Uint8Array, bitLength = -1): string {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
return (byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function trim (value: Uint8Array, halfLength: number): string {
|
||||
return `${unprefixed(value.subarray(0, halfLength))}…${unprefixed(value.subarray(value.length - halfLength))}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToHex
|
||||
* @summary Creates a hex string from a Uint8Array object.
|
||||
@@ -49,9 +35,13 @@ function trim (value: Uint8Array, halfLength: number): string {
|
||||
* ```
|
||||
*/
|
||||
export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): HexString {
|
||||
const length = Math.ceil(bitLength / 8);
|
||||
|
||||
return `${isPrefixed ? '0x' : ''}${
|
||||
!value || !value.length
|
||||
? ''
|
||||
: unprefixed(value, bitLength)
|
||||
: (length > 0 && value.length > length)
|
||||
? `${hex(value.subarray(0, length / 2))}…${hex(value.subarray(value.length - length / 2))}`
|
||||
: hex(value)
|
||||
}` as HexString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user