@swarm-tips/client 0.4.9 → 0.4.10

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.
@@ -20,17 +20,27 @@ const DEFAULT_RPC = {
20
20
  async function main() {
21
21
  const args = process.argv.slice(2);
22
22
  if (args.length === 0 || args[0] === "--help" || args[0] === "-h") {
23
- process.stderr.write("Usage: vow-verify <attestation.json> [--rpc <url>]\n" +
24
- " vow-verify - [--rpc <url>] (read from stdin)\n");
23
+ process.stderr.write("Usage: vow-verify <attestation.json> [--network mainnet|devnet] [--rpc <url>]\n" +
24
+ " vow-verify - [--network mainnet|devnet] [--rpc <url>] (read from stdin)\n");
25
25
  return 2;
26
26
  }
27
27
  const path = args[0];
28
28
  let rpcUrlOverride;
29
+ let expectedNetwork = "mainnet";
29
30
  for (let i = 1; i < args.length; i++) {
30
31
  if (args[i] === "--rpc" && i + 1 < args.length) {
31
32
  rpcUrlOverride = args[i + 1];
32
33
  i++;
33
34
  }
35
+ else if (args[i] === "--network" && i + 1 < args.length) {
36
+ const candidate = args[i + 1];
37
+ if (candidate !== "mainnet" && candidate !== "devnet") {
38
+ process.stderr.write(`invalid --network "${candidate}" (expected "mainnet" or "devnet")\n`);
39
+ return 2;
40
+ }
41
+ expectedNetwork = candidate;
42
+ i++;
43
+ }
34
44
  }
35
45
  let raw;
36
46
  try {
@@ -48,14 +58,9 @@ async function main() {
48
58
  process.stderr.write(`invalid JSON in ${path}: ${e.message}\n`);
49
59
  return 2;
50
60
  }
51
- const network = typeof attestation === "object" &&
52
- attestation !== null &&
53
- typeof attestation.network === "string"
54
- ? attestation.network
55
- : "mainnet";
56
- const rpcUrl = rpcUrlOverride ?? DEFAULT_RPC[network] ?? DEFAULT_RPC.mainnet;
61
+ const rpcUrl = rpcUrlOverride ?? DEFAULT_RPC[expectedNetwork];
57
62
  const rpc = new Connection(rpcUrl, "confirmed");
58
- const verdict = await verifyV1(attestation, shillbotProtocol, rpc);
63
+ const verdict = await verifyV1(attestation, shillbotProtocol, rpc, undefined, expectedNetwork);
59
64
  process.stdout.write(JSON.stringify(verdict, null, 2) + "\n");
60
65
  return verdict.valid ? 0 : 1;
61
66
  }
@@ -4022,17 +4022,29 @@ export type Shillbot = {
4022
4022
  ];
4023
4023
  "type": "u8";
4024
4024
  },
4025
+ {
4026
+ "name": "payoutRouteKind";
4027
+ "docs": [
4028
+ "Kind of `payout_to` route recorded at `set_payout_to` time (P2-01,",
4029
+ "carved from `_reserved` — bytewise-compatible with existing on-chain",
4030
+ "tasks because legacy `_reserved` bytes are zero):",
4031
+ "- `0` (`PAYOUT_ROUTE_UNSET_OR_LEGACY`): unset or legacy pre-upgrade task",
4032
+ "- `1` (`PAYOUT_ROUTE_EXTENSION_CREDIT`): `extension-credit` Advance PDA",
4033
+ "- `2` (`PAYOUT_ROUTE_SYSTEM_WALLET`): explicit System-owned EOA wallet"
4034
+ ];
4035
+ "type": "u8";
4036
+ },
4025
4037
  {
4026
4038
  "name": "reserved";
4027
4039
  "docs": [
4028
4040
  "Reserved space for future fields without reallocation.",
4029
- "Reduced from 20 → 19 by D1 (`requires_approval`) → 18 by the",
4030
- "2026-07-07 `verification_kind` carve."
4041
+ "Reduced from 20 → 19 by D1 (`requires_approval`) → 18 by",
4042
+ "`verification_kind` → 17 by `payout_route_kind`."
4031
4043
  ];
4032
4044
  "type": {
4033
4045
  "array": [
4034
4046
  "u8",
4035
- 18
4047
+ 17
4036
4048
  ];
4037
4049
  };
4038
4050
  },
@@ -4016,17 +4016,29 @@
4016
4016
  ],
