@cheqd/sdk
Version:
A TypeScript SDK built with CosmJS to interact with the cheqd network ledger
190 lines • 11.7 kB
TypeScript
import { EncodeObject, OfflineSigner } from '@cosmjs/proto-signing';
import { DeliverTxResponse, GasPrice, HttpEndpoint, SigningStargateClient, SigningStargateClientOptions, SignerData } from '@cosmjs/stargate';
import { CometClient } from '@cosmjs/tendermint-rpc';
import { MsgCreateDidDocPayload, SignInfo, MsgUpdateDidDocPayload, MsgDeactivateDidDocPayload, VerificationMethod } from '@cheqd/ts-proto/cheqd/did/v2/index.js';
import { DIDDocument, DidStdFee, ISignInputs, MessageBatch, TSignerAlgo } from './types.js';
import { Signer } from 'did-jwt';
import { SignerInfo, TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx.js';
import { SignMode } from 'cosmjs-types/cosmos/tx/signing/v1beta1/signing.js';
import { Any } from 'cosmjs-types/google/protobuf/any.js';
import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin.js';
import { CheqdQuerier } from './querier.js';
import { TxExtension } from './types';
/**
* Calculates the transaction fee for DID operations using gas limit and gas price.
*
* @param gasLimit - Maximum amount of gas units that can be consumed by the transaction
* @param gasPrice - Price per gas unit, either as a string or GasPrice object
* @returns DidStdFee object containing the calculated fee structure
*/
export declare function calculateDidFee(gasLimit: number, gasPrice: string | GasPrice): DidStdFee;
/**
* Creates SignerInfo objects for transaction authentication from signer data.
* Each signer info contains the public key, sequence number, and signing mode.
*
* @param signers - Array of signer objects containing public keys and sequence numbers
* @param signMode - Signing mode to use (e.g., SIGN_MODE_DIRECT)
* @returns Array of SignerInfo objects for transaction authentication
*/
export declare function makeSignerInfos(signers: ReadonlyArray<{
readonly pubkey: Any;
readonly sequence: number;
}>, signMode: SignMode): SignerInfo[];
/**
* Creates encoded AuthInfo bytes for DID transactions with fee payer support.
* The AuthInfo contains signer information, fee details, and gas limit.
*
* @param signers - Array of signer objects with public keys and sequence numbers
* @param feeAmount - Array of coins representing the transaction fee
* @param gasLimit - Maximum gas units that can be consumed
* @param feePayer - Address of the account paying the transaction fees
* @param signMode - Signing mode to use, defaults to SIGN_MODE_DIRECT
* @returns Encoded AuthInfo as Uint8Array for transaction construction
*/
export declare function makeDidAuthInfoBytes(signers: ReadonlyArray<{
readonly pubkey: Any;
readonly sequence: number;
}>, feeAmount: readonly Coin[], gasLimit: bigint, feePayer: string, signMode?: SignMode): Uint8Array;
/**
* Extended SigningStargateClient specifically designed for Cheqd blockchain operations.
* Provides enhanced transaction signing, broadcasting, and DID-specific functionality
* with support for custom fee payers and advanced retry mechanisms.
*/
export declare class CheqdSigningStargateClient extends SigningStargateClient {
/** Map of DID signing algorithms for different verification method types */
private didSigners;
/** Gas price configuration for transaction fee calculation */
private readonly _gasPrice;
/** Offline signer instance for transaction signing */
private readonly _signer;
/** RPC endpoint URL for blockchain communication */
private readonly endpoint?;
/** Maximum gas limit allowed for transactions */
static readonly maxGasLimit: number;
/**
* Creates a new CheqdSigningStargateClient by establishing a connection to the specified endpoint.
* This is the primary factory method for creating a signing client instance.
*
* @param endpoint - RPC endpoint URL or HttpEndpoint object to connect to
* @param signer - Offline signer for transaction signing
* @param options - Additional client configuration options including registry and gas price
* @returns Promise resolving to a connected CheqdSigningStargateClient instance
*/
static connectWithSigner(endpoint: string | HttpEndpoint, signer: OfflineSigner, options?: (SigningStargateClientOptions & {
endpoint?: string;
}) | undefined): Promise<CheqdSigningStargateClient>;
/**
* Constructs a new CheqdSigningStargateClient instance with the provided Comet client and signer.
*
* @param cometClient - Comet client for blockchain communication
* @param signer - Offline signer for transaction signing
* @param options - Additional configuration options including registry, gas price, and endpoint
*/
constructor(cometClient: CometClient | undefined, signer: OfflineSigner, options?: SigningStargateClientOptions & {
endpoint?: string;
});
/**
* Signs and broadcasts a transaction to the blockchain network.
* Supports automatic fee calculation and custom fee payer functionality.
*
* @param signerAddress - Address of the account signing the transaction
* @param messages - Array of messages to include in the transaction
* @param fee - Fee configuration: 'auto' for automatic calculation, number for multiplier, or DidStdFee object
* @param memo - Optional transaction memo string
* @returns Promise resolving to DeliverTxResponse with transaction results
* @throws Error if gas price is not set when using automatic fee calculation
*/
signAndBroadcast(signerAddress: string, messages: readonly EncodeObject[], fee: DidStdFee | 'auto' | number, memo?: string): Promise<DeliverTxResponse>;
/**
* Signs a transaction without broadcasting it to the network.
* Creates a signed transaction that can be broadcast later.
*
* @param signerAddress - Address of the account signing the transaction
* @param messages - Array of messages to include in the transaction
* @param fee - Fee configuration for the transaction
* @param memo - Transaction memo string
* @param explicitSignerData - Optional explicit signer data to avoid querying the chain
* @returns Promise resolving to TxRaw containing the signed transaction
*/
sign(signerAddress: string, messages: readonly EncodeObject[], fee: DidStdFee, memo: string, explicitSignerData?: SignerData): Promise<TxRaw>;
/**
* Internal method for direct signing of transactions using SIGN_MODE_DIRECT.
* Handles the low-level transaction construction and signing process.
*
* @param signerAddress - Address of the account signing the transaction
* @param messages - Array of messages to include in the transaction
* @param fee - Fee configuration for the transaction
* @param memo - Transaction memo string
* @param signerData - Account data including number, sequence, and chain ID
* @returns Promise resolving to TxRaw containing the signed transaction
* @private
*/
private _signDirect;
/**
* Broadcasts a signed transaction to the network and monitors its inclusion in a block,
* with support for retrying on failure and graceful timeout handling.
*
* ## Optimizations over base implementation:
* - Implements a retry policy (`maxRetries`, default: 3) to handle transient broadcast errors.
* - Prevents double spend by reusing the exact same signed transaction (immutable `Uint8Array`).
* - Tracks and returns the last known transaction hash (`txId`), even in the case of timeout or failure.
* - Throws a `TimeoutError` if the transaction is not found within `timeoutMs`, attaching the `txHash` if known.
* - Polling frequency and timeout are customizable via `pollIntervalMs` and `timeoutMs` parameters.
*
* @param tx - The signed transaction bytes to broadcast.
* @param timeoutMs - Maximum duration (in milliseconds) to wait for block inclusion. Defaults to 60,000 ms.
* @param pollIntervalMs - Polling interval (in milliseconds) when checking for transaction inclusion. Defaults to 3,000 ms.
* @param maxRetries - Maximum number of times to retry `broadcastTxSync` on failure. Defaults to 3.
*
* @returns A `Promise` that resolves to `DeliverTxResponse` upon successful inclusion in a block.
* @throws `BroadcastTxError` if the transaction is rejected by the node during CheckTx.
* @throws `TimeoutError` if the transaction is not found on-chain within the timeout window. Includes `txHash` if available.
*/
broadcastTx(tx: Uint8Array, timeoutMs?: number, pollIntervalMs?: number, maxRetries?: number): Promise<DeliverTxResponse>;
simulate(signerAddress: string, messages: readonly EncodeObject[], memo: string | undefined): Promise<number>;
constructSimulateExtension(querier: CheqdQuerier): Promise<TxExtension>;
/**
* Batches multiple messages into optimal groups based on gas limits.
* Simulates each message individually and groups them to maximize throughput
* while staying within gas constraints.
*
* @param messages - Array of messages to batch
* @param signerAddress - Address of the account that will sign the transactions
* @param memo - Optional transaction memo
* @param maxGasLimit - Maximum gas limit per batch, defaults to 30,000,000
* @returns Promise resolving to MessageBatch with grouped messages and gas estimates
*/
batchMessages(messages: readonly EncodeObject[], signerAddress: string, memo?: string, maxGasLimit?: number): Promise<MessageBatch>;
/**
* Validates and initializes DID signers for the provided verification methods.
* Ensures that all verification method types are supported and assigns appropriate signers.
*
* @param verificationMethods - Array of verification methods to validate
* @returns Promise resolving to the configured signer algorithm map
* @throws Error if no verification methods are provided or unsupported types are found
*/
checkDidSigners(verificationMethods?: Partial<VerificationMethod>[]): Promise<TSignerAlgo>;
/**
* Retrieves the appropriate DID signer for a specific verification method.
* Looks up the verification method by ID and returns the corresponding signer function.
*
* @param verificationMethodId - ID of the verification method to get signer for
* @param verificationMethods - Array of available verification methods
* @returns Promise resolving to a signer function that takes a secret key
* @throws Error if the verification method is not found
*/
getDidSigner(verificationMethodId: string, verificationMethods: Partial<VerificationMethod>[]): Promise<(secretKey: Uint8Array) => Signer>;
/**
* Signs a CreateDidDoc transaction payload using the provided signing inputs.
* Validates verification methods and creates signatures for each signing input.
*
* @param signInputs - Array of signing inputs containing verification method IDs and private keys
* @param payload - CreateDidDoc payload to sign
* @returns Promise resolving to array of SignInfo objects with signatures
*/
signCreateDidDocTx(signInputs: ISignInputs[], payload: MsgCreateDidDocPayload): Promise<SignInfo[]>;
signUpdateDidDocTx(signInputs: ISignInputs[], payload: MsgUpdateDidDocPayload, externalControllers?: DIDDocument[], previousDidDocument?: DIDDocument): Promise<SignInfo[]>;
signDeactivateDidDocTx(signInputs: ISignInputs[], payload: MsgDeactivateDidDocPayload, verificationMethod: VerificationMethod[]): Promise<SignInfo[]>;
static signIdentityTx(signBytes: Uint8Array, signInputs: ISignInputs[]): Promise<SignInfo[]>;
}
//# sourceMappingURL=signer.d.ts.map