Bump dev (w/ ts-eslint updates) (#1854)
* Bump dev (w/ ts-eslint updates) * Adjust ipfs compat check
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/keyring authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { EncryptedJsonEncoding, Keypair, KeypairType } from '@polkadot/util-crypto/types';
|
||||
import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types.js';
|
||||
import type { PairInfo } from './types.js';
|
||||
@@ -165,7 +164,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
setMeta: (additional: KeyringPair$Meta): void => {
|
||||
meta = objectSpread({}, meta, additional);
|
||||
},
|
||||
sign: (message: HexString | string | Uint8Array, options: SignOptions = {}): Uint8Array => {
|
||||
sign: (message: string | Uint8Array, options: SignOptions = {}): Uint8Array => {
|
||||
if (isLocked(secretKey)) {
|
||||
throw new Error('Cannot sign with a locked key pair');
|
||||
}
|
||||
@@ -192,10 +191,10 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
unlock: (passphrase?: string): void => {
|
||||
return decodePkcs8(passphrase);
|
||||
},
|
||||
verify: (message: HexString | string | Uint8Array, signature: HexString | string | Uint8Array, signerPublic: HexString | string | Uint8Array): boolean => {
|
||||
verify: (message: string | Uint8Array, signature: string | Uint8Array, signerPublic: string | Uint8Array): boolean => {
|
||||
return signatureVerify(message, signature, TYPE_ADDRESS[type](u8aToU8a(signerPublic))).isValid;
|
||||
},
|
||||
vrfSign: (message: HexString | string | Uint8Array, context?: HexString | string | Uint8Array, extra?: string | Uint8Array): Uint8Array => {
|
||||
vrfSign: (message: string | Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array => {
|
||||
if (isLocked(secretKey)) {
|
||||
throw new Error('Cannot sign with a locked key pair');
|
||||
}
|
||||
@@ -208,7 +207,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
|
||||
return u8aConcat(vrfHash(proof, context, extra), proof);
|
||||
},
|
||||
vrfVerify: (message: HexString | string | Uint8Array, vrfResult: Uint8Array, signerPublic: HexString | Uint8Array | string, context?: HexString | string | Uint8Array, extra?: HexString | string | Uint8Array): boolean => {
|
||||
vrfVerify: (message: string | Uint8Array, vrfResult: Uint8Array, signerPublic: Uint8Array | string, context?: string | Uint8Array, extra?: string | Uint8Array): boolean => {
|
||||
if (type === 'sr25519') {
|
||||
return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/keyring authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { KeyringPair, KeyringPairs } from './types.js';
|
||||
|
||||
import { isHex, isU8a, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
@@ -22,7 +21,7 @@ export class Pairs implements KeyringPairs {
|
||||
return Object.values(this.#map);
|
||||
}
|
||||
|
||||
public get (address: HexString | string | Uint8Array): KeyringPair {
|
||||
public get (address: string | Uint8Array): KeyringPair {
|
||||
const pair = this.#map[decodeAddress(address).toString()];
|
||||
|
||||
if (!pair) {
|
||||
@@ -36,7 +35,7 @@ export class Pairs implements KeyringPairs {
|
||||
return pair;
|
||||
}
|
||||
|
||||
public remove (address: HexString | string | Uint8Array): void {
|
||||
public remove (address: string | Uint8Array): void {
|
||||
delete this.#map[decodeAddress(address).toString()];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ function createMeta (name?: string, seed?: string) {
|
||||
|
||||
return {
|
||||
isTesting: true,
|
||||
name: name || (seed && seed.replace('//', '_').toLowerCase())
|
||||
name: name || seed?.replace('//', '_').toLowerCase()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -42,9 +42,7 @@ describe('testingPairs', (): void => {
|
||||
const keyring = new Keyring({ type: 'ethereum' });
|
||||
const pair = keyring.createFromUri('0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133');
|
||||
|
||||
expect(
|
||||
pair && pair.address
|
||||
).toEqual(TEST_ADD);
|
||||
expect(pair?.address).toEqual(TEST_ADD);
|
||||
});
|
||||
|
||||
describe('checks eth test addresses', (): void => {
|
||||
|
||||
@@ -47,7 +47,9 @@ export function createTestPairs <O extends KeyringOptions, M = DetectMap<O>> (op
|
||||
const map: TestKeyringMap = { nobody: nobody() };
|
||||
|
||||
for (const p of pairs) {
|
||||
map[p.meta.name as string] = p;
|
||||
if (p.meta.name) {
|
||||
map[p.meta.name] = p;
|
||||
}
|
||||
}
|
||||
|
||||
return map as M;
|
||||
|
||||
@@ -70,7 +70,7 @@ export interface KeyringPair$Meta extends KeyringPair$MetaExtension, KeyringPair
|
||||
|
||||
export interface KeyringPair$Json extends EncryptedJson {
|
||||
/** The ss58 encoded address or the hex-encoded version (the latter is for ETH-compat chains) */
|
||||
address: string | HexString;
|
||||
address: string;
|
||||
/** The underlying metadata associated with the keypair */
|
||||
meta: KeyringPair$Meta;
|
||||
}
|
||||
@@ -95,12 +95,12 @@ export interface KeyringPair {
|
||||
encodePkcs8 (passphrase?: string): Uint8Array;
|
||||
lock (): void;
|
||||
setMeta (meta: KeyringPair$Meta): void;
|
||||
sign (message: HexString | string | Uint8Array, options?: SignOptions): Uint8Array;
|
||||
sign (message: string | Uint8Array, options?: SignOptions): Uint8Array;
|
||||
toJson (passphrase?: string): KeyringPair$Json;
|
||||
unlock (passphrase?: string): void;
|
||||
verify (message: HexString | string | Uint8Array, signature: Uint8Array, signerPublic: HexString | string | Uint8Array): boolean;
|
||||
vrfSign (message: HexString | string | Uint8Array, context?: HexString | string | Uint8Array, extra?: HexString | string | Uint8Array): Uint8Array;
|
||||
vrfVerify (message: HexString | string | Uint8Array, vrfResult: Uint8Array, signerPublic: HexString | Uint8Array | string, context?: HexString | string | Uint8Array, extra?: HexString | string | Uint8Array): boolean;
|
||||
verify (message: string | Uint8Array, signature: Uint8Array, signerPublic: string | Uint8Array): boolean;
|
||||
vrfSign (message: string | Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array;
|
||||
vrfVerify (message: string | Uint8Array, vrfResult: Uint8Array, signerPublic: string | Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): boolean;
|
||||
}
|
||||
|
||||
export interface KeyringPairs {
|
||||
|
||||
@@ -29,8 +29,8 @@ function toExpanded (o: KnownSubstrate): SubstrateNetwork {
|
||||
n.isIgnored = n.isTestnet || (
|
||||
!(
|
||||
o.standardAccount &&
|
||||
o.decimals && o.decimals.length &&
|
||||
o.symbols && o.symbols.length
|
||||
o.decimals?.length &&
|
||||
o.symbols?.length
|
||||
) &&
|
||||
o.prefix !== 42
|
||||
);
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { decodeAddress } from './decode.js';
|
||||
|
||||
/**
|
||||
* @name addressToEvm
|
||||
* @summary Converts an SS58 address to its corresponding EVM address.
|
||||
*/
|
||||
export function addressToEvm (address: HexString | string | Uint8Array, ignoreChecksum?: boolean): Uint8Array {
|
||||
export function addressToEvm (address: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array {
|
||||
return decodeAddress(address, ignoreChecksum).subarray(0, 20);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
import { base58Decode } from '../base58/index.js';
|
||||
@@ -14,7 +13,7 @@ import { defaults } from './defaults.js';
|
||||
* @description
|
||||
* From the provided input, validate that the address is a valid input.
|
||||
*/
|
||||
export function checkAddress (address: HexString | string, prefix: Prefix): [boolean, string | null] {
|
||||
export function checkAddress (address: string, prefix: Prefix): [boolean, string | null] {
|
||||
let decoded;
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
|
||||
@@ -11,7 +10,7 @@ import { base58Decode } from '../base58/index.js';
|
||||
import { checkAddressChecksum } from './checksum.js';
|
||||
import { defaults } from './defaults.js';
|
||||
|
||||
export function decodeAddress (encoded?: HexString | string | Uint8Array | null, ignoreChecksum?: boolean, ss58Format: Prefix = -1): Uint8Array {
|
||||
export function decodeAddress (encoded?: string | Uint8Array | null, ignoreChecksum?: boolean, ss58Format: Prefix = -1): Uint8Array {
|
||||
if (!encoded) {
|
||||
throw new Error('Invalid empty address passed');
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { DeriveJunction } from '../key/DeriveJunction.js';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
@@ -20,7 +19,7 @@ function filterHard ({ isHard }: DeriveJunction): boolean {
|
||||
* @description
|
||||
* Creates a sr25519 derived address based on the input address/publicKey and the uri supplied.
|
||||
*/
|
||||
export function deriveAddress (who: HexString | Uint8Array | string, suri: string, ss58Format?: Prefix): string {
|
||||
export function deriveAddress (who: string | Uint8Array, suri: string, ss58Format?: Prefix): string {
|
||||
const { path } = keyExtractPath(suri);
|
||||
|
||||
if (!path.length || path.every(filterHard)) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
||||
@@ -12,7 +11,7 @@ import { decodeAddress } from './decode.js';
|
||||
import { defaults } from './defaults.js';
|
||||
import { sshash } from './sshash.js';
|
||||
|
||||
export function encodeAddress (key: HexString | Uint8Array | string, ss58Format: Prefix = defaults.prefix): string {
|
||||
export function encodeAddress (key: string | Uint8Array, ss58Format: Prefix = defaults.prefix): string {
|
||||
// decode it, this means we can re-encode an address
|
||||
const u8a = decodeAddress(key);
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
import { decodeAddress } from './decode.js';
|
||||
@@ -15,6 +14,6 @@ import { createKeyDerived } from './keyDerived.js';
|
||||
* @description
|
||||
* Creates a Substrate derived address based on the input address/publicKey and the index supplied.
|
||||
*/
|
||||
export function encodeDerivedAddress (who: HexString | Uint8Array | string, index: bigint | BN | number, ss58Format?: Prefix): string {
|
||||
export function encodeDerivedAddress (who: string | Uint8Array, index: bigint | BN | number, ss58Format?: Prefix): string {
|
||||
return encodeAddress(createKeyDerived(decodeAddress(who), index), ss58Format);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
import { encodeAddress } from './encode.js';
|
||||
@@ -14,6 +13,6 @@ import { createKeyMulti } from './keyMulti.js';
|
||||
* @description
|
||||
* Creates a Substrate multisig address based on the input address and the required threshold.
|
||||
*/
|
||||
export function encodeMultiAddress (who: (HexString | Uint8Array | string)[], threshold: bigint | BN | number, ss58Format?: Prefix): string {
|
||||
export function encodeMultiAddress (who: (string | Uint8Array)[], threshold: bigint | BN | number, ss58Format?: Prefix): string {
|
||||
return encodeAddress(createKeyMulti(who, threshold), ss58Format);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aEq } from '@polkadot/util';
|
||||
|
||||
import { decodeAddress } from './decode.js';
|
||||
@@ -21,6 +19,6 @@ import { decodeAddress } from './decode.js';
|
||||
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
|
||||
* ```
|
||||
*/
|
||||
export function addressEq (a: HexString | string | Uint8Array, b: HexString | string | Uint8Array): boolean {
|
||||
export function addressEq (a: string | Uint8Array, b: string | Uint8Array): boolean {
|
||||
return u8aEq(decodeAddress(a), decodeAddress(b));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { HashType } from '../secp256k1/types.js';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
@@ -14,7 +13,7 @@ import { encodeAddress } from './encode.js';
|
||||
* @name evmToAddress
|
||||
* @summary Converts an EVM address to its corresponding SS58 address.
|
||||
*/
|
||||
export function evmToAddress (evmAddress: HexString | string | Uint8Array, ss58Format?: Prefix, hashType: HashType = 'blake2'): string {
|
||||
export function evmToAddress (evmAddress: string | Uint8Array, ss58Format?: Prefix, hashType: HashType = 'blake2'): string {
|
||||
const message = u8aConcat('evm:', evmAddress);
|
||||
|
||||
if (message.length !== 24) {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
import { validateAddress } from './validate.js';
|
||||
|
||||
export function isAddress (address?: HexString | string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): address is string {
|
||||
export function isAddress (address?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): address is string {
|
||||
try {
|
||||
return validateAddress(address, ignoreChecksum, ss58Format);
|
||||
} catch {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
@@ -12,7 +11,7 @@ import { decodeAddress } from './decode.js';
|
||||
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
|
||||
export function createKeyDerived (who: HexString | Uint8Array | string, index: bigint | BN | number): Uint8Array {
|
||||
export function createKeyDerived (who: string | Uint8Array, index: bigint | BN | number): Uint8Array {
|
||||
return blake2AsU8a(
|
||||
u8aConcat(
|
||||
PREFIX,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
|
||||
|
||||
@@ -12,7 +11,7 @@ import { addressToU8a } from './util.js';
|
||||
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
|
||||
export function createKeyMulti (who: (HexString | Uint8Array | string)[], threshold: bigint | BN | number): Uint8Array {
|
||||
export function createKeyMulti (who: (string | Uint8Array)[], threshold: bigint | BN | number): Uint8Array {
|
||||
return blake2AsU8a(
|
||||
u8aConcat(
|
||||
PREFIX,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
import { u8aSorted } from '@polkadot/util';
|
||||
@@ -9,7 +8,7 @@ import { u8aSorted } from '@polkadot/util';
|
||||
import { encodeAddress } from './encode.js';
|
||||
import { addressToU8a } from './util.js';
|
||||
|
||||
export function sortAddresses (addresses: (HexString | Uint8Array | string)[], ss58Format?: Prefix): string[] {
|
||||
export function sortAddresses (addresses: (string | Uint8Array)[], ss58Format?: Prefix): string[] {
|
||||
const u8aToAddress = (u8a: Uint8Array) => encodeAddress(u8a, ss58Format);
|
||||
|
||||
return u8aSorted(
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types.js';
|
||||
|
||||
import { decodeAddress } from './decode.js';
|
||||
|
||||
export function validateAddress (encoded?: HexString | string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): encoded is string {
|
||||
export function validateAddress (encoded?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): encoded is string {
|
||||
return !!decodeAddress(encoded, ignoreChecksum, ss58Format);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export function createValidate ({ chars, ipfs, type, withPadding }: Config): Val
|
||||
return (value?: unknown, ipfsCompat?: boolean): value is string => {
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error(`Expected ${type} string input`);
|
||||
} else if (ipfs && ipfsCompat && value[0] !== ipfs) {
|
||||
} else if (ipfs && ipfsCompat && !value.startsWith(ipfs)) {
|
||||
throw new Error(`Expected ipfs-compatible ${type} to start with '${ipfs}'`);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* @description Trims padding characters
|
||||
*/
|
||||
export function base64Trim (value: string): string {
|
||||
while (value.length && value[value.length - 1] === '=') {
|
||||
while (value.length && value.endsWith('=')) {
|
||||
value = value.slice(0, -1);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { blake2b as blake2bJs } from '@noble/hashes/blake2b';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
@@ -24,7 +22,7 @@ import { createAsHex } from '../helpers.js';
|
||||
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
|
||||
* ```
|
||||
*/
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||
export function blake2AsU8a (data: string | Uint8Array, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import { ed25519 } from '@noble/curves/ed25519';
|
||||
@@ -23,7 +22,7 @@ import { ed25519Sign as wasmSign, isReady } from '@polkadot/wasm-crypto';
|
||||
* ed25519Sign([...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function ed25519Sign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>, onlyJs?: boolean): Uint8Array {
|
||||
export function ed25519Sign (message: string | Uint8Array, { publicKey, secretKey }: Partial<Keypair>, onlyJs?: boolean): Uint8Array {
|
||||
if (!secretKey) {
|
||||
throw new Error('Expected a valid secretKey');
|
||||
} else if (!publicKey) {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { ed25519 } from '@noble/curves/ed25519';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
@@ -22,7 +20,7 @@ import { ed25519Verify as wasmVerify, isReady } from '@polkadot/wasm-crypto';
|
||||
* ed25519Verify([...], [...], [...]); // => true/false
|
||||
* ```
|
||||
*/
|
||||
export function ed25519Verify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string, onlyJs?: boolean): boolean {
|
||||
export function ed25519Verify (message: string | Uint8Array, signature: string | Uint8Array, publicKey: string | Uint8Array, onlyJs?: boolean): boolean {
|
||||
const messageU8a = u8aToU8a(message);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
@@ -16,7 +16,7 @@ function getH160 (u8a: Uint8Array): Uint8Array {
|
||||
return u8a.slice(-20);
|
||||
}
|
||||
|
||||
export function ethereumEncode (addressOrPublic?: HexString | string | Uint8Array): HexString {
|
||||
export function ethereumEncode (addressOrPublic?: string | Uint8Array): HexString {
|
||||
if (!addressOrPublic) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { isHex } from '@polkadot/util';
|
||||
|
||||
import { isEthereumChecksum } from './isChecksum.js';
|
||||
|
||||
export function isEthereumAddress (address?: HexString | string): boolean {
|
||||
export function isEthereumAddress (address?: string): boolean {
|
||||
if (!address || address.length !== 42 || !isHex(address)) {
|
||||
return false;
|
||||
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
|
||||
|
||||
@@ -21,14 +21,14 @@ export function createAsHex <T extends (...args: never[]) => Uint8Array> (fn: T)
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function createBitHasher (bitLength: 256 | 512, fn: (data: HexString | Uint8Array | string, bitLength: 256 | 512, onlyJs?: boolean) => Uint8Array): (data: HexString | Uint8Array | string, onlyJs?: boolean) => Uint8Array {
|
||||
return (data: HexString | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
export function createBitHasher (bitLength: 256 | 512, fn: (data: string | Uint8Array, bitLength: 256 | 512, onlyJs?: boolean) => Uint8Array): (data: string | Uint8Array, onlyJs?: boolean) => Uint8Array {
|
||||
return (data: string | Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||
fn(data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function createDualHasher (wa: DualHash, js: DualHash): (value: HexString | Uint8Array | string, bitLength?: 256 | 512, onlyJs?: boolean) => Uint8Array {
|
||||
return (value: HexString | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array => {
|
||||
export function createDualHasher (wa: DualHash, js: DualHash): (value: string | Uint8Array, bitLength?: 256 | 512, onlyJs?: boolean) => Uint8Array {
|
||||
return (value: string | Uint8Array, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array => {
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
export type EncryptedJsonVersion = '0' | '1' | '2' | '3';
|
||||
|
||||
export type EncryptedJsonEncoding = 'none' | 'scrypt' | 'xsalsa20-poly1305';
|
||||
@@ -14,6 +12,6 @@ export interface EncryptedJsonDescriptor {
|
||||
}
|
||||
|
||||
export interface EncryptedJson {
|
||||
encoded: HexString | string;
|
||||
encoded: string;
|
||||
encoding: EncryptedJsonDescriptor;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
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.js';
|
||||
@@ -46,7 +44,7 @@ export class DeriveJunction {
|
||||
return !this.#isHard;
|
||||
}
|
||||
|
||||
public hard (value: HexString | number | string | bigint | BN | Uint8Array): DeriveJunction {
|
||||
public hard (value: number | string | bigint | BN | Uint8Array): DeriveJunction {
|
||||
return this.soft(value).harden();
|
||||
}
|
||||
|
||||
@@ -56,7 +54,7 @@ export class DeriveJunction {
|
||||
return this;
|
||||
}
|
||||
|
||||
public soft (value: HexString | number | string | bigint | BN | Uint8Array): DeriveJunction {
|
||||
public soft (value: number | string | bigint | BN | Uint8Array): DeriveJunction {
|
||||
if (isNumber(value) || isBn(value) || isBigInt(value)) {
|
||||
return this.soft(bnToU8a(value, BN_LE_256_OPTS));
|
||||
} else if (isHex(value)) {
|
||||
|
||||
@@ -81,7 +81,7 @@ export function mnemonicToEntropy (mnemonic: string, wordlist: string[] = DEFAUL
|
||||
|
||||
// calculate the checksum and compare
|
||||
const matched = entropyBits.match(/(.{1,8})/g);
|
||||
const entropyBytes = matched && matched.map(binaryToByte);
|
||||
const entropyBytes = matched?.map(binaryToByte);
|
||||
|
||||
if (!entropyBytes || (entropyBytes.length % 4 !== 0) || (entropyBytes.length < 16) || (entropyBytes.length > 32)) {
|
||||
throw new Error(INVALID_ENTROPY);
|
||||
@@ -103,7 +103,7 @@ export function entropyToMnemonic (entropy: Uint8Array, wordlist: string[] = DEF
|
||||
}
|
||||
|
||||
const matched = `${bytesToBinary(Array.from(entropy))}${deriveChecksumBits(entropy)}`.match(/(.{1,11})/g);
|
||||
const mapped = matched && matched.map((b) => wordlist[binaryToByte(b)]);
|
||||
const mapped = matched?.map((b) => wordlist[binaryToByte(b)]);
|
||||
|
||||
if (!mapped || (mapped.length < 12)) {
|
||||
throw new Error('Unable to map entropy to mnemonic');
|
||||
|
||||
@@ -136,7 +136,7 @@ describe('tweetnacl/secretbox', (): void => {
|
||||
});
|
||||
|
||||
describe('tweetnacl/test/04-secretbox.js', (): void => {
|
||||
for (let i = 0; i < TEST_DATA.length; i++) {
|
||||
for (let i = 0, count = TEST_DATA.length; i < count; i++) {
|
||||
const [keyBase64, nonceBase64, msgBase64, expBase64] = TEST_DATA[i];
|
||||
|
||||
it(`is ok on "${msgBase64}"`, (): void => {
|
||||
|
||||
@@ -958,7 +958,7 @@ function checkLengths (k: Uint8Array, n: Uint8Array): void {
|
||||
// }
|
||||
|
||||
function checkArrayTypes (...args: unknown[]): void {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
for (let i = 0, count = args.length; i < count; i++) {
|
||||
if (!(args[i] instanceof Uint8Array))
|
||||
throw new TypeError('unexpected type, use Uint8Array');
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { pbkdf2 as pbkdf2Js } from '@noble/hashes/pbkdf2';
|
||||
import { sha512 } from '@noble/hashes/sha512';
|
||||
|
||||
@@ -17,7 +15,7 @@ interface Result {
|
||||
salt: Uint8Array;
|
||||
}
|
||||
|
||||
export function pbkdf2Encode (passphrase?: HexString | Uint8Array | string, salt: Uint8Array = randomAsU8a(), rounds = 2048, onlyJs?: boolean): Result {
|
||||
export function pbkdf2Encode (passphrase?: string | Uint8Array, salt: Uint8Array = randomAsU8a(), rounds = 2048, onlyJs?: boolean): Result {
|
||||
const u8aPass = u8aToU8a(passphrase);
|
||||
const u8aSalt = u8aToU8a(salt);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Params } from './types.js';
|
||||
|
||||
import { scrypt as scryptJs } from '@noble/hashes/scrypt';
|
||||
@@ -18,7 +17,7 @@ interface Result {
|
||||
salt: Uint8Array;
|
||||
}
|
||||
|
||||
export function scryptEncode (passphrase?: HexString | Uint8Array | string, salt = randomAsU8a(), params = DEFAULT_PARAMS, onlyJs?: boolean): Result {
|
||||
export function scryptEncode (passphrase?: string | Uint8Array, salt = randomAsU8a(), params = DEFAULT_PARAMS, onlyJs?: boolean): Result {
|
||||
const u8a = u8aToU8a(passphrase);
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { HashType } from './types.js';
|
||||
|
||||
import { secp256k1 } from '@noble/curves/secp256k1';
|
||||
@@ -16,7 +15,7 @@ import { secp256k1Expand } from './expand.js';
|
||||
* @name secp256k1Recover
|
||||
* @description Recovers a publicKey from the supplied signature
|
||||
*/
|
||||
export function secp256k1Recover (msgHash: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, recovery: number, hashType: HashType = 'blake2', onlyJs?: boolean): Uint8Array {
|
||||
export function secp256k1Recover (msgHash: string | Uint8Array, signature: string | Uint8Array, recovery: number, hashType: HashType = 'blake2', onlyJs?: boolean): Uint8Array {
|
||||
const sig = u8aToU8a(signature).subarray(0, 64);
|
||||
const msg = u8aToU8a(msgHash);
|
||||
const publicKey = !hasBigInt || (!onlyJs && isReady())
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { HashType } from './types.js';
|
||||
|
||||
import { u8aEq, u8aToU8a } from '@polkadot/util';
|
||||
@@ -13,7 +12,7 @@ import { secp256k1Recover } from './recover.js';
|
||||
* @name secp256k1Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function secp256k1Verify (msgHash: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, address: HexString | Uint8Array | string, hashType: HashType = 'blake2', onlyJs?: boolean): boolean {
|
||||
export function secp256k1Verify (msgHash: string | Uint8Array, signature: string | Uint8Array, address: string | Uint8Array, hashType: HashType = 'blake2', onlyJs?: boolean): boolean {
|
||||
const sig = u8aToU8a(signature);
|
||||
|
||||
if (sig.length !== 65) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { KeypairType, VerifyResult } from '../types.js';
|
||||
|
||||
import { u8aIsWrapped, u8aToU8a, u8aUnwrapBytes, u8aWrapBytes } from '@polkadot/util';
|
||||
@@ -87,7 +86,7 @@ function getVerifyFn (signature: Uint8Array): VerifyFn {
|
||||
: verifyDetect;
|
||||
}
|
||||
|
||||
export function signatureVerify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, addressOrPublicKey: HexString | Uint8Array | string): VerifyResult {
|
||||
export function signatureVerify (message: string | Uint8Array, signature: string | Uint8Array, addressOrPublicKey: string | Uint8Array): VerifyResult {
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
if (![64, 65, 66].includes(signatureU8a.length)) {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Agree } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -10,7 +8,7 @@ import { sr25519Agree } from '@polkadot/wasm-crypto';
|
||||
* @name sr25519Agreement
|
||||
* @description Key agreement between other's public key and self secret key
|
||||
*/
|
||||
export function sr25519Agreement (secretKey: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string): Uint8Array {
|
||||
export function sr25519Agreement (secretKey: string | Uint8Array, publicKey: string | Uint8Array): Uint8Array {
|
||||
const secretKeyU8a = u8aToU8a(secretKey);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519DerivePublicSoft } from '@polkadot/wasm-crypto';
|
||||
|
||||
export function sr25519DerivePublic (publicKey: HexString | Uint8Array | string, chainCode: Uint8Array): Uint8Array {
|
||||
export function sr25519DerivePublic (publicKey: string | Uint8Array, chainCode: Uint8Array): Uint8Array {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
|
||||
if (!isU8a(chainCode) || chainCode.length !== 32) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Keypair } from '../../types.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
@@ -13,7 +12,7 @@ import { sr25519PairFromU8a } from './fromU8a.js';
|
||||
* @name sr25519PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export function sr25519PairFromSeed (seed: HexString | Uint8Array | string): Keypair {
|
||||
export function sr25519PairFromSeed (seed: string | Uint8Array): Keypair {
|
||||
const seedU8a = u8aToU8a(seed);
|
||||
|
||||
if (seedU8a.length !== 32) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Keypair } from '../../types.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
@@ -10,7 +9,7 @@ const SEC_LEN = 64;
|
||||
const PUB_LEN = 32;
|
||||
const TOT_LEN = SEC_LEN + PUB_LEN;
|
||||
|
||||
export function sr25519PairFromU8a (full: HexString | Uint8Array | string): Keypair {
|
||||
export function sr25519PairFromU8a (full: string | Uint8Array): Keypair {
|
||||
const fullU8a = u8aToU8a(full);
|
||||
|
||||
if (fullU8a.length !== TOT_LEN) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
@@ -11,7 +10,7 @@ import { sr25519Sign as wasmSign } from '@polkadot/wasm-crypto';
|
||||
* @name sr25519Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function sr25519Sign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
export function sr25519Sign (message: string | Uint8Array, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
if (publicKey?.length !== 32) {
|
||||
throw new Error('Expected a valid publicKey, 32-bytes');
|
||||
} else if (secretKey?.length !== 64) {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Verify as wasmVerify } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -10,7 +8,7 @@ import { sr25519Verify as wasmVerify } from '@polkadot/wasm-crypto';
|
||||
* @name sr25519Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function sr25519Verify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string): boolean {
|
||||
export function sr25519Verify (message: string | Uint8Array, signature: string | Uint8Array, publicKey: string | Uint8Array): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
@@ -13,7 +12,7 @@ const EMPTY_U8A = new Uint8Array();
|
||||
* @name sr25519VrfSign
|
||||
* @description Sign with sr25519 vrf signing (deterministic)
|
||||
*/
|
||||
export function sr25519VrfSign (message: HexString | Uint8Array | string, { secretKey }: Partial<Keypair>, context: HexString | string | Uint8Array = EMPTY_U8A, extra: HexString | string | Uint8Array = EMPTY_U8A): Uint8Array {
|
||||
export function sr25519VrfSign (message: string | Uint8Array, { secretKey }: Partial<Keypair>, context: string | Uint8Array = EMPTY_U8A, extra: string | Uint8Array = EMPTY_U8A): Uint8Array {
|
||||
if (secretKey?.length !== 64) {
|
||||
throw new Error('Invalid secretKey, expected 64-bytes');
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { vrfVerify } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -12,7 +10,7 @@ const EMPTY_U8A = new Uint8Array();
|
||||
* @name sr25519VrfVerify
|
||||
* @description Verify with sr25519 vrf verification
|
||||
*/
|
||||
export function sr25519VrfVerify (message: HexString | Uint8Array | string, signOutput: HexString | string | Uint8Array, publicKey: HexString | Uint8Array | string, context: HexString | string | Uint8Array = EMPTY_U8A, extra: HexString | string | Uint8Array = EMPTY_U8A): boolean {
|
||||
export function sr25519VrfVerify (message: string | Uint8Array, signOutput: string | Uint8Array, publicKey: string | Uint8Array, context: string | Uint8Array = EMPTY_U8A, extra: string | Uint8Array = EMPTY_U8A): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const proofU8a = u8aToU8a(signOutput);
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, twox } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -23,7 +21,7 @@ import { xxhash64 } from './xxhash64.js';
|
||||
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
||||
* ```
|
||||
*/
|
||||
export function xxhashAsU8a (data: HexString | Uint8Array | string, bitLength: 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512 = 64, onlyJs?: boolean): Uint8Array {
|
||||
export function xxhashAsU8a (data: string | Uint8Array, bitLength: 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512 = 64, onlyJs?: boolean): Uint8Array {
|
||||
const rounds = Math.ceil(bitLength / 64);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { HexString, ToBigInt, ToBn } from '../types.js';
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
@@ -16,7 +16,7 @@ import { isToBn } from '../is/toBn.js';
|
||||
* @name nToBigInt
|
||||
* @summary Creates a bigInt value from a BN, bigint, string (base 10 or hex) or number input.
|
||||
*/
|
||||
export function nToBigInt <ExtToBn extends ToBigInt | ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
export function nToBigInt <ExtToBn extends ToBigInt | ToBn> (value?: ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
return typeof value === 'bigint'
|
||||
? value
|
||||
: !value
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString, ToBigInt, ToBn } from '../types.js';
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
|
||||
import { hexToBn } from '../hex/toBn.js';
|
||||
import { isBigInt } from '../is/bigInt.js';
|
||||
@@ -26,7 +26,7 @@ import { BN } from './bn.js';
|
||||
* bnToBn(new BN(0x1234)); // => BN(0x1234)
|
||||
* ```
|
||||
*/
|
||||
export function bnToBn <ExtToBn extends ToBigInt | ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): BN {
|
||||
export function bnToBn <ExtToBn extends ToBigInt | ToBn> (value?: ExtToBn | BN | bigint | string | number | null): BN {
|
||||
return value
|
||||
? BN.isBN(value)
|
||||
? value
|
||||
|
||||
@@ -24,7 +24,7 @@ function formatValue (elapsed: number): string {
|
||||
* @description Formats an elapsed value into s, m, h or day segments
|
||||
*/
|
||||
export function formatElapsed <ExtToBn extends ToBn> (now?: Date | null, value?: bigint | BN | ExtToBn | Date | number | null): string {
|
||||
const tsNow = (now && now.getTime()) || 0;
|
||||
const tsNow = now?.getTime() || 0;
|
||||
const tsValue = value instanceof Date
|
||||
? value.getTime()
|
||||
: bnToBn(value).toNumber();
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
const CHR = '0123456789abcdef';
|
||||
const U8 = new Uint8Array(256);
|
||||
const U16 = new Uint8Array(256 * 256);
|
||||
@@ -38,7 +36,7 @@ for (let i = 0; i < 256; i++) {
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export function hexToU8a (value?: HexString | string | null, bitLength = -1): Uint8Array {
|
||||
export function hexToU8a (value?: string | null, bitLength = -1): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
@@ -18,7 +16,7 @@ import type { HexString } from '../types.js';
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export function hexToU8a (_value?: HexString | string | null, bitLength = -1): Uint8Array {
|
||||
export function hexToU8a (_value?: string | null, bitLength = -1): Uint8Array {
|
||||
if (!_value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@
|
||||
* @name isArray
|
||||
* @summary Tests for a Array instance.
|
||||
*/
|
||||
export function isArray <T> (value?: unknown): value is Array<T> {
|
||||
export function isArray <T> (value?: unknown): value is T[] {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -3,10 +3,8 @@
|
||||
|
||||
import { stringify } from '../stringify.js';
|
||||
|
||||
interface ObjectIndexed {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
[index: string]: any;
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ObjectIndexed = Record<string, any>;
|
||||
|
||||
/**
|
||||
* @name isJsonObject
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
interface ObjectIndexed {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
[index: string]: any;
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ObjectIndexed = Record<string, any>;
|
||||
|
||||
/**
|
||||
* @name isObject
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
// Adapted from https://github.com/JulienPalard/is_utf8/blob/master/is_utf8.c
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
import { u8aToU8a } from '../u8a/toU8a.js';
|
||||
import { isString } from './string.js';
|
||||
|
||||
@@ -14,7 +12,7 @@ import { isString } from './string.js';
|
||||
* @description
|
||||
* Checks to see if the input string or Uint8Array is valid Utf8
|
||||
*/
|
||||
export function isUtf8 (value?: HexString | number[] | Uint8Array | string | null): boolean {
|
||||
export function isUtf8 (value?: number[] | Uint8Array | string | null): boolean {
|
||||
if (!value) {
|
||||
return isString(value);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
type AnyFn = (...args: unknown[]) => unknown;
|
||||
|
||||
type WithToString = { toString: () => string };
|
||||
interface WithToString { toString: () => string }
|
||||
|
||||
/**
|
||||
* @name lazyMethod
|
||||
|
||||
@@ -40,7 +40,7 @@ export function memoize <T, F extends (...args: any[]) => T> (fn: F, { getInstan
|
||||
const stringParams = stringify(args);
|
||||
const instanceId = getInstanceId();
|
||||
|
||||
if (cache[instanceId] && cache[instanceId][stringParams] !== undefined) {
|
||||
if (cache[instanceId]?.[stringParams] !== undefined) {
|
||||
delete cache[instanceId][stringParams];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -34,7 +34,7 @@ describe('objectProperty/objectProperties', (): void => {
|
||||
b = 1;
|
||||
|
||||
get a () {
|
||||
return 1;
|
||||
return 1 + 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ export interface BufferObjClass extends Class<BufferObj> {
|
||||
isBuffer: (value: unknown) => boolean;
|
||||
}
|
||||
|
||||
export type U8aLike = HexString | number[] | Uint8Array | AnyString;
|
||||
export type U8aLike = number[] | Uint8Array | AnyString;
|
||||
|
||||
export interface Observable {
|
||||
next: (...params: unknown[]) => unknown;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
import { u8aToU8a } from './toU8a.js';
|
||||
|
||||
/**
|
||||
@@ -21,7 +19,7 @@ import { u8aToU8a } from './toU8a.js';
|
||||
* u8aCmp(new Uint8Array([0x69, 0x65]), new Uint8Array([0x68, 0x65])); // +1
|
||||
* ```
|
||||
*/
|
||||
export function u8aCmp (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): number {
|
||||
export function u8aCmp (a: string | Uint8Array, b: string | Uint8Array): number {
|
||||
const u8aa = u8aToU8a(a);
|
||||
const u8ab = u8aToU8a(b);
|
||||
let i = 0;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
import { u8aToU8a } from './toU8a.js';
|
||||
|
||||
/**
|
||||
@@ -19,7 +17,7 @@ import { u8aToU8a } from './toU8a.js';
|
||||
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
|
||||
* ```
|
||||
*/
|
||||
export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): boolean {
|
||||
export function u8aEq (a: string | Uint8Array, b: string | Uint8Array): boolean {
|
||||
const u8aa = u8aToU8a(a);
|
||||
const u8ab = u8aToU8a(b);
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed
|
||||
? '0x'
|
||||
: '' as HexString;
|
||||
|
||||
if (!value || !value.length) {
|
||||
if (!value?.length) {
|
||||
return empty;
|
||||
} else if (bitLength > 0) {
|
||||
const length = Math.ceil(bitLength / 8);
|
||||
|
||||
@@ -21,7 +21,7 @@ export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed
|
||||
const length = Math.ceil(bitLength / 8);
|
||||
|
||||
return `${isPrefixed ? '0x' : ''}${
|
||||
!value || !value.length
|
||||
!value?.length
|
||||
? ''
|
||||
: (bitLength > 0 && value.length > length)
|
||||
? `${Buffer.from(value.subarray(0, length / 2)).toString('hex')}…${Buffer.from(value.subarray(value.length - length / 2)).toString('hex')}`
|
||||
|
||||
@@ -21,7 +21,7 @@ async function nodeFetch (...args: Parameters<typeof fetch>): Promise<Response>
|
||||
|
||||
const mod = await importFetch;
|
||||
|
||||
if (!mod || !mod.default) {
|
||||
if (!mod?.default) {
|
||||
throw new Error('Unable to import node-fetch in this environment');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user