4017
4017
  "type": "u8"
4018
4018
  },
4019
+ {
4020
+ "name": "payout_route_kind",
4021
+ "docs": [
4022
+ "Kind of `payout_to` route recorded at `set_payout_to` time (P2-01,",
4023
+ "carved from `_reserved` — bytewise-compatible with existing on-chain",
4024
+ "tasks because legacy `_reserved` bytes are zero):",
4025
+ "- `0` (`PAYOUT_ROUTE_UNSET_OR_LEGACY`): unset or legacy pre-upgrade task",
4026
+ "- `1` (`PAYOUT_ROUTE_EXTENSION_CREDIT`): `extension-credit` Advance PDA",
4027
+ "- `2` (`PAYOUT_ROUTE_SYSTEM_WALLET`): explicit System-owned EOA wallet"
4028
+ ],
4029
+ "type": "u8"
4030
+ },
4019
4031
  {
4020
4032
  "name": "_reserved",
4021
4033
  "docs": [
4022
4034
  "Reserved space for future fields without reallocation.",
4023
- "Reduced from 20 → 19 by D1 (`requires_approval`) → 18 by the",
4024
- "2026-07-07 `verification_kind` carve."
4035
+ "Reduced from 20 → 19 by D1 (`requires_approval`) → 18 by",
4036
+ "`verification_kind` → 17 by `payout_route_kind`."
4025
4037
  ],
4026
4038
  "type": {
4027
4039
  "array": [
4028
4040
  "u8",
4029
- 18
4041
+ 17
4030
4042
  ]
4031
4043
  }
4032
4044
  },
@@ -1,5 +1,7 @@
1
1
  import { type Hex } from "viem";
2
2
  import type { CoordinationGameApiClient, EvmMatchPayload, XMatchPayload, XOutcomeCertPayload } from "../api.js";
3
+ /** Maximum horizon (7 days) allowed for relay-supplied `fund_deadline` / `match_deadline`. */
4
+ export declare const MAX_XCHAIN_DEADLINE_HORIZON_SECS: bigint;
3
5
  export declare function unpadAddress(word: string): Hex;
4
6
  /**
5
7
  * Normalize a 65-byte `0x` secp256k1 `[r||s||v]` signature so `v` is in the
@@ -51,6 +53,10 @@ export interface EvmFundCall {
51
53
  export interface EvmFundPin {
52
54
  contract: Hex;
53
55
  stakeWei: bigint;
56
+ /** Expected local EVM session key address (`leg_b.session_key`). Verified whenever provided. */
57
+ sessionKey?: Hex;
58
+ /** Optional Unix-seconds clock override for deadline horizon validation. */
59
+ nowSec?: number;
54
60
  }
55
61
  /**
56
62
  * Reconstruct the `CertLib.MatchLiveCert` tuple expected by `CrossChainGame`
@@ -1,8 +1,30 @@
1
1
  import { createPublicClient, encodeFunctionData, http, keccak256, toBytes, } from "viem";
2
2
  import { CROSS_CHAIN_GAME_ABI } from "./abi.js";
3
+ const ZERO_ADDRESS_HEX = "0000000000000000000000000000000000000000";
4
+ /** Maximum horizon (7 days) allowed for relay-supplied `fund_deadline` / `match_deadline`. */
5
+ export const MAX_XCHAIN_DEADLINE_HORIZON_SECS = 7n * 86400n;
3
6
  export function unpadAddress(word) {
4
- const hex = word.startsWith("0x") ? word.slice(2) : word;
5
- return `0x${hex.slice(-40)}`;
7
+ if (typeof word !== "string") {
8
+ throw new Error("invalid EVM address: expected hex string");
9
+ }
10
+ const hex = word.startsWith("0x") || word.startsWith("0X") ? word.slice(2) : word;
11
+ let addr;
12
+ if (/^[0-9a-fA-F]{40}$/.test(hex)) {
13
+ addr = hex.toLowerCase();
14
+ }
15
+ else if (/^[0-9a-fA-F]{64}$/.test(hex)) {
16
+ if (!hex.startsWith("000000000000000000000000")) {
17
+ throw new Error(`invalid 32-byte padded EVM address: non-zero high 12 bytes in ${word}`);
18
+ }
19
+ addr = hex.slice(24).toLowerCase();
20
+ }
21
+ else {
22
+ throw new Error(`invalid EVM address: expected 20-byte or zero-padded 32-byte hex, got ${word}`);
23
+ }
24
+ if (addr === ZERO_ADDRESS_HEX) {
25
+ throw new Error("invalid EVM address: zero address is not allowed");
26
+ }
27
+ return `0x${addr}`;
6
28
  }
