@oracle-agent/oracle 0.1.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/CONTRIBUTING.md +98 -0
- package/LICENSE +202 -0
- package/README.md +384 -0
- package/SECURITY.md +89 -0
- package/SETUP.md +235 -0
- package/artifacts/inscription/oracle-was-here-preview.png +0 -0
- package/artifacts/inscription/oracle-was-here.svg +1 -0
- package/artifacts/specialist-packs/oracle-full-crypto.json +273 -0
- package/bin/desk-server.mjs +438 -0
- package/bin/oracle-data-mcp.mjs +603 -0
- package/bin/oracle-init.mjs +423 -0
- package/bin/oracle-public-server.mjs +36 -0
- package/bin/oracle-route.mjs +254 -0
- package/bin/oracle-scan.mjs +192 -0
- package/docs/adding-a-chain.md +229 -0
- package/docs/architecture.md +135 -0
- package/docs/profiles.md +132 -0
- package/examples/add-a-chain.mjs +65 -0
- package/examples/research-a-token.mjs +70 -0
- package/package.json +90 -0
- package/profiles/_template/SOUL.md +47 -0
- package/profiles/_template/profile.json +22 -0
- package/profiles/bitcoin-agent/SOUL.md +31 -0
- package/profiles/bitcoin-agent/profile.json +32 -0
- package/profiles/hyperliquid-agent/SOUL.md +34 -0
- package/profiles/hyperliquid-agent/profile.json +37 -0
- package/profiles/oracle/SOUL.md +65 -0
- package/profiles/oracle/profile.json +35 -0
- package/profiles/polymarket-agent/SOUL.md +35 -0
- package/profiles/polymarket-agent/profile.json +34 -0
- package/profiles/profile.schema.json +90 -0
- package/profiles/protocol-builder/SOUL.md +50 -0
- package/profiles/protocol-builder/profile.json +37 -0
- package/profiles/robinhood-agent/SOUL.md +45 -0
- package/profiles/robinhood-agent/profile.json +39 -0
- package/profiles/solana-agent/SOUL.md +37 -0
- package/profiles/solana-agent/profile.json +37 -0
- package/profiles/stable-agent/SOUL.md +43 -0
- package/profiles/stable-agent/profile.json +37 -0
- package/public/oracle-console/app.js +272 -0
- package/public/oracle-console/bitcoin-wallets.js +206 -0
- package/public/oracle-console/index.html +91 -0
- package/public/oracle-console/styles.css +239 -0
- package/public/oracle-splash/index.html +931 -0
- package/scripts/build-inscription.py +230 -0
- package/scripts/check-test-count.mjs +105 -0
- package/scripts/e2e-hl-markets.mjs +21 -0
- package/scripts/e2e-hl-perps.mjs +48 -0
- package/scripts/e2e-hypercore-staking.mjs +128 -0
- package/scripts/e2e-solana-bitcoin.mjs +183 -0
- package/scripts/public-api-scan.mjs +23 -0
- package/scripts/secret-scan.mjs +181 -0
- package/scripts/verify-v3-venues.mjs +192 -0
- package/skills/oracle-best-execution/SKILL.md +127 -0
- package/skills/oracle-bitcoin/SKILL.md +53 -0
- package/skills/oracle-chain-graphs-telegram-cards/SKILL.md +59 -0
- package/skills/oracle-circuit-breaker/SKILL.md +51 -0
- package/skills/oracle-contract-research/SKILL.md +55 -0
- package/skills/oracle-desk/SKILL.md +58 -0
- package/skills/oracle-dex-launch/SKILL.md +38 -0
- package/skills/oracle-grants/SKILL.md +69 -0
- package/skills/oracle-hypercore-staking/SKILL.md +57 -0
- package/skills/oracle-hyperliquid/SKILL.md +56 -0
- package/skills/oracle-meme-token-sniper/SKILL.md +73 -0
- package/skills/oracle-nft-gacha-launch/SKILL.md +48 -0
- package/skills/oracle-nft-mint-gas-war/SKILL.md +63 -0
- package/skills/oracle-polymarket/SKILL.md +60 -0
- package/skills/oracle-protocol-builder/SKILL.md +38 -0
- package/skills/oracle-protocol-security/SKILL.md +60 -0
- package/skills/oracle-public-product/SKILL.md +44 -0
- package/skills/oracle-receipts/SKILL.md +52 -0
- package/skills/oracle-rfq-tokenized-assets/SKILL.md +69 -0
- package/skills/oracle-smart-wallet-scanner/SKILL.md +49 -0
- package/skills/oracle-solana/SKILL.md +65 -0
- package/skills/oracle-solana-nft/SKILL.md +54 -0
- package/skills/oracle-token-research/SKILL.md +67 -0
- package/src/agent-auth.mjs +191 -0
- package/src/approval-guard.mjs +282 -0
- package/src/attestation-secret.mjs +88 -0
- package/src/audit-log.mjs +196 -0
- package/src/auto-slippage.mjs +378 -0
- package/src/capability-posture.mjs +125 -0
- package/src/chains.mjs +62 -0
- package/src/data/catalog.mjs +495 -0
- package/src/data/desk-data.mjs +623 -0
- package/src/data/http.mjs +200 -0
- package/src/data/provider-endpoint.mjs +94 -0
- package/src/data/providers/aerodrome.mjs +244 -0
- package/src/data/providers/balancer.mjs +208 -0
- package/src/data/providers/bitcoin-esplora.mjs +230 -0
- package/src/data/providers/bitcoin-meta.mjs +378 -0
- package/src/data/providers/blockscout.mjs +14 -0
- package/src/data/providers/bridges.mjs +241 -0
- package/src/data/providers/cowswap.mjs +501 -0
- package/src/data/providers/curve.mjs +200 -0
- package/src/data/providers/defillama.mjs +88 -0
- package/src/data/providers/dexscreener.mjs +43 -0
- package/src/data/providers/evm-rpc.mjs +203 -0
- package/src/data/providers/geckoterminal.mjs +34 -0
- package/src/data/providers/gmx.mjs +495 -0
- package/src/data/providers/hl-info.mjs +102 -0
- package/src/data/providers/hl-markets.mjs +210 -0
- package/src/data/providers/hl-perps.mjs +382 -0
- package/src/data/providers/hl-staking.mjs +352 -0
- package/src/data/providers/hl-ws.mjs +119 -0
- package/src/data/providers/hyperevm-dex.mjs +49 -0
- package/src/data/providers/jupiter.mjs +182 -0
- package/src/data/providers/lifi.mjs +150 -0
- package/src/data/providers/magiceden-sol.mjs +355 -0
- package/src/data/providers/morpho.mjs +173 -0
- package/src/data/providers/odos.mjs +155 -0
- package/src/data/providers/oneinch.mjs +173 -0
- package/src/data/providers/opensea-multichain.mjs +136 -0
- package/src/data/providers/opensea-nft.mjs +99 -0
- package/src/data/providers/paraswap.mjs +117 -0
- package/src/data/providers/pendle.mjs +187 -0
- package/src/data/providers/poly-public.mjs +96 -0
- package/src/data/providers/poly-ws.mjs +103 -0
- package/src/data/providers/rh-agent.mjs +59 -0
- package/src/data/providers/satflow.mjs +336 -0
- package/src/data/providers/solana-rpc.mjs +186 -0
- package/src/data/providers/uniswap-v3.mjs +303 -0
- package/src/data/providers/zerox.mjs +166 -0
- package/src/data/public-api-scan.mjs +61 -0
- package/src/data/quote-placeholder.mjs +31 -0
- package/src/exact-integer.mjs +72 -0
- package/src/exec-policy.mjs +444 -0
- package/src/flags.mjs +15 -0
- package/src/fresh-window.mjs +76 -0
- package/src/gmx-attestation.mjs +175 -0
- package/src/index.mjs +50 -0
- package/src/nft-gas-war-guard.mjs +139 -0
- package/src/onboarding/agent-keys.mjs +157 -0
- package/src/onboarding/index.mjs +18 -0
- package/src/onboarding/tiers.mjs +139 -0
- package/src/oracle-env.mjs +38 -0
- package/src/protocol-execution.mjs +84 -0
- package/src/public-api/buzz-integration.mjs +256 -0
- package/src/public-api/connect-agent.mjs +397 -0
- package/src/public-api/grants.mjs +142 -0
- package/src/public-api/http.mjs +374 -0
- package/src/public-control/aa-adapter.mjs +402 -0
- package/src/public-control/build-registry.mjs +227 -0
- package/src/public-control/bundler-client.mjs +372 -0
- package/src/public-control/grant-indexer.mjs +296 -0
- package/src/public-control/policy-render.mjs +69 -0
- package/src/public-control/policy-schema.mjs +318 -0
- package/src/public-control/runtime-config.mjs +265 -0
- package/src/public-control/session-key-model.mjs +374 -0
- package/src/public-control/session-orchestrator.mjs +412 -0
- package/src/route-attestation.mjs +132 -0
- package/src/router/best-execution.mjs +221 -0
- package/src/router/index.mjs +185 -0
- package/src/router/prepare-bridge.mjs +288 -0
- package/src/router/prepare-route.mjs +341 -0
- package/src/router/proposal.mjs +311 -0
- package/src/router/risk-classifier.mjs +119 -0
- package/src/router/route-sources.mjs +292 -0
- package/src/scanner/chains.config.mjs +381 -0
- package/src/scanner/contract.mjs +270 -0
- package/src/scanner/evm-scanner.mjs +394 -0
- package/src/scanner/index.mjs +9 -0
- package/src/scanner/v2-venue.mjs +335 -0
- package/src/scanner/v3-venue.mjs +290 -0
- package/src/scopes.mjs +44 -0
- package/src/sell-simulation.mjs +167 -0
- package/src/token-transfer-guard.mjs +188 -0
- package/src/vault-attestation.mjs +145 -0
- package/src/venues.mjs +206 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Desk HTTP surface: read-only data plane + onboarding (scoped keys only).
|
|
3
|
+
// NEVER enables wallet signing/exec. Loopback by default.
|
|
4
|
+
//
|
|
5
|
+
// MAD_DESK_PORT=8787 node bin/desk-server.mjs
|
|
6
|
+
|
|
7
|
+
import { createServer } from "node:http";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { randomBytes } from "node:crypto";
|
|
10
|
+
import { verifyMessage } from "ethers";
|
|
11
|
+
import { data, dataCall, dataHealth, dataCatalog, isExecutionProvider } from "../src/data/desk-data.mjs";
|
|
12
|
+
import { getProvider } from "../src/data/catalog.mjs";
|
|
13
|
+
import { csvEnv, env, envEnabled, envFlag } from "../src/oracle-env.mjs";
|
|
14
|
+
import {
|
|
15
|
+
listTiers,
|
|
16
|
+
onboardUser,
|
|
17
|
+
onboardAuthenticate,
|
|
18
|
+
onboardListKeys,
|
|
19
|
+
onboardRevoke,
|
|
20
|
+
} from "../src/onboarding/index.mjs";
|
|
21
|
+
|
|
22
|
+
const HOST = env("ORACLE_DATA_HOST", "MAD_DESK_HOST", "127.0.0.1");
|
|
23
|
+
const PORT = Number(env("ORACLE_DATA_PORT", "MAD_DESK_PORT", 8787));
|
|
24
|
+
const ONBOARD_ENABLED = envEnabled("ORACLE_ONBOARD_HTTP", "MAD_ONBOARD_HTTP", false);
|
|
25
|
+
const PUBLIC_API_MODE = envFlag("ORACLE_PUBLIC_API_MODE", "MAD_PUBLIC_API_MODE", false);
|
|
26
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
27
|
+
const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]);
|
|
28
|
+
const MAX_BODY_BYTES = 64 * 1024;
|
|
29
|
+
const CHALLENGE_TTL_SECONDS = 300;
|
|
30
|
+
const MAX_CHALLENGES = 10_000;
|
|
31
|
+
const walletChallenges = new Map();
|
|
32
|
+
|
|
33
|
+
if (!LOOPBACK_HOSTS.has(String(HOST).toLowerCase())) {
|
|
34
|
+
throw new Error("oracle data server only binds loopback; use 127.0.0.1 and do not expose it through a reverse proxy");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function csvSet(value) {
|
|
38
|
+
return new Set(String(value || "").split(",").map((s) => s.trim()).filter(Boolean));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function publicAllowsServerKeyProvider(provider) {
|
|
42
|
+
return csvSet(csvEnv("ORACLE_PUBLIC_ALLOW_SERVER_KEY_PROVIDERS", "MAD_PUBLIC_ALLOW_SERVER_KEY_PROVIDERS")).has(provider);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function providerNeedsServerKey(meta) {
|
|
46
|
+
return meta && meta.auth && meta.auth !== "none";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function publicCallableProviders() {
|
|
50
|
+
return dataCatalog()
|
|
51
|
+
.filter((p) => !providerNeedsServerKey(p) || publicAllowsServerKeyProvider(p.id))
|
|
52
|
+
.map((p) => p.id);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class HttpError extends Error {
|
|
56
|
+
constructor(status, code, message) {
|
|
57
|
+
super(message || code);
|
|
58
|
+
this.status = status;
|
|
59
|
+
this.code = code;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function normalizedOwner(owner) {
|
|
64
|
+
const value = String(owner || "").trim().toLowerCase();
|
|
65
|
+
if (!/^0x[0-9a-f]{40}$/.test(value)) throw new HttpError(400, "invalid-owner", "owner must be a 0x-prefixed address");
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function parseProofMessage(message) {
|
|
70
|
+
const out = {};
|
|
71
|
+
const lines = String(message || "").split(/\r?\n/).map((s) => s.trim()).filter(Boolean);
|
|
72
|
+
if (lines[0] !== "Oracle onboarding") throw new HttpError(401, "invalid-proof", "wallet proof must be an Oracle onboarding challenge");
|
|
73
|
+
for (const line of lines.slice(1)) {
|
|
74
|
+
const idx = line.indexOf(":");
|
|
75
|
+
if (idx <= 0) continue;
|
|
76
|
+
out[line.slice(0, idx).trim()] = line.slice(idx + 1).trim();
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function pruneWalletChallenges(now = Math.floor(Date.now() / 1000)) {
|
|
82
|
+
for (const [nonce, challenge] of walletChallenges) {
|
|
83
|
+
if (challenge.expiresAt <= now) walletChallenges.delete(nonce);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function issueWalletChallenge({ owner, action, tier, id } = {}) {
|
|
88
|
+
const expectedOwner = normalizedOwner(owner);
|
|
89
|
+
const expectedAction = String(action || "");
|
|
90
|
+
if (!new Set(["onboard", "list", "revoke"]).has(expectedAction)) {
|
|
91
|
+
throw new HttpError(400, "invalid-action", "action must be onboard, list, or revoke");
|
|
92
|
+
}
|
|
93
|
+
const expectedTier = tier == null ? null : String(tier);
|
|
94
|
+
if (expectedAction === "onboard" && !expectedTier) {
|
|
95
|
+
throw new HttpError(400, "tier-required", "onboard challenge requires tier");
|
|
96
|
+
}
|
|
97
|
+
const expectedId = id == null ? null : String(id).trim();
|
|
98
|
+
if (expectedAction === "revoke" && !expectedId) {
|
|
99
|
+
throw new HttpError(400, "id-required", "revoke challenge requires key id");
|
|
100
|
+
}
|
|
101
|
+
if (expectedId && expectedId.length > 256) {
|
|
102
|
+
throw new HttpError(400, "invalid-id", "key id is too long");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const issuedAt = Math.floor(Date.now() / 1000);
|
|
106
|
+
pruneWalletChallenges(issuedAt);
|
|
107
|
+
if (walletChallenges.size >= MAX_CHALLENGES) {
|
|
108
|
+
throw new HttpError(429, "challenge-capacity", "too many pending wallet challenges");
|
|
109
|
+
}
|
|
110
|
+
const expiresAt = issuedAt + CHALLENGE_TTL_SECONDS;
|
|
111
|
+
const nonce = randomBytes(32).toString("hex");
|
|
112
|
+
const lines = [
|
|
113
|
+
"Oracle onboarding",
|
|
114
|
+
`owner: ${expectedOwner}`,
|
|
115
|
+
`action: ${expectedAction}`,
|
|
116
|
+
];
|
|
117
|
+
if (expectedTier != null) lines.push(`tier: ${expectedTier}`);
|
|
118
|
+
if (expectedId != null) lines.push(`id: ${expectedId}`);
|
|
119
|
+
lines.push(`nonce: ${nonce}`, `issuedAt: ${issuedAt}`, `expiresAt: ${expiresAt}`);
|
|
120
|
+
const message = lines.join("\n");
|
|
121
|
+
walletChallenges.set(nonce, {
|
|
122
|
+
owner: expectedOwner,
|
|
123
|
+
action: expectedAction,
|
|
124
|
+
tier: expectedTier,
|
|
125
|
+
id: expectedId,
|
|
126
|
+
issuedAt,
|
|
127
|
+
expiresAt,
|
|
128
|
+
message,
|
|
129
|
+
used: false,
|
|
130
|
+
});
|
|
131
|
+
return { message, issuedAt, expiresAt };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function requireWalletProof(body = {}, { owner, action, tier, id } = {}) {
|
|
135
|
+
const expectedOwner = normalizedOwner(owner);
|
|
136
|
+
const proof = body.proof || body.walletProof || {};
|
|
137
|
+
const message = proof.message || body.message;
|
|
138
|
+
const signature = proof.signature || body.signature;
|
|
139
|
+
if (!message || !signature) throw new HttpError(401, "wallet-proof-required", "wallet signature proof required");
|
|
140
|
+
let recovered;
|
|
141
|
+
try {
|
|
142
|
+
recovered = verifyMessage(String(message), String(signature)).toLowerCase();
|
|
143
|
+
} catch {
|
|
144
|
+
throw new HttpError(401, "invalid-proof", "wallet proof signature is invalid");
|
|
145
|
+
}
|
|
146
|
+
if (recovered !== expectedOwner) throw new HttpError(401, "owner-signature-mismatch", "wallet proof signer does not match owner");
|
|
147
|
+
const fields = parseProofMessage(message);
|
|
148
|
+
if (normalizedOwner(fields.owner) !== expectedOwner) throw new HttpError(401, "owner-proof-mismatch", "wallet proof owner mismatch");
|
|
149
|
+
const nonce = String(fields.nonce || "");
|
|
150
|
+
if (!/^[0-9a-f]{64}$/.test(nonce)) {
|
|
151
|
+
throw new HttpError(401, "challenge-required", "wallet proof must use a server-issued challenge");
|
|
152
|
+
}
|
|
153
|
+
const challenge = walletChallenges.get(nonce);
|
|
154
|
+
if (!challenge) throw new HttpError(401, "challenge-required", "wallet challenge is unknown or expired");
|
|
155
|
+
if (challenge.used) throw new HttpError(401, "proof-replayed", "wallet challenge was already used");
|
|
156
|
+
if (String(message) !== challenge.message) {
|
|
157
|
+
throw new HttpError(401, "challenge-mismatch", "wallet proof does not match the issued challenge");
|
|
158
|
+
}
|
|
159
|
+
challenge.used = true;
|
|
160
|
+
|
|
161
|
+
if (challenge.owner !== expectedOwner) throw new HttpError(401, "owner-proof-mismatch", "wallet proof owner mismatch");
|
|
162
|
+
if (challenge.action !== action || String(fields.action || "") !== action) {
|
|
163
|
+
throw new HttpError(401, "action-proof-mismatch", "wallet proof action mismatch");
|
|
164
|
+
}
|
|
165
|
+
if (tier != null && (challenge.tier !== String(tier) || String(fields.tier || "") !== String(tier))) {
|
|
166
|
+
throw new HttpError(401, "tier-proof-mismatch", "wallet proof tier mismatch");
|
|
167
|
+
}
|
|
168
|
+
if (id != null && (challenge.id !== String(id) || String(fields.id || "") !== String(id))) {
|
|
169
|
+
throw new HttpError(401, "id-proof-mismatch", "wallet proof key id mismatch");
|
|
170
|
+
}
|
|
171
|
+
const now = Math.floor(Date.now() / 1000);
|
|
172
|
+
const issuedAt = Number(fields.issuedAt || 0);
|
|
173
|
+
const expiresAt = Number(fields.expiresAt || 0);
|
|
174
|
+
if (issuedAt !== challenge.issuedAt || expiresAt !== challenge.expiresAt) {
|
|
175
|
+
throw new HttpError(401, "proof-expiry-mismatch", "wallet proof timestamps do not match the issued challenge");
|
|
176
|
+
}
|
|
177
|
+
if (issuedAt > now + 60) throw new HttpError(401, "proof-from-future", "wallet proof issuedAt is in the future");
|
|
178
|
+
if (expiresAt <= now) throw new HttpError(401, "proof-expired", "wallet proof expired");
|
|
179
|
+
if (expiresAt - issuedAt > 600) throw new HttpError(401, "proof-window-too-long", "wallet proof window must be <= 10 minutes");
|
|
180
|
+
return { owner: expectedOwner, recovered, action, expiresAt };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function send(res, status, body) {
|
|
184
|
+
const json = JSON.stringify(body);
|
|
185
|
+
res.writeHead(status, {
|
|
186
|
+
"content-type": "application/json; charset=utf-8",
|
|
187
|
+
"cache-control": "no-store",
|
|
188
|
+
"x-oracle-plane": "data+onboard",
|
|
189
|
+
"x-oracle-exec": "false",
|
|
190
|
+
});
|
|
191
|
+
res.end(json);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function readBody(req) {
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
const declared = Number(req.headers["content-length"] || 0);
|
|
197
|
+
if (Number.isFinite(declared) && declared > MAX_BODY_BYTES) {
|
|
198
|
+
reject(new HttpError(413, "body-too-large", `request body exceeds ${MAX_BODY_BYTES} bytes`));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const chunks = [];
|
|
202
|
+
let size = 0;
|
|
203
|
+
let rejected = false;
|
|
204
|
+
req.on("data", (c) => {
|
|
205
|
+
if (rejected) return;
|
|
206
|
+
size += c.length;
|
|
207
|
+
if (size > MAX_BODY_BYTES) {
|
|
208
|
+
rejected = true;
|
|
209
|
+
chunks.length = 0;
|
|
210
|
+
reject(new HttpError(413, "body-too-large", `request body exceeds ${MAX_BODY_BYTES} bytes`));
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
chunks.push(c);
|
|
214
|
+
});
|
|
215
|
+
req.on("end", () => {
|
|
216
|
+
if (rejected) return;
|
|
217
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
218
|
+
if (!raw) return resolve({});
|
|
219
|
+
try {
|
|
220
|
+
resolve(JSON.parse(raw));
|
|
221
|
+
} catch (e) {
|
|
222
|
+
reject(e);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
req.on("error", reject);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Local-only origin guard.
|
|
231
|
+
*
|
|
232
|
+
* This server binds loopback and is documented as "safe because it's local",
|
|
233
|
+
* but loopback is NOT a security boundary against a browser. Any page the
|
|
234
|
+
* operator visits can issue requests to http://127.0.0.1:8787, and a
|
|
235
|
+
* DNS-rebinding host can resolve an attacker domain to 127.0.0.1 and reach it
|
|
236
|
+
* under that name. Either way the request arrives with the server's own API
|
|
237
|
+
* keys attached, able to call every prepare op.
|
|
238
|
+
*
|
|
239
|
+
* Two cheap checks close both: the Host header must actually be loopback
|
|
240
|
+
* (defeats rebinding, which needs its own hostname), and a cross-origin
|
|
241
|
+
* browser request must be rejected (defeats the visited-page case; real CLI
|
|
242
|
+
* and server-side callers send no Origin at all).
|
|
243
|
+
*/
|
|
244
|
+
function assertLocalCaller(req) {
|
|
245
|
+
// The SOCKET is the boundary, not the Host header. Host is caller-controlled,
|
|
246
|
+
// and `0.0.0.0` was on the accept list — so binding ORACLE_DATA_HOST=0.0.0.0
|
|
247
|
+
// and sending `Host: 0.0.0.0` from another machine returned 200. Check the
|
|
248
|
+
// actual peer address first; nothing a remote client can type changes it.
|
|
249
|
+
const peer = String(req.socket?.remoteAddress || "");
|
|
250
|
+
const normalisedPeer = peer.replace(/^::ffff:/, "");
|
|
251
|
+
const PEER_LOOPBACK = new Set(["127.0.0.1", "::1"]);
|
|
252
|
+
const peerIsLoopback =
|
|
253
|
+
PEER_LOOPBACK.has(normalisedPeer) || normalisedPeer.startsWith("127.");
|
|
254
|
+
if (!peerIsLoopback) {
|
|
255
|
+
return `remote address ${peer || "(unknown)"} is not loopback — this server only answers local callers`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const host = String(req.headers.host || "").toLowerCase();
|
|
259
|
+
const hostname = host.replace(/:\d+$/, "").replace(/^\[|\]$/g, "");
|
|
260
|
+
// 0.0.0.0 is deliberately NOT here: it is a bind wildcard, never a legitimate
|
|
261
|
+
// Host for a loopback-only service, and accepting it defeated the check.
|
|
262
|
+
const LOOPBACK = new Set(["127.0.0.1", "localhost", "::1"]);
|
|
263
|
+
if (!LOOPBACK.has(hostname)) {
|
|
264
|
+
return `unexpected Host header ${host || "(none)"} — this server only answers to loopback names`;
|
|
265
|
+
}
|
|
266
|
+
const origin = req.headers.origin;
|
|
267
|
+
if (origin && origin !== "null") {
|
|
268
|
+
try {
|
|
269
|
+
const oh = new URL(origin).hostname;
|
|
270
|
+
if (!LOOPBACK.has(oh)) return `cross-origin request from ${origin} refused`;
|
|
271
|
+
} catch {
|
|
272
|
+
return `malformed Origin ${origin} refused`;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const server = createServer(async (req, res) => {
|
|
279
|
+
try {
|
|
280
|
+
const localOnly = assertLocalCaller(req);
|
|
281
|
+
if (localOnly) return send(res, 403, { error: localOnly });
|
|
282
|
+
|
|
283
|
+
const url = new URL(req.url || "/", `http://${HOST}:${PORT}`);
|
|
284
|
+
const path = url.pathname;
|
|
285
|
+
|
|
286
|
+
if (req.method === "GET" && path === "/health") {
|
|
287
|
+
return send(res, 200, {
|
|
288
|
+
ok: true,
|
|
289
|
+
service: "oracle",
|
|
290
|
+
plane: "data+onboard",
|
|
291
|
+
exec: false,
|
|
292
|
+
version: pkg.version,
|
|
293
|
+
onboardHttp: ONBOARD_ENABLED,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (req.method === "GET" && path === "/data/catalog") {
|
|
298
|
+
return send(res, 200, { catalog: dataCatalog() });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (req.method === "GET" && path === "/data/health") {
|
|
302
|
+
const report = await dataHealth({
|
|
303
|
+
timeoutMs: 10_000,
|
|
304
|
+
providers: PUBLIC_API_MODE ? publicCallableProviders() : undefined,
|
|
305
|
+
});
|
|
306
|
+
return send(res, 200, report);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (path === "/data/call") {
|
|
310
|
+
let provider, op, args;
|
|
311
|
+
if (req.method === "GET") {
|
|
312
|
+
provider = url.searchParams.get("provider");
|
|
313
|
+
op = url.searchParams.get("op");
|
|
314
|
+
const argsRaw = url.searchParams.get("args");
|
|
315
|
+
args = argsRaw ? JSON.parse(argsRaw) : {};
|
|
316
|
+
} else if (req.method === "POST") {
|
|
317
|
+
const body = await readBody(req);
|
|
318
|
+
provider = body.provider;
|
|
319
|
+
op = body.op;
|
|
320
|
+
args = body.args || {};
|
|
321
|
+
} else {
|
|
322
|
+
return send(res, 405, { error: "method not allowed" });
|
|
323
|
+
}
|
|
324
|
+
if (!provider || !op) return send(res, 400, { error: "provider and op required" });
|
|
325
|
+
// Provider-level boundary. A name-based verb filter is NOT a boundary:
|
|
326
|
+
// `op:"trade"` matches none of these words, so the live signing lane was
|
|
327
|
+
// reachable through a server whose /health advertises exec:false. Reject
|
|
328
|
+
// the execution provider outright, before any op-name heuristic.
|
|
329
|
+
if (isExecutionProvider(provider)) {
|
|
330
|
+
return send(res, 403, {
|
|
331
|
+
error: `${provider} is an execution provider and is not served by the data plane; run the execution binary instead`,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (/sign|execute|broadcast|approve|submit|place|send|write|trade|order|swap/i.test(op)) {
|
|
335
|
+
return send(res, 403, { error: "sign/exec ops forbidden on data server" });
|
|
336
|
+
}
|
|
337
|
+
const meta = getProvider(provider);
|
|
338
|
+
if (PUBLIC_API_MODE && providerNeedsServerKey(meta) && !publicAllowsServerKeyProvider(provider)) {
|
|
339
|
+
return send(res, 403, {
|
|
340
|
+
error: `public mode blocks ${provider}.${op}: server API key providers are disabled unless explicitly allowlisted`,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
const result = await dataCall(provider, op, args, { timeoutMs: 15_000 });
|
|
344
|
+
return send(res, 200, { provider, op, result });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (path.startsWith("/onboard")) {
|
|
348
|
+
if (!ONBOARD_ENABLED) return send(res, 403, { error: "onboard HTTP disabled" });
|
|
349
|
+
|
|
350
|
+
if (req.method === "GET" && path === "/onboard/tiers") {
|
|
351
|
+
return send(res, 200, { tiers: listTiers() });
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (req.method === "POST" && path === "/onboard/challenge") {
|
|
355
|
+
const body = await readBody(req);
|
|
356
|
+
return send(res, 200, issueWalletChallenge(body));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (req.method === "POST" && path === "/onboard") {
|
|
360
|
+
const body = await readBody(req);
|
|
361
|
+
if (body.privateKey || body.secretKey) {
|
|
362
|
+
return send(res, 400, { error: "do not send wallet private keys — owner address only" });
|
|
363
|
+
}
|
|
364
|
+
const tier = body.tier || "observer";
|
|
365
|
+
requireWalletProof(body, { owner: body.owner, action: "onboard", tier });
|
|
366
|
+
const controllers = csvEnv("ORACLE_EXECUTOR_CONTROLLERS", "MAD_EXECUTOR_CONTROLLERS")
|
|
367
|
+
.split(",")
|
|
368
|
+
.map((s) => s.trim())
|
|
369
|
+
.filter(Boolean);
|
|
370
|
+
const result = onboardUser({
|
|
371
|
+
owner: body.owner,
|
|
372
|
+
tier,
|
|
373
|
+
name: body.name,
|
|
374
|
+
controllerAllowlist: controllers,
|
|
375
|
+
});
|
|
376
|
+
return send(res, 201, result);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (req.method === "POST" && path === "/onboard/auth") {
|
|
380
|
+
const body = await readBody(req);
|
|
381
|
+
const auth = onboardAuthenticate(body.key || body.apiKey);
|
|
382
|
+
return send(res, 200, auth);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (req.method === "GET" && path === "/onboard/keys") {
|
|
386
|
+
const owner = url.searchParams.get("owner");
|
|
387
|
+
if (!owner) return send(res, 400, { error: "owner query required" });
|
|
388
|
+
return send(res, 405, { error: "method not allowed", message: "use POST /onboard/keys with wallet proof" });
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (req.method === "POST" && path === "/onboard/keys") {
|
|
392
|
+
const body = await readBody(req);
|
|
393
|
+
requireWalletProof(body, { owner: body.owner, action: "list" });
|
|
394
|
+
return send(res, 200, { keys: onboardListKeys(body.owner) });
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (req.method === "POST" && path === "/onboard/revoke") {
|
|
398
|
+
const body = await readBody(req);
|
|
399
|
+
requireWalletProof(body, { owner: body.owner, action: "revoke", id: body.id });
|
|
400
|
+
const rec = onboardRevoke({ owner: body.owner, id: body.id });
|
|
401
|
+
return send(res, 200, { revoked: rec });
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return send(res, 404, {
|
|
406
|
+
error: "not found",
|
|
407
|
+
routes: [
|
|
408
|
+
"/health",
|
|
409
|
+
"/data/catalog",
|
|
410
|
+
"/data/health",
|
|
411
|
+
"/data/call",
|
|
412
|
+
"/onboard/tiers",
|
|
413
|
+
"POST /onboard/challenge",
|
|
414
|
+
"POST /onboard",
|
|
415
|
+
"POST /onboard/auth",
|
|
416
|
+
"POST /onboard/keys",
|
|
417
|
+
"POST /onboard/revoke",
|
|
418
|
+
],
|
|
419
|
+
});
|
|
420
|
+
} catch (e) {
|
|
421
|
+
if (e instanceof HttpError) {
|
|
422
|
+
return send(res, e.status, { error: e.code, message: e.message });
|
|
423
|
+
}
|
|
424
|
+
return send(res, 500, { error: String(e.message || e) });
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
server.listen(PORT, HOST, () => {
|
|
429
|
+
console.log(
|
|
430
|
+
JSON.stringify({
|
|
431
|
+
listening: `http://${HOST}:${PORT}`,
|
|
432
|
+
plane: "data+onboard",
|
|
433
|
+
exec: false,
|
|
434
|
+
catalog: data.catalog().map((p) => p.id),
|
|
435
|
+
tiers: listTiers().map((t) => t.id),
|
|
436
|
+
})
|
|
437
|
+
);
|
|
438
|
+
});
|