mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-07-14 10:45:41 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -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,90 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicToSeedSync = mnemonicToSeedSync;
|
||||
exports.mnemonicToEntropy = mnemonicToEntropy;
|
||||
exports.entropyToMnemonic = entropyToMnemonic;
|
||||
exports.generateMnemonic = generateMnemonic;
|
||||
exports.validateMnemonic = validateMnemonic;
|
||||
const tslib_1 = require("tslib");
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const index_js_1 = require("../pbkdf2/index.js");
|
||||
const index_js_2 = require("../random/index.js");
|
||||
const index_js_3 = require("../sha/index.js");
|
||||
const en_js_1 = tslib_1.__importDefault(require("./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((0, index_js_3.sha256AsU8a)(entropyBuffer))).slice(0, (entropyBuffer.length * 8) / 32);
|
||||
}
|
||||
function mnemonicToSeedSync(mnemonic, password) {
|
||||
return (0, index_js_1.pbkdf2Encode)((0, util_1.stringToU8a)(normalize(mnemonic)), (0, util_1.stringToU8a)(`mnemonic${normalize(password)}`)).password;
|
||||
}
|
||||
function mnemonicToEntropy(mnemonic, wordlist = en_js_1.default) {
|
||||
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 = (0, util_1.u8aToU8a)(entropyBytes);
|
||||
if (deriveChecksumBits(entropy) !== checksumBits) {
|
||||
throw new Error(INVALID_CHECKSUM);
|
||||
}
|
||||
return entropy;
|
||||
}
|
||||
function entropyToMnemonic(entropy, wordlist = en_js_1.default) {
|
||||
// 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(' ');
|
||||
}
|
||||
function generateMnemonic(numWords, wordlist) {
|
||||
return entropyToMnemonic((0, index_js_2.randomAsU8a)((numWords / 3) * 4), wordlist);
|
||||
}
|
||||
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,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicGenerate = mnemonicGenerate;
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const wasm_crypto_1 = require("@pezkuwi/wasm-crypto");
|
||||
const bip39_js_1 = require("./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
|
||||
* ```
|
||||
*/
|
||||
function mnemonicGenerate(numWords = 12, wordlist, onlyJs) {
|
||||
return !util_1.hasBigInt || (!wordlist && !onlyJs && (0, wasm_crypto_1.isReady)())
|
||||
? (0, wasm_crypto_1.bip39Generate)(numWords)
|
||||
: (0, bip39_js_1.generateMnemonic)(numWords, wordlist);
|
||||
}
|
||||
@@ -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,16 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicValidate = exports.mnemonicToMiniSecret = exports.mnemonicToLegacySeed = exports.mnemonicToEntropy = exports.mnemonicGenerate = void 0;
|
||||
/**
|
||||
* @summary Create valid mnemonic strings, validate them using BIP39, and convert them to valid seeds
|
||||
*/
|
||||
var generate_js_1 = require("./generate.js");
|
||||
Object.defineProperty(exports, "mnemonicGenerate", { enumerable: true, get: function () { return generate_js_1.mnemonicGenerate; } });
|
||||
var toEntropy_js_1 = require("./toEntropy.js");
|
||||
Object.defineProperty(exports, "mnemonicToEntropy", { enumerable: true, get: function () { return toEntropy_js_1.mnemonicToEntropy; } });
|
||||
var toLegacySeed_js_1 = require("./toLegacySeed.js");
|
||||
Object.defineProperty(exports, "mnemonicToLegacySeed", { enumerable: true, get: function () { return toLegacySeed_js_1.mnemonicToLegacySeed; } });
|
||||
var toMiniSecret_js_1 = require("./toMiniSecret.js");
|
||||
Object.defineProperty(exports, "mnemonicToMiniSecret", { enumerable: true, get: function () { return toMiniSecret_js_1.mnemonicToMiniSecret; } });
|
||||
var validate_js_1 = require("./validate.js");
|
||||
Object.defineProperty(exports, "mnemonicValidate", { enumerable: true, get: function () { return validate_js_1.mnemonicValidate; } });
|
||||
@@ -0,0 +1 @@
|
||||
export declare function mnemonicToEntropy(mnemonic: string, wordlist?: string[], onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicToEntropy = mnemonicToEntropy;
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const wasm_crypto_1 = require("@pezkuwi/wasm-crypto");
|
||||
const bip39_js_1 = require("./bip39.js");
|
||||
function mnemonicToEntropy(mnemonic, wordlist, onlyJs) {
|
||||
return !util_1.hasBigInt || (!wordlist && !onlyJs && (0, wasm_crypto_1.isReady)())
|
||||
? (0, wasm_crypto_1.bip39ToEntropy)(mnemonic)
|
||||
: (0, bip39_js_1.mnemonicToEntropy)(mnemonic, wordlist);
|
||||
}
|
||||
@@ -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,37 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicToLegacySeed = mnemonicToLegacySeed;
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const wasm_crypto_1 = require("@pezkuwi/wasm-crypto");
|
||||
const bip39_js_1 = require("./bip39.js");
|
||||
const validate_js_1 = require("./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
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
function mnemonicToLegacySeed(mnemonic, password = '', onlyJs, byteLength = 32) {
|
||||
if (!(0, validate_js_1.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
|
||||
? !util_1.hasBigInt || (!onlyJs && (0, wasm_crypto_1.isReady)())
|
||||
? (0, wasm_crypto_1.bip39ToSeed)(mnemonic, password)
|
||||
: (0, bip39_js_1.mnemonicToSeedSync)(mnemonic, password).subarray(0, 32)
|
||||
: (0, bip39_js_1.mnemonicToSeedSync)(mnemonic, password);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export declare function mnemonicToMiniSecret(mnemonic: string, password?: string, wordlist?: string[], onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicToMiniSecret = mnemonicToMiniSecret;
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const wasm_crypto_1 = require("@pezkuwi/wasm-crypto");
|
||||
const index_js_1 = require("../pbkdf2/index.js");
|
||||
const toEntropy_js_1 = require("./toEntropy.js");
|
||||
const validate_js_1 = require("./validate.js");
|
||||
function mnemonicToMiniSecret(mnemonic, password = '', wordlist, onlyJs) {
|
||||
if (!(0, validate_js_1.mnemonicValidate)(mnemonic, wordlist, onlyJs)) {
|
||||
throw new Error('Invalid bip39 mnemonic specified');
|
||||
}
|
||||
else if (!wordlist && !onlyJs && (0, wasm_crypto_1.isReady)()) {
|
||||
return (0, wasm_crypto_1.bip39ToMiniSecret)(mnemonic, password);
|
||||
}
|
||||
const entropy = (0, toEntropy_js_1.mnemonicToEntropy)(mnemonic, wordlist);
|
||||
const salt = (0, util_1.stringToU8a)(`mnemonic${password}`);
|
||||
// return the first 32 bytes as the seed
|
||||
return (0, index_js_1.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,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mnemonicValidate = mnemonicValidate;
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const wasm_crypto_1 = require("@pezkuwi/wasm-crypto");
|
||||
const bip39_js_1 = require("./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
|
||||
* ```
|
||||
*/
|
||||
function mnemonicValidate(mnemonic, wordlist, onlyJs) {
|
||||
return !util_1.hasBigInt || (!wordlist && !onlyJs && (0, wasm_crypto_1.isReady)())
|
||||
? (0, wasm_crypto_1.bip39Validate)(mnemonic)
|
||||
: (0, bip39_js_1.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,20 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.chineseTraditional = exports.chineseSimplified = exports.korean = exports.japanese = exports.italian = exports.french = exports.spanish = exports.english = void 0;
|
||||
const tslib_1 = require("tslib");
|
||||
var en_js_1 = require("./en.js");
|
||||
Object.defineProperty(exports, "english", { enumerable: true, get: function () { return tslib_1.__importDefault(en_js_1).default; } });
|
||||
var es_js_1 = require("./es.js");
|
||||
Object.defineProperty(exports, "spanish", { enumerable: true, get: function () { return tslib_1.__importDefault(es_js_1).default; } });
|
||||
var fr_js_1 = require("./fr.js");
|
||||
Object.defineProperty(exports, "french", { enumerable: true, get: function () { return tslib_1.__importDefault(fr_js_1).default; } });
|
||||
var it_js_1 = require("./it.js");
|
||||
Object.defineProperty(exports, "italian", { enumerable: true, get: function () { return tslib_1.__importDefault(it_js_1).default; } });
|
||||
var jp_js_1 = require("./jp.js");
|
||||
Object.defineProperty(exports, "japanese", { enumerable: true, get: function () { return tslib_1.__importDefault(jp_js_1).default; } });
|
||||
var ko_js_1 = require("./ko.js");
|
||||
Object.defineProperty(exports, "korean", { enumerable: true, get: function () { return tslib_1.__importDefault(ko_js_1).default; } });
|
||||
var zh_s_js_1 = require("./zh-s.js");
|
||||
Object.defineProperty(exports, "chineseSimplified", { enumerable: true, get: function () { return tslib_1.__importDefault(zh_s_js_1).default; } });
|
||||
var zh_t_js_1 = require("./zh-t.js");
|
||||
Object.defineProperty(exports, "chineseTraditional", { enumerable: true, get: function () { return tslib_1.__importDefault(zh_t_js_1).default; } });
|
||||
@@ -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