@greenlandai/sdk 3.0.1 → 3.0.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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 3.0.2 (2026-11-02)
2
+ Only this change since 3.0.1 (built from 3.0.1's tree; the later unreleased work stays for its own release):
3
+ - **The anchor check survives a reorg** (messaging v4 SPEC §15, the rule of enyal-verify 0.1.4). The anchored-transaction fetch asks every
4
+ BSV source, and a source counts only when the block it names is its OWN main-chain block at that height. A source still on an orphaned
5
+ block is reported in `stale`, one that cannot answer (rate limit, outage, not yet in a block) in `unavailable`; neither is counted. At
6
+ least 2 counted sources must agree. WhatsOnChain is added as a third default source (`BSV_SOURCES`; a source needs a `chain` height
7
+ index). Bytes that hash to another txid still refuse. Real case: 2026-11-02, one source still indexed a transaction in orphaned block
8
+ 973536 while two others had it at 973539 — 3.0.1 refused it ("the sources disagree on the block"); 3.0.2 accepts it.
9
+
1
10
  ## 3.0.1 (2026-10-18)
2
11
  The first published release since 0.5.17: it carries everything listed below under "Unreleased" (the messaging v4 client of
3
12
  3.0.0, which was signed but never published), plus:
package/dist/index.cjs CHANGED
@@ -5026,7 +5026,7 @@ var RareeaiSubmodule = class _RareeaiSubmodule {
5026
5026
  };
5027
5027
 
5028
5028
  // index.js
5029
- var VERSION = "3.0.1";
5029
+ var VERSION = "3.0.2";
5030
5030
  var Client = class {
5031
5031
  /**
5032
5032
  * @param {object} opts
package/index.js CHANGED
@@ -16,7 +16,7 @@ export {
16
16
  export { ENFORCED, NOT_SUPPORTED, SAFE, IDEMPOTENCY_MAP } from "./src/core/idempotency.js";
17
17
  export * as crypto from "./src/core/crypto.js";
18
18
 
19
- export const VERSION = "3.0.1";
19
+ export const VERSION = "3.0.2";
20
20
 
21
21
  export class Client {
22
22
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greenlandai/sdk",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "description": "Unified SDK for the GreenlandAI ecosystem (ENYAL, JoulePAI, RAREEAI, GreenlandAI)",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://greenlandai.ai",
@@ -402,36 +402,59 @@ export async function verifyBatchInclusion({ chunkHash, batch, rawTx }) {
402
402
  return { ok: true, reason: null, leafIndex };
403
403
  }
404
404
 
405
- /** Default public BSV sources (two independent operators). A user may replace them, e.g. with their own node. */
405
+ /**
406
+ * Default public BSV sources: three independent operators, each with its OWN height → main-chain block hash index (`chain`, field `hash`).
407
+ * A user may replace them, e.g. with their own node (a source needs all four: raw, status, block, chain). Same list as @enyalai/verify 0.1.4.
408
+ */
406
409
  export const BSV_SOURCES = Object.freeze([
407
410
  { name: "bitails", raw: (t) => `https://api.bitails.io/download/tx/${t}`, status: (t) => `https://api.bitails.io/tx/${t}`,
408
- block: (j) => ({ height: j.blockheight, hash: j.blockhash }) },
411
+ block: (j) => ({ height: j.blockheight, hash: j.blockhash }), chain: (h) => `https://api.bitails.io/block/height/${h}` },
409
412
  { name: "gorillapool", raw: (t) => `https://ordinals.gorillapool.io/api/tx/${t}/raw`, status: (t) => `https://junglebus.gorillapool.io/v1/transaction/get/${t}`,
410
- block: (j) => ({ height: j.block_height, hash: j.block_hash }) },
413
+ block: (j) => ({ height: j.block_height, hash: j.block_hash }), chain: (h) => `https://junglebus.gorillapool.io/v1/block_header/get/${h}` },
414
+ { name: "whatsonchain", raw: (t) => `https://api.whatsonchain.com/v1/bsv/main/tx/${t}/hex`, rawHex: true, status: (t) => `https://api.whatsonchain.com/v1/bsv/main/tx/hash/${t}`,
415
+ block: (j) => ({ height: j.blockheight, hash: j.blockhash }), chain: (h) => `https://api.whatsonchain.com/v1/bsv/main/block/height/${h}` },
411
416
  ]);
