@purseragents/sdk 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +24 -2
- package/dist/abi.d.ts +3235 -0
- package/dist/abi.js +254 -0
- package/dist/all.d.ts +2 -0
- package/dist/all.js +3 -0
- package/dist/chain.d.ts +31 -0
- package/dist/chain.js +28 -0
- package/dist/client.d.ts +421 -0
- package/dist/client.js +221 -0
- package/dist/eip712.d.ts +381 -0
- package/dist/eip712.js +132 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +66 -0
- package/dist/merchant.d.ts +41 -0
- package/dist/merchant.js +91 -0
- package/dist/schema.d.ts +583 -0
- package/dist/schema.js +11 -0
- package/dist/tools.d.ts +42 -0
- package/dist/tools.js +212 -0
- package/dist/webhooks.d.ts +8 -0
- package/dist/webhooks.js +24 -0
- package/dist/x402.d.ts +100 -0
- package/dist/x402.js +89 -0
- package/package.json +28 -4
package/dist/eip712.d.ts
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
/** EIP-712 types for Purser intents. They match contracts/src/PurserModule.sol exactly. */
|
|
3
|
+
export declare const intentTypes: {
|
|
4
|
+
readonly Pay: readonly [{
|
|
5
|
+
readonly name: "safe";
|
|
6
|
+
readonly type: "address";
|
|
7
|
+
}, {
|
|
8
|
+
readonly name: "token";
|
|
9
|
+
readonly type: "address";
|
|
10
|
+
}, {
|
|
11
|
+
readonly name: "to";
|
|
12
|
+
readonly type: "address";
|
|
13
|
+
}, {
|
|
14
|
+
readonly name: "amount";
|
|
15
|
+
readonly type: "uint256";
|
|
16
|
+
}, {
|
|
17
|
+
readonly name: "service";
|
|
18
|
+
readonly type: "bytes32";
|
|
19
|
+
}, {
|
|
20
|
+
readonly name: "nonce";
|
|
21
|
+
readonly type: "uint256";
|
|
22
|
+
}, {
|
|
23
|
+
readonly name: "deadline";
|
|
24
|
+
readonly type: "uint256";
|
|
25
|
+
}];
|
|
26
|
+
readonly Call: readonly [{
|
|
27
|
+
readonly name: "safe";
|
|
28
|
+
readonly type: "address";
|
|
29
|
+
}, {
|
|
30
|
+
readonly name: "target";
|
|
31
|
+
readonly type: "address";
|
|
32
|
+
}, {
|
|
33
|
+
readonly name: "value";
|
|
34
|
+
readonly type: "uint256";
|
|
35
|
+
}, {
|
|
36
|
+
readonly name: "dataHash";
|
|
37
|
+
readonly type: "bytes32";
|
|
38
|
+
}, {
|
|
39
|
+
readonly name: "spendToken";
|
|
40
|
+
readonly type: "address";
|
|
41
|
+
}, {
|
|
42
|
+
readonly name: "spendLimit";
|
|
43
|
+
readonly type: "uint256";
|
|
44
|
+
}, {
|
|
45
|
+
readonly name: "nonce";
|
|
46
|
+
readonly type: "uint256";
|
|
47
|
+
}, {
|
|
48
|
+
readonly name: "deadline";
|
|
49
|
+
readonly type: "uint256";
|
|
50
|
+
}];
|
|
51
|
+
readonly Treasury: readonly [{
|
|
52
|
+
readonly name: "safe";
|
|
53
|
+
readonly type: "address";
|
|
54
|
+
}, {
|
|
55
|
+
readonly name: "vault";
|
|
56
|
+
readonly type: "address";
|
|
57
|
+
}, {
|
|
58
|
+
readonly name: "deposit";
|
|
59
|
+
readonly type: "bool";
|
|
60
|
+
}, {
|
|
61
|
+
readonly name: "assets";
|
|
62
|
+
readonly type: "uint256";
|
|
63
|
+
}, {
|
|
64
|
+
readonly name: "limit";
|
|
65
|
+
readonly type: "uint256";
|
|
66
|
+
}, {
|
|
67
|
+
readonly name: "nonce";
|
|
68
|
+
readonly type: "uint256";
|
|
69
|
+
}, {
|
|
70
|
+
readonly name: "deadline";
|
|
71
|
+
readonly type: "uint256";
|
|
72
|
+
}];
|
|
73
|
+
readonly AgentOp: readonly [{
|
|
74
|
+
readonly name: "safe";
|
|
75
|
+
readonly type: "address";
|
|
76
|
+
}, {
|
|
77
|
+
readonly name: "extension";
|
|
78
|
+
readonly type: "address";
|
|
79
|
+
}, {
|
|
80
|
+
readonly name: "opHash";
|
|
81
|
+
readonly type: "bytes32";
|
|
82
|
+
}, {
|
|
83
|
+
readonly name: "token";
|
|
84
|
+
readonly type: "address";
|
|
85
|
+
}, {
|
|
86
|
+
readonly name: "amount";
|
|
87
|
+
readonly type: "uint256";
|
|
88
|
+
}, {
|
|
89
|
+
readonly name: "nonce";
|
|
90
|
+
readonly type: "uint256";
|
|
91
|
+
}, {
|
|
92
|
+
readonly name: "deadline";
|
|
93
|
+
readonly type: "uint256";
|
|
94
|
+
}];
|
|
95
|
+
};
|
|
96
|
+
export interface PayIntent {
|
|
97
|
+
safe: Address;
|
|
98
|
+
token: Address;
|
|
99
|
+
to: Address;
|
|
100
|
+
amount: bigint;
|
|
101
|
+
service: Hex;
|
|
102
|
+
nonce: bigint;
|
|
103
|
+
deadline: bigint;
|
|
104
|
+
}
|
|
105
|
+
export interface CallIntent {
|
|
106
|
+
safe: Address;
|
|
107
|
+
target: Address;
|
|
108
|
+
value: bigint;
|
|
109
|
+
data: Hex;
|
|
110
|
+
spendToken: Address;
|
|
111
|
+
spendLimit: bigint;
|
|
112
|
+
nonce: bigint;
|
|
113
|
+
deadline: bigint;
|
|
114
|
+
}
|
|
115
|
+
export interface TreasuryIntent {
|
|
116
|
+
safe: Address;
|
|
117
|
+
vault: Address;
|
|
118
|
+
deposit: boolean;
|
|
119
|
+
assets: bigint;
|
|
120
|
+
limit: bigint;
|
|
121
|
+
nonce: bigint;
|
|
122
|
+
deadline: bigint;
|
|
123
|
+
}
|
|
124
|
+
export interface AgentOpIntent {
|
|
125
|
+
safe: Address;
|
|
126
|
+
extension: Address;
|
|
127
|
+
opHash: Hex;
|
|
128
|
+
token: Address;
|
|
129
|
+
amount: bigint;
|
|
130
|
+
nonce: bigint;
|
|
131
|
+
deadline: bigint;
|
|
132
|
+
}
|
|
133
|
+
export declare function purserDomain(chainId: number, module: Address): {
|
|
134
|
+
readonly name: "Purser";
|
|
135
|
+
readonly version: "1";
|
|
136
|
+
readonly chainId: number;
|
|
137
|
+
readonly verifyingContract: `0x${string}`;
|
|
138
|
+
};
|
|
139
|
+
export declare function recurringDomain(chainId: number, recurring: Address): {
|
|
140
|
+
readonly name: "Purser Recurring";
|
|
141
|
+
readonly version: "2";
|
|
142
|
+
readonly chainId: number;
|
|
143
|
+
readonly verifyingContract: `0x${string}`;
|
|
144
|
+
};
|
|
145
|
+
export declare function payTypedData(chainId: number, module: Address, p: PayIntent): {
|
|
146
|
+
domain: {
|
|
147
|
+
readonly name: "Purser";
|
|
148
|
+
readonly version: "1";
|
|
149
|
+
readonly chainId: number;
|
|
150
|
+
readonly verifyingContract: `0x${string}`;
|
|
151
|
+
};
|
|
152
|
+
types: {
|
|
153
|
+
Pay: readonly [{
|
|
154
|
+
readonly name: "safe";
|
|
155
|
+
readonly type: "address";
|
|
156
|
+
}, {
|
|
157
|
+
readonly name: "token";
|
|
158
|
+
readonly type: "address";
|
|
159
|
+
}, {
|
|
160
|
+
readonly name: "to";
|
|
161
|
+
readonly type: "address";
|
|
162
|
+
}, {
|
|
163
|
+
readonly name: "amount";
|
|
164
|
+
readonly type: "uint256";
|
|
165
|
+
}, {
|
|
166
|
+
readonly name: "service";
|
|
167
|
+
readonly type: "bytes32";
|
|
168
|
+
}, {
|
|
169
|
+
readonly name: "nonce";
|
|
170
|
+
readonly type: "uint256";
|
|
171
|
+
}, {
|
|
172
|
+
readonly name: "deadline";
|
|
173
|
+
readonly type: "uint256";
|
|
174
|
+
}];
|
|
175
|
+
};
|
|
176
|
+
primaryType: "Pay";
|
|
177
|
+
message: PayIntent;
|
|
178
|
+
};
|
|
179
|
+
export declare function callTypedData(chainId: number, module: Address, c: CallIntent): {
|
|
180
|
+
domain: {
|
|
181
|
+
readonly name: "Purser";
|
|
182
|
+
readonly version: "1";
|
|
183
|
+
readonly chainId: number;
|
|
184
|
+
readonly verifyingContract: `0x${string}`;
|
|
185
|
+
};
|
|
186
|
+
types: {
|
|
187
|
+
Call: readonly [{
|
|
188
|
+
readonly name: "safe";
|
|
189
|
+
readonly type: "address";
|
|
190
|
+
}, {
|
|
191
|
+
readonly name: "target";
|
|
192
|
+
readonly type: "address";
|
|
193
|
+
}, {
|
|
194
|
+
readonly name: "value";
|
|
195
|
+
readonly type: "uint256";
|
|
196
|
+
}, {
|
|
197
|
+
readonly name: "dataHash";
|
|
198
|
+
readonly type: "bytes32";
|
|
199
|
+
}, {
|
|
200
|
+
readonly name: "spendToken";
|
|
201
|
+
readonly type: "address";
|
|
202
|
+
}, {
|
|
203
|
+
readonly name: "spendLimit";
|
|
204
|
+
readonly type: "uint256";
|
|
205
|
+
}, {
|
|
206
|
+
readonly name: "nonce";
|
|
207
|
+
readonly type: "uint256";
|
|
208
|
+
}, {
|
|
209
|
+
readonly name: "deadline";
|
|
210
|
+
readonly type: "uint256";
|
|
211
|
+
}];
|
|
212
|
+
};
|
|
213
|
+
primaryType: "Call";
|
|
214
|
+
message: {
|
|
215
|
+
safe: `0x${string}`;
|
|
216
|
+
target: `0x${string}`;
|
|
217
|
+
value: bigint;
|
|
218
|
+
dataHash: `0x${string}`;
|
|
219
|
+
spendToken: `0x${string}`;
|
|
220
|
+
spendLimit: bigint;
|
|
221
|
+
nonce: bigint;
|
|
222
|
+
deadline: bigint;
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
export declare function treasuryTypedData(chainId: number, module: Address, t: TreasuryIntent): {
|
|
226
|
+
domain: {
|
|
227
|
+
readonly name: "Purser";
|
|
228
|
+
readonly version: "1";
|
|
229
|
+
readonly chainId: number;
|
|
230
|
+
readonly verifyingContract: `0x${string}`;
|
|
231
|
+
};
|
|
232
|
+
types: {
|
|
233
|
+
Treasury: readonly [{
|
|
234
|
+
readonly name: "safe";
|
|
235
|
+
readonly type: "address";
|
|
236
|
+
}, {
|
|
237
|
+
readonly name: "vault";
|
|
238
|
+
readonly type: "address";
|
|
239
|
+
}, {
|
|
240
|
+
readonly name: "deposit";
|
|
241
|
+
readonly type: "bool";
|
|
242
|
+
}, {
|
|
243
|
+
readonly name: "assets";
|
|
244
|
+
readonly type: "uint256";
|
|
245
|
+
}, {
|
|
246
|
+
readonly name: "limit";
|
|
247
|
+
readonly type: "uint256";
|
|
248
|
+
}, {
|
|
249
|
+
readonly name: "nonce";
|
|
250
|
+
readonly type: "uint256";
|
|
251
|
+
}, {
|
|
252
|
+
readonly name: "deadline";
|
|
253
|
+
readonly type: "uint256";
|
|
254
|
+
}];
|
|
255
|
+
};
|
|
256
|
+
primaryType: "Treasury";
|
|
257
|
+
message: TreasuryIntent;
|
|
258
|
+
};
|
|
259
|
+
export declare function agentOpTypedData(chainId: number, module: Address, o: AgentOpIntent): {
|
|
260
|
+
domain: {
|
|
261
|
+
readonly name: "Purser";
|
|
262
|
+
readonly version: "1";
|
|
263
|
+
readonly chainId: number;
|
|
264
|
+
readonly verifyingContract: `0x${string}`;
|
|
265
|
+
};
|
|
266
|
+
types: {
|
|
267
|
+
AgentOp: readonly [{
|
|
268
|
+
readonly name: "safe";
|
|
269
|
+
readonly type: "address";
|
|
270
|
+
}, {
|
|
271
|
+
readonly name: "extension";
|
|
272
|
+
readonly type: "address";
|
|
273
|
+
}, {
|
|
274
|
+
readonly name: "opHash";
|
|
275
|
+
readonly type: "bytes32";
|
|
276
|
+
}, {
|
|
277
|
+
readonly name: "token";
|
|
278
|
+
readonly type: "address";
|
|
279
|
+
}, {
|
|
280
|
+
readonly name: "amount";
|
|
281
|
+
readonly type: "uint256";
|
|
282
|
+
}, {
|
|
283
|
+
readonly name: "nonce";
|
|
284
|
+
readonly type: "uint256";
|
|
285
|
+
}, {
|
|
286
|
+
readonly name: "deadline";
|
|
287
|
+
readonly type: "uint256";
|
|
288
|
+
}];
|
|
289
|
+
};
|
|
290
|
+
primaryType: "AgentOp";
|
|
291
|
+
message: AgentOpIntent;
|
|
292
|
+
};
|
|
293
|
+
export declare function heartbeatTypedData(chainId: number, recurring: Address, streamId: bigint, upTo: bigint): {
|
|
294
|
+
domain: {
|
|
295
|
+
readonly name: "Purser Recurring";
|
|
296
|
+
readonly version: "2";
|
|
297
|
+
readonly chainId: number;
|
|
298
|
+
readonly verifyingContract: `0x${string}`;
|
|
299
|
+
};
|
|
300
|
+
types: {
|
|
301
|
+
Heartbeat: {
|
|
302
|
+
name: string;
|
|
303
|
+
type: string;
|
|
304
|
+
}[];
|
|
305
|
+
};
|
|
306
|
+
primaryType: "Heartbeat";
|
|
307
|
+
message: {
|
|
308
|
+
streamId: bigint;
|
|
309
|
+
upTo: bigint;
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
export declare const payDigest: (chainId: number, module: Address, p: PayIntent) => `0x${string}`;
|
|
313
|
+
export declare const callDigest: (chainId: number, module: Address, c: CallIntent) => `0x${string}`;
|
|
314
|
+
export declare const treasuryDigest: (chainId: number, module: Address, t: TreasuryIntent) => `0x${string}`;
|
|
315
|
+
export declare const agentOpDigest: (chainId: number, module: Address, o: AgentOpIntent) => `0x${string}`;
|
|
316
|
+
/** Signatures go to the module as one blob: agent, then co-signer (if needed), then firewall (if set). */
|
|
317
|
+
export declare function packSignatures(...sigs: (Hex | null | undefined)[]): Hex;
|
|
318
|
+
/** An unordered nonce: 128 random bits, as a bigint. */
|
|
319
|
+
export declare function randomNonce(): bigint;
|
|
320
|
+
/** Service ids are keccak256 of a readable name, e.g. serviceId("model:frontier-1"). */
|
|
321
|
+
export declare const serviceId: (name: string) => Hex;
|
|
322
|
+
export declare const transferWithAuthorizationTypes: {
|
|
323
|
+
readonly TransferWithAuthorization: readonly [{
|
|
324
|
+
readonly name: "from";
|
|
325
|
+
readonly type: "address";
|
|
326
|
+
}, {
|
|
327
|
+
readonly name: "to";
|
|
328
|
+
readonly type: "address";
|
|
329
|
+
}, {
|
|
330
|
+
readonly name: "value";
|
|
331
|
+
readonly type: "uint256";
|
|
332
|
+
}, {
|
|
333
|
+
readonly name: "validAfter";
|
|
334
|
+
readonly type: "uint256";
|
|
335
|
+
}, {
|
|
336
|
+
readonly name: "validBefore";
|
|
337
|
+
readonly type: "uint256";
|
|
338
|
+
}, {
|
|
339
|
+
readonly name: "nonce";
|
|
340
|
+
readonly type: "bytes32";
|
|
341
|
+
}];
|
|
342
|
+
};
|
|
343
|
+
export interface Eip3009Authorization {
|
|
344
|
+
from: Address;
|
|
345
|
+
to: Address;
|
|
346
|
+
value: bigint;
|
|
347
|
+
validAfter: bigint;
|
|
348
|
+
validBefore: bigint;
|
|
349
|
+
nonce: Hex;
|
|
350
|
+
}
|
|
351
|
+
export declare function transferAuthorizationTypedData(chainId: number, token: Address, tokenName: string, tokenVersion: string, auth: Eip3009Authorization): {
|
|
352
|
+
domain: {
|
|
353
|
+
name: string;
|
|
354
|
+
version: string;
|
|
355
|
+
chainId: number;
|
|
356
|
+
verifyingContract: `0x${string}`;
|
|
357
|
+
};
|
|
358
|
+
types: {
|
|
359
|
+
readonly TransferWithAuthorization: readonly [{
|
|
360
|
+
readonly name: "from";
|
|
361
|
+
readonly type: "address";
|
|
362
|
+
}, {
|
|
363
|
+
readonly name: "to";
|
|
364
|
+
readonly type: "address";
|
|
365
|
+
}, {
|
|
366
|
+
readonly name: "value";
|
|
367
|
+
readonly type: "uint256";
|
|
368
|
+
}, {
|
|
369
|
+
readonly name: "validAfter";
|
|
370
|
+
readonly type: "uint256";
|
|
371
|
+
}, {
|
|
372
|
+
readonly name: "validBefore";
|
|
373
|
+
readonly type: "uint256";
|
|
374
|
+
}, {
|
|
375
|
+
readonly name: "nonce";
|
|
376
|
+
readonly type: "bytes32";
|
|
377
|
+
}];
|
|
378
|
+
};
|
|
379
|
+
primaryType: "TransferWithAuthorization";
|
|
380
|
+
message: Eip3009Authorization;
|
|
381
|
+
};
|
package/dist/eip712.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { concat, hashTypedData, keccak256, toHex } from "viem";
|
|
2
|
+
/** EIP-712 types for Purser intents. They match contracts/src/PurserModule.sol exactly. */
|
|
3
|
+
export const intentTypes = {
|
|
4
|
+
Pay: [
|
|
5
|
+
{ name: "safe", type: "address" },
|
|
6
|
+
{ name: "token", type: "address" },
|
|
7
|
+
{ name: "to", type: "address" },
|
|
8
|
+
{ name: "amount", type: "uint256" },
|
|
9
|
+
{ name: "service", type: "bytes32" },
|
|
10
|
+
{ name: "nonce", type: "uint256" },
|
|
11
|
+
{ name: "deadline", type: "uint256" },
|
|
12
|
+
],
|
|
13
|
+
Call: [
|
|
14
|
+
{ name: "safe", type: "address" },
|
|
15
|
+
{ name: "target", type: "address" },
|
|
16
|
+
{ name: "value", type: "uint256" },
|
|
17
|
+
{ name: "dataHash", type: "bytes32" },
|
|
18
|
+
{ name: "spendToken", type: "address" },
|
|
19
|
+
{ name: "spendLimit", type: "uint256" },
|
|
20
|
+
{ name: "nonce", type: "uint256" },
|
|
21
|
+
{ name: "deadline", type: "uint256" },
|
|
22
|
+
],
|
|
23
|
+
Treasury: [
|
|
24
|
+
{ name: "safe", type: "address" },
|
|
25
|
+
{ name: "vault", type: "address" },
|
|
26
|
+
{ name: "deposit", type: "bool" },
|
|
27
|
+
{ name: "assets", type: "uint256" },
|
|
28
|
+
{ name: "limit", type: "uint256" },
|
|
29
|
+
{ name: "nonce", type: "uint256" },
|
|
30
|
+
{ name: "deadline", type: "uint256" },
|
|
31
|
+
],
|
|
32
|
+
AgentOp: [
|
|
33
|
+
{ name: "safe", type: "address" },
|
|
34
|
+
{ name: "extension", type: "address" },
|
|
35
|
+
{ name: "opHash", type: "bytes32" },
|
|
36
|
+
{ name: "token", type: "address" },
|
|
37
|
+
{ name: "amount", type: "uint256" },
|
|
38
|
+
{ name: "nonce", type: "uint256" },
|
|
39
|
+
{ name: "deadline", type: "uint256" },
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
export function purserDomain(chainId, module) {
|
|
43
|
+
return { name: "Purser", version: "1", chainId, verifyingContract: module };
|
|
44
|
+
}
|
|
45
|
+
export function recurringDomain(chainId, recurring) {
|
|
46
|
+
return { name: "Purser Recurring", version: "2", chainId, verifyingContract: recurring }; // v2: a fresh firewall signature on every payout
|
|
47
|
+
}
|
|
48
|
+
export function payTypedData(chainId, module, p) {
|
|
49
|
+
return { domain: purserDomain(chainId, module), types: { Pay: intentTypes.Pay }, primaryType: "Pay", message: p };
|
|
50
|
+
}
|
|
51
|
+
export function callTypedData(chainId, module, c) {
|
|
52
|
+
return {
|
|
53
|
+
domain: purserDomain(chainId, module),
|
|
54
|
+
types: { Call: intentTypes.Call },
|
|
55
|
+
primaryType: "Call",
|
|
56
|
+
message: {
|
|
57
|
+
safe: c.safe,
|
|
58
|
+
target: c.target,
|
|
59
|
+
value: c.value,
|
|
60
|
+
dataHash: keccak256(c.data),
|
|
61
|
+
spendToken: c.spendToken,
|
|
62
|
+
spendLimit: c.spendLimit,
|
|
63
|
+
nonce: c.nonce,
|
|
64
|
+
deadline: c.deadline,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function treasuryTypedData(chainId, module, t) {
|
|
69
|
+
return {
|
|
70
|
+
domain: purserDomain(chainId, module),
|
|
71
|
+
types: { Treasury: intentTypes.Treasury },
|
|
72
|
+
primaryType: "Treasury",
|
|
73
|
+
message: t,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export function agentOpTypedData(chainId, module, o) {
|
|
77
|
+
return {
|
|
78
|
+
domain: purserDomain(chainId, module),
|
|
79
|
+
types: { AgentOp: intentTypes.AgentOp },
|
|
80
|
+
primaryType: "AgentOp",
|
|
81
|
+
message: o,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export function heartbeatTypedData(chainId, recurring, streamId, upTo) {
|
|
85
|
+
return {
|
|
86
|
+
domain: recurringDomain(chainId, recurring),
|
|
87
|
+
types: {
|
|
88
|
+
Heartbeat: [
|
|
89
|
+
{ name: "streamId", type: "uint256" },
|
|
90
|
+
{ name: "upTo", type: "uint256" },
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
primaryType: "Heartbeat",
|
|
94
|
+
message: { streamId, upTo },
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export const payDigest = (chainId, module, p) => hashTypedData(payTypedData(chainId, module, p));
|
|
98
|
+
export const callDigest = (chainId, module, c) => hashTypedData(callTypedData(chainId, module, c));
|
|
99
|
+
export const treasuryDigest = (chainId, module, t) => hashTypedData(treasuryTypedData(chainId, module, t));
|
|
100
|
+
export const agentOpDigest = (chainId, module, o) => hashTypedData(agentOpTypedData(chainId, module, o));
|
|
101
|
+
/** Signatures go to the module as one blob: agent, then co-signer (if needed), then firewall (if set). */
|
|
102
|
+
export function packSignatures(...sigs) {
|
|
103
|
+
const list = sigs.filter((s) => !!s);
|
|
104
|
+
return list.length ? concat(list) : "0x";
|
|
105
|
+
}
|
|
106
|
+
/** An unordered nonce: 128 random bits, as a bigint. */
|
|
107
|
+
export function randomNonce() {
|
|
108
|
+
const b = new Uint8Array(16);
|
|
109
|
+
crypto.getRandomValues(b);
|
|
110
|
+
return BigInt(toHex(b));
|
|
111
|
+
}
|
|
112
|
+
/** Service ids are keccak256 of a readable name, e.g. serviceId("model:frontier-1"). */
|
|
113
|
+
export const serviceId = (name) => keccak256(new TextEncoder().encode(name));
|
|
114
|
+
// ---------------------------------------------------------------------------------------------------- EIP-3009 / x402
|
|
115
|
+
export const transferWithAuthorizationTypes = {
|
|
116
|
+
TransferWithAuthorization: [
|
|
117
|
+
{ name: "from", type: "address" },
|
|
118
|
+
{ name: "to", type: "address" },
|
|
119
|
+
{ name: "value", type: "uint256" },
|
|
120
|
+
{ name: "validAfter", type: "uint256" },
|
|
121
|
+
{ name: "validBefore", type: "uint256" },
|
|
122
|
+
{ name: "nonce", type: "bytes32" },
|
|
123
|
+
],
|
|
124
|
+
};
|
|
125
|
+
export function transferAuthorizationTypedData(chainId, token, tokenName, tokenVersion, auth) {
|
|
126
|
+
return {
|
|
127
|
+
domain: { name: tokenName, version: tokenVersion, chainId, verifyingContract: token },
|
|
128
|
+
types: transferWithAuthorizationTypes,
|
|
129
|
+
primaryType: "TransferWithAuthorization",
|
|
130
|
+
message: auth,
|
|
131
|
+
};
|
|
132
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purseragents/sdk: a bank account for your AI agent, on Robinhood Chain.
|
|
3
|
+
*
|
|
4
|
+
* import { Purser, usdg } from "@purseragents/sdk";
|
|
5
|
+
* const purser = new Purser({ baseUrl: "http://localhost:8787", apiKey: process.env.PURSER_API_KEY });
|
|
6
|
+
* const r = await purser.pay({ to: "0x...", amount: usdg("2.50"), memo: "data report" });
|
|
7
|
+
* if (r.intent.status === "blocked") console.log(r.verdict.summary);
|
|
8
|
+
*/
|
|
9
|
+
import { type Hex, type LocalAccount } from "viem";
|
|
10
|
+
export { Purser, PurserError, type PurserOptions } from "./client.js";
|
|
11
|
+
export { verifyWebhook } from "./webhooks.js";
|
|
12
|
+
export * from "./eip712.js";
|
|
13
|
+
export * from "./chain.js";
|
|
14
|
+
export * from "./x402.js";
|
|
15
|
+
export { purserModuleAbi, purserFactoryAbi, purserRecurringAbi, purserEscrowAbi, purserPasskeyAbi, safeAbi, erc20Abi, erc4626Abi } from "./abi.js";
|
|
16
|
+
export * from "./tools.js";
|
|
17
|
+
export type * from "./schema.js";
|
|
18
|
+
/** USDG amount in base units (6 decimals): usdg("2.50") === 2500000n. */
|
|
19
|
+
export declare const usdg: (v: string | number) => bigint;
|
|
20
|
+
/** ETH amount in wei. */
|
|
21
|
+
export declare const eth: (v: string | number) => bigint;
|
|
22
|
+
/**
|
|
23
|
+
* For agents that keep their own session key: sign a payment intent locally and send it with
|
|
24
|
+
* `purser.submitIntent(...)`. The engine still checks the rules and runs the firewall before relaying it.
|
|
25
|
+
*/
|
|
26
|
+
export declare function signPayIntent(agent: LocalAccount, account: {
|
|
27
|
+
safe: `0x${string}`;
|
|
28
|
+
module: `0x${string}`;
|
|
29
|
+
chainId?: number;
|
|
30
|
+
}, p: {
|
|
31
|
+
token: `0x${string}`;
|
|
32
|
+
to: `0x${string}`;
|
|
33
|
+
amount: bigint;
|
|
34
|
+
service?: string;
|
|
35
|
+
validForSeconds?: number;
|
|
36
|
+
}): Promise<{
|
|
37
|
+
kind: "pay";
|
|
38
|
+
safe: `0x${string}`;
|
|
39
|
+
nonce: string;
|
|
40
|
+
deadline: string;
|
|
41
|
+
signature: `0x${string}`;
|
|
42
|
+
pay: {
|
|
43
|
+
token: `0x${string}`;
|
|
44
|
+
to: `0x${string}`;
|
|
45
|
+
amount: string;
|
|
46
|
+
service: `0x${string}`;
|
|
47
|
+
};
|
|
48
|
+
}>;
|
|
49
|
+
export declare function signCallIntent(agent: LocalAccount, account: {
|
|
50
|
+
safe: `0x${string}`;
|
|
51
|
+
module: `0x${string}`;
|
|
52
|
+
chainId?: number;
|
|
53
|
+
}, c: {
|
|
54
|
+
target: `0x${string}`;
|
|
55
|
+
data: Hex;
|
|
56
|
+
value?: bigint;
|
|
57
|
+
spendToken?: `0x${string}`;
|
|
58
|
+
spendLimit?: bigint;
|
|
59
|
+
validForSeconds?: number;
|
|
60
|
+
}): Promise<{
|
|
61
|
+
kind: "call";
|
|
62
|
+
safe: `0x${string}`;
|
|
63
|
+
nonce: string;
|
|
64
|
+
deadline: string;
|
|
65
|
+
signature: `0x${string}`;
|
|
66
|
+
call: {
|
|
67
|
+
target: `0x${string}`;
|
|
68
|
+
value: string;
|
|
69
|
+
data: `0x${string}`;
|
|
70
|
+
spendToken: `0x${string}`;
|
|
71
|
+
spendLimit: string;
|
|
72
|
+
};
|
|
73
|
+
}>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purseragents/sdk: a bank account for your AI agent, on Robinhood Chain.
|
|
3
|
+
*
|
|
4
|
+
* import { Purser, usdg } from "@purseragents/sdk";
|
|
5
|
+
* const purser = new Purser({ baseUrl: "http://localhost:8787", apiKey: process.env.PURSER_API_KEY });
|
|
6
|
+
* const r = await purser.pay({ to: "0x...", amount: usdg("2.50"), memo: "data report" });
|
|
7
|
+
* if (r.intent.status === "blocked") console.log(r.verdict.summary);
|
|
8
|
+
*/
|
|
9
|
+
import { parseUnits } from "viem";
|
|
10
|
+
import { callTypedData, payTypedData, randomNonce, serviceId } from "./eip712.js";
|
|
11
|
+
export { Purser, PurserError } from "./client.js";
|
|
12
|
+
export { verifyWebhook } from "./webhooks.js";
|
|
13
|
+
export * from "./eip712.js";
|
|
14
|
+
export * from "./chain.js";
|
|
15
|
+
export * from "./x402.js";
|
|
16
|
+
export { purserModuleAbi, purserFactoryAbi, purserRecurringAbi, purserEscrowAbi, purserPasskeyAbi, safeAbi, erc20Abi, erc4626Abi } from "./abi.js";
|
|
17
|
+
export * from "./tools.js";
|
|
18
|
+
/** USDG amount in base units (6 decimals): usdg("2.50") === 2500000n. */
|
|
19
|
+
export const usdg = (v) => parseUnits(String(v), 6);
|
|
20
|
+
/** ETH amount in wei. */
|
|
21
|
+
export const eth = (v) => parseUnits(String(v), 18);
|
|
22
|
+
/**
|
|
23
|
+
* For agents that keep their own session key: sign a payment intent locally and send it with
|
|
24
|
+
* `purser.submitIntent(...)`. The engine still checks the rules and runs the firewall before relaying it.
|
|
25
|
+
*/
|
|
26
|
+
export async function signPayIntent(agent, account, p) {
|
|
27
|
+
const intent = {
|
|
28
|
+
safe: account.safe,
|
|
29
|
+
token: p.token,
|
|
30
|
+
to: p.to,
|
|
31
|
+
amount: p.amount,
|
|
32
|
+
service: p.service ? serviceId(p.service) : `0x${"0".repeat(64)}`,
|
|
33
|
+
nonce: randomNonce(),
|
|
34
|
+
deadline: BigInt(Math.floor(Date.now() / 1000) + (p.validForSeconds ?? 900)),
|
|
35
|
+
};
|
|
36
|
+
const signature = await agent.signTypedData(payTypedData(account.chainId ?? 4663, account.module, intent));
|
|
37
|
+
return {
|
|
38
|
+
kind: "pay",
|
|
39
|
+
safe: intent.safe,
|
|
40
|
+
nonce: intent.nonce.toString(),
|
|
41
|
+
deadline: intent.deadline.toString(),
|
|
42
|
+
signature,
|
|
43
|
+
pay: { token: intent.token, to: intent.to, amount: intent.amount.toString(), service: intent.service },
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export async function signCallIntent(agent, account, c) {
|
|
47
|
+
const intent = {
|
|
48
|
+
safe: account.safe,
|
|
49
|
+
target: c.target,
|
|
50
|
+
value: c.value ?? 0n,
|
|
51
|
+
data: c.data,
|
|
52
|
+
spendToken: c.spendToken ?? "0x0000000000000000000000000000000000000000",
|
|
53
|
+
spendLimit: c.spendLimit ?? 0n,
|
|
54
|
+
nonce: randomNonce(),
|
|
55
|
+
deadline: BigInt(Math.floor(Date.now() / 1000) + (c.validForSeconds ?? 900)),
|
|
56
|
+
};
|
|
57
|
+
const signature = await agent.signTypedData(callTypedData(account.chainId ?? 4663, account.module, intent));
|
|
58
|
+
return {
|
|
59
|
+
kind: "call",
|
|
60
|
+
safe: intent.safe,
|
|
61
|
+
nonce: intent.nonce.toString(),
|
|
62
|
+
deadline: intent.deadline.toString(),
|
|
63
|
+
signature,
|
|
64
|
+
call: { target: intent.target, value: intent.value.toString(), data: intent.data, spendToken: intent.spendToken, spendLimit: intent.spendLimit.toString() },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type PaymentPayload, type PaymentRequired, type PaymentRequirements, type SettleResponse } from "./x402.js";
|
|
3
|
+
/**
|
|
4
|
+
* Merchant side of x402: price an endpoint, receive into an address (a Purser account receives through the Purser
|
|
5
|
+
* module, so the owner's revenue splits apply on arrival). Works with any facilitator that speaks x402 v2; the
|
|
6
|
+
* default is a Purser engine's facilitator.
|
|
7
|
+
*/
|
|
8
|
+
export interface PaywallOptions {
|
|
9
|
+
facilitatorUrl: string;
|
|
10
|
+
payTo: Address;
|
|
11
|
+
asset: Address;
|
|
12
|
+
price: string | bigint;
|
|
13
|
+
network?: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
mimeType?: string;
|
|
16
|
+
serviceName?: string;
|
|
17
|
+
maxTimeoutSeconds?: number;
|
|
18
|
+
tokenName?: string;
|
|
19
|
+
tokenVersion?: string;
|
|
20
|
+
fetch?: typeof fetch;
|
|
21
|
+
}
|
|
22
|
+
export declare function requirementsFor(o: PaywallOptions): PaymentRequirements;
|
|
23
|
+
export type PaywallDecision = {
|
|
24
|
+
kind: "pay";
|
|
25
|
+
status: 402;
|
|
26
|
+
headers: Record<string, string>;
|
|
27
|
+
body: PaymentRequired;
|
|
28
|
+
} | {
|
|
29
|
+
kind: "ok";
|
|
30
|
+
payment: PaymentPayload;
|
|
31
|
+
payer: Address | undefined;
|
|
32
|
+
settle: () => Promise<SettleResponse>;
|
|
33
|
+
};
|
|
34
|
+
/** Framework-neutral check: returns either the 402 to send, or a verified payment to settle after the work. */
|
|
35
|
+
export declare function checkPayment(o: PaywallOptions, url: string, headers: {
|
|
36
|
+
get(name: string): string | null | undefined;
|
|
37
|
+
}): Promise<PaywallDecision>;
|
|
38
|
+
/** Hono middleware. Settles after the handler answered with a status below 400. */
|
|
39
|
+
export declare function purserPaywall(o: PaywallOptions): (c: any, next: () => Promise<void>) => Promise<any>;
|
|
40
|
+
/** Express middleware. Settles when the response finishes with a status below 400. */
|
|
41
|
+
export declare function purserPaywallExpress(o: PaywallOptions): (req: any, res: any, next: (e?: unknown) => void) => Promise<void>;
|