moltspay 0.2.10 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -1
- package/dist/cli.js +30496 -19
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +30698 -215
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +322 -2
- package/dist/index.d.ts +322 -2
- package/dist/index.js +31459 -721
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +31397 -667
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -3
package/dist/index.d.mts
CHANGED
|
@@ -5,7 +5,9 @@ export { PermitPayment } from './permit/index.mjs';
|
|
|
5
5
|
export { CreateOrderParams, MemoryOrderStore, Order, OrderManager, OrderStatus, OrderStore } from './orders/index.mjs';
|
|
6
6
|
export { VerifyPaymentParams, VerifyPaymentResult, getTransactionStatus, verifyPayment, waitForTransaction } from './verify/index.mjs';
|
|
7
7
|
export { PaymentGuideParams, extractTransactionHash, generatePaymentGuide, generatePaymentReminder, generateWalletGuide, hasTransactionHash } from './guide/index.mjs';
|
|
8
|
-
|
|
8
|
+
import { getChain } from './chains/index.mjs';
|
|
9
|
+
export { CHAINS, ERC20_ABI, getChainById, listChains } from './chains/index.mjs';
|
|
10
|
+
import { ethers } from 'ethers';
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* PaymentAgent - Core Payment Agent
|
|
@@ -615,4 +617,322 @@ declare function parseStatusMarker(message: string): {
|
|
|
615
617
|
data: Record<string, string>;
|
|
616
618
|
} | null;
|
|
617
619
|
|
|
618
|
-
|
|
620
|
+
/**
|
|
621
|
+
* x402 Client - Easy HTTP client with automatic payment handling
|
|
622
|
+
*
|
|
623
|
+
* Wraps @x402/fetch to provide simple API for AI Agents.
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```typescript
|
|
627
|
+
* import { createX402Client } from 'moltspay/x402';
|
|
628
|
+
*
|
|
629
|
+
* // Using local wallet (AgentWallet)
|
|
630
|
+
* const client = await createX402Client({ chain: 'base' });
|
|
631
|
+
*
|
|
632
|
+
* // Using CDP wallet
|
|
633
|
+
* const client = await createX402Client({ chain: 'base', useCDP: true });
|
|
634
|
+
*
|
|
635
|
+
* // Make request - payment handled automatically
|
|
636
|
+
* const response = await client.fetch('https://api.example.com/paid-resource');
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
|
|
640
|
+
interface X402ClientConfig {
|
|
641
|
+
/** Chain to use */
|
|
642
|
+
chain?: ChainName;
|
|
643
|
+
/** Use CDP wallet instead of local wallet */
|
|
644
|
+
useCDP?: boolean;
|
|
645
|
+
/** Custom private key (overrides stored wallet) */
|
|
646
|
+
privateKey?: string;
|
|
647
|
+
/** Storage directory for wallet */
|
|
648
|
+
storageDir?: string;
|
|
649
|
+
}
|
|
650
|
+
interface X402Client {
|
|
651
|
+
/** Fetch with automatic x402 payment handling */
|
|
652
|
+
fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
653
|
+
/** Wallet address */
|
|
654
|
+
address: string;
|
|
655
|
+
/** Chain being used */
|
|
656
|
+
chain: ChainName;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Check if @x402/fetch is available
|
|
660
|
+
*/
|
|
661
|
+
declare function isX402Available(): boolean;
|
|
662
|
+
/**
|
|
663
|
+
* Create x402 client
|
|
664
|
+
*
|
|
665
|
+
* Automatically handles 402 Payment Required responses.
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```typescript
|
|
669
|
+
* import { createX402Client } from 'moltspay/x402';
|
|
670
|
+
*
|
|
671
|
+
* const client = await createX402Client({ chain: 'base' });
|
|
672
|
+
*
|
|
673
|
+
* // Request paid API - payment handled automatically
|
|
674
|
+
* const response = await client.fetch('https://zen7.api/video', {
|
|
675
|
+
* method: 'POST',
|
|
676
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
677
|
+
* body: JSON.stringify({ prompt: 'a cat dancing' })
|
|
678
|
+
* });
|
|
679
|
+
*
|
|
680
|
+
* const result = await response.json();
|
|
681
|
+
* ```
|
|
682
|
+
*/
|
|
683
|
+
declare function createX402Client(config?: X402ClientConfig): Promise<X402Client>;
|
|
684
|
+
/**
|
|
685
|
+
* Simple one-shot x402 request
|
|
686
|
+
*
|
|
687
|
+
* For when you just need to make one paid request.
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```typescript
|
|
691
|
+
* import { x402Fetch } from 'moltspay/x402';
|
|
692
|
+
*
|
|
693
|
+
* const response = await x402Fetch('https://zen7.api/video', {
|
|
694
|
+
* method: 'POST',
|
|
695
|
+
* body: JSON.stringify({ prompt: 'a cat dancing' })
|
|
696
|
+
* }, { chain: 'base' });
|
|
697
|
+
* ```
|
|
698
|
+
*/
|
|
699
|
+
declare function x402Fetch(input: string | URL | Request, init?: RequestInit, config?: X402ClientConfig): Promise<Response>;
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* x402 Protocol Support for MoltsPay
|
|
703
|
+
*
|
|
704
|
+
* x402 is an open standard for HTTP-native payments.
|
|
705
|
+
* When a server returns 402 Payment Required, the client can pay and retry.
|
|
706
|
+
*
|
|
707
|
+
* @see https://x402.org
|
|
708
|
+
* @see https://github.com/coinbase/x402
|
|
709
|
+
*/
|
|
710
|
+
|
|
711
|
+
declare const X402_VERSION = 2;
|
|
712
|
+
declare const PAYMENT_REQUIRED_HEADER = "x-payment-required";
|
|
713
|
+
declare const PAYMENT_HEADER = "x-payment";
|
|
714
|
+
declare const PAYMENT_RESPONSE_HEADER = "x-payment-response";
|
|
715
|
+
/**
|
|
716
|
+
* x402 Payment Requirements (from server 402 response)
|
|
717
|
+
*/
|
|
718
|
+
interface X402PaymentRequirements {
|
|
719
|
+
/** Scheme (e.g., "exact") */
|
|
720
|
+
scheme: string;
|
|
721
|
+
/** Network (e.g., "eip155:8453" for Base) */
|
|
722
|
+
network: string;
|
|
723
|
+
/** Maximum amount in base units (e.g., "990000" for 0.99 USDC) */
|
|
724
|
+
maxAmountRequired: string;
|
|
725
|
+
/** Payee address */
|
|
726
|
+
resource: string;
|
|
727
|
+
/** Payment description */
|
|
728
|
+
description?: string;
|
|
729
|
+
/** MIME type of the resource */
|
|
730
|
+
mimeType?: string;
|
|
731
|
+
/** Output schema for the resource */
|
|
732
|
+
outputSchema?: unknown;
|
|
733
|
+
/** Expiration timestamp */
|
|
734
|
+
expiration?: number;
|
|
735
|
+
/** Extra data */
|
|
736
|
+
extra?: string;
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* x402 Payment Payload (client sends to server)
|
|
740
|
+
*/
|
|
741
|
+
interface X402PaymentPayload {
|
|
742
|
+
/** x402 protocol version */
|
|
743
|
+
x402Version: number;
|
|
744
|
+
/** Scheme used */
|
|
745
|
+
scheme: string;
|
|
746
|
+
/** Network used */
|
|
747
|
+
network: string;
|
|
748
|
+
/** Scheme-specific payload */
|
|
749
|
+
payload: unknown;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Parse x402 Payment Required header from 402 response
|
|
753
|
+
*/
|
|
754
|
+
declare function parsePaymentRequired(header: string): X402PaymentRequirements[];
|
|
755
|
+
/**
|
|
756
|
+
* Encode payment payload for x-payment header
|
|
757
|
+
*/
|
|
758
|
+
declare function encodePaymentPayload(payload: X402PaymentPayload): string;
|
|
759
|
+
/**
|
|
760
|
+
* Convert chain name to x402 network identifier
|
|
761
|
+
*/
|
|
762
|
+
declare function chainToNetwork(chain: ChainName): string;
|
|
763
|
+
/**
|
|
764
|
+
* Convert x402 network identifier to chain name
|
|
765
|
+
*/
|
|
766
|
+
declare function networkToChain(network: string): ChainName | null;
|
|
767
|
+
/**
|
|
768
|
+
* EIP-3009 Authorization for USDC transferWithAuthorization
|
|
769
|
+
*/
|
|
770
|
+
interface EIP3009Authorization {
|
|
771
|
+
from: string;
|
|
772
|
+
to: string;
|
|
773
|
+
value: string;
|
|
774
|
+
validAfter: string;
|
|
775
|
+
validBefore: string;
|
|
776
|
+
nonce: string;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Sign EIP-3009 transferWithAuthorization
|
|
780
|
+
* Used for x402 "exact" scheme with USDC
|
|
781
|
+
*/
|
|
782
|
+
declare function signEIP3009(wallet: ethers.Wallet, params: {
|
|
783
|
+
to: string;
|
|
784
|
+
amount: number;
|
|
785
|
+
validAfter?: number;
|
|
786
|
+
validBefore?: number;
|
|
787
|
+
chain: ChainName;
|
|
788
|
+
}): Promise<{
|
|
789
|
+
authorization: EIP3009Authorization;
|
|
790
|
+
signature: string;
|
|
791
|
+
}>;
|
|
792
|
+
/**
|
|
793
|
+
* Create x402 payment payload for "exact" scheme on EVM
|
|
794
|
+
*/
|
|
795
|
+
declare function createExactEvmPayload(wallet: ethers.Wallet, requirements: X402PaymentRequirements, chain: ChainName): Promise<X402PaymentPayload>;
|
|
796
|
+
/**
|
|
797
|
+
* Wrap fetch to handle x402 402 responses automatically
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```typescript
|
|
801
|
+
* import { AgentWallet } from 'moltspay';
|
|
802
|
+
* import { wrapFetchWith402 } from 'moltspay/x402';
|
|
803
|
+
*
|
|
804
|
+
* const wallet = new AgentWallet({ chain: 'base' });
|
|
805
|
+
* const fetch402 = wrapFetchWith402(fetch, wallet);
|
|
806
|
+
*
|
|
807
|
+
* // Automatically handles 402 and pays
|
|
808
|
+
* const response = await fetch402('https://api.example.com/paid-resource');
|
|
809
|
+
* ```
|
|
810
|
+
*/
|
|
811
|
+
declare function wrapFetchWith402(fetchFn: typeof fetch, wallet: {
|
|
812
|
+
address: string;
|
|
813
|
+
chain: ChainName;
|
|
814
|
+
}, privateKey: string): (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
815
|
+
/**
|
|
816
|
+
* Server-side: Generate x402 Payment Required response
|
|
817
|
+
*/
|
|
818
|
+
declare function createPaymentRequiredResponse(requirements: X402PaymentRequirements[]): {
|
|
819
|
+
status: 402;
|
|
820
|
+
headers: Record<string, string>;
|
|
821
|
+
};
|
|
822
|
+
/**
|
|
823
|
+
* Server-side: Verify x402 payment header
|
|
824
|
+
*/
|
|
825
|
+
declare function verifyPaymentHeader(header: string, expectedRecipient: string, expectedAmount: number): {
|
|
826
|
+
valid: boolean;
|
|
827
|
+
error?: string;
|
|
828
|
+
payload?: X402PaymentPayload;
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* CDP (Coinbase Developer Platform) Wallet Integration
|
|
833
|
+
*
|
|
834
|
+
* Creates and manages wallets via Coinbase's CDP SDK.
|
|
835
|
+
* These wallets are hosted by Coinbase, making them easy to use for AI Agents.
|
|
836
|
+
*
|
|
837
|
+
* @see https://docs.cdp.coinbase.com/
|
|
838
|
+
*/
|
|
839
|
+
|
|
840
|
+
interface CDPWalletConfig {
|
|
841
|
+
/** Storage directory (default: ~/.moltspay) */
|
|
842
|
+
storageDir?: string;
|
|
843
|
+
/** Chain name */
|
|
844
|
+
chain?: ChainName;
|
|
845
|
+
/** CDP API credentials (or use env vars) */
|
|
846
|
+
apiKeyId?: string;
|
|
847
|
+
apiKeySecret?: string;
|
|
848
|
+
walletSecret?: string;
|
|
849
|
+
}
|
|
850
|
+
interface CDPWalletData {
|
|
851
|
+
/** Wallet address */
|
|
852
|
+
address: string;
|
|
853
|
+
/** CDP wallet ID */
|
|
854
|
+
walletId: string;
|
|
855
|
+
/** Chain */
|
|
856
|
+
chain: ChainName;
|
|
857
|
+
/** Created timestamp */
|
|
858
|
+
createdAt: string;
|
|
859
|
+
/** CDP account data (for restoration) */
|
|
860
|
+
accountData?: string;
|
|
861
|
+
}
|
|
862
|
+
interface CDPInitResult {
|
|
863
|
+
success: boolean;
|
|
864
|
+
address?: string;
|
|
865
|
+
walletId?: string;
|
|
866
|
+
isNew?: boolean;
|
|
867
|
+
error?: string;
|
|
868
|
+
storagePath?: string;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Check if CDP SDK is available
|
|
872
|
+
*/
|
|
873
|
+
declare function isCDPAvailable(): boolean;
|
|
874
|
+
/**
|
|
875
|
+
* Initialize CDP wallet
|
|
876
|
+
*
|
|
877
|
+
* Creates a new CDP wallet or loads existing one.
|
|
878
|
+
*
|
|
879
|
+
* @example
|
|
880
|
+
* ```bash
|
|
881
|
+
* # Set credentials
|
|
882
|
+
* export CDP_API_KEY_ID=your-key-id
|
|
883
|
+
* export CDP_API_KEY_SECRET=your-key-secret
|
|
884
|
+
*
|
|
885
|
+
* # Initialize
|
|
886
|
+
* npx moltspay init --cdp --chain base
|
|
887
|
+
* ```
|
|
888
|
+
*/
|
|
889
|
+
declare function initCDPWallet(config?: CDPWalletConfig): Promise<CDPInitResult>;
|
|
890
|
+
/**
|
|
891
|
+
* Load existing CDP wallet
|
|
892
|
+
*/
|
|
893
|
+
declare function loadCDPWallet(config?: CDPWalletConfig): CDPWalletData | null;
|
|
894
|
+
/**
|
|
895
|
+
* Get CDP wallet address (quick check without full init)
|
|
896
|
+
*/
|
|
897
|
+
declare function getCDPWalletAddress(storageDir?: string): string | null;
|
|
898
|
+
/**
|
|
899
|
+
* CDP Wallet class for making payments
|
|
900
|
+
*
|
|
901
|
+
* Uses CDP SDK for wallet operations with x402 support.
|
|
902
|
+
*/
|
|
903
|
+
declare class CDPWallet {
|
|
904
|
+
readonly address: string;
|
|
905
|
+
readonly chain: ChainName;
|
|
906
|
+
readonly chainConfig: ReturnType<typeof getChain>;
|
|
907
|
+
private storageDir;
|
|
908
|
+
constructor(config?: CDPWalletConfig);
|
|
909
|
+
/**
|
|
910
|
+
* Get USDC balance
|
|
911
|
+
*/
|
|
912
|
+
getBalance(): Promise<{
|
|
913
|
+
usdc: string;
|
|
914
|
+
eth: string;
|
|
915
|
+
}>;
|
|
916
|
+
/**
|
|
917
|
+
* Transfer USDC to a recipient
|
|
918
|
+
*
|
|
919
|
+
* Requires CDP SDK and credentials to sign transactions.
|
|
920
|
+
*/
|
|
921
|
+
transfer(params: {
|
|
922
|
+
to: string;
|
|
923
|
+
amount: number;
|
|
924
|
+
}): Promise<{
|
|
925
|
+
success: boolean;
|
|
926
|
+
txHash?: string;
|
|
927
|
+
error?: string;
|
|
928
|
+
explorerUrl?: string;
|
|
929
|
+
}>;
|
|
930
|
+
/**
|
|
931
|
+
* Create viem-compatible signer for x402
|
|
932
|
+
*
|
|
933
|
+
* This allows using CDP wallet with x402 protocol.
|
|
934
|
+
*/
|
|
935
|
+
getViemAccount(): Promise<unknown>;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
export { AgentWallet, AuditAction, AuditEntry, AuditLog, BuyerTemplates, type CDPInitResult, CDPWallet, type CDPWalletConfig, type CDPWalletData, ChainConfig, ChainName, CreateInvoiceParams, type EIP3009Authorization, Invoice, PAYMENT_HEADER, PAYMENT_REQUIRED_HEADER, PAYMENT_RESPONSE_HEADER, PaymentAgent, PaymentAgentConfig, type Receipt, type ReceiptParams, SellerTemplates, StatusMarkers, VerifyOptions, VerifyResult, WalletBalance, type X402Client, type X402ClientConfig, type X402PaymentPayload, type X402PaymentRequirements, X402_VERSION, chainToNetwork, createExactEvmPayload, createPaymentRequiredResponse, createX402Client, encodePaymentPayload, formatReceiptJson, formatReceiptMessage, formatReceiptText, generateReceipt, generateReceiptFromInvoice, getAgentAddress, getCDPWalletAddress, getChain, initCDPWallet, isCDPAvailable, isX402Available, loadCDPWallet, networkToChain, parsePaymentRequired, parseStatusMarker, signEIP3009, verifyPaymentHeader, wrapFetchWith402, x402Fetch };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ export { PermitPayment } from './permit/index.js';
|
|
|
5
5
|
export { CreateOrderParams, MemoryOrderStore, Order, OrderManager, OrderStatus, OrderStore } from './orders/index.js';
|
|
6
6
|
export { VerifyPaymentParams, VerifyPaymentResult, getTransactionStatus, verifyPayment, waitForTransaction } from './verify/index.js';
|
|
7
7
|
export { PaymentGuideParams, extractTransactionHash, generatePaymentGuide, generatePaymentReminder, generateWalletGuide, hasTransactionHash } from './guide/index.js';
|
|
8
|
-
|
|
8
|
+
import { getChain } from './chains/index.js';
|
|
9
|
+
export { CHAINS, ERC20_ABI, getChainById, listChains } from './chains/index.js';
|
|
10
|
+
import { ethers } from 'ethers';
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* PaymentAgent - Core Payment Agent
|
|
@@ -615,4 +617,322 @@ declare function parseStatusMarker(message: string): {
|
|
|
615
617
|
data: Record<string, string>;
|
|
616
618
|
} | null;
|
|
617
619
|
|
|
618
|
-
|
|
620
|
+
/**
|
|
621
|
+
* x402 Client - Easy HTTP client with automatic payment handling
|
|
622
|
+
*
|
|
623
|
+
* Wraps @x402/fetch to provide simple API for AI Agents.
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```typescript
|
|
627
|
+
* import { createX402Client } from 'moltspay/x402';
|
|
628
|
+
*
|
|
629
|
+
* // Using local wallet (AgentWallet)
|
|
630
|
+
* const client = await createX402Client({ chain: 'base' });
|
|
631
|
+
*
|
|
632
|
+
* // Using CDP wallet
|
|
633
|
+
* const client = await createX402Client({ chain: 'base', useCDP: true });
|
|
634
|
+
*
|
|
635
|
+
* // Make request - payment handled automatically
|
|
636
|
+
* const response = await client.fetch('https://api.example.com/paid-resource');
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
|
|
640
|
+
interface X402ClientConfig {
|
|
641
|
+
/** Chain to use */
|
|
642
|
+
chain?: ChainName;
|
|
643
|
+
/** Use CDP wallet instead of local wallet */
|
|
644
|
+
useCDP?: boolean;
|
|
645
|
+
/** Custom private key (overrides stored wallet) */
|
|
646
|
+
privateKey?: string;
|
|
647
|
+
/** Storage directory for wallet */
|
|
648
|
+
storageDir?: string;
|
|
649
|
+
}
|
|
650
|
+
interface X402Client {
|
|
651
|
+
/** Fetch with automatic x402 payment handling */
|
|
652
|
+
fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
653
|
+
/** Wallet address */
|
|
654
|
+
address: string;
|
|
655
|
+
/** Chain being used */
|
|
656
|
+
chain: ChainName;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Check if @x402/fetch is available
|
|
660
|
+
*/
|
|
661
|
+
declare function isX402Available(): boolean;
|
|
662
|
+
/**
|
|
663
|
+
* Create x402 client
|
|
664
|
+
*
|
|
665
|
+
* Automatically handles 402 Payment Required responses.
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```typescript
|
|
669
|
+
* import { createX402Client } from 'moltspay/x402';
|
|
670
|
+
*
|
|
671
|
+
* const client = await createX402Client({ chain: 'base' });
|
|
672
|
+
*
|
|
673
|
+
* // Request paid API - payment handled automatically
|
|
674
|
+
* const response = await client.fetch('https://zen7.api/video', {
|
|
675
|
+
* method: 'POST',
|
|
676
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
677
|
+
* body: JSON.stringify({ prompt: 'a cat dancing' })
|
|
678
|
+
* });
|
|
679
|
+
*
|
|
680
|
+
* const result = await response.json();
|
|
681
|
+
* ```
|
|
682
|
+
*/
|
|
683
|
+
declare function createX402Client(config?: X402ClientConfig): Promise<X402Client>;
|
|
684
|
+
/**
|
|
685
|
+
* Simple one-shot x402 request
|
|
686
|
+
*
|
|
687
|
+
* For when you just need to make one paid request.
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```typescript
|
|
691
|
+
* import { x402Fetch } from 'moltspay/x402';
|
|
692
|
+
*
|
|
693
|
+
* const response = await x402Fetch('https://zen7.api/video', {
|
|
694
|
+
* method: 'POST',
|
|
695
|
+
* body: JSON.stringify({ prompt: 'a cat dancing' })
|
|
696
|
+
* }, { chain: 'base' });
|
|
697
|
+
* ```
|
|
698
|
+
*/
|
|
699
|
+
declare function x402Fetch(input: string | URL | Request, init?: RequestInit, config?: X402ClientConfig): Promise<Response>;
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* x402 Protocol Support for MoltsPay
|
|
703
|
+
*
|
|
704
|
+
* x402 is an open standard for HTTP-native payments.
|
|
705
|
+
* When a server returns 402 Payment Required, the client can pay and retry.
|
|
706
|
+
*
|
|
707
|
+
* @see https://x402.org
|
|
708
|
+
* @see https://github.com/coinbase/x402
|
|
709
|
+
*/
|
|
710
|
+
|
|
711
|
+
declare const X402_VERSION = 2;
|
|
712
|
+
declare const PAYMENT_REQUIRED_HEADER = "x-payment-required";
|
|
713
|
+
declare const PAYMENT_HEADER = "x-payment";
|
|
714
|
+
declare const PAYMENT_RESPONSE_HEADER = "x-payment-response";
|
|
715
|
+
/**
|
|
716
|
+
* x402 Payment Requirements (from server 402 response)
|
|
717
|
+
*/
|
|
718
|
+
interface X402PaymentRequirements {
|
|
719
|
+
/** Scheme (e.g., "exact") */
|
|
720
|
+
scheme: string;
|
|
721
|
+
/** Network (e.g., "eip155:8453" for Base) */
|
|
722
|
+
network: string;
|
|
723
|
+
/** Maximum amount in base units (e.g., "990000" for 0.99 USDC) */
|
|
724
|
+
maxAmountRequired: string;
|
|
725
|
+
/** Payee address */
|
|
726
|
+
resource: string;
|
|
727
|
+
/** Payment description */
|
|
728
|
+
description?: string;
|
|
729
|
+
/** MIME type of the resource */
|
|
730
|
+
mimeType?: string;
|
|
731
|
+
/** Output schema for the resource */
|
|
732
|
+
outputSchema?: unknown;
|
|
733
|
+
/** Expiration timestamp */
|
|
734
|
+
expiration?: number;
|
|
735
|
+
/** Extra data */
|
|
736
|
+
extra?: string;
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* x402 Payment Payload (client sends to server)
|
|
740
|
+
*/
|
|
741
|
+
interface X402PaymentPayload {
|
|
742
|
+
/** x402 protocol version */
|
|
743
|
+
x402Version: number;
|
|
744
|
+
/** Scheme used */
|
|
745
|
+
scheme: string;
|
|
746
|
+
/** Network used */
|
|
747
|
+
network: string;
|
|
748
|
+
/** Scheme-specific payload */
|
|
749
|
+
payload: unknown;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Parse x402 Payment Required header from 402 response
|
|
753
|
+
*/
|
|
754
|
+
declare function parsePaymentRequired(header: string): X402PaymentRequirements[];
|
|
755
|
+
/**
|
|
756
|
+
* Encode payment payload for x-payment header
|
|
757
|
+
*/
|
|
758
|
+
declare function encodePaymentPayload(payload: X402PaymentPayload): string;
|
|
759
|
+
/**
|
|
760
|
+
* Convert chain name to x402 network identifier
|
|
761
|
+
*/
|
|
762
|
+
declare function chainToNetwork(chain: ChainName): string;
|
|
763
|
+
/**
|
|
764
|
+
* Convert x402 network identifier to chain name
|
|
765
|
+
*/
|
|
766
|
+
declare function networkToChain(network: string): ChainName | null;
|
|
767
|
+
/**
|
|
768
|
+
* EIP-3009 Authorization for USDC transferWithAuthorization
|
|
769
|
+
*/
|
|
770
|
+
interface EIP3009Authorization {
|
|
771
|
+
from: string;
|
|
772
|
+
to: string;
|
|
773
|
+
value: string;
|
|
774
|
+
validAfter: string;
|
|
775
|
+
validBefore: string;
|
|
776
|
+
nonce: string;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Sign EIP-3009 transferWithAuthorization
|
|
780
|
+
* Used for x402 "exact" scheme with USDC
|
|
781
|
+
*/
|
|
782
|
+
declare function signEIP3009(wallet: ethers.Wallet, params: {
|
|
783
|
+
to: string;
|
|
784
|
+
amount: number;
|
|
785
|
+
validAfter?: number;
|
|
786
|
+
validBefore?: number;
|
|
787
|
+
chain: ChainName;
|
|
788
|
+
}): Promise<{
|
|
789
|
+
authorization: EIP3009Authorization;
|
|
790
|
+
signature: string;
|
|
791
|
+
}>;
|
|
792
|
+
/**
|
|
793
|
+
* Create x402 payment payload for "exact" scheme on EVM
|
|
794
|
+
*/
|
|
795
|
+
declare function createExactEvmPayload(wallet: ethers.Wallet, requirements: X402PaymentRequirements, chain: ChainName): Promise<X402PaymentPayload>;
|
|
796
|
+
/**
|
|
797
|
+
* Wrap fetch to handle x402 402 responses automatically
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```typescript
|
|
801
|
+
* import { AgentWallet } from 'moltspay';
|
|
802
|
+
* import { wrapFetchWith402 } from 'moltspay/x402';
|
|
803
|
+
*
|
|
804
|
+
* const wallet = new AgentWallet({ chain: 'base' });
|
|
805
|
+
* const fetch402 = wrapFetchWith402(fetch, wallet);
|
|
806
|
+
*
|
|
807
|
+
* // Automatically handles 402 and pays
|
|
808
|
+
* const response = await fetch402('https://api.example.com/paid-resource');
|
|
809
|
+
* ```
|
|
810
|
+
*/
|
|
811
|
+
declare function wrapFetchWith402(fetchFn: typeof fetch, wallet: {
|
|
812
|
+
address: string;
|
|
813
|
+
chain: ChainName;
|
|
814
|
+
}, privateKey: string): (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
815
|
+
/**
|
|
816
|
+
* Server-side: Generate x402 Payment Required response
|
|
817
|
+
*/
|
|
818
|
+
declare function createPaymentRequiredResponse(requirements: X402PaymentRequirements[]): {
|
|
819
|
+
status: 402;
|
|
820
|
+
headers: Record<string, string>;
|
|
821
|
+
};
|
|
822
|
+
/**
|
|
823
|
+
* Server-side: Verify x402 payment header
|
|
824
|
+
*/
|
|
825
|
+
declare function verifyPaymentHeader(header: string, expectedRecipient: string, expectedAmount: number): {
|
|
826
|
+
valid: boolean;
|
|
827
|
+
error?: string;
|
|
828
|
+
payload?: X402PaymentPayload;
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* CDP (Coinbase Developer Platform) Wallet Integration
|
|
833
|
+
*
|
|
834
|
+
* Creates and manages wallets via Coinbase's CDP SDK.
|
|
835
|
+
* These wallets are hosted by Coinbase, making them easy to use for AI Agents.
|
|
836
|
+
*
|
|
837
|
+
* @see https://docs.cdp.coinbase.com/
|
|
838
|
+
*/
|
|
839
|
+
|
|
840
|
+
interface CDPWalletConfig {
|
|
841
|
+
/** Storage directory (default: ~/.moltspay) */
|
|
842
|
+
storageDir?: string;
|
|
843
|
+
/** Chain name */
|
|
844
|
+
chain?: ChainName;
|
|
845
|
+
/** CDP API credentials (or use env vars) */
|
|
846
|
+
apiKeyId?: string;
|
|
847
|
+
apiKeySecret?: string;
|
|
848
|
+
walletSecret?: string;
|
|
849
|
+
}
|
|
850
|
+
interface CDPWalletData {
|
|
851
|
+
/** Wallet address */
|
|
852
|
+
address: string;
|
|
853
|
+
/** CDP wallet ID */
|
|
854
|
+
walletId: string;
|
|
855
|
+
/** Chain */
|
|
856
|
+
chain: ChainName;
|
|
857
|
+
/** Created timestamp */
|
|
858
|
+
createdAt: string;
|
|
859
|
+
/** CDP account data (for restoration) */
|
|
860
|
+
accountData?: string;
|
|
861
|
+
}
|
|
862
|
+
interface CDPInitResult {
|
|
863
|
+
success: boolean;
|
|
864
|
+
address?: string;
|
|
865
|
+
walletId?: string;
|
|
866
|
+
isNew?: boolean;
|
|
867
|
+
error?: string;
|
|
868
|
+
storagePath?: string;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Check if CDP SDK is available
|
|
872
|
+
*/
|
|
873
|
+
declare function isCDPAvailable(): boolean;
|
|
874
|
+
/**
|
|
875
|
+
* Initialize CDP wallet
|
|
876
|
+
*
|
|
877
|
+
* Creates a new CDP wallet or loads existing one.
|
|
878
|
+
*
|
|
879
|
+
* @example
|
|
880
|
+
* ```bash
|
|
881
|
+
* # Set credentials
|
|
882
|
+
* export CDP_API_KEY_ID=your-key-id
|
|
883
|
+
* export CDP_API_KEY_SECRET=your-key-secret
|
|
884
|
+
*
|
|
885
|
+
* # Initialize
|
|
886
|
+
* npx moltspay init --cdp --chain base
|
|
887
|
+
* ```
|
|
888
|
+
*/
|
|
889
|
+
declare function initCDPWallet(config?: CDPWalletConfig): Promise<CDPInitResult>;
|
|
890
|
+
/**
|
|
891
|
+
* Load existing CDP wallet
|
|
892
|
+
*/
|
|
893
|
+
declare function loadCDPWallet(config?: CDPWalletConfig): CDPWalletData | null;
|
|
894
|
+
/**
|
|
895
|
+
* Get CDP wallet address (quick check without full init)
|
|
896
|
+
*/
|
|
897
|
+
declare function getCDPWalletAddress(storageDir?: string): string | null;
|
|
898
|
+
/**
|
|
899
|
+
* CDP Wallet class for making payments
|
|
900
|
+
*
|
|
901
|
+
* Uses CDP SDK for wallet operations with x402 support.
|
|
902
|
+
*/
|
|
903
|
+
declare class CDPWallet {
|
|
904
|
+
readonly address: string;
|
|
905
|
+
readonly chain: ChainName;
|
|
906
|
+
readonly chainConfig: ReturnType<typeof getChain>;
|
|
907
|
+
private storageDir;
|
|
908
|
+
constructor(config?: CDPWalletConfig);
|
|
909
|
+
/**
|
|
910
|
+
* Get USDC balance
|
|
911
|
+
*/
|
|
912
|
+
getBalance(): Promise<{
|
|
913
|
+
usdc: string;
|
|
914
|
+
eth: string;
|
|
915
|
+
}>;
|
|
916
|
+
/**
|
|
917
|
+
* Transfer USDC to a recipient
|
|
918
|
+
*
|
|
919
|
+
* Requires CDP SDK and credentials to sign transactions.
|
|
920
|
+
*/
|
|
921
|
+
transfer(params: {
|
|
922
|
+
to: string;
|
|
923
|
+
amount: number;
|
|
924
|
+
}): Promise<{
|
|
925
|
+
success: boolean;
|
|
926
|
+
txHash?: string;
|
|
927
|
+
error?: string;
|
|
928
|
+
explorerUrl?: string;
|
|
929
|
+
}>;
|
|
930
|
+
/**
|
|
931
|
+
* Create viem-compatible signer for x402
|
|
932
|
+
*
|
|
933
|
+
* This allows using CDP wallet with x402 protocol.
|
|
934
|
+
*/
|
|
935
|
+
getViemAccount(): Promise<unknown>;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
export { AgentWallet, AuditAction, AuditEntry, AuditLog, BuyerTemplates, type CDPInitResult, CDPWallet, type CDPWalletConfig, type CDPWalletData, ChainConfig, ChainName, CreateInvoiceParams, type EIP3009Authorization, Invoice, PAYMENT_HEADER, PAYMENT_REQUIRED_HEADER, PAYMENT_RESPONSE_HEADER, PaymentAgent, PaymentAgentConfig, type Receipt, type ReceiptParams, SellerTemplates, StatusMarkers, VerifyOptions, VerifyResult, WalletBalance, type X402Client, type X402ClientConfig, type X402PaymentPayload, type X402PaymentRequirements, X402_VERSION, chainToNetwork, createExactEvmPayload, createPaymentRequiredResponse, createX402Client, encodePaymentPayload, formatReceiptJson, formatReceiptMessage, formatReceiptText, generateReceipt, generateReceiptFromInvoice, getAgentAddress, getCDPWalletAddress, getChain, initCDPWallet, isCDPAvailable, isX402Available, loadCDPWallet, networkToChain, parsePaymentRequired, parseStatusMarker, signEIP3009, verifyPaymentHeader, wrapFetchWith402, x402Fetch };
|