@inversealtruism/cairnx-core 0.2.9 → 0.2.11
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/CHANGELOG.md +51 -1
- package/CONVENTION.md +5 -0
- package/README.md +14 -0
- package/dist/index.cjs +265 -160
- package/dist/index.d.cts +49 -17
- package/dist/index.d.ts +49 -17
- package/dist/index.js +257 -156
- package/dist/mm/index.d.cts +1 -1
- package/dist/mm/index.d.ts +1 -1
- package/package.json +2 -2
- package/test/vectors/cases-rules.json +671 -0
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { EPOCH_LEN, MAX_URI_BYTES, epochOf } from "@inversealtruism/csd-codec";
|
|
|
3
3
|
var DOMAIN = "cairnx:v1";
|
|
4
4
|
var ACTIVATION_HEIGHT = 29860;
|
|
5
5
|
var beforeActivation = (height) => height < ACTIVATION_HEIGHT;
|
|
6
|
+
var isChainHeight = (height) => Number.isSafeInteger(height);
|
|
6
7
|
var V11_HEIGHT = 29960;
|
|
7
8
|
var V12_HEIGHT = 30300;
|
|
8
9
|
var V13_HEIGHT = 31100;
|
|
@@ -1825,7 +1826,7 @@ function replay(events, tipHeight, opts = {}) {
|
|
|
1825
1826
|
for (const e of [...events].sort(applyOrder)) fold.apply(e);
|
|
1826
1827
|
return fold.finish(tipHeight);
|
|
1827
1828
|
}
|
|
1828
|
-
function createFold(opts) {
|
|
1829
|
+
function createFold(opts = {}) {
|
|
1829
1830
|
const seenIds = /* @__PURE__ */ new Set();
|
|
1830
1831
|
const store = createStore(opts);
|
|
1831
1832
|
const c = createContext(store);
|
|
@@ -1836,7 +1837,7 @@ function createFold(opts) {
|
|
|
1836
1837
|
c.pendingCancels = [];
|
|
1837
1838
|
};
|
|
1838
1839
|
const inDomain = (height) => {
|
|
1839
|
-
if (!
|
|
1840
|
+
if (!isChainHeight(height)) store.endSplit();
|
|
1840
1841
|
};
|
|
1841
1842
|
const apply = (ev) => {
|
|
1842
1843
|
inDomain(ev.height);
|
|
@@ -1982,6 +1983,233 @@ function canonicalState(s) {
|
|
|
1982
1983
|
return JSON.stringify(sortKeys(data));
|
|
1983
1984
|
}
|
|
1984
1985
|
|
|
1986
|
+
// src/events.ts
|
|
1987
|
+
import { isWellFormedUtf16 as isWellFormedUtf162 } from "@inversealtruism/csd-codec";
|
|
1988
|
+
|
|
1989
|
+
// src/paidto.ts
|
|
1990
|
+
function paidToFromOutputs(outputs) {
|
|
1991
|
+
const m = /* @__PURE__ */ Object.create(null);
|
|
1992
|
+
for (const o of outputs) {
|
|
1993
|
+
if (typeof o.value !== "number" || !Number.isSafeInteger(o.value) || o.value < 0) continue;
|
|
1994
|
+
m[o.addr] = (BigInt(m[o.addr] ?? "0") + BigInt(o.value)).toString();
|
|
1995
|
+
}
|
|
1996
|
+
return m;
|
|
1997
|
+
}
|
|
1998
|
+
function burnedPayment(paidTo, payer, offer2) {
|
|
1999
|
+
const read = (addr) => addr === payer ? 0n : ptAmt(paidTo?.[addr]);
|
|
2000
|
+
let counterparty = 0n;
|
|
2001
|
+
if (offer2) {
|
|
2002
|
+
for (const addr of /* @__PURE__ */ new Set([offer2.seller, offer2.want.payto])) if (addr !== TREASURY_ADDR) counterparty += read(addr);
|
|
2003
|
+
}
|
|
2004
|
+
return { treasury: read(TREASURY_ADDR), counterparty };
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
// src/events.ts
|
|
2008
|
+
var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
|
|
2009
|
+
var fail = (what) => {
|
|
2010
|
+
throw new Error(`events rule: ${what}`);
|
|
2011
|
+
};
|
|
2012
|
+
var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
2013
|
+
function hexOf(v, bytes, what) {
|
|
2014
|
+
const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
|
|
2015
|
+
if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
|
|
2016
|
+
return "0x" + h.toLowerCase();
|
|
2017
|
+
}
|
|
2018
|
+
function u64Of(v, what) {
|
|
2019
|
+
if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
|
|
2020
|
+
if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
|
|
2021
|
+
if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
|
|
2022
|
+
return fail(`${what} is not a u64`);
|
|
2023
|
+
}
|
|
2024
|
+
function u32Of(v, what) {
|
|
2025
|
+
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
|
|
2026
|
+
}
|
|
2027
|
+
function checkRule(rule) {
|
|
2028
|
+
const d = rule?.domain;
|
|
2029
|
+
if (typeof d !== "string" || d === "" || !isWellFormedUtf162(d) || d.includes("\0") || d.includes(""))
|
|
2030
|
+
fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
|
|
2031
|
+
if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
|
|
2032
|
+
}
|
|
2033
|
+
var kindOf = (app) => {
|
|
2034
|
+
const t = app?.type;
|
|
2035
|
+
return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
|
|
2036
|
+
};
|
|
2037
|
+
var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
|
|
2038
|
+
function paidToOf(outputs) {
|
|
2039
|
+
if (!Array.isArray(outputs)) fail("outputs is not a list");
|
|
2040
|
+
const counted = [];
|
|
2041
|
+
for (const o of outputs) {
|
|
2042
|
+
const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
|
|
2043
|
+
if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
|
|
2044
|
+
}
|
|
2045
|
+
return paidToFromOutputs(counted);
|
|
2046
|
+
}
|
|
2047
|
+
function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
|
|
2048
|
+
checkRule(rule);
|
|
2049
|
+
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2050
|
+
if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
|
|
2051
|
+
const txid = hexOf(t.txid, 32, "txid");
|
|
2052
|
+
const app = t.tx?.app;
|
|
2053
|
+
const kind = kindOf(app);
|
|
2054
|
+
if (kind === "none" || height < rule.activationHeight) return null;
|
|
2055
|
+
if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2056
|
+
if (kind === "propose" && domainOf(app) !== rule.domain) return null;
|
|
2057
|
+
const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
|
|
2058
|
+
if (kind === "propose") {
|
|
2059
|
+
const p = app;
|
|
2060
|
+
const uri = p.uri;
|
|
2061
|
+
if (typeof uri !== "string" || !isWellFormedUtf162(uri)) return fail("uri is not well-formed text");
|
|
2062
|
+
const e = u64Of(p.expiresEpoch, "expiresEpoch");
|
|
2063
|
+
return {
|
|
2064
|
+
kind: "propose",
|
|
2065
|
+
id: txid,
|
|
2066
|
+
proposer: who,
|
|
2067
|
+
uri: uri.replaceAll("\0", ""),
|
|
2068
|
+
payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
|
|
2069
|
+
expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
|
|
2070
|
+
height,
|
|
2071
|
+
pos: t.pos,
|
|
2072
|
+
paidTo
|
|
2073
|
+
};
|
|
2074
|
+
}
|
|
2075
|
+
const a = app;
|
|
2076
|
+
return {
|
|
2077
|
+
kind: "attest",
|
|
2078
|
+
txid,
|
|
2079
|
+
proposalId: hexOf(a.proposalId, 32, "proposalId"),
|
|
2080
|
+
attester: who,
|
|
2081
|
+
score: u32Of(a.score, "score"),
|
|
2082
|
+
confidence: u32Of(a.confidence, "confidence"),
|
|
2083
|
+
height,
|
|
2084
|
+
pos: t.pos,
|
|
2085
|
+
paidTo
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2088
|
+
function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
|
|
2089
|
+
checkRule(rule);
|
|
2090
|
+
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2091
|
+
if (height < rule.activationHeight) return [];
|
|
2092
|
+
if (!Array.isArray(txs)) fail("txs is not a list");
|
|
2093
|
+
const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
|
|
2094
|
+
const read = [];
|
|
2095
|
+
let last = -1;
|
|
2096
|
+
for (const item of txs) {
|
|
2097
|
+
if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
|
|
2098
|
+
last = item.pos;
|
|
2099
|
+
const id = hexOf(item.txid, 32, "txid");
|
|
2100
|
+
if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
|
|
2101
|
+
ids.add(id);
|
|
2102
|
+
const kind = kindOf(item.tx?.app);
|
|
2103
|
+
if (kind === "none") continue;
|
|
2104
|
+
if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2105
|
+
if (kind === "propose") {
|
|
2106
|
+
if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
|
|
2107
|
+
proposals.add(id);
|
|
2108
|
+
read.push({ item, kind, id });
|
|
2109
|
+
} else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
|
|
2110
|
+
}
|
|
2111
|
+
return read.filter((r) => r.kind === "propose" || !sofar.eventIds.has(r.id) && (sofar.proposeIds.has(r.proposal) || proposals.has(r.proposal))).map((r) => ({ kind: r.kind, item: r.item }));
|
|
2112
|
+
}
|
|
2113
|
+
function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
|
|
2114
|
+
const members = blockMembers(height, txs, sofar, rule);
|
|
2115
|
+
if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
|
|
2116
|
+
const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
|
|
2117
|
+
return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
// src/replay-context.ts
|
|
2121
|
+
var ReplayContextRefusal = class extends Error {
|
|
2122
|
+
code;
|
|
2123
|
+
constructor(code, message) {
|
|
2124
|
+
super(message);
|
|
2125
|
+
this.name = "ReplayContextRefusal";
|
|
2126
|
+
this.code = code;
|
|
2127
|
+
}
|
|
2128
|
+
};
|
|
2129
|
+
var strictCode = /* @__PURE__ */ (function() {
|
|
2130
|
+
return this === void 0;
|
|
2131
|
+
})();
|
|
2132
|
+
function newReplayContext() {
|
|
2133
|
+
if (!strictCode) throw new ReplayContextRefusal("sloppy", "this build runs cairnx-core in sloppy mode, where a write to a frozen object is ignored");
|
|
2134
|
+
const fold = createFold();
|
|
2135
|
+
let height = -1;
|
|
2136
|
+
let broken = false;
|
|
2137
|
+
const live = () => {
|
|
2138
|
+
if (broken) throw new ReplayContextRefusal("broken", "the replay threw inside advance or stateAt; replay with resolve()");
|
|
2139
|
+
};
|
|
2140
|
+
return {
|
|
2141
|
+
get height() {
|
|
2142
|
+
return height;
|
|
2143
|
+
},
|
|
2144
|
+
advance(toHeight, events) {
|
|
2145
|
+
live();
|
|
2146
|
+
if (!isChainHeight(toHeight)) throw new ReplayContextRefusal("domain", `target height ${String(toHeight)} is not a safe integer`);
|
|
2147
|
+
const batch = [...events].map((e) => ({ ...e }));
|
|
2148
|
+
for (const e of batch) {
|
|
2149
|
+
if (!isChainHeight(e.height)) throw new ReplayContextRefusal("domain", `event height ${String(e.height)} is not a safe integer`);
|
|
2150
|
+
if (!isIndex(e.pos)) throw new ReplayContextRefusal("domain", `event position ${String(e.pos)} is not a safe integer >= 0`);
|
|
2151
|
+
if (typeof eventId(e) !== "string") throw new ReplayContextRefusal("domain", `an event id of type ${typeof eventId(e)}`);
|
|
2152
|
+
}
|
|
2153
|
+
if (toHeight < height) throw new ReplayContextRefusal("order", `target ${toHeight} is below the context's height ${height}`);
|
|
2154
|
+
for (const e of batch) {
|
|
2155
|
+
if (e.height <= height || e.height > toHeight) throw new ReplayContextRefusal("order", `event height ${e.height} is outside (${height}, ${toHeight}]`);
|
|
2156
|
+
}
|
|
2157
|
+
batch.sort(applyOrder);
|
|
2158
|
+
try {
|
|
2159
|
+
for (const e of batch) fold.apply(e);
|
|
2160
|
+
} catch (err) {
|
|
2161
|
+
broken = true;
|
|
2162
|
+
throw err;
|
|
2163
|
+
}
|
|
2164
|
+
height = toHeight;
|
|
2165
|
+
},
|
|
2166
|
+
stateAt(tipHeight) {
|
|
2167
|
+
live();
|
|
2168
|
+
if (tipHeight !== height) throw new ReplayContextRefusal("tip", `an answer at ${String(tipHeight)} from a context at ${height}`);
|
|
2169
|
+
try {
|
|
2170
|
+
return detach(fold.finish(tipHeight));
|
|
2171
|
+
} catch (err) {
|
|
2172
|
+
broken = true;
|
|
2173
|
+
throw err;
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
};
|
|
2177
|
+
}
|
|
2178
|
+
var freezeDeep = (v) => {
|
|
2179
|
+
if (v === null || typeof v !== "object" || Object.isFrozen(v)) return;
|
|
2180
|
+
Object.freeze(v);
|
|
2181
|
+
for (const x of Object.values(v)) freezeDeep(x);
|
|
2182
|
+
};
|
|
2183
|
+
var copyRecord = (r) => {
|
|
2184
|
+
const out = {};
|
|
2185
|
+
for (const [k, v] of Object.entries(r)) {
|
|
2186
|
+
if (Array.isArray(v)) {
|
|
2187
|
+
for (const x of v) freezeDeep(x);
|
|
2188
|
+
out[k] = [...v];
|
|
2189
|
+
} else {
|
|
2190
|
+
freezeDeep(v);
|
|
2191
|
+
out[k] = v;
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
return out;
|
|
2195
|
+
};
|
|
2196
|
+
function detach(st) {
|
|
2197
|
+
const offers = {};
|
|
2198
|
+
for (const [id, o] of Object.entries(st.offers)) {
|
|
2199
|
+
if (o.status === "open") offers[id] = copyRecord(o);
|
|
2200
|
+
else {
|
|
2201
|
+
freezeDeep(o);
|
|
2202
|
+
offers[id] = o;
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
const bids = {};
|
|
2206
|
+
for (const [id, b] of Object.entries(st.bids)) bids[id] = copyRecord(b);
|
|
2207
|
+
for (const n of Object.values(st.names)) freezeDeep(n.profile);
|
|
2208
|
+
const events = st.events.slice();
|
|
2209
|
+
for (const e of events) freezeDeep(e);
|
|
2210
|
+
return { ...st, offers, bids, events };
|
|
2211
|
+
}
|
|
2212
|
+
|
|
1985
2213
|
// src/client.ts
|
|
1986
2214
|
import { blockReward, INITIAL_REWARD } from "@inversealtruism/csd-codec";
|
|
1987
2215
|
var CLAIM_ATTACKER_Q = 0.2;
|
|
@@ -1998,6 +2226,27 @@ function requiredClaimDepth(valueSats, height) {
|
|
|
1998
2226
|
return { depth: max, reversalPct: Math.pow(r, max) * 100, capped: true };
|
|
1999
2227
|
}
|
|
2000
2228
|
|
|
2229
|
+
// src/holdcount.ts
|
|
2230
|
+
var lastLegacyClaimEnd = V28_HEIGHT - 1 + claimWindowAt(V28_HEIGHT - 1);
|
|
2231
|
+
var LEGACY_HOLD_SUNSET_HEIGHT = lastLegacyClaimEnd + claimGraceOf(lastLegacyClaimEnd);
|
|
2232
|
+
function countOtherHoldsAtGrant(candidates, target, me, closed = /* @__PURE__ */ new Set()) {
|
|
2233
|
+
if (!(Number.isSafeInteger(target.height) && target.height >= LEGACY_HOLD_SUNSET_HEIGHT))
|
|
2234
|
+
return { ok: false, reason: `this reservation was granted at ${target.height}, before block ${LEGACY_HOLD_SUNSET_HEIGHT}, while claims made before V28 could still count against the cap; it cannot be checked here` };
|
|
2235
|
+
const meL = String(me).toLowerCase(), tid = String(target.id).toLowerCase(), toffer = String(target.offer).toLowerCase();
|
|
2236
|
+
const offers = /* @__PURE__ */ new Set();
|
|
2237
|
+
for (const c of candidates) {
|
|
2238
|
+
const cid = String(c.id).toLowerCase();
|
|
2239
|
+
if (cid === tid || closed.has(cid) || String(c.proposer).toLowerCase() !== meL) continue;
|
|
2240
|
+
if (!(c.height < target.height || c.height === target.height && c.pos < target.pos)) continue;
|
|
2241
|
+
if (!(fclaimHoldEnd(Number(c.expiresEpoch)) >= target.height)) continue;
|
|
2242
|
+
const rec = parseRecord(String(c.uri), String(c.payloadHash));
|
|
2243
|
+
if (!rec || rec.t !== "fclaim") continue;
|
|
2244
|
+
const offer2 = String(rec.offer).toLowerCase();
|
|
2245
|
+
if (offer2 !== toffer) offers.add(offer2);
|
|
2246
|
+
}
|
|
2247
|
+
return { ok: true, count: offers.size };
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2001
2250
|
// src/preflight.ts
|
|
2002
2251
|
function previewFill(offer2, payRaw) {
|
|
2003
2252
|
const pay = BigInt(payRaw);
|
|
@@ -2106,6 +2355,7 @@ function fillEndorsement(offer2, me, pay, tip, opts) {
|
|
|
2106
2355
|
} else if (holdRouted) {
|
|
2107
2356
|
return refuse("v2.8: a live fclaim hold routes this fill - pass opts.fillTargetId (use fillTargetId(offer, tip)) so the endorsement can verify what your attest targets");
|
|
2108
2357
|
}
|
|
2358
|
+
if (tip >= LEGACY_HOLD_SUNSET_HEIGHT && isOpenClaimLane(offer2, tip) && !holdRouted) return refuse(`from block ${LEGACY_HOLD_SUNSET_HEIGHT} an open CSD offer is filled only through a reservation: an attest on the offer id is chain-rejected after the payment moved (reserve the offer and attest the fclaim txid)`);
|
|
2109
2359
|
if (isTokenWant(offer2.want))
|
|
2110
2360
|
return { verdict: "not-endorsable", reason: "token-priced want - deliverability is the attester's token balance, which a pure record predicate cannot see; NOT an endorsement and NOT a refusal (verify your token balance + proven terms, then proceed)", preview };
|
|
2111
2361
|
if (!preview.deliverable) {
|
|
@@ -2170,7 +2420,7 @@ function nameView(names, name) {
|
|
|
2170
2420
|
}
|
|
2171
2421
|
|
|
2172
2422
|
// src/version.ts
|
|
2173
|
-
var CAIRNX_CORE_VERSION = "0.2.
|
|
2423
|
+
var CAIRNX_CORE_VERSION = "0.2.11";
|
|
2174
2424
|
|
|
2175
2425
|
// src/gates.ts
|
|
2176
2426
|
var GATE_HEIGHTS = Object.freeze({
|
|
@@ -2360,138 +2610,6 @@ function bindProvenOffer(offerEv) {
|
|
|
2360
2610
|
return { payto, seller, terms: provenOfferTerms(rec, offerEv.height) };
|
|
2361
2611
|
}
|
|
2362
2612
|
|
|
2363
|
-
// src/paidto.ts
|
|
2364
|
-
function paidToFromOutputs(outputs) {
|
|
2365
|
-
const m = /* @__PURE__ */ Object.create(null);
|
|
2366
|
-
for (const o of outputs) {
|
|
2367
|
-
if (typeof o.value !== "number" || !Number.isSafeInteger(o.value) || o.value < 0) continue;
|
|
2368
|
-
m[o.addr] = (BigInt(m[o.addr] ?? "0") + BigInt(o.value)).toString();
|
|
2369
|
-
}
|
|
2370
|
-
return m;
|
|
2371
|
-
}
|
|
2372
|
-
function burnedPayment(paidTo, payer, offer2) {
|
|
2373
|
-
const read = (addr) => addr === payer ? 0n : ptAmt(paidTo?.[addr]);
|
|
2374
|
-
let counterparty = 0n;
|
|
2375
|
-
if (offer2) {
|
|
2376
|
-
for (const addr of /* @__PURE__ */ new Set([offer2.seller, offer2.want.payto])) if (addr !== TREASURY_ADDR) counterparty += read(addr);
|
|
2377
|
-
}
|
|
2378
|
-
return { treasury: read(TREASURY_ADDR), counterparty };
|
|
2379
|
-
}
|
|
2380
|
-
|
|
2381
|
-
// src/events.ts
|
|
2382
|
-
import { isWellFormedUtf16 as isWellFormedUtf162 } from "@inversealtruism/csd-codec";
|
|
2383
|
-
var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
|
|
2384
|
-
var fail = (what) => {
|
|
2385
|
-
throw new Error(`events rule: ${what}`);
|
|
2386
|
-
};
|
|
2387
|
-
var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
2388
|
-
function hexOf(v, bytes, what) {
|
|
2389
|
-
const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
|
|
2390
|
-
if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
|
|
2391
|
-
return "0x" + h.toLowerCase();
|
|
2392
|
-
}
|
|
2393
|
-
function u64Of(v, what) {
|
|
2394
|
-
if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
|
|
2395
|
-
if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
|
|
2396
|
-
if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
|
|
2397
|
-
return fail(`${what} is not a u64`);
|
|
2398
|
-
}
|
|
2399
|
-
function u32Of(v, what) {
|
|
2400
|
-
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
|
|
2401
|
-
}
|
|
2402
|
-
function checkRule(rule) {
|
|
2403
|
-
const d = rule?.domain;
|
|
2404
|
-
if (typeof d !== "string" || d === "" || !isWellFormedUtf162(d) || d.includes("\0") || d.includes(""))
|
|
2405
|
-
fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
|
|
2406
|
-
if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
|
|
2407
|
-
}
|
|
2408
|
-
var kindOf = (app) => {
|
|
2409
|
-
const t = app?.type;
|
|
2410
|
-
return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
|
|
2411
|
-
};
|
|
2412
|
-
var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
|
|
2413
|
-
function paidToOf(outputs) {
|
|
2414
|
-
if (!Array.isArray(outputs)) fail("outputs is not a list");
|
|
2415
|
-
const counted = [];
|
|
2416
|
-
for (const o of outputs) {
|
|
2417
|
-
const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
|
|
2418
|
-
if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
|
|
2419
|
-
}
|
|
2420
|
-
return paidToFromOutputs(counted);
|
|
2421
|
-
}
|
|
2422
|
-
function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
|
|
2423
|
-
checkRule(rule);
|
|
2424
|
-
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2425
|
-
if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
|
|
2426
|
-
const txid = hexOf(t.txid, 32, "txid");
|
|
2427
|
-
const app = t.tx?.app;
|
|
2428
|
-
const kind = kindOf(app);
|
|
2429
|
-
if (kind === "none" || height < rule.activationHeight) return null;
|
|
2430
|
-
if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2431
|
-
if (kind === "propose" && domainOf(app) !== rule.domain) return null;
|
|
2432
|
-
const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
|
|
2433
|
-
if (kind === "propose") {
|
|
2434
|
-
const p = app;
|
|
2435
|
-
const uri = p.uri;
|
|
2436
|
-
if (typeof uri !== "string" || !isWellFormedUtf162(uri)) return fail("uri is not well-formed text");
|
|
2437
|
-
const e = u64Of(p.expiresEpoch, "expiresEpoch");
|
|
2438
|
-
return {
|
|
2439
|
-
kind: "propose",
|
|
2440
|
-
id: txid,
|
|
2441
|
-
proposer: who,
|
|
2442
|
-
uri: uri.replaceAll("\0", ""),
|
|
2443
|
-
payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
|
|
2444
|
-
expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
|
|
2445
|
-
height,
|
|
2446
|
-
pos: t.pos,
|
|
2447
|
-
paidTo
|
|
2448
|
-
};
|
|
2449
|
-
}
|
|
2450
|
-
const a = app;
|
|
2451
|
-
return {
|
|
2452
|
-
kind: "attest",
|
|
2453
|
-
txid,
|
|
2454
|
-
proposalId: hexOf(a.proposalId, 32, "proposalId"),
|
|
2455
|
-
attester: who,
|
|
2456
|
-
score: u32Of(a.score, "score"),
|
|
2457
|
-
confidence: u32Of(a.confidence, "confidence"),
|
|
2458
|
-
height,
|
|
2459
|
-
pos: t.pos,
|
|
2460
|
-
paidTo
|
|
2461
|
-
};
|
|
2462
|
-
}
|
|
2463
|
-
function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
|
|
2464
|
-
checkRule(rule);
|
|
2465
|
-
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2466
|
-
if (height < rule.activationHeight) return [];
|
|
2467
|
-
if (!Array.isArray(txs)) fail("txs is not a list");
|
|
2468
|
-
const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
|
|
2469
|
-
const read = [];
|
|
2470
|
-
let last = -1;
|
|
2471
|
-
for (const item of txs) {
|
|
2472
|
-
if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
|
|
2473
|
-
last = item.pos;
|
|
2474
|
-
const id = hexOf(item.txid, 32, "txid");
|
|
2475
|
-
if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
|
|
2476
|
-
ids.add(id);
|
|
2477
|
-
const kind = kindOf(item.tx?.app);
|
|
2478
|
-
if (kind === "none") continue;
|
|
2479
|
-
if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2480
|
-
if (kind === "propose") {
|
|
2481
|
-
if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
|
|
2482
|
-
proposals.add(id);
|
|
2483
|
-
read.push({ item, kind, id });
|
|
2484
|
-
} else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
|
|
2485
|
-
}
|
|
2486
|
-
return read.filter((r) => r.kind === "propose" || !sofar.eventIds.has(r.id) && (sofar.proposeIds.has(r.proposal) || proposals.has(r.proposal))).map((r) => ({ kind: r.kind, item: r.item }));
|
|
2487
|
-
}
|
|
2488
|
-
function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
|
|
2489
|
-
const members = blockMembers(height, txs, sofar, rule);
|
|
2490
|
-
if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
|
|
2491
|
-
const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
|
|
2492
|
-
return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
|
|
2493
|
-
}
|
|
2494
|
-
|
|
2495
2613
|
// src/hashes.ts
|
|
2496
2614
|
import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from "@inversealtruism/csd-codec";
|
|
2497
2615
|
import { sha256 } from "@noble/hashes/sha256";
|
|
@@ -2585,27 +2703,6 @@ function myProvenFillClosedOffer(fill, fclaim2, offerEv, me, grantHeight) {
|
|
|
2585
2703
|
return need.every((leg) => ptAmt(fill.paidTo?.[lc(leg.to)]) >= leg.value);
|
|
2586
2704
|
}
|
|
2587
2705
|
|
|
2588
|
-
// src/holdcount.ts
|
|
2589
|
-
var lastLegacyClaimEnd = V28_HEIGHT - 1 + claimWindowAt(V28_HEIGHT - 1);
|
|
2590
|
-
var LEGACY_HOLD_SUNSET_HEIGHT = lastLegacyClaimEnd + claimGraceOf(lastLegacyClaimEnd);
|
|
2591
|
-
function countOtherHoldsAtGrant(candidates, target, me, closed = /* @__PURE__ */ new Set()) {
|
|
2592
|
-
if (!(Number.isSafeInteger(target.height) && target.height >= LEGACY_HOLD_SUNSET_HEIGHT))
|
|
2593
|
-
return { ok: false, reason: `this reservation was granted at ${target.height}, before block ${LEGACY_HOLD_SUNSET_HEIGHT}, while claims made before V28 could still count against the cap; it cannot be checked here` };
|
|
2594
|
-
const meL = String(me).toLowerCase(), tid = String(target.id).toLowerCase(), toffer = String(target.offer).toLowerCase();
|
|
2595
|
-
const offers = /* @__PURE__ */ new Set();
|
|
2596
|
-
for (const c of candidates) {
|
|
2597
|
-
const cid = String(c.id).toLowerCase();
|
|
2598
|
-
if (cid === tid || closed.has(cid) || String(c.proposer).toLowerCase() !== meL) continue;
|
|
2599
|
-
if (!(c.height < target.height || c.height === target.height && c.pos < target.pos)) continue;
|
|
2600
|
-
if (!(fclaimHoldEnd(Number(c.expiresEpoch)) >= target.height)) continue;
|
|
2601
|
-
const rec = parseRecord(String(c.uri), String(c.payloadHash));
|
|
2602
|
-
if (!rec || rec.t !== "fclaim") continue;
|
|
2603
|
-
const offer2 = String(rec.offer).toLowerCase();
|
|
2604
|
-
if (offer2 !== toffer) offers.add(offer2);
|
|
2605
|
-
}
|
|
2606
|
-
return { ok: true, count: offers.size };
|
|
2607
|
-
}
|
|
2608
|
-
|
|
2609
2706
|
// src/admission.ts
|
|
2610
2707
|
function provenOfferRefusal(rec, tx) {
|
|
2611
2708
|
if (tx.domain !== DOMAIN) return `not a ${DOMAIN} Propose (domain ${JSON.stringify(String(tx.domain))})`;
|
|
@@ -2819,6 +2916,7 @@ export {
|
|
|
2819
2916
|
REG_FINALIZE_GRACE_BLOCKS,
|
|
2820
2917
|
RESERVED_NAMES,
|
|
2821
2918
|
RecordError,
|
|
2919
|
+
ReplayContextRefusal,
|
|
2822
2920
|
SALT_RE,
|
|
2823
2921
|
SCAN_MARGIN,
|
|
2824
2922
|
SCORE_CANCEL,
|
|
@@ -2882,6 +2980,8 @@ export {
|
|
|
2882
2980
|
fillTargetId,
|
|
2883
2981
|
finalizeWinnerCheck,
|
|
2884
2982
|
hasLiveClaim,
|
|
2983
|
+
isChainHeight,
|
|
2984
|
+
isIndex,
|
|
2885
2985
|
isName,
|
|
2886
2986
|
isNameGive,
|
|
2887
2987
|
isOpenClaimLane,
|
|
@@ -2899,6 +2999,7 @@ export {
|
|
|
2899
2999
|
nameSet,
|
|
2900
3000
|
nameView,
|
|
2901
3001
|
nameXfer,
|
|
3002
|
+
newReplayContext,
|
|
2902
3003
|
offer,
|
|
2903
3004
|
offerCancelAll,
|
|
2904
3005
|
offerExpiryHeightOf,
|
package/dist/mm/index.d.cts
CHANGED
|
@@ -175,7 +175,7 @@ interface OfferState {
|
|
|
175
175
|
fills?: FillEntry[];
|
|
176
176
|
/** v1.2: the bid this offer responds to */
|
|
177
177
|
bid?: string;
|
|
178
|
-
/** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse
|
|
178
|
+
/** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse:
|
|
179
179
|
* a past claimUntilHeight just means no live claim (the fields persist as the last-claim record). */
|
|
180
180
|
claimedBy?: string;
|
|
181
181
|
claimUntilHeight?: number;
|
package/dist/mm/index.d.ts
CHANGED
|
@@ -175,7 +175,7 @@ interface OfferState {
|
|
|
175
175
|
fills?: FillEntry[];
|
|
176
176
|
/** v1.2: the bid this offer responds to */
|
|
177
177
|
bid?: string;
|
|
178
|
-
/** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse
|
|
178
|
+
/** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse:
|
|
179
179
|
* a past claimUntilHeight just means no live claim (the fields persist as the last-claim record). */
|
|
180
180
|
claimedBy?: string;
|
|
181
181
|
claimUntilHeight?: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inversealtruism/cairnx-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=18"
|
|
6
6
|
},
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@noble/hashes": "1.8.0",
|
|
45
|
-
"@inversealtruism/csd-codec": "0.1.
|
|
45
|
+
"@inversealtruism/csd-codec": "0.1.19"
|
|
46
46
|
},
|
|
47
47
|
"license": "MIT",
|
|
48
48
|
"sideEffects": false,
|