412
417
  /**
413
- * Fetch an anchor transaction from at least 2 sources. Each source's bytes must hash to the txid (checked here, so a source cannot
414
- * change the content), each must report it in a block, and the block hashes must agree. → { raw, block }
415
- * Limit (SPEC §15): the sources' word that the block is in the chain is trusted; block headers are not verified here.
418
+ * Fetch an anchor transaction (SPEC §15, the rule of @enyalai/verify 0.1.4). Every source is asked. A source COUNTS only if its bytes hash to the
419
+ * txid (checked here, so a source cannot change the content — bytes that parse but hash to anything else REFUSE), it places the tx in a block,
420
+ * and that block is the block its OWN height index has as main chain at that height. One failing the last check is STALE (a reorg it has not
421
+ * re-indexed); one that cannot answer (HTTP error, network error, no transaction, unreadable status, not yet in a block) is UNAVAILABLE —
422
+ * both reported, never counted. Counted sources must agree on the block, and at least `minSources` must count.
423
+ * → { raw, block, sources (counted), stale, unavailable }
424
+ * Limit (SPEC §15): the sources' word that their main chain is the chain is trusted; block headers are not verified here.
416
425
  */
417
426
  export async function fetchAnchoredTransaction(txid, { sources = BSV_SOURCES, fetchImpl = globalThis.fetch, minSources = 2 } = {}) {
418
427
  if (sources.length < minSources) throw new Error(`at least ${minSources} sources are required`);
419
- let raw = null; const blocks = [];
428
+ for (const s of sources) if (typeof s.chain !== "function") throw new Error(`${s.name}: a source needs its own height index ('chain') to be checked against its main chain`);
429
+ const counted = [], stale = [], unavailable = []; let raw = null;
430
+ const unav = (source, why) => unavailable.push({ source, why: String(why).slice(0, 240) });
431
+ const getOk = async (url) => { const r = await fetchImpl(url); if (!r.ok) throw new Error(`HTTP ${r.status}`); return r; };
420
432
  for (const s of sources) {
421
- const r = await fetchImpl(s.raw(txid));
422
- if (!r.ok) throw new Error(`${s.name}: HTTP ${r.status} for the transaction`);
423
- const bytes = new Uint8Array(await r.arrayBuffer());
424
- const { txid: got } = await parseRawTx(bytes);
433
+ let bytes, got;
434
+ try {
435
+ const r = await getOk(s.raw(txid));
436
+ if (s.rawHex) { const h = (await r.text()).trim(); if (!/^(?:[0-9a-fA-F]{2})+$/.test(h)) throw new Error("the reply is not hex"); bytes = fromHex(h); }
437
+ else bytes = new Uint8Array(await r.arrayBuffer());
438
+ ({ txid: got } = await parseRawTx(bytes));
439
+ } catch (e) { unav(s.name, `no transaction (${e.message})`); continue; }
425
440
  if (got !== txid) throw new Error(`${s.name}: the returned bytes hash to ${got}, not ${txid}`);
426
441
  raw = raw || bytes;
427
- const st = await fetchImpl(s.status(txid));
428
- if (!st.ok) throw new Error(`${s.name}: HTTP ${st.status} for the transaction status`);
429
- const b = s.block(await st.json());
430
- if (!b.height || !b.hash) throw new Error(`${s.name}: the transaction is not in a block yet`);
431
- blocks.push({ source: s.name, ...b });
442
+ let b;
443
+ try { b = s.block(await (await getOk(s.status(txid))).json()); } catch (e) { unav(s.name, `no status (${e.message})`); continue; }
444
+ if (!b.height || !b.hash) { unav(s.name, "the transaction is not in a block yet"); continue; }
445
+ let main;
446
+ try { main = (await (await getOk(s.chain(b.height))).json()).hash; } catch (e) { unav(s.name, `no main-chain block at height ${b.height} (${e.message})`); continue; }
447
+ if (main !== b.hash) {
448
+ stale.push({ source: s.name, height: b.height, hash: b.hash, mainChainHash: main,
449
+ why: `its transaction index places the tx in block ${b.hash} at height ${b.height}, but its own main chain has ${main} there (a reorg it has not re-indexed)` });
450
+ continue;
451
+ }
452
+ counted.push({ source: s.name, ...b });
432
453
  }
433
- if (new Set(blocks.map((b) => `${b.height}:${b.hash}`)).size !== 1) throw new Error("the sources disagree on the block");
434
- return { raw, block: blocks[0], sources: blocks };
454
+ if (new Set(counted.map((c) => `${c.height}:${c.hash}`)).size > 1) throw new Error("the sources disagree on the block: " + counted.map((c) => `${c.source} ${c.height}:${c.hash}`).join("; "));
455
+ if (counted.length < minSources) throw new Error(`only ${counted.length} of ${sources.length} sources place the transaction in a block on their own main chain; at least ${minSources} must agree`
456
+ + [...stale, ...unavailable].map((x) => ` — ${x.source}: ${x.why}`).join(""));
457
+ return { raw, block: counted[0], sources: counted, stale, unavailable };
435
458
  }
436
459
 
437
460
  // ── pins and key-change warnings (SPEC §16) ──────────────────────────────────────────────────────────────