@degenly/agent 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +143 -2
- package/SKILL.md +90 -0
- package/dist/api.js +242 -0
- package/dist/cli.js +780 -0
- package/dist/client.js +213 -0
- package/dist/demo/fill.js +143 -0
- package/dist/demo/store.js +144 -0
- package/dist/keys.js +138 -0
- package/dist/mcp.js +272 -0
- package/dist/sanitize.js +35 -0
- package/dist/sign.js +162 -0
- package/dist/tools/confirm.js +84 -0
- package/dist/tools/live-read.js +338 -0
- package/dist/tools/live-trade.js +173 -0
- package/dist/tx.js +417 -0
- package/docs/clients.md +73 -0
- package/npm-shrinkwrap.json +2508 -0
- package/package.json +52 -4
package/dist/tx.js
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DGAJ16: signs the Solana transaction of a `/v1/quote` answer with the agent wallet. No dependency (node:crypto
|
|
3
|
+
* ed25519 lives in ./keys.ts behind `Signer`); the byte layout is the server's own decoder, read only:
|
|
4
|
+
*
|
|
5
|
+
* wire tx = compact-u16 N ‖ N × 64-byte signature slot ‖ MESSAGE (degenly/src/swap/programs.ts:93-155)
|
|
6
|
+
* MESSAGE = [0x80 | version (v0 only)] ‖ requiredSignatures ‖ readonlySigned ‖ readonlyUnsigned
|
|
7
|
+
* ‖ compact-u16 K ‖ K × 32-byte static keys ‖ 32-byte blockhash ‖ instructions [‖ lookup tables (v0)]
|
|
8
|
+
* signed = the MESSAGE bytes (everything after the slots: programs.ts:164-173 `signedMessageBytes`)
|
|
9
|
+
* slot i = signer keys[i], i < requiredSignatures (degenly/src/orders/e2e.test.ts:86-93 `signWire`)
|
|
10
|
+
*
|
|
11
|
+
* Fail closed: anything not decoded with certainty (other message version, trailing / missing bytes, slot count
|
|
12
|
+
* != requiredSignatures, more signers than keys) → TX_INVALID; the agent wallet not among the signers →
|
|
13
|
+
* TX_NOT_FOR_WALLET (nothing is signed). Only the agent's own slot is written; every other byte stays as quoted,
|
|
14
|
+
* so the server's GF1 check (sha256 of the message == the quoted one, swapApi.ts:875-880) holds.
|
|
15
|
+
*
|
|
16
|
+
* There is NO export that signs caller-given bytes for a tool: ./tools/live-trade.ts calls this only with the
|
|
17
|
+
* transaction of the quote it fetched itself in the same call.
|
|
18
|
+
*
|
|
19
|
+
* DGAJ22 (security audit DGAJ14 E-1): the quote comes from whatever DEGENLY_API_BASE points at, so the package no
|
|
20
|
+
* longer trusts it. `checkAgentTx` runs BEFORE the human is asked and again inside `signTransactionFor`; a refusal
|
|
21
|
+
* means nothing is signed. It is a copy of the degenly repo's own rules (read only):
|
|
22
|
+
* (a) every invoked program ∈ ALLOWED_PROGRAMS (degenly/src/swap/programs.ts:27-35, :117-127)
|
|
23
|
+
* (b) a program id behind an address-lookup table → TX_INVALID (programs.ts:123-124; signPolicy.ts:238-239)
|
|
24
|
+
* (c) what leaves the agent wallet (System Transfer / CreateAccount(+WithSeed), Token Transfer(Checked) with the
|
|
25
|
+
* agent as authority, CloseAccount owned by the agent, ATA creation paid by the agent) may only go to the
|
|
26
|
+
* agent itself / an account the agent owns; everything else that touches the agent wallet is refused.
|
|
27
|
+
* = the standard GF5 policy `checkStandardTx` (degenly/src/swap/signPolicy.ts:530-674) with the swap context
|
|
28
|
+
* of the server (`feeRecipient: undefined`, embeddedSigner.gf5.test.ts:113-118): NO fee / tip destination
|
|
29
|
+
* is allowed; the Jupiter Trigger co-signer and the in-app withdraw exceptions are NOT copied (no swap uses
|
|
30
|
+
* them), so this is stricter than the site, never looser.
|
|
31
|
+
* Limit (stated, not hidden): a Jupiter program instruction is opaque here, exactly as on the server
|
|
32
|
+
* (signPolicy.ts:67-71). A bad route through an allowed program is NOT caught.
|
|
33
|
+
*/
|
|
34
|
+
import { createHash } from "node:crypto";
|
|
35
|
+
import { base58Decode, base58Encode } from "./sign.js";
|
|
36
|
+
// ---- (a) program allow-list ------------------------------------------------------------------------
|
|
37
|
+
/**
|
|
38
|
+
* Must be the same set as `ALLOWED_PROGRAMS` in the degenly repo, src/swap/programs.ts:27-35 (7 programs; the site
|
|
39
|
+
* labels both Jupiter ids "Jupiter"). Dflow / OKX routers are not listed there either.
|
|
40
|
+
*/
|
|
41
|
+
export const ALLOWED_PROGRAMS = [
|
|
42
|
+
{ name: "Compute Budget", id: "ComputeBudget111111111111111111111111111111" }, // programs.ts:28
|
|
43
|
+
{ name: "System", id: "11111111111111111111111111111111" }, // programs.ts:29
|
|
44
|
+
{ name: "Token", id: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" }, // programs.ts:30
|
|
45
|
+
{ name: "Token-2022", id: "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb" }, // programs.ts:31
|
|
46
|
+
{ name: "Associated Token Account", id: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" }, // programs.ts:32
|
|
47
|
+
{ name: "Jupiter", id: "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4" }, // programs.ts:33
|
|
48
|
+
{ name: "Jupiter RFQ", id: "61DFfeTKM7trxYcPQCM78bJ794ddZprZpAwAnLiwTpYH" }, // programs.ts:34
|
|
49
|
+
];
|
|
50
|
+
const ALLOWED = new Set(ALLOWED_PROGRAMS.map((p) => p.id));
|
|
51
|
+
// same constants as degenly/src/swap/signPolicy.ts:82-86
|
|
52
|
+
const SYSTEM_PROGRAM = "11111111111111111111111111111111";
|
|
53
|
+
const COMPUTE_BUDGET_PROGRAM = "ComputeBudget111111111111111111111111111111";
|
|
54
|
+
const TOKEN_PROGRAM = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
55
|
+
const TOKEN_2022_PROGRAM = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb";
|
|
56
|
+
const ATA_PROGRAM = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
|
|
57
|
+
/** degenly/src/config/constants.ts:629 */
|
|
58
|
+
const SOL_MINT = "So11111111111111111111111111111111111111112";
|
|
59
|
+
/** degenly/src/config/constants.ts:1850 `ORDER_DEPOSIT_RENT_MAX_LAMPORTS` */
|
|
60
|
+
const RENT_MAX_LAMPORTS = 2500000n;
|
|
61
|
+
class Reader {
|
|
62
|
+
b;
|
|
63
|
+
o = 0;
|
|
64
|
+
constructor(b) {
|
|
65
|
+
this.b = b;
|
|
66
|
+
}
|
|
67
|
+
byte() {
|
|
68
|
+
const v = this.b[this.o];
|
|
69
|
+
if (v === undefined)
|
|
70
|
+
throw new RangeError("eof");
|
|
71
|
+
this.o++;
|
|
72
|
+
return v;
|
|
73
|
+
}
|
|
74
|
+
/** Solana compact-u16 (same as programs.ts Reader.cu16) */
|
|
75
|
+
cu16() {
|
|
76
|
+
let v = 0;
|
|
77
|
+
for (let i = 0; i < 3; i++) {
|
|
78
|
+
const x = this.byte();
|
|
79
|
+
v |= (x & 0x7f) << (7 * i);
|
|
80
|
+
if ((x & 0x80) === 0)
|
|
81
|
+
return v;
|
|
82
|
+
}
|
|
83
|
+
throw new RangeError("bad compact-u16");
|
|
84
|
+
}
|
|
85
|
+
take(n) {
|
|
86
|
+
if (n < 0 || this.o + n > this.b.length)
|
|
87
|
+
throw new RangeError("eof");
|
|
88
|
+
const s = this.b.subarray(this.o, this.o + n);
|
|
89
|
+
this.o += n;
|
|
90
|
+
return s;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** The layout of a serialized transaction, or undefined when it is not decoded with certainty. */
|
|
94
|
+
export function parseTxLayout(tx) {
|
|
95
|
+
try {
|
|
96
|
+
const r = new Reader(tx);
|
|
97
|
+
const numSignatures = r.cu16();
|
|
98
|
+
const slotsOffset = r.o;
|
|
99
|
+
r.take(numSignatures * 64);
|
|
100
|
+
const messageOffset = r.o;
|
|
101
|
+
let version = "legacy";
|
|
102
|
+
const first = r.b[r.o];
|
|
103
|
+
if (first === undefined)
|
|
104
|
+
return undefined;
|
|
105
|
+
if (first & 0x80) {
|
|
106
|
+
if ((first & 0x7f) !== 0)
|
|
107
|
+
return undefined;
|
|
108
|
+
r.o++;
|
|
109
|
+
version = 0;
|
|
110
|
+
}
|
|
111
|
+
const requiredSignatures = r.byte();
|
|
112
|
+
r.byte(); // readonly signed
|
|
113
|
+
r.byte(); // readonly unsigned
|
|
114
|
+
const nkeys = r.cu16();
|
|
115
|
+
const keys = [];
|
|
116
|
+
for (let i = 0; i < nkeys; i++)
|
|
117
|
+
keys.push(base58Encode(r.take(32)));
|
|
118
|
+
r.take(32); // recent blockhash
|
|
119
|
+
const nix = r.cu16();
|
|
120
|
+
const instructions = [];
|
|
121
|
+
for (let i = 0; i < nix; i++) {
|
|
122
|
+
const programIndex = r.byte();
|
|
123
|
+
const accounts = [...r.take(r.cu16())];
|
|
124
|
+
instructions.push({ programIndex, accounts, data: r.take(r.cu16()) });
|
|
125
|
+
}
|
|
126
|
+
if (version === 0) {
|
|
127
|
+
const nlut = r.cu16();
|
|
128
|
+
for (let i = 0; i < nlut; i++) {
|
|
129
|
+
r.take(32);
|
|
130
|
+
r.take(r.cu16());
|
|
131
|
+
r.take(r.cu16());
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (r.o !== r.b.length)
|
|
135
|
+
return undefined;
|
|
136
|
+
if (requiredSignatures === 0 || requiredSignatures !== numSignatures || requiredSignatures > nkeys)
|
|
137
|
+
return undefined;
|
|
138
|
+
return { version, numSignatures, slotsOffset, messageOffset, requiredSignatures, signers: keys.slice(0, requiredSignatures), keys, instructions };
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** Where the agent wallet signs, without signing (layout + signer only; the full check is `checkAgentTx`). */
|
|
145
|
+
export function agentSlot(tx, address) {
|
|
146
|
+
const layout = parseTxLayout(tx);
|
|
147
|
+
if (!layout)
|
|
148
|
+
return { ok: false, error: "TX_INVALID" };
|
|
149
|
+
const slot = layout.signers.indexOf(address);
|
|
150
|
+
if (slot < 0)
|
|
151
|
+
return { ok: false, error: "TX_NOT_FOR_WALLET" };
|
|
152
|
+
return { ok: true, slot, layout };
|
|
153
|
+
}
|
|
154
|
+
// ---- PDA / ATA (sync copy of degenly/src/data/helius.ts:182-237 + signPolicy.ts:262-277) -------------
|
|
155
|
+
const P = (1n << 255n) - 19n;
|
|
156
|
+
function modPow(b, e, m) {
|
|
157
|
+
let r = 1n;
|
|
158
|
+
b %= m;
|
|
159
|
+
while (e > 0n) {
|
|
160
|
+
if (e & 1n)
|
|
161
|
+
r = (r * b) % m;
|
|
162
|
+
b = (b * b) % m;
|
|
163
|
+
e >>= 1n;
|
|
164
|
+
}
|
|
165
|
+
return r;
|
|
166
|
+
}
|
|
167
|
+
const D = (((-121665n * modPow(121666n, P - 2n, P)) % P) + P) % P;
|
|
168
|
+
/** ed25519 point decompression check (helius.ts:200-212, the rule Solana's `is_on_curve` uses). */
|
|
169
|
+
function isOnCurve(bytes) {
|
|
170
|
+
if (bytes.length !== 32)
|
|
171
|
+
return false;
|
|
172
|
+
let y = 0n;
|
|
173
|
+
for (let i = 31; i >= 0; i--)
|
|
174
|
+
y = (y << 8n) | BigInt(bytes[i] ?? 0);
|
|
175
|
+
y &= (1n << 255n) - 1n;
|
|
176
|
+
y %= P;
|
|
177
|
+
const y2 = (y * y) % P;
|
|
178
|
+
const u = (y2 - 1n + P) % P;
|
|
179
|
+
const v = (D * y2 + 1n) % P;
|
|
180
|
+
const ratio = (u * modPow(v, P - 2n, P)) % P;
|
|
181
|
+
if (ratio === 0n)
|
|
182
|
+
return true;
|
|
183
|
+
return modPow(ratio, (P - 1n) / 2n, P) === 1n;
|
|
184
|
+
}
|
|
185
|
+
const PDA_MARKER = new TextEncoder().encode("ProgramDerivedAddress");
|
|
186
|
+
/** Solana `find_program_address` (helius.ts:229-237). */
|
|
187
|
+
function findProgramAddress(seeds, programId) {
|
|
188
|
+
const program = base58Decode(programId);
|
|
189
|
+
if (!program || program.length !== 32)
|
|
190
|
+
return undefined;
|
|
191
|
+
for (let bump = 255; bump >= 0; bump--) {
|
|
192
|
+
const h = createHash("sha256");
|
|
193
|
+
for (const s of seeds)
|
|
194
|
+
h.update(s);
|
|
195
|
+
h.update(Uint8Array.of(bump)).update(program).update(PDA_MARKER);
|
|
196
|
+
const out = new Uint8Array(h.digest());
|
|
197
|
+
if (!isOnCurve(out))
|
|
198
|
+
return base58Encode(out);
|
|
199
|
+
}
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
/** ATA(owner, mint) under `tokenProgram` (Solana `get_associated_token_address`); undefined on a bad key. */
|
|
203
|
+
export function associatedTokenAddress(owner, mint, tokenProgram) {
|
|
204
|
+
const o = base58Decode(owner);
|
|
205
|
+
const m = base58Decode(mint);
|
|
206
|
+
const p = base58Decode(tokenProgram);
|
|
207
|
+
if (!o || !m || !p || o.length !== 32 || m.length !== 32 || p.length !== 32)
|
|
208
|
+
return undefined;
|
|
209
|
+
return findProgramAddress([o, p, m], ATA_PROGRAM);
|
|
210
|
+
}
|
|
211
|
+
/** The agent's ATAs of `mints` + wSOL under Token and Token-2022 (signPolicy.ts:262-277 `userAtas`). */
|
|
212
|
+
function agentAtas(agent, mints) {
|
|
213
|
+
const out = new Set();
|
|
214
|
+
for (const m of new Set([...mints, SOL_MINT])) {
|
|
215
|
+
for (const prog of [TOKEN_PROGRAM, TOKEN_2022_PROGRAM]) {
|
|
216
|
+
const a = associatedTokenAddress(agent, m, prog);
|
|
217
|
+
if (a)
|
|
218
|
+
out.add(a);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
function u32(d, off) {
|
|
224
|
+
if (off + 4 > d.length)
|
|
225
|
+
return undefined;
|
|
226
|
+
return ((d[off] ?? 0) | ((d[off + 1] ?? 0) << 8) | ((d[off + 2] ?? 0) << 16)) + (d[off + 3] ?? 0) * 0x1000000;
|
|
227
|
+
}
|
|
228
|
+
function u64(d, off) {
|
|
229
|
+
if (off + 8 > d.length)
|
|
230
|
+
return undefined;
|
|
231
|
+
let v = 0n;
|
|
232
|
+
for (let i = 7; i >= 0; i--)
|
|
233
|
+
v = (v << 8n) | BigInt(d[off + i] ?? 0);
|
|
234
|
+
return v;
|
|
235
|
+
}
|
|
236
|
+
function keyAt(d, off) {
|
|
237
|
+
return off + 32 <= d.length ? base58Encode(d.subarray(off, off + 32)) : undefined;
|
|
238
|
+
}
|
|
239
|
+
// ---- the check ------------------------------------------------------------------------------------
|
|
240
|
+
/**
|
|
241
|
+
* DGAJ22: may the agent wallet sign this quoted transaction? Pure, no network. Order:
|
|
242
|
+
* layout (TX_INVALID) → agent among the signers (TX_NOT_FOR_WALLET) → no program id behind a lookup table
|
|
243
|
+
* (TX_INVALID, signPolicy.ts:239) → every program allowed (TX_PROGRAM_NOT_ALLOWED, programs.ts:126 / signPolicy.ts:533)
|
|
244
|
+
* → the GF5 standard policy for the agent (signPolicy.ts:537-673, swap context, no exception):
|
|
245
|
+
* value to a foreign account → TX_UNEXPECTED_TRANSFER; any other instruction kind with the agent → TX_INSTRUCTION_NOT_ALLOWED.
|
|
246
|
+
*/
|
|
247
|
+
export function checkAgentTx(tx, agent) {
|
|
248
|
+
const at = agentSlot(tx, agent);
|
|
249
|
+
if (!at.ok)
|
|
250
|
+
return at;
|
|
251
|
+
const { keys, instructions } = at.layout;
|
|
252
|
+
const ixs = [];
|
|
253
|
+
for (const ix of instructions) {
|
|
254
|
+
const programId = keys[ix.programIndex];
|
|
255
|
+
if (programId === undefined)
|
|
256
|
+
return { ok: false, error: "TX_INVALID" }; // invoked program behind a lookup table
|
|
257
|
+
// an account index past the static keys is a lookup-table address: never the agent (a signer is always static)
|
|
258
|
+
ixs.push({ programId, accounts: ix.accounts.map((a) => keys[a] ?? `lut#${a}`), data: ix.data });
|
|
259
|
+
}
|
|
260
|
+
if (ixs.length === 0)
|
|
261
|
+
return { ok: false, error: "TX_INVALID" }; // programs.ts:150 `programs.length > 0`
|
|
262
|
+
for (const ix of ixs)
|
|
263
|
+
if (!ALLOWED.has(ix.programId))
|
|
264
|
+
return { ok: false, error: "TX_PROGRAM_NOT_ALLOWED" };
|
|
265
|
+
const refuse = (error) => ({ ok: false, error });
|
|
266
|
+
// Pass 1: ownership facts established by THIS tx (signPolicy.ts:542-566; the ATA program enforces ata = ATA(owner, mint)).
|
|
267
|
+
const owner = new Map();
|
|
268
|
+
const mints = new Set();
|
|
269
|
+
for (const ix of ixs) {
|
|
270
|
+
const a = ix.accounts;
|
|
271
|
+
if (ix.programId === ATA_PROGRAM && a[1] !== undefined && a[2] !== undefined) {
|
|
272
|
+
owner.set(a[1], a[2]);
|
|
273
|
+
if (a[3] !== undefined)
|
|
274
|
+
mints.add(a[3]);
|
|
275
|
+
}
|
|
276
|
+
if (ix.programId === TOKEN_PROGRAM || ix.programId === TOKEN_2022_PROGRAM) {
|
|
277
|
+
const op = ix.data[0];
|
|
278
|
+
const o18 = op === 18 && ix.data.length === 33 ? keyAt(ix.data, 1) : undefined; // InitializeAccount3
|
|
279
|
+
const o16 = op === 16 && ix.data.length === 33 ? keyAt(ix.data, 1) : undefined; // InitializeAccount2
|
|
280
|
+
const o1 = op === 1 && ix.data.length === 1 ? a[2] : undefined; // InitializeAccount [acct, mint, owner, rent]
|
|
281
|
+
const o = o18 ?? o16 ?? o1;
|
|
282
|
+
if (o !== undefined && a[0] !== undefined)
|
|
283
|
+
owner.set(a[0], o);
|
|
284
|
+
if (op === 12 && a[1] !== undefined)
|
|
285
|
+
mints.add(a[1]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const atas = agentAtas(agent, mints);
|
|
289
|
+
// The site's `trusted` minus the Jupiter Trigger co-signer owners (no swap has that co-signer): own accounts only.
|
|
290
|
+
const own = (acct) => acct !== undefined && (acct === agent || atas.has(acct) || owner.get(acct) === agent);
|
|
291
|
+
// Pass 2 (signPolicy.ts:579-665)
|
|
292
|
+
for (const ix of ixs) {
|
|
293
|
+
const a = ix.accounts;
|
|
294
|
+
const data = ix.data;
|
|
295
|
+
if (ix.programId === COMPUTE_BUDGET_PROGRAM) {
|
|
296
|
+
if ((data[0] === 2 && data.length === 5) || (data[0] === 3 && data.length === 9) || (data[0] === 1 && data.length === 5))
|
|
297
|
+
continue;
|
|
298
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
299
|
+
}
|
|
300
|
+
// Jupiter programs: opaque, allow-listed (the server's limit too, signPolicy.ts:67-71, :587-588).
|
|
301
|
+
if (ix.programId !== SYSTEM_PROGRAM && ix.programId !== TOKEN_PROGRAM && ix.programId !== TOKEN_2022_PROGRAM && ix.programId !== ATA_PROGRAM)
|
|
302
|
+
continue;
|
|
303
|
+
// An instruction that does not list the agent cannot spend the agent's funds.
|
|
304
|
+
if (!a.includes(agent))
|
|
305
|
+
continue;
|
|
306
|
+
if (ix.programId === SYSTEM_PROGRAM) {
|
|
307
|
+
const kind = u32(data, 0);
|
|
308
|
+
if (kind === 2 && data.length === 12) {
|
|
309
|
+
// Transfer [from, to]
|
|
310
|
+
if (u64(data, 4) === undefined || a[1] === undefined)
|
|
311
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
312
|
+
if (a[0] !== agent)
|
|
313
|
+
continue; // into the agent
|
|
314
|
+
if (own(a[1]))
|
|
315
|
+
continue; // e.g. SOL wrapped into the agent's own wSOL account
|
|
316
|
+
return refuse("TX_UNEXPECTED_TRANSFER"); // no fee / tip destination in a swap (feeRecipient undefined)
|
|
317
|
+
}
|
|
318
|
+
if (kind === 0 && data.length === 52) {
|
|
319
|
+
// CreateAccount [funder, new]: lamports u64 @4, space u64 @12, owner @20
|
|
320
|
+
if (a[0] !== agent)
|
|
321
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
322
|
+
const lamports = u64(data, 4);
|
|
323
|
+
const prog = keyAt(data, 20);
|
|
324
|
+
if (lamports === undefined || (prog !== TOKEN_PROGRAM && prog !== TOKEN_2022_PROGRAM))
|
|
325
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
326
|
+
if (!own(a[1]) || lamports > RENT_MAX_LAMPORTS)
|
|
327
|
+
return refuse("TX_UNEXPECTED_TRANSFER");
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (kind === 3) {
|
|
331
|
+
// CreateAccountWithSeed [funder, new, base]: base(32) | seedLen(u64) | seed | lamports | space | owner
|
|
332
|
+
if (a[0] !== agent)
|
|
333
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
334
|
+
const seedLen = u64(data, 36);
|
|
335
|
+
if (seedLen === undefined || seedLen > 32n)
|
|
336
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
337
|
+
const off = 44 + Number(seedLen);
|
|
338
|
+
const lamports = u64(data, off);
|
|
339
|
+
const prog = keyAt(data, off + 16);
|
|
340
|
+
if (lamports === undefined || data.length !== off + 48 || (prog !== TOKEN_PROGRAM && prog !== TOKEN_2022_PROGRAM))
|
|
341
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
342
|
+
if (!own(a[1]) || lamports > RENT_MAX_LAMPORTS)
|
|
343
|
+
return refuse("TX_UNEXPECTED_TRANSFER");
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
// TransferWithSeed [from(derived), base = agent, to]: value moved by the agent's signature, never in a swap
|
|
347
|
+
if (kind === 11)
|
|
348
|
+
return refuse("TX_UNEXPECTED_TRANSFER");
|
|
349
|
+
// Assign, Allocate, nonce … never with the agent (the server: INSTRUCTION_NOT_ALLOWED, signPolicy.ts:631)
|
|
350
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
351
|
+
}
|
|
352
|
+
if (ix.programId === ATA_PROGRAM) {
|
|
353
|
+
// Create / CreateIdempotent [payer, ata, owner, mint, system, token]
|
|
354
|
+
if (!(data.length === 0 || (data.length === 1 && (data[0] === 0 || data[0] === 1))))
|
|
355
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED");
|
|
356
|
+
if (a[0] === agent && a[2] !== agent)
|
|
357
|
+
return refuse("TX_UNEXPECTED_TRANSFER"); // the agent pays rent for someone else's ATA
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
// Token / Token-2022
|
|
361
|
+
const op = data[0];
|
|
362
|
+
if (op === 3 && data.length === 9) {
|
|
363
|
+
// Transfer [source, dest, authority]
|
|
364
|
+
if (a[2] !== agent)
|
|
365
|
+
continue;
|
|
366
|
+
if (!own(a[1]))
|
|
367
|
+
return refuse("TX_UNEXPECTED_TRANSFER");
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (op === 12 && data.length === 10) {
|
|
371
|
+
// TransferChecked [source, mint, dest, authority]
|
|
372
|
+
if (a[3] !== agent)
|
|
373
|
+
continue;
|
|
374
|
+
if (!own(a[2]))
|
|
375
|
+
return refuse("TX_UNEXPECTED_TRANSFER");
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (op === 9 && data.length === 1) {
|
|
379
|
+
// CloseAccount [account, destination, owner]: the rent goes back to the agent
|
|
380
|
+
if (a[2] !== agent)
|
|
381
|
+
continue;
|
|
382
|
+
if (!own(a[1]))
|
|
383
|
+
return refuse("TX_UNEXPECTED_TRANSFER");
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
if ((op === 17 && data.length === 1) || (op === 5 && data.length === 1))
|
|
387
|
+
continue; // SyncNative, Revoke
|
|
388
|
+
if ((op === 18 || op === 16) && data.length === 33)
|
|
389
|
+
continue; // InitializeAccount3 / 2 (owner recorded above)
|
|
390
|
+
if (op === 1 && data.length === 1)
|
|
391
|
+
continue; // InitializeAccount
|
|
392
|
+
return refuse("TX_INSTRUCTION_NOT_ALLOWED"); // Approve, SetAuthority, Burn, Freeze …
|
|
393
|
+
}
|
|
394
|
+
return at;
|
|
395
|
+
}
|
|
396
|
+
/** A copy of `tx` with the agent wallet's ed25519 signature over the message in its own slot (only after `checkAgentTx`). */
|
|
397
|
+
export function signTransactionFor(tx, signer) {
|
|
398
|
+
const at = checkAgentTx(tx, signer.address);
|
|
399
|
+
if (!at.ok)
|
|
400
|
+
return at;
|
|
401
|
+
const out = new Uint8Array(tx);
|
|
402
|
+
const sig = signer.sign(out.subarray(at.layout.messageOffset));
|
|
403
|
+
if (sig.length !== 64)
|
|
404
|
+
return { ok: false, error: "TX_INVALID" };
|
|
405
|
+
out.set(sig, at.layout.slotsOffset + at.slot * 64);
|
|
406
|
+
return { ok: true, signed: out, slot: at.slot, layout: at.layout };
|
|
407
|
+
}
|
|
408
|
+
const B64 = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
409
|
+
/** Strict base64 → bytes (undefined for anything else). */
|
|
410
|
+
export function base64ToBytes(b64) {
|
|
411
|
+
if (b64.length === 0 || b64.length % 4 !== 0 || !B64.test(b64))
|
|
412
|
+
return undefined;
|
|
413
|
+
return new Uint8Array(Buffer.from(b64, "base64"));
|
|
414
|
+
}
|
|
415
|
+
export function bytesToBase64(bytes) {
|
|
416
|
+
return Buffer.from(bytes).toString("base64");
|
|
417
|
+
}
|
package/docs/clients.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Client setup
|
|
2
|
+
|
|
3
|
+
`@degenly/agent` is a local MCP stdio server. Every client below starts it with `npx -y @degenly/agent@0.1.1`.
|
|
4
|
+
Demo tools use demo SOL only; live tools use the agent wallet after the 3 setup steps in the README.
|
|
5
|
+
|
|
6
|
+
How the confirmation step works in this package: before a live buy / sell is sent, the server asks the human with
|
|
7
|
+
an MCP **form elicitation** (one checkbox, "Confirm this real trade", 120 s). Only a ticked, accepted form sends the
|
|
8
|
+
trade. If the client does not announce form elicitation, the trade is NOT sent and the answer is
|
|
9
|
+
`CONFIRM_UNSUPPORTED`; only the human can then set `DEGENLY_CONFIRM=off` in the server's configuration (spending
|
|
10
|
+
limits on degenly.app `/bot` still apply). "Not verified" below means we have not confirmed it from the client's own
|
|
11
|
+
documentation or tested it with this package.
|
|
12
|
+
|
|
13
|
+
## Claude Code
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
claude mcp add degenly -- npx -y @degenly/agent@0.1.1
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Source: [Claude Code MCP](https://code.claude.com/docs/en/mcp).
|
|
20
|
+
|
|
21
|
+
- Confirmation form: the Claude Code MCP docs mention elicitation dialogs that block a tool call while open
|
|
22
|
+
(interactive sessions). Not tested with this package. Non-interactive runs (`claude -p`): not verified; any
|
|
23
|
+
answer other than a ticked, accepted form means nothing is sent.
|
|
24
|
+
- Extra safety layer: add `Read(~/.degenly/**)` to `permissions.deny` in `~/.claude/settings.json`. It does not
|
|
25
|
+
cover Bash commands in general, e.g. `grep -r` run from the folder ("Read and Edit" warning in
|
|
26
|
+
[Claude Code permissions](https://code.claude.com/docs/en/permissions)).
|
|
27
|
+
|
|
28
|
+
## Gemini CLI
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
gemini mcp add degenly npx -y @degenly/agent@0.1.1
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Source: [gemini-cli mcp-server.md](https://github.com/google-gemini/gemini-cli/blob/main/docs/tools/mcp-server.md)
|
|
35
|
+
(stdio transport; tool calls go through the CLI's own confirmation logic based on server trust settings).
|
|
36
|
+
|
|
37
|
+
- Confirmation form (elicitation): not verified. If it is not supported, live buys / sells answer
|
|
38
|
+
`CONFIRM_UNSUPPORTED` and nothing is sent.
|
|
39
|
+
- A file-read deny rule like Claude Code's: not verified.
|
|
40
|
+
|
|
41
|
+
## Cursor
|
|
42
|
+
|
|
43
|
+
Add to `mcp.json`:
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{ "mcpServers": { "degenly": { "command": "npx", "args": ["-y", "@degenly/agent@0.1.1"] } } }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Source: [Cursor MCP](https://cursor.com/docs/context/mcp) (stdio servers; the page lists elicitation support).
|
|
50
|
+
|
|
51
|
+
- Confirmation form: supported according to Cursor's docs. Not tested with this package.
|
|
52
|
+
- A file-read deny rule like Claude Code's: not verified.
|
|
53
|
+
|
|
54
|
+
## Claude Desktop
|
|
55
|
+
|
|
56
|
+
Add to `claude_desktop_config.json`:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{ "mcpServers": { "degenly": { "command": "npx", "args": ["-y", "@degenly/agent@0.1.1"] } } }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Source: [MCP: connect local servers](https://modelcontextprotocol.io/docs/develop/connect-local-servers)
|
|
63
|
+
(Claude Desktop asks the user before each tool call).
|
|
64
|
+
|
|
65
|
+
- Confirmation form (elicitation): not verified. If it is not supported, live buys / sells answer
|
|
66
|
+
`CONFIRM_UNSUPPORTED` and nothing is sent. Claude Desktop's own per-tool permission prompt is not the trade
|
|
67
|
+
confirmation: it does not show the trade summary.
|
|
68
|
+
|
|
69
|
+
## Not supported / not verified
|
|
70
|
+
|
|
71
|
+
- ChatGPT: developer mode accepts only remote servers (SSE / streaming HTTP), not a local stdio server
|
|
72
|
+
([ChatGPT developer mode](https://developers.openai.com/api/docs/guides/developer-mode)). Not supported.
|
|
73
|
+
- Gemini web app: not verified.
|