@swarm-tips/client 0.2.13 → 0.3.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/README.md +59 -18
- package/SBOM.spdx.json +5 -5
- package/dist/agent.d.ts +176 -0
- package/dist/agent.js +561 -0
- package/dist/auth/token-manager.d.ts +33 -0
- package/dist/auth/token-manager.js +175 -0
- package/dist/cli/keyfile.d.ts +24 -0
- package/dist/cli/keyfile.js +97 -0
- package/dist/cli/swarm.d.ts +14 -0
- package/dist/cli/swarm.js +516 -0
- package/dist/coordination-game/api.d.ts +4 -4
- package/dist/coordination-game/api.js +8 -8
- package/dist/errors.d.ts +1 -1
- package/dist/generated/openapi.d.ts +1963 -0
- package/dist/generated/openapi.js +8 -0
- package/dist/http.d.ts +6 -3
- package/dist/http.js +18 -4
- package/dist/inbox/client.d.ts +16 -1
- package/dist/inbox/client.js +48 -2
- package/dist/inbox/index.d.ts +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +7 -2
- package/dist/shillbot/api.d.ts +49 -2
- package/dist/shillbot/api.js +377 -33
- package/dist/signer/evm.d.ts +25 -0
- package/dist/signer/evm.js +249 -0
- package/dist/signer/index.d.ts +3 -0
- package/dist/signer/index.js +3 -0
- package/dist/signer/solana.d.ts +24 -0
- package/dist/signer/solana.js +183 -0
- package/dist/signer/types.d.ts +30 -0
- package/dist/signer/types.js +31 -0
- package/dist/swarm/index.d.ts +1 -0
- package/dist/swarm/index.js +9 -2
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/package.json +5 -3
package/dist/shillbot/api.js
CHANGED
|
@@ -1,37 +1,343 @@
|
|
|
1
1
|
import { Buffer } from "buffer";
|
|
2
|
-
import { ComputeBudgetProgram, Secp256k1Program } from "@solana/web3.js";
|
|
2
|
+
import { ComputeBudgetProgram, Secp256k1Program, } from "@solana/web3.js";
|
|
3
3
|
import { HttpClient } from "../http.js";
|
|
4
4
|
import { SwarmClientError } from "../errors.js";
|
|
5
5
|
import { SHILLBOT_PROGRAM_ID, inspectTransaction, shillbotInstructionDiscriminator, signAndBroadcast, } from "./index.js";
|
|
6
|
+
function decodeJwtWallet(token) {
|
|
7
|
+
if (!token)
|
|
8
|
+
return undefined;
|
|
9
|
+
const parts = token.split(".");
|
|
10
|
+
if (parts.length < 2)
|
|
11
|
+
return undefined;
|
|
12
|
+
try {
|
|
13
|
+
const b64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
14
|
+
const padded = b64 + "=".repeat((4 - (b64.length % 4)) % 4);
|
|
15
|
+
const decoded = JSON.parse(Buffer.from(padded, "base64").toString("utf8"));
|
|
16
|
+
if (typeof decoded.wallet === "string" && decoded.wallet)
|
|
17
|
+
return decoded.wallet;
|
|
18
|
+
if (typeof decoded.sub === "string" && decoded.sub)
|
|
19
|
+
return decoded.sub;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// Non-JWT opaque token; caller can supply opts.payer explicitly.
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
6
26
|
export class ShillbotApiClient {
|
|
7
27
|
http;
|
|
8
|
-
constructor(options) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.http = new HttpClient(options);
|
|
30
|
+
}
|
|
31
|
+
get(path) {
|
|
32
|
+
return this.http.get(path);
|
|
33
|
+
}
|
|
34
|
+
post(path, body) {
|
|
35
|
+
return this.http.post(path, body);
|
|
36
|
+
}
|
|
37
|
+
patch(path, body) {
|
|
38
|
+
return this.http.patch(path, body);
|
|
39
|
+
}
|
|
40
|
+
listTasks(limit = 50) {
|
|
41
|
+
return this.get(`/tasks?limit=${encodeURIComponent(String(limit))}`);
|
|
42
|
+
}
|
|
43
|
+
getTask(id) {
|
|
44
|
+
return this.get(`/tasks/${encodeURIComponent(id)}`);
|
|
45
|
+
}
|
|
46
|
+
listAgentTasks() {
|
|
47
|
+
return this.get("/agent/tasks");
|
|
48
|
+
}
|
|
49
|
+
listPendingApproval() {
|
|
50
|
+
return this.get("/tasks?status=pending_approval");
|
|
51
|
+
}
|
|
52
|
+
listCampaigns() {
|
|
53
|
+
return this.get("/campaigns");
|
|
54
|
+
}
|
|
55
|
+
campaign(id) {
|
|
56
|
+
return this.get(`/campaigns/${encodeURIComponent(id)}`);
|
|
57
|
+
}
|
|
58
|
+
campaignTasks(id) {
|
|
59
|
+
return this.get(`/campaigns/${encodeURIComponent(id)}/tasks`);
|
|
60
|
+
}
|
|
61
|
+
campaignMetrics(id) {
|
|
62
|
+
return this.get(`/campaigns/${encodeURIComponent(id)}/metrics`);
|
|
63
|
+
}
|
|
64
|
+
createCampaign(body) {
|
|
65
|
+
return this.post("/campaigns", body);
|
|
66
|
+
}
|
|
67
|
+
updateCampaign(id, action) {
|
|
68
|
+
return this.patch(`/campaigns/${encodeURIComponent(id)}`, { action });
|
|
69
|
+
}
|
|
70
|
+
fundCampaignTask(id, body) {
|
|
71
|
+
return this.post(`/campaigns/${encodeURIComponent(id)}/fund`, body);
|
|
72
|
+
}
|
|
73
|
+
onboard() {
|
|
74
|
+
return this.post("/agent/onboard");
|
|
75
|
+
}
|
|
76
|
+
earnings() {
|
|
77
|
+
return this.get("/agent/earnings");
|
|
78
|
+
}
|
|
79
|
+
claimTask(id, sponsor = "auto") {
|
|
80
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/claim?sponsor=${encodeURIComponent(sponsor)}`);
|
|
81
|
+
}
|
|
82
|
+
sponsorTask(id, body) {
|
|
83
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/sponsor`, body);
|
|
84
|
+
}
|
|
85
|
+
submitTask(id, body) {
|
|
86
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/submit`, body);
|
|
87
|
+
}
|
|
88
|
+
approveTask(id) {
|
|
89
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/approve`);
|
|
90
|
+
}
|
|
91
|
+
getVerificationData(id, network) {
|
|
92
|
+
const suffix = network && network !== "mainnet" ? `?network=${encodeURIComponent(network)}` : "";
|
|
93
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/build-verify${suffix}`);
|
|
94
|
+
}
|
|
95
|
+
async buildVerify(id, opts) {
|
|
96
|
+
const mcpBaseUrl = (opts?.mcpBaseUrl ?? "https://mcp.swarm.tips").replace(/\/$/, "");
|
|
97
|
+
const resolvedNetwork = opts?.network ?? (this.http.network === "devnet" ? "devnet" : "mainnet");
|
|
98
|
+
const token = await this.http.getToken?.();
|
|
99
|
+
const payer = opts?.payer ?? decodeJwtWallet(token);
|
|
100
|
+
const postMcpBuildVerify = async (payload) => {
|
|
101
|
+
const mcpHttp = new HttpClient({
|
|
102
|
+
baseUrl: mcpBaseUrl,
|
|
103
|
+
fetch: this.http.fetchImpl,
|
|
104
|
+
getToken: this.http.getToken,
|
|
105
|
+
});
|
|
106
|
+
return mcpHttp.post("/internal/build-verify-tx", payload);
|
|
107
|
+
};
|
|
108
|
+
const oneHopBody = {
|
|
109
|
+
task_id: id,
|
|
110
|
+
network: resolvedNetwork,
|
|
111
|
+
...(payer ? { payer } : {}),
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
const direct = await postMcpBuildVerify(oneHopBody);
|
|
115
|
+
return {
|
|
116
|
+
...direct,
|
|
117
|
+
task_id: direct.task_id ?? id,
|
|
118
|
+
transaction: direct.transaction,
|
|
119
|
+
task_pda: direct.task_pda,
|
|
120
|
+
transaction_intent: direct.transaction_intent,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
if (!(err instanceof SwarmClientError) ||
|
|
125
|
+
err.code !== "HTTP_ERROR" ||
|
|
126
|
+
(err.status !== 400 && err.status !== 422)) {
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
const vdata = await this.getVerificationData(id, resolvedNetwork);
|
|
130
|
+
const fallback = await postMcpBuildVerify({
|
|
131
|
+
task_id: id,
|
|
132
|
+
...(payer ? { payer } : {}),
|
|
133
|
+
network: resolvedNetwork,
|
|
134
|
+
task_pda: vdata.task_pda,
|
|
135
|
+
score: vdata.composite_score,
|
|
136
|
+
hash: vdata.verification_hash,
|
|
137
|
+
feed: vdata.switchboard_feed,
|
|
138
|
+
...(vdata.global_state ? { global_state: vdata.global_state } : {}),
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
...fallback,
|
|
142
|
+
task_id: fallback.task_id ?? id,
|
|
143
|
+
transaction: fallback.transaction,
|
|
144
|
+
task_pda: fallback.task_pda ?? vdata.task_pda,
|
|
145
|
+
transaction_intent: fallback.transaction_intent,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
buildFinalize(id) {
|
|
150
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/build-finalize`);
|
|
151
|
+
}
|
|
152
|
+
confirmTask(id, body) {
|
|
153
|
+
return this.post(`/tasks/${encodeURIComponent(id)}/confirm`, body);
|
|
154
|
+
}
|
|
30
155
|
}
|
|
31
156
|
const SWITCHBOARD_PROGRAM_IDS = {
|
|
32
157
|
mainnet: "SBondMDrcV3K4kxZR1HNVT7osZxAHVHgYXL5Ze1oMUv",
|
|
33
158
|
devnet: "Aio4gaXjXzJNVLtzwtNVmSqGKpANtXhybbkhtAC94ji2",
|
|
34
159
|
};
|
|
160
|
+
/** Canonical deployed ShillbotEscrow EVM contracts keyed by CAIP-2 chain ID. */
|
|
161
|
+
export const SHILLBOT_ESCROW_DEPLOYMENTS = {
|
|
162
|
+
"eip155:84532": "0xaFe061778f9A76fCe7da4124dC89DAF8309E5F3c",
|
|
163
|
+
};
|
|
164
|
+
/** Canonical 4-byte function selectors for `ShillbotEscrow.sol` lifecycle actions. */
|
|
165
|
+
export const SHILLBOT_EVM_SELECTORS = {
|
|
166
|
+
create: "0xa4aaf1b3", // createTask(bytes32,bytes32,uint8,uint64,bool)
|
|
167
|
+
claim: "0xe6a81fdd", // claimTask(uint64)
|
|
168
|
+
submit: "0xfb78a429", // submitWork(uint64,bytes32,bytes32)
|
|
169
|
+
approve: "0x97312249", // approveTask(uint64)
|
|
170
|
+
verify: "0x3bae608b", // verifyTaskAttested(uint64,uint64,bytes)
|
|
171
|
+
finalize: "0xcb26e65b", // finalizeTask(uint64)
|
|
172
|
+
};
|
|
173
|
+
function normalizeEvmChainId(chain) {
|
|
174
|
+
const trimmed = chain.trim().toLowerCase();
|
|
175
|
+
if (trimmed === "base-sepolia" || trimmed === "84532")
|
|
176
|
+
return "eip155:84532";
|
|
177
|
+
if (trimmed === "base" || trimmed === "8453")
|
|
178
|
+
return "eip155:8453";
|
|
179
|
+
if (trimmed === "ethereum" || trimmed === "1")
|
|
180
|
+
return "eip155:1";
|
|
181
|
+
if (trimmed === "ethereum-sepolia" || trimmed === "sepolia" || trimmed === "11155111") {
|
|
182
|
+
return "eip155:11155111";
|
|
183
|
+
}
|
|
184
|
+
return trimmed;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* C10 fail-closed local validator for server-prepared EVM `ShillbotEscrow` calls.
|
|
188
|
+
* Verifies canonical contract address for `chain`, 4-byte function selector for
|
|
189
|
+
* `action`, and `value_wei <= maxValueWei` (strictly `0n` on non-create actions).
|
|
190
|
+
*/
|
|
191
|
+
export function validatePreparedEvmCall(evmCall, expected) {
|
|
192
|
+
if (!evmCall || typeof evmCall !== "object") {
|
|
193
|
+
throw new SwarmClientError({
|
|
194
|
+
code: "TRANSACTION_MISMATCH",
|
|
195
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
196
|
+
message: "Prepared EVM call must be an object",
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
const raw = evmCall;
|
|
200
|
+
if (typeof raw.to !== "string" || !/^0x[0-9a-fA-F]{40}$/.test(raw.to)) {
|
|
201
|
+
throw new SwarmClientError({
|
|
202
|
+
code: "TRANSACTION_MISMATCH",
|
|
203
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
204
|
+
message: "Prepared EVM call has an invalid target contract address",
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
if (typeof raw.data !== "string" ||
|
|
208
|
+
!/^0x[0-9a-fA-F]{8,}$/.test(raw.data) ||
|
|
209
|
+
raw.data.length % 2 !== 0) {
|
|
210
|
+
throw new SwarmClientError({
|
|
211
|
+
code: "TRANSACTION_MISMATCH",
|
|
212
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
213
|
+
message: "Prepared EVM call data must include at least a 4-byte function selector",
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
const callChain = typeof raw.chain === "string"
|
|
217
|
+
? normalizeEvmChainId(raw.chain)
|
|
218
|
+
: typeof raw.chain_id === "number"
|
|
219
|
+
? `eip155:${raw.chain_id}`
|
|
220
|
+
: undefined;
|
|
221
|
+
const expectedChain = expected.chain ? normalizeEvmChainId(expected.chain) : undefined;
|
|
222
|
+
if (callChain && expectedChain && callChain !== expectedChain) {
|
|
223
|
+
throw new SwarmClientError({
|
|
224
|
+
code: "TRANSACTION_MISMATCH",
|
|
225
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
226
|
+
message: `Prepared EVM call chain ${callChain} differs from expected ${expectedChain}`,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
const resolvedChain = expectedChain ?? callChain ?? "eip155:84532";
|
|
230
|
+
const canonicalContract = SHILLBOT_ESCROW_DEPLOYMENTS[resolvedChain];
|
|
231
|
+
if (!canonicalContract) {
|
|
232
|
+
throw new SwarmClientError({
|
|
233
|
+
code: "TRANSACTION_MISMATCH",
|
|
234
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
235
|
+
message: `No canonical ShillbotEscrow contract deployed for chain ${resolvedChain}`,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
if (raw.to.toLowerCase() !== canonicalContract.toLowerCase()) {
|
|
239
|
+
throw new SwarmClientError({
|
|
240
|
+
code: "TRANSACTION_MISMATCH",
|
|
241
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
242
|
+
message: `Target contract ${raw.to} does not match canonical ShillbotEscrow ${canonicalContract}`,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const expectedSelector = SHILLBOT_EVM_SELECTORS[expected.action];
|
|
246
|
+
if (!expectedSelector) {
|
|
247
|
+
throw new SwarmClientError({
|
|
248
|
+
code: "INVALID_ARGUMENT",
|
|
249
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
250
|
+
message: `Unsupported Shillbot action: ${String(expected.action)}`,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
const actualSelector = raw.data.slice(0, 10).toLowerCase();
|
|
254
|
+
if (actualSelector !== expectedSelector.toLowerCase()) {
|
|
255
|
+
throw new SwarmClientError({
|
|
256
|
+
code: "TRANSACTION_MISMATCH",
|
|
257
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
258
|
+
message: `Function selector ${actualSelector} does not match expected ${expectedSelector} for ${expected.action}`,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
let valueWei;
|
|
262
|
+
try {
|
|
263
|
+
const rawVal = raw.value_wei ?? raw.value ?? "0";
|
|
264
|
+
valueWei = typeof rawVal === "bigint" ? rawVal : BigInt(String(rawVal));
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
throw new SwarmClientError({
|
|
268
|
+
code: "TRANSACTION_MISMATCH",
|
|
269
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
270
|
+
message: "Prepared EVM call value_wei is not a valid integer",
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
if (valueWei < 0n) {
|
|
274
|
+
throw new SwarmClientError({
|
|
275
|
+
code: "TRANSACTION_MISMATCH",
|
|
276
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
277
|
+
message: "Prepared EVM call value_wei cannot be negative",
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
if (expected.action !== "create" && valueWei !== 0n) {
|
|
281
|
+
throw new SwarmClientError({
|
|
282
|
+
code: "TRANSACTION_MISMATCH",
|
|
283
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
284
|
+
message: `Non-create Shillbot EVM action (${expected.action}) must have 0 value_wei, got ${valueWei}`,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const maxAllowed = expected.action === "create" ? (expected.maxValueWei ?? 0n) : 0n;
|
|
288
|
+
if (valueWei > maxAllowed) {
|
|
289
|
+
throw new SwarmClientError({
|
|
290
|
+
code: "TRANSACTION_MISMATCH",
|
|
291
|
+
operation: "shillbot.validatePreparedEvmCall",
|
|
292
|
+
message: `Prepared EVM call value_wei ${valueWei} exceeds maximum allowed ${maxAllowed}`,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
to: canonicalContract,
|
|
297
|
+
data: raw.data,
|
|
298
|
+
value: valueWei,
|
|
299
|
+
chain: resolvedChain,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Estimate the maximum Solana network fee (base signature fees + ComputeBudget
|
|
304
|
+
* priority fee) charged to `wallet` for an inspected transaction. Returns `0n`
|
|
305
|
+
* when a third-party sponsor is the `fee_payer`.
|
|
306
|
+
*/
|
|
307
|
+
export function estimateSolanaNetworkFeeLamports(inspected, wallet) {
|
|
308
|
+
if (inspected.fee_payer !== wallet)
|
|
309
|
+
return 0n;
|
|
310
|
+
const computeBudgetId = ComputeBudgetProgram.programId.toBase58();
|
|
311
|
+
let computeUnitLimit;
|
|
312
|
+
let microLamportsPerCu = 0n;
|
|
313
|
+
let nonComputeBudgetIxCount = 0;
|
|
314
|
+
for (const ix of inspected.instructions) {
|
|
315
|
+
if (ix.program_id !== computeBudgetId) {
|
|
316
|
+
nonComputeBudgetIxCount += 1;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const data = Buffer.from(ix.data_base64, "base64");
|
|
320
|
+
if (data.length === 0)
|
|
321
|
+
continue;
|
|
322
|
+
const tag = data[0];
|
|
323
|
+
if (tag === 2 && data.length >= 5) {
|
|
324
|
+
computeUnitLimit = BigInt(data.readUInt32LE(1));
|
|
325
|
+
}
|
|
326
|
+
else if (tag === 3 && data.length >= 9) {
|
|
327
|
+
const view = new DataView(data.buffer, data.byteOffset + 1, 8);
|
|
328
|
+
microLamportsPerCu = view.getBigUint64(0, true);
|
|
329
|
+
}
|
|
330
|
+
else if (tag === 1 && data.length >= 9) {
|
|
331
|
+
// Legacy RequestUnitsDeprecated: u32 units + u32 additionalFeeLamports
|
|
332
|
+
const additionalFee = BigInt(data.readUInt32LE(5));
|
|
333
|
+
return BigInt(inspected.signers.length) * 5000n + additionalFee;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const effectiveUnits = computeUnitLimit ?? BigInt(Math.max(1, nonComputeBudgetIxCount)) * 200000n;
|
|
337
|
+
const priorityFeeLamports = (effectiveUnits * microLamportsPerCu + 999999n) / 1000000n;
|
|
338
|
+
const baseSignatureFeeLamports = BigInt(Math.max(1, inspected.signers.length)) * 5000n;
|
|
339
|
+
return baseSignatureFeeLamports + priorityFeeLamports;
|
|
340
|
+
}
|
|
35
341
|
export function permittedPreparedProgramIds(action, network) {
|
|
36
342
|
return action === "verify"
|
|
37
343
|
? [SWITCHBOARD_PROGRAM_IDS[network], Secp256k1Program.programId.toBase58()]
|
|
@@ -55,14 +361,26 @@ function matchesDiscriminator(dataBase64, action) {
|
|
|
55
361
|
*/
|
|
56
362
|
export function validatePreparedTransaction(encoded, expected) {
|
|
57
363
|
if (expected.network !== "mainnet" && expected.network !== "devnet") {
|
|
58
|
-
throw new SwarmClientError({
|
|
364
|
+
throw new SwarmClientError({
|
|
365
|
+
code: "INVALID_ARGUMENT",
|
|
366
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
367
|
+
message: "Unsupported network",
|
|
368
|
+
});
|
|
59
369
|
}
|
|
60
370
|
const inspected = inspectTransaction(encoded);
|
|
61
371
|
if (!inspected.signers.includes(expected.wallet)) {
|
|
62
|
-
throw new SwarmClientError({
|
|
372
|
+
throw new SwarmClientError({
|
|
373
|
+
code: "TRANSACTION_MISMATCH",
|
|
374
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
375
|
+
message: "Wallet is not a required signer",
|
|
376
|
+
});
|
|
63
377
|
}
|
|
64
378
|
if (expected.feePayer && inspected.fee_payer !== expected.feePayer) {
|
|
65
|
-
throw new SwarmClientError({
|
|
379
|
+
throw new SwarmClientError({
|
|
380
|
+
code: "TRANSACTION_MISMATCH",
|
|
381
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
382
|
+
message: "Fee payer differs from expectation",
|
|
383
|
+
});
|
|
66
384
|
}
|
|
67
385
|
const shillbotId = SHILLBOT_PROGRAM_ID.toBase58();
|
|
68
386
|
const shillbotIxs = inspected.instructions.filter((instruction) => instruction.program_id === shillbotId);
|
|
@@ -78,11 +396,19 @@ export function validatePreparedTransaction(encoded, expected) {
|
|
|
78
396
|
(!expected.taskPda || companions[0].accounts[0] === expected.taskPda) &&
|
|
79
397
|
companions[0].accounts[1] === expected.wallet);
|
|
80
398
|
if (lifecycle.length !== 1 || !validCompanions) {
|
|
81
|
-
throw new SwarmClientError({
|
|
399
|
+
throw new SwarmClientError({
|
|
400
|
+
code: "TRANSACTION_MISMATCH",
|
|
401
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
402
|
+
message: "Expected exactly one matching Shillbot lifecycle instruction",
|
|
403
|
+
});
|
|
82
404
|
}
|
|
83
405
|
const taskAccountIndex = expected.action === "create" ? 1 : 0;
|
|
84
406
|
if (expected.taskPda && lifecycle[0].accounts[taskAccountIndex] !== expected.taskPda) {
|
|
85
|
-
throw new SwarmClientError({
|
|
407
|
+
throw new SwarmClientError({
|
|
408
|
+
code: "TRANSACTION_MISMATCH",
|
|
409
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
410
|
+
message: "Task PDA differs from expectation",
|
|
411
|
+
});
|
|
86
412
|
}
|
|
87
413
|
const alwaysAllowed = new Set([
|
|
88
414
|
shillbotId,
|
|
@@ -92,16 +418,29 @@ export function validatePreparedTransaction(encoded, expected) {
|
|
|
92
418
|
]);
|
|
93
419
|
const unrelated = inspected.instructions.find((instruction) => !alwaysAllowed.has(instruction.program_id));
|
|
94
420
|
if (unrelated) {
|
|
95
|
-
throw new SwarmClientError({
|
|
421
|
+
throw new SwarmClientError({
|
|
422
|
+
code: "TRANSACTION_MISMATCH",
|
|
423
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
424
|
+
message: `Unexpected program ${unrelated.program_id}`,
|
|
425
|
+
});
|
|
96
426
|
}
|
|
97
427
|
if (expected.action === "create" && expected.escrowLamports !== undefined) {
|
|
98
428
|
const data = Buffer.from(lifecycle[0].data_base64, "base64");
|
|
99
|
-
if (data.length < 24)
|
|
100
|
-
throw new SwarmClientError({
|
|
429
|
+
if (data.length < 24) {
|
|
430
|
+
throw new SwarmClientError({
|
|
431
|
+
code: "TRANSACTION_MISMATCH",
|
|
432
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
433
|
+
message: "Create instruction is truncated",
|
|
434
|
+
});
|
|
435
|
+
}
|
|
101
436
|
const view = new DataView(data.buffer, data.byteOffset + 16, 8);
|
|
102
437
|
const amount = view.getBigUint64(0, true);
|
|
103
438
|
if (amount !== expected.escrowLamports) {
|
|
104
|
-
throw new SwarmClientError({
|
|
439
|
+
throw new SwarmClientError({
|
|
440
|
+
code: "TRANSACTION_MISMATCH",
|
|
441
|
+
operation: "shillbot.validatePreparedTransaction",
|
|
442
|
+
message: `Escrow amount ${amount} differs from expected ${expected.escrowLamports}`,
|
|
443
|
+
});
|
|
105
444
|
}
|
|
106
445
|
}
|
|
107
446
|
return inspected;
|
|
@@ -114,7 +453,12 @@ export async function signBroadcastPreparedTransaction(input) {
|
|
|
114
453
|
return await Promise.race([
|
|
115
454
|
signAndBroadcast(input.connection, input.transaction, input.sign, input.confirmation),
|
|
116
455
|
new Promise((_, reject) => {
|
|
117
|
-
timeout = (input.setTimeout ?? globalThis.setTimeout)(() => reject(new SwarmClientError({
|
|
456
|
+
timeout = (input.setTimeout ?? globalThis.setTimeout)(() => reject(new SwarmClientError({
|
|
457
|
+
code: "TIMEOUT",
|
|
458
|
+
operation: "shillbot.signBroadcastPreparedTransaction",
|
|
459
|
+
message: `Transaction did not confirm within ${timeoutMs}ms`,
|
|
460
|
+
retryable: true,
|
|
461
|
+
})), timeoutMs);
|
|
118
462
|
}),
|
|
119
463
|
]);
|
|
120
464
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type EvmSwarmSigner } from "./types.js";
|
|
2
|
+
export interface EvmPrivateKeyOptions {
|
|
3
|
+
chainId?: number;
|
|
4
|
+
rpcUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface EvmWalletClientLike {
|
|
7
|
+
account?: {
|
|
8
|
+
address: `0x${string}`;
|
|
9
|
+
} | `0x${string}`;
|
|
10
|
+
chain?: {
|
|
11
|
+
id: number;
|
|
12
|
+
};
|
|
13
|
+
signMessage: (args: any) => Promise<`0x${string}`>;
|
|
14
|
+
sendTransaction?: (args: any) => Promise<`0x${string}`>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a browser-clean `EvmSwarmSigner` from a `0x`-prefixed 32-byte hex private key.
|
|
18
|
+
* Derives the EIP-55 address and EIP-191 `personal_sign` signatures without requiring
|
|
19
|
+
* optional peer `viem` at module load, and uses `viem` dynamically if `sendTransaction` is called.
|
|
20
|
+
*/
|
|
21
|
+
export declare function fromEvmPrivateKey(privateKeyHex: `0x${string}`, opts?: EvmPrivateKeyOptions): EvmSwarmSigner;
|
|
22
|
+
/**
|
|
23
|
+
* Adapt a `viem` / `wagmi` `WalletClient` into a browser-clean `EvmSwarmSigner`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function fromEvmWalletClient(walletClient: EvmWalletClientLike): EvmSwarmSigner;
|