mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-30 21:17:57 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
export declare function mnemonicToSeedSync(mnemonic: string, password?: string): Uint8Array;
|
||||
export declare function mnemonicToEntropy(mnemonic: string, wordlist?: string[]): Uint8Array;
|
||||
export declare function entropyToMnemonic(entropy: Uint8Array, wordlist?: string[]): string;
|
||||
export declare function generateMnemonic(numWords: 12 | 15 | 18 | 21 | 24, wordlist?: string[]): string;
|
||||
export declare function validateMnemonic(mnemonic: string, wordlist?: string[]): boolean;
|
||||
@@ -0,0 +1,82 @@
|
||||
import { stringToU8a, u8aToU8a } from '@pezkuwi/util';
|
||||
import { pbkdf2Encode } from '../pbkdf2/index.js';
|
||||
import { randomAsU8a } from '../random/index.js';
|
||||
import { sha256AsU8a } from '../sha/index.js';
|
||||
import DEFAULT_WORDLIST from './wordlists/en.js';
|
||||
const INVALID_MNEMONIC = 'Invalid mnemonic';
|
||||
const INVALID_ENTROPY = 'Invalid entropy';
|
||||
const INVALID_CHECKSUM = 'Invalid mnemonic checksum';
|
||||
/** @internal */
|
||||
function normalize(str) {
|
||||
return (str || '').normalize('NFKD');
|
||||
}
|
||||
/** @internal */
|
||||
function binaryToByte(bin) {
|
||||
return parseInt(bin, 2);
|
||||
}
|
||||
/** @internal */
|
||||
function bytesToBinary(bytes) {
|
||||
return bytes.map((x) => x.toString(2).padStart(8, '0')).join('');
|
||||
}
|
||||
/** @internal */
|
||||
function deriveChecksumBits(entropyBuffer) {
|
||||
return bytesToBinary(Array.from(sha256AsU8a(entropyBuffer))).slice(0, (entropyBuffer.length * 8) / 32);
|
||||
}
|
||||
export function mnemonicToSeedSync(mnemonic, password) {
|
||||
return pbkdf2Encode(stringToU8a(normalize(mnemonic)), stringToU8a(`mnemonic${normalize(password)}`)).password;
|
||||
}
|
||||
export function mnemonicToEntropy(mnemonic, wordlist = DEFAULT_WORDLIST) {
|
||||
const words = normalize(mnemonic).split(' ');
|
||||
if (words.length % 3 !== 0) {
|
||||
throw new Error(INVALID_MNEMONIC);
|
||||
}
|
||||
// convert word indices to 11 bit binary strings
|
||||
const bits = words
|
||||
.map((word) => {
|
||||
const index = wordlist.indexOf(word);
|
||||
if (index === -1) {
|
||||
throw new Error(INVALID_MNEMONIC);
|
||||
}
|
||||
return index.toString(2).padStart(11, '0');
|
||||
})
|
||||
.join('');
|
||||
// split the binary string into ENT/CS
|
||||
const dividerIndex = Math.floor(bits.length / 33) * 32;
|
||||
const entropyBits = bits.slice(0, dividerIndex);
|
||||
const checksumBits = bits.slice(dividerIndex);
|
||||
// calculate the checksum and compare
|
||||
const matched = entropyBits.match(/(.{1,8})/g);
|
||||
const entropyBytes = matched?.map(binaryToByte);
|
||||
if (!entropyBytes || (entropyBytes.length % 4 !== 0) || (entropyBytes.length < 16) || (entropyBytes.length > 32)) {
|
||||
throw new Error(INVALID_ENTROPY);
|
||||
}
|
||||
const entropy = u8aToU8a(entropyBytes);
|
||||
if (deriveChecksumBits(entropy) !== checksumBits) {
|
||||
throw new Error(INVALID_CHECKSUM);
|
||||
}
|
||||
return entropy;
|
||||
}
|
||||
export function entropyToMnemonic(entropy, wordlist = DEFAULT_WORDLIST) {
|
||||
// 128 <= ENT <= 256
|
||||
if ((entropy.length % 4 !== 0) || (entropy.length < 16) || (entropy.length > 32)) {
|
||||
throw new Error(INVALID_ENTROPY);
|
||||
}
|
||||
const matched = `${bytesToBinary(Array.from(entropy))}${deriveChecksumBits(entropy)}`.match(/(.{1,11})/g);
|
||||
const mapped = matched?.map((b) => wordlist[binaryToByte(b)]);
|
||||
if (!mapped || (mapped.length < 12)) {
|
||||
throw new Error('Unable to map entropy to mnemonic');
|
||||
}
|
||||
return mapped.join(' ');
|
||||
}
|
||||
export function generateMnemonic(numWords, wordlist) {
|
||||
return entropyToMnemonic(randomAsU8a((numWords / 3) * 4), wordlist);
|
||||
}
|
||||
export function validateMnemonic(mnemonic, wordlist) {
|
||||
try {
|
||||
mnemonicToEntropy(mnemonic, wordlist);
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name mnemonicGenerate
|
||||
* @summary Creates a valid mnemonic string using using [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { mnemonicGenerate } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* ```
|
||||
*/
|
||||
export declare function mnemonicGenerate(numWords?: 12 | 15 | 18 | 21 | 24, wordlist?: string[], onlyJs?: boolean): string;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { hasBigInt } from '@pezkuwi/util';
|
||||
import { bip39Generate, isReady } from '@pezkuwi/wasm-crypto';
|
||||
import { generateMnemonic } from './bip39.js';
|
||||
/**
|
||||
* @name mnemonicGenerate
|
||||
* @summary Creates a valid mnemonic string using using [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { mnemonicGenerate } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicGenerate(numWords = 12, wordlist, onlyJs) {
|
||||
return !hasBigInt || (!wordlist && !onlyJs && isReady())
|
||||
? bip39Generate(numWords)
|
||||
: generateMnemonic(numWords, wordlist);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @summary Create valid mnemonic strings, validate them using BIP39, and convert them to valid seeds
|
||||
*/
|
||||
export { mnemonicGenerate } from './generate.js';
|
||||
export { mnemonicToEntropy } from './toEntropy.js';
|
||||
export { mnemonicToLegacySeed } from './toLegacySeed.js';
|
||||
export { mnemonicToMiniSecret } from './toMiniSecret.js';
|
||||
export { mnemonicValidate } from './validate.js';
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @summary Create valid mnemonic strings, validate them using BIP39, and convert them to valid seeds
|
||||
*/
|
||||
export { mnemonicGenerate } from './generate.js';
|
||||
export { mnemonicToEntropy } from './toEntropy.js';
|
||||
export { mnemonicToLegacySeed } from './toLegacySeed.js';
|
||||
export { mnemonicToMiniSecret } from './toMiniSecret.js';
|
||||
export { mnemonicValidate } from './validate.js';
|
||||
@@ -0,0 +1 @@
|
||||
export declare function mnemonicToEntropy(mnemonic: string, wordlist?: string[], onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { hasBigInt } from '@pezkuwi/util';
|
||||
import { bip39ToEntropy, isReady } from '@pezkuwi/wasm-crypto';
|
||||
import { mnemonicToEntropy as jsToEntropy } from './bip39.js';
|
||||
export function mnemonicToEntropy(mnemonic, wordlist, onlyJs) {
|
||||
return !hasBigInt || (!wordlist && !onlyJs && isReady())
|
||||
? bip39ToEntropy(mnemonic)
|
||||
: jsToEntropy(mnemonic, wordlist);
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @name mnemonicToLegacySeed
|
||||
* @summary Creates a valid Ethereum/Bitcoin-compatible seed from a mnemonic input
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { mnemonicGenerate, mnemonicToLegacySeed, mnemonicValidate } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
||||
*
|
||||
* if (isValidMnemonic) {
|
||||
* console.log(`Seed generated from mnemonic: ${mnemonicToLegacySeed(mnemonic)}`); => u8a
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export declare function mnemonicToLegacySeed(mnemonic: string, password?: string, onlyJs?: boolean, byteLength?: 32 | 64): Uint8Array;
|
||||
@@ -0,0 +1,34 @@
|
||||
import { hasBigInt } from '@pezkuwi/util';
|
||||
import { bip39ToSeed, isReady } from '@pezkuwi/wasm-crypto';
|
||||
import { mnemonicToSeedSync } from './bip39.js';
|
||||
import { mnemonicValidate } from './validate.js';
|
||||
/**
|
||||
* @name mnemonicToLegacySeed
|
||||
* @summary Creates a valid Ethereum/Bitcoin-compatible seed from a mnemonic input
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { mnemonicGenerate, mnemonicToLegacySeed, mnemonicValidate } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
||||
*
|
||||
* if (isValidMnemonic) {
|
||||
* console.log(`Seed generated from mnemonic: ${mnemonicToLegacySeed(mnemonic)}`); => u8a
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicToLegacySeed(mnemonic, password = '', onlyJs, byteLength = 32) {
|
||||
if (!mnemonicValidate(mnemonic)) {
|
||||
throw new Error('Invalid bip39 mnemonic specified');
|
||||
}
|
||||
else if (![32, 64].includes(byteLength)) {
|
||||
throw new Error(`Invalid seed length ${byteLength}, expected 32 or 64`);
|
||||
}
|
||||
return byteLength === 32
|
||||
? !hasBigInt || (!onlyJs && isReady())
|
||||
? bip39ToSeed(mnemonic, password)
|
||||
: mnemonicToSeedSync(mnemonic, password).subarray(0, 32)
|
||||
: mnemonicToSeedSync(mnemonic, password);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export declare function mnemonicToMiniSecret(mnemonic: string, password?: string, wordlist?: string[], onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,17 @@
|
||||
import { stringToU8a } from '@pezkuwi/util';
|
||||
import { bip39ToMiniSecret, isReady } from '@pezkuwi/wasm-crypto';
|
||||
import { pbkdf2Encode } from '../pbkdf2/index.js';
|
||||
import { mnemonicToEntropy } from './toEntropy.js';
|
||||
import { mnemonicValidate } from './validate.js';
|
||||
export function mnemonicToMiniSecret(mnemonic, password = '', wordlist, onlyJs) {
|
||||
if (!mnemonicValidate(mnemonic, wordlist, onlyJs)) {
|
||||
throw new Error('Invalid bip39 mnemonic specified');
|
||||
}
|
||||
else if (!wordlist && !onlyJs && isReady()) {
|
||||
return bip39ToMiniSecret(mnemonic, password);
|
||||
}
|
||||
const entropy = mnemonicToEntropy(mnemonic, wordlist);
|
||||
const salt = stringToU8a(`mnemonic${password}`);
|
||||
// return the first 32 bytes as the seed
|
||||
return pbkdf2Encode(entropy, salt).password.slice(0, 32);
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @name mnemonicValidate
|
||||
* @summary Validates a mnemonic input using [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { mnemonicGenerate, mnemonicValidate } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
||||
* ```
|
||||
*/
|
||||
export declare function mnemonicValidate(mnemonic: string, wordlist?: string[], onlyJs?: boolean): boolean;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { hasBigInt } from '@pezkuwi/util';
|
||||
import { bip39Validate, isReady } from '@pezkuwi/wasm-crypto';
|
||||
import { validateMnemonic } from './bip39.js';
|
||||
/**
|
||||
* @name mnemonicValidate
|
||||
* @summary Validates a mnemonic input using [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { mnemonicGenerate, mnemonicValidate } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicValidate(mnemonic, wordlist, onlyJs) {
|
||||
return !hasBigInt || (!wordlist && !onlyJs && isReady())
|
||||
? bip39Validate(mnemonic)
|
||||
: validateMnemonic(mnemonic, wordlist);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,8 @@
|
||||
export { default as english } from './en.js';
|
||||
export { default as spanish } from './es.js';
|
||||
export { default as french } from './fr.js';
|
||||
export { default as italian } from './it.js';
|
||||
export { default as japanese } from './jp.js';
|
||||
export { default as korean } from './ko.js';
|
||||
export { default as chineseSimplified } from './zh-s.js';
|
||||
export { default as chineseTraditional } from './zh-t.js';
|
||||
@@ -0,0 +1,8 @@
|
||||
export { default as english } from './en.js';
|
||||
export { default as spanish } from './es.js';
|
||||
export { default as french } from './fr.js';
|
||||
export { default as italian } from './it.js';
|
||||
export { default as japanese } from './jp.js';
|
||||
export { default as korean } from './ko.js';
|
||||
export { default as chineseSimplified } from './zh-s.js';
|
||||
export { default as chineseTraditional } from './zh-t.js';
|
||||
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
declare const _default: string[];
|
||||
export default _default;
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user