UNPKG

@interchainjs/injective

Version:
89 lines (88 loc) 4.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_INJECTIVE_SIGNER_CONFIG = exports.encodeInjectivePublicKey = void 0; exports.createInjectiveSignerConfig = createInjectiveSignerConfig; exports.mergeInjectiveSignerOptions = mergeInjectiveSignerOptions; const signature_processor_1 = require("./signature-processor"); const deepmerge_1 = __importDefault(require("deepmerge")); const cosmos_types_1 = require("@interchainjs/cosmos-types"); /** * Encode public key for Injective * Uses the Injective-specific public key type URL */ const encodeInjectivePublicKey = (publicKey) => { return { typeUrl: '/injective.crypto.v1beta1.ethsecp256k1.PubKey', value: cosmos_types_1.CosmosCryptoSecp256k1PubKey.encode(cosmos_types_1.CosmosCryptoSecp256k1PubKey.fromPartial({ key: publicKey })).finish(), }; }; exports.encodeInjectivePublicKey = encodeInjectivePublicKey; /** * Default configuration for Injective signers * Provides Injective-specific defaults for fee calculation, signing options, and transaction options */ exports.DEFAULT_INJECTIVE_SIGNER_CONFIG = { // FeeOptions - Gas and fee calculation defaults for Injective multiplier: 1.5, // Higher gas multiplier for Injective due to EVM compatibility gasPrice: 'average', // Use average gas price from network // SignOptions - Injective-specific signing and address defaults addressPrefix: 'inj', // Injective address prefix message: { hash: 'keccak256' // Injective uses keccak256 for Ethereum compatibility }, // Signature format options - Injective-specific signature processing signature: { format: signature_processor_1.PRESET_INJECTIVE_SIGNATURE_FORMATS['compact'] }, // TxOptions - Transaction-level defaults unordered: false, // Ordered transactions by default extensionOptions: [], // No extension options by default nonCriticalExtensionOptions: [], // No non-critical extension options by default // Public key encoding - Injective specific encodePublicKey: exports.encodeInjectivePublicKey, pubkeyDecoders: { // @ts-ignore - Returns proto PubKey (has `key`) instead of amino Pubkey (has `type`/`value`). // Functionally correct for Injective e2e; the decoded pubkey isn't used for signing. '/injective.crypto.v1beta1.ethsecp256k1.PubKey': (pubkey) => { const { key } = cosmos_types_1.CosmosCryptoSecp256k1PubKey.decode(pubkey.value); return cosmos_types_1.CosmosCryptoSecp256k1PubKey.fromPartial({ key }); } } }; /** * Creates a complete Injective signer configuration by merging user-provided config with defaults * @param userConfig - User-provided configuration (must include required EndpointOptions) * @returns Complete CosmosSignerConfig with Injective defaults applied */ function createInjectiveSignerConfig(userConfig) { // Ensure required EndpointOptions are present if (!userConfig.queryClient) { throw new Error('queryClient is required in signer configuration'); } const queryClient = userConfig.queryClient; // Deep merge user config with Injective defaults, giving priority to user config const mergedConfig = (0, deepmerge_1.default)(exports.DEFAULT_INJECTIVE_SIGNER_CONFIG, userConfig, { // Custom merge function to handle arrays properly arrayMerge: (_destinationArray, sourceArray) => sourceArray, // Clone to avoid mutations clone: true }); mergedConfig.queryClient = queryClient; return mergedConfig; } /** * Creates a partial configuration for use in sign operations * Merges the base signer config with operation-specific options * @param baseConfig - Base signer configuration * @param operationOptions - Operation-specific options (from sign args) * @returns Merged configuration for the operation */ function mergeInjectiveSignerOptions(baseConfig, operationOptions = {}) { return (0, deepmerge_1.default)(baseConfig, operationOptions, { arrayMerge: (_destinationArray, sourceArray) => sourceArray, clone: true }); }