@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
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { Buffer } from "buffer";
|
|
2
|
+
import { Secp256k1Program } from "@solana/web3.js";
|
|
3
|
+
import { SwarmClientError } from "../errors.js";
|
|
4
|
+
import { redactSigner } from "./types.js";
|
|
5
|
+
// Compact pure-TS Keccak-256 for EIP-55 address checksumming with zero optional peer dependency at load time.
|
|
6
|
+
const KECCAK_ROUNDS = 24;
|
|
7
|
+
const RC_LOW = [
|
|
8
|
+
0x00000001, 0x00008082, 0x0000808a, 0x80008000, 0x0000808b, 0x80000001,
|
|
9
|
+
0x80008081, 0x00008009, 0x0000008a, 0x00000088, 0x80008009, 0x8000000a,
|
|
10
|
+
0x8000808b, 0x0000008b, 0x00008089, 0x00008003, 0x00008002, 0x00000080,
|
|
11
|
+
0x0000800a, 0x8000000a, 0x80008081, 0x00008080, 0x80000001, 0x80008008,
|
|
12
|
+
];
|
|
13
|
+
const RC_HIGH = [
|
|
14
|
+
0x00000000, 0x00000000, 0x80000000, 0x80000000, 0x00000000, 0x00000000,
|
|
15
|
+
0x80000000, 0x80000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
16
|
+
0x00000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000,
|
|
17
|
+
0x00000000, 0x80000000, 0x80000000, 0x80000000, 0x00000000, 0x80000000,
|
|
18
|
+
];
|
|
19
|
+
const ROTC = [
|
|
20
|
+
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
|
|
21
|
+
27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
|
|
22
|
+
];
|
|
23
|
+
const PILN = [
|
|
24
|
+
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
|
|
25
|
+
15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
|
|
26
|
+
];
|
|
27
|
+
function keccakF1600(s) {
|
|
28
|
+
const bc = new Uint32Array(10);
|
|
29
|
+
for (let round = 0; round < KECCAK_ROUNDS; round++) {
|
|
30
|
+
for (let i = 0; i < 10; i += 2) {
|
|
31
|
+
bc[i] = s[i] ^ s[i + 10] ^ s[i + 20] ^ s[i + 30] ^ s[i + 40];
|
|
32
|
+
bc[i + 1] = s[i + 1] ^ s[i + 11] ^ s[i + 21] ^ s[i + 31] ^ s[i + 41];
|
|
33
|
+
}
|
|
34
|
+
for (let i = 0; i < 10; i += 2) {
|
|
35
|
+
const prev = (i + 8) % 10;
|
|
36
|
+
const next = (i + 2) % 10;
|
|
37
|
+
const dLow = bc[prev] ^ ((bc[next] << 1) | (bc[next + 1] >>> 31));
|
|
38
|
+
const dHigh = bc[prev + 1] ^ ((bc[next + 1] << 1) | (bc[next] >>> 31));
|
|
39
|
+
for (let j = 0; j < 50; j += 10) {
|
|
40
|
+
s[j + i] ^= dLow;
|
|
41
|
+
s[j + i + 1] ^= dHigh;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
let tLow = s[2];
|
|
45
|
+
let tHigh = s[3];
|
|
46
|
+
for (let i = 0; i < 24; i++) {
|
|
47
|
+
const j = PILN[i] * 2;
|
|
48
|
+
const r = ROTC[i];
|
|
49
|
+
const nLow = s[j];
|
|
50
|
+
const nHigh = s[j + 1];
|
|
51
|
+
if (r < 32) {
|
|
52
|
+
s[j] = (tLow << r) | (tHigh >>> (32 - r));
|
|
53
|
+
s[j + 1] = (tHigh << r) | (tLow >>> (32 - r));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const sShift = r - 32;
|
|
57
|
+
s[j] = (tHigh << sShift) | (tLow >>> (32 - sShift));
|
|
58
|
+
s[j + 1] = (tLow << sShift) | (tHigh >>> (32 - sShift));
|
|
59
|
+
}
|
|
60
|
+
tLow = nLow;
|
|
61
|
+
tHigh = nHigh;
|
|
62
|
+
}
|
|
63
|
+
for (let j = 0; j < 50; j += 10) {
|
|
64
|
+
for (let i = 0; i < 10; i++)
|
|
65
|
+
bc[i] = s[j + i];
|
|
66
|
+
for (let i = 0; i < 10; i += 2) {
|
|
67
|
+
s[j + i] ^= ~bc[(i + 2) % 10] & bc[(i + 4) % 10];
|
|
68
|
+
s[j + i + 1] ^= ~bc[(i + 3) % 10] & bc[(i + 5) % 10];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
s[0] ^= RC_LOW[round];
|
|
72
|
+
s[1] ^= RC_HIGH[round];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function keccak256Bytes(input) {
|
|
76
|
+
const rate = 136;
|
|
77
|
+
const state = new Uint32Array(50);
|
|
78
|
+
const block = new Uint8Array(rate);
|
|
79
|
+
let offset = 0;
|
|
80
|
+
while (offset + rate <= input.length) {
|
|
81
|
+
for (let i = 0; i < rate; i += 4) {
|
|
82
|
+
state[i >> 2] ^=
|
|
83
|
+
input[offset + i] |
|
|
84
|
+
(input[offset + i + 1] << 8) |
|
|
85
|
+
(input[offset + i + 2] << 16) |
|
|
86
|
+
(input[offset + i + 3] << 24);
|
|
87
|
+
}
|
|
88
|
+
keccakF1600(state);
|
|
89
|
+
offset += rate;
|
|
90
|
+
}
|
|
91
|
+
const rem = input.length - offset;
|
|
92
|
+
block.fill(0);
|
|
93
|
+
block.set(input.subarray(offset), 0);
|
|
94
|
+
block[rem] ^= 0x01;
|
|
95
|
+
block[rate - 1] ^= 0x80;
|
|
96
|
+
for (let i = 0; i < rate; i += 4) {
|
|
97
|
+
state[i >> 2] ^=
|
|
98
|
+
block[i] | (block[i + 1] << 8) | (block[i + 2] << 16) | (block[i + 3] << 24);
|
|
99
|
+
}
|
|
100
|
+
keccakF1600(state);
|
|
101
|
+
const out = new Uint8Array(32);
|
|
102
|
+
for (let i = 0; i < 8; i++) {
|
|
103
|
+
const w = state[i];
|
|
104
|
+
out[i * 4] = w & 0xff;
|
|
105
|
+
out[i * 4 + 1] = (w >>> 8) & 0xff;
|
|
106
|
+
out[i * 4 + 2] = (w >>> 16) & 0xff;
|
|
107
|
+
out[i * 4 + 3] = (w >>> 24) & 0xff;
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
function toEip55Address(lowerHexNoPrefix) {
|
|
112
|
+
const clean = lowerHexNoPrefix.toLowerCase();
|
|
113
|
+
const hash = keccak256Bytes(new TextEncoder().encode(clean));
|
|
114
|
+
let out = "0x";
|
|
115
|
+
for (let i = 0; i < 40; i++) {
|
|
116
|
+
const nibble = (hash[i >> 1] >> (i % 2 === 0 ? 4 : 0)) & 0x0f;
|
|
117
|
+
out += nibble >= 8 ? clean[i].toUpperCase() : clean[i];
|
|
118
|
+
}
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create a browser-clean `EvmSwarmSigner` from a `0x`-prefixed 32-byte hex private key.
|
|
123
|
+
* Derives the EIP-55 address and EIP-191 `personal_sign` signatures without requiring
|
|
124
|
+
* optional peer `viem` at module load, and uses `viem` dynamically if `sendTransaction` is called.
|
|
125
|
+
*/
|
|
126
|
+
export function fromEvmPrivateKey(privateKeyHex, opts = {}) {
|
|
127
|
+
if (typeof privateKeyHex !== "string" || !/^0x[0-9a-fA-F]{64}$/.test(privateKeyHex)) {
|
|
128
|
+
throw new SwarmClientError({
|
|
129
|
+
code: "INVALID_ARGUMENT",
|
|
130
|
+
operation: "signer.fromEvmPrivateKey",
|
|
131
|
+
message: "Expected a 0x-prefixed 32-byte hex EVM private key",
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const secretBytes = Buffer.from(privateKeyHex.slice(2), "hex");
|
|
135
|
+
const normalizedKey = `0x${privateKeyHex.slice(2).toLowerCase()}`;
|
|
136
|
+
let rawAddressHex;
|
|
137
|
+
try {
|
|
138
|
+
const probe = Secp256k1Program.createInstructionWithPrivateKey({
|
|
139
|
+
privateKey: secretBytes,
|
|
140
|
+
message: new Uint8Array(0),
|
|
141
|
+
});
|
|
142
|
+
const addrOffset = probe.data.readUInt16LE(4);
|
|
143
|
+
rawAddressHex = Buffer.from(probe.data.subarray(addrOffset, addrOffset + 20)).toString("hex");
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
throw new SwarmClientError({
|
|
147
|
+
code: "INVALID_ARGUMENT",
|
|
148
|
+
operation: "signer.fromEvmPrivateKey",
|
|
149
|
+
message: "Invalid secp256k1 private key",
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const address = toEip55Address(rawAddressHex);
|
|
153
|
+
return redactSigner({
|
|
154
|
+
chain: "evm",
|
|
155
|
+
address,
|
|
156
|
+
...(opts.chainId !== undefined ? { chainId: opts.chainId } : {}),
|
|
157
|
+
async signMessage(message) {
|
|
158
|
+
const msgBytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
159
|
+
const prefix = new TextEncoder().encode(`\x19Ethereum Signed Message:\n${msgBytes.length}`);
|
|
160
|
+
const payload = new Uint8Array(prefix.length + msgBytes.length);
|
|
161
|
+
payload.set(prefix, 0);
|
|
162
|
+
payload.set(msgBytes, prefix.length);
|
|
163
|
+
const ix = Secp256k1Program.createInstructionWithPrivateKey({
|
|
164
|
+
privateKey: secretBytes,
|
|
165
|
+
message: payload,
|
|
166
|
+
});
|
|
167
|
+
const sigOffset = ix.data.readUInt16LE(1);
|
|
168
|
+
const rsHex = Buffer.from(ix.data.subarray(sigOffset, sigOffset + 64)).toString("hex");
|
|
169
|
+
const vHex = (ix.data[sigOffset + 64] + 27).toString(16).padStart(2, "0");
|
|
170
|
+
return `0x${rsHex}${vHex}`;
|
|
171
|
+
},
|
|
172
|
+
async sendTransaction(call) {
|
|
173
|
+
if (!opts.rpcUrl) {
|
|
174
|
+
throw new SwarmClientError({
|
|
175
|
+
code: "INVALID_ARGUMENT",
|
|
176
|
+
operation: "signer.sendTransaction",
|
|
177
|
+
message: "rpcUrl is required in fromEvmPrivateKey options to broadcast EVM transactions",
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
const [{ createWalletClient, http }, { privateKeyToAccount }] = await Promise.all([
|
|
181
|
+
import("viem"),
|
|
182
|
+
import("viem/accounts"),
|
|
183
|
+
]);
|
|
184
|
+
const account = privateKeyToAccount(normalizedKey);
|
|
185
|
+
const chainId = call.chainId ?? opts.chainId ?? 84532;
|
|
186
|
+
const client = createWalletClient({
|
|
187
|
+
account,
|
|
188
|
+
transport: http(opts.rpcUrl),
|
|
189
|
+
});
|
|
190
|
+
return client.sendTransaction({
|
|
191
|
+
account,
|
|
192
|
+
chain: {
|
|
193
|
+
id: chainId,
|
|
194
|
+
name: `eip155:${chainId}`,
|
|
195
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
196
|
+
rpcUrls: { default: { http: [opts.rpcUrl] } },
|
|
197
|
+
},
|
|
198
|
+
to: call.to,
|
|
199
|
+
data: call.data,
|
|
200
|
+
value: call.value ?? 0n,
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Adapt a `viem` / `wagmi` `WalletClient` into a browser-clean `EvmSwarmSigner`.
|
|
207
|
+
*/
|
|
208
|
+
export function fromEvmWalletClient(walletClient) {
|
|
209
|
+
const rawAccount = walletClient?.account;
|
|
210
|
+
const rawAddress = typeof rawAccount === "string"
|
|
211
|
+
? rawAccount
|
|
212
|
+
: rawAccount && typeof rawAccount === "object"
|
|
213
|
+
? rawAccount.address
|
|
214
|
+
: undefined;
|
|
215
|
+
if (!rawAddress || !/^0x[0-9a-fA-F]{40}$/.test(rawAddress)) {
|
|
216
|
+
throw new SwarmClientError({
|
|
217
|
+
code: "INVALID_ARGUMENT",
|
|
218
|
+
operation: "signer.fromEvmWalletClient",
|
|
219
|
+
message: "WalletClient has no connected account address",
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const address = toEip55Address(rawAddress.slice(2));
|
|
223
|
+
const chainId = walletClient.chain?.id;
|
|
224
|
+
const signer = {
|
|
225
|
+
chain: "evm",
|
|
226
|
+
address,
|
|
227
|
+
...(chainId !== undefined ? { chainId } : {}),
|
|
228
|
+
async signMessage(message) {
|
|
229
|
+
return walletClient.signMessage({
|
|
230
|
+
account: rawAccount ?? address,
|
|
231
|
+
message: typeof message === "string" ? message : { raw: message },
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
...(walletClient.sendTransaction
|
|
235
|
+
? {
|
|
236
|
+
async sendTransaction(call) {
|
|
237
|
+
return walletClient.sendTransaction({
|
|
238
|
+
account: rawAccount ?? address,
|
|
239
|
+
to: call.to,
|
|
240
|
+
data: call.data,
|
|
241
|
+
value: call.value ?? 0n,
|
|
242
|
+
...(call.chainId ? { chainId: call.chainId } : {}),
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
}
|
|
246
|
+
: {}),
|
|
247
|
+
};
|
|
248
|
+
return redactSigner(signer);
|
|
249
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { redactSigner, type EvmCallRequest, type EvmSwarmSigner, type SolanaSwarmSigner, type SwarmSigner, } from "./types.js";
|
|
2
|
+
export { fromSolanaKeypair, fromSolanaSecretKey, fromSolanaWalletAdapter, type SolanaWalletAdapterLike, } from "./solana.js";
|
|
3
|
+
export { fromEvmPrivateKey, fromEvmWalletClient, type EvmPrivateKeyOptions, type EvmWalletClientLike, } from "./evm.js";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Keypair, VersionedTransaction, type PublicKey, type Transaction } from "@solana/web3.js";
|
|
2
|
+
import { type SolanaSwarmSigner } from "./types.js";
|
|
3
|
+
export interface SolanaWalletAdapterLike {
|
|
4
|
+
publicKey: PublicKey | {
|
|
5
|
+
toBase58(): string;
|
|
6
|
+
} | null | undefined;
|
|
7
|
+
signMessage?: (message: Uint8Array) => Promise<Uint8Array>;
|
|
8
|
+
signTransaction?: <T extends Transaction | VersionedTransaction>(tx: T) => Promise<T>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create a browser-clean `SolanaSwarmSigner` from a `@solana/web3.js` `Keypair`.
|
|
12
|
+
* Secret key bytes stay trapped inside a private closure and are redacted from logs/JSON.
|
|
13
|
+
*/
|
|
14
|
+
export declare function fromSolanaKeypair(keypair: Keypair): SolanaSwarmSigner;
|
|
15
|
+
/**
|
|
16
|
+
* Create a `SolanaSwarmSigner` from a 64-byte `Uint8Array`, base58-encoded secret key,
|
|
17
|
+
* or JSON `[u8, ...]` array string (matching `solana-keygen` format).
|
|
18
|
+
*/
|
|
19
|
+
export declare function fromSolanaSecretKey(secret: Uint8Array | string): SolanaSwarmSigner;
|
|
20
|
+
/**
|
|
21
|
+
* Adapt a browser Solana Wallet Adapter (`@solana/wallet-adapter-base` compatible)
|
|
22
|
+
* into a `SolanaSwarmSigner`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function fromSolanaWalletAdapter(adapter: SolanaWalletAdapterLike): SolanaSwarmSigner;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import bs58 from "bs58";
|
|
2
|
+
import { Ed25519Program, Keypair, VersionedTransaction, } from "@solana/web3.js";
|
|
3
|
+
import { SwarmClientError } from "../errors.js";
|
|
4
|
+
import { redactSigner } from "./types.js";
|
|
5
|
+
const ED25519_PKCS8_PREFIX = new Uint8Array([
|
|
6
|
+
0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
|
|
7
|
+
0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,
|
|
8
|
+
]);
|
|
9
|
+
async function signEd25519Detached(secretKey64, message) {
|
|
10
|
+
if (globalThis.crypto?.subtle) {
|
|
11
|
+
try {
|
|
12
|
+
const pkcs8 = new Uint8Array(48);
|
|
13
|
+
pkcs8.set(ED25519_PKCS8_PREFIX, 0);
|
|
14
|
+
pkcs8.set(secretKey64.subarray(0, 32), 16);
|
|
15
|
+
const key = await globalThis.crypto.subtle.importKey("pkcs8", pkcs8, { name: "Ed25519" }, false, ["sign"]);
|
|
16
|
+
pkcs8.fill(0);
|
|
17
|
+
const payload = new Uint8Array(message.byteLength);
|
|
18
|
+
payload.set(message);
|
|
19
|
+
const sig = await globalThis.crypto.subtle.sign("Ed25519", key, payload);
|
|
20
|
+
return new Uint8Array(sig);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Fall back to @solana/web3.js Ed25519Program below on runtimes without WebCrypto Ed25519.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const ix = Ed25519Program.createInstructionWithPrivateKey({
|
|
27
|
+
privateKey: secretKey64,
|
|
28
|
+
message,
|
|
29
|
+
});
|
|
30
|
+
const sigOffset = ix.data[2] | (ix.data[3] << 8);
|
|
31
|
+
return new Uint8Array(ix.data.subarray(sigOffset, sigOffset + 64));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create a browser-clean `SolanaSwarmSigner` from a `@solana/web3.js` `Keypair`.
|
|
35
|
+
* Secret key bytes stay trapped inside a private closure and are redacted from logs/JSON.
|
|
36
|
+
*/
|
|
37
|
+
export function fromSolanaKeypair(keypair) {
|
|
38
|
+
if (!keypair?.secretKey || keypair.secretKey.length !== 64) {
|
|
39
|
+
throw new SwarmClientError({
|
|
40
|
+
code: "INVALID_ARGUMENT",
|
|
41
|
+
operation: "signer.fromSolanaKeypair",
|
|
42
|
+
message: "Expected a valid 64-byte Solana Keypair",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const secretBytes = new Uint8Array(keypair.secretKey);
|
|
46
|
+
const address = keypair.publicKey.toBase58();
|
|
47
|
+
return redactSigner({
|
|
48
|
+
chain: "solana",
|
|
49
|
+
address,
|
|
50
|
+
async signMessage(message) {
|
|
51
|
+
return signEd25519Detached(secretBytes, message);
|
|
52
|
+
},
|
|
53
|
+
async signTransaction(tx) {
|
|
54
|
+
const kp = Keypair.fromSecretKey(secretBytes);
|
|
55
|
+
if (tx instanceof VersionedTransaction) {
|
|
56
|
+
tx.sign([kp]);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
tx.partialSign(kp);
|
|
60
|
+
}
|
|
61
|
+
return tx;
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a `SolanaSwarmSigner` from a 64-byte `Uint8Array`, base58-encoded secret key,
|
|
67
|
+
* or JSON `[u8, ...]` array string (matching `solana-keygen` format).
|
|
68
|
+
*/
|
|
69
|
+
export function fromSolanaSecretKey(secret) {
|
|
70
|
+
let bytes;
|
|
71
|
+
if (secret instanceof Uint8Array) {
|
|
72
|
+
bytes = new Uint8Array(secret);
|
|
73
|
+
}
|
|
74
|
+
else if (typeof secret === "string") {
|
|
75
|
+
const trimmed = secret.trim();
|
|
76
|
+
if (!trimmed) {
|
|
77
|
+
throw new SwarmClientError({
|
|
78
|
+
code: "INVALID_ARGUMENT",
|
|
79
|
+
operation: "signer.fromSolanaSecretKey",
|
|
80
|
+
message: "Solana secret key string must not be empty",
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (trimmed.startsWith("[")) {
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = JSON.parse(trimmed);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
throw new SwarmClientError({
|
|
90
|
+
code: "INVALID_ARGUMENT",
|
|
91
|
+
operation: "signer.fromSolanaSecretKey",
|
|
92
|
+
message: "Invalid JSON array for Solana secret key",
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (!Array.isArray(parsed) ||
|
|
96
|
+
parsed.length !== 64 ||
|
|
97
|
+
parsed.some((n) => typeof n !== "number" || !Number.isInteger(n) || n < 0 || n > 255)) {
|
|
98
|
+
throw new SwarmClientError({
|
|
99
|
+
code: "INVALID_ARGUMENT",
|
|
100
|
+
operation: "signer.fromSolanaSecretKey",
|
|
101
|
+
message: "Solana JSON secret key must be an array of 64 byte integers (0..255)",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
bytes = Uint8Array.from(parsed);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
try {
|
|
108
|
+
bytes = bs58.decode(trimmed);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw new SwarmClientError({
|
|
112
|
+
code: "INVALID_ARGUMENT",
|
|
113
|
+
operation: "signer.fromSolanaSecretKey",
|
|
114
|
+
message: "Invalid base58 Solana secret key",
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new SwarmClientError({
|
|
121
|
+
code: "INVALID_ARGUMENT",
|
|
122
|
+
operation: "signer.fromSolanaSecretKey",
|
|
123
|
+
message: "Expected a 64-byte Uint8Array, base58 string, or JSON byte array string",
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
if (bytes.length !== 64) {
|
|
127
|
+
throw new SwarmClientError({
|
|
128
|
+
code: "INVALID_ARGUMENT",
|
|
129
|
+
operation: "signer.fromSolanaSecretKey",
|
|
130
|
+
message: `Expected 64-byte Solana secret key, received ${bytes.length} bytes`,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
let kp;
|
|
134
|
+
try {
|
|
135
|
+
kp = Keypair.fromSecretKey(bytes);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
throw new SwarmClientError({
|
|
139
|
+
code: "INVALID_ARGUMENT",
|
|
140
|
+
operation: "signer.fromSolanaSecretKey",
|
|
141
|
+
message: "Invalid Solana Ed25519 secret key bytes",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return fromSolanaKeypair(kp);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Adapt a browser Solana Wallet Adapter (`@solana/wallet-adapter-base` compatible)
|
|
148
|
+
* into a `SolanaSwarmSigner`.
|
|
149
|
+
*/
|
|
150
|
+
export function fromSolanaWalletAdapter(adapter) {
|
|
151
|
+
if (!adapter?.publicKey) {
|
|
152
|
+
throw new SwarmClientError({
|
|
153
|
+
code: "INVALID_ARGUMENT",
|
|
154
|
+
operation: "signer.fromSolanaWalletAdapter",
|
|
155
|
+
message: "Wallet adapter has no connected publicKey",
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const address = adapter.publicKey.toBase58();
|
|
159
|
+
return redactSigner({
|
|
160
|
+
chain: "solana",
|
|
161
|
+
address,
|
|
162
|
+
async signMessage(message) {
|
|
163
|
+
if (!adapter.signMessage) {
|
|
164
|
+
throw new SwarmClientError({
|
|
165
|
+
code: "INVALID_ARGUMENT",
|
|
166
|
+
operation: "signer.signMessage",
|
|
167
|
+
message: "Connected Solana wallet adapter does not support signMessage",
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return adapter.signMessage(message);
|
|
171
|
+
},
|
|
172
|
+
async signTransaction(tx) {
|
|
173
|
+
if (!adapter.signTransaction) {
|
|
174
|
+
throw new SwarmClientError({
|
|
175
|
+
code: "INVALID_ARGUMENT",
|
|
176
|
+
operation: "signer.signTransaction",
|
|
177
|
+
message: "Connected Solana wallet adapter does not support signTransaction",
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return adapter.signTransaction(tx);
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Transaction, VersionedTransaction } from "@solana/web3.js";
|
|
2
|
+
export interface SolanaSwarmSigner {
|
|
3
|
+
readonly chain: "solana";
|
|
4
|
+
readonly address: string;
|
|
5
|
+
signMessage(message: Uint8Array): Promise<Uint8Array>;
|
|
6
|
+
signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T>;
|
|
7
|
+
}
|
|
8
|
+
export interface EvmCallRequest {
|
|
9
|
+
to: `0x${string}`;
|
|
10
|
+
data: `0x${string}`;
|
|
11
|
+
value?: bigint;
|
|
12
|
+
chainId?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface EvmSwarmSigner {
|
|
15
|
+
readonly chain: "evm";
|
|
16
|
+
readonly address: `0x${string}`;
|
|
17
|
+
readonly chainId?: number;
|
|
18
|
+
signMessage(message: string | Uint8Array): Promise<`0x${string}`>;
|
|
19
|
+
sendTransaction?(call: EvmCallRequest): Promise<`0x${string}`>;
|
|
20
|
+
}
|
|
21
|
+
export type SwarmSigner = SolanaSwarmSigner | EvmSwarmSigner;
|
|
22
|
+
/**
|
|
23
|
+
* C6 Redaction: attach safe serialization and inspection hooks while freezing
|
|
24
|
+
* the public signer surface so secret key material can never leak into JSON,
|
|
25
|
+
* string formatting, or `util.inspect` logs.
|
|
26
|
+
*/
|
|
27
|
+
export declare function redactSigner<T extends {
|
|
28
|
+
readonly chain: string;
|
|
29
|
+
readonly address: string;
|
|
30
|
+
}>(signer: T): T;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const NODE_INSPECT_CUSTOM = Symbol.for("nodejs.util.inspect.custom");
|
|
2
|
+
/**
|
|
3
|
+
* C6 Redaction: attach safe serialization and inspection hooks while freezing
|
|
4
|
+
* the public signer surface so secret key material can never leak into JSON,
|
|
5
|
+
* string formatting, or `util.inspect` logs.
|
|
6
|
+
*/
|
|
7
|
+
export function redactSigner(signer) {
|
|
8
|
+
const { chain, address } = signer;
|
|
9
|
+
const label = `SwarmSigner(${chain}:${address})`;
|
|
10
|
+
Object.defineProperties(signer, {
|
|
11
|
+
toJSON: {
|
|
12
|
+
value: () => ({ chain, address }),
|
|
13
|
+
enumerable: false,
|
|
14
|
+
configurable: false,
|
|
15
|
+
writable: false,
|
|
16
|
+
},
|
|
17
|
+
toString: {
|
|
18
|
+
value: () => label,
|
|
19
|
+
enumerable: false,
|
|
20
|
+
configurable: false,
|
|
21
|
+
writable: false,
|
|
22
|
+
},
|
|
23
|
+
[NODE_INSPECT_CUSTOM]: {
|
|
24
|
+
value: () => label,
|
|
25
|
+
enumerable: false,
|
|
26
|
+
configurable: false,
|
|
27
|
+
writable: false,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
return Object.freeze(signer);
|
|
31
|
+
}
|
package/dist/swarm/index.d.ts
CHANGED
|
@@ -78,6 +78,7 @@ export declare class SwarmClient {
|
|
|
78
78
|
results: SearchHit[];
|
|
79
79
|
corpus_size: number;
|
|
80
80
|
}>;
|
|
81
|
+
earningCandidates<T = unknown>(limit?: number): Promise<T>;
|
|
81
82
|
reputation(wallet: string, network?: string): Promise<AgentReputation>;
|
|
82
83
|
leaderboard(limit?: number): Promise<EigenTrustRecord[]>;
|
|
83
84
|
toolCount(): Promise<number | null>;
|
package/dist/swarm/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpClient } from "../http.js";
|
|
2
|
+
import { CLIENT_VERSION } from "../version.js";
|
|
2
3
|
function parseRpc(body) {
|
|
3
4
|
const trimmed = body.trimStart();
|
|
4
5
|
if (trimmed.startsWith("{")) {
|
|
@@ -26,7 +27,9 @@ export class SwarmClient {
|
|
|
26
27
|
baseUrl;
|
|
27
28
|
constructor(options) {
|
|
28
29
|
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
29
|
-
this.fetchImpl =
|
|
30
|
+
this.fetchImpl = options.fetch
|
|
31
|
+
? (input, init) => options.fetch(input, init)
|
|
32
|
+
: globalThis.fetch.bind(globalThis);
|
|
30
33
|
this.http = new HttpClient(options);
|
|
31
34
|
}
|
|
32
35
|
async listings() { return this.http.get("/internal/listings"); }
|
|
@@ -37,6 +40,10 @@ export class SwarmClient {
|
|
|
37
40
|
params.set("limit", String(limit));
|
|
38
41
|
return this.http.get(`/internal/mcp/search?${params.toString()}`);
|
|
39
42
|
}
|
|
43
|
+
async earningCandidates(limit) {
|
|
44
|
+
const qs = limit !== undefined ? `?limit=${encodeURIComponent(String(limit))}` : "";
|
|
45
|
+
return this.http.get(`/internal/mcp/earning-candidates${qs}`);
|
|
46
|
+
}
|
|
40
47
|
reputation(wallet, network = "mainnet") {
|
|
41
48
|
if (!wallet)
|
|
42
49
|
return Promise.reject(new TypeError("wallet is required"));
|
|
@@ -49,7 +56,7 @@ export class SwarmClient {
|
|
|
49
56
|
async toolCount() {
|
|
50
57
|
const headers = { "Content-Type": "application/json", Accept: "application/json, text/event-stream" };
|
|
51
58
|
try {
|
|
52
|
-
const init = await this.fetchImpl(`${this.baseUrl}/mcp`, { method: "POST", headers, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-03-26", capabilities: {}, clientInfo: { name: "swarm-client", version:
|
|
59
|
+
const init = await this.fetchImpl(`${this.baseUrl}/mcp`, { method: "POST", headers, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-03-26", capabilities: {}, clientInfo: { name: "swarm-client", version: CLIENT_VERSION } } }) });
|
|
53
60
|
if (!init.ok)
|
|
54
61
|
return null;
|
|
55
62
|
const session = init.headers.get("mcp-session-id");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CLIENT_VERSION = "0.3.0";
|
package/dist/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CLIENT_VERSION = "0.3.0";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swarm-tips/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Typed browser and Node.js clients for Swarm Tips, Shillbot, and Coordination Game",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -72,8 +72,10 @@
|
|
|
72
72
|
"./package.json": "./package.json"
|
|
73
73
|
},
|
|
74
74
|
"bin": {
|
|
75
|
-
"swarm
|
|
76
|
-
"
|
|
75
|
+
"swarm": "./dist/cli/swarm.js",
|
|
76
|
+
"client": "./dist/cli/swarm.js",
|
|
77
|
+
"swarm-tx": "./dist/cli/swarm-tx.js",
|
|
78
|
+
"vow-verify": "./dist/cli/vow-verify.js"
|
|
77
79
|
},
|
|
78
80
|
"files": [
|
|
79
81
|
"dist",
|