Adjust base* validation function (#1139)

* Adjust base* validation function

* Adjust sr25519 derive functions

* More dedupe

* Rename

* Adjust
This commit is contained in:
Jaco
2021-10-23 13:11:58 +03:00
committed by GitHub
parent f4d933936e
commit 93d5041e8f
8 changed files with 48 additions and 65 deletions
+2 -6
View File
@@ -1,17 +1,13 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { validateChars } from '../base58/validate';
import { createValidateFn } from '../base58/validate';
import { BASE32_ALPHABET } from './bs32';
const BASE_CONFIG = { alphabet: BASE32_ALPHABET, ipfsChar: 'b', type: 'base32' };
/**
* @name base32Validate
* @summary Validates a base32 value.
* @description
* Validates that the supplied value is valid base32
*/
export function base32Validate (value?: string | null, ipfsCompat?: boolean): value is string {
return validateChars(BASE_CONFIG, value, ipfsCompat);
}
export const base32Validate = createValidateFn({ alphabet: BASE32_ALPHABET, ipfsChar: 'b', type: 'base32' });
+10 -12
View File
@@ -11,17 +11,17 @@ interface CheckConfig {
type: string;
}
const BASE_CONFIG = { alphabet: BASE58_ALPHABET, ipfsChar: 'z', type: 'base58' };
export function createValidateFn ({ alphabet, ipfsChar, type }: CheckConfig): (value?: unknown, ipfsCompat?: boolean) => value is string {
return (value?: unknown, ipfsCompat?: boolean): value is string => {
assert(value && typeof value === 'string', () => `Expected non-null, non-empty ${type} string input`);
assert(!ipfsCompat || value[0] === ipfsChar, () => `Expected ${type} to start with '${ipfsChar}'`);
export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?: unknown, ipfsCompat?: boolean): true {
assert(value && typeof value === 'string', () => `Expected non-null, non-empty ${type} string input`);
assert(!ipfsCompat || value[0] === ipfsChar, () => `Expected ${type} to start with '${ipfsChar}'`);
for (let i = (ipfsCompat ? 1 : 0); i < value.length; i++) {
assert(alphabet.includes(value[i]), () => `Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
}
for (let i = (ipfsCompat ? 1 : 0); i < value.length; i++) {
assert(alphabet.includes(value[i]), () => `Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
}
return true;
return true;
};
}
/**
@@ -30,6 +30,4 @@ export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?
* @description
* Validates that the supplied value is valid base58
*/
export function base58Validate (value?: string | null, ipfsCompat?: boolean): value is string {
return validateChars(BASE_CONFIG, value, ipfsCompat);
}
export const base58Validate = createValidateFn({ alphabet: BASE58_ALPHABET, ipfsChar: 'z', type: 'base58' });
@@ -0,0 +1,18 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Keypair } from '../types';
import { assert } from '@polkadot/util';
import { DeriveJunction } from './DeriveJunction';
export function createSeedDeriveFn (fromSeed: (seed: Uint8Array) => Keypair, derive: (seed: Uint8Array, chainCode: Uint8Array) => Uint8Array): (keypair: Keypair, junction: DeriveJunction) => Keypair {
return (keypair: Keypair, { chainCode, isHard }: DeriveJunction): Keypair => {
assert(isHard, 'A soft key was found in the path and is not supported');
return fromSeed(
derive(keypair.secretKey.subarray(0, 32), chainCode)
);
};
}
+2 -12
View File
@@ -1,18 +1,8 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Keypair } from '../types';
import { assert } from '@polkadot/util';
import { secp256k1DeriveHard } from '../secp256k1/deriveHard';
import { secp256k1KeypairFromSeed } from '../secp256k1/keypair/fromSeed';
import { DeriveJunction } from './DeriveJunction';
import { createSeedDeriveFn } from './hdkdDerive';
export function keyHdkdEcdsa (keypair: Keypair, { chainCode, isHard }: DeriveJunction): Keypair {
assert(isHard, 'A soft key was found in the path (and is unsupported)');
return secp256k1KeypairFromSeed(
secp256k1DeriveHard(keypair.secretKey.subarray(0, 32), chainCode)
);
}
export const keyHdkdEcdsa = createSeedDeriveFn(secp256k1KeypairFromSeed, secp256k1DeriveHard);
+2 -12
View File
@@ -1,18 +1,8 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Keypair } from '../types';
import { assert } from '@polkadot/util';
import { naclDeriveHard } from '../nacl/deriveHard';
import { naclKeypairFromSeed } from '../nacl/keypair/fromSeed';
import { DeriveJunction } from './DeriveJunction';
import { createSeedDeriveFn } from './hdkdDerive';
export function keyHdkdEd25519 (keypair: Keypair, { chainCode, isHard }: DeriveJunction): Keypair {
assert(isHard, 'A soft key was found in the path (and is unsupported)');
return naclKeypairFromSeed(
naclDeriveHard(keypair.secretKey.subarray(0, 32), chainCode)
);
}
export const keyHdkdEd25519 = createSeedDeriveFn(naclKeypairFromSeed, naclDeriveHard);
+8 -11
View File
@@ -5,19 +5,16 @@ import type { HexString } from '@polkadot/util/types';
import type { Keypair } from '../types';
import { u8aToU8a } from '@polkadot/util';
import { sr25519DeriveKeypairHard, sr25519DeriveKeypairSoft } from '@polkadot/wasm-crypto';
import { schnorrkelKeypairFromU8a } from './keypair/fromU8a';
import { schnorrkelKeypairToU8a } from './keypair/toU8a';
export function schnorrkelDerive (keypair: Keypair, chainCode: HexString | Uint8Array | string, isHard: boolean): Keypair {
return schnorrkelKeypairFromU8a(
(isHard
? sr25519DeriveKeypairHard
: sr25519DeriveKeypairSoft
)(
schnorrkelKeypairToU8a(keypair),
u8aToU8a(chainCode)
)
);
export function createDeriveFn (derive: (pair: Uint8Array, cc: Uint8Array) => Uint8Array): (keypair: Keypair, chainCode: HexString | Uint8Array | string) => Keypair {
return (keypair: Keypair, chainCode: HexString | Uint8Array | string) =>
schnorrkelKeypairFromU8a(
derive(
schnorrkelKeypairToU8a(keypair),
u8aToU8a(chainCode)
)
);
}
@@ -1,11 +1,8 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Keypair } from '../types';
import { sr25519DeriveKeypairHard } from '@polkadot/wasm-crypto';
import { schnorrkelDerive } from './derive';
import { createDeriveFn } from './derive';
export function schnorrkelDeriveHard (keypair: Keypair, chainCode: HexString | Uint8Array | string): Keypair {
return schnorrkelDerive(keypair, chainCode, true);
}
export const schnorrkelDeriveHard = createDeriveFn(sr25519DeriveKeypairHard);
@@ -1,11 +1,8 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Keypair } from '../types';
import { sr25519DeriveKeypairSoft } from '@polkadot/wasm-crypto';
import { schnorrkelDerive } from './derive';
import { createDeriveFn } from './derive';
export function schnorrkelDeriveSoft (keypair: Keypair, chainCode: HexString | Uint8Array | string): Keypair {
return schnorrkelDerive(keypair, chainCode, false);
}
export const schnorrkelDeriveSoft = createDeriveFn(sr25519DeriveKeypairSoft);