@swarm-tips/client 0.4.2 → 0.4.4
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 +3 -2
- package/SBOM.spdx.json +5 -5
- package/dist/agent.d.ts +17 -1
- package/dist/agent.js +71 -54
- package/dist/cli/swarm.d.ts +5 -0
- package/dist/cli/swarm.js +48 -21
- package/dist/contracts/generated/coordination_game.d.ts +179 -1
- package/dist/contracts/generated/shillbot.d.ts +119 -12
- package/dist/contracts/idl/coordination_game.json +180 -2
- package/dist/contracts/idl/shillbot.json +119 -12
- package/dist/generated/openapi.d.ts +17 -2
- package/dist/generated/openapi.js +1 -1
- package/dist/http.js +1 -1
- package/dist/inbox/client.d.ts +2 -4
- package/dist/inbox/client.js +11 -6
- package/dist/inbox/index.d.ts +1 -1
- package/dist/inbox/index.js +1 -1
- package/dist/inbox/types.d.ts +9 -0
- package/dist/inbox/types.js +10 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/shillbot/api.d.ts +8 -1
- package/dist/shillbot/api.js +53 -133
- package/dist/shillbot/index.d.ts +1 -0
- package/dist/shillbot/index.js +3 -0
- package/dist/shillbot/verify-tx.d.ts +62 -0
- package/dist/shillbot/verify-tx.js +643 -0
- package/dist/signer/evm.d.ts +1 -1
- package/dist/signer/evm.js +72 -2
- package/dist/signer/index.d.ts +1 -1
- package/dist/signer/solana.d.ts +3 -2
- package/dist/signer/solana.js +36 -9
- package/dist/signer/types.d.ts +4 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/vow/index.d.ts +1 -1
- package/dist/vow/index.js +1 -1
- package/dist/vow/verify.d.ts +36 -0
- package/dist/vow/verify.js +458 -1
- package/package.json +1 -1
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
import { Buffer } from "buffer";
|
|
2
|
+
import { ComputeBudgetProgram, PublicKey, Secp256k1Program } from "@solana/web3.js";
|
|
3
|
+
import { SHILLBOT_PROGRAM_ID, shillbotInstructionDiscriminator } from "../contracts/index.js";
|
|
4
|
+
import { SwarmClientError } from "../errors.js";
|
|
5
|
+
import { inspectTransaction, } from "./index.js";
|
|
6
|
+
/** Default maximum allowed Solana priority fee price per compute unit (`100_000` micro-lamports/CU). */
|
|
7
|
+
export const MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS = 100000n;
|
|
8
|
+
/** Solana runtime ceiling for `ComputeBudget::SetComputeUnitLimit` (`1_400_000` CUs). */
|
|
9
|
+
export const MAX_COMPUTE_UNIT_LIMIT = 1_400_000;
|
|
10
|
+
const MIN_HEAP_FRAME_BYTES = 32 * 1024;
|
|
11
|
+
const MAX_HEAP_FRAME_BYTES = 256 * 1024;
|
|
12
|
+
const SWITCHBOARD_PROGRAM_IDS = {
|
|
13
|
+
mainnet: "SBondMDrcV3K4kxZR1HNVT7osZxAHVHgYXL5Ze1oMUv",
|
|
14
|
+
devnet: "Aio4gaXjXzJNVLtzwtNVmSqGKpANtXhybbkhtAC94ji2",
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Exact Anchor account-index and authority-slot specification for every Shillbot instruction,
|
|
18
|
+
* derived directly from `programs/shillbot/src/instructions/*.rs` and `shillbot.json` IDL.
|
|
19
|
+
*/
|
|
20
|
+
export const SHILLBOT_INSTRUCTION_SPECS = {
|
|
21
|
+
create_task: {
|
|
22
|
+
name: "create_task",
|
|
23
|
+
minAccounts: 6,
|
|
24
|
+
taskSlot: 1,
|
|
25
|
+
authoritySlot: 3, // client (Signer)
|
|
26
|
+
allowsSponsoredFeePayer: false,
|
|
27
|
+
},
|
|
28
|
+
claim_task: {
|
|
29
|
+
name: "claim_task",
|
|
30
|
+
minAccounts: 5,
|
|
31
|
+
taskSlot: 0,
|
|
32
|
+
authoritySlot: 3, // agent (Signer)
|
|
33
|
+
allowsSponsoredFeePayer: true,
|
|
34
|
+
},
|
|
35
|
+
claim_task_session: {
|
|
36
|
+
name: "claim_task_session",
|
|
37
|
+
minAccounts: 7,
|
|
38
|
+
taskSlot: 0,
|
|
39
|
+
authoritySlot: 4, // delegate (Signer)
|
|
40
|
+
allowsSponsoredFeePayer: true,
|
|
41
|
+
},
|
|
42
|
+
set_payout_to: {
|
|
43
|
+
name: "set_payout_to",
|
|
44
|
+
minAccounts: 2,
|
|
45
|
+
exactAccounts: 2,
|
|
46
|
+
taskSlot: 0,
|
|
47
|
+
authoritySlot: 1, // agent (Signer)
|
|
48
|
+
allowsSponsoredFeePayer: true,
|
|
49
|
+
},
|
|
50
|
+
submit_work: {
|
|
51
|
+
name: "submit_work",
|
|
52
|
+
minAccounts: 4,
|
|
53
|
+
taskSlot: 0,
|
|
54
|
+
authoritySlot: 3, // agent (Signer)
|
|
55
|
+
allowsSponsoredFeePayer: true,
|
|
56
|
+
},
|
|
57
|
+
submit_work_session: {
|
|
58
|
+
name: "submit_work_session",
|
|
59
|
+
minAccounts: 5,
|
|
60
|
+
taskSlot: 0,
|
|
61
|
+
authoritySlot: 4, // delegate (Signer)
|
|
62
|
+
allowsSponsoredFeePayer: true,
|
|
63
|
+
},
|
|
64
|
+
approve_task: {
|
|
65
|
+
name: "approve_task",
|
|
66
|
+
minAccounts: 2,
|
|
67
|
+
taskSlot: 0,
|
|
68
|
+
authoritySlot: 1, // client (Signer)
|
|
69
|
+
allowsSponsoredFeePayer: false,
|
|
70
|
+
},
|
|
71
|
+
verify_task: {
|
|
72
|
+
name: "verify_task",
|
|
73
|
+
minAccounts: 3,
|
|
74
|
+
taskSlot: 0,
|
|
75
|
+
authoritySlot: "verify_task",
|
|
76
|
+
allowsSponsoredFeePayer: false,
|
|
77
|
+
},
|
|
78
|
+
verify_task_attested: {
|
|
79
|
+
name: "verify_task_attested",
|
|
80
|
+
minAccounts: 3,
|
|
81
|
+
taskSlot: 0,
|
|
82
|
+
authoritySlot: "verify_task_attested", // attester at [2] (Signer)
|
|
83
|
+
allowsSponsoredFeePayer: true,
|
|
84
|
+
},
|
|
85
|
+
finalize_task: {
|
|
86
|
+
name: "finalize_task",
|
|
87
|
+
minAccounts: 5,
|
|
88
|
+
taskSlot: 0,
|
|
89
|
+
authoritySlot: "fee_payer",
|
|
90
|
+
allowsSponsoredFeePayer: false,
|
|
91
|
+
},
|
|
92
|
+
challenge_task: {
|
|
93
|
+
name: "challenge_task",
|
|
94
|
+
minAccounts: 5,
|
|
95
|
+
taskSlot: 0,
|
|
96
|
+
authoritySlot: 3, // challenger (Signer)
|
|
97
|
+
allowsSponsoredFeePayer: false,
|
|
98
|
+
},
|
|
99
|
+
expire_task: {
|
|
100
|
+
name: "expire_task",
|
|
101
|
+
minAccounts: 3,
|
|
102
|
+
taskSlot: 0,
|
|
103
|
+
authoritySlot: "fee_payer",
|
|
104
|
+
allowsSponsoredFeePayer: false,
|
|
105
|
+
},
|
|
106
|
+
resolve_challenge: {
|
|
107
|
+
name: "resolve_challenge",
|
|
108
|
+
minAccounts: 8,
|
|
109
|
+
taskSlot: 0,
|
|
110
|
+
authoritySlot: 3, // authority (Signer)
|
|
111
|
+
allowsSponsoredFeePayer: false,
|
|
112
|
+
},
|
|
113
|
+
resolve_challenge_default: {
|
|
114
|
+
name: "resolve_challenge_default",
|
|
115
|
+
minAccounts: 7,
|
|
116
|
+
taskSlot: 0,
|
|
117
|
+
authoritySlot: "fee_payer",
|
|
118
|
+
allowsSponsoredFeePayer: false,
|
|
119
|
+
},
|
|
120
|
+
create_session: {
|
|
121
|
+
name: "create_session",
|
|
122
|
+
minAccounts: 4,
|
|
123
|
+
taskSlot: null,
|
|
124
|
+
authoritySlot: 1, // agent (Signer)
|
|
125
|
+
allowsSponsoredFeePayer: false,
|
|
126
|
+
},
|
|
127
|
+
revoke_session: {
|
|
128
|
+
name: "revoke_session",
|
|
129
|
+
minAccounts: 2,
|
|
130
|
+
taskSlot: null,
|
|
131
|
+
authoritySlot: 1, // agent (Signer)
|
|
132
|
+
allowsSponsoredFeePayer: false,
|
|
133
|
+
},
|
|
134
|
+
register_identity: {
|
|
135
|
+
name: "register_identity",
|
|
136
|
+
minAccounts: 3,
|
|
137
|
+
taskSlot: null,
|
|
138
|
+
authoritySlot: 1, // agent (Signer)
|
|
139
|
+
allowsSponsoredFeePayer: false,
|
|
140
|
+
},
|
|
141
|
+
revoke_identity: {
|
|
142
|
+
name: "revoke_identity",
|
|
143
|
+
minAccounts: 2,
|
|
144
|
+
taskSlot: null,
|
|
145
|
+
authoritySlot: 1, // agent (Signer)
|
|
146
|
+
allowsSponsoredFeePayer: false,
|
|
147
|
+
},
|
|
148
|
+
close_agent_state: {
|
|
149
|
+
name: "close_agent_state",
|
|
150
|
+
minAccounts: 2,
|
|
151
|
+
taskSlot: null,
|
|
152
|
+
authoritySlot: 1, // agent (Signer)
|
|
153
|
+
allowsSponsoredFeePayer: false,
|
|
154
|
+
},
|
|
155
|
+
migrate_agent_state: {
|
|
156
|
+
name: "migrate_agent_state",
|
|
157
|
+
minAccounts: 2,
|
|
158
|
+
taskSlot: null,
|
|
159
|
+
authoritySlot: 0, // agent (Signer)
|
|
160
|
+
allowsSponsoredFeePayer: false,
|
|
161
|
+
},
|
|
162
|
+
migrate_task: {
|
|
163
|
+
name: "migrate_task",
|
|
164
|
+
minAccounts: 2,
|
|
165
|
+
taskSlot: 0,
|
|
166
|
+
authoritySlot: 1, // payer (Signer)
|
|
167
|
+
allowsSponsoredFeePayer: false,
|
|
168
|
+
},
|
|
169
|
+
initialize: {
|
|
170
|
+
name: "initialize",
|
|
171
|
+
minAccounts: 4,
|
|
172
|
+
taskSlot: null,
|
|
173
|
+
authoritySlot: 1, // authority (Signer)
|
|
174
|
+
allowsSponsoredFeePayer: false,
|
|
175
|
+
},
|
|
176
|
+
update_params: {
|
|
177
|
+
name: "update_params",
|
|
178
|
+
minAccounts: 2,
|
|
179
|
+
taskSlot: null,
|
|
180
|
+
authoritySlot: 1, // authority (Signer)
|
|
181
|
+
allowsSponsoredFeePayer: false,
|
|
182
|
+
},
|
|
183
|
+
transfer_authority: {
|
|
184
|
+
name: "transfer_authority",
|
|
185
|
+
minAccounts: 2,
|
|
186
|
+
taskSlot: null,
|
|
187
|
+
authoritySlot: 1, // authority (Signer)
|
|
188
|
+
allowsSponsoredFeePayer: false,
|
|
189
|
+
},
|
|
190
|
+
update_treasury: {
|
|
191
|
+
name: "update_treasury",
|
|
192
|
+
minAccounts: 2,
|
|
193
|
+
taskSlot: null,
|
|
194
|
+
authoritySlot: 1, // authority (Signer)
|
|
195
|
+
allowsSponsoredFeePayer: false,
|
|
196
|
+
},
|
|
197
|
+
update_oracle_authority: {
|
|
198
|
+
name: "update_oracle_authority",
|
|
199
|
+
minAccounts: 2,
|
|
200
|
+
taskSlot: null,
|
|
201
|
+
authoritySlot: 1, // authority (Signer)
|
|
202
|
+
allowsSponsoredFeePayer: false,
|
|
203
|
+
},
|
|
204
|
+
emergency_return: {
|
|
205
|
+
name: "emergency_return",
|
|
206
|
+
minAccounts: 2,
|
|
207
|
+
taskSlot: null,
|
|
208
|
+
authoritySlot: 1, // authority (Signer)
|
|
209
|
+
allowsSponsoredFeePayer: false,
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
const DISCRIMINATOR_TO_SPEC = (() => {
|
|
213
|
+
const map = new Map();
|
|
214
|
+
for (const spec of Object.values(SHILLBOT_INSTRUCTION_SPECS)) {
|
|
215
|
+
const discHex = shillbotInstructionDiscriminator(spec.name).toString("hex");
|
|
216
|
+
map.set(discHex, spec);
|
|
217
|
+
}
|
|
218
|
+
return map;
|
|
219
|
+
})();
|
|
220
|
+
function txMismatch(operation, message) {
|
|
221
|
+
return new SwarmClientError({
|
|
222
|
+
code: "TRANSACTION_MISMATCH",
|
|
223
|
+
operation,
|
|
224
|
+
message,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
function invalidArg(operation, message) {
|
|
228
|
+
return new SwarmClientError({
|
|
229
|
+
code: "INVALID_ARGUMENT",
|
|
230
|
+
operation,
|
|
231
|
+
message,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Validate a single `ComputeBudget111111111111111111111111111111` instruction's tag,
|
|
236
|
+
* byte length, and value bounds. Never early-returns from a multi-instruction loop.
|
|
237
|
+
*/
|
|
238
|
+
export function validateComputeBudgetInstruction(data, maxComputeUnitPriceMicroLamports = MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS, operation = "shillbot.verifyBuiltTransaction") {
|
|
239
|
+
const buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
240
|
+
if (buf.length === 0) {
|
|
241
|
+
throw txMismatch(operation, "Empty ComputeBudget instruction data");
|
|
242
|
+
}
|
|
243
|
+
const tag = buf[0];
|
|
244
|
+
if (tag === 1) {
|
|
245
|
+
// RequestHeapFrame(u32 bytes)
|
|
246
|
+
if (buf.length !== 5) {
|
|
247
|
+
throw txMismatch(operation, `Malformed ComputeBudget RequestHeapFrame length: expected 5 bytes, got ${buf.length}`);
|
|
248
|
+
}
|
|
249
|
+
const heapBytes = buf.readUInt32LE(1);
|
|
250
|
+
if (heapBytes < MIN_HEAP_FRAME_BYTES ||
|
|
251
|
+
heapBytes > MAX_HEAP_FRAME_BYTES ||
|
|
252
|
+
heapBytes % 1024 !== 0) {
|
|
253
|
+
throw txMismatch(operation, `ComputeBudget RequestHeapFrame ${heapBytes} out of valid range (${MIN_HEAP_FRAME_BYTES}..${MAX_HEAP_FRAME_BYTES}, multiple of 1024)`);
|
|
254
|
+
}
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (tag === 2) {
|
|
258
|
+
// SetComputeUnitLimit(u32 units)
|
|
259
|
+
if (buf.length !== 5) {
|
|
260
|
+
throw txMismatch(operation, `Malformed ComputeBudget SetComputeUnitLimit length: expected 5 bytes, got ${buf.length}`);
|
|
261
|
+
}
|
|
262
|
+
const units = buf.readUInt32LE(1);
|
|
263
|
+
if (units < 1 || units > MAX_COMPUTE_UNIT_LIMIT) {
|
|
264
|
+
throw txMismatch(operation, `ComputeBudget SetComputeUnitLimit ${units} out of valid range (1..${MAX_COMPUTE_UNIT_LIMIT})`);
|
|
265
|
+
}
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (tag === 3) {
|
|
269
|
+
// SetComputeUnitPrice(u64 microLamports)
|
|
270
|
+
if (buf.length !== 9) {
|
|
271
|
+
throw txMismatch(operation, `Malformed ComputeBudget SetComputeUnitPrice length: expected 9 bytes, got ${buf.length}`);
|
|
272
|
+
}
|
|
273
|
+
const view = new DataView(buf.buffer, buf.byteOffset + 1, 8);
|
|
274
|
+
const microLamports = view.getBigUint64(0, true);
|
|
275
|
+
if (microLamports > maxComputeUnitPriceMicroLamports) {
|
|
276
|
+
throw txMismatch(operation, `ComputeBudget SetComputeUnitPrice ${microLamports} micro-lamports/CU exceeds maximum allowed ${maxComputeUnitPriceMicroLamports}`);
|
|
277
|
+
}
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (tag === 4) {
|
|
281
|
+
// SetLoadedAccountsDataSizeLimit(u32 bytes)
|
|
282
|
+
if (buf.length !== 5) {
|
|
283
|
+
throw txMismatch(operation, `Malformed ComputeBudget SetLoadedAccountsDataSizeLimit length: expected 5 bytes, got ${buf.length}`);
|
|
284
|
+
}
|
|
285
|
+
const bytesLimit = buf.readUInt32LE(1);
|
|
286
|
+
if (bytesLimit === 0) {
|
|
287
|
+
throw txMismatch(operation, "ComputeBudget SetLoadedAccountsDataSizeLimit cannot be zero");
|
|
288
|
+
}
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
throw txMismatch(operation, `Unsupported or deprecated ComputeBudget instruction tag ${tag}`);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Validate every `ComputeBudget` instruction in an inspected transaction without
|
|
295
|
+
* short-circuiting on `RequestHeapFrame` (tag 1) or any preceding instruction.
|
|
296
|
+
*/
|
|
297
|
+
export function validateComputeBudgetInstructions(instructions, maxComputeUnitPriceMicroLamports = MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS, operation = "shillbot.verifyBuiltTransaction") {
|
|
298
|
+
const computeBudgetId = ComputeBudgetProgram.programId.toBase58();
|
|
299
|
+
const seenTags = new Set();
|
|
300
|
+
for (const ix of instructions) {
|
|
301
|
+
if (ix.program_id !== computeBudgetId) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
const data = Buffer.from(ix.data_base64, "base64");
|
|
305
|
+
if (data.length === 0) {
|
|
306
|
+
throw txMismatch(operation, "Empty ComputeBudget instruction data");
|
|
307
|
+
}
|
|
308
|
+
const tag = data[0];
|
|
309
|
+
if (seenTags.has(tag)) {
|
|
310
|
+
throw txMismatch(operation, `Duplicate ComputeBudget instruction tag ${tag}`);
|
|
311
|
+
}
|
|
312
|
+
seenTags.add(tag);
|
|
313
|
+
validateComputeBudgetInstruction(data, maxComputeUnitPriceMicroLamports, operation);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function allowedInstructionNamesForAction(action) {
|
|
317
|
+
switch (action) {
|
|
318
|
+
case "create":
|
|
319
|
+
return ["create_task"];
|
|
320
|
+
case "claim":
|
|
321
|
+
return ["claim_task", "claim_task_session"];
|
|
322
|
+
case "submit":
|
|
323
|
+
return ["submit_work", "submit_work_session"];
|
|
324
|
+
case "approve":
|
|
325
|
+
return ["approve_task"];
|
|
326
|
+
case "verify":
|
|
327
|
+
return ["verify_task", "verify_task_attested"];
|
|
328
|
+
case "finalize":
|
|
329
|
+
return ["finalize_task"];
|
|
330
|
+
default:
|
|
331
|
+
return [];
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function parseValidatedPubkey(value, fieldName, operation) {
|
|
335
|
+
try {
|
|
336
|
+
const pk = new PublicKey(value);
|
|
337
|
+
if (pk.toBase58() !== value) {
|
|
338
|
+
throw new Error("non-canonical base58");
|
|
339
|
+
}
|
|
340
|
+
return pk;
|
|
341
|
+
}
|
|
342
|
+
catch {
|
|
343
|
+
throw invalidArg(operation, `Invalid ${fieldName} public key: ${value}`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
function validateSetPayoutToInstruction(decoded, expectedWallet, expectedTaskPda, expectedPayoutPubkey, operation) {
|
|
347
|
+
if (!expectedPayoutPubkey) {
|
|
348
|
+
throw txMismatch(operation, "Unexpected set_payout_to instruction: caller did not authorize expected.payoutTo");
|
|
349
|
+
}
|
|
350
|
+
const payoutB58 = expectedPayoutPubkey.toBase58();
|
|
351
|
+
if (payoutB58 === PublicKey.default.toBase58()) {
|
|
352
|
+
throw txMismatch(operation, "set_payout_to destination cannot be the default zero pubkey");
|
|
353
|
+
}
|
|
354
|
+
if (payoutB58 === expectedTaskPda) {
|
|
355
|
+
throw txMismatch(operation, "set_payout_to destination cannot equal the task PDA");
|
|
356
|
+
}
|
|
357
|
+
if (decoded.ix.accounts.length !== 2) {
|
|
358
|
+
throw txMismatch(operation, `set_payout_to must have exact 2 accounts [task, agent], got ${decoded.ix.accounts.length}`);
|
|
359
|
+
}
|
|
360
|
+
if (decoded.ix.accounts[0] !== expectedTaskPda) {
|
|
361
|
+
throw txMismatch(operation, "set_payout_to account [0] does not match expected task PDA");
|
|
362
|
+
}
|
|
363
|
+
if (decoded.ix.accounts[1] !== expectedWallet) {
|
|
364
|
+
throw txMismatch(operation, "set_payout_to authority slot [1] does not match expected agent wallet");
|
|
365
|
+
}
|
|
366
|
+
if (decoded.data.length !== 40) {
|
|
367
|
+
throw txMismatch(operation, `set_payout_to data must be exact 40 bytes (8-byte discriminator + 32-byte pubkey), got ${decoded.data.length}`);
|
|
368
|
+
}
|
|
369
|
+
const actualPayoutBytes = decoded.data.subarray(8, 40);
|
|
370
|
+
const expectedPayoutBytes = Buffer.from(expectedPayoutPubkey.toBytes());
|
|
371
|
+
if (!actualPayoutBytes.equals(expectedPayoutBytes)) {
|
|
372
|
+
throw txMismatch(operation, `set_payout_to destination pubkey ${new PublicKey(actualPayoutBytes).toBase58()} differs from expected.payoutTo ${payoutB58}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function verifyInstructionAuthorityAndShape(decoded, inspected, expected, operation) {
|
|
376
|
+
const { spec, ix, data } = decoded;
|
|
377
|
+
if (ix.accounts.length < spec.minAccounts) {
|
|
378
|
+
throw txMismatch(operation, `Instruction ${spec.name} requires at least ${spec.minAccounts} accounts, got ${ix.accounts.length}`);
|
|
379
|
+
}
|
|
380
|
+
if (spec.exactAccounts !== undefined && ix.accounts.length !== spec.exactAccounts) {
|
|
381
|
+
throw txMismatch(operation, `Instruction ${spec.name} requires exact ${spec.exactAccounts} accounts, got ${ix.accounts.length}`);
|
|
382
|
+
}
|
|
383
|
+
const expectedFeePayer = expected.feePayer ?? expected.wallet;
|
|
384
|
+
if (!spec.allowsSponsoredFeePayer && inspected.fee_payer !== expectedFeePayer) {
|
|
385
|
+
throw txMismatch(operation, `Instruction ${spec.name} requires fee_payer ${expectedFeePayer}, got ${inspected.fee_payer}`);
|
|
386
|
+
}
|
|
387
|
+
if (typeof spec.authoritySlot === "number") {
|
|
388
|
+
const slotAccount = ix.accounts[spec.authoritySlot];
|
|
389
|
+
if (slotAccount !== expected.wallet) {
|
|
390
|
+
throw txMismatch(operation, `Instruction ${spec.name} authority slot [${spec.authoritySlot}] is ${slotAccount}, expected ${expected.wallet}`);
|
|
391
|
+
}
|
|
392
|
+
if (!inspected.signers.includes(slotAccount)) {
|
|
393
|
+
throw txMismatch(operation, `Instruction ${spec.name} authority slot [${spec.authoritySlot}] (${slotAccount}) is not a required signer`);
|
|
394
|
+
}
|
|
395
|
+
// Session instructions require payer at [5] (claim_task_session) to also be a signer
|
|
396
|
+
if (spec.name === "claim_task_session" && !inspected.signers.includes(ix.accounts[5])) {
|
|
397
|
+
throw txMismatch(operation, `Instruction claim_task_session payer slot [5] (${ix.accounts[5]}) is not a required signer`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
else if (spec.authoritySlot === "fee_payer") {
|
|
401
|
+
if (inspected.fee_payer !== expectedFeePayer) {
|
|
402
|
+
throw txMismatch(operation, `Instruction ${spec.name} requires fee_payer ${expectedFeePayer}, got ${inspected.fee_payer}`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
else if (spec.authoritySlot === "verify_task") {
|
|
406
|
+
if (data.length !== 48) {
|
|
407
|
+
throw txMismatch(operation, `Instruction verify_task data must be 48 bytes, got ${data.length}`);
|
|
408
|
+
}
|
|
409
|
+
if (expected.oracleAuthority && ix.accounts[2] !== expected.oracleAuthority) {
|
|
410
|
+
throw txMismatch(operation, `verify_task switchboard_feed [2] (${ix.accounts[2]}) differs from expected ${expected.oracleAuthority}`);
|
|
411
|
+
}
|
|
412
|
+
if (inspected.fee_payer !== expectedFeePayer) {
|
|
413
|
+
throw txMismatch(operation, `Instruction verify_task requires fee_payer ${expectedFeePayer}, got ${inspected.fee_payer}`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else if (spec.authoritySlot === "verify_task_attested") {
|
|
417
|
+
if (data.length !== 48) {
|
|
418
|
+
throw txMismatch(operation, `Instruction verify_task_attested data must be 48 bytes, got ${data.length}`);
|
|
419
|
+
}
|
|
420
|
+
const attester = ix.accounts[2];
|
|
421
|
+
if (!inspected.signers.includes(attester)) {
|
|
422
|
+
throw txMismatch(operation, `Instruction verify_task_attested attester slot [2] (${attester}) must be a required signer`);
|
|
423
|
+
}
|
|
424
|
+
if (expected.oracleAuthority && attester !== expected.oracleAuthority) {
|
|
425
|
+
throw txMismatch(operation, `Instruction verify_task_attested attester slot [2] (${attester}) differs from expected oracleAuthority ${expected.oracleAuthority}`);
|
|
426
|
+
}
|
|
427
|
+
const walletIsAttester = attester === expected.wallet;
|
|
428
|
+
const walletIsExpectedFeePayer = inspected.fee_payer === expectedFeePayer && expected.wallet === expectedFeePayer;
|
|
429
|
+
if (!walletIsAttester && !walletIsExpectedFeePayer) {
|
|
430
|
+
throw txMismatch(operation, `Instruction verify_task_attested requires wallet ${expected.wallet} to occupy attester slot [2] or fee_payer`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Strict fail-closed local validator for built or server-prepared Shillbot Solana transactions.
|
|
436
|
+
*
|
|
437
|
+
* Enforces:
|
|
438
|
+
* - Allowed program IDs (Shillbot + ComputeBudget, plus Switchboard + Secp256k1 only on `verify`).
|
|
439
|
+
* - Full loop validation of all `ComputeBudget` instructions (no early return on `RequestHeapFrame`),
|
|
440
|
+
* capping `SetComputeUnitPrice <= MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS` (`100_000` micro-lamports/CU).
|
|
441
|
+
* - Exact Anchor account-index positions for `task` PDA and authority signer slots per discriminator.
|
|
442
|
+
* - Explicit `set_payout_to` opt-in guard: rejects any `set_payout_to` instruction unless
|
|
443
|
+
* `expected.payoutTo` is explicitly provided AND matches `ix.data[8..40]` and `accounts == [task, wallet]`.
|
|
444
|
+
*/
|
|
445
|
+
export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.verifyBuiltTransaction") {
|
|
446
|
+
if (expected.network !== "mainnet" && expected.network !== "devnet") {
|
|
447
|
+
throw invalidArg(operation, "Unsupported network");
|
|
448
|
+
}
|
|
449
|
+
if (!expected.action && !expected.instructionName) {
|
|
450
|
+
throw invalidArg(operation, "Either action or instructionName is required");
|
|
451
|
+
}
|
|
452
|
+
parseValidatedPubkey(expected.wallet, "wallet", operation);
|
|
453
|
+
if (expected.taskPda !== undefined) {
|
|
454
|
+
parseValidatedPubkey(expected.taskPda, "taskPda", operation);
|
|
455
|
+
}
|
|
456
|
+
if (expected.feePayer !== undefined) {
|
|
457
|
+
parseValidatedPubkey(expected.feePayer, "feePayer", operation);
|
|
458
|
+
}
|
|
459
|
+
if (expected.oracleAuthority !== undefined) {
|
|
460
|
+
parseValidatedPubkey(expected.oracleAuthority, "oracleAuthority", operation);
|
|
461
|
+
}
|
|
462
|
+
const expectedPayoutPubkey = expected.payoutTo !== undefined
|
|
463
|
+
? parseValidatedPubkey(expected.payoutTo, "payoutTo", operation)
|
|
464
|
+
: undefined;
|
|
465
|
+
if (expectedPayoutPubkey &&
|
|
466
|
+
expectedPayoutPubkey.toBase58() === PublicKey.default.toBase58()) {
|
|
467
|
+
throw invalidArg(operation, "expected.payoutTo cannot be the default zero pubkey");
|
|
468
|
+
}
|
|
469
|
+
let inspected;
|
|
470
|
+
try {
|
|
471
|
+
inspected = inspectTransaction(encoded);
|
|
472
|
+
}
|
|
473
|
+
catch (err) {
|
|
474
|
+
throw txMismatch(operation, err instanceof Error ? err.message : `Malformed transaction: ${String(err)}`);
|
|
475
|
+
}
|
|
476
|
+
if (!inspected.signers.includes(expected.wallet)) {
|
|
477
|
+
throw txMismatch(operation, "Wallet is not a required signer");
|
|
478
|
+
}
|
|
479
|
+
if (expected.feePayer && inspected.fee_payer !== expected.feePayer) {
|
|
480
|
+
throw txMismatch(operation, "Fee payer differs from expectation");
|
|
481
|
+
}
|
|
482
|
+
const isVerifyContext = expected.action === "verify" ||
|
|
483
|
+
expected.instructionName === "verify_task" ||
|
|
484
|
+
expected.instructionName === "verify_task_attested";
|
|
485
|
+
const shillbotId = SHILLBOT_PROGRAM_ID.toBase58();
|
|
486
|
+
const alwaysAllowed = new Set([
|
|
487
|
+
shillbotId,
|
|
488
|
+
ComputeBudgetProgram.programId.toBase58(),
|
|
489
|
+
...(isVerifyContext
|
|
490
|
+
? [SWITCHBOARD_PROGRAM_IDS[expected.network], Secp256k1Program.programId.toBase58()]
|
|
491
|
+
: []),
|
|
492
|
+
...(expected.allowedProgramIds ?? []),
|
|
493
|
+
]);
|
|
494
|
+
const unrelated = inspected.instructions.find((instruction) => !alwaysAllowed.has(instruction.program_id));
|
|
495
|
+
if (unrelated) {
|
|
496
|
+
throw txMismatch(operation, `Unexpected program ${unrelated.program_id}`);
|
|
497
|
+
}
|
|
498
|
+
validateComputeBudgetInstructions(inspected.instructions, expected.maxComputeUnitPriceMicroLamports ?? MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS, operation);
|
|
499
|
+
const decodedShillbotIxs = inspected.instructions.flatMap((ix, txIndex) => {
|
|
500
|
+
if (ix.program_id !== shillbotId)
|
|
501
|
+
return [];
|
|
502
|
+
const data = Buffer.from(ix.data_base64, "base64");
|
|
503
|
+
if (data.length < 8) {
|
|
504
|
+
throw txMismatch(operation, "Shillbot instruction is shorter than 8-byte discriminator");
|
|
505
|
+
}
|
|
506
|
+
const spec = DISCRIMINATOR_TO_SPEC.get(data.subarray(0, 8).toString("hex"));
|
|
507
|
+
if (!spec) {
|
|
508
|
+
throw txMismatch(operation, "Unknown Shillbot instruction discriminator");
|
|
509
|
+
}
|
|
510
|
+
return [{ ix, txIndex, data, spec }];
|
|
511
|
+
});
|
|
512
|
+
const targetNames = expected.instructionName
|
|
513
|
+
? [expected.instructionName]
|
|
514
|
+
: allowedInstructionNamesForAction(expected.action);
|
|
515
|
+
const lifecycleMatches = decodedShillbotIxs.filter((d) => targetNames.includes(d.spec.name));
|
|
516
|
+
if (lifecycleMatches.length !== 1) {
|
|
517
|
+
throw txMismatch(operation, "Expected exactly one matching Shillbot lifecycle instruction");
|
|
518
|
+
}
|
|
519
|
+
const primary = lifecycleMatches[0];
|
|
520
|
+
const companions = decodedShillbotIxs.filter((d) => d !== primary);
|
|
521
|
+
// Check task PDA before or alongside authority check
|
|
522
|
+
if (primary.spec.taskSlot !== null) {
|
|
523
|
+
if (primary.ix.accounts.length <= primary.spec.taskSlot) {
|
|
524
|
+
throw txMismatch(operation, `Instruction ${primary.spec.name} is missing task account`);
|
|
525
|
+
}
|
|
526
|
+
const actualTaskPda = primary.ix.accounts[primary.spec.taskSlot];
|
|
527
|
+
if (expected.taskPda && actualTaskPda !== expected.taskPda) {
|
|
528
|
+
throw txMismatch(operation, "Task PDA differs from expectation");
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
verifyInstructionAuthorityAndShape(primary, inspected, expected, operation);
|
|
532
|
+
// Instruction-specific payload validation
|
|
533
|
+
if (primary.spec.name === "create_task") {
|
|
534
|
+
if (primary.data.length < 24) {
|
|
535
|
+
throw txMismatch(operation, "Create instruction is truncated");
|
|
536
|
+
}
|
|
537
|
+
if (expected.escrowLamports !== undefined) {
|
|
538
|
+
const view = new DataView(primary.data.buffer, primary.data.byteOffset + 16, 8);
|
|
539
|
+
const amount = view.getBigUint64(0, true);
|
|
540
|
+
if (amount !== expected.escrowLamports) {
|
|
541
|
+
throw txMismatch(operation, `Escrow amount ${amount} differs from expected ${expected.escrowLamports}`);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
else if (primary.spec.name === "submit_work" ||
|
|
546
|
+
primary.spec.name === "submit_work_session") {
|
|
547
|
+
if (primary.data.length < 12) {
|
|
548
|
+
throw txMismatch(operation, "Submit instruction is truncated");
|
|
549
|
+
}
|
|
550
|
+
const contentLen = primary.data.readUInt32LE(8);
|
|
551
|
+
if (primary.data.length !== 12 + contentLen) {
|
|
552
|
+
throw txMismatch(operation, "Submit instruction content_id length mismatch");
|
|
553
|
+
}
|
|
554
|
+
if (expected.contentId !== undefined) {
|
|
555
|
+
const actualContent = primary.data.subarray(12, 12 + contentLen).toString("utf8");
|
|
556
|
+
if (actualContent !== expected.contentId) {
|
|
557
|
+
throw txMismatch(operation, "Submit instruction content_id differs from expectation");
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
else if (primary.spec.name === "set_payout_to") {
|
|
562
|
+
validateSetPayoutToInstruction(primary, expected.wallet, primary.ix.accounts[0], expectedPayoutPubkey, operation);
|
|
563
|
+
}
|
|
564
|
+
else if (primary.spec.name === "finalize_task" && expectedPayoutPubkey) {
|
|
565
|
+
if (primary.ix.accounts[2] !== expectedPayoutPubkey.toBase58()) {
|
|
566
|
+
throw txMismatch(operation, `finalize_task payout recipient slot [2] (${primary.ix.accounts[2]}) differs from expected.payoutTo ${expectedPayoutPubkey.toBase58()}`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
// Validate companion Shillbot instructions (strictly fail-closed)
|
|
570
|
+
const validateClaimCompanion = (claimIx) => {
|
|
571
|
+
if (primary.spec.name === "submit_work") {
|
|
572
|
+
return (claimIx.spec.name === "claim_task" &&
|
|
573
|
+
claimIx.ix.accounts.length >= SHILLBOT_INSTRUCTION_SPECS.claim_task.minAccounts &&
|
|
574
|
+
claimIx.ix.accounts[0] === primary.ix.accounts[0] &&
|
|
575
|
+
claimIx.ix.accounts[3] === expected.wallet);
|
|
576
|
+
}
|
|
577
|
+
if (primary.spec.name === "submit_work_session") {
|
|
578
|
+
return (claimIx.spec.name === "claim_task_session" &&
|
|
579
|
+
claimIx.ix.accounts.length >= SHILLBOT_INSTRUCTION_SPECS.claim_task_session.minAccounts &&
|
|
580
|
+
claimIx.ix.accounts[0] === primary.ix.accounts[0] &&
|
|
581
|
+
claimIx.ix.accounts[3] === primary.ix.accounts[3] &&
|
|
582
|
+
claimIx.ix.accounts[4] === expected.wallet &&
|
|
583
|
+
inspected.signers.includes(claimIx.ix.accounts[5]));
|
|
584
|
+
}
|
|
585
|
+
return false;
|
|
586
|
+
};
|
|
587
|
+
const isClaimLifecycle = expected.action === "claim" ||
|
|
588
|
+
primary.spec.name === "claim_task" ||
|
|
589
|
+
primary.spec.name === "claim_task_session";
|
|
590
|
+
const isSubmitLifecycle = expected.action === "submit" ||
|
|
591
|
+
primary.spec.name === "submit_work" ||
|
|
592
|
+
primary.spec.name === "submit_work_session";
|
|
593
|
+
if (companions.length === 0) {
|
|
594
|
+
if (expectedPayoutPubkey &&
|
|
595
|
+
primary.spec.name !== "set_payout_to" &&
|
|
596
|
+
primary.spec.name !== "finalize_task") {
|
|
597
|
+
throw txMismatch(operation, "Expected set_payout_to instruction matching expected.payoutTo, but none was present");
|
|
598
|
+
}
|
|
599
|
+
return inspected;
|
|
600
|
+
}
|
|
601
|
+
if (isClaimLifecycle &&
|
|
602
|
+
companions.length === 1 &&
|
|
603
|
+
companions[0].spec.name === "set_payout_to") {
|
|
604
|
+
if (primary.spec.name !== "claim_task") {
|
|
605
|
+
throw txMismatch(operation, "set_payout_to companion requires direct agent claim_task");
|
|
606
|
+
}
|
|
607
|
+
if (companions[0].txIndex <= primary.txIndex) {
|
|
608
|
+
throw txMismatch(operation, "payout route must follow claim");
|
|
609
|
+
}
|
|
610
|
+
validateSetPayoutToInstruction(companions[0], expected.wallet, primary.ix.accounts[0], expectedPayoutPubkey, operation);
|
|
611
|
+
return inspected;
|
|
612
|
+
}
|
|
613
|
+
if (isSubmitLifecycle &&
|
|
614
|
+
companions.length === 1 &&
|
|
615
|
+
(companions[0].spec.name === "claim_task" ||
|
|
616
|
+
companions[0].spec.name === "claim_task_session")) {
|
|
617
|
+
if (expectedPayoutPubkey) {
|
|
618
|
+
throw txMismatch(operation, "Expected set_payout_to instruction matching expected.payoutTo, but none was present");
|
|
619
|
+
}
|
|
620
|
+
if (primary.txIndex !== companions[0].txIndex + 1 ||
|
|
621
|
+
!validateClaimCompanion(companions[0])) {
|
|
622
|
+
throw txMismatch(operation, "Expected exactly one matching Shillbot lifecycle instruction");
|
|
623
|
+
}
|
|
624
|
+
return inspected;
|
|
625
|
+
}
|
|
626
|
+
if (isSubmitLifecycle &&
|
|
627
|
+
companions.length === 2 &&
|
|
628
|
+
companions[0].spec.name === "claim_task" &&
|
|
629
|
+
companions[1].spec.name === "set_payout_to") {
|
|
630
|
+
if (primary.spec.name !== "submit_work" ||
|
|
631
|
+
companions[1].txIndex !== companions[0].txIndex + 1 ||
|
|
632
|
+
primary.txIndex !== companions[1].txIndex + 1 ||
|
|
633
|
+
!validateClaimCompanion(companions[0])) {
|
|
634
|
+
throw txMismatch(operation, "Expected exactly one matching Shillbot lifecycle instruction");
|
|
635
|
+
}
|
|
636
|
+
validateSetPayoutToInstruction(companions[1], expected.wallet, primary.ix.accounts[0], expectedPayoutPubkey, operation);
|
|
637
|
+
return inspected;
|
|
638
|
+
}
|
|
639
|
+
if (companions.some((c) => c.spec.name === "set_payout_to")) {
|
|
640
|
+
throw txMismatch(operation, "Unexpected set_payout_to companion instruction in transaction");
|
|
641
|
+
}
|
|
642
|
+
throw txMismatch(operation, "Expected exactly one matching Shillbot lifecycle instruction");
|
|
643
|
+
}
|