UNPKG

@xpla/xpla

Version:

<p align="center"> <img src="https://user-images.githubusercontent.com/545047/188804067-28e67e5e-0214-4449-ab04-2e0c564a6885.svg" width="80"> </p>

38 lines (36 loc) 1.95 kB
import { COSMOS_EVM_ADDRESS_STRATEGY } from "../auth/strategy.js"; import { createCosmosEvmConfig } from "../auth/config.js"; import { HDPath } from "@interchainjs/types"; import { Secp256k1HDWallet } from "@interchainjs/cosmos/wallets/secp256k1hd"; import * as bip39 from "bip39"; import { PrivateKey, registerAddressStrategy } from "@interchainjs/auth"; import deepmerge from "deepmerge"; //#region src/wallets/ethSecp256k1hd.ts registerAddressStrategy(COSMOS_EVM_ADDRESS_STRATEGY); /** * HD Wallet implementation for secp256k1 with Ethereum-style address derivation for CosmosEvm * Extends Secp256k1HDWallet from Cosmos for consistent wallet behavior * Uses proper HD derivation with configurable derivation paths * Uses keccak256 hashing for address generation instead of standard Cosmos approach */ var EthSecp256k1HDWallet = class EthSecp256k1HDWallet extends Secp256k1HDWallet { constructor(privateKeys, config) { const mergedConfig = deepmerge(createCosmosEvmConfig(config?.derivations, config?.privateKeyConfig?.passphrase), config || {}); super(privateKeys, mergedConfig); } /** * Create wallet from mnemonic with derivation paths from config * @param mnemonic BIP39 mnemonic phrase * @param config Wallet configuration including derivation paths and address prefix * @returns Promise<EthSecp256k1HDWallet> instance */ static async fromMnemonic(mnemonic, config) { if (!bip39.validateMnemonic(mnemonic)) throw new Error("Invalid mnemonic"); const walletConfig = deepmerge(createCosmosEvmConfig(config?.derivations, config?.privateKeyConfig?.passphrase), config || {}); const privateKeyConfig = walletConfig.privateKeyConfig; const hdPaths = config?.derivations?.map((derivation) => HDPath.fromString(derivation.hdPath)) || [HDPath.eth(0, 0, 0)]; return new EthSecp256k1HDWallet(await PrivateKey.fromMnemonic(mnemonic, hdPaths, privateKeyConfig), walletConfig); } }; //#endregion export { EthSecp256k1HDWallet };