@pay-skill/sdk 0.1.7 → 0.1.10
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 +80 -91
- package/dist/auth.d.ts +11 -6
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +19 -7
- package/dist/auth.js.map +1 -1
- package/dist/errors.d.ts +4 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +8 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +2 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -6
- package/dist/index.js.map +1 -1
- package/dist/keychain.d.ts +8 -0
- package/dist/keychain.d.ts.map +1 -0
- package/dist/keychain.js +17 -0
- package/dist/keychain.js.map +1 -0
- package/dist/wallet.d.ts +135 -104
- package/dist/wallet.d.ts.map +1 -1
- package/dist/wallet.js +683 -280
- package/dist/wallet.js.map +1 -1
- package/jsr.json +1 -1
- package/package.json +5 -2
- package/src/auth.ts +28 -18
- package/src/errors.ts +10 -3
- package/src/index.ts +12 -39
- package/src/keychain.ts +18 -0
- package/src/wallet.ts +1074 -355
- package/tests/test_auth_rejection.ts +43 -95
- package/tests/test_crypto.ts +59 -172
- package/tests/test_e2e.ts +46 -105
- package/tests/test_errors.ts +9 -1
- package/tests/test_ows.ts +153 -0
- package/tests/test_wallet.ts +194 -0
- package/dist/client.d.ts +0 -94
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -443
- package/dist/client.js.map +0 -1
- package/dist/models.d.ts +0 -78
- package/dist/models.d.ts.map +0 -1
- package/dist/models.js +0 -2
- package/dist/models.js.map +0 -1
- package/dist/ows-signer.d.ts +0 -75
- package/dist/ows-signer.d.ts.map +0 -1
- package/dist/ows-signer.js +0 -130
- package/dist/ows-signer.js.map +0 -1
- package/dist/signer.d.ts +0 -46
- package/dist/signer.d.ts.map +0 -1
- package/dist/signer.js +0 -111
- package/dist/signer.js.map +0 -1
- package/src/client.ts +0 -644
- package/src/models.ts +0 -77
- package/src/ows-signer.ts +0 -223
- package/src/signer.ts +0 -147
- package/tests/test_ows_integration.ts +0 -92
- package/tests/test_ows_signer.ts +0 -365
- package/tests/test_signer.ts +0 -47
- package/tests/test_validation.ts +0 -66
package/src/wallet.ts
CHANGED
|
@@ -1,30 +1,155 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Wallet —
|
|
3
|
-
* Wraps PayClient with private key signing and balance tracking.
|
|
2
|
+
* Wallet — the single entry point for the pay SDK.
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Zero-config for agents: new Wallet() (reads PAYSKILL_KEY env)
|
|
5
|
+
* Explicit key: new Wallet({ privateKey: "0x..." })
|
|
6
|
+
* OS keychain (CLI key): await Wallet.create()
|
|
7
|
+
* OWS wallet extension: await Wallet.fromOws({ walletId: "..." })
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import { type Hex, type Address } from "viem";
|
|
10
11
|
import { privateKeyToAccount, type PrivateKeyAccount } from "viem/accounts";
|
|
11
|
-
import { buildAuthHeaders,
|
|
12
|
+
import { buildAuthHeaders, buildAuthHeadersSigned } from "./auth.js";
|
|
13
|
+
import { signTransferAuthorization, combinedSignature } from "./eip3009.js";
|
|
14
|
+
import { readFromKeychain } from "./keychain.js";
|
|
15
|
+
import {
|
|
16
|
+
PayError,
|
|
17
|
+
PayValidationError,
|
|
18
|
+
PayNetworkError,
|
|
19
|
+
PayServerError,
|
|
20
|
+
PayInsufficientFundsError,
|
|
21
|
+
} from "./errors.js";
|
|
22
|
+
|
|
23
|
+
// ── Constants ────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
const MAINNET_API_URL = "https://pay-skill.com/api/v1";
|
|
26
|
+
const TESTNET_API_URL = "https://testnet.pay-skill.com/api/v1";
|
|
27
|
+
const ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
|
|
28
|
+
const KEY_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
29
|
+
const DIRECT_MIN_MICRO = 1_000_000; // $1.00
|
|
30
|
+
const TAB_MIN_MICRO = 5_000_000; // $5.00
|
|
31
|
+
const TAB_MULTIPLIER = 10;
|
|
32
|
+
const DEFAULT_TIMEOUT = 30_000;
|
|
33
|
+
|
|
34
|
+
// Internal sentinel for OWS construction
|
|
35
|
+
const _OWS_INIT = Symbol("ows-init");
|
|
36
|
+
|
|
37
|
+
// ── Public Types ─────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/** Dollar amount (default) or micro-USDC for precision. */
|
|
40
|
+
export type Amount = number | { micro: number };
|
|
12
41
|
|
|
13
42
|
export interface WalletOptions {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
|
|
43
|
+
/** Hex private key. If omitted, reads from PAYSKILL_KEY env var. */
|
|
44
|
+
privateKey?: string;
|
|
45
|
+
/** Use Base Sepolia testnet. Default: false (mainnet). Also reads PAYSKILL_TESTNET env. */
|
|
46
|
+
testnet?: boolean;
|
|
47
|
+
/** Request timeout in ms. Default: 30000. */
|
|
48
|
+
timeout?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface OwsWalletOptions {
|
|
52
|
+
/** OWS wallet name or UUID (e.g. "pay-my-agent"). */
|
|
53
|
+
walletId: string;
|
|
54
|
+
/** OWS API key token (passed as passphrase to OWS signing calls). */
|
|
55
|
+
owsApiKey?: string;
|
|
56
|
+
/** Use Base Sepolia testnet. Default: false (mainnet). */
|
|
57
|
+
testnet?: boolean;
|
|
58
|
+
/** Request timeout in ms. Default: 30000. */
|
|
59
|
+
timeout?: number;
|
|
60
|
+
/** @internal Inject OWS module for testing. */
|
|
61
|
+
_owsModule?: unknown;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface SendResult {
|
|
65
|
+
txHash: string;
|
|
66
|
+
status: string;
|
|
67
|
+
amount: number;
|
|
68
|
+
fee: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface Tab {
|
|
72
|
+
id: string;
|
|
73
|
+
provider: string;
|
|
74
|
+
amount: number;
|
|
75
|
+
balanceRemaining: number;
|
|
76
|
+
totalCharged: number;
|
|
77
|
+
chargeCount: number;
|
|
78
|
+
maxChargePerCall: number;
|
|
79
|
+
totalWithdrawn: number;
|
|
80
|
+
status: "open" | "closed";
|
|
81
|
+
pendingChargeCount: number;
|
|
82
|
+
pendingChargeTotal: number;
|
|
83
|
+
effectiveBalance: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface ChargeResult {
|
|
87
|
+
chargeId: string;
|
|
88
|
+
status: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface Balance {
|
|
92
|
+
total: number;
|
|
93
|
+
locked: number;
|
|
94
|
+
available: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface Status {
|
|
98
|
+
address: string;
|
|
99
|
+
balance: Balance;
|
|
100
|
+
openTabs: number;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface DiscoverService {
|
|
104
|
+
name: string;
|
|
105
|
+
description: string;
|
|
106
|
+
baseUrl: string;
|
|
107
|
+
category: string;
|
|
108
|
+
keywords: string[];
|
|
109
|
+
routes: {
|
|
110
|
+
path: string;
|
|
111
|
+
method?: string;
|
|
112
|
+
price?: string;
|
|
113
|
+
settlement?: string;
|
|
114
|
+
}[];
|
|
115
|
+
docsUrl?: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface DiscoverOptions {
|
|
119
|
+
sort?: string;
|
|
120
|
+
category?: string;
|
|
121
|
+
settlement?: string;
|
|
20
122
|
}
|
|
21
123
|
|
|
22
124
|
export interface FundLinkOptions {
|
|
23
|
-
|
|
125
|
+
message?: string;
|
|
24
126
|
agentName?: string;
|
|
25
127
|
}
|
|
26
128
|
|
|
27
|
-
export interface
|
|
129
|
+
export interface WebhookRegistration {
|
|
130
|
+
id: string;
|
|
131
|
+
url: string;
|
|
132
|
+
events: string[];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface MintResult {
|
|
136
|
+
txHash: string;
|
|
137
|
+
amount: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ── Private Types ────────────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
interface Contracts {
|
|
143
|
+
router: Address;
|
|
144
|
+
tab: Address;
|
|
145
|
+
direct: Address;
|
|
146
|
+
fee: Address;
|
|
147
|
+
usdc: Address;
|
|
148
|
+
chainId: number;
|
|
149
|
+
relayer: Address;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
interface Permit {
|
|
28
153
|
nonce: string;
|
|
29
154
|
deadline: number;
|
|
30
155
|
v: number;
|
|
@@ -32,414 +157,1008 @@ export interface PermitResult {
|
|
|
32
157
|
s: string;
|
|
33
158
|
}
|
|
34
159
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
160
|
+
type SignTypedDataFn = (params: {
|
|
161
|
+
domain: Record<string, unknown>;
|
|
162
|
+
types: Record<string, readonly { name: string; type: string }[]>;
|
|
163
|
+
primaryType: string;
|
|
164
|
+
message: Record<string, unknown>;
|
|
165
|
+
}) => Promise<string>;
|
|
166
|
+
|
|
167
|
+
// Raw server response (snake_case)
|
|
168
|
+
interface RawTab {
|
|
169
|
+
tab_id: string;
|
|
170
|
+
provider: string;
|
|
171
|
+
amount: number;
|
|
172
|
+
balance_remaining: number;
|
|
173
|
+
total_charged: number;
|
|
174
|
+
charge_count: number;
|
|
175
|
+
max_charge_per_call: number;
|
|
176
|
+
total_withdrawn: number;
|
|
177
|
+
status: "open" | "closed";
|
|
178
|
+
pending_charge_count: number;
|
|
179
|
+
pending_charge_total: number;
|
|
180
|
+
effective_balance: number;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Subset of @open-wallet-standard/core we call at runtime. */
|
|
184
|
+
interface OwsModule {
|
|
185
|
+
getWallet(
|
|
186
|
+
nameOrId: string,
|
|
187
|
+
vaultPath?: string,
|
|
188
|
+
): {
|
|
189
|
+
id: string;
|
|
190
|
+
name: string;
|
|
191
|
+
accounts: Array<{
|
|
192
|
+
chainId: string;
|
|
193
|
+
address: string;
|
|
194
|
+
derivationPath: string;
|
|
195
|
+
}>;
|
|
196
|
+
};
|
|
197
|
+
signTypedData(
|
|
198
|
+
wallet: string,
|
|
199
|
+
chain: string,
|
|
200
|
+
typedDataJson: string,
|
|
201
|
+
passphrase?: string,
|
|
202
|
+
index?: number,
|
|
203
|
+
vaultPath?: string,
|
|
204
|
+
): { signature: string; recoveryId?: number };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ── Helpers ──────────────────────────────────────────────────────────
|
|
208
|
+
|
|
209
|
+
function normalizeKey(key: string): Hex {
|
|
210
|
+
const clean = key.startsWith("0x") ? key : "0x" + key;
|
|
211
|
+
if (!KEY_RE.test(clean)) {
|
|
212
|
+
throw new PayValidationError(
|
|
213
|
+
"Invalid private key: must be 32 bytes hex",
|
|
214
|
+
"privateKey",
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return clean as Hex;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function validateAddress(address: string): void {
|
|
221
|
+
if (!ADDRESS_RE.test(address)) {
|
|
222
|
+
throw new PayValidationError(
|
|
223
|
+
`Invalid Ethereum address: ${address}`,
|
|
224
|
+
"address",
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function toMicro(amount: Amount): number {
|
|
230
|
+
if (typeof amount === "number") {
|
|
231
|
+
if (!Number.isFinite(amount) || amount < 0) {
|
|
232
|
+
throw new PayValidationError(
|
|
233
|
+
"Amount must be a positive finite number",
|
|
234
|
+
"amount",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return Math.round(amount * 1_000_000);
|
|
238
|
+
}
|
|
239
|
+
if (!Number.isInteger(amount.micro) || amount.micro < 0) {
|
|
240
|
+
throw new PayValidationError(
|
|
241
|
+
"Micro amount must be a non-negative integer",
|
|
242
|
+
"amount",
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
return amount.micro;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function toDollars(micro: number): number {
|
|
249
|
+
return micro / 1_000_000;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function parseTab(raw: RawTab): Tab {
|
|
253
|
+
return {
|
|
254
|
+
id: raw.tab_id,
|
|
255
|
+
provider: raw.provider,
|
|
256
|
+
amount: toDollars(raw.amount),
|
|
257
|
+
balanceRemaining: toDollars(raw.balance_remaining),
|
|
258
|
+
totalCharged: toDollars(raw.total_charged),
|
|
259
|
+
chargeCount: raw.charge_count,
|
|
260
|
+
maxChargePerCall: toDollars(raw.max_charge_per_call),
|
|
261
|
+
totalWithdrawn: toDollars(raw.total_withdrawn),
|
|
262
|
+
status: raw.status,
|
|
263
|
+
pendingChargeCount: raw.pending_charge_count,
|
|
264
|
+
pendingChargeTotal: toDollars(raw.pending_charge_total),
|
|
265
|
+
effectiveBalance: toDollars(raw.effective_balance),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function parseSig(signature: string): { v: number; r: string; s: string } {
|
|
270
|
+
const sig = signature.startsWith("0x")
|
|
271
|
+
? signature.slice(2)
|
|
272
|
+
: signature;
|
|
273
|
+
return {
|
|
274
|
+
v: parseInt(sig.slice(128, 130), 16),
|
|
275
|
+
r: "0x" + sig.slice(0, 64),
|
|
276
|
+
s: "0x" + sig.slice(64, 128),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function resolveApiUrl(testnet: boolean): string {
|
|
281
|
+
return (
|
|
282
|
+
process.env.PAYSKILL_API_URL ??
|
|
283
|
+
(testnet ? TESTNET_API_URL : MAINNET_API_URL)
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function createOwsSignTypedData(
|
|
288
|
+
ows: OwsModule,
|
|
289
|
+
walletId: string,
|
|
290
|
+
owsApiKey?: string,
|
|
291
|
+
): SignTypedDataFn {
|
|
292
|
+
return async (params) => {
|
|
293
|
+
// Build EIP712Domain type from domain fields
|
|
294
|
+
const domainType: Array<{ name: string; type: string }> = [];
|
|
295
|
+
const d = params.domain;
|
|
296
|
+
if (d.name !== undefined)
|
|
297
|
+
domainType.push({ name: "name", type: "string" });
|
|
298
|
+
if (d.version !== undefined)
|
|
299
|
+
domainType.push({ name: "version", type: "string" });
|
|
300
|
+
if (d.chainId !== undefined)
|
|
301
|
+
domainType.push({ name: "chainId", type: "uint256" });
|
|
302
|
+
if (d.verifyingContract !== undefined)
|
|
303
|
+
domainType.push({ name: "verifyingContract", type: "address" });
|
|
304
|
+
|
|
305
|
+
const fullTypedData = {
|
|
306
|
+
types: {
|
|
307
|
+
EIP712Domain: domainType,
|
|
308
|
+
...Object.fromEntries(
|
|
309
|
+
Object.entries(params.types).map(([k, v]) => [k, [...v]]),
|
|
310
|
+
),
|
|
311
|
+
},
|
|
312
|
+
primaryType: params.primaryType,
|
|
313
|
+
domain: params.domain,
|
|
314
|
+
message: params.message,
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const json = JSON.stringify(fullTypedData, (_key, v) =>
|
|
318
|
+
typeof v === "bigint" ? v.toString() : (v as unknown),
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const result = ows.signTypedData(walletId, "evm", json, owsApiKey);
|
|
322
|
+
|
|
323
|
+
const sig = result.signature.startsWith("0x")
|
|
324
|
+
? result.signature.slice(2)
|
|
325
|
+
: result.signature;
|
|
326
|
+
if (sig.length === 130) return `0x${sig}` as `0x${string}`;
|
|
327
|
+
const v = (result.recoveryId ?? 0) + 27;
|
|
328
|
+
return `0x${sig}${v.toString(16).padStart(2, "0")}` as `0x${string}`;
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ── Standalone discover (no wallet needed) ───────────────────────────
|
|
333
|
+
|
|
334
|
+
export async function discover(
|
|
335
|
+
query?: string,
|
|
336
|
+
options?: DiscoverOptions & { testnet?: boolean },
|
|
337
|
+
): Promise<DiscoverService[]> {
|
|
338
|
+
const testnet = options?.testnet ?? !!process.env.PAYSKILL_TESTNET;
|
|
339
|
+
const apiUrl = resolveApiUrl(testnet);
|
|
340
|
+
return discoverImpl(apiUrl, DEFAULT_TIMEOUT, query, options);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async function discoverImpl(
|
|
344
|
+
apiUrl: string,
|
|
345
|
+
timeout: number,
|
|
346
|
+
query?: string,
|
|
347
|
+
options?: DiscoverOptions,
|
|
348
|
+
): Promise<DiscoverService[]> {
|
|
349
|
+
const params = new URLSearchParams();
|
|
350
|
+
if (query) params.set("q", query);
|
|
351
|
+
if (options?.sort) params.set("sort", options.sort);
|
|
352
|
+
if (options?.category) params.set("category", options.category);
|
|
353
|
+
if (options?.settlement) params.set("settlement", options.settlement);
|
|
354
|
+
const qs = params.toString();
|
|
355
|
+
const url = `${apiUrl}/discover${qs ? `?${qs}` : ""}`;
|
|
356
|
+
const resp = await fetch(url, { signal: AbortSignal.timeout(timeout) });
|
|
357
|
+
if (!resp.ok) {
|
|
358
|
+
throw new PayServerError(`discover failed: ${resp.status}`, resp.status);
|
|
359
|
+
}
|
|
360
|
+
const data = (await resp.json()) as {
|
|
361
|
+
services: Array<Record<string, unknown>>;
|
|
362
|
+
};
|
|
363
|
+
return data.services.map((s) => ({
|
|
364
|
+
name: String(s.name ?? ""),
|
|
365
|
+
description: String(s.description ?? ""),
|
|
366
|
+
baseUrl: String(s.base_url ?? s.baseUrl ?? ""),
|
|
367
|
+
category: String(s.category ?? ""),
|
|
368
|
+
keywords: (s.keywords as string[]) ?? [],
|
|
369
|
+
routes: (s.routes as DiscoverService["routes"]) ?? [],
|
|
370
|
+
docsUrl: s.docs_url != null || s.docsUrl != null
|
|
371
|
+
? String(s.docs_url ?? s.docsUrl)
|
|
372
|
+
: undefined,
|
|
373
|
+
}));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ── Wallet ───────────────────────────────────────────────────────────
|
|
40
377
|
|
|
41
378
|
export class Wallet {
|
|
42
379
|
readonly address: string;
|
|
43
|
-
|
|
44
|
-
private
|
|
45
|
-
private
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
380
|
+
|
|
381
|
+
// Signing: signTypedData works for both private key and OWS.
|
|
382
|
+
// rawKey is non-null only for private-key wallets (needed for x402 direct / EIP-3009).
|
|
383
|
+
#signTypedData: SignTypedDataFn;
|
|
384
|
+
#rawKey: Hex | null;
|
|
385
|
+
#apiUrl: string;
|
|
386
|
+
#basePath: string;
|
|
387
|
+
#testnet: boolean;
|
|
388
|
+
#timeout: number;
|
|
389
|
+
#contracts: Contracts | null = null;
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Sync constructor. Resolves key from: privateKey arg -> PAYSKILL_KEY env -> error.
|
|
393
|
+
* For OS keychain, use `await Wallet.create()`.
|
|
394
|
+
* For OWS, use `await Wallet.fromOws({ walletId })`.
|
|
395
|
+
*/
|
|
396
|
+
constructor(options?: WalletOptions) {
|
|
397
|
+
// Check for internal OWS init (symbol key hidden from public API)
|
|
398
|
+
const raw = options as Record<symbol, unknown> | undefined;
|
|
399
|
+
if (raw && raw[_OWS_INIT]) {
|
|
400
|
+
const init = raw as unknown as {
|
|
401
|
+
[_OWS_INIT]: true;
|
|
402
|
+
_address: string;
|
|
403
|
+
_signTypedData: SignTypedDataFn;
|
|
404
|
+
_testnet: boolean;
|
|
405
|
+
_timeout: number;
|
|
406
|
+
};
|
|
407
|
+
this.address = init._address;
|
|
408
|
+
this.#signTypedData = init._signTypedData;
|
|
409
|
+
this.#rawKey = null;
|
|
410
|
+
this.#testnet = init._testnet;
|
|
411
|
+
this.#timeout = init._timeout;
|
|
412
|
+
this.#apiUrl = resolveApiUrl(this.#testnet);
|
|
413
|
+
try {
|
|
414
|
+
this.#basePath = new URL(this.#apiUrl).pathname.replace(
|
|
415
|
+
/\/+$/,
|
|
416
|
+
"",
|
|
417
|
+
);
|
|
418
|
+
} catch {
|
|
419
|
+
this.#basePath = "";
|
|
420
|
+
}
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const key = options?.privateKey ?? process.env.PAYSKILL_KEY;
|
|
425
|
+
if (!key) {
|
|
426
|
+
throw new PayError(
|
|
427
|
+
"No private key found. Provide { privateKey }, set PAYSKILL_KEY env var, " +
|
|
428
|
+
"or use Wallet.create() to read from OS keychain.",
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
this.#rawKey = normalizeKey(key);
|
|
432
|
+
const account = privateKeyToAccount(this.#rawKey);
|
|
433
|
+
this.address = account.address;
|
|
434
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
435
|
+
this.#signTypedData = (p) => account.signTypedData(p as any);
|
|
436
|
+
this.#testnet = options?.testnet ?? !!process.env.PAYSKILL_TESTNET;
|
|
437
|
+
this.#timeout = options?.timeout ?? DEFAULT_TIMEOUT;
|
|
438
|
+
this.#apiUrl = resolveApiUrl(this.#testnet);
|
|
64
439
|
try {
|
|
65
|
-
this
|
|
440
|
+
this.#basePath = new URL(this.#apiUrl).pathname.replace(/\/+$/, "");
|
|
66
441
|
} catch {
|
|
67
|
-
this
|
|
442
|
+
this.#basePath = "";
|
|
68
443
|
}
|
|
69
444
|
}
|
|
70
445
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
446
|
+
/** Async factory. Resolves key from: privateKey arg -> OS keychain -> PAYSKILL_KEY env -> error. */
|
|
447
|
+
static async create(options?: WalletOptions): Promise<Wallet> {
|
|
448
|
+
if (options?.privateKey) return new Wallet(options);
|
|
449
|
+
const keychainKey = await readFromKeychain();
|
|
450
|
+
if (keychainKey) {
|
|
451
|
+
return new Wallet({ ...options, privateKey: keychainKey });
|
|
452
|
+
}
|
|
453
|
+
return new Wallet(options);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Sync factory. Reads key from PAYSKILL_KEY env var only. */
|
|
457
|
+
static fromEnv(options?: { testnet?: boolean }): Wallet {
|
|
458
|
+
const key = process.env.PAYSKILL_KEY;
|
|
459
|
+
if (!key) throw new PayError("PAYSKILL_KEY env var not set");
|
|
460
|
+
return new Wallet({ privateKey: key, testnet: options?.testnet });
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** Async factory. Creates a wallet backed by an OWS (Open Wallet Standard) wallet. */
|
|
464
|
+
static async fromOws(options: OwsWalletOptions): Promise<Wallet> {
|
|
465
|
+
let owsModule: OwsModule;
|
|
466
|
+
if (options._owsModule) {
|
|
467
|
+
owsModule = options._owsModule as OwsModule;
|
|
468
|
+
} else {
|
|
469
|
+
try {
|
|
470
|
+
const moduleName = "@open-wallet-standard/core";
|
|
471
|
+
owsModule = (await import(moduleName)) as unknown as OwsModule;
|
|
472
|
+
} catch {
|
|
473
|
+
throw new PayError(
|
|
474
|
+
"@open-wallet-standard/core is not installed. " +
|
|
475
|
+
"Install it with: npm install @open-wallet-standard/core",
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const walletInfo = owsModule.getWallet(options.walletId);
|
|
481
|
+
const evmAccount = walletInfo.accounts.find(
|
|
482
|
+
(a) => a.chainId === "evm" || a.chainId.startsWith("eip155:"),
|
|
483
|
+
);
|
|
484
|
+
if (!evmAccount) {
|
|
485
|
+
throw new PayError(
|
|
486
|
+
`No EVM account found in OWS wallet '${options.walletId}'. ` +
|
|
487
|
+
`Available chains: ${walletInfo.accounts.map((a) => a.chainId).join(", ") || "none"}.`,
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const signFn = createOwsSignTypedData(
|
|
492
|
+
owsModule,
|
|
493
|
+
options.walletId,
|
|
494
|
+
options.owsApiKey,
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
// Use the internal init path through the constructor
|
|
498
|
+
return new Wallet({
|
|
499
|
+
[_OWS_INIT]: true,
|
|
500
|
+
_address: evmAccount.address,
|
|
501
|
+
_signTypedData: signFn,
|
|
502
|
+
_testnet: options.testnet ?? !!process.env.PAYSKILL_TESTNET,
|
|
503
|
+
_timeout: options.timeout ?? DEFAULT_TIMEOUT,
|
|
504
|
+
} as unknown as WalletOptions);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// ── Internal: contracts ──────────────────────────────────────────
|
|
508
|
+
|
|
509
|
+
private async ensureContracts(): Promise<Contracts> {
|
|
510
|
+
if (this.#contracts) return this.#contracts;
|
|
511
|
+
let resp: Response;
|
|
512
|
+
try {
|
|
513
|
+
resp = await fetch(`${this.#apiUrl}/contracts`, {
|
|
514
|
+
signal: AbortSignal.timeout(this.#timeout),
|
|
515
|
+
});
|
|
516
|
+
} catch (e) {
|
|
517
|
+
throw new PayNetworkError(`Failed to reach server: ${e}`);
|
|
518
|
+
}
|
|
519
|
+
if (!resp.ok) {
|
|
520
|
+
throw new PayNetworkError(
|
|
521
|
+
`Failed to fetch contracts: ${resp.status}`,
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
const data = (await resp.json()) as Record<string, unknown>;
|
|
525
|
+
this.#contracts = {
|
|
526
|
+
router: String(data.router ?? "") as Address,
|
|
527
|
+
tab: String(data.tab ?? "") as Address,
|
|
528
|
+
direct: String(data.direct ?? "") as Address,
|
|
529
|
+
fee: String(data.fee ?? "") as Address,
|
|
530
|
+
usdc: String(data.usdc ?? "") as Address,
|
|
531
|
+
chainId: Number(data.chain_id ?? 0),
|
|
532
|
+
relayer: String(data.relayer ?? "") as Address,
|
|
75
533
|
};
|
|
534
|
+
return this.#contracts;
|
|
76
535
|
}
|
|
77
536
|
|
|
78
|
-
|
|
79
|
-
|
|
537
|
+
// ── Internal: HTTP ───────────────────────────────────────────────
|
|
538
|
+
|
|
539
|
+
private async authFetch(
|
|
80
540
|
path: string,
|
|
81
|
-
init: RequestInit = {}
|
|
541
|
+
init: RequestInit = {},
|
|
82
542
|
): Promise<Response> {
|
|
543
|
+
const contracts = await this.ensureContracts();
|
|
83
544
|
const method = (init.method ?? "GET").toUpperCase();
|
|
84
|
-
// Sign only the path portion (no query string) — server verifies against uri.path().
|
|
85
545
|
const pathOnly = path.split("?")[0];
|
|
86
|
-
const signPath = this
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
546
|
+
const signPath = this.#basePath + pathOnly;
|
|
547
|
+
const config = {
|
|
548
|
+
chainId: contracts.chainId,
|
|
549
|
+
routerAddress: contracts.router,
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
const headers = this.#rawKey
|
|
553
|
+
? await buildAuthHeaders(this.#rawKey, method, signPath, config)
|
|
554
|
+
: await buildAuthHeadersSigned(
|
|
555
|
+
this.address,
|
|
556
|
+
this.#signTypedData,
|
|
557
|
+
method,
|
|
558
|
+
signPath,
|
|
559
|
+
config,
|
|
560
|
+
);
|
|
561
|
+
|
|
562
|
+
return fetch(`${this.#apiUrl}${path}`, {
|
|
94
563
|
...init,
|
|
564
|
+
signal: init.signal ?? AbortSignal.timeout(this.#timeout),
|
|
95
565
|
headers: {
|
|
96
566
|
"Content-Type": "application/json",
|
|
97
|
-
...
|
|
567
|
+
...headers,
|
|
98
568
|
...(init.headers as Record<string, string> | undefined),
|
|
99
569
|
},
|
|
100
570
|
});
|
|
101
|
-
return resp;
|
|
102
571
|
}
|
|
103
572
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return
|
|
573
|
+
private async get<T>(path: string): Promise<T> {
|
|
574
|
+
let resp: Response;
|
|
575
|
+
try {
|
|
576
|
+
resp = await this.authFetch(path);
|
|
577
|
+
} catch (e) {
|
|
578
|
+
if (e instanceof PayError) throw e;
|
|
579
|
+
throw new PayNetworkError(String(e));
|
|
580
|
+
}
|
|
581
|
+
return this.handleResponse<T>(resp);
|
|
113
582
|
}
|
|
114
583
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
584
|
+
private async post<T>(path: string, body: unknown): Promise<T> {
|
|
585
|
+
let resp: Response;
|
|
586
|
+
try {
|
|
587
|
+
resp = await this.authFetch(path, {
|
|
588
|
+
method: "POST",
|
|
589
|
+
body: JSON.stringify(body),
|
|
590
|
+
});
|
|
591
|
+
} catch (e) {
|
|
592
|
+
if (e instanceof PayError) throw e;
|
|
593
|
+
throw new PayNetworkError(String(e));
|
|
594
|
+
}
|
|
595
|
+
return this.handleResponse<T>(resp);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
private async del(path: string): Promise<void> {
|
|
599
|
+
let resp: Response;
|
|
600
|
+
try {
|
|
601
|
+
resp = await this.authFetch(path, { method: "DELETE" });
|
|
602
|
+
} catch (e) {
|
|
603
|
+
if (e instanceof PayError) throw e;
|
|
604
|
+
throw new PayNetworkError(String(e));
|
|
605
|
+
}
|
|
606
|
+
if (resp.status >= 400) {
|
|
607
|
+
const text = await resp.text();
|
|
608
|
+
throw new PayServerError(text, resp.status);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
private async handleResponse<T>(resp: Response): Promise<T> {
|
|
613
|
+
if (resp.status >= 400) {
|
|
614
|
+
let msg: string;
|
|
615
|
+
try {
|
|
616
|
+
const body = (await resp.json()) as {
|
|
617
|
+
error?: string;
|
|
618
|
+
code?: string;
|
|
619
|
+
};
|
|
620
|
+
msg = body.error ?? `Server error: ${resp.status}`;
|
|
621
|
+
if (
|
|
622
|
+
body.code === "insufficient_funds" ||
|
|
623
|
+
msg.toLowerCase().includes("insufficient")
|
|
624
|
+
) {
|
|
625
|
+
throw new PayInsufficientFundsError(msg);
|
|
626
|
+
}
|
|
627
|
+
} catch (e) {
|
|
628
|
+
if (e instanceof PayInsufficientFundsError) throw e;
|
|
629
|
+
msg = `Server error: ${resp.status}`;
|
|
630
|
+
}
|
|
631
|
+
throw new PayServerError(msg, resp.status);
|
|
134
632
|
}
|
|
135
|
-
|
|
633
|
+
if (resp.status === 204) return undefined as T;
|
|
634
|
+
return (await resp.json()) as T;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// ── Internal: permits ────────────────────────────────────────────
|
|
638
|
+
|
|
639
|
+
private async signPermit(
|
|
640
|
+
flow: "direct" | "tab" | "withdraw",
|
|
641
|
+
microAmount: number,
|
|
642
|
+
): Promise<Permit> {
|
|
643
|
+
const contracts = await this.ensureContracts();
|
|
644
|
+
const spender =
|
|
645
|
+
flow === "tab"
|
|
646
|
+
? contracts.tab
|
|
647
|
+
: flow === "withdraw"
|
|
648
|
+
? contracts.relayer
|
|
649
|
+
: contracts.direct;
|
|
650
|
+
const prep = await this.post<{
|
|
136
651
|
hash: string;
|
|
137
652
|
nonce: string;
|
|
138
653
|
deadline: number;
|
|
139
|
-
|
|
140
|
-
amount: number;
|
|
141
|
-
};
|
|
654
|
+
}>("/permit/prepare", { amount: microAmount, spender });
|
|
142
655
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
656
|
+
if (this.#rawKey) {
|
|
657
|
+
// Private key path: sign the pre-computed hash directly
|
|
658
|
+
const account = privateKeyToAccount(this.#rawKey);
|
|
659
|
+
const signature = await account.sign({
|
|
660
|
+
hash: prep.hash as `0x${string}`,
|
|
661
|
+
});
|
|
662
|
+
return { nonce: prep.nonce, deadline: prep.deadline, ...parseSig(signature) };
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// OWS path: sign full EIP-2612 permit typed data
|
|
666
|
+
const signature = await this.#signTypedData({
|
|
667
|
+
domain: {
|
|
668
|
+
name: "USD Coin",
|
|
669
|
+
version: "2",
|
|
670
|
+
chainId: contracts.chainId,
|
|
671
|
+
verifyingContract: contracts.usdc as string,
|
|
672
|
+
},
|
|
673
|
+
types: {
|
|
674
|
+
Permit: [
|
|
675
|
+
{ name: "owner", type: "address" },
|
|
676
|
+
{ name: "spender", type: "address" },
|
|
677
|
+
{ name: "value", type: "uint256" },
|
|
678
|
+
{ name: "nonce", type: "uint256" },
|
|
679
|
+
{ name: "deadline", type: "uint256" },
|
|
680
|
+
],
|
|
681
|
+
},
|
|
682
|
+
primaryType: "Permit",
|
|
683
|
+
message: {
|
|
684
|
+
owner: this.address,
|
|
685
|
+
spender: spender as string,
|
|
686
|
+
value: BigInt(microAmount),
|
|
687
|
+
nonce: BigInt(prep.nonce),
|
|
688
|
+
deadline: BigInt(prep.deadline),
|
|
689
|
+
},
|
|
146
690
|
});
|
|
691
|
+
return { nonce: prep.nonce, deadline: prep.deadline, ...parseSig(signature) };
|
|
692
|
+
}
|
|
147
693
|
|
|
148
|
-
|
|
149
|
-
const sigClean = signature.startsWith("0x") ? signature.slice(2) : signature;
|
|
150
|
-
const r = "0x" + sigClean.slice(0, 64);
|
|
151
|
-
const s = "0x" + sigClean.slice(64, 128);
|
|
152
|
-
const v = parseInt(sigClean.slice(128, 130), 16);
|
|
694
|
+
// ── Internal: x402 ───────────────────────────────────────────────
|
|
153
695
|
|
|
154
|
-
|
|
696
|
+
private async parse402(resp: Response): Promise<{
|
|
697
|
+
settlement: string;
|
|
698
|
+
amount: number;
|
|
699
|
+
to: string;
|
|
700
|
+
accepted?: Record<string, unknown>;
|
|
701
|
+
}> {
|
|
702
|
+
const prHeader = resp.headers.get("payment-required");
|
|
703
|
+
if (prHeader) {
|
|
704
|
+
try {
|
|
705
|
+
const decoded = JSON.parse(atob(prHeader)) as Record<
|
|
706
|
+
string,
|
|
707
|
+
unknown
|
|
708
|
+
>;
|
|
709
|
+
return extract402(decoded);
|
|
710
|
+
} catch {
|
|
711
|
+
/* fall through to body */
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
const body = (await resp.json()) as Record<string, unknown>;
|
|
715
|
+
return extract402(
|
|
716
|
+
(body.requirements ?? body) as Record<string, unknown>,
|
|
717
|
+
);
|
|
155
718
|
}
|
|
156
719
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
): Promise<
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
let permit = options?.permit;
|
|
168
|
-
if (!permit) {
|
|
169
|
-
permit = await this.signPermit("direct", microAmount);
|
|
720
|
+
private async handle402(
|
|
721
|
+
resp: Response,
|
|
722
|
+
url: string,
|
|
723
|
+
method: string,
|
|
724
|
+
body: string | undefined,
|
|
725
|
+
headers: Record<string, string>,
|
|
726
|
+
): Promise<Response> {
|
|
727
|
+
const reqs = await this.parse402(resp);
|
|
728
|
+
if (reqs.settlement === "tab") {
|
|
729
|
+
return this.settleViaTab(url, method, body, headers, reqs);
|
|
170
730
|
}
|
|
731
|
+
return this.settleViaDirect(url, method, body, headers, reqs);
|
|
732
|
+
}
|
|
171
733
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
734
|
+
private async settleViaDirect(
|
|
735
|
+
url: string,
|
|
736
|
+
method: string,
|
|
737
|
+
body: string | undefined,
|
|
738
|
+
headers: Record<string, string>,
|
|
739
|
+
reqs: {
|
|
740
|
+
amount: number;
|
|
741
|
+
to: string;
|
|
742
|
+
accepted?: Record<string, unknown>;
|
|
743
|
+
},
|
|
744
|
+
): Promise<Response> {
|
|
745
|
+
if (!this.#rawKey) {
|
|
746
|
+
throw new PayError(
|
|
747
|
+
"x402 direct settlement requires a private key. " +
|
|
748
|
+
"OWS wallets only support tab settlement. " +
|
|
749
|
+
"Ask the provider to enable tab settlement, or use a private key wallet.",
|
|
750
|
+
);
|
|
184
751
|
}
|
|
185
|
-
|
|
752
|
+
const contracts = await this.ensureContracts();
|
|
753
|
+
const auth = await signTransferAuthorization(
|
|
754
|
+
this.#rawKey,
|
|
755
|
+
reqs.to as Address,
|
|
756
|
+
reqs.amount,
|
|
757
|
+
contracts.chainId,
|
|
758
|
+
contracts.usdc,
|
|
759
|
+
);
|
|
760
|
+
const paymentPayload = {
|
|
761
|
+
x402Version: 2,
|
|
762
|
+
accepted: reqs.accepted ?? {
|
|
763
|
+
scheme: "exact",
|
|
764
|
+
network: `eip155:${contracts.chainId}`,
|
|
765
|
+
amount: String(reqs.amount),
|
|
766
|
+
payTo: reqs.to,
|
|
767
|
+
},
|
|
768
|
+
payload: {
|
|
769
|
+
signature: combinedSignature(auth),
|
|
770
|
+
authorization: {
|
|
771
|
+
from: auth.from,
|
|
772
|
+
to: auth.to,
|
|
773
|
+
value: String(reqs.amount),
|
|
774
|
+
validAfter: "0",
|
|
775
|
+
validBefore: "0",
|
|
776
|
+
nonce: auth.nonce,
|
|
777
|
+
},
|
|
778
|
+
},
|
|
779
|
+
extensions: {},
|
|
780
|
+
};
|
|
781
|
+
return fetch(url, {
|
|
782
|
+
method,
|
|
783
|
+
body,
|
|
784
|
+
signal: AbortSignal.timeout(this.#timeout),
|
|
785
|
+
headers: {
|
|
786
|
+
...headers,
|
|
787
|
+
"Content-Type": "application/json",
|
|
788
|
+
"PAYMENT-SIGNATURE": btoa(JSON.stringify(paymentPayload)),
|
|
789
|
+
},
|
|
790
|
+
});
|
|
186
791
|
}
|
|
187
792
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
793
|
+
private async settleViaTab(
|
|
794
|
+
url: string,
|
|
795
|
+
method: string,
|
|
796
|
+
body: string | undefined,
|
|
797
|
+
headers: Record<string, string>,
|
|
798
|
+
reqs: {
|
|
799
|
+
amount: number;
|
|
800
|
+
to: string;
|
|
801
|
+
accepted?: Record<string, unknown>;
|
|
802
|
+
},
|
|
803
|
+
): Promise<Response> {
|
|
804
|
+
const contracts = await this.ensureContracts();
|
|
805
|
+
const rawTabs = await this.get<RawTab[]>("/tabs");
|
|
806
|
+
let tab = rawTabs.find(
|
|
807
|
+
(t) => t.provider === reqs.to && t.status === "open",
|
|
808
|
+
);
|
|
809
|
+
|
|
810
|
+
if (!tab) {
|
|
811
|
+
const tabMicro = Math.max(
|
|
812
|
+
reqs.amount * TAB_MULTIPLIER,
|
|
813
|
+
TAB_MIN_MICRO,
|
|
814
|
+
);
|
|
815
|
+
const bal = await this.balance();
|
|
816
|
+
const tabDollars = toDollars(tabMicro);
|
|
817
|
+
if (bal.available < tabDollars) {
|
|
818
|
+
throw new PayInsufficientFundsError(
|
|
819
|
+
`Insufficient balance for tab: have $${bal.available.toFixed(2)}, need $${tabDollars.toFixed(2)}`,
|
|
820
|
+
bal.available,
|
|
821
|
+
tabDollars,
|
|
822
|
+
);
|
|
823
|
+
}
|
|
824
|
+
const permit = await this.signPermit("tab", tabMicro);
|
|
825
|
+
tab = await this.post<RawTab>("/tabs", {
|
|
826
|
+
provider: reqs.to,
|
|
827
|
+
amount: tabMicro,
|
|
828
|
+
max_charge_per_call: reqs.amount,
|
|
829
|
+
permit,
|
|
830
|
+
});
|
|
200
831
|
}
|
|
201
|
-
const data = (await resp.json()) as { url: string };
|
|
202
|
-
return data.url;
|
|
203
|
-
}
|
|
204
832
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
833
|
+
const charge = await this.post<{ charge_id?: string }>(
|
|
834
|
+
`/tabs/${tab.tab_id}/charge`,
|
|
835
|
+
{ amount: reqs.amount },
|
|
836
|
+
);
|
|
837
|
+
|
|
838
|
+
const paymentPayload = {
|
|
839
|
+
x402Version: 2,
|
|
840
|
+
accepted: reqs.accepted ?? {
|
|
841
|
+
scheme: "exact",
|
|
842
|
+
network: `eip155:${contracts.chainId}`,
|
|
843
|
+
amount: String(reqs.amount),
|
|
844
|
+
payTo: reqs.to,
|
|
845
|
+
},
|
|
846
|
+
payload: {
|
|
847
|
+
authorization: { from: this.address },
|
|
848
|
+
},
|
|
849
|
+
extensions: {
|
|
850
|
+
pay: {
|
|
851
|
+
settlement: "tab",
|
|
852
|
+
tabId: tab.tab_id,
|
|
853
|
+
chargeId: charge.charge_id ?? "",
|
|
854
|
+
},
|
|
855
|
+
},
|
|
856
|
+
};
|
|
857
|
+
return fetch(url, {
|
|
858
|
+
method,
|
|
859
|
+
body,
|
|
860
|
+
signal: AbortSignal.timeout(this.#timeout),
|
|
861
|
+
headers: {
|
|
862
|
+
...headers,
|
|
863
|
+
"Content-Type": "application/json",
|
|
864
|
+
"PAYMENT-SIGNATURE": btoa(JSON.stringify(paymentPayload)),
|
|
865
|
+
},
|
|
213
866
|
});
|
|
214
|
-
if (!resp.ok) {
|
|
215
|
-
const err = (await resp.json().catch(() => ({}))) as { error?: string };
|
|
216
|
-
throw new Error(err.error ?? `createWithdrawLink failed: ${resp.status}`);
|
|
217
|
-
}
|
|
218
|
-
const data = (await resp.json()) as { url: string };
|
|
219
|
-
return data.url;
|
|
220
867
|
}
|
|
221
868
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
869
|
+
// ── Public: Direct Payment ───────────────────────────────────────
|
|
870
|
+
|
|
871
|
+
async send(
|
|
872
|
+
to: string,
|
|
873
|
+
amount: Amount,
|
|
874
|
+
memo?: string,
|
|
875
|
+
): Promise<SendResult> {
|
|
876
|
+
validateAddress(to);
|
|
877
|
+
const micro = toMicro(amount);
|
|
878
|
+
if (micro < DIRECT_MIN_MICRO) {
|
|
879
|
+
throw new PayValidationError(
|
|
880
|
+
"Amount below minimum ($1.00)",
|
|
881
|
+
"amount",
|
|
882
|
+
);
|
|
883
|
+
}
|
|
884
|
+
const permit = await this.signPermit("direct", micro);
|
|
885
|
+
const raw = await this.post<{
|
|
886
|
+
tx_hash: string;
|
|
887
|
+
status: string;
|
|
888
|
+
amount: number;
|
|
889
|
+
fee: number;
|
|
890
|
+
}>("/direct", {
|
|
891
|
+
to,
|
|
892
|
+
amount: micro,
|
|
893
|
+
memo: memo ?? "",
|
|
894
|
+
permit,
|
|
233
895
|
});
|
|
234
|
-
|
|
235
|
-
|
|
896
|
+
return {
|
|
897
|
+
txHash: raw.tx_hash,
|
|
898
|
+
status: raw.status,
|
|
899
|
+
amount: toDollars(raw.amount),
|
|
900
|
+
fee: toDollars(raw.fee),
|
|
901
|
+
};
|
|
236
902
|
}
|
|
237
903
|
|
|
238
|
-
|
|
904
|
+
// ── Public: Tabs ─────────────────────────────────────────────────
|
|
905
|
+
|
|
239
906
|
async openTab(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (amount === undefined || maxChargePerCall === undefined) {
|
|
259
|
-
throw new Error("amount and maxChargePerCall are required when provider is a string");
|
|
260
|
-
}
|
|
261
|
-
provider = providerOrOpts;
|
|
262
|
-
amt = amount;
|
|
263
|
-
maxCharge = maxChargePerCall;
|
|
264
|
-
permit = options?.permit;
|
|
265
|
-
} else {
|
|
266
|
-
provider = providerOrOpts.to;
|
|
267
|
-
amt = providerOrOpts.limit;
|
|
268
|
-
maxCharge = providerOrOpts.perUnit;
|
|
269
|
-
permit = providerOrOpts.permit;
|
|
907
|
+
provider: string,
|
|
908
|
+
amount: Amount,
|
|
909
|
+
maxChargePerCall: Amount,
|
|
910
|
+
): Promise<Tab> {
|
|
911
|
+
validateAddress(provider);
|
|
912
|
+
const microAmount = toMicro(amount);
|
|
913
|
+
const microMax = toMicro(maxChargePerCall);
|
|
914
|
+
if (microAmount < TAB_MIN_MICRO) {
|
|
915
|
+
throw new PayValidationError(
|
|
916
|
+
"Tab amount below minimum ($5.00)",
|
|
917
|
+
"amount",
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
if (microMax <= 0) {
|
|
921
|
+
throw new PayValidationError(
|
|
922
|
+
"maxChargePerCall must be positive",
|
|
923
|
+
"maxChargePerCall",
|
|
924
|
+
);
|
|
270
925
|
}
|
|
926
|
+
const permit = await this.signPermit("tab", microAmount);
|
|
927
|
+
const raw = await this.post<RawTab>("/tabs", {
|
|
928
|
+
provider,
|
|
929
|
+
amount: microAmount,
|
|
930
|
+
max_charge_per_call: microMax,
|
|
931
|
+
permit,
|
|
932
|
+
});
|
|
933
|
+
return parseTab(raw);
|
|
934
|
+
}
|
|
271
935
|
|
|
272
|
-
|
|
936
|
+
async closeTab(tabId: string): Promise<Tab> {
|
|
937
|
+
const raw = await this.post<RawTab>(`/tabs/${tabId}/close`, {});
|
|
938
|
+
return parseTab(raw);
|
|
939
|
+
}
|
|
273
940
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
941
|
+
async topUpTab(tabId: string, amount: Amount): Promise<Tab> {
|
|
942
|
+
const micro = toMicro(amount);
|
|
943
|
+
if (micro <= 0) {
|
|
944
|
+
throw new PayValidationError(
|
|
945
|
+
"Amount must be positive",
|
|
946
|
+
"amount",
|
|
947
|
+
);
|
|
277
948
|
}
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
provider,
|
|
283
|
-
amount: microAmount,
|
|
284
|
-
max_charge_per_call: Math.round(maxCharge * 1_000_000),
|
|
285
|
-
permit,
|
|
286
|
-
}),
|
|
287
|
-
});
|
|
288
|
-
if (!resp.ok) throw new Error(`openTab failed: ${resp.status}`);
|
|
289
|
-
const data = (await resp.json()) as { tab_id: string };
|
|
290
|
-
return { id: data.tab_id, tab_id: data.tab_id };
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/** Charge a tab (provider-side). */
|
|
294
|
-
async chargeTab(
|
|
295
|
-
tabId: string,
|
|
296
|
-
amountOrOpts:
|
|
297
|
-
| number
|
|
298
|
-
| {
|
|
299
|
-
amount: number;
|
|
300
|
-
cumulative: number;
|
|
301
|
-
callCount: number;
|
|
302
|
-
providerSig: string;
|
|
303
|
-
}
|
|
304
|
-
): Promise<{ status: string }> {
|
|
305
|
-
const body =
|
|
306
|
-
typeof amountOrOpts === "number"
|
|
307
|
-
? { amount: Math.round(amountOrOpts * 1_000_000) }
|
|
308
|
-
: {
|
|
309
|
-
amount: Math.round(amountOrOpts.amount * 1_000_000),
|
|
310
|
-
cumulative: Math.round(amountOrOpts.cumulative * 1_000_000),
|
|
311
|
-
call_count: amountOrOpts.callCount,
|
|
312
|
-
provider_sig: amountOrOpts.providerSig,
|
|
313
|
-
};
|
|
314
|
-
const resp = await this._authFetch(`/tabs/${tabId}/charge`, {
|
|
315
|
-
method: "POST",
|
|
316
|
-
body: JSON.stringify(body),
|
|
949
|
+
const permit = await this.signPermit("tab", micro);
|
|
950
|
+
const raw = await this.post<RawTab>(`/tabs/${tabId}/topup`, {
|
|
951
|
+
amount: micro,
|
|
952
|
+
permit,
|
|
317
953
|
});
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
): Promise<
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
954
|
+
return parseTab(raw);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
async listTabs(): Promise<Tab[]> {
|
|
958
|
+
const raw = await this.get<RawTab[]>("/tabs");
|
|
959
|
+
return raw.map(parseTab);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
async getTab(tabId: string): Promise<Tab> {
|
|
963
|
+
const raw = await this.get<RawTab>(`/tabs/${tabId}`);
|
|
964
|
+
return parseTab(raw);
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
async chargeTab(tabId: string, amount: Amount): Promise<ChargeResult> {
|
|
968
|
+
const micro = toMicro(amount);
|
|
969
|
+
const raw = await this.post<{ charge_id?: string; status: string }>(
|
|
970
|
+
`/tabs/${tabId}/charge`,
|
|
971
|
+
{ amount: micro },
|
|
972
|
+
);
|
|
973
|
+
return { chargeId: raw.charge_id ?? "", status: raw.status };
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
// ── Public: x402 ─────────────────────────────────────────────────
|
|
977
|
+
|
|
978
|
+
async request(
|
|
979
|
+
url: string,
|
|
980
|
+
options?: {
|
|
981
|
+
method?: string;
|
|
982
|
+
body?: unknown;
|
|
983
|
+
headers?: Record<string, string>;
|
|
984
|
+
},
|
|
985
|
+
): Promise<Response> {
|
|
986
|
+
const method = options?.method ?? "GET";
|
|
987
|
+
const headers = options?.headers ?? {};
|
|
988
|
+
const bodyStr = options?.body
|
|
989
|
+
? JSON.stringify(options.body)
|
|
990
|
+
: undefined;
|
|
991
|
+
const resp = await fetch(url, {
|
|
992
|
+
method,
|
|
993
|
+
body: bodyStr,
|
|
994
|
+
headers,
|
|
995
|
+
signal: AbortSignal.timeout(this.#timeout),
|
|
334
996
|
});
|
|
335
|
-
if (
|
|
336
|
-
return (
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
997
|
+
if (resp.status !== 402) return resp;
|
|
998
|
+
return this.handle402(resp, url, method, bodyStr, headers);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// ── Public: Wallet ───────────────────────────────────────────────
|
|
1002
|
+
|
|
1003
|
+
async balance(): Promise<Balance> {
|
|
1004
|
+
const raw = await this.get<{
|
|
1005
|
+
balance_usdc: string | null;
|
|
1006
|
+
total_locked: number;
|
|
1007
|
+
}>("/status");
|
|
1008
|
+
const total = raw.balance_usdc
|
|
1009
|
+
? Number(raw.balance_usdc) / 1_000_000
|
|
1010
|
+
: 0;
|
|
1011
|
+
const locked = (raw.total_locked ?? 0) / 1_000_000;
|
|
1012
|
+
return { total, locked, available: total - locked };
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
async status(): Promise<Status> {
|
|
1016
|
+
const raw = await this.get<{
|
|
1017
|
+
wallet: string;
|
|
1018
|
+
balance_usdc: string | null;
|
|
1019
|
+
total_locked: number;
|
|
1020
|
+
open_tabs: number;
|
|
1021
|
+
}>("/status");
|
|
1022
|
+
const total = raw.balance_usdc
|
|
1023
|
+
? Number(raw.balance_usdc) / 1_000_000
|
|
1024
|
+
: 0;
|
|
1025
|
+
const locked = (raw.total_locked ?? 0) / 1_000_000;
|
|
1026
|
+
return {
|
|
1027
|
+
address: raw.wallet,
|
|
1028
|
+
balance: { total, locked, available: total - locked },
|
|
1029
|
+
openTabs: raw.open_tabs,
|
|
359
1030
|
};
|
|
360
|
-
return this._contractsCache;
|
|
361
1031
|
}
|
|
362
1032
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
1033
|
+
// ── Public: Discovery ────────────────────────────────────────────
|
|
1034
|
+
|
|
1035
|
+
async discover(
|
|
1036
|
+
query?: string,
|
|
1037
|
+
options?: DiscoverOptions,
|
|
1038
|
+
): Promise<DiscoverService[]> {
|
|
1039
|
+
return discoverImpl(this.#apiUrl, this.#timeout, query, options);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
// ── Public: Funding ──────────────────────────────────────────────
|
|
1043
|
+
|
|
1044
|
+
async createFundLink(options?: FundLinkOptions): Promise<string> {
|
|
1045
|
+
// Sign permit so dashboard withdraw tab works from any link.
|
|
1046
|
+
const payload: Record<string, unknown> = {
|
|
1047
|
+
messages: options?.message ? [{ text: options.message }] : [],
|
|
1048
|
+
agent_name: options?.agentName,
|
|
1049
|
+
};
|
|
1050
|
+
try {
|
|
1051
|
+
const st = await this.status();
|
|
1052
|
+
const microBalance = Math.round(st.balance.total * 1_000_000);
|
|
1053
|
+
if (microBalance > 0) {
|
|
1054
|
+
const permit = await this.signPermit("withdraw", microBalance);
|
|
1055
|
+
payload.permit = {
|
|
1056
|
+
value: microBalance,
|
|
1057
|
+
deadline: permit.deadline,
|
|
1058
|
+
v: permit.v,
|
|
1059
|
+
r: permit.r,
|
|
1060
|
+
s: permit.s,
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
} catch {
|
|
1064
|
+
// Best effort — fund link still works without permit
|
|
1065
|
+
}
|
|
1066
|
+
const data = await this.post<{ url: string }>("/links/fund", payload);
|
|
1067
|
+
return data.url;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
async createWithdrawLink(options?: FundLinkOptions): Promise<string> {
|
|
1071
|
+
// Sign a USDC permit granting the relayer allowance for the full balance.
|
|
1072
|
+
const status = await this.status();
|
|
1073
|
+
const microBalance = Math.round(status.balance.total * 1_000_000);
|
|
1074
|
+
if (microBalance <= 0) {
|
|
1075
|
+
throw new PayError("no USDC balance to create withdraw link");
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
const permit = await this.signPermit("withdraw", microBalance);
|
|
1079
|
+
|
|
1080
|
+
const data = await this.post<{ url: string }>("/links/withdraw", {
|
|
1081
|
+
messages: options?.message ? [{ text: options.message }] : [],
|
|
1082
|
+
agent_name: options?.agentName,
|
|
1083
|
+
permit: {
|
|
1084
|
+
value: microBalance,
|
|
1085
|
+
deadline: permit.deadline,
|
|
1086
|
+
v: permit.v,
|
|
1087
|
+
r: permit.r,
|
|
1088
|
+
s: permit.s,
|
|
389
1089
|
},
|
|
390
1090
|
});
|
|
1091
|
+
return data.url;
|
|
391
1092
|
}
|
|
392
1093
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
1094
|
+
// ── Public: Webhooks ─────────────────────────────────────────────
|
|
1095
|
+
|
|
1096
|
+
async registerWebhook(
|
|
1097
|
+
url: string,
|
|
1098
|
+
events?: string[],
|
|
1099
|
+
secret?: string,
|
|
1100
|
+
): Promise<WebhookRegistration> {
|
|
1101
|
+
const payload: Record<string, unknown> = { url };
|
|
1102
|
+
if (events) payload.events = events;
|
|
1103
|
+
if (secret) payload.secret = secret;
|
|
1104
|
+
const raw = await this.post<{
|
|
1105
|
+
id: string;
|
|
1106
|
+
url: string;
|
|
1107
|
+
events: string[];
|
|
1108
|
+
}>("/webhooks", payload);
|
|
1109
|
+
return { id: raw.id, url: raw.url, events: raw.events };
|
|
408
1110
|
}
|
|
409
|
-
}
|
|
410
1111
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
1112
|
+
async listWebhooks(): Promise<WebhookRegistration[]> {
|
|
1113
|
+
const raw = await this.get<
|
|
1114
|
+
{ id: string; url: string; events: string[] }[]
|
|
1115
|
+
>("/webhooks");
|
|
1116
|
+
return raw.map((w) => ({ id: w.id, url: w.url, events: w.events }));
|
|
1117
|
+
}
|
|
415
1118
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
types: types as Parameters<
|
|
433
|
-
PrivateKeyAccount["signTypedData"]
|
|
434
|
-
>[0]["types"],
|
|
435
|
-
primaryType: Object.keys(types)[0],
|
|
436
|
-
message,
|
|
437
|
-
});
|
|
1119
|
+
async deleteWebhook(webhookId: string): Promise<void> {
|
|
1120
|
+
await this.del(`/webhooks/${webhookId}`);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// ── Public: Testnet ──────────────────────────────────────────────
|
|
1124
|
+
|
|
1125
|
+
async mint(amount: Amount): Promise<MintResult> {
|
|
1126
|
+
if (!this.#testnet) {
|
|
1127
|
+
throw new PayError("mint is only available on testnet");
|
|
1128
|
+
}
|
|
1129
|
+
const micro = toMicro(amount);
|
|
1130
|
+
const raw = await this.post<{ tx_hash: string; amount: number }>(
|
|
1131
|
+
"/mint",
|
|
1132
|
+
{ amount: micro },
|
|
1133
|
+
);
|
|
1134
|
+
return { txHash: raw.tx_hash, amount: toDollars(raw.amount) };
|
|
438
1135
|
}
|
|
439
1136
|
}
|
|
440
1137
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
1138
|
+
// ── x402 helpers ─────────────────────────────────────────────────────
|
|
1139
|
+
|
|
1140
|
+
function extract402(obj: Record<string, unknown>): {
|
|
1141
|
+
settlement: string;
|
|
1142
|
+
amount: number;
|
|
1143
|
+
to: string;
|
|
1144
|
+
accepted?: Record<string, unknown>;
|
|
1145
|
+
} {
|
|
1146
|
+
const accepts = obj.accepts as
|
|
1147
|
+
| Array<Record<string, unknown>>
|
|
1148
|
+
| undefined;
|
|
1149
|
+
if (Array.isArray(accepts) && accepts.length > 0) {
|
|
1150
|
+
const offer = accepts[0];
|
|
1151
|
+
const extra = (offer.extra ?? {}) as Record<string, unknown>;
|
|
1152
|
+
return {
|
|
1153
|
+
settlement: String(extra.settlement ?? "direct"),
|
|
1154
|
+
amount: Number(offer.amount ?? 0),
|
|
1155
|
+
to: String(offer.payTo ?? ""),
|
|
1156
|
+
accepted: offer,
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
return {
|
|
1160
|
+
settlement: String(obj.settlement ?? "direct"),
|
|
1161
|
+
amount: Number(obj.amount ?? 0),
|
|
1162
|
+
to: String(obj.to ?? ""),
|
|
1163
|
+
};
|
|
445
1164
|
}
|