UNPKG

@anuragchvn-blip/mandatekit

Version:

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

186 lines 5.9 kB
/** * Cryptographic utilities for MandateKit SDK * Provides secure hashing, signature recovery, and EIP-712 helpers * @module utils/crypto */ import { type Address, type Hex } from 'viem'; import type { Mandate, MandateDomain, MandateTypedData } from '../types/index.js'; /** * EIP-712 type definitions for Mandate * Order matters for proper hashing */ export declare const MANDATE_TYPES: { readonly Mandate: readonly [{ readonly name: "subscriber"; readonly type: "address"; }, { readonly name: "token"; readonly type: "address"; }, { readonly name: "amount"; readonly type: "uint256"; }, { readonly name: "cadenceInterval"; readonly type: "uint8"; }, { readonly name: "cadenceCount"; readonly type: "uint256"; }, { readonly name: "recipient"; readonly type: "address"; }, { readonly name: "validAfter"; readonly type: "uint256"; }, { readonly name: "validBefore"; readonly type: "uint256"; }, { readonly name: "nonce"; readonly type: "uint256"; }, { readonly name: "maxPayments"; readonly type: "uint256"; }, { readonly name: "metadata"; readonly type: "string"; }]; }; /** * Converts cadence interval string to uint8 for EIP-712 * @param interval - Cadence interval type * @returns Numeric representation (0=daily, 1=weekly, 2=monthly, 3=yearly, 4=custom) */ export declare function cadenceIntervalToUint8(interval: 'daily' | 'weekly' | 'monthly' | 'yearly' | 'custom'): number; /** * Converts uint8 to cadence interval string * @param value - Numeric interval value * @returns Cadence interval type */ export declare function uint8ToCadenceInterval(value: number): 'daily' | 'weekly' | 'monthly' | 'yearly' | 'custom'; /** * Prepares EIP-712 typed data for mandate signing * * @param mandate - Mandate to prepare for signing * @param domain - EIP-712 domain separator configuration * @returns Properly formatted typed data structure * * @example * ```typescript * const typedData = prepareMandateTypedData(mandate, { * name: 'MandateRegistry', * version: '1', * chainId: 1, * verifyingContract: '0x...', * }); * ``` */ export declare function prepareMandateTypedData(mandate: Mandate, domain: MandateDomain): MandateTypedData; /** * Computes the EIP-712 hash of a mandate * This is the message hash that should be signed * * @param mandate - Mandate to hash * @param domain - EIP-712 domain configuration * @returns Keccak256 hash of the structured data * * @example * ```typescript * const hash = hashMandate(mandate, domain); * console.log('Mandate hash:', hash); * ``` */ export declare function hashMandate(mandate: Mandate, domain: MandateDomain): Hex; /** * Recovers the signer address from a mandate signature * Used for signature verification and authentication * * @param mandate - Original mandate that was signed * @param signature - EIP-712 signature to verify * @param domain - EIP-712 domain used for signing * @returns Recovered Ethereum address of the signer * * @example * ```typescript * const signer = await recoverMandateSigner(mandate, signature, domain); * if (signer.toLowerCase() === mandate.subscriber.toLowerCase()) { * console.log('Valid signature from subscriber'); * } * ``` */ export declare function recoverMandateSigner(mandate: Mandate, signature: Hex, domain: MandateDomain): Promise<Address>; /** * Generates a unique mandate ID from its hash * Useful for indexing and tracking mandates * * @param mandate - Mandate to generate ID for * @param domain - EIP-712 domain configuration * @returns Unique mandate identifier * * @example * ```typescript * const mandateId = generateMandateId(mandate, domain); * // Store in database with this ID * ``` */ export declare function generateMandateId(mandate: Mandate, domain: MandateDomain): Hex; /** * Computes a payment record hash for verification * Used to prove a payment was executed correctly * * @param mandateHash - Hash of the original mandate * @param executionCount - Sequential payment number * @param executedAt - Timestamp of execution * @param amountPaid - Actual amount transferred * @returns Hash of the payment record * * @example * ```typescript * const recordHash = hashPaymentRecord( * mandateHash, * 1, * Math.floor(Date.now() / 1000), * parseEther('10') * ); * ``` */ export declare function hashPaymentRecord(mandateHash: Hex, executionCount: number, executedAt: number, amountPaid: bigint): Hex; /** * Validates that a signature is properly formatted * @param signature - Signature to validate * @returns True if signature format is valid */ export declare function isValidSignature(signature: string): signature is Hex; /** * Securely compares two signatures for equality * Uses constant-time comparison to prevent timing attacks * * @param sig1 - First signature * @param sig2 - Second signature * @returns True if signatures are equal */ export declare function compareSignatures(sig1: Hex, sig2: Hex): boolean; /** * Generates a unique nonce based on timestamp and random data * Useful for creating fresh nonces for new mandates * * @returns Cryptographically secure random nonce * * @example * ```typescript * const nonce = generateNonce(); * ``` */ export declare function generateNonce(): bigint; /** * Validates an Ethereum address format * @param address - Address to validate * @returns True if address is valid */ export declare function isValidAddress(address: string): address is Address; /** * Normalizes an address to checksummed format * @param address - Address to normalize * @returns Checksummed address */ export declare function normalizeAddress(address: string): Address; //# sourceMappingURL=crypto.d.ts.map