@sei-js/cosmjs
Version:
TypeScript library for CosmJS interactions on the Sei blockchain
62 lines (61 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.restoreWallet = exports.generateWallet = exports.getHdPath = void 0;
const crypto_1 = require("@cosmjs/crypto");
const proto_signing_1 = require("@cosmjs/proto-signing");
/**
* Gets the Hierarchical deterministic Cosmos Hub/Atom path for the accountIndex
* @param accountIndex The account index
* @param coinType The coin type to use when deriving the path (118 for Cosmos, 60 for EVM)
* @returns A HdPath object
* @category Wallets (Advanced)
*/
const getHdPath = (accountIndex = 0, coinType = 118) => {
const stringPath = `m/44'/${coinType}'/0'/0/${accountIndex}`;
return (0, crypto_1.stringToPath)(stringPath);
};
exports.getHdPath = getHdPath;
/**
* Creates a DirectSecp256K1HdWallet object given an HD path (see #getHDPath).
*
* @example
* ```tsx
* import { generateWallet, restoreWallet } from "@sei-js/cosmjs";
*
* // 12 word mnemonic by default
* const generatedWallet = await generateWallet(); // has optional parameter for account index
* console.log('generated mnemonic', generatedWallet.mnemonic);
* ```
*
* @param mnemonicLength The length of the mnemonic phrase to generate with the wallet.
* @param hdPath hdPath for the wallet you want to generate.
* @returns A DirectSecp256k1HdWallet object representing a newly generated wallet.
* @category Wallets (Advanced)
*/
const generateWallet = async (mnemonicLength = 12, hdPath) => {
return await proto_signing_1.DirectSecp256k1HdWallet.generate(mnemonicLength, {
prefix: 'sei',
hdPaths: [hdPath || (0, exports.getHdPath)(0)]
});
};
exports.generateWallet = generateWallet;
/**
* Uses the given mnemonic phrase and hd path to re-generate a wallet.
*
* @example
* ```tsx
* import { generateWallet, restoreWallet } from "@sei-js/cosmjs";
*
* const restoredWallet = await restoreWallet(SEED_PHRASE); // has optional parameter for account index
* console.log('restored mnemonic', restoredWallet.mnemonic);
* ```
*
* @param seedPhrase The mnemonic phrase created with the wallet
* @param hdPath hdPath for the wallet you want to restore.
* @returns A DirectSecp256k1HdWallet object representing an existing wallet.
* @category Wallets (Advanced)
*/
const restoreWallet = async (seedPhrase, hdPath) => {
return await proto_signing_1.DirectSecp256k1HdWallet.fromMnemonic(seedPhrase, { prefix: 'sei', hdPaths: [hdPath || (0, exports.getHdPath)(0)] });
};
exports.restoreWallet = restoreWallet;