@ic402/client 0.1.5 → 2.0.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/dist/client.d.ts +96 -22
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +325 -46
- package/dist/client.js.map +1 -1
- package/dist/evm.d.ts +108 -0
- package/dist/evm.d.ts.map +1 -0
- package/dist/evm.js +437 -0
- package/dist/evm.js.map +1 -0
- package/dist/idl.d.ts +25 -4
- package/dist/idl.d.ts.map +1 -1
- package/dist/idl.js +129 -39
- package/dist/idl.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +107 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/voucher.d.ts +9 -3
- package/dist/voucher.d.ts.map +1 -1
- package/dist/voucher.js +11 -5
- package/dist/voucher.js.map +1 -1
- package/package.json +8 -8
package/dist/client.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
maxTotal?: bigint;
|
|
7
|
-
maxSessionDeposit?: bigint;
|
|
8
|
-
alertThreshold?: bigint;
|
|
1
|
+
/** Minimal identity interface — any ICP identity that can provide a principal. */
|
|
2
|
+
export interface Ic402Identity {
|
|
3
|
+
getPrincipal(): {
|
|
4
|
+
toText(): string;
|
|
5
|
+
};
|
|
9
6
|
}
|
|
7
|
+
import type { ContentDelivery, Job, PaymentReceipt, ServiceDefinition } from './types.js';
|
|
8
|
+
import { type VoucherSigner } from './voucher.js';
|
|
9
|
+
import { type FetchX402Result } from './evm.js';
|
|
10
10
|
export interface SessionPreferences {
|
|
11
11
|
preferSession?: boolean;
|
|
12
12
|
maxDeposit?: bigint;
|
|
@@ -22,26 +22,40 @@ export interface SessionPreferences {
|
|
|
22
22
|
evmToken?: string;
|
|
23
23
|
/** For EVM sessions: the canister's EVM address (settlement recipient). */
|
|
24
24
|
evmRecipient?: string;
|
|
25
|
+
/** For EVM sessions: the EIP-3009 authorization (signed by the payer). */
|
|
26
|
+
authorization?: {
|
|
27
|
+
from: string;
|
|
28
|
+
to: string;
|
|
29
|
+
value: number | bigint;
|
|
30
|
+
validAfter: number | bigint;
|
|
31
|
+
validBefore: number | bigint;
|
|
32
|
+
nonce: number[];
|
|
33
|
+
v: number;
|
|
34
|
+
r: number[];
|
|
35
|
+
s: number[];
|
|
36
|
+
};
|
|
25
37
|
}
|
|
26
38
|
export interface Ic402ClientConfig {
|
|
39
|
+
/** Target canister ID. Required for all operations. */
|
|
40
|
+
canisterId: string;
|
|
41
|
+
/** Factory to create actors for canister calls. Required for all operations. */
|
|
42
|
+
actorFactory: (canisterId: string) => any;
|
|
27
43
|
/** ICP identity for signing payments */
|
|
28
|
-
identity:
|
|
29
|
-
/** CAIP-2 network identifier */
|
|
44
|
+
identity: Ic402Identity | null;
|
|
45
|
+
/** CAIP-2 network identifier (e.g., "icp:1" for ICP, "eip155:84532" for Base Sepolia) */
|
|
30
46
|
network: string;
|
|
31
|
-
/** Automatically handle 402 responses */
|
|
47
|
+
/** Automatically handle 402 responses (ICP: ICRC-2 approve + retry) */
|
|
32
48
|
autoPayment?: boolean;
|
|
33
|
-
/** Budget limits */
|
|
34
|
-
budget?: BudgetConfig;
|
|
35
49
|
/** Session preferences */
|
|
36
50
|
sessions?: SessionPreferences;
|
|
37
|
-
/**
|
|
38
|
-
onBudgetAlert?: (spent: bigint, limit: bigint) => Promise<void>;
|
|
39
|
-
/** Ledger canister ID for ICRC-2 auto-approval */
|
|
51
|
+
/** Ledger canister ID for ICRC-2 auto-approval (ICP payments) */
|
|
40
52
|
ledger?: string;
|
|
41
|
-
/**
|
|
42
|
-
canisterId?: string;
|
|
43
|
-
/** Factory to create a ledger actor for ICRC-2 calls. Required for autoPayment. */
|
|
53
|
+
/** Factory for ledger actors. Required for ICP auto-payment. */
|
|
44
54
|
ledgerActorFactory?: (ledgerCanisterId: string) => any;
|
|
55
|
+
/** Custom EVM RPC URL. If omitted, uses a public RPC for the chain. */
|
|
56
|
+
evmRpcUrl?: string;
|
|
57
|
+
/** Fee buffer added to ICRC-2 approval amount (default: 100_000). */
|
|
58
|
+
approvalFeeBuffer?: bigint;
|
|
45
59
|
}
|
|
46
60
|
export interface SessionHandle {
|
|
47
61
|
id: string;
|
|
@@ -60,20 +74,19 @@ export interface SessionHandle {
|
|
|
60
74
|
*/
|
|
61
75
|
export declare class Ic402Client {
|
|
62
76
|
private config;
|
|
63
|
-
private totalSpent;
|
|
64
77
|
constructor(config: Ic402ClientConfig);
|
|
65
78
|
/**
|
|
66
79
|
* Call a canister method, auto-handling 402 payment if needed.
|
|
67
80
|
*
|
|
68
81
|
* Flow: call method → if #paymentRequired → icrc2_approve → create sig → retry
|
|
69
82
|
*/
|
|
70
|
-
call(
|
|
83
|
+
call(method: string, args: unknown[], canisterId?: string): Promise<unknown>;
|
|
71
84
|
/**
|
|
72
85
|
* Open a streaming session with escrow deposit.
|
|
73
86
|
*
|
|
74
87
|
* Flow: requestSession → calculate deposit → icrc2_approve → openSession → SessionHandle
|
|
75
88
|
*/
|
|
76
|
-
openSession(
|
|
89
|
+
openSession(sessionConfig?: Partial<SessionPreferences>, signer?: VoucherSigner, canisterId?: string): Promise<SessionHandle>;
|
|
77
90
|
/**
|
|
78
91
|
* Fetch content from a ContentDelivery response.
|
|
79
92
|
* Handles all delivery methods: inline, httpUrl, canisterQuery, assetCanister.
|
|
@@ -82,6 +95,67 @@ export declare class Ic402Client {
|
|
|
82
95
|
canisterId?: string;
|
|
83
96
|
actorFactory?: (canisterId: string) => any;
|
|
84
97
|
}): Promise<Uint8Array>;
|
|
98
|
+
/**
|
|
99
|
+
* Fetch from an x402-gated URL. The full flow:
|
|
100
|
+
* 1. Client probes the URL → gets 402 with payment requirements
|
|
101
|
+
* 2. Client calls canister signX402Payment → gets signed EIP-3009 header
|
|
102
|
+
* 3. Client retries the URL with the signed X-Payment header
|
|
103
|
+
*
|
|
104
|
+
* @param url - The x402-gated URL to fetch
|
|
105
|
+
* @param actorFactory - Factory to create canister actor
|
|
106
|
+
*/
|
|
107
|
+
fetchX402(url: string, options?: {
|
|
108
|
+
init?: RequestInit;
|
|
109
|
+
chainId?: number;
|
|
110
|
+
}): Promise<FetchX402Result>;
|
|
111
|
+
/**
|
|
112
|
+
* Register the canister as an agent on the ERC-8004 IdentityRegistry.
|
|
113
|
+
* Full flow: get chain state → canister signs → client broadcasts → poll receipt.
|
|
114
|
+
*
|
|
115
|
+
* @param actorFactory - Factory to create canister actor
|
|
116
|
+
* @param rpcUrl - Optional custom RPC URL for the target chain
|
|
117
|
+
*/
|
|
118
|
+
registerAgent(rpcUrl?: string, chainId?: number): Promise<{
|
|
119
|
+
tokenId: bigint | null;
|
|
120
|
+
txHash: string;
|
|
121
|
+
}>;
|
|
122
|
+
/**
|
|
123
|
+
* Sign and broadcast an ERC-20 transfer.
|
|
124
|
+
* Client fetches chain state, canister signs, client broadcasts.
|
|
125
|
+
*/
|
|
126
|
+
sendErc20Transfer(tokenAddress: string, recipient: string, amount: bigint, rpcUrl?: string): Promise<{
|
|
127
|
+
txHash: string;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* Sign and broadcast a native ETH transfer.
|
|
131
|
+
*/
|
|
132
|
+
sendEthTransfer(recipient: string, amountWei: bigint, rpcUrl?: string): Promise<{
|
|
133
|
+
txHash: string;
|
|
134
|
+
}>;
|
|
135
|
+
/** Extract numeric chain ID from CAIP-2 network string, or null if not EVM. */
|
|
136
|
+
private tryExtractChainId;
|
|
137
|
+
/** Extract chain ID or throw. */
|
|
138
|
+
private extractChainId;
|
|
139
|
+
/**
|
|
140
|
+
* List available services from the canister.
|
|
141
|
+
*/
|
|
142
|
+
listServices(): Promise<ServiceDefinition[]>;
|
|
143
|
+
/**
|
|
144
|
+
* Submit a paid service request. Handles x402 payment automatically.
|
|
145
|
+
* Returns the job ID for polling.
|
|
146
|
+
*/
|
|
147
|
+
submitServiceRequest(serviceId: string, params: Uint8Array): Promise<{
|
|
148
|
+
jobId: string;
|
|
149
|
+
}>;
|
|
150
|
+
/**
|
|
151
|
+
* Poll for a job result until completed or max attempts reached.
|
|
152
|
+
* Returns the full job record when done.
|
|
153
|
+
*/
|
|
154
|
+
pollJobResult(jobId: string, maxAttempts?: number, intervalMs?: number): Promise<Job>;
|
|
155
|
+
/**
|
|
156
|
+
* Dispute a job result (for BuyerConfirm verification).
|
|
157
|
+
*/
|
|
158
|
+
disputeJob(jobId: string, reason: string): Promise<void>;
|
|
85
159
|
/**
|
|
86
160
|
* Discover agents via ERC-8004 registries.
|
|
87
161
|
*
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,kFAAkF;AAClF,MAAM,WAAW,aAAa;IAC5B,YAAY,IAAI;QAAE,MAAM,IAAI,MAAM,CAAA;KAAE,CAAC;CACtC;AAQD,OAAO,KAAK,EACV,eAAe,EACf,GAAG,EACH,cAAc,EACd,iBAAiB,EAMlB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAe,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EASL,KAAK,eAAe,EACrB,MAAM,UAAU,CAAC;AAElB,MAAM,WAAW,kBAAkB;IACjC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,uEAAuE;IACvE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6FAA6F;IAC7F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uEAAuE;I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