@kraken-crypto/ccxt
Version:
A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go
51 lines (50 loc) • 1.61 kB
JavaScript
import { HDKey } from '../scure-bip32/index.js';
import { entropyToMnemonic, mnemonicToSeedSync } from '../scure-bip39/index.js';
import { wordlist } from '../scure-bip39/wordlists/english.js';
/**
* @description Get Mnemonic and priv/pub keys from privateKeyBytes and BIP44 HD path
*
* @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
*
* @param entropy used to generate mnemonic
*
* @param path BIP44 HD Path. Default is The Cosmos Hub path
*
* @throws Error if the hdkey does not exist
*
* @returns Mnemonic and priv/pub keys
*/
export const exportMnemonicAndPrivateKey = (entropy, path = "m/44'/118'/0'/0/0") => {
const mnemonic = entropyToMnemonic(entropy, wordlist);
const { privateKey, publicKey } = deriveHDKeyFromMnemonic(mnemonic, path);
return {
mnemonic,
privateKey,
publicKey,
};
};
/**
* @description Derive priv/pub keys from mnemonic and BIP44 HD path
*
* @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
*
* @param mnemonic used to generate seed
*
* @param path BIP44 HD Path. Default is The Cosmos Hub path
*
* @throws Error if the hdkey does not exist
*
* @returns Priv/pub keys
*/
export const deriveHDKeyFromMnemonic = (mnemonic, path = "m/44'/118'/0'/0/0") => {
const seed = mnemonicToSeedSync(mnemonic);
const hdkey = HDKey.fromMasterSeed(seed);
const derivedHdkey = hdkey.derive(path);
if (!hdkey.privateKey) {
throw new Error('null hd key');
}
return {
privateKey: derivedHdkey.privateKey,
publicKey: derivedHdkey.publicKey,
};
};