/** * Copyright (c) 2025 weimeme * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ApiPromise } from "@polkadot/api"; import { KeyringPair } from "@polkadot/keyring/types"; import { NativeTokenInfo, TransferDetailWithMemo } from "../types"; import { SubscanApi } from "./subscan-api"; import { type ICryptMessage, type KeypairType } from "@eliza-dot-aes/common"; /** * Main client class for interacting with Substrate-based blockchains. * Provides methods for address validation, key management, balance queries, * transfers with encrypted memos, and message sending. * Supports both native token and asset transfers with optional memo encryption. */ export declare class SubstrateChain { rpcUrl: string; private privateKey; api: ApiPromise; ss58Format: number; chainName: string; cryptMessage: ICryptMessage | null; isEthereum: boolean; nativeToken: NativeTokenInfo; subscanApi: SubscanApi | null; keyPairType: KeypairType; private constructor(); /** * Factory method to create and initialize a SubstrateChain instance. * This is the preferred way to create a SubstrateChain as it ensures proper initialization. * * @param rpcUrl - The RPC endpoint URL of the Substrate chain * @param privateKey - The private key (hex string) for signing transactions * @param subscanApi - Optional SubscanApi instance for querying transaction memos * @param cryptMessage - Optional encryption service for memo encryption/decryption * @returns Promise resolving to an initialized SubstrateChain instance * @throws Error if initialization fails */ static create(rpcUrl: string, privateKey: string, keypairType?: KeypairType, subscanApi?: SubscanApi | null, cryptMessage?: ICryptMessage | null): Promise; /** * Asynchronously initializes the Substrate API connection and chain properties. * Fetches chain information including chain name, SS58 format, token details, and Ethereum compatibility. * Must be called before using any other methods. * * @throws Error if the RPC connection fails or chain properties cannot be retrieved */ private init; /** * Updates the RPC endpoint URL and reconnects to the chain. * Disconnects the existing API connection before establishing a new one. * * @param rpcUrl - The new RPC endpoint URL * @throws Error if the new RPC connection fails */ updateRpcUrl(rpcUrl: string): Promise; /** * Checks if the connected chain is Ethereum-compatible. * * @returns Promise resolving to true if the chain is Ethereum-compatible, false otherwise */ isEthereumChain(): Promise; /** * Updates the private key used for signing transactions. * * @param privateKey - The new private key (hex string) * @returns Promise resolving to the updated private key */ updatePrivateKey(privateKey: string): Promise; /** * Validates a Substrate address format using the chain's SS58 format. * Ethereum-compatible chains are not supported. * * @param address - The address string to validate * @returns true if the address is valid, false otherwise */ validateAddress(address: string): boolean; /** * Extracts the public key from a Substrate address. * Ethereum-compatible chains are not supported. * * @param address - The Substrate address * @returns Promise resolving to the public key as a hex string, or empty string if Ethereum chain * @throws Error if the address is invalid or extraction fails */ getAddressPublicKey(address: string): Promise; /** * Derives the address from the stored private key. * Uses the chain's SS58 format for address encoding. * * @returns Promise resolving to the derived address * @throws Error if key derivation fails */ getMyAddress(): Promise; /** * Gets the SS58 address format used by this chain. * * @returns The SS58 format number */ getSs58Format(): number; /** * Gets the name of the connected chain. * * @returns The chain name as a string */ getChainName(): string; /** * Gets the current RPC endpoint URL. * * @returns The RPC URL string */ getRpcUrl(): string; getUserBalance(address: string, assetId?: number | null): Promise; /** * Creates a keyring pair from the stored private key. * Used for signing transactions. * * @returns Promise resolving to the KeyringPair instance * @throws Error if key pair creation fails */ getMyKeyPair(): Promise; /** * Retrieves and decrypts the memo message from a transfer transaction. * Fetches transaction details via Subscan API and attempts to decrypt the memo if encrypted. * Returns empty string if no memo is found or if decryption fails. * * @param txHash - The transaction hash to query * @returns Promise resolving to the decrypted memo string, or empty string if not found * @throws Error if SubscanApi is not initialized, transaction not found, or decryption fails */ getTransferMemo(txHash: string): Promise; /** * Encrypts a memo message using the recipient's public key. * This is a private helper method used internally for memo encryption. * Ethereum-compatible chains are not supported. * * @param memo - The plaintext memo message to encrypt * @param to - The recipient address * @param toPublicKey - Optional recipient public key (will be derived from address if not provided) * @returns Promise resolving to EncryptedMemo object, or null if encryption fails or Ethereum chain */ private encryptMemo; /** * Decrypts an encrypted memo message using the current account's private key. * This is a private helper method used internally for memo decryption. * Ethereum-compatible chains are not supported. * * @param memo - The EncryptedMemo object containing the encrypted message * @returns Promise resolving to the decrypted memo string, or empty string if decryption fails */ /** * Performs a native token transfer with an optional encrypted memo. * If memo is provided, encrypts it and includes it in a batch transaction with the transfer. * The memo is encrypted using the recipient's public key so only they can decrypt it. * * @param to - The recipient address * @param amount - The transfer amount (can be 0 for message-only transfers) * @param memo - Optional memo message to encrypt and attach to the transfer * @returns Promise resolving to the transaction hash * @throws Error if address is invalid or transaction fails */ transferWithMemo(to: string, amount: bigint, memo?: string | null): Promise; /** * Sends an encrypted message to a recipient without transferring tokens. * Performs a zero-amount transfer with the message as an encrypted memo. * * @param to - The recipient address * @param message - The message content to encrypt and send * @returns Promise resolving to the transaction hash * @throws Error if message is null or transaction fails */ sendMessage(to: string, message: string): Promise; /** * Performs an asset transfer with an optional encrypted memo. * If assetId is null, performs a native token transfer instead. * If memo is provided, encrypts it and includes it in a batch transaction with the transfer. * * @param to - The recipient address * @param amount - The transfer amount (must be greater than 0 for asset transfers) * @param assetId - The asset ID to transfer (null for native token) * @param memo - Optional memo message to encrypt and attach to the transfer * @returns Promise resolving to the transaction hash * @throws Error if amount is 0, address is invalid, or transaction fails */ assetsTransferWithMemo(to: string, amount: bigint, assetId: number | null, memo?: string | null): Promise; /** * Retrieves the latest finalized block height from the chain. * * @returns Promise resolving to the latest block height as a number * @throws Error if the RPC call fails */ getLatestBlockHeight(): Promise; /** * * @param assetId - The asset ID to get the decimals for (null for native token) * @returns Promise resolving to the asset decimals as a number * @throws Error if the RPC call fails */ getAssetsDecimals(assetId?: number | null): Promise; /** * Retrieves the native token information for the chain. * Includes token symbol and decimal places. * * @returns Promise resolving to NativeTokenInfo object */ getNativeTokenInfo(): Promise; } //# sourceMappingURL=substrate-chain.d.ts.map