plotlink-ows 1.0.16 → 1.0.18
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/app/routes/settings.ts +33 -21
- package/package.json +1 -1
package/app/routes/settings.ts
CHANGED
|
@@ -8,6 +8,21 @@ import { db } from "../db";
|
|
|
8
8
|
import {
|
|
9
9
|
signMessage as owsSignMsg,
|
|
10
10
|
} from "@open-wallet-standard/core";
|
|
11
|
+
import { CONFIG_DIR } from "../lib/paths";
|
|
12
|
+
import fs from "fs";
|
|
13
|
+
import path from "path";
|
|
14
|
+
|
|
15
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
|
|
16
|
+
|
|
17
|
+
function readConfig(): Record<string, unknown> {
|
|
18
|
+
try { return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8")); } catch { return {}; }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function writeConfig(updates: Record<string, unknown>) {
|
|
22
|
+
const config = readConfig();
|
|
23
|
+
Object.assign(config, updates);
|
|
24
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
25
|
+
}
|
|
11
26
|
|
|
12
27
|
const ERC_8004 = "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432" as const;
|
|
13
28
|
const rpcUrl = process.env.NEXT_PUBLIC_RPC_URL || "https://mainnet.base.org";
|
|
@@ -133,12 +148,8 @@ settings.post("/register-agent", async (c) => {
|
|
|
133
148
|
return c.json({ error: "Transaction succeeded but Registered event not found" }, 500);
|
|
134
149
|
}
|
|
135
150
|
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
where: { key: "agent_id" },
|
|
139
|
-
update: { value: String(agentId) },
|
|
140
|
-
create: { key: "agent_id", value: String(agentId) },
|
|
141
|
-
});
|
|
151
|
+
// Cache agentId in config.json (survives npx reinstalls, no Prisma dependency)
|
|
152
|
+
writeConfig({ agentId });
|
|
142
153
|
|
|
143
154
|
return c.json({
|
|
144
155
|
agentId,
|
|
@@ -161,6 +172,13 @@ settings.get("/link-status", async (c) => {
|
|
|
161
172
|
const address = getBaseAddress(wallet);
|
|
162
173
|
if (!address) return c.json({ linked: false, error: "No EVM address" });
|
|
163
174
|
|
|
175
|
+
// Check config.json cache first (survives npx reinstalls + RPC rate limits)
|
|
176
|
+
const config = readConfig();
|
|
177
|
+
if (config.agentId) {
|
|
178
|
+
return c.json({ linked: true, agentId: Number(config.agentId), owsWallet: address });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// RPC: try agentIdByWallet (for bound wallets)
|
|
164
182
|
try {
|
|
165
183
|
const agentId = await publicClient.readContract({
|
|
166
184
|
address: ERC_8004,
|
|
@@ -170,21 +188,12 @@ settings.get("/link-status", async (c) => {
|
|
|
170
188
|
}) as bigint;
|
|
171
189
|
|
|
172
190
|
if (agentId > 0n) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
try {
|
|
176
|
-
owner = await publicClient.readContract({
|
|
177
|
-
address: ERC_8004,
|
|
178
|
-
abi: [{ type: "function", name: "ownerOf", stateMutability: "view", inputs: [{ name: "tokenId", type: "uint256" }], outputs: [{ name: "", type: "address" }] }] as const,
|
|
179
|
-
functionName: "ownerOf",
|
|
180
|
-
args: [agentId],
|
|
181
|
-
}) as string;
|
|
182
|
-
} catch { /* best effort */ }
|
|
183
|
-
return c.json({ linked: true, agentId: Number(agentId), owsWallet: address, owner });
|
|
191
|
+
writeConfig({ agentId: Number(agentId) });
|
|
192
|
+
return c.json({ linked: true, agentId: Number(agentId), owsWallet: address });
|
|
184
193
|
}
|
|
185
194
|
} catch { /* agentIdByWallet may revert if not bound */ }
|
|
186
195
|
|
|
187
|
-
//
|
|
196
|
+
// RPC fallback: check balanceOf (for owned but unbound NFTs)
|
|
188
197
|
try {
|
|
189
198
|
const balance = await publicClient.readContract({
|
|
190
199
|
address: ERC_8004,
|
|
@@ -194,7 +203,7 @@ settings.get("/link-status", async (c) => {
|
|
|
194
203
|
}) as bigint;
|
|
195
204
|
|
|
196
205
|
if (balance > 0n) {
|
|
197
|
-
// Try to get token ID
|
|
206
|
+
// Try to get token ID
|
|
198
207
|
let agentId: number | undefined;
|
|
199
208
|
try {
|
|
200
209
|
const tokenId = await publicClient.readContract({
|
|
@@ -204,11 +213,14 @@ settings.get("/link-status", async (c) => {
|
|
|
204
213
|
args: [address as `0x${string}`, 0n],
|
|
205
214
|
}) as bigint;
|
|
206
215
|
agentId = Number(tokenId);
|
|
207
|
-
} catch { /* ERC-721 Enumerable not supported
|
|
216
|
+
} catch { /* ERC-721 Enumerable not supported */ }
|
|
208
217
|
|
|
218
|
+
if (agentId !== undefined) {
|
|
219
|
+
writeConfig({ agentId });
|
|
220
|
+
}
|
|
209
221
|
return c.json({ linked: true, agentId, owsWallet: address });
|
|
210
222
|
}
|
|
211
|
-
} catch { /*
|
|
223
|
+
} catch { /* RPC failed — rate limited or unavailable */ }
|
|
212
224
|
|
|
213
225
|
return c.json({ linked: false, owsWallet: address });
|
|
214
226
|
} catch (err: unknown) {
|