@cmdoss/agent-pass-plugin 0.0.0-stage → 0.1.0-dev.5.1
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 +131 -2
- package/dist/agentpass.js +199 -0
- package/dist/cli.js +37 -0
- package/dist/domain.js +103 -0
- package/dist/engine.js +260 -0
- package/dist/http.js +170 -0
- package/dist/mcp.js +259 -0
- package/dist/onboarding.js +258 -0
- package/dist/public/catalog.js +35 -0
- package/dist/public/http.js +89 -0
- package/dist/public-server.js +11 -0
- package/dist/runtime/config.js +16 -0
- package/dist/runtime/installation.js +107 -0
- package/dist/runtime/lock.js +38 -0
- package/dist/runtime/paths.js +56 -0
- package/dist/runtime/setup.js +155 -0
- package/dist/sdk-ui.js +48 -0
- package/dist/server.js +107 -0
- package/dist/shared/constants.js +9 -0
- package/dist/store.js +84 -0
- package/dist/testnet/adapter.js +275 -0
- package/dist/testnet/authorization.js +620 -0
- package/dist/testnet/chain/codec.js +68 -0
- package/dist/testnet/chain/inspection.js +54 -0
- package/dist/testnet/chain/policy.js +78 -0
- package/dist/testnet/chain/prepared.js +150 -0
- package/dist/testnet/chain/receipt.js +49 -0
- package/dist/testnet/chain/sdk.js +34 -0
- package/dist/testnet/chain/types.js +1 -0
- package/dist/testnet/engine.js +395 -0
- package/dist/testnet/http.js +139 -0
- package/dist/testnet/model.js +49 -0
- package/dist/testnet/policy.js +44 -0
- package/dist/testnet/read-model.js +217 -0
- package/dist/testnet/repository.js +62 -0
- package/dist/testnet/runner.js +306 -0
- package/dist/testnet/types.js +1 -0
- package/dist/workflows.js +379 -0
- package/docs/agent-pass-integration.md +75 -0
- package/docs/architecture.md +93 -0
- package/docs/npm-release.md +83 -0
- package/npm-shrinkwrap.json +5376 -0
- package/package.json +72 -4
- package/public/assets/agent-pass-plugin-client.js +16 -0
- package/public/assets/agent-pass-plugin-sdk.js +1 -0
- package/public/assets/sui-payment-rails.avif +0 -0
- package/public/assets/sui-payments-hero.json +1 -0
- package/public/fonts/TWKEverett-Medium.woff2 +0 -0
- package/public/fonts/TWKEverett-Regular.woff2 +0 -0
- package/public/fonts/TWKEverettMono-Medium.woff2 +0 -0
- package/public/fonts/TWKEverettMono-Regular.woff2 +0 -0
- package/public/index.html +81 -0
- package/public/styles/base.css +117 -0
- package/public/styles/catalog.css +279 -0
- package/public/styles/controls.css +118 -0
- package/public/styles/dialog.css +257 -0
- package/public/styles/home.css +82 -0
- package/public/styles/layout.css +45 -0
- package/public/styles/plans.css +271 -0
- package/public/styles/responsive.css +643 -0
- package/public/styles/review.css +150 -0
- package/public/styles/sdk-wallet.css +125 -0
- package/public/styles/sdk.css +441 -0
- package/public/styles/shell.css +348 -0
- package/public/styles/tokens.css +75 -0
- package/public/styles.css +11 -0
- package/scripts/setup.mjs +10 -0
- package/skills/agent-pass-plugin-trading/SKILL.md +60 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { SUI, address, integer, list, ratio, record, stable, } from "./chain/codec.js";
|
|
5
|
+
import { readInspection } from "./chain/inspection.js";
|
|
6
|
+
import { assertDeployment, validateBinding, validateInput, } from "./chain/policy.js";
|
|
7
|
+
import { validatePrepared } from "./chain/prepared.js";
|
|
8
|
+
import { decodeReceipt } from "./chain/receipt.js";
|
|
9
|
+
import { officialSDK } from "./chain/sdk.js";
|
|
10
|
+
const INCLUDE = {
|
|
11
|
+
effects: true,
|
|
12
|
+
balanceChanges: true,
|
|
13
|
+
events: true,
|
|
14
|
+
transaction: true,
|
|
15
|
+
};
|
|
16
|
+
/** Session lifetime and chain I/O; codecs and verification are separate pure modules. */
|
|
17
|
+
export class OfficialChainAdapter {
|
|
18
|
+
options;
|
|
19
|
+
sdkPromise;
|
|
20
|
+
sessions = new Map();
|
|
21
|
+
now;
|
|
22
|
+
quoteMaxAgeMs;
|
|
23
|
+
preparedTtlMs;
|
|
24
|
+
constructor(options = {}) {
|
|
25
|
+
this.options = options;
|
|
26
|
+
this.now = options.now ?? Date.now;
|
|
27
|
+
this.quoteMaxAgeMs = options.quoteMaxAgeMs ?? 30_000;
|
|
28
|
+
this.preparedTtlMs = Math.min(options.preparedTtlMs ?? 15_000, 15_000);
|
|
29
|
+
}
|
|
30
|
+
sdk() {
|
|
31
|
+
return (this.sdkPromise ??= this.options.sdk
|
|
32
|
+
? Promise.resolve(this.options.sdk)
|
|
33
|
+
: (this.options.loadSDK ?? officialSDK)());
|
|
34
|
+
}
|
|
35
|
+
async session(binding) {
|
|
36
|
+
validateBinding(binding);
|
|
37
|
+
// Recheck the stored public metadata even when reusing an already-open signer.
|
|
38
|
+
const metadata = await (this.options.readSessionMetadata ??
|
|
39
|
+
(async (directory) => JSON.parse(await readFile(join(directory, "session.json"), "utf8"))))(binding.directory);
|
|
40
|
+
const d = record(metadata.deployment), active = record(metadata.active);
|
|
41
|
+
assertDeployment(binding, d);
|
|
42
|
+
if (d.network !== "testnet" ||
|
|
43
|
+
d.chainIdentifier !== binding.chainIdentifier ||
|
|
44
|
+
address(d.packageId) !== address(binding.packageId) ||
|
|
45
|
+
address(d.configId) !== address(binding.configId) ||
|
|
46
|
+
address(metadata.sessionAddress) !== address(binding.sessionAddress) ||
|
|
47
|
+
address(active.owner) !== address(binding.owner) ||
|
|
48
|
+
address(active.passId) !== address(binding.passId) ||
|
|
49
|
+
active.authorizationDigest !== binding.authorizationDigest ||
|
|
50
|
+
stable(metadata.setup) !== stable(binding.expected.setup))
|
|
51
|
+
throw new Error("Local session metadata does not match the verified binding.");
|
|
52
|
+
if (!this.sessions.has(binding.directory)) {
|
|
53
|
+
const opening = (async () => {
|
|
54
|
+
return (await this.sdk()).open(binding.directory);
|
|
55
|
+
})();
|
|
56
|
+
this.sessions.set(binding.directory, opening);
|
|
57
|
+
opening.catch(() => {
|
|
58
|
+
if (this.sessions.get(binding.directory) === opening)
|
|
59
|
+
this.sessions.delete(binding.directory);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const session = await this.sessions.get(binding.directory);
|
|
63
|
+
assertDeployment(binding, session.identity.deployment);
|
|
64
|
+
return session;
|
|
65
|
+
}
|
|
66
|
+
async fresh(binding) {
|
|
67
|
+
const sdk = await this.sdk(), session = await this.session(binding);
|
|
68
|
+
await sdk.assertChain(session.client, session.identity.deployment);
|
|
69
|
+
const live = await sdk.readStatus(session.client, session.identity.deployment, session.identity.active, session.identity.sessionAddress);
|
|
70
|
+
const gasId = String(session.identity.active.gasAllowance ?? "");
|
|
71
|
+
const gasAllowance = gasId
|
|
72
|
+
? await sdk.readAllowance(session.client, gasId, SUI)
|
|
73
|
+
: null;
|
|
74
|
+
const info = { ...session.identity, ...live };
|
|
75
|
+
assertDeployment(binding, session.identity.deployment);
|
|
76
|
+
return { sdk, session, live, info, gasAllowance };
|
|
77
|
+
}
|
|
78
|
+
async inspect(binding, input) {
|
|
79
|
+
validateInput(binding, input);
|
|
80
|
+
return readInspection(binding, await this.fresh(binding));
|
|
81
|
+
}
|
|
82
|
+
assertSpend(inspection, amountIn) {
|
|
83
|
+
if (!inspection.active)
|
|
84
|
+
throw new Error(inspection.reason ?? "The bound Agent Pass is not active.");
|
|
85
|
+
const amount = integer(amountIn, true);
|
|
86
|
+
if (amount > integer(inspection.ownerInputBalance))
|
|
87
|
+
throw new Error("Owner address balance cannot fund this order.");
|
|
88
|
+
if (amount > integer(inspection.allowanceRemaining))
|
|
89
|
+
throw new Error("Agent Pass lifetime allowance is exhausted.");
|
|
90
|
+
if (amount > integer(inspection.dailyRemaining))
|
|
91
|
+
throw new Error("Agent Pass daily limit is exhausted.");
|
|
92
|
+
if (integer(inspection.agentGasBalance) < 2000000n)
|
|
93
|
+
throw new Error("Agent gas is insufficient; no automatic refill transaction will be sent.");
|
|
94
|
+
}
|
|
95
|
+
async quote(binding, input, amountIn) {
|
|
96
|
+
validateInput(binding, input);
|
|
97
|
+
if (integer(amountIn, true) > integer(input.amountIn, true))
|
|
98
|
+
throw new Error("The requested quote exceeds the approved per-order amount.");
|
|
99
|
+
const context = await this.fresh(binding);
|
|
100
|
+
this.assertSpend(readInspection(binding, context), amountIn);
|
|
101
|
+
const result = await context.sdk.quote(context.session.identity, {
|
|
102
|
+
coinIn: input.coinIn.type,
|
|
103
|
+
coinOut: input.coinOut.type,
|
|
104
|
+
poolId: binding.poolId,
|
|
105
|
+
venue: input.venue,
|
|
106
|
+
}, amountIn);
|
|
107
|
+
if (address(result.pool.poolId) !== address(binding.poolId) ||
|
|
108
|
+
integer(result.amountIn, true) > integer(amountIn, true))
|
|
109
|
+
throw new Error("The venue quote does not match the approved order.");
|
|
110
|
+
const inAmount = integer(result.amountIn, true), outAmount = integer(result.amountOut, true);
|
|
111
|
+
const price = input.coinIn.symbol === "USDC"
|
|
112
|
+
? ratio(inAmount * 10n ** BigInt(input.coinOut.decimals), outAmount * 10n ** BigInt(input.coinIn.decimals))
|
|
113
|
+
: ratio(outAmount * 10n ** BigInt(input.coinIn.decimals), inAmount * 10n ** BigInt(input.coinOut.decimals));
|
|
114
|
+
return {
|
|
115
|
+
amountIn: result.amountIn,
|
|
116
|
+
amountOut: result.amountOut,
|
|
117
|
+
at: this.now(),
|
|
118
|
+
poolId: binding.poolId,
|
|
119
|
+
price,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async prepare(binding, input, quote, slippageBps) {
|
|
123
|
+
validateInput(binding, input);
|
|
124
|
+
if (!Number.isInteger(slippageBps) || slippageBps < 1 || slippageBps > 500)
|
|
125
|
+
throw new Error("Slippage must be between 1 and 500 basis points.");
|
|
126
|
+
if (!Number.isSafeInteger(quote.at) ||
|
|
127
|
+
quote.at > this.now() + 1000 ||
|
|
128
|
+
this.now() - quote.at > this.quoteMaxAgeMs ||
|
|
129
|
+
address(quote.poolId) !== address(binding.poolId))
|
|
130
|
+
throw new Error("Quote is stale or belongs to a different pool.");
|
|
131
|
+
if (integer(quote.amountIn, true) > integer(input.amountIn, true))
|
|
132
|
+
throw new Error("Quoted input exceeds the approved order.");
|
|
133
|
+
const context = await this.fresh(binding), inspection = readInspection(binding, context);
|
|
134
|
+
this.assertSpend(inspection, quote.amountIn);
|
|
135
|
+
const minOut = ((integer(quote.amountOut, true) * BigInt(10_000 - slippageBps)) /
|
|
136
|
+
10000n).toString();
|
|
137
|
+
integer(minOut, true);
|
|
138
|
+
const tx = context.sdk.swap(context.session.identity, {
|
|
139
|
+
coinIn: input.coinIn.type,
|
|
140
|
+
coinOut: input.coinOut.type,
|
|
141
|
+
poolId: binding.poolId,
|
|
142
|
+
venue: input.venue,
|
|
143
|
+
}, quote.amountIn, minOut);
|
|
144
|
+
tx.setSender(binding.sessionAddress);
|
|
145
|
+
tx.setGasOwner(binding.sessionAddress);
|
|
146
|
+
tx.setGasPayment([]);
|
|
147
|
+
const { systemState } = await context.session.client.getCurrentSystemState();
|
|
148
|
+
const expiresAt = Math.min(this.now() + this.preparedTtlMs, quote.at + this.preparedTtlMs, Date.parse(binding.expiresAt));
|
|
149
|
+
if (expiresAt <= this.now())
|
|
150
|
+
throw new Error("The quote or bound pass expires before the transaction can be prepared.");
|
|
151
|
+
tx.setExpiration({
|
|
152
|
+
ValidDuring: {
|
|
153
|
+
minEpoch: systemState.epoch,
|
|
154
|
+
maxEpoch: (integer(systemState.epoch) + 1n).toString(),
|
|
155
|
+
minTimestamp: null,
|
|
156
|
+
maxTimestamp: null,
|
|
157
|
+
chain: binding.chainIdentifier,
|
|
158
|
+
nonce: randomBytes(4).readUInt32LE(),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
const bytes = await tx.build({ client: context.session.client });
|
|
162
|
+
const digest = await context.sdk.transactionFrom(bytes).getDigest();
|
|
163
|
+
const prepared = {
|
|
164
|
+
bytes: Buffer.from(bytes).toString("base64"),
|
|
165
|
+
digest,
|
|
166
|
+
amountIn: quote.amountIn,
|
|
167
|
+
minOut,
|
|
168
|
+
quotedOut: quote.amountOut,
|
|
169
|
+
quoteAt: quote.at,
|
|
170
|
+
expiresAt,
|
|
171
|
+
};
|
|
172
|
+
await validatePrepared(binding, prepared, context, {
|
|
173
|
+
now: this.now,
|
|
174
|
+
preparedTtlMs: this.preparedTtlMs,
|
|
175
|
+
quoteMaxAgeMs: this.quoteMaxAgeMs,
|
|
176
|
+
});
|
|
177
|
+
return prepared;
|
|
178
|
+
}
|
|
179
|
+
async sign(binding, prepared) {
|
|
180
|
+
const context = await this.fresh(binding);
|
|
181
|
+
this.assertSpend(readInspection(binding, context), prepared.amountIn);
|
|
182
|
+
const maxOrder = record(list(record(record(binding.expected.setup).policy).swaps)[0]).perTrade;
|
|
183
|
+
if (integer(prepared.amountIn) > integer(maxOrder, true))
|
|
184
|
+
throw new Error("Prepared amount exceeds the bound per-trade limit.");
|
|
185
|
+
const bytes = await validatePrepared(binding, prepared, context, {
|
|
186
|
+
now: this.now,
|
|
187
|
+
preparedTtlMs: this.preparedTtlMs,
|
|
188
|
+
quoteMaxAgeMs: this.quoteMaxAgeMs,
|
|
189
|
+
});
|
|
190
|
+
assertDeployment(binding, context.session.identity.deployment);
|
|
191
|
+
const reply = await context.session.mcp.callTool({
|
|
192
|
+
name: "sign_transaction",
|
|
193
|
+
arguments: { transactionBytes: prepared.bytes },
|
|
194
|
+
});
|
|
195
|
+
if (reply.isError)
|
|
196
|
+
throw new Error("The official local signer refused this transaction.");
|
|
197
|
+
const text = reply.content?.find((item) => item.type === "text")?.text;
|
|
198
|
+
if (!text)
|
|
199
|
+
throw new Error("The local signer returned no signature.");
|
|
200
|
+
const signed = JSON.parse(text);
|
|
201
|
+
if (signed.bytes !== prepared.bytes ||
|
|
202
|
+
signed.digest !== prepared.digest ||
|
|
203
|
+
address(signed.signer) !== address(binding.sessionAddress) ||
|
|
204
|
+
typeof signed.signature !== "string")
|
|
205
|
+
throw new Error("The local signer response does not match the persisted transaction.");
|
|
206
|
+
await context.sdk.verifySignature(bytes, signed.signature, binding.sessionAddress, context.session.client);
|
|
207
|
+
return { signature: signed.signature, digest: prepared.digest };
|
|
208
|
+
}
|
|
209
|
+
async submit(binding, prepared, signed, beforeBroadcast) {
|
|
210
|
+
if (signed.digest !== prepared.digest || !signed.signature)
|
|
211
|
+
throw new Error("Persisted signature does not match the prepared digest.");
|
|
212
|
+
const context = await this.fresh(binding);
|
|
213
|
+
this.assertSpend(readInspection(binding, context), prepared.amountIn);
|
|
214
|
+
const bytes = await validatePrepared(binding, prepared, context, {
|
|
215
|
+
now: this.now,
|
|
216
|
+
preparedTtlMs: this.preparedTtlMs,
|
|
217
|
+
quoteMaxAgeMs: this.quoteMaxAgeMs,
|
|
218
|
+
});
|
|
219
|
+
await context.sdk.verifySignature(bytes, signed.signature, binding.sessionAddress, context.session.client);
|
|
220
|
+
assertDeployment(binding, context.session.identity.deployment);
|
|
221
|
+
// The engine checks its persisted pause/stop epoch here, after every async
|
|
222
|
+
// policy/chain/signature check. Keep the callback outside the network catch
|
|
223
|
+
// so cancellation is not mistaken for an uncertain submission.
|
|
224
|
+
beforeBroadcast?.();
|
|
225
|
+
try {
|
|
226
|
+
const result = await context.session.client.executeTransaction({
|
|
227
|
+
transaction: bytes,
|
|
228
|
+
signatures: [signed.signature],
|
|
229
|
+
include: INCLUDE,
|
|
230
|
+
signal: AbortSignal.timeout(15_000),
|
|
231
|
+
});
|
|
232
|
+
return {
|
|
233
|
+
status: "confirmed",
|
|
234
|
+
receipt: decodeReceipt(binding, prepared, result),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
return { status: "pending" };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async lookup(binding, prepared) {
|
|
242
|
+
// Reconciliation must still work after pass expiry/revocation. Do not sign or
|
|
243
|
+
// rebuild anything here, and do not require a currently spendable allowance.
|
|
244
|
+
const session = await this.session(binding), sdk = await this.sdk();
|
|
245
|
+
await sdk.assertChain(session.client, session.identity.deployment);
|
|
246
|
+
try {
|
|
247
|
+
const result = await session.client.getTransaction({
|
|
248
|
+
digest: prepared.digest,
|
|
249
|
+
include: INCLUDE,
|
|
250
|
+
signal: AbortSignal.timeout(15_000),
|
|
251
|
+
});
|
|
252
|
+
return {
|
|
253
|
+
status: "confirmed",
|
|
254
|
+
receipt: decodeReceipt(binding, prepared, result),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
if (record(error).reason === "notFound")
|
|
259
|
+
return { status: "not_found" };
|
|
260
|
+
return { status: "pending" };
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async close() {
|
|
264
|
+
const pending = [...this.sessions.values()];
|
|
265
|
+
this.sessions.clear();
|
|
266
|
+
await Promise.all(pending.map(async (session) => {
|
|
267
|
+
try {
|
|
268
|
+
await (await session).close();
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
/* Close only SDK-owned connection/session child, never the authorization-provider process. */
|
|
272
|
+
}
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
}
|