@interchainjs/injective
Version:
78 lines (77 loc) • 3.74 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EthSecp256k1HDWallet = void 0;
const types_1 = require("@interchainjs/types");
const cosmos_1 = require("@interchainjs/cosmos");
const bip39 = __importStar(require("bip39"));
const auth_1 = require("@interchainjs/auth");
const config_1 = require("../auth/config");
const deepmerge_1 = __importDefault(require("deepmerge"));
/**
* HD Wallet implementation for secp256k1 with Ethereum-style address derivation for Injective
* 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
*/
class EthSecp256k1HDWallet extends cosmos_1.Secp256k1HDWallet {
constructor(privateKeys, config) {
const preset = (0, config_1.createInjectiveEthConfig)(config?.derivations, config?.privateKeyConfig?.passphrase);
const mergedConfig = (0, deepmerge_1.default)(preset, 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 presetInjectiveConfig = (0, config_1.createInjectiveEthConfig)(config?.derivations, config?.privateKeyConfig?.passphrase);
const walletConfig = (0, deepmerge_1.default)(presetInjectiveConfig, config || {});
const privateKeyConfig = walletConfig.privateKeyConfig;
const hdPaths = config?.derivations?.map((derivation) => types_1.HDPath.fromString(derivation.hdPath)) || [types_1.HDPath.eth(0, 0, 0)];
// Use PrivateKey.fromMnemonic to create private keys
const privateKeys = await auth_1.PrivateKey.fromMnemonic(mnemonic, hdPaths, privateKeyConfig);
return new EthSecp256k1HDWallet(privateKeys, walletConfig);
}
}
exports.EthSecp256k1HDWallet = EthSecp256k1HDWallet;