7
29
  /**
8
30
  * Normalize a 65-byte `0x` secp256k1 `[r||s||v]` signature so `v` is in the
@@ -16,10 +38,12 @@ export function normalizeEvmSigV(sig) {
16
38
  }
17
39
  const body = sig.slice(2).toLowerCase();
18
40
  const rs = body.slice(0, 128);
19
- let v = parseInt(body.slice(128, 130), 16);
20
- if (v < 27)
21
- v += 27;
22
- return `0x${rs}${v.toString(16).padStart(2, "0")}`;
41
+ const v = parseInt(body.slice(128, 130), 16);
42
+ if (v !== 0 && v !== 1 && v !== 27 && v !== 28) {
43
+ throw new Error(`invalid secp256k1 signature recovery id v=${v}; expected 0, 1, 27, or 28`);
44
+ }
45
+ const normalizedV = v < 27 ? v + 27 : v;
46
+ return `0x${rs}${normalizedV.toString(16).padStart(2, "0")}`;
23
47
  }
24
48
  export async function findCrossChainMatch(api, input, options = {}) {
25
49
  const resolvedToken = options.token ?? input.token;
@@ -67,15 +91,24 @@ export async function findEvmMatch(api, input, options = {}) {
67
91
  throw new Error("no opponent found before timeout");
68
92
  }
69
93
  function verifyPin(match, pin) {
94
+ const pinnedContract = unpadAddress(pin.contract);
70
95
  const to = unpadAddress(match.leg_b.contract);
71
- if (to.toLowerCase() !== pin.contract.toLowerCase()) {
72
- throw new Error(`relay leg_b.contract ${to} != pinned CrossChainGame ${pin.contract}`);
96
+ if (to !== pinnedContract) {
97
+ throw new Error(`relay leg_b.contract ${to} != pinned CrossChainGame ${pinnedContract}`);
73
98
  }
74
99
  const stakeWei = BigInt(match.leg_b.stake_base_units);
75
100
  if (stakeWei !== pin.stakeWei) {
76
101
  throw new Error(`relay leg_b.stake ${stakeWei} != pinned stake ${pin.stakeWei}`);
77
102
  }
78
- return { to, stakeWei };
103
+ const legBSessionKey = unpadAddress(match.leg_b.session_key);
104
+ const legASessionKey = unpadAddress(match.leg_a.session_key);
105
+ if (pin.sessionKey !== undefined) {
106
+ const pinnedSessionKey = unpadAddress(pin.sessionKey);
107
+ if (legBSessionKey !== pinnedSessionKey) {
108
+ throw new Error(`relay leg_b.session_key ${legBSessionKey} != pinned sessionKey ${pinnedSessionKey}`);
109
+ }
110
+ }
111
+ return { to, stakeWei, legBSessionKey, legASessionKey };
79
112
  }
80
113
  function legTupleFromPayload(leg) {
81
114
  return {
@@ -108,18 +141,35 @@ export function certTupleFromMatchPayload(match) {
108
141
  };
109
142
  }
110
143
  export function buildEvmFundCall(match, pin) {
111
- const { to, stakeWei } = verifyPin(match, pin);
144
+ const { to, stakeWei, legBSessionKey, legASessionKey } = verifyPin(match, pin);
145
+ if (match.a_is_p1 !== 0 && match.a_is_p1 !== 1) {
146
+ throw new Error(`invalid a_is_p1=${String(match.a_is_p1)}; expected 0 or 1`);
147
+ }
148
+ const fundDeadline = BigInt(match.fund_deadline);
149
+ const matchDeadline = BigInt(match.match_deadline);
150
+ const nowSec = BigInt(pin.nowSec !== undefined ? Math.floor(pin.nowSec) : Math.floor(Date.now() / 1000));
151
+ const maxDeadline = nowSec + MAX_XCHAIN_DEADLINE_HORIZON_SECS;
152
+ if (fundDeadline <= 0n || matchDeadline <= 0n) {
153
+ throw new Error(`invalid deadlines (fund_deadline=${fundDeadline}, match_deadline=${matchDeadline}): must be > 0`);
154
+ }
155
+ if (matchDeadline < fundDeadline) {
156
+ throw new Error(`invalid deadlines: match_deadline (${matchDeadline}) < fund_deadline (${fundDeadline})`);
157
+ }
158
+ if (fundDeadline > maxDeadline || matchDeadline > maxDeadline) {
159
+ throw new Error(`relay deadline exceeds 7-day horizon (fund_deadline=${fundDeadline}, match_deadline=${matchDeadline}, max=${maxDeadline})`);
160
+ }
161
+ const operatorSig = normalizeEvmSigV(match.create_match_operator_sig);
112
162
  const data = encodeFunctionData({
113
163
  abi: CROSS_CHAIN_GAME_ABI,
114
164
  functionName: "createMatch",
115
165
  args: [
116
166
  match.match_id,
117
- unpadAddress(match.leg_b.session_key),
118
- unpadAddress(match.leg_a.session_key),
167
+ legBSessionKey,
168
+ legASessionKey,
119
169
  match.a_is_p1 === 0,
120
- BigInt(match.fund_deadline),
121
- BigInt(match.match_deadline),
122
- match.create_match_operator_sig,
170
+ fundDeadline,
171
+ matchDeadline,
172
+ operatorSig,
123
173
  ],
124
174
  });
125
175
  return { to, data, value: stakeWei };
@@ -316,6 +316,9 @@ export class InboxClient {
316
316
  if (!challenge.nonce) {
317
317
  throw new InboxApiError(0, "session challenge returned no nonce");
318
318
  }
319
+ if (typeof challenge.nonce !== "string" || !/^[0-9a-f]{64}$/.test(challenge.nonce)) {
320
+ throw new InboxApiError(0, "session challenge nonce must be 64 lowercase hex characters; refusing to sign arbitrary payload");
321
+ }
319
322
  const signature = await signNonce(challenge.nonce);
320
323
  const verified = await this.postUnauthed(SESSION_PATH, { wallet, nonce: challenge.nonce, signature });
321
324
  if (!verified.session_id) {
@@ -272,10 +272,111 @@ export interface PreparedTransactionExpectation {
272
272
  export declare const SHILLBOT_ESCROW_DEPLOYMENTS: Readonly<Record<string, `0x${string}`>>;
273
273
  /** Canonical 4-byte function selectors for `ShillbotEscrow.sol` lifecycle actions. */
