UNPKG

interchainjs

Version:

InterchainJS is a JavaScript library for interacting with Cosmos SDK based blockchains.

188 lines (187 loc) 7.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SOLANA_STD = exports.ETHEREUM_EIP1559 = exports.ETHEREUM_LEGACY = exports.COSMOS_DIRECT = exports.COSMOS_AMINO = void 0; exports.getSigner = getSigner; const cosmos_1 = require("@interchainjs/cosmos"); const ethereum_1 = require("@interchainjs/ethereum"); const solana_1 = require("@interchainjs/solana"); // Exported signer type constants exports.COSMOS_AMINO = 'cosmos_amino'; exports.COSMOS_DIRECT = 'cosmos_direct'; exports.ETHEREUM_LEGACY = 'ethereum_legacy'; exports.ETHEREUM_EIP1559 = 'ethereum_eip1559'; exports.SOLANA_STD = 'solana_std'; const SUPPORTED_SIGN_TYPES = [ exports.COSMOS_AMINO, exports.COSMOS_DIRECT, exports.ETHEREUM_LEGACY, exports.ETHEREUM_EIP1559, exports.SOLANA_STD ]; /** * Signer factory function that returns the appropriate signer instance * based on the preferred sign type and configuration options. * * @template T - The specific signer type that extends IUniSigner * @param walletOrSigner - Wallet instance or OfflineSigner for signing (Solana Keypair implements IWallet) * @param options - Configuration options including preferredSignType and signer-specific settings * @returns Configured signer instance of type T * @throws Error if the sign type is unsupported or required dependencies are missing * * @example * ```typescript * import { * getSigner, * COSMOS_DIRECT, * COSMOS_AMINO, * ETHEREUM_LEGACY, * ETHEREUM_EIP1559, * SOLANA_STD * } from '@interchainjs/interchain/core'; * * // Create a Cosmos direct signer with specific type using IWallet * const directSigner = getSigner<DirectSigner>(myWallet, { * preferredSignType: COSMOS_DIRECT, * signerOptions: { * queryClient: cosmosQueryClient, * chainId: 'cosmoshub-4', * addressPrefix: 'cosmos' * } * }); * * // Create an Amino signer with specific type using OfflineAminoSigner * const aminoOfflineSigner = await wallet.toOfflineAminoSigner(); * const aminoSigner = getSigner<AminoSigner>(aminoOfflineSigner, { * preferredSignType: COSMOS_AMINO, * signerOptions: { * queryClient: cosmosQueryClient, * chainId: 'osmosis-1', * addressPrefix: 'osmo' * } * }); * * // Create an Ethereum legacy signer with specific type * const legacySigner = getSigner<LegacyEthereumSigner>(myWallet, { * preferredSignType: ETHEREUM_LEGACY, * signerOptions: { * queryClient: ethereumQueryClient, * gasMultiplier: 1.2 * } * }); * * // Create a Solana signer using any IWallet (Solana Keypair implements IWallet) * const solanaSigner = getSigner<SolanaSigner>(myKeypair, { * preferredSignType: SOLANA_STD, * signerOptions: { * queryClient: solanaQueryClient, * commitment: 'confirmed' * } * }); * ``` */ function getSigner(walletOrSigner, options) { // Validate required parameters if (!walletOrSigner) { throw new Error('walletOrSigner is required'); } if (!options) { throw new Error('options are required'); } if (!options.signerOptions) { throw new Error('signerOptions are required'); } switch (options.preferredSignType) { case exports.COSMOS_AMINO: return createAminoSigner(walletOrSigner, options.signerOptions); case exports.COSMOS_DIRECT: return createDirectSigner(walletOrSigner, options.signerOptions); case exports.SOLANA_STD: return createSolanaSigner(walletOrSigner, options.signerOptions); case exports.ETHEREUM_LEGACY: return createLegacyEthereumSigner(walletOrSigner, options.signerOptions); case exports.ETHEREUM_EIP1559: return createEIP1559EthereumSigner(walletOrSigner, options.signerOptions); default: throw new Error(`Unsupported sign type: ${options.preferredSignType}. Supported types: ${SUPPORTED_SIGN_TYPES.join(', ')}`); } } /** * Creates a Cosmos Amino signer instance */ function createAminoSigner(walletOrSigner, signerOptions) { try { // Build configuration object from signerOptions const config = (0, cosmos_1.createCosmosSignerConfig)(signerOptions); return new cosmos_1.AminoSigner(walletOrSigner, config); } catch (error) { throw new Error(`Failed to create Amino signer: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure @interchainjs/cosmos is installed.`); } } /** * Creates a Cosmos Direct signer instance */ function createDirectSigner(walletOrSigner, signerOptions) { try { // Build configuration object from signerOptions const config = (0, cosmos_1.createCosmosSignerConfig)(signerOptions); return new cosmos_1.DirectSigner(walletOrSigner, config); } catch (error) { throw new Error(`Failed to create Direct signer: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure @interchainjs/cosmos is installed.`); } } /** * Creates a Solana signer instance */ function createSolanaSigner(walletOrSigner, signerOptions) { const config = signerOptions; if (!config?.queryClient) { throw new Error('Failed to create Solana signer: queryClient is required in signerOptions'); } if (isWalletAuth(walletOrSigner)) { return new solana_1.SolanaSigner(walletOrSigner, config); } throw new Error('Failed to create Solana signer: walletOrSigner must implement IWallet'); } /** * Creates an Ethereum Legacy signer instance */ function createLegacyEthereumSigner(walletOrSigner, signerOptions) { // Ethereum signers only work with IWallet, not OfflineSigner if (!('privateKeys' in walletOrSigner)) { throw new Error('Ethereum signers require IWallet, OfflineSigner is not supported'); } try { // Build configuration object from signerOptions const config = (0, ethereum_1.createEthereumSignerConfig)(signerOptions); return new ethereum_1.LegacyEthereumSigner(walletOrSigner, config); } catch (error) { throw new Error(`Failed to create Legacy Ethereum signer: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure @interchainjs/ethereum is installed.`); } } /** * Creates an Ethereum EIP-1559 signer instance */ function createEIP1559EthereumSigner(walletOrSigner, signerOptions) { // Ethereum signers only work with IWallet, not OfflineSigner if (!('privateKeys' in walletOrSigner)) { throw new Error('Ethereum signers require IWallet, OfflineSigner is not supported'); } try { // Build configuration object from signerOptions const config = (0, ethereum_1.createEthereumSignerConfig)(signerOptions); return new ethereum_1.EIP1559EthereumSigner(walletOrSigner, config); } catch (error) { throw new Error(`Failed to create EIP-1559 Ethereum signer: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure @interchainjs/ethereum is installed.`); } } function isWalletAuth(value) { if (!value || typeof value !== 'object') { return false; } const candidate = value; return typeof candidate.getAccounts === 'function' && typeof candidate.signByIndex === 'function'; }