@pacspace-io/sdk 0.15.1 → 0.15.2
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/dist/index.js +359 -17
- package/dist/index.js.map +4 -4
- package/dist/inputs.txt +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -457,7 +457,7 @@ var HttpClient = class {
|
|
|
457
457
|
const headers = {
|
|
458
458
|
"X-Api-Key": this.apiKey,
|
|
459
459
|
"Content-Type": "application/json",
|
|
460
|
-
"User-Agent": "@pacspace-io/sdk/0.15.
|
|
460
|
+
"User-Agent": "@pacspace-io/sdk/0.15.2"
|
|
461
461
|
};
|
|
462
462
|
if (options?.dashboardToken) {
|
|
463
463
|
headers.Authorization = `Bearer ${options.dashboardToken}`;
|
|
@@ -3239,6 +3239,150 @@ var MIRROR_NODE_BY_CHAIN = Object.freeze({
|
|
|
3239
3239
|
295: "https://mainnet-public.mirrornode.hedera.com"
|
|
3240
3240
|
});
|
|
3241
3241
|
|
|
3242
|
+
// ../shared/src/records/ledger-write-search-v1.ts
|
|
3243
|
+
var LEDGER_WRITE_SEARCH_WINDOW_LIMIT_SECONDS = 7 * 24 * 60 * 60;
|
|
3244
|
+
var LEDGER_WRITE_SEARCH_PAGE_LIMIT = 100;
|
|
3245
|
+
var MIRROR_ANSWER_MAX_BYTES = 1048576;
|
|
3246
|
+
var HEX40 = /^0x[0-9a-f]{40}$/;
|
|
3247
|
+
var HEX642 = /^0x[0-9a-f]{64}$/;
|
|
3248
|
+
var HEX_DATA = /^0x[0-9a-f]*$/;
|
|
3249
|
+
var TIMESTAMP = /^\d+(\.\d+)?$/;
|
|
3250
|
+
var REFUSAL_CODES = ["redirect", "too-large", "too-many-logs", "next-page"];
|
|
3251
|
+
var MirrorAnswerRefused = class extends Error {
|
|
3252
|
+
constructor(code, message) {
|
|
3253
|
+
super(message);
|
|
3254
|
+
this.name = "MirrorAnswerRefused";
|
|
3255
|
+
this.code = code;
|
|
3256
|
+
}
|
|
3257
|
+
};
|
|
3258
|
+
function mirrorRefusalOf(error) {
|
|
3259
|
+
if (!error || typeof error !== "object") return null;
|
|
3260
|
+
const { name, code } = error;
|
|
3261
|
+
return name === "MirrorAnswerRefused" && typeof code === "string" && REFUSAL_CODES.includes(code) ? code : null;
|
|
3262
|
+
}
|
|
3263
|
+
function pathLabel(path) {
|
|
3264
|
+
return path ? ` (${path.split("?")[0]})` : "";
|
|
3265
|
+
}
|
|
3266
|
+
var mirrorRefusal = {
|
|
3267
|
+
redirect: (path) => new MirrorAnswerRefused("redirect", `mirror node answered with a redirect, which is not followed${pathLabel(path)}`),
|
|
3268
|
+
tooLarge: (maxBytes, path) => new MirrorAnswerRefused("too-large", `mirror node answer is larger than ${maxBytes} bytes${pathLabel(path)}`),
|
|
3269
|
+
tooManyLogs: (asked, path) => new MirrorAnswerRefused("too-many-logs", `mirror node returned more logs than the ${asked} asked for${pathLabel(path)}`),
|
|
3270
|
+
nextPage: () => new MirrorAnswerRefused("next-page", "mirror node gave a next page that is not this contract's log")
|
|
3271
|
+
};
|
|
3272
|
+
function isMirrorRedirect(response) {
|
|
3273
|
+
return response.redirected === true || response.type === "opaqueredirect" || response.status >= 300 && response.status < 400;
|
|
3274
|
+
}
|
|
3275
|
+
async function readMirrorText(response, maxBytes = MIRROR_ANSWER_MAX_BYTES, path) {
|
|
3276
|
+
const declared = Number(response.headers?.get?.("content-length") ?? Number.NaN);
|
|
3277
|
+
const body = response.body;
|
|
3278
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
3279
|
+
await body?.cancel?.().catch(() => void 0);
|
|
3280
|
+
throw mirrorRefusal.tooLarge(maxBytes, path);
|
|
3281
|
+
}
|
|
3282
|
+
if (body && typeof body.getReader === "function") {
|
|
3283
|
+
const reader = body.getReader();
|
|
3284
|
+
const decoder = new TextDecoder();
|
|
3285
|
+
let text2 = "";
|
|
3286
|
+
let read = 0;
|
|
3287
|
+
for (; ; ) {
|
|
3288
|
+
const { done, value } = await reader.read();
|
|
3289
|
+
if (done) break;
|
|
3290
|
+
read += value.byteLength;
|
|
3291
|
+
if (read > maxBytes) {
|
|
3292
|
+
await reader.cancel().catch(() => void 0);
|
|
3293
|
+
throw mirrorRefusal.tooLarge(maxBytes, path);
|
|
3294
|
+
}
|
|
3295
|
+
text2 += decoder.decode(value, { stream: true });
|
|
3296
|
+
}
|
|
3297
|
+
return text2 + decoder.decode();
|
|
3298
|
+
}
|
|
3299
|
+
const text = await response.text();
|
|
3300
|
+
if (new TextEncoder().encode(text).byteLength > maxBytes) throw mirrorRefusal.tooLarge(maxBytes, path);
|
|
3301
|
+
return text;
|
|
3302
|
+
}
|
|
3303
|
+
async function readMirrorJson(response, maxBytes = MIRROR_ANSWER_MAX_BYTES, path) {
|
|
3304
|
+
return JSON.parse(await readMirrorText(response, maxBytes, path));
|
|
3305
|
+
}
|
|
3306
|
+
function contractLogPath(contractAddress) {
|
|
3307
|
+
return `/api/v1/contracts/${contractAddress}/results/logs`;
|
|
3308
|
+
}
|
|
3309
|
+
function ledgerWriteSearchPath(input) {
|
|
3310
|
+
const query = `topic0=${input.topic0}&topic1=${input.recordKey}×tamp=gte:${input.lower}×tamp=lt:${input.upper}&order=desc&limit=${LEDGER_WRITE_SEARCH_PAGE_LIMIT}`;
|
|
3311
|
+
return `${contractLogPath(input.contractAddress)}?${query}`;
|
|
3312
|
+
}
|
|
3313
|
+
function nextLogPath(next, source) {
|
|
3314
|
+
if (typeof next !== "string" || next === "") return null;
|
|
3315
|
+
const contract = source.contract.toLowerCase();
|
|
3316
|
+
if (!HEX40.test(contract)) return null;
|
|
3317
|
+
if (!/^[\x21-\x5b\x5d-\x7e]+$/.test(next)) return null;
|
|
3318
|
+
if (next.startsWith("//")) return null;
|
|
3319
|
+
let base;
|
|
3320
|
+
let url;
|
|
3321
|
+
try {
|
|
3322
|
+
base = new URL(source.base);
|
|
3323
|
+
url = new URL(next, `${base.origin}/`);
|
|
3324
|
+
} catch {
|
|
3325
|
+
return null;
|
|
3326
|
+
}
|
|
3327
|
+
if (base.protocol !== "https:" && base.protocol !== "http:") return null;
|
|
3328
|
+
if (url.origin !== base.origin || url.username !== "" || url.password !== "" || url.hash !== "" || next.includes("#")) return null;
|
|
3329
|
+
const written = (next.startsWith("/") ? next : next.slice(next.indexOf("/", next.indexOf("//") + 2))).split("?")[0];
|
|
3330
|
+
if (/(^|\/)\.{1,2}(\/|$)/.test(written) || /%2e/i.test(written) || written !== url.pathname) return null;
|
|
3331
|
+
if (url.search.length < 2) return null;
|
|
3332
|
+
const logs = contractLogPath(contract);
|
|
3333
|
+
const prefix = base.pathname.replace(/\/+$/, "");
|
|
3334
|
+
const path = url.pathname.toLowerCase();
|
|
3335
|
+
if (path !== logs && !(prefix !== "" && url.pathname.startsWith(`${prefix}/`) && url.pathname.slice(prefix.length).toLowerCase() === logs)) return null;
|
|
3336
|
+
return `${logs}${url.search}`;
|
|
3337
|
+
}
|
|
3338
|
+
function withLogPageLimit(path, limit) {
|
|
3339
|
+
const at = path.indexOf("?");
|
|
3340
|
+
const before = at === -1 ? path : path.slice(0, at);
|
|
3341
|
+
const parts = at === -1 ? [] : path.slice(at + 1).split("&").filter((part) => part !== "");
|
|
3342
|
+
let placed = false;
|
|
3343
|
+
const out = [];
|
|
3344
|
+
for (const part of parts) {
|
|
3345
|
+
if (!part.startsWith("limit=")) out.push(part);
|
|
3346
|
+
else if (!placed) {
|
|
3347
|
+
out.push(`limit=${limit}`);
|
|
3348
|
+
placed = true;
|
|
3349
|
+
}
|
|
3350
|
+
}
|
|
3351
|
+
if (!placed) out.push(`limit=${limit}`);
|
|
3352
|
+
return `${before}?${out.join("&")}`;
|
|
3353
|
+
}
|
|
3354
|
+
function parseLedgerWriteLog(log, expected) {
|
|
3355
|
+
if (!log || typeof log !== "object") return null;
|
|
3356
|
+
const raw = log;
|
|
3357
|
+
const address = typeof raw.address === "string" ? raw.address.toLowerCase() : "";
|
|
3358
|
+
if (!HEX40.test(address) || address !== String(expected.contractAddress).toLowerCase()) return null;
|
|
3359
|
+
const topics = Array.isArray(raw.topics) ? raw.topics.map((topic) => String(topic).toLowerCase()) : [];
|
|
3360
|
+
if (topics[0] !== expected.topic0 || topics[1] !== expected.recordKey) return null;
|
|
3361
|
+
const data = typeof raw.data === "string" ? raw.data.toLowerCase() : "";
|
|
3362
|
+
const transactionHash = typeof raw.transaction_hash === "string" ? raw.transaction_hash.toLowerCase() : "";
|
|
3363
|
+
const timestamp = typeof raw.timestamp === "string" ? raw.timestamp : "";
|
|
3364
|
+
if (!HEX_DATA.test(data) || data.length < 2 + 64 * 3) return null;
|
|
3365
|
+
if (!HEX642.test(transactionHash) || !TIMESTAMP.test(timestamp)) return null;
|
|
3366
|
+
const encodingVersion = Number.parseInt(data.slice(2, 2 + 64).slice(-8), 16);
|
|
3367
|
+
const previousWord = data.slice(2 + 64, 2 + 64 * 2);
|
|
3368
|
+
const newWord = data.slice(2 + 64 * 2, 2 + 64 * 3);
|
|
3369
|
+
const [seconds, fraction = ""] = timestamp.split(".");
|
|
3370
|
+
const ms = Number(seconds) * 1e3 + Number(fraction.slice(0, 3).padEnd(3, "0"));
|
|
3371
|
+
if (!Number.isFinite(ms) || Math.abs(ms) > 864e13) return null;
|
|
3372
|
+
return {
|
|
3373
|
+
transactionHash,
|
|
3374
|
+
timestamp,
|
|
3375
|
+
committedAt: new Date(ms).toISOString(),
|
|
3376
|
+
previousWord: `0x${previousWord}`,
|
|
3377
|
+
newWord: `0x${newWord}`,
|
|
3378
|
+
operator: HEX642.test(topics[2] ?? "") ? `0x${topics[2].slice(26)}` : "",
|
|
3379
|
+
encodingVersion,
|
|
3380
|
+
address,
|
|
3381
|
+
blockNumber: Number.isSafeInteger(raw.block_number) ? raw.block_number : null,
|
|
3382
|
+
logIndex: Number.isSafeInteger(raw.index) ? raw.index : null
|
|
3383
|
+
};
|
|
3384
|
+
}
|
|
3385
|
+
|
|
3242
3386
|
// ../shared/src/demo/independent-verify-records.ts
|
|
3243
3387
|
var import_ethers11 = require("ethers");
|
|
3244
3388
|
|
|
@@ -3598,11 +3742,97 @@ function createRpcRecordChainReader(options) {
|
|
|
3598
3742
|
});
|
|
3599
3743
|
}
|
|
3600
3744
|
return out;
|
|
3745
|
+
},
|
|
3746
|
+
async findRecordWrites(recordKey, search) {
|
|
3747
|
+
const key = normalizeProofHash(recordKey, "recordKey");
|
|
3748
|
+
const now = search.now ?? Date.now;
|
|
3749
|
+
const deadline = now() + search.budgetMs;
|
|
3750
|
+
const writes = [];
|
|
3751
|
+
const blockTimes = /* @__PURE__ */ new Map();
|
|
3752
|
+
let requests = 0;
|
|
3753
|
+
const CUT = Symbol("cut");
|
|
3754
|
+
const within = async (p) => {
|
|
3755
|
+
const left = deadline - now();
|
|
3756
|
+
if (left <= 0) return CUT;
|
|
3757
|
+
let timer;
|
|
3758
|
+
const cut = new Promise((resolve) => {
|
|
3759
|
+
timer = setTimeout(() => resolve(CUT), left);
|
|
3760
|
+
});
|
|
3761
|
+
p.catch(() => void 0);
|
|
3762
|
+
try {
|
|
3763
|
+
return await Promise.race([p, cut]);
|
|
3764
|
+
} finally {
|
|
3765
|
+
if (timer) clearTimeout(timer);
|
|
3766
|
+
}
|
|
3767
|
+
};
|
|
3768
|
+
const end = (ended) => ({ writes, ended, requests });
|
|
3769
|
+
if (search.maxRequests < 1) return end("limit");
|
|
3770
|
+
requests += 1;
|
|
3771
|
+
const latest = await within(provider.getBlockNumber());
|
|
3772
|
+
if (latest === CUT) return end("timeout");
|
|
3773
|
+
for (let to = latest; to >= 0; to -= chunk) {
|
|
3774
|
+
if (search.done(writes)) return end("done");
|
|
3775
|
+
if (requests >= search.maxRequests || now() >= deadline) return end("limit");
|
|
3776
|
+
requests += 1;
|
|
3777
|
+
const logs = await within(provider.getLogs({ address: contractAddress, topics: [RECORD_WRITTEN_TOPIC, key], fromBlock: Math.max(0, to - chunk + 1), toBlock: to }));
|
|
3778
|
+
if (logs === CUT) return end("timeout");
|
|
3779
|
+
for (const log of [...logs].reverse()) {
|
|
3780
|
+
const parsed = iface.parseLog({ topics: [...log.topics], data: log.data });
|
|
3781
|
+
if (!parsed) continue;
|
|
3782
|
+
let committedAt = blockTimes.get(log.blockNumber);
|
|
3783
|
+
if (committedAt === void 0) {
|
|
3784
|
+
if (requests >= search.maxRequests || now() >= deadline) return end("limit");
|
|
3785
|
+
requests += 1;
|
|
3786
|
+
const block = await within(provider.getBlock(log.blockNumber));
|
|
3787
|
+
if (block === CUT) return end("timeout");
|
|
3788
|
+
if (!block || !Number.isSafeInteger(block.timestamp)) throw new Error(`getBlock ${log.blockNumber} returned no time`);
|
|
3789
|
+
committedAt = new Date(block.timestamp * 1e3).toISOString();
|
|
3790
|
+
blockTimes.set(log.blockNumber, committedAt);
|
|
3791
|
+
}
|
|
3792
|
+
writes.push({
|
|
3793
|
+
previousWord: hex256(BigInt(parsed.args.previousWord)),
|
|
3794
|
+
newWord: hex256(BigInt(parsed.args.newWord)),
|
|
3795
|
+
operator: String(parsed.args.operator).toLowerCase(),
|
|
3796
|
+
encodingVersion: Number(parsed.args.encodingVersion),
|
|
3797
|
+
blockNumber: log.blockNumber,
|
|
3798
|
+
transactionHash: String(log.transactionHash).toLowerCase(),
|
|
3799
|
+
logIndex: log.index,
|
|
3800
|
+
committedAt,
|
|
3801
|
+
contractAddress: String(log.address).toLowerCase()
|
|
3802
|
+
});
|
|
3803
|
+
}
|
|
3804
|
+
}
|
|
3805
|
+
return end(search.done(writes) ? "done" : "limit");
|
|
3601
3806
|
}
|
|
3602
3807
|
};
|
|
3603
3808
|
}
|
|
3604
3809
|
|
|
3605
3810
|
// ../shared/src/demo/mirror-node-record-chain-reader.ts
|
|
3811
|
+
var RECORD_WRITE_SEARCH_WINDOW_SECONDS = 6 * 24 * 60 * 60;
|
|
3812
|
+
var DAY_SECONDS = 24 * 60 * 60;
|
|
3813
|
+
var CONSENSUS_TIMESTAMP = /^\d{1,12}\.\d{9}$/;
|
|
3814
|
+
function committedAtOf(timestamp) {
|
|
3815
|
+
if (typeof timestamp !== "string" || !CONSENSUS_TIMESTAMP.test(timestamp)) return void 0;
|
|
3816
|
+
const [seconds, fraction] = timestamp.split(".");
|
|
3817
|
+
return new Date(Number(seconds) * 1e3 + Number(fraction.slice(0, 3))).toISOString();
|
|
3818
|
+
}
|
|
3819
|
+
var KEPT_TOPICS = [RECORD_WRITTEN_TOPIC.toLowerCase(), LEDGER_HEAD_ADVANCED_TOPIC.toLowerCase()];
|
|
3820
|
+
function keptMirrorLog(raw) {
|
|
3821
|
+
if (!raw || typeof raw !== "object") return null;
|
|
3822
|
+
const log = raw;
|
|
3823
|
+
if (!Array.isArray(log.topics) || typeof log.data !== "string") return null;
|
|
3824
|
+
const topics = log.topics.map((topic) => String(topic));
|
|
3825
|
+
if (!KEPT_TOPICS.includes((topics[0] ?? "").toLowerCase())) return null;
|
|
3826
|
+
return {
|
|
3827
|
+
address: log.address,
|
|
3828
|
+
data: log.data,
|
|
3829
|
+
index: log.index,
|
|
3830
|
+
topics,
|
|
3831
|
+
block_number: log.block_number,
|
|
3832
|
+
transaction_hash: log.transaction_hash,
|
|
3833
|
+
timestamp: log.timestamp
|
|
3834
|
+
};
|
|
3835
|
+
}
|
|
3606
3836
|
function createMirrorNodeRecordChainReader(options) {
|
|
3607
3837
|
const chainId = options.chainId ?? 296;
|
|
3608
3838
|
const base = (options.mirrorBaseUrl ?? MIRROR_NODE_BY_CHAIN[chainId] ?? "").replace(/\/+$/, "");
|
|
@@ -3614,22 +3844,28 @@ function createMirrorNodeRecordChainReader(options) {
|
|
|
3614
3844
|
const fetchImpl = options.fetchImpl ?? fetch;
|
|
3615
3845
|
const iface = new import_ethers12.ethers.Interface(RECORD_LANE_ABI_V38);
|
|
3616
3846
|
async function request(path, init) {
|
|
3617
|
-
const url =
|
|
3847
|
+
const url = `${base}${path}`;
|
|
3618
3848
|
let lastError = null;
|
|
3619
3849
|
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
3620
3850
|
const controller = new AbortController();
|
|
3621
3851
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
3622
3852
|
try {
|
|
3623
|
-
const response = await fetchImpl(url, { ...init, signal: controller.signal });
|
|
3853
|
+
const response = await fetchImpl(url, { ...init, redirect: "manual", signal: controller.signal });
|
|
3854
|
+
if (isMirrorRedirect(response)) {
|
|
3855
|
+
await response.body?.cancel?.().catch(() => void 0);
|
|
3856
|
+
throw mirrorRefusal.redirect(path);
|
|
3857
|
+
}
|
|
3624
3858
|
if (response.status === 429 || response.status >= 500) {
|
|
3859
|
+
await response.body?.cancel?.().catch(() => void 0);
|
|
3625
3860
|
lastError = new Error(`mirror node ${response.status} for ${path}`);
|
|
3626
3861
|
} else if (!response.ok) {
|
|
3627
|
-
const body = await response
|
|
3862
|
+
const body = await readMirrorText(response, MIRROR_ANSWER_MAX_BYTES, path).catch(() => "");
|
|
3628
3863
|
throw new Error(`mirror node ${response.status} for ${path}${body ? `: ${body.slice(0, 200)}` : ""}`);
|
|
3629
3864
|
} else {
|
|
3630
|
-
return await response
|
|
3865
|
+
return await readMirrorJson(response, MIRROR_ANSWER_MAX_BYTES, path);
|
|
3631
3866
|
}
|
|
3632
3867
|
} catch (error) {
|
|
3868
|
+
if (mirrorRefusalOf(error)) throw error;
|
|
3633
3869
|
if (error.name === "AbortError") lastError = new Error(`mirror node request timed out after ${timeoutMs}ms (${path})`);
|
|
3634
3870
|
else if (/^mirror node \d{3} for/.test(error.message) && !/^mirror node (429|5\d\d)/.test(error.message)) throw error;
|
|
3635
3871
|
else lastError = error;
|
|
@@ -3656,28 +3892,86 @@ function createMirrorNodeRecordChainReader(options) {
|
|
|
3656
3892
|
function matchesTopics(log, topics) {
|
|
3657
3893
|
return topics.every((topic, index) => !topic || (log.topics[index] ?? "").toLowerCase() === topic.toLowerCase());
|
|
3658
3894
|
}
|
|
3659
|
-
|
|
3895
|
+
let keptLogs = null;
|
|
3896
|
+
function readAllLogs() {
|
|
3897
|
+
if (!options.keepLogPages) return readAllLogPages();
|
|
3898
|
+
if (!keptLogs) {
|
|
3899
|
+
keptLogs = readAllLogPages();
|
|
3900
|
+
keptLogs.catch(() => {
|
|
3901
|
+
keptLogs = null;
|
|
3902
|
+
});
|
|
3903
|
+
}
|
|
3904
|
+
return keptLogs;
|
|
3905
|
+
}
|
|
3906
|
+
async function readAllLogPages() {
|
|
3660
3907
|
const params = new URLSearchParams();
|
|
3661
3908
|
params.set("order", "asc");
|
|
3662
3909
|
params.set("limit", String(pageSize));
|
|
3663
|
-
let next =
|
|
3910
|
+
let next = `${contractLogPath(contractAddress)}?${params.toString()}`;
|
|
3664
3911
|
const out = [];
|
|
3665
3912
|
let pages = 0;
|
|
3913
|
+
let size = pageSize;
|
|
3666
3914
|
while (next) {
|
|
3667
|
-
const page = await
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3915
|
+
const { page, asked } = await readLogPage(next, size);
|
|
3916
|
+
size = Math.min(pageSize, asked * 2);
|
|
3917
|
+
const logs = Array.isArray(page.logs) ? page.logs : [...page.logs ?? []];
|
|
3918
|
+
if (logs.length > asked) throw mirrorRefusal.tooManyLogs(asked, next);
|
|
3919
|
+
for (const raw of logs) {
|
|
3920
|
+
const log = keptMirrorLog(raw);
|
|
3921
|
+
if (log) out.push(log);
|
|
3922
|
+
}
|
|
3923
|
+
const link = page.links?.next;
|
|
3924
|
+
if (link === null || link === void 0 || link === "") {
|
|
3925
|
+
next = null;
|
|
3926
|
+
} else {
|
|
3927
|
+
const path = nextLogPath(link, { base, contract: contractAddress });
|
|
3928
|
+
if (!path) throw mirrorRefusal.nextPage();
|
|
3929
|
+
next = withLogPageLimit(path, size);
|
|
3673
3930
|
}
|
|
3674
|
-
next = page.links?.next ?? null;
|
|
3675
3931
|
pages += 1;
|
|
3676
3932
|
if (pages > 1e4) throw new Error("mirror node log scan exceeded 10,000 pages");
|
|
3677
3933
|
}
|
|
3934
|
+
return out;
|
|
3935
|
+
}
|
|
3936
|
+
async function readLogPage(path, start) {
|
|
3937
|
+
let asked = start;
|
|
3938
|
+
for (; ; ) {
|
|
3939
|
+
try {
|
|
3940
|
+
return { page: await request(withLogPageLimit(path, asked)), asked };
|
|
3941
|
+
} catch (error) {
|
|
3942
|
+
if (mirrorRefusalOf(error) !== "too-large" || asked <= 1) throw error;
|
|
3943
|
+
asked = Math.max(1, Math.floor(asked / 2));
|
|
3944
|
+
}
|
|
3945
|
+
}
|
|
3946
|
+
}
|
|
3947
|
+
async function scanLogs(topics, fromBlock, toBlock) {
|
|
3948
|
+
const out = [];
|
|
3949
|
+
for (const log of await readAllLogs()) {
|
|
3950
|
+
if (!matchesTopics(log, topics)) continue;
|
|
3951
|
+
if (log.block_number < fromBlock) continue;
|
|
3952
|
+
if (toBlock !== "latest" && log.block_number > toBlock) continue;
|
|
3953
|
+
out.push(log);
|
|
3954
|
+
}
|
|
3678
3955
|
out.sort((a, b) => a.block_number - b.block_number || a.index - b.index);
|
|
3679
3956
|
return out;
|
|
3680
3957
|
}
|
|
3958
|
+
async function searchPage(path, ms) {
|
|
3959
|
+
const controller = new AbortController();
|
|
3960
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
3961
|
+
try {
|
|
3962
|
+
const response = await fetchImpl(`${base}${path}`, { headers: { accept: "application/json" }, redirect: "error", signal: controller.signal });
|
|
3963
|
+
if (isMirrorRedirect(response)) throw mirrorRefusal.redirect(path);
|
|
3964
|
+
if (!response.ok) throw new Error(`mirror node ${response.status} for a record's writes`);
|
|
3965
|
+
const page = await readMirrorJson(response, MIRROR_ANSWER_MAX_BYTES, path);
|
|
3966
|
+
if (Array.isArray(page.logs) && page.logs.length > LEDGER_WRITE_SEARCH_PAGE_LIMIT) throw mirrorRefusal.tooManyLogs(LEDGER_WRITE_SEARCH_PAGE_LIMIT, path);
|
|
3967
|
+
return page;
|
|
3968
|
+
} catch (error) {
|
|
3969
|
+
if (controller.signal.aborted) return "timeout";
|
|
3970
|
+
throw error;
|
|
3971
|
+
} finally {
|
|
3972
|
+
clearTimeout(timer);
|
|
3973
|
+
}
|
|
3974
|
+
}
|
|
3681
3975
|
return {
|
|
3682
3976
|
contractAddress,
|
|
3683
3977
|
async getRecord(recordKey) {
|
|
@@ -3735,10 +4029,58 @@ function createMirrorNodeRecordChainReader(options) {
|
|
|
3735
4029
|
encodingVersion: Number(parsed.args.encodingVersion),
|
|
3736
4030
|
blockNumber: log.block_number,
|
|
3737
4031
|
transactionHash: log.transaction_hash,
|
|
3738
|
-
logIndex: log.index
|
|
4032
|
+
logIndex: log.index,
|
|
4033
|
+
committedAt: committedAtOf(log.timestamp),
|
|
4034
|
+
contractAddress: typeof log.address === "string" ? log.address.toLowerCase() : void 0
|
|
3739
4035
|
});
|
|
3740
4036
|
}
|
|
3741
4037
|
return out;
|
|
4038
|
+
},
|
|
4039
|
+
async findRecordWrites(recordKey, search) {
|
|
4040
|
+
const key = normalizeProofHash(recordKey, "recordKey").toLowerCase();
|
|
4041
|
+
const topic0 = RECORD_WRITTEN_TOPIC.toLowerCase();
|
|
4042
|
+
const now = search.now ?? Date.now;
|
|
4043
|
+
const deadline = now() + search.budgetMs;
|
|
4044
|
+
const windowSeconds = Math.max(1, Math.floor(search.windowSeconds ?? RECORD_WRITE_SEARCH_WINDOW_SECONDS));
|
|
4045
|
+
const nowSeconds = Math.floor(now() / 1e3);
|
|
4046
|
+
const hint = search.startAt ? Date.parse(search.startAt) : Number.NaN;
|
|
4047
|
+
let upper = Math.min(Number.isFinite(hint) ? Math.floor(hint / 1e3) : nowSeconds, nowSeconds) + DAY_SECONDS;
|
|
4048
|
+
const writes = [];
|
|
4049
|
+
let requests = 0;
|
|
4050
|
+
const end = (ended) => ({ writes, ended, requests });
|
|
4051
|
+
while (!search.done(writes)) {
|
|
4052
|
+
if (upper <= 0) return end("limit");
|
|
4053
|
+
const lower = Math.max(0, upper - windowSeconds);
|
|
4054
|
+
let path = ledgerWriteSearchPath({ contractAddress, topic0, recordKey: key, lower, upper });
|
|
4055
|
+
while (path) {
|
|
4056
|
+
const left = deadline - now();
|
|
4057
|
+
if (requests >= search.maxRequests || left <= 0) return end("limit");
|
|
4058
|
+
requests += 1;
|
|
4059
|
+
const page = await searchPage(path, left);
|
|
4060
|
+
if (page === "timeout") return end("timeout");
|
|
4061
|
+
for (const log of Array.isArray(page?.logs) ? page.logs : []) {
|
|
4062
|
+
const write = parseLedgerWriteLog(log, { topic0, recordKey: key, contractAddress });
|
|
4063
|
+
if (!write || !CONSENSUS_TIMESTAMP.test(write.timestamp)) continue;
|
|
4064
|
+
writes.push({
|
|
4065
|
+
previousWord: write.previousWord,
|
|
4066
|
+
newWord: write.newWord,
|
|
4067
|
+
operator: write.operator,
|
|
4068
|
+
encodingVersion: write.encodingVersion,
|
|
4069
|
+
blockNumber: write.blockNumber ?? void 0,
|
|
4070
|
+
transactionHash: write.transactionHash,
|
|
4071
|
+
logIndex: write.logIndex ?? void 0,
|
|
4072
|
+
committedAt: write.committedAt,
|
|
4073
|
+
contractAddress: write.address
|
|
4074
|
+
});
|
|
4075
|
+
}
|
|
4076
|
+
if (search.done(writes)) return end("done");
|
|
4077
|
+
const link = page?.links?.next;
|
|
4078
|
+
path = nextLogPath(link, { base, contract: contractAddress });
|
|
4079
|
+
if (!path && link !== null && link !== void 0 && link !== "") throw mirrorRefusal.nextPage();
|
|
4080
|
+
}
|
|
4081
|
+
upper = lower;
|
|
4082
|
+
}
|
|
4083
|
+
return end("done");
|
|
3742
4084
|
}
|
|
3743
4085
|
};
|
|
3744
4086
|
}
|
|
@@ -6844,7 +7186,7 @@ async function readWorkspaceSecretFile(path) {
|
|
|
6844
7186
|
var import_promises = require("node:fs/promises");
|
|
6845
7187
|
var import_node_path = require("node:path");
|
|
6846
7188
|
var PIN_FILE_SCHEMA = "pacspace-pins/v1";
|
|
6847
|
-
var
|
|
7189
|
+
var HEX643 = /^[0-9a-f]{64}$/;
|
|
6848
7190
|
function filePinStore(path) {
|
|
6849
7191
|
const read = async () => {
|
|
6850
7192
|
let text;
|
|
@@ -6864,7 +7206,7 @@ function filePinStore(path) {
|
|
|
6864
7206
|
return {
|
|
6865
7207
|
async get(workspaceId) {
|
|
6866
7208
|
const pin = (await read())[workspaceId];
|
|
6867
|
-
return pin && typeof pin.publicKeyId === "string" &&
|
|
7209
|
+
return pin && typeof pin.publicKeyId === "string" && HEX643.test(pin.publicKeyId) ? { ...pin } : null;
|
|
6868
7210
|
},
|
|
6869
7211
|
async set(workspaceId, pin) {
|
|
6870
7212
|
const pins = await read();
|