UNPKG

@anuragchvn-blip/mandatekit

Version:

Production-ready Web3 autopay SDK for crypto-based recurring payments using EIP-712 mandates

145 lines 4.83 kB
/** * MandateClient - Core client for creating and managing mandates * Factory-based approach with support for both Node.js and browser environments * @module client */ import type { Address, Hex, WalletClient, PrivateKeyAccount } from 'viem'; import type { Mandate, SignedMandate, MandateDomain, VerificationResult } from '../types/index.js'; import { prepareMandateTypedData } from '../utils/crypto.js'; /** * Configuration for MandateClient */ export interface MandateClientConfig { /** EIP-712 domain configuration */ domain: MandateDomain; /** Private key account for Node.js (from viem) */ account?: PrivateKeyAccount; /** Wallet client for browser environments */ walletClient?: WalletClient; /** Address to use (required if using walletClient) */ address?: Address; } /** * MandateClient interface - composable functions for mandate management */ export interface MandateClient { /** Get the current domain configuration */ getDomain: () => MandateDomain; /** Get the current signer address */ getAddress: () => Address; /** Sign a new mandate */ signMandate: (mandateParams: Omit<Mandate, 'nonce'> & { nonce?: bigint | string; }) => Promise<SignedMandate>; /** Prepare typed data for external signing */ prepareTypedData: (mandate: Mandate) => ReturnType<typeof prepareMandateTypedData>; /** Verify a signed mandate */ verifyMandate: (signedMandate: SignedMandate) => Promise<VerificationResult>; /** Hash a mandate to get its ID */ getMandateId: (mandate: Mandate) => Hex; /** Generate a fresh nonce */ generateNonce: () => bigint; } /** * Creates a MandateClient instance using factory pattern * Supports both Node.js (with private key) and browser (with wallet) environments * * @param config - Client configuration * @returns Composable MandateClient functions * * @example * // Node.js with private key * ```typescript * import { createMandateClient } from '@mandatekit/sdk/client'; * import { privateKeyToAccount } from 'viem/accounts'; * * const account = privateKeyToAccount('0x...'); * const client = createMandateClient({ * domain: { * name: 'MandateRegistry', * version: '1', * chainId: 1, * verifyingContract: '0x...', * }, * account, * }); * * const signedMandate = await client.signMandate({ * subscriber: account.address, * token: '0xUSDC...', * amount: '10000000', * cadence: { interval: 'monthly', count: 1 }, * recipient: '0x456...', * validAfter: Math.floor(Date.now() / 1000), * validBefore: Math.floor(Date.now() / 1000) + 31536000, * }); * ``` * * @example * // Browser with wallet * ```typescript * import { createMandateClient } from '@mandatekit/sdk/client'; * import { createWalletClient, custom } from 'viem'; * * const walletClient = createWalletClient({ * chain: mainnet, * transport: custom(window.ethereum), * }); * * const [address] = await walletClient.getAddresses(); * const client = createMandateClient({ * domain: { ... }, * walletClient, * address, * }); * ``` */ export declare function createMandateClient(config: MandateClientConfig): MandateClient; /** * Standalone function to verify a mandate signature without a client instance * Useful for quick verification in stateless environments * * @param mandate - Mandate to verify * @param signature - Signature to verify * @param domain - EIP-712 domain * @param expectedSigner - Expected signer address (defaults to mandate.subscriber) * @returns Verification result * * @example * ```typescript * const isValid = await verifyMandateSignature( * mandate, * signature, * domain, * mandate.subscriber * ); * ``` */ export declare function verifyMandateSignature(mandate: Mandate, signature: Hex, domain: MandateDomain, expectedSigner?: Address): Promise<VerificationResult>; /** * Batch sign multiple mandates efficiently * * @param client - MandateClient instance * @param mandates - Array of mandate parameters * @returns Array of signed mandates * * @example * ```typescript * const signedMandates = await batchSignMandates(client, [ * { subscriber: '0x...', token: '0x...', ... }, * { subscriber: '0x...', token: '0x...', ... }, * ]); * ``` */ export declare function batchSignMandates(client: MandateClient, mandates: Array<Omit<Mandate, 'nonce'> & { nonce?: bigint | string; }>): Promise<SignedMandate[]>; /** * Batch verify multiple signed mandates * * @param client - MandateClient instance * @param signedMandates - Array of signed mandates to verify * @returns Array of verification results */ export declare function batchVerifyMandates(client: MandateClient, signedMandates: SignedMandate[]): Promise<VerificationResult[]>; //# sourceMappingURL=index.d.ts.map