@swarm-tips/client 0.4.6 → 0.4.7
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/SBOM.spdx.json +5 -5
- package/dist/agent.d.ts +19 -0
- package/dist/agent.js +82 -15
- package/dist/cli/keyfile.d.ts +3 -2
- package/dist/cli/keyfile.js +21 -4
- package/dist/cli/swarm.js +43 -11
- package/dist/coordination-game/api.d.ts +61 -26
- package/dist/coordination-game/api.js +47 -18
- package/dist/coordination-game/evm/cross-chain.d.ts +25 -4
- package/dist/coordination-game/evm/cross-chain.js +28 -2
- package/dist/coordination-game/evm/index.d.ts +1 -1
- package/dist/coordination-game/evm/session.d.ts +81 -26
- package/dist/coordination-game/evm/session.js +273 -75
- package/dist/coordination-game/index.d.ts +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +6 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/shillbot/api.d.ts +8 -4
- package/dist/shillbot/api.js +9 -4
- package/dist/shillbot/index.d.ts +1 -1
- package/dist/shillbot/index.js +1 -1
- package/dist/shillbot/verify-tx.d.ts +8 -3
- package/dist/shillbot/verify-tx.js +47 -12
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { HttpClient } from "../http.js";
|
|
2
2
|
import { SwarmClientError } from "../errors.js";
|
|
3
|
+
function normalizeAiFlag(body) {
|
|
4
|
+
const { isAi, is_ai, ...rest } = body;
|
|
5
|
+
const resolved = is_ai ?? isAi;
|
|
6
|
+
return {
|
|
7
|
+
...rest,
|
|
8
|
+
...(resolved !== undefined ? { is_ai: resolved } : {}),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
3
11
|
export class CoordinationGameApiClient {
|
|
4
12
|
http;
|
|
5
13
|
constructor(options) {
|
|
@@ -10,8 +18,12 @@ export class CoordinationGameApiClient {
|
|
|
10
18
|
authSession(body) { return this.http.post("/auth/session", body); }
|
|
11
19
|
evmAuthChallenge(body) { return this.http.post("/auth/evm/challenge", body); }
|
|
12
20
|
evmAuthVerify(body) { return this.http.post("/auth/evm/verify", body); }
|
|
13
|
-
joinQueue(body, token) {
|
|
14
|
-
|
|
21
|
+
joinQueue(body, token) {
|
|
22
|
+
return this.http.post("/queue/join", normalizeAiFlag(body), token);
|
|
23
|
+
}
|
|
24
|
+
leaveQueue(body, token) {
|
|
25
|
+
return this.http.post("/queue/leave", normalizeAiFlag(body), token);
|
|
26
|
+
}
|
|
15
27
|
gameStarted(body, token) { return this.http.post("/games/started", body, token); }
|
|
16
28
|
gameJoined(body, token) { return this.http.post("/games/joined", body, token); }
|
|
17
29
|
cosign(body, token) { return this.http.post("/games/cosign", body, token); }
|
|
@@ -20,7 +32,10 @@ export class CoordinationGameApiClient {
|
|
|
20
32
|
bothCommitted(body, token) { return this.http.post("/games/both-committed", body, token); }
|
|
21
33
|
resolved(body, token) { return this.http.post("/games/resolved", body, token); }
|
|
22
34
|
joinCrossChainQueue(body, token) {
|
|
23
|
-
return this.http.post("/internal/xqueue/join", body, token);
|
|
35
|
+
return this.http.post("/internal/xqueue/join", normalizeAiFlag(body), token);
|
|
36
|
+
}
|
|
37
|
+
leaveCrossChainQueue(body, token) {
|
|
38
|
+
return this.http.post("/internal/xqueue/leave", body, token);
|
|
24
39
|
}
|
|
25
40
|
crossChainStatus(by, token) {
|
|
26
41
|
const query = "handle" in by
|
|
@@ -28,20 +43,20 @@ export class CoordinationGameApiClient {
|
|
|
28
43
|
: `wallet=${encodeURIComponent(by.wallet)}`;
|
|
29
44
|
return this.http.get(`/internal/xqueue/status?${query}`, token);
|
|
30
45
|
}
|
|
31
|
-
crossChainCommit(body) {
|
|
32
|
-
return this.http.post("/internal/xqueue/commit", body);
|
|
46
|
+
crossChainCommit(body, token) {
|
|
47
|
+
return this.http.post("/internal/xqueue/commit", body, token);
|
|
33
48
|
}
|
|
34
|
-
crossChainSign(body) {
|
|
35
|
-
return this.http.post("/internal/xqueue/sign", body);
|
|
49
|
+
crossChainSign(body, token) {
|
|
50
|
+
return this.http.post("/internal/xqueue/sign", body, token);
|
|
36
51
|
}
|
|
37
|
-
crossChainReveal(body) {
|
|
38
|
-
return this.http.post("/internal/xqueue/reveal", body);
|
|
52
|
+
crossChainReveal(body, token) {
|
|
53
|
+
return this.http.post("/internal/xqueue/reveal", body, token);
|
|
39
54
|
}
|
|
40
|
-
crossChainGameplay(by) {
|
|
55
|
+
crossChainGameplay(by, token) {
|
|
41
56
|
const query = "handle" in by
|
|
42
57
|
? `handle=${encodeURIComponent(by.handle)}`
|
|
43
58
|
: `wallet=${encodeURIComponent(by.wallet)}`;
|
|
44
|
-
return this.http.get(`/internal/xqueue/gameplay?${query}
|
|
59
|
+
return this.http.get(`/internal/xqueue/gameplay?${query}`, token);
|
|
45
60
|
}
|
|
46
61
|
submitCrossChainSettleSigs(body, token) {
|
|
47
62
|
return this.http.post("/internal/xqueue/settle-sigs", body, token);
|
|
@@ -49,20 +64,34 @@ export class CoordinationGameApiClient {
|
|
|
49
64
|
crossChainOutcomeCosign(body, token) {
|
|
50
65
|
return this.http.post("/internal/xqueue/outcome-cosign", body, token);
|
|
51
66
|
}
|
|
67
|
+
buildCrossChainSolFund(body, token) {
|
|
68
|
+
return this.http.post("/internal/xqueue/build-sol-fund", body, token);
|
|
69
|
+
}
|
|
70
|
+
buildCrossChainSolLock(body, token) {
|
|
71
|
+
return this.http.post("/internal/xqueue/build-sol-lock", body, token);
|
|
72
|
+
}
|
|
52
73
|
buildCrossChainSolSettle(body, token) {
|
|
53
74
|
return this.http.post("/internal/xqueue/build-sol-settle", body, token);
|
|
54
75
|
}
|
|
76
|
+
buildCrossChainSolRefund(body, token) {
|
|
77
|
+
return this.http.post("/internal/xqueue/build-sol-refund", body, token);
|
|
78
|
+
}
|
|
55
79
|
joinEvmGame(body, token) {
|
|
56
|
-
return this.http.post("/internal/evmgame/join", body, token);
|
|
80
|
+
return this.http.post("/internal/evmgame/join", normalizeAiFlag(body), token);
|
|
57
81
|
}
|
|
58
|
-
evmGameStatus(
|
|
59
|
-
|
|
82
|
+
evmGameStatus(walletOrBy, token) {
|
|
83
|
+
const query = typeof walletOrBy === "string"
|
|
84
|
+
? `wallet=${encodeURIComponent(walletOrBy)}`
|
|
85
|
+
: walletOrBy.handle
|
|
86
|
+
? `handle=${encodeURIComponent(walletOrBy.handle)}`
|
|
87
|
+
: `wallet=${encodeURIComponent(walletOrBy.wallet ?? "")}`;
|
|
88
|
+
return this.http.get(`/internal/evmgame/status?${query}`, token);
|
|
60
89
|
}
|
|
61
|
-
evmGameCommitted(body) {
|
|
62
|
-
return this.http.post("/internal/evmgame/committed", body);
|
|
90
|
+
evmGameCommitted(body, token) {
|
|
91
|
+
return this.http.post("/internal/evmgame/committed", body, token);
|
|
63
92
|
}
|
|
64
|
-
evmGameStaked(body) {
|
|
65
|
-
return this.http.post("/internal/evmgame/staked", body);
|
|
93
|
+
evmGameStaked(body, token) {
|
|
94
|
+
return this.http.post("/internal/evmgame/staked", body, token);
|
|
66
95
|
}
|
|
67
96
|
}
|
|
68
97
|
export class CoordinationGameWebSocketClient {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Hex } from "viem";
|
|
2
|
-
import type { CoordinationGameApiClient, XMatchPayload, XOutcomeCertPayload } from "../api.js";
|
|
2
|
+
import type { CoordinationGameApiClient, EvmMatchPayload, XMatchPayload, XOutcomeCertPayload } from "../api.js";
|
|
3
3
|
export declare function unpadAddress(word: string): Hex;
|
|
4
4
|
/**
|
|
5
5
|
* Normalize a 65-byte `0x` secp256k1 `[r||s||v]` signature so `v` is in the
|
|
@@ -12,16 +12,37 @@ export interface CrossChainMatchResult {
|
|
|
12
12
|
match: XMatchPayload;
|
|
13
13
|
handle: string | undefined;
|
|
14
14
|
}
|
|
15
|
-
export
|
|
15
|
+
export interface FindCrossChainMatchInput {
|
|
16
16
|
wallet: string;
|
|
17
17
|
sessionKeyAddress: string;
|
|
18
18
|
tournamentId: number;
|
|
19
19
|
chain: string;
|
|
20
|
-
|
|
20
|
+
isAi?: boolean;
|
|
21
|
+
is_ai?: boolean;
|
|
22
|
+
token?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface FindMatchPollOptions {
|
|
21
25
|
pollMs?: number;
|
|
22
26
|
maxPolls?: number;
|
|
23
27
|
sleep?: (ms: number) => Promise<void>;
|
|
24
|
-
|
|
28
|
+
token?: string;
|
|
29
|
+
isAi?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export declare function findCrossChainMatch(api: CoordinationGameApiClient, input: FindCrossChainMatchInput, options?: FindMatchPollOptions): Promise<CrossChainMatchResult>;
|
|
32
|
+
export interface FindEvmMatchInput {
|
|
33
|
+
wallet: string;
|
|
34
|
+
playerWallet?: string;
|
|
35
|
+
tournamentId: number;
|
|
36
|
+
chain: string;
|
|
37
|
+
isAi?: boolean;
|
|
38
|
+
is_ai?: boolean;
|
|
39
|
+
token?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface EvmMatchResult {
|
|
42
|
+
match: EvmMatchPayload;
|
|
43
|
+
sessionId: string | undefined;
|
|
44
|
+
}
|
|
45
|
+
export declare function findEvmMatch(api: CoordinationGameApiClient, input: FindEvmMatchInput, options?: FindMatchPollOptions): Promise<EvmMatchResult>;
|
|
25
46
|
export interface EvmFundCall {
|
|
26
47
|
to: Hex;
|
|
27
48
|
data: Hex;
|
|
@@ -22,24 +22,50 @@ export function normalizeEvmSigV(sig) {
|
|
|
22
22
|
return `0x${rs}${v.toString(16).padStart(2, "0")}`;
|
|
23
23
|
}
|
|
24
24
|
export async function findCrossChainMatch(api, input, options = {}) {
|
|
25
|
+
const resolvedToken = options.token ?? input.token;
|
|
26
|
+
const resolvedIsAi = options.isAi ?? input.isAi ?? input.is_ai;
|
|
25
27
|
const joined = await api.joinCrossChainQueue({
|
|
26
28
|
wallet: input.wallet,
|
|
27
29
|
chain: input.chain,
|
|
28
30
|
session_key: input.sessionKeyAddress,
|
|
29
31
|
tournament_id: input.tournamentId,
|
|
30
|
-
|
|
32
|
+
...(resolvedIsAi !== undefined ? { is_ai: resolvedIsAi } : {}),
|
|
33
|
+
}, resolvedToken);
|
|
31
34
|
const handle = joined.poll_handle;
|
|
32
35
|
if (joined.status === "matched" && joined.match)
|
|
33
36
|
return { match: joined.match, handle };
|
|
34
37
|
const sleep = options.sleep ?? ((ms) => new Promise((resolve) => globalThis.setTimeout(resolve, ms)));
|
|
35
38
|
for (let index = 0; index < (options.maxPolls ?? 60); index += 1) {
|
|
36
39
|
await sleep(options.pollMs ?? 4_000);
|
|
37
|
-
const next = await api.crossChainStatus(handle ? { handle } : { wallet: input.wallet });
|
|
40
|
+
const next = await api.crossChainStatus(handle ? { handle } : { wallet: input.wallet }, resolvedToken);
|
|
38
41
|
if (next.status === "matched" && next.match)
|
|
39
42
|
return { match: next.match, handle };
|
|
40
43
|
}
|
|
41
44
|
throw new Error("no opponent found before timeout");
|
|
42
45
|
}
|
|
46
|
+
export async function findEvmMatch(api, input, options = {}) {
|
|
47
|
+
const resolvedToken = options.token ?? input.token;
|
|
48
|
+
const resolvedIsAi = options.isAi ?? input.isAi ?? input.is_ai;
|
|
49
|
+
const joined = await api.joinEvmGame({
|
|
50
|
+
wallet: input.wallet,
|
|
51
|
+
...(input.playerWallet ? { player_wallet: input.playerWallet } : {}),
|
|
52
|
+
chain: input.chain,
|
|
53
|
+
tournament_id: input.tournamentId,
|
|
54
|
+
...(resolvedIsAi !== undefined ? { is_ai: resolvedIsAi } : {}),
|
|
55
|
+
}, resolvedToken);
|
|
56
|
+
if (joined.status === "matched" && joined.match) {
|
|
57
|
+
return { match: joined.match, sessionId: joined.session_id };
|
|
58
|
+
}
|
|
59
|
+
const sleep = options.sleep ?? ((ms) => new Promise((resolve) => globalThis.setTimeout(resolve, ms)));
|
|
60
|
+
for (let index = 0; index < (options.maxPolls ?? 60); index += 1) {
|
|
61
|
+
await sleep(options.pollMs ?? 4_000);
|
|
62
|
+
const next = await api.evmGameStatus(input.wallet, resolvedToken);
|
|
63
|
+
if (next.status === "matched" && next.match) {
|
|
64
|
+
return { match: next.match, sessionId: next.session_id ?? joined.session_id };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
throw new Error("no opponent found before timeout");
|
|
68
|
+
}
|
|
43
69
|
function verifyPin(match, pin) {
|
|
44
70
|
const to = unpadAddress(match.leg_b.contract);
|
|
45
71
|
if (to.toLowerCase() !== pin.contract.toLowerCase()) {
|
|
@@ -6,4 +6,4 @@ export * from "./transactions.js";
|
|
|
6
6
|
export * from "./cross-chain.js";
|
|
7
7
|
export * from "../chains.js";
|
|
8
8
|
export { signDigest, signDigestForRelay } from "../../evm/sign.js";
|
|
9
|
-
export type { XLegPayload, XMatchPayload, XOutcomeCertPayload, XSettleSigsResponse, XSolSettleTxResponse, } from "../api.js";
|
|
9
|
+
export type { EvmGameJoinInput, XLegPayload, XMatchPayload, XOutcomeCertPayload, XQueueJoinInput, XSettleSigsResponse, XSolSettleTxResponse, } from "../api.js";
|
|
@@ -1,43 +1,98 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Ephemeral EVM session key — the Base-game analog of the Solana session
|
|
3
3
|
* keypair (`session.ts`). One main-wallet popup funds the session address with
|
|
4
|
-
* stake
|
|
5
|
-
* revealGuess locally.
|
|
4
|
+
* gas (while stake is escrowed to the player's on-chain ledger); the session key
|
|
5
|
+
* then signs createGame/joinGame/commitGuess/revealGuess locally.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* main wallet, with a 24h TTL — same guards as the Solana session. Only gas
|
|
14
|
-
* dust ever sits on the session address.
|
|
7
|
+
* Persistence invariant (`P1-5`): active EVM session keys are persisted in
|
|
8
|
+
* `localStorage` first (mirroring to `sessionStorage`, and falling back to
|
|
9
|
+
* `sessionStorage` when `localStorage` is unavailable) and retained until the
|
|
10
|
+
* delegate's on-chain ETH balance is swept to `0`, so closing and reopening a
|
|
11
|
+
* browser tab never orphans funded ETH in an ephemeral EVM session key. Legacy
|
|
12
|
+
* keys stored only in `sessionStorage` are transparently migrated to `localStorage`.
|
|
15
13
|
*/
|
|
16
|
-
import { type Chain, type Hex, type PublicClient, type WalletClient } from "viem";
|
|
14
|
+
import { type Account, type Chain, type Hex, type PublicClient, type WalletClient } from "viem";
|
|
15
|
+
export declare const EVM_SESSION_STORAGE_KEY = "coordination-evm-session";
|
|
17
16
|
export interface SessionStorageLike {
|
|
18
17
|
getItem(key: string): string | null;
|
|
19
18
|
setItem(key: string, value: string): void;
|
|
20
19
|
removeItem(key: string): void;
|
|
21
20
|
}
|
|
21
|
+
export interface EvmSessionStoragePair {
|
|
22
|
+
localStorage?: SessionStorageLike | null;
|
|
23
|
+
sessionStorage?: SessionStorageLike | null;
|
|
24
|
+
}
|
|
25
|
+
export type EvmSessionStorageInput = SessionStorageLike | EvmSessionStoragePair | null | undefined;
|
|
22
26
|
/** Generate a fresh ephemeral secp256k1 private key for session signing. */
|
|
23
27
|
export declare function generateEvmSessionKey(): Hex;
|
|
24
28
|
/**
|
|
25
|
-
* Read the session key from
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export declare function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
* Read the persisted EVM session key from `localStorage` first, falling back to
|
|
30
|
+
* `sessionStorage` and migrating legacy `sessionStorage`-only keys into `localStorage`.
|
|
31
|
+
* Retains valid keys across tab closes and past 24h until explicitly cleared after
|
|
32
|
+
* on-chain ETH balance is swept to `0`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function getStoredEvmSession(storage?: EvmSessionStorageInput, walletAddress?: string, _now?: () => number): Hex | null;
|
|
35
|
+
/**
|
|
36
|
+
* Ergonomic loader for persisted EVM session keys (`localStorage` primary,
|
|
37
|
+
* `sessionStorage` fallback + legacy migration).
|
|
38
|
+
*/
|
|
39
|
+
export declare function loadSessionKey(walletAddress?: string, storage?: EvmSessionStorageInput): Hex | null;
|
|
40
|
+
export declare function loadSessionKey(storage: EvmSessionStorageInput, walletAddress?: string): Hex | null;
|
|
41
|
+
/**
|
|
42
|
+
* Persist the EVM session key in `localStorage` first, mirroring to `sessionStorage`
|
|
43
|
+
* (and falling back to `sessionStorage` when `localStorage` is unavailable).
|
|
44
|
+
*/
|
|
45
|
+
export declare function storeEvmSession(storage: EvmSessionStorageInput, privateKey: Hex, walletAddress: string, now?: () => number): void;
|
|
46
|
+
/** Ergonomic helper to persist an EVM session key in `localStorage` + `sessionStorage`. */
|
|
47
|
+
export declare function saveSessionKey(privateKey: Hex, walletAddress: string, options?: EvmSessionStoragePair & {
|
|
48
|
+
now?: () => number;
|
|
49
|
+
}): void;
|
|
50
|
+
/** Remove the EVM session key from both `localStorage` and `sessionStorage`. */
|
|
51
|
+
export declare function clearEvmSession(storage?: EvmSessionStorageInput): void;
|
|
52
|
+
/** A plain native-ETH transfer uses 21,000 gas. */
|
|
53
|
+
export declare const SWEEP_GAS_LIMIT = 21000n;
|
|
54
|
+
/**
|
|
55
|
+
* Compute the native ETH amount to sweep from a session key back to the main wallet:
|
|
56
|
+
* `balance - 1.25 * (21_000 * maxFeePerGas)`, clamped at `0n`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function sweepValueWei(balance: bigint, maxFeePerGas: bigint): bigint;
|
|
59
|
+
export interface SweepEvmSessionOptions {
|
|
60
|
+
storage?: EvmSessionStorageInput;
|
|
61
|
+
/** When `true`, deduct exact `21_000 * maxFeePerGas` with `maxPriorityFeePerGas = maxFeePerGas` so EIP-1559 drains to `0n`. */
|
|
62
|
+
exact?: boolean;
|
|
63
|
+
readClient?: {
|
|
64
|
+
getBalance: (args: {
|
|
65
|
+
address: Hex;
|
|
66
|
+
}) => Promise<bigint>;
|
|
67
|
+
estimateFeesPerGas?: () => Promise<{
|
|
68
|
+
maxFeePerGas?: bigint;
|
|
69
|
+
maxPriorityFeePerGas?: bigint;
|
|
70
|
+
}>;
|
|
71
|
+
getGasPrice?: () => Promise<bigint>;
|
|
72
|
+
waitForTransactionReceipt: (args: {
|
|
73
|
+
hash: Hex;
|
|
74
|
+
}) => Promise<{
|
|
75
|
+
status?: string;
|
|
76
|
+
}>;
|
|
77
|
+
};
|
|
78
|
+
walletClient?: {
|
|
79
|
+
account?: Account | Hex;
|
|
80
|
+
sendTransaction: (args: Record<string, unknown>) => Promise<Hex>;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Sweep leftover ETH from an EVM session key back to `mainWallet`, clearing
|
|
85
|
+
* persisted storage **only** once the session address's on-chain balance reaches `0n`.
|
|
86
|
+
* If the sweep fails or non-zero balance remains on-chain, the key is retained in
|
|
87
|
+
* `localStorage` so funded ETH is never orphaned.
|
|
88
|
+
*/
|
|
89
|
+
export declare function sweepEvmSession(sessionKey: Hex, mainWallet: Hex, chain: Chain, rpcUrls: string[], options?: SweepEvmSessionOptions): Promise<bigint>;
|
|
90
|
+
/** Persist the committed guess preimage for `gameId` so a reload/reopen can still reveal. */
|
|
91
|
+
export declare function persistEvmGuess(storage: EvmSessionStorageInput, gameId: string, r: Uint8Array): void;
|
|
92
|
+
/** The persisted preimage for `gameId`, or null (missing / different game / unreadable). */
|
|
93
|
+
export declare function loadEvmGuess(storage: EvmSessionStorageInput, gameId: string): Uint8Array | null;
|
|
39
94
|
/** Drop the persisted guess once the game is done. */
|
|
40
|
-
export declare function clearEvmGuess(storage
|
|
95
|
+
export declare function clearEvmGuess(storage?: EvmSessionStorageInput): void;
|
|
41
96
|
/** 0x address of a session private key. */
|
|
42
97
|
export declare function evmSessionAddress(privateKey: Hex): Hex;
|
|
43
98
|
/**
|