plotlink-ows 1.0.15 → 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 +44 -27
- 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,16 +203,24 @@ settings.get("/link-status", async (c) => {
|
|
|
194
203
|
}) as bigint;
|
|
195
204
|
|
|
196
205
|
if (balance > 0n) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
206
|
+
// Try to get token ID
|
|
207
|
+
let agentId: number | undefined;
|
|
208
|
+
try {
|
|
209
|
+
const tokenId = await publicClient.readContract({
|
|
210
|
+
address: ERC_8004,
|
|
211
|
+
abi: [{ type: "function", name: "tokenOfOwnerByIndex", stateMutability: "view", inputs: [{ name: "owner", type: "address" }, { name: "index", type: "uint256" }], outputs: [{ name: "", type: "uint256" }] }] as const,
|
|
212
|
+
functionName: "tokenOfOwnerByIndex",
|
|
213
|
+
args: [address as `0x${string}`, 0n],
|
|
214
|
+
}) as bigint;
|
|
215
|
+
agentId = Number(tokenId);
|
|
216
|
+
} catch { /* ERC-721 Enumerable not supported */ }
|
|
217
|
+
|
|
218
|
+
if (agentId !== undefined) {
|
|
219
|
+
writeConfig({ agentId });
|
|
220
|
+
}
|
|
221
|
+
return c.json({ linked: true, agentId, owsWallet: address });
|
|
205
222
|
}
|
|
206
|
-
} catch { /*
|
|
223
|
+
} catch { /* RPC failed — rate limited or unavailable */ }
|
|
207
224
|
|
|
208
225
|
return c.json({ linked: false, owsWallet: address });
|
|
209
226
|
} catch (err: unknown) {
|