274
274
  export declare const SHILLBOT_EVM_SELECTORS: Readonly<Record<Action, `0x${string}`>>;
275
+ /** Canonical ABI for `ShillbotEscrow.sol` lifecycle functions (`P2-27`). */
276
+ export declare const SHILLBOT_ESCROW_ABI: readonly [{
277
+ readonly type: "function";
278
+ readonly name: "createTask";
279
+ readonly stateMutability: "payable";
280
+ readonly inputs: readonly [{
281
+ readonly name: "statementCommitment";
282
+ readonly type: "bytes32";
283
+ }, {
284
+ readonly name: "policyId";
285
+ readonly type: "bytes32";
286
+ }, {
287
+ readonly name: "verificationKind";
288
+ readonly type: "uint8";
289
+ }, {
290
+ readonly name: "deadline";
291
+ readonly type: "uint64";
292
+ }, {
293
+ readonly name: "requiresApproval";
294
+ readonly type: "bool";
295
+ }];
296
+ readonly outputs: readonly [{
297
+ readonly name: "taskId";
298
+ readonly type: "uint64";
299
+ }];
300
+ }, {
301
+ readonly type: "function";
302
+ readonly name: "claimTask";
303
+ readonly stateMutability: "nonpayable";
304
+ readonly inputs: readonly [{
305
+ readonly name: "taskId";
306
+ readonly type: "uint64";
307
+ }];
308
+ readonly outputs: readonly [];
309
+ }, {
310
+ readonly type: "function";
311
+ readonly name: "submitWork";
312
+ readonly stateMutability: "nonpayable";
313
+ readonly inputs: readonly [{
314
+ readonly name: "taskId";
315
+ readonly type: "uint64";
316
+ }, {
317
+ readonly name: "contentIdHash";
318
+ readonly type: "bytes32";
319
+ }, {
320
+ readonly name: "artifactHash";
321
+ readonly type: "bytes32";
322
+ }];
323
+ readonly outputs: readonly [];
324
+ }, {
325
+ readonly type: "function";
326
+ readonly name: "approveTask";
327
+ readonly stateMutability: "nonpayable";
328
+ readonly inputs: readonly [{
329
+ readonly name: "taskId";
330
+ readonly type: "uint64";
331
+ }];
332
+ readonly outputs: readonly [];
333
+ }, {
334
+ readonly type: "function";
335
+ readonly name: "verifyTaskAttested";
336
+ readonly stateMutability: "nonpayable";
337
+ readonly inputs: readonly [{
338
+ readonly name: "taskId";
339
+ readonly type: "uint64";
340
+ }, {
341
+ readonly name: "score";
342
+ readonly type: "uint64";
343
+ }, {
344
+ readonly name: "sig";
345
+ readonly type: "bytes";
346
+ }];
347
+ readonly outputs: readonly [];
348
+ }, {
349
+ readonly type: "function";
350
+ readonly name: "finalizeTask";
351
+ readonly stateMutability: "nonpayable";
352
+ readonly inputs: readonly [{
353
+ readonly name: "taskId";
354
+ readonly type: "uint64";
355
+ }];
356
+ readonly outputs: readonly [];
357
+ }, {
358
+ readonly type: "function";
359
+ readonly name: "expireTask";
360
+ readonly stateMutability: "nonpayable";
361
+ readonly inputs: readonly [{
362
+ readonly name: "taskId";
363
+ readonly type: "uint64";
364
+ }];
365
+ readonly outputs: readonly [];
366
+ }];
275
367
  export interface PreparedEvmCallExpectation {
276
368
  action: Action;
277
369
  chain?: string;
278
370
  maxValueWei?: bigint;
371
+ taskId?: bigint | number | string;
372
+ contentIdHash?: `0x${string}`;
373
+ artifactHash?: `0x${string}`;
374
+ statementCommitment?: `0x${string}`;
375
+ policyId?: `0x${string}`;
376
+ verificationKind?: number;
377
+ deadline?: bigint | number | string;
378
+ requiresApproval?: boolean;
379
+ score?: bigint | number | string;
279
380
  }
280
381
  export interface ValidatedEvmCall {
281
382
  to: `0x${string}`;
@@ -285,9 +386,9 @@ export interface ValidatedEvmCall {
285
386
  }
286
387
  export declare function normalizeEvmChainId(chain: string | number): string;
287
388
  /**
288
- * C10 fail-closed local validator for server-prepared EVM `ShillbotEscrow` calls.
289
- * Verifies canonical contract address for `chain`, 4-byte function selector for
290
- * `action`, and `value_wei <= maxValueWei` (strictly `0n` on non-create actions).
389
+ * C10 + P2-27 fail-closed local validator for server-prepared EVM `ShillbotEscrow` calls.
390
+ * Verifies canonical contract address for `chain`, 4-byte function selector and full ABI-decoded
391
+ * calldata arguments for `action`, and `value_wei <= maxValueWei` (strictly `0n` on non-create actions).
291
392
  */
292
393
  export declare function validatePreparedEvmCall(evmCall: unknown, expected: PreparedEvmCallExpectation): ValidatedEvmCall;
293
394
  /**