mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-29 14:57:57 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* @name addressToEvm
|
||||
* @summary Converts an SS58 address to its corresponding EVM address.
|
||||
*/
|
||||
export declare function addressToEvm(address: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { decodeAddress } from './decode.js';
|
||||
/**
|
||||
* @name addressToEvm
|
||||
* @summary Converts an SS58 address to its corresponding EVM address.
|
||||
*/
|
||||
export function addressToEvm(address, ignoreChecksum) {
|
||||
return decodeAddress(address, ignoreChecksum).subarray(0, 20);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { Prefix } from './types.js';
|
||||
/**
|
||||
* @name checkAddress
|
||||
* @summary Validates an ss58 address.
|
||||
* @description
|
||||
* From the provided input, validate that the address is a valid input.
|
||||
*/
|
||||
export declare function checkAddress(address: string, prefix: Prefix): [boolean, string | null];
|
||||
@@ -0,0 +1,26 @@
|
||||
import { base58Decode } from '../base58/index.js';
|
||||
import { checkAddressChecksum } from './checksum.js';
|
||||
import { defaults } from './defaults.js';
|
||||
/**
|
||||
* @name checkAddress
|
||||
* @summary Validates an ss58 address.
|
||||
* @description
|
||||
* From the provided input, validate that the address is a valid input.
|
||||
*/
|
||||
export function checkAddress(address, prefix) {
|
||||
let decoded;
|
||||
try {
|
||||
decoded = base58Decode(address);
|
||||
}
|
||||
catch (error) {
|
||||
return [false, error.message];
|
||||
}
|
||||
const [isValid, , , ss58Decoded] = checkAddressChecksum(decoded);
|
||||
if (ss58Decoded !== prefix) {
|
||||
return [false, `Prefix mismatch, expected ${prefix}, found ${ss58Decoded}`];
|
||||
}
|
||||
else if (!defaults.allowedEncodedLengths.includes(decoded.length)) {
|
||||
return [false, 'Invalid decoded address length'];
|
||||
}
|
||||
return [isValid, isValid ? null : 'Invalid decoded address checksum'];
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function checkAddressChecksum(decoded: Uint8Array): [boolean, number, number, number];
|
||||
@@ -0,0 +1,16 @@
|
||||
import { sshash } from './sshash.js';
|
||||
export function checkAddressChecksum(decoded) {
|
||||
const ss58Length = (decoded[0] & 0b0100_0000) ? 2 : 1;
|
||||
const ss58Decoded = ss58Length === 1
|
||||
? decoded[0]
|
||||
: ((decoded[0] & 0b0011_1111) << 2) | (decoded[1] >> 6) | ((decoded[1] & 0b0011_1111) << 8);
|
||||
// 32/33 bytes public + 2 bytes checksum + prefix
|
||||
const isPublicKey = [34 + ss58Length, 35 + ss58Length].includes(decoded.length);
|
||||
const length = decoded.length - (isPublicKey ? 2 : 1);
|
||||
// calculate the hash and do the checksum byte checks
|
||||
const hash = sshash(decoded.subarray(0, length));
|
||||
const isValid = (decoded[0] & 0b1000_0000) === 0 && ![46, 47].includes(decoded[0]) && (isPublicKey
|
||||
? decoded[decoded.length - 2] === hash[0] && decoded[decoded.length - 1] === hash[1]
|
||||
: decoded[decoded.length - 1] === hash[0]);
|
||||
return [isValid, length, ss58Length, ss58Decoded];
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Prefix } from './types.js';
|
||||
export declare function decodeAddress(encoded?: string | Uint8Array | null, ignoreChecksum?: boolean, ss58Format?: Prefix): Uint8Array;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { isHex, isU8a, u8aToU8a } from '@pezkuwi/util';
|
||||
import { base58Decode } from '../base58/index.js';
|
||||
import { checkAddressChecksum } from './checksum.js';
|
||||
import { defaults } from './defaults.js';
|
||||
export function decodeAddress(encoded, ignoreChecksum, ss58Format = -1) {
|
||||
if (!encoded) {
|
||||
throw new Error('Invalid empty address passed');
|
||||
}
|
||||
if (isU8a(encoded) || isHex(encoded)) {
|
||||
return u8aToU8a(encoded);
|
||||
}
|
||||
try {
|
||||
const decoded = base58Decode(encoded);
|
||||
if (!defaults.allowedEncodedLengths.includes(decoded.length)) {
|
||||
throw new Error('Invalid decoded address length');
|
||||
}
|
||||
const [isValid, endPos, ss58Length, ss58Decoded] = checkAddressChecksum(decoded);
|
||||
if (!isValid && !ignoreChecksum) {
|
||||
throw new Error('Invalid decoded address checksum');
|
||||
}
|
||||
else if (ss58Format !== -1 && ss58Format !== ss58Decoded) {
|
||||
throw new Error(`Expected ss58Format ${ss58Format}, received ${ss58Decoded}`);
|
||||
}
|
||||
return decoded.slice(ss58Length, endPos);
|
||||
}
|
||||
catch (error) {
|
||||
throw new Error(`Decoding ${encoded}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export declare const defaults: {
|
||||
allowedDecodedLengths: number[];
|
||||
allowedEncodedLengths: number[];
|
||||
allowedPrefix: number[];
|
||||
prefix: number;
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
import { availableNetworks } from '../networks.js';
|
||||
export const defaults = {
|
||||
allowedDecodedLengths: [1, 2, 4, 8, 32, 33],
|
||||
// publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte
|
||||
allowedEncodedLengths: [3, 4, 6, 10, 35, 36, 37, 38],
|
||||
allowedPrefix: availableNetworks.map(({ prefix }) => prefix),
|
||||
prefix: 42
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { Prefix } from './types.js';
|
||||
/**
|
||||
* @name deriveAddress
|
||||
* @summary Creates a sr25519 derived address from the supplied and path.
|
||||
* @description
|
||||
* Creates a sr25519 derived address based on the input address/publicKey and the uri supplied.
|
||||
*/
|
||||
export declare function deriveAddress(who: string | Uint8Array, suri: string, ss58Format?: Prefix): string;
|
||||
@@ -0,0 +1,24 @@
|
||||
import { keyExtractPath } from '../key/index.js';
|
||||
import { sr25519DerivePublic } from '../sr25519/index.js';
|
||||
import { decodeAddress } from './decode.js';
|
||||
import { encodeAddress } from './encode.js';
|
||||
function filterHard({ isHard }) {
|
||||
return isHard;
|
||||
}
|
||||
/**
|
||||
* @name deriveAddress
|
||||
* @summary Creates a sr25519 derived address from the supplied and path.
|
||||
* @description
|
||||
* Creates a sr25519 derived address based on the input address/publicKey and the uri supplied.
|
||||
*/
|
||||
export function deriveAddress(who, suri, ss58Format) {
|
||||
const { path } = keyExtractPath(suri);
|
||||
if (!path.length || path.every(filterHard)) {
|
||||
throw new Error('Expected suri to contain a combination of non-hard paths');
|
||||
}
|
||||
let publicKey = decodeAddress(who);
|
||||
for (const { chainCode } of path) {
|
||||
publicKey = sr25519DerivePublic(publicKey, chainCode);
|
||||
}
|
||||
return encodeAddress(publicKey, ss58Format);
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Prefix } from './types.js';
|
||||
export declare function encodeAddress(key: string | Uint8Array, ss58Format?: Prefix): string;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { u8aConcat } from '@pezkuwi/util';
|
||||
import { base58Encode } from '../base58/index.js';
|
||||
import { decodeAddress } from './decode.js';
|
||||
import { defaults } from './defaults.js';
|
||||
import { sshash } from './sshash.js';
|
||||
export function encodeAddress(key, ss58Format = defaults.prefix) {
|
||||
// decode it, this means we can re-encode an address
|
||||
const u8a = decodeAddress(key);
|
||||
if ((ss58Format < 0) || (ss58Format > 16383 && !ss58Exceptions.includes(ss58Format)) || [46, 47].includes(ss58Format)) {
|
||||
throw new Error('Out of range ss58Format specified');
|
||||
}
|
||||
else if (!defaults.allowedDecodedLengths.includes(u8a.length)) {
|
||||
throw new Error(`Expected a valid key to convert, with length ${defaults.allowedDecodedLengths.join(', ')}`);
|
||||
}
|
||||
const input = u8aConcat(ss58Format < 64
|
||||
? [ss58Format]
|
||||
: [
|
||||
((ss58Format & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000,
|
||||
(ss58Format >> 8) | ((ss58Format & 0b0000_0000_0000_0011) << 6)
|
||||
], u8a);
|
||||
return base58Encode(u8aConcat(input, sshash(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1)));
|
||||
}
|
||||
const ss58Exceptions = [29972];
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
import type { Prefix } from './types.js';
|
||||
/**
|
||||
* @name encodeDerivedAddress
|
||||
* @summary Creates a derived address as used in Substrate utility.
|
||||
* @description
|
||||
* Creates a Substrate derived address based on the input address/publicKey and the index supplied.
|
||||
*/
|
||||
export declare function encodeDerivedAddress(who: string | Uint8Array, index: bigint | BN | number, ss58Format?: Prefix): string;
|
||||
@@ -0,0 +1,12 @@
|
||||
import { decodeAddress } from './decode.js';
|
||||
import { encodeAddress } from './encode.js';
|
||||
import { createKeyDerived } from './keyDerived.js';
|
||||
/**
|
||||
* @name encodeDerivedAddress
|
||||
* @summary Creates a derived address as used in Substrate utility.
|
||||
* @description
|
||||
* Creates a Substrate derived address based on the input address/publicKey and the index supplied.
|
||||
*/
|
||||
export function encodeDerivedAddress(who, index, ss58Format) {
|
||||
return encodeAddress(createKeyDerived(decodeAddress(who), index), ss58Format);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
import type { Prefix } from './types.js';
|
||||
/**
|
||||
* @name encodeMultiAddress
|
||||
* @summary Creates a multisig address.
|
||||
* @description
|
||||
* Creates a Substrate multisig address based on the input address and the required threshold.
|
||||
*/
|
||||
export declare function encodeMultiAddress(who: (string | Uint8Array)[], threshold: bigint | BN | number, ss58Format?: Prefix): string;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { encodeAddress } from './encode.js';
|
||||
import { createKeyMulti } from './keyMulti.js';
|
||||
/**
|
||||
* @name encodeMultiAddress
|
||||
* @summary Creates a multisig address.
|
||||
* @description
|
||||
* Creates a Substrate multisig address based on the input address and the required threshold.
|
||||
*/
|
||||
export function encodeMultiAddress(who, threshold, ss58Format) {
|
||||
return encodeAddress(createKeyMulti(who, threshold), ss58Format);
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 '@pezkuwi/util';
|
||||
*
|
||||
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
|
||||
* ```
|
||||
*/
|
||||
export declare function addressEq(a: string | Uint8Array, b: string | Uint8Array): boolean;
|
||||
@@ -0,0 +1,19 @@
|
||||
import { u8aEq } from '@pezkuwi/util';
|
||||
import { decodeAddress } from './decode.js';
|
||||
/**
|
||||
* @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 '@pezkuwi/util';
|
||||
*
|
||||
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
|
||||
* ```
|
||||
*/
|
||||
export function addressEq(a, b) {
|
||||
return u8aEq(decodeAddress(a), decodeAddress(b));
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { HashType } from '../secp256k1/types.js';
|
||||
import type { Prefix } from './types.js';
|
||||
/**
|
||||
* @name evmToAddress
|
||||
* @summary Converts an EVM address to its corresponding SS58 address.
|
||||
*/
|
||||
export declare function evmToAddress(evmAddress: string | Uint8Array, ss58Format?: Prefix, hashType?: HashType): string;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { u8aConcat } from '@pezkuwi/util';
|
||||
import { hasher } from '../secp256k1/hasher.js';
|
||||
import { encodeAddress } from './encode.js';
|
||||
/**
|
||||
* @name evmToAddress
|
||||
* @summary Converts an EVM address to its corresponding SS58 address.
|
||||
*/
|
||||
export function evmToAddress(evmAddress, ss58Format, hashType = 'blake2') {
|
||||
const message = u8aConcat('evm:', evmAddress);
|
||||
if (message.length !== 24) {
|
||||
throw new Error(`Converting ${evmAddress}: Invalid evm address length`);
|
||||
}
|
||||
return encodeAddress(hasher(hashType, message), ss58Format);
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
export { addressToEvm } from './addressToEvm.js';
|
||||
export { checkAddress } from './check.js';
|
||||
export { checkAddressChecksum } from './checksum.js';
|
||||
export { decodeAddress } from './decode.js';
|
||||
export { deriveAddress } from './derive.js';
|
||||
export { encodeAddress } from './encode.js';
|
||||
export { encodeDerivedAddress } from './encodeDerived.js';
|
||||
export { encodeMultiAddress } from './encodeMulti.js';
|
||||
export { addressEq } from './eq.js';
|
||||
export { evmToAddress } from './evmToAddress.js';
|
||||
export { isAddress } from './is.js';
|
||||
export { createKeyDerived } from './keyDerived.js';
|
||||
export { createKeyMulti } from './keyMulti.js';
|
||||
export { sortAddresses } from './sort.js';
|
||||
export { validateAddress } from './validate.js';
|
||||
export { setSS58Format } from './setSS58Format.js';
|
||||
@@ -0,0 +1,16 @@
|
||||
export { addressToEvm } from './addressToEvm.js';
|
||||
export { checkAddress } from './check.js';
|
||||
export { checkAddressChecksum } from './checksum.js';
|
||||
export { decodeAddress } from './decode.js';
|
||||
export { deriveAddress } from './derive.js';
|
||||
export { encodeAddress } from './encode.js';
|
||||
export { encodeDerivedAddress } from './encodeDerived.js';
|
||||
export { encodeMultiAddress } from './encodeMulti.js';
|
||||
export { addressEq } from './eq.js';
|
||||
export { evmToAddress } from './evmToAddress.js';
|
||||
export { isAddress } from './is.js';
|
||||
export { createKeyDerived } from './keyDerived.js';
|
||||
export { createKeyMulti } from './keyMulti.js';
|
||||
export { sortAddresses } from './sort.js';
|
||||
export { validateAddress } from './validate.js';
|
||||
export { setSS58Format } from './setSS58Format.js';
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { Prefix } from './types.js';
|
||||
export declare function isAddress(address?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): address is string;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { validateAddress } from './validate.js';
|
||||
export function isAddress(address, ignoreChecksum, ss58Format) {
|
||||
try {
|
||||
return validateAddress(address, ignoreChecksum, ss58Format);
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
export declare function createKeyDerived(who: string | Uint8Array, index: bigint | BN | number): Uint8Array;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { bnToU8a, stringToU8a, u8aConcat } from '@pezkuwi/util';
|
||||
import { blake2AsU8a } from '../blake2/asU8a.js';
|
||||
import { BN_LE_16_OPTS } from '../bn.js';
|
||||
import { decodeAddress } from './decode.js';
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
export function createKeyDerived(who, index) {
|
||||
return blake2AsU8a(u8aConcat(PREFIX, decodeAddress(who), bnToU8a(index, BN_LE_16_OPTS)));
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
export declare function createKeyMulti(who: (string | Uint8Array)[], threshold: bigint | BN | number): Uint8Array;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@pezkuwi/util';
|
||||
import { blake2AsU8a } from '../blake2/asU8a.js';
|
||||
import { BN_LE_16_OPTS } from '../bn.js';
|
||||
import { addressToU8a } from './util.js';
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
export function createKeyMulti(who, threshold) {
|
||||
return blake2AsU8a(u8aConcat(PREFIX, compactToU8a(who.length), ...u8aSorted(who.map(addressToU8a)), bnToU8a(threshold, BN_LE_16_OPTS)));
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { Prefix } from './types.js';
|
||||
/**
|
||||
* @description Sets the global SS58 format to use for address encoding
|
||||
* @deprecated Use keyring.setSS58Format
|
||||
*/
|
||||
export declare function setSS58Format(prefix: Prefix): void;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { logger } from '@pezkuwi/util';
|
||||
import { defaults } from './defaults.js';
|
||||
const l = logger('setSS58Format');
|
||||
/**
|
||||
* @description Sets the global SS58 format to use for address encoding
|
||||
* @deprecated Use keyring.setSS58Format
|
||||
*/
|
||||
export function setSS58Format(prefix) {
|
||||
l.warn('Global setting of the ss58Format is deprecated and not recommended. Set format on the keyring (if used) or as part of the address encode function');
|
||||
defaults.prefix = prefix;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Prefix } from './types.js';
|
||||
export declare function sortAddresses(addresses: (string | Uint8Array)[], ss58Format?: Prefix): string[];
|
||||
@@ -0,0 +1,7 @@
|
||||
import { u8aSorted } from '@pezkuwi/util';
|
||||
import { encodeAddress } from './encode.js';
|
||||
import { addressToU8a } from './util.js';
|
||||
export function sortAddresses(addresses, ss58Format) {
|
||||
const u8aToAddress = (u8a) => encodeAddress(u8a, ss58Format);
|
||||
return u8aSorted(addresses.map(addressToU8a)).map(u8aToAddress);
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function sshash(key: Uint8Array): Uint8Array;
|
||||
@@ -0,0 +1,6 @@
|
||||
import { stringToU8a, u8aConcat } from '@pezkuwi/util';
|
||||
import { blake2AsU8a } from '../blake2/asU8a.js';
|
||||
const SS58_PREFIX = stringToU8a('SS58PRE');
|
||||
export function sshash(key) {
|
||||
return blake2AsU8a(u8aConcat(SS58_PREFIX, key), 512);
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export type Prefix = number;
|
||||
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function addressToU8a(who: string | Uint8Array): Uint8Array;
|
||||
@@ -0,0 +1,4 @@
|
||||
import { decodeAddress } from './decode.js';
|
||||
export function addressToU8a(who) {
|
||||
return decodeAddress(who);
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Prefix } from './types.js';
|
||||
export declare function validateAddress(encoded?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): encoded is string;
|
||||
@@ -0,0 +1,4 @@
|
||||
import { decodeAddress } from './decode.js';
|
||||
export function validateAddress(encoded, ignoreChecksum, ss58Format) {
|
||||
return !!decodeAddress(encoded, ignoreChecksum, ss58Format);
|
||||
}
|
||||
Reference in New Issue
Block a user