@flapsdk/vault-runtime 0.1.24 → 0.1.26
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 +2 -2
- package/chunk-CPAMDXX7.js +45 -0
- package/chunk-CPAMDXX7.js.map +1 -0
- package/chunk-GA5VEHC2.js +40 -0
- package/chunk-GA5VEHC2.js.map +1 -0
- package/chunk-JLI25XFB.js +78 -0
- package/chunk-JLI25XFB.js.map +1 -0
- package/chunk-SYEMIHOY.js +408 -0
- package/chunk-SYEMIHOY.js.map +1 -0
- package/chunk-WX6B7JWO.js +183 -0
- package/chunk-WX6B7JWO.js.map +1 -0
- package/chunk-XGUVYKIJ.js +185 -0
- package/chunk-XGUVYKIJ.js.map +1 -0
- package/chunk-ZK3O6QPF.js +544 -0
- package/chunk-ZK3O6QPF.js.map +1 -0
- package/host.d.mts +4 -4
- package/host.js +8 -555
- package/host.js.map +1 -1
- package/hostRuntimeConfig-C_JJVgY0.d.mts +34 -0
- package/package.json +2 -2
- package/runtime-contract.json +1 -1
- package/sdk.d.mts +55 -11
- package/sdk.js +90 -516
- package/sdk.js.map +1 -1
- package/server.d.mts +12 -24
- package/server.js +197 -277
- package/server.js.map +1 -1
- package/{txError-BQUh3OY8.d.mts → txError-BEercCCG.d.mts} +1 -1
- package/{types-CUhkhTgO.d.mts → types-RxLUOC02.d.mts} +27 -1
- package/ui.d.mts +13 -2
- package/ui.js +3 -522
- package/ui.js.map +1 -1
- package/hostRuntimeConfig-oF7rDRpq.d.mts +0 -14
package/server.js
CHANGED
|
@@ -1,279 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
portal: "0xe2ce6ab80874fa9fa2aae65d277dd6b8e65c9de0",
|
|
9
|
-
taxTokenHelperAddress: "0x53841c73217735F37BC1775538b03b23feFD8346",
|
|
10
|
-
vaultPortal: "0x90497450f2a706f1951b5bdda52B4E5d16f34C06",
|
|
11
|
-
wrappedNativeTokenAddress: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
12
|
-
giftVaultFactory: "0x025549F52B03cF36f9e1a337c02d3AA7Af66ab32",
|
|
13
|
-
hostChainSlug: "bnb",
|
|
14
|
-
ipfsGateway: "https://flap.mypinata.cloud"
|
|
15
|
-
},
|
|
16
|
-
97: {
|
|
17
|
-
portal: "0x5bEacaF7ABCbB3aB280e80D007FD31fcE26510e9",
|
|
18
|
-
taxTokenHelperAddress: "0xD64441e5FcD02D342B8cf6eBA10Ef6E40d0dA90f",
|
|
19
|
-
vaultPortal: "0x027e3704fC5C16522e9393d04C60A3ac5c0d775f",
|
|
20
|
-
wrappedNativeTokenAddress: "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
|
|
21
|
-
giftVaultFactory: "0xa02DA44D67DB6D692efa7f751b5952bd670d5326",
|
|
22
|
-
hostChainSlug: "bnb-testnet",
|
|
23
|
-
ipfsGateway: "https://magenta-naval-penguin-822.mypinata.cloud"
|
|
24
|
-
},
|
|
25
|
-
4663: {
|
|
26
|
-
portal: "0x26605f322f7fF986f381bB9A6e3f5DAb0bEaEb09",
|
|
27
|
-
taxTokenHelperAddress: "0xb10bD2672aE63735d677164A54B573a016f0203C",
|
|
28
|
-
vaultPortal: "0xe9F7AB7DE8FB8756acbB6a1cd13316a43308197B",
|
|
29
|
-
wrappedNativeTokenAddress: "0x0Bd7D308f8E1639FAb988df18A8011f41EAcAD73",
|
|
30
|
-
giftVaultFactory: "0xe6ca297D1d963b6F00d5b216986123CAeB883AF6",
|
|
31
|
-
hostChainSlug: "robinhood",
|
|
32
|
-
ipfsGateway: "https://flap.mypinata.cloud"
|
|
33
|
-
},
|
|
34
|
-
46630: {
|
|
35
|
-
portal: "0x26605f322f7fF986f381bB9A6e3f5DAb0bEaEb09",
|
|
36
|
-
taxTokenHelperAddress: "0xb10bD2672aE63735d677164A54B573a016f0203C",
|
|
37
|
-
vaultPortal: "0xe9F7AB7DE8FB8756acbB6a1cd13316a43308197B",
|
|
38
|
-
wrappedNativeTokenAddress: "0x7943e237c7F95DA44E0301572D358911207852Fa",
|
|
39
|
-
hostChainSlug: "robinhood-testnet",
|
|
40
|
-
ipfsGateway: "https://flap.mypinata.cloud"
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
function getTaxVaultHostChainConfig(chainId) {
|
|
44
|
-
return taxVaultHostChains[chainId];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// src/sdk/hostPresentation.ts
|
|
48
|
-
var DEFAULT_FLAP_HOST_ORIGIN = "https://flap.sh";
|
|
49
|
-
var DEFAULT_RUNTIME_PRESENTATION_ENDPOINT = "/api/runtime/token-presentation";
|
|
50
|
-
var DEFAULT_IPFS_GATEWAY = "https://flap.mypinata.cloud";
|
|
51
|
-
var LEGACY_IPFS_GATEWAY_HOSTS = /* @__PURE__ */ new Set([
|
|
52
|
-
"cf-ipfs.com",
|
|
53
|
-
"pump.mypinata.cloud",
|
|
54
|
-
"gateway.pinata.cloud",
|
|
55
|
-
"flap.mypinata.cloud",
|
|
56
|
-
"magenta-naval-penguin-822.mypinata.cloud"
|
|
57
|
-
]);
|
|
58
|
-
function normalizeOrigin(origin) {
|
|
59
|
-
if (!origin) return DEFAULT_FLAP_HOST_ORIGIN;
|
|
60
|
-
return origin.replace(/\/+$/, "");
|
|
61
|
-
}
|
|
62
|
-
function normalizeAddress(address) {
|
|
63
|
-
return isAddress(address) ? getAddress(address) : null;
|
|
64
|
-
}
|
|
65
|
-
function normalizeString(value) {
|
|
66
|
-
return typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
67
|
-
}
|
|
68
|
-
function resolveHostChainSlug(chainId) {
|
|
69
|
-
return getTaxVaultHostChainConfig(chainId)?.hostChainSlug;
|
|
70
|
-
}
|
|
71
|
-
function resolveIpfsGateway(chainId) {
|
|
72
|
-
return getTaxVaultHostChainConfig(chainId)?.ipfsGateway ?? DEFAULT_IPFS_GATEWAY;
|
|
73
|
-
}
|
|
74
|
-
function rewriteToPublicIpfsGateway(rawUrl, chainId) {
|
|
75
|
-
try {
|
|
76
|
-
const gateway = resolveIpfsGateway(chainId);
|
|
77
|
-
const parsed = new URL(rawUrl);
|
|
78
|
-
if (!LEGACY_IPFS_GATEWAY_HOSTS.has(parsed.hostname)) return rawUrl;
|
|
79
|
-
const publicGateway = new URL(gateway);
|
|
80
|
-
parsed.protocol = publicGateway.protocol;
|
|
81
|
-
parsed.host = publicGateway.host;
|
|
82
|
-
return parsed.toString();
|
|
83
|
-
} catch {
|
|
84
|
-
return void 0;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function wrapHostImageUrl(image, chainId) {
|
|
88
|
-
const raw = normalizeString(image);
|
|
89
|
-
if (!raw) return void 0;
|
|
90
|
-
if (raw.startsWith("/")) return raw;
|
|
91
|
-
if (raw.startsWith("https://")) return rewriteToPublicIpfsGateway(raw, chainId);
|
|
92
|
-
if (raw.startsWith("http://")) return void 0;
|
|
93
|
-
const gateway = resolveIpfsGateway(chainId).replace(/\/+$/, "");
|
|
94
|
-
if (raw.startsWith("ipfs://")) return `${gateway}/ipfs/${raw.slice("ipfs://".length)}`;
|
|
95
|
-
return `${gateway}/ipfs/${raw}`;
|
|
96
|
-
}
|
|
97
|
-
function buildTokenDetailHref(origin, chainSlug, tokenAddress) {
|
|
98
|
-
return `${origin}/${chainSlug}/${tokenAddress.toLowerCase()}/taxinfo`;
|
|
99
|
-
}
|
|
100
|
-
function buildChainHref(origin, chainSlug) {
|
|
101
|
-
return `${origin}/${chainSlug}`;
|
|
102
|
-
}
|
|
103
|
-
function resolveMetadataItem(payload, tokenAddress) {
|
|
104
|
-
const data = payload?.data;
|
|
105
|
-
if (!data || typeof data !== "object") return null;
|
|
106
|
-
const checksum = getAddress(tokenAddress);
|
|
107
|
-
const lower = tokenAddress.toLowerCase();
|
|
108
|
-
const direct = data[checksum] ?? data[lower];
|
|
109
|
-
if (direct && typeof direct === "object") return direct;
|
|
110
|
-
for (const value of Object.values(data)) {
|
|
111
|
-
if (!value || typeof value !== "object") continue;
|
|
112
|
-
const itemAddress = normalizeString(value.address);
|
|
113
|
-
if (itemAddress && normalizeAddress(itemAddress)?.toLowerCase() === lower) {
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
function toHostPresentation(chainId, tokenAddress, flapHostOrigin, item) {
|
|
120
|
-
if (!item) return null;
|
|
121
|
-
const chainSlug = resolveHostChainSlug(chainId);
|
|
122
|
-
if (!chainSlug) return null;
|
|
123
|
-
const tokenSymbol = normalizeString(item.symbol);
|
|
124
|
-
const tokenName = normalizeString(item.name);
|
|
125
|
-
const tokenImageUrl = wrapHostImageUrl(item.metadata?.image, chainId);
|
|
126
|
-
if (!tokenSymbol && !tokenName && !tokenImageUrl) return null;
|
|
127
|
-
return {
|
|
128
|
-
tokenSymbol,
|
|
129
|
-
tokenName,
|
|
130
|
-
tokenImageUrl,
|
|
131
|
-
tokenDetailHref: buildTokenDetailHref(flapHostOrigin, chainSlug, tokenAddress),
|
|
132
|
-
chainHref: buildChainHref(flapHostOrigin, chainSlug),
|
|
133
|
-
extraConfig: {
|
|
134
|
-
hostPresentationOrigin: flapHostOrigin,
|
|
135
|
-
hostPresentationChain: chainSlug,
|
|
136
|
-
...normalizeString(item.metadata?.description) ? { tokenDescription: normalizeString(item.metadata?.description) } : {}
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
async function loadFlapHostTokenPresentation(input) {
|
|
141
|
-
const chainSlug = resolveHostChainSlug(input.chainId);
|
|
142
|
-
if (!chainSlug) return null;
|
|
143
|
-
const tokenAddress = normalizeAddress(input.tokenAddress);
|
|
144
|
-
if (!tokenAddress) return null;
|
|
145
|
-
const flapHostOrigin = normalizeOrigin(input.flapHostOrigin);
|
|
146
|
-
const fetchImpl = input.fetchImpl ?? fetch;
|
|
147
|
-
const response = await fetchImpl(`${flapHostOrigin}/api/tax-dashboard/metadata`, {
|
|
148
|
-
method: "POST",
|
|
149
|
-
headers: {
|
|
150
|
-
"content-type": "application/json"
|
|
151
|
-
},
|
|
152
|
-
body: JSON.stringify({
|
|
153
|
-
chain: chainSlug,
|
|
154
|
-
addresses: [tokenAddress.toLowerCase()]
|
|
155
|
-
}),
|
|
156
|
-
cache: "no-store"
|
|
157
|
-
});
|
|
158
|
-
if (!response.ok) return null;
|
|
159
|
-
const payload = await response.json();
|
|
160
|
-
return toHostPresentation(input.chainId, tokenAddress, flapHostOrigin, resolveMetadataItem(payload, tokenAddress));
|
|
161
|
-
}
|
|
162
|
-
async function loadFlapHostTokenPresentationBatch(input) {
|
|
163
|
-
const chainSlug = resolveHostChainSlug(input.chainId);
|
|
164
|
-
if (!chainSlug) return {};
|
|
165
|
-
const normalizedAddresses = input.tokenAddresses.map((tokenAddress) => normalizeAddress(tokenAddress)).filter((tokenAddress) => Boolean(tokenAddress));
|
|
166
|
-
if (!normalizedAddresses.length) return {};
|
|
167
|
-
const flapHostOrigin = normalizeOrigin(input.flapHostOrigin);
|
|
168
|
-
const fetchImpl = input.fetchImpl ?? fetch;
|
|
169
|
-
const response = await fetchImpl(`${flapHostOrigin}/api/tax-dashboard/metadata`, {
|
|
170
|
-
method: "POST",
|
|
171
|
-
headers: {
|
|
172
|
-
"content-type": "application/json"
|
|
173
|
-
},
|
|
174
|
-
body: JSON.stringify({
|
|
175
|
-
chain: chainSlug,
|
|
176
|
-
addresses: normalizedAddresses.map((tokenAddress) => tokenAddress.toLowerCase())
|
|
177
|
-
}),
|
|
178
|
-
cache: "no-store"
|
|
179
|
-
});
|
|
180
|
-
if (!response.ok) return {};
|
|
181
|
-
const payload = await response.json();
|
|
182
|
-
return Object.fromEntries(
|
|
183
|
-
normalizedAddresses.map((tokenAddress) => [
|
|
184
|
-
tokenAddress,
|
|
185
|
-
toHostPresentation(input.chainId, tokenAddress, flapHostOrigin, resolveMetadataItem(payload, tokenAddress))
|
|
186
|
-
])
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
function parsePresentationPayload(payload) {
|
|
190
|
-
if (!payload || typeof payload !== "object") return null;
|
|
191
|
-
const record = payload;
|
|
192
|
-
const tokenSymbol = normalizeString(record.tokenSymbol);
|
|
193
|
-
const tokenName = normalizeString(record.tokenName);
|
|
194
|
-
const tokenImageUrl = normalizeString(record.tokenImageUrl);
|
|
195
|
-
if (!tokenSymbol && !tokenName && !tokenImageUrl) return null;
|
|
196
|
-
return {
|
|
197
|
-
tokenSymbol,
|
|
198
|
-
tokenName,
|
|
199
|
-
tokenImageUrl,
|
|
200
|
-
tokenDetailHref: normalizeString(record.tokenDetailHref),
|
|
201
|
-
chainHref: normalizeString(record.chainHref),
|
|
202
|
-
extraConfig: record.extraConfig && typeof record.extraConfig === "object" && !Array.isArray(record.extraConfig) ? record.extraConfig : void 0
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
function createLocalHostPresentationFetcher(options = {}) {
|
|
206
|
-
const endpoint = options.endpoint ?? DEFAULT_RUNTIME_PRESENTATION_ENDPOINT;
|
|
207
|
-
const fetchImpl = options.fetchImpl ?? fetch;
|
|
208
|
-
return async ({ chainId, tokenAddress, factoryAddress, vaultAddress }) => {
|
|
209
|
-
const query = new URLSearchParams({
|
|
210
|
-
chainId: String(chainId),
|
|
211
|
-
tokenAddress
|
|
212
|
-
});
|
|
213
|
-
if (factoryAddress) query.set("factoryAddress", factoryAddress);
|
|
214
|
-
if (vaultAddress) query.set("vaultAddress", vaultAddress);
|
|
215
|
-
const response = await fetchImpl(`${endpoint}?${query.toString()}`, {
|
|
216
|
-
method: "GET",
|
|
217
|
-
cache: "no-store"
|
|
218
|
-
});
|
|
219
|
-
if (!response.ok) return null;
|
|
220
|
-
const payload = await response.json();
|
|
221
|
-
return parsePresentationPayload(payload?.data);
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// src/sdk/oracle.ts
|
|
226
|
-
var OracleReadError = class extends Error {
|
|
227
|
-
constructor(message, status) {
|
|
228
|
-
super(message);
|
|
229
|
-
this.name = "OracleReadError";
|
|
230
|
-
this.status = status;
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
function appendSearchParams(url, params) {
|
|
234
|
-
for (const [key, value] of Object.entries(params ?? {})) {
|
|
235
|
-
url.searchParams.set(key, value);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
function toOracleProvision(provision) {
|
|
239
|
-
return typeof provision === "string" ? { endpoint: provision } : provision;
|
|
240
|
-
}
|
|
241
|
-
function buildProvisionedParams(provision, params) {
|
|
242
|
-
const filteredParams = provision.allowedParams && provision.allowedParams.length ? Object.fromEntries(Object.entries(params ?? {}).filter(([key]) => provision.allowedParams?.includes(key))) : params;
|
|
243
|
-
return {
|
|
244
|
-
...filteredParams ?? {},
|
|
245
|
-
...provision.fixedParams ?? {}
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
async function fetchOracleJson({
|
|
249
|
-
endpoint,
|
|
250
|
-
params,
|
|
251
|
-
fetchImpl
|
|
252
|
-
}) {
|
|
253
|
-
const fallbackOrigin = typeof window !== "undefined" ? window.location.origin : "http://localhost";
|
|
254
|
-
const url = new URL(endpoint, fallbackOrigin);
|
|
255
|
-
appendSearchParams(url, params);
|
|
256
|
-
const response = await (fetchImpl ?? fetch)(url.toString(), {
|
|
257
|
-
cache: "no-store",
|
|
258
|
-
method: "GET"
|
|
259
|
-
});
|
|
260
|
-
if (!response.ok) {
|
|
261
|
-
throw new OracleReadError(`Oracle request returned ${response.status}.`, response.status);
|
|
262
|
-
}
|
|
263
|
-
return await response.json();
|
|
264
|
-
}
|
|
265
|
-
async function fetchProvisionedOracle({
|
|
266
|
-
provision,
|
|
267
|
-
params,
|
|
268
|
-
fetchImpl
|
|
269
|
-
}) {
|
|
270
|
-
const normalizedProvision = toOracleProvision(provision);
|
|
271
|
-
return fetchOracleJson({
|
|
272
|
-
endpoint: normalizedProvision.endpoint,
|
|
273
|
-
params: buildProvisionedParams(normalizedProvision, params),
|
|
274
|
-
fetchImpl
|
|
275
|
-
});
|
|
276
|
-
}
|
|
1
|
+
import { parseInlineNftMetadata, sanitizeNftMetadataRecord, normalizeInlineNftImage } from './chunk-XGUVYKIJ.js';
|
|
2
|
+
export { createLocalHostPresentationFetcher, loadFlapHostTokenPresentation, loadFlapHostTokenPresentationBatch } from './chunk-WX6B7JWO.js';
|
|
3
|
+
import { fetchProvisionedOracle } from './chunk-JLI25XFB.js';
|
|
4
|
+
import { getTaxVaultHostChainConfig } from './chunk-CPAMDXX7.js';
|
|
5
|
+
export { getTaxVaultHostChainConfig } from './chunk-CPAMDXX7.js';
|
|
6
|
+
import { lookup } from 'dns/promises';
|
|
7
|
+
import { isIP } from 'net';
|
|
277
8
|
|
|
278
9
|
// src/sdk/oracleServer.ts
|
|
279
10
|
var FLAP_RUNTIME_ORACLE_REGISTRY_ENV = "FLAP_RUNTIME_ORACLE_REGISTRY";
|
|
@@ -479,7 +210,196 @@ async function loadRuntimeOracle({
|
|
|
479
210
|
}
|
|
480
211
|
return loadBuiltinRuntimeOracle(oracleId, fetchImpl);
|
|
481
212
|
}
|
|
213
|
+
var DEFAULT_IPFS_GATEWAY = "https://flap.mypinata.cloud";
|
|
214
|
+
var CID_RE = /^(?:Qm[1-9A-HJ-NP-Za-km-z]{44}|b[a-z2-7]{20,})$/;
|
|
215
|
+
var IPFS_PATH_SEGMENT_RE = /^[A-Za-z0-9._~%-]+$/;
|
|
216
|
+
var MAX_REMOTE_METADATA_BYTES = 512 * 1024;
|
|
217
|
+
var MAX_REMOTE_IMAGE_BYTES = 5 * 1024 * 1024;
|
|
218
|
+
var MAX_REDIRECTS = 3;
|
|
219
|
+
var REQUEST_TIMEOUT_MS = 8e3;
|
|
220
|
+
var ALLOWED_IMAGE_MEDIA_TYPES = /* @__PURE__ */ new Set(["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif", "image/svg+xml"]);
|
|
221
|
+
var BLOCKED_HOST_SUFFIXES = [".localhost", ".local", ".internal", ".home", ".lan"];
|
|
222
|
+
function sourceForUri(value) {
|
|
223
|
+
if (value.trim().startsWith("data:")) return "data-json";
|
|
224
|
+
return /^https:\/\//i.test(value.trim()) ? "https" : "ipfs";
|
|
225
|
+
}
|
|
226
|
+
function isBlockedIpv4(address) {
|
|
227
|
+
const octets = address.split(".").map(Number);
|
|
228
|
+
if (octets.length !== 4 || octets.some((value) => !Number.isInteger(value) || value < 0 || value > 255)) return true;
|
|
229
|
+
const [a, b] = octets;
|
|
230
|
+
return a === 0 || a === 10 || a === 127 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || a === 192 && b === 0 || a === 192 && b === 2 || a === 198 && (b === 18 || b === 19 || b === 51) || a === 203 && b === 0 || a >= 224;
|
|
231
|
+
}
|
|
232
|
+
function isBlockedIp(address) {
|
|
233
|
+
const version = isIP(address);
|
|
234
|
+
if (version === 4) return isBlockedIpv4(address);
|
|
235
|
+
if (version !== 6) return true;
|
|
236
|
+
const normalized = address.toLowerCase();
|
|
237
|
+
if (normalized.startsWith("::ffff:")) return isBlockedIpv4(normalized.slice("::ffff:".length));
|
|
238
|
+
return normalized === "::" || normalized === "::1" || normalized.startsWith("fc") || normalized.startsWith("fd") || /^fe[89ab]/.test(normalized) || normalized.startsWith("ff") || normalized.startsWith("2001:db8:");
|
|
239
|
+
}
|
|
240
|
+
async function assertSafeHttpsUrl(url) {
|
|
241
|
+
if (url.protocol !== "https:" || url.username || url.password || url.port && url.port !== "443") {
|
|
242
|
+
throw new Error("NFT metadata resources must use credential-free HTTPS.");
|
|
243
|
+
}
|
|
244
|
+
const hostname = url.hostname.toLowerCase().replace(/\.$/, "");
|
|
245
|
+
if (!hostname || hostname === "localhost" || BLOCKED_HOST_SUFFIXES.some((suffix) => hostname.endsWith(suffix))) {
|
|
246
|
+
throw new Error("NFT metadata resource host is not allowed.");
|
|
247
|
+
}
|
|
248
|
+
if (isIP(hostname)) {
|
|
249
|
+
if (isBlockedIp(hostname)) throw new Error("NFT metadata resource host is not public.");
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const addresses = await lookup(hostname, { all: true, verbatim: true });
|
|
253
|
+
if (!addresses.length || addresses.some((entry) => isBlockedIp(entry.address))) {
|
|
254
|
+
throw new Error("NFT metadata resource host did not resolve to public addresses.");
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function parseIpfsPath(value) {
|
|
258
|
+
const trimmed = value.trim();
|
|
259
|
+
const raw = trimmed.startsWith("ipfs://") ? trimmed.slice("ipfs://".length).replace(/^ipfs\//, "") : trimmed;
|
|
260
|
+
if (!raw || raw.includes("?") || raw.includes("#") || raw.startsWith("/") || raw.endsWith("/")) return null;
|
|
261
|
+
const segments = raw.split("/");
|
|
262
|
+
const cid = segments.shift();
|
|
263
|
+
if (!cid || !CID_RE.test(cid)) return null;
|
|
264
|
+
if (segments.some((segment) => {
|
|
265
|
+
if (!segment || !IPFS_PATH_SEGMENT_RE.test(segment)) return true;
|
|
266
|
+
let decoded;
|
|
267
|
+
try {
|
|
268
|
+
decoded = decodeURIComponent(segment);
|
|
269
|
+
} catch {
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
return !decoded || decoded === "." || decoded === ".." || decoded.includes("/") || decoded.includes("\\") || /[\u0000-\u001f\u007f]/.test(decoded);
|
|
273
|
+
})) return null;
|
|
274
|
+
return [cid, ...segments].join("/");
|
|
275
|
+
}
|
|
276
|
+
function resolveResourceUrl(value, chainId, baseUrl) {
|
|
277
|
+
const trimmed = value.trim();
|
|
278
|
+
const ipfsPath = parseIpfsPath(trimmed);
|
|
279
|
+
if (ipfsPath) {
|
|
280
|
+
const gateway = (getTaxVaultHostChainConfig(chainId)?.ipfsGateway ?? DEFAULT_IPFS_GATEWAY).replace(/\/+$/, "");
|
|
281
|
+
return new URL(`${gateway}/ipfs/${ipfsPath}`);
|
|
282
|
+
}
|
|
283
|
+
if (/^https:\/\//i.test(trimmed)) return new URL(trimmed);
|
|
284
|
+
if (baseUrl && !/^[a-z][a-z0-9+.-]*:/i.test(trimmed)) return new URL(trimmed, baseUrl);
|
|
285
|
+
throw new Error("NFT metadata resource URI must be data JSON, IPFS, or HTTPS.");
|
|
286
|
+
}
|
|
287
|
+
async function readLimitedBody(response, maxBytes) {
|
|
288
|
+
const rawLength = response.headers.get("content-length");
|
|
289
|
+
if (rawLength && Number(rawLength) > maxBytes) throw new Error("NFT metadata resource is too large.");
|
|
290
|
+
if (!response.body) return new Uint8Array();
|
|
291
|
+
const reader = response.body.getReader();
|
|
292
|
+
const chunks = [];
|
|
293
|
+
let total = 0;
|
|
294
|
+
while (true) {
|
|
295
|
+
const { done, value } = await reader.read();
|
|
296
|
+
if (done) break;
|
|
297
|
+
if (!value) continue;
|
|
298
|
+
total += value.byteLength;
|
|
299
|
+
if (total > maxBytes) {
|
|
300
|
+
await reader.cancel();
|
|
301
|
+
throw new Error("NFT metadata resource is too large.");
|
|
302
|
+
}
|
|
303
|
+
chunks.push(value);
|
|
304
|
+
}
|
|
305
|
+
const output = new Uint8Array(total);
|
|
306
|
+
let offset = 0;
|
|
307
|
+
for (const chunk of chunks) {
|
|
308
|
+
output.set(chunk, offset);
|
|
309
|
+
offset += chunk.byteLength;
|
|
310
|
+
}
|
|
311
|
+
return output;
|
|
312
|
+
}
|
|
313
|
+
async function fetchLimitedResource(initialUrl, maxBytes, fetchImpl) {
|
|
314
|
+
let url = initialUrl;
|
|
315
|
+
for (let redirect = 0; redirect <= MAX_REDIRECTS; redirect += 1) {
|
|
316
|
+
await assertSafeHttpsUrl(url);
|
|
317
|
+
const response = await fetchImpl(url, {
|
|
318
|
+
method: "GET",
|
|
319
|
+
cache: "no-store",
|
|
320
|
+
redirect: "manual",
|
|
321
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
322
|
+
headers: { accept: "application/json,image/*;q=0.9,*/*;q=0.1" }
|
|
323
|
+
});
|
|
324
|
+
if (response.status >= 300 && response.status < 400) {
|
|
325
|
+
const location = response.headers.get("location");
|
|
326
|
+
if (!location || redirect === MAX_REDIRECTS) throw new Error("NFT metadata resource redirect is invalid.");
|
|
327
|
+
url = new URL(location, url);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (!response.ok) throw new Error(`NFT metadata resource returned ${response.status}.`);
|
|
331
|
+
return {
|
|
332
|
+
bytes: await readLimitedBody(response, maxBytes),
|
|
333
|
+
contentType: response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() ?? "",
|
|
334
|
+
finalUrl: url.toString()
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
throw new Error("NFT metadata resource redirected too many times.");
|
|
338
|
+
}
|
|
339
|
+
function bytesToDataUrl(bytes, mediaType) {
|
|
340
|
+
return `data:${mediaType};base64,${Buffer.from(bytes).toString("base64")}`;
|
|
341
|
+
}
|
|
342
|
+
function assertSafeSvg(bytes) {
|
|
343
|
+
const svg = new TextDecoder().decode(bytes);
|
|
344
|
+
if (!/<svg(?:\s|>)/i.test(svg)) throw new Error("NFT SVG image is malformed.");
|
|
345
|
+
const blockedMarkup = /<(?:script|foreignObject|iframe|object|embed|image|use)\b|\bon[a-z]+\s*=|\b(?:href|xlink:href)\s*=\s*["'](?!#)|javascript:|@import|url\(\s*["']?(?!#)/i;
|
|
346
|
+
if (blockedMarkup.test(svg)) throw new Error("NFT SVG image contains unsupported active or external content.");
|
|
347
|
+
}
|
|
348
|
+
async function resolveImage(imageUri, chainId, fetchImpl, baseUrl) {
|
|
349
|
+
const inlineImage = normalizeInlineNftImage(imageUri);
|
|
350
|
+
if (inlineImage) return inlineImage;
|
|
351
|
+
const response = await fetchLimitedResource(resolveResourceUrl(imageUri, chainId, baseUrl), MAX_REMOTE_IMAGE_BYTES, fetchImpl);
|
|
352
|
+
if (!ALLOWED_IMAGE_MEDIA_TYPES.has(response.contentType)) throw new Error("NFT image response uses an unsupported content type.");
|
|
353
|
+
if (response.contentType === "image/svg+xml") assertSafeSvg(response.bytes);
|
|
354
|
+
return {
|
|
355
|
+
imageDataUrl: bytesToDataUrl(response.bytes, response.contentType),
|
|
356
|
+
imageMediaType: response.contentType
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
async function loadNftMetadata(input) {
|
|
360
|
+
if (!Number.isInteger(input.chainId) || input.chainId <= 0) throw new Error("NFT metadata requires a valid chainId.");
|
|
361
|
+
const hasTokenUri = typeof input.tokenUri === "string" && Boolean(input.tokenUri.trim());
|
|
362
|
+
const hasImageUri = typeof input.imageUri === "string" && Boolean(input.imageUri.trim());
|
|
363
|
+
if (hasTokenUri === hasImageUri) throw new Error("Provide exactly one NFT tokenUri or imageUri.");
|
|
364
|
+
const fetchImpl = input.fetchImpl ?? fetch;
|
|
365
|
+
if (hasImageUri) {
|
|
366
|
+
const imageUri = input.imageUri.trim();
|
|
367
|
+
return {
|
|
368
|
+
tokenUri: "",
|
|
369
|
+
source: sourceForUri(imageUri),
|
|
370
|
+
...await resolveImage(imageUri, input.chainId, fetchImpl)
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
const tokenUri = input.tokenUri.trim();
|
|
374
|
+
const inlineMetadata = parseInlineNftMetadata(tokenUri);
|
|
375
|
+
if (inlineMetadata) {
|
|
376
|
+
return {
|
|
377
|
+
tokenUri,
|
|
378
|
+
source: "data-json",
|
|
379
|
+
name: inlineMetadata.name,
|
|
380
|
+
description: inlineMetadata.description,
|
|
381
|
+
attributes: inlineMetadata.attributes,
|
|
382
|
+
...await resolveImage(inlineMetadata.imageUri, input.chainId, fetchImpl)
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
const metadataResponse = await fetchLimitedResource(resolveResourceUrl(tokenUri, input.chainId), MAX_REMOTE_METADATA_BYTES, fetchImpl);
|
|
386
|
+
let metadataJson;
|
|
387
|
+
try {
|
|
388
|
+
metadataJson = JSON.parse(new TextDecoder().decode(metadataResponse.bytes));
|
|
389
|
+
} catch {
|
|
390
|
+
throw new Error("NFT metadata response is not valid JSON.");
|
|
391
|
+
}
|
|
392
|
+
const metadata = sanitizeNftMetadataRecord(metadataJson);
|
|
393
|
+
return {
|
|
394
|
+
tokenUri,
|
|
395
|
+
source: sourceForUri(tokenUri),
|
|
396
|
+
name: metadata.name,
|
|
397
|
+
description: metadata.description,
|
|
398
|
+
attributes: metadata.attributes,
|
|
399
|
+
...await resolveImage(metadata.imageUri, input.chainId, fetchImpl, metadataResponse.finalUrl)
|
|
400
|
+
};
|
|
401
|
+
}
|
|
482
402
|
|
|
483
|
-
export { FLAP_RUNTIME_ORACLE_REGISTRY_ENV,
|
|
403
|
+
export { FLAP_RUNTIME_ORACLE_REGISTRY_ENV, loadDefaultRuntimeOracle, loadNftMetadata, loadRuntimeOracle, parseRuntimeOracleRegistry, resolveRuntimeOracleProvision };
|
|
484
404
|
//# sourceMappingURL=server.js.map
|
|
485
405
|
//# sourceMappingURL=server.js.map
|