@cloak.dev/sdk 0.2.6-staging.f92dd96 → 0.2.6
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 +33 -3
- package/README.md +102 -4
- package/dist/index.cjs +144 -75
- package/dist/index.d.cts +85 -5
- package/dist/index.d.ts +85 -5
- package/dist/index.js +144 -75
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -676,7 +676,7 @@ function parseStrictViewingKeyChallenge(response, userPubkey, viewingKeyHex) {
|
|
|
676
676
|
|
|
677
677
|
// src/config/relay.ts
|
|
678
678
|
var CLOAK_PRODUCTION_RELAY_URL = "https://api.cloak.ag";
|
|
679
|
-
var RELAY_ORIGIN_ALLOWLIST = [
|
|
679
|
+
var RELAY_ORIGIN_ALLOWLIST = [CLOAK_PRODUCTION_RELAY_URL];
|
|
680
680
|
var ABSOLUTE_URL = /^[a-z][a-z0-9+.-]*:\/\//i;
|
|
681
681
|
var LOOPBACK_IPV4 = /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
|
|
682
682
|
var THIS_NETWORK_IPV4 = /^0\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
|
|
@@ -756,7 +756,7 @@ var BUILD_ALLOWS_LOCAL_ENDPOINTS = RELAY_ORIGIN_ALLOWLIST.some(isLoopbackHost);
|
|
|
756
756
|
// src/relay/clock.ts
|
|
757
757
|
var RELAY_SERVER_TIME_HEADER = "x-cloak-server-time";
|
|
758
758
|
var MAX_CLOCK_DIVERGENCE_MS = 2e3;
|
|
759
|
-
var MAX_PLAUSIBLE_OFFSET_MS =
|
|
759
|
+
var MAX_PLAUSIBLE_OFFSET_MS = 24 * 60 * 60 * 1e3;
|
|
760
760
|
var sample = null;
|
|
761
761
|
function monotonicNowMs() {
|
|
762
762
|
const perf = globalThis.performance;
|
|
@@ -11979,7 +11979,7 @@ function parseSwapRefund(r) {
|
|
|
11979
11979
|
checkLeafPair(commitment, leaf, "SWAP_REFUND");
|
|
11980
11980
|
return { kind: 3, mint, slot, commitment, leaf, amountAfterFee, nullifier0, signature };
|
|
11981
11981
|
}
|
|
11982
|
-
function parseNoteIndexPage(bytes, expect) {
|
|
11982
|
+
function parseNoteIndexPage(bytes, expect, options = {}) {
|
|
11983
11983
|
if (!(bytes instanceof Uint8Array)) throw new NoteIndexFormatError("page must be a Uint8Array");
|
|
11984
11984
|
if (bytes.length > NOTE_INDEX_MAX_PAGE_BYTES) {
|
|
11985
11985
|
throw new NoteIndexFormatError(`page is ${bytes.length} bytes, over the ${NOTE_INDEX_MAX_PAGE_BYTES}-byte bound`);
|
|
@@ -12011,7 +12011,9 @@ function parseNoteIndexPage(bytes, expect) {
|
|
|
12011
12011
|
}
|
|
12012
12012
|
const items = [];
|
|
12013
12013
|
const rawItems = [];
|
|
12014
|
+
const itemPositions = [];
|
|
12014
12015
|
let skippedUnknown = 0;
|
|
12016
|
+
let skippedMalformed = 0;
|
|
12015
12017
|
let offset = NOTE_INDEX_HEADER_LEN;
|
|
12016
12018
|
for (let i = 0; i < count; i++) {
|
|
12017
12019
|
if (offset + ITEM_HEADER_LEN > bytes.length) throw new NoteIndexFormatError(`truncated item ${i} header`);
|
|
@@ -12020,34 +12022,45 @@ function parseNoteIndexPage(bytes, expect) {
|
|
|
12020
12022
|
const bodyStart = offset + ITEM_HEADER_LEN;
|
|
12021
12023
|
const bodyEnd = bodyStart + bodyLen;
|
|
12022
12024
|
if (bodyEnd > bytes.length) throw new NoteIndexFormatError(`truncated item ${i} body`);
|
|
12023
|
-
|
|
12025
|
+
if (kind !== NOTE_INDEX_KIND_TX && kind !== NOTE_INDEX_KIND_DELIVERY && kind !== NOTE_INDEX_KIND_SWAP_REFUND) {
|
|
12026
|
+
if (bodyLen > MAX_UNKNOWN_BODY_LEN) throw new NoteIndexFormatError(`unknown kind ${kind} with body_len ${bodyLen}`);
|
|
12027
|
+
skippedUnknown++;
|
|
12028
|
+
offset = bodyEnd;
|
|
12029
|
+
continue;
|
|
12030
|
+
}
|
|
12024
12031
|
let item;
|
|
12025
|
-
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12029
|
-
|
|
12030
|
-
|
|
12031
|
-
|
|
12032
|
-
item = parseDelivery(r);
|
|
12033
|
-
break;
|
|
12034
|
-
case NOTE_INDEX_KIND_SWAP_REFUND:
|
|
12035
|
-
if (bodyLen !== SWAP_REFUND_BODY_LEN) throw new NoteIndexFormatError(`SWAP_REFUND: body_len ${bodyLen}`);
|
|
12036
|
-
item = parseSwapRefund(r);
|
|
12037
|
-
break;
|
|
12038
|
-
default:
|
|
12039
|
-
if (bodyLen > MAX_UNKNOWN_BODY_LEN) throw new NoteIndexFormatError(`unknown kind ${kind} with body_len ${bodyLen}`);
|
|
12040
|
-
skippedUnknown++;
|
|
12041
|
-
offset = bodyEnd;
|
|
12042
|
-
continue;
|
|
12032
|
+
try {
|
|
12033
|
+
item = parseKnownBody(bytes, kind, bodyStart, bodyEnd, i);
|
|
12034
|
+
} catch (error) {
|
|
12035
|
+
if (!options.skipMalformedItems || !(error instanceof NoteIndexFormatError)) throw error;
|
|
12036
|
+
skippedMalformed++;
|
|
12037
|
+
offset = bodyEnd;
|
|
12038
|
+
continue;
|
|
12043
12039
|
}
|
|
12044
|
-
if (r.position !== bodyEnd) throw new NoteIndexFormatError(`item ${i} body has unread bytes`);
|
|
12045
12040
|
items.push(item);
|
|
12046
12041
|
rawItems.push(bytes.slice(offset, bodyEnd));
|
|
12042
|
+
itemPositions.push(i);
|
|
12047
12043
|
offset = bodyEnd;
|
|
12048
12044
|
}
|
|
12049
12045
|
if (offset !== bytes.length) throw new NoteIndexFormatError(`${bytes.length - offset} trailing bytes after ${count} items`);
|
|
12050
|
-
return { epoch, page, firstSeq, complete, count, prevChain, items, rawItems, skippedUnknown };
|
|
12046
|
+
return { epoch, page, firstSeq, complete, count, prevChain, items, rawItems, skippedUnknown, itemPositions, skippedMalformed };
|
|
12047
|
+
}
|
|
12048
|
+
function parseKnownBody(bytes, kind, bodyStart, bodyEnd, i) {
|
|
12049
|
+
const bodyLen = bodyEnd - bodyStart;
|
|
12050
|
+
const r = new Reader(bytes, bodyStart, bodyEnd);
|
|
12051
|
+
let item;
|
|
12052
|
+
if (kind === NOTE_INDEX_KIND_TX) {
|
|
12053
|
+
if (bodyLen < TX_FIXED_LEN) throw new NoteIndexFormatError(`TX: body_len ${bodyLen}`);
|
|
12054
|
+
item = parseTx(r, bodyLen);
|
|
12055
|
+
} else if (kind === NOTE_INDEX_KIND_DELIVERY) {
|
|
12056
|
+
if (bodyLen !== DELIVERY_BODY_LEN) throw new NoteIndexFormatError(`DELIVERY: body_len ${bodyLen}`);
|
|
12057
|
+
item = parseDelivery(r);
|
|
12058
|
+
} else {
|
|
12059
|
+
if (bodyLen !== SWAP_REFUND_BODY_LEN) throw new NoteIndexFormatError(`SWAP_REFUND: body_len ${bodyLen}`);
|
|
12060
|
+
item = parseSwapRefund(r);
|
|
12061
|
+
}
|
|
12062
|
+
if (r.position !== bodyEnd) throw new NoteIndexFormatError(`item ${i} body has unread bytes`);
|
|
12063
|
+
return item;
|
|
12051
12064
|
}
|
|
12052
12065
|
function parseNoteIndexItem(raw) {
|
|
12053
12066
|
if (!(raw instanceof Uint8Array) || raw.length < ITEM_HEADER_LEN) throw new NoteIndexFormatError("truncated item");
|
|
@@ -12378,6 +12391,7 @@ var MAX_REJECTED_IDS = 1024;
|
|
|
12378
12391
|
var LEAF_WINDOW = 512;
|
|
12379
12392
|
var MAX_OVERRIDES = 256;
|
|
12380
12393
|
var MAX_CURSOR_OWN_ITEMS = 65536;
|
|
12394
|
+
var MAX_LEGACY_VIEWING_KEYS = 8;
|
|
12381
12395
|
var MAX_CURSOR_KEYS = 131072;
|
|
12382
12396
|
var MAX_CURSOR_POOLS = 256;
|
|
12383
12397
|
var MAX_CURSOR_PAGE = 2 ** 32;
|
|
@@ -12468,7 +12482,23 @@ var LeafBuffer = class {
|
|
|
12468
12482
|
return this.view.getBigUint64(o) << 192n | this.view.getBigUint64(o + 8) << 128n | this.view.getBigUint64(o + 16) << 64n | this.view.getBigUint64(o + 24);
|
|
12469
12483
|
}
|
|
12470
12484
|
};
|
|
12471
|
-
async function trialItem(item,
|
|
12485
|
+
async function trialItem(item, trials) {
|
|
12486
|
+
for (const trial of trials) {
|
|
12487
|
+
if (trial.legacy && item.kind === 2) continue;
|
|
12488
|
+
const opened = await trialItemWith(item, trial);
|
|
12489
|
+
if (opened) return { ...opened, trial };
|
|
12490
|
+
}
|
|
12491
|
+
return null;
|
|
12492
|
+
}
|
|
12493
|
+
async function trialItemIsolated(item, trials, stats) {
|
|
12494
|
+
try {
|
|
12495
|
+
return await trialItem(item, trials);
|
|
12496
|
+
} catch {
|
|
12497
|
+
stats.skipped++;
|
|
12498
|
+
return null;
|
|
12499
|
+
}
|
|
12500
|
+
}
|
|
12501
|
+
async function trialItemWith(item, trial) {
|
|
12472
12502
|
if (item.kind === 1) {
|
|
12473
12503
|
if (item.note.length === 0) return null;
|
|
12474
12504
|
let note = isAllZero3(item.comm0) ? null : await trial.decrypt(item.note, item.comm0);
|
|
@@ -12499,8 +12529,14 @@ function leafOfTxOutput(item, commitment) {
|
|
|
12499
12529
|
if (!isAllZero3(item.comm1) && commitment === bytesToBigint2(item.comm1)) return Number(item.leaf1);
|
|
12500
12530
|
return null;
|
|
12501
12531
|
}
|
|
12502
|
-
|
|
12503
|
-
|
|
12532
|
+
function changeBlindingOrNull(nk, noteSalt, outputIndex) {
|
|
12533
|
+
try {
|
|
12534
|
+
return deriveChangeNoteBlinding(nk, noteSalt, outputIndex);
|
|
12535
|
+
} catch {
|
|
12536
|
+
return null;
|
|
12537
|
+
}
|
|
12538
|
+
}
|
|
12539
|
+
async function matchOwnItems(own, owner, onReject, onSkip) {
|
|
12504
12540
|
const candidates = [];
|
|
12505
12541
|
const claimed = /* @__PURE__ */ new Set();
|
|
12506
12542
|
const byNullifier = /* @__PURE__ */ new Map();
|
|
@@ -12524,27 +12560,26 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12524
12560
|
queued.add(o);
|
|
12525
12561
|
queue.push(o);
|
|
12526
12562
|
};
|
|
12527
|
-
const add = async (c) => {
|
|
12563
|
+
const add = async (o, c) => {
|
|
12528
12564
|
const nullifier = hex32(
|
|
12529
12565
|
await computeNullifier({ amount: c.amount, keypair: c.keypair, blinding: c.blinding, mintAddress: c.mint, index: c.leaf, commitment: c.commitment })
|
|
12530
12566
|
);
|
|
12531
|
-
const full = { ...c, nullifier };
|
|
12567
|
+
const full = { ...c, atRisk: c.atRisk || o.trial.legacy, signature: o.item.signature, page: o.page, seq: o.seq, nullifier };
|
|
12532
12568
|
candidates.push(full);
|
|
12533
12569
|
if (!byNullifier.has(nullifier)) {
|
|
12534
12570
|
byNullifier.set(nullifier, full);
|
|
12535
|
-
for (const
|
|
12571
|
+
for (const s of spendsOf.get(nullifier) ?? []) enqueue(s);
|
|
12536
12572
|
}
|
|
12537
|
-
if (
|
|
12538
|
-
legacyKeys.set(
|
|
12539
|
-
for (const
|
|
12573
|
+
if (c.atRisk && c.keypair.publicKey !== owner.publicKey && !legacyKeys.has(c.keypair.publicKey)) {
|
|
12574
|
+
legacyKeys.set(c.keypair.publicKey, c.keypair);
|
|
12575
|
+
for (const s of byOutKey.get(c.keypair.publicKey) ?? []) enqueue(s);
|
|
12540
12576
|
}
|
|
12541
12577
|
};
|
|
12542
|
-
const base = (o) => ({ signature: o.item.signature, page: o.page, seq: o.seq });
|
|
12543
12578
|
const tryDeposit = async (o, item, amount) => {
|
|
12544
12579
|
const note = o.note;
|
|
12545
12580
|
const mint = addressFromBytes(item.mint);
|
|
12546
12581
|
const deposit = await matchDepositNote({
|
|
12547
|
-
viewingKeyNk: nk,
|
|
12582
|
+
viewingKeyNk: o.trial.nk,
|
|
12548
12583
|
noteSalt: note.noteSalt,
|
|
12549
12584
|
amount,
|
|
12550
12585
|
mintAddress: mint,
|
|
@@ -12553,7 +12588,7 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12553
12588
|
});
|
|
12554
12589
|
const leaf = deposit ? leafOfTxOutput(item, deposit.commitment) : null;
|
|
12555
12590
|
if (!deposit || leaf === null) return false;
|
|
12556
|
-
await add({
|
|
12591
|
+
await add(o, {
|
|
12557
12592
|
kind: "deposit",
|
|
12558
12593
|
atRisk: deposit.legacyExposed,
|
|
12559
12594
|
amount: deposit.amount,
|
|
@@ -12561,8 +12596,7 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12561
12596
|
keypair: deposit.legacyExposed ? deposit.keypair : owner,
|
|
12562
12597
|
mint,
|
|
12563
12598
|
commitment: deposit.commitment,
|
|
12564
|
-
leaf
|
|
12565
|
-
...base(o)
|
|
12599
|
+
leaf
|
|
12566
12600
|
});
|
|
12567
12601
|
return true;
|
|
12568
12602
|
};
|
|
@@ -12570,7 +12604,7 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12570
12604
|
const note = o.note;
|
|
12571
12605
|
const mint = addressFromBytes(item.mint);
|
|
12572
12606
|
const change = await matchChangeNote({
|
|
12573
|
-
viewingKeyNk: nk,
|
|
12607
|
+
viewingKeyNk: o.trial.nk,
|
|
12574
12608
|
noteSalt: note.noteSalt,
|
|
12575
12609
|
amount: note.outAmount0,
|
|
12576
12610
|
keypair,
|
|
@@ -12580,7 +12614,7 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12580
12614
|
ownerUtxoPublicKey: owner.publicKey
|
|
12581
12615
|
});
|
|
12582
12616
|
if (!change) return false;
|
|
12583
|
-
await add({
|
|
12617
|
+
await add(o, {
|
|
12584
12618
|
kind: "change",
|
|
12585
12619
|
atRisk,
|
|
12586
12620
|
amount: change.amount,
|
|
@@ -12588,12 +12622,11 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12588
12622
|
keypair,
|
|
12589
12623
|
mint,
|
|
12590
12624
|
commitment: change.commitment,
|
|
12591
|
-
leaf: Number(item.leaf0)
|
|
12592
|
-
...base(o)
|
|
12625
|
+
leaf: Number(item.leaf0)
|
|
12593
12626
|
});
|
|
12594
12627
|
return true;
|
|
12595
12628
|
};
|
|
12596
|
-
|
|
12629
|
+
const direct = async (o) => {
|
|
12597
12630
|
const item = o.item;
|
|
12598
12631
|
if (item.kind === 1 && o.note?.noteSalt !== void 0) {
|
|
12599
12632
|
const note = o.note;
|
|
@@ -12607,7 +12640,7 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12607
12640
|
const mint = addressFromBytes(item.mint);
|
|
12608
12641
|
if (o.delivery.amount <= 0n || o.delivery.blinding === 0n) {
|
|
12609
12642
|
onReject(o);
|
|
12610
|
-
|
|
12643
|
+
return;
|
|
12611
12644
|
}
|
|
12612
12645
|
const commitment = await computeCommitment({
|
|
12613
12646
|
amount: o.delivery.amount,
|
|
@@ -12617,9 +12650,9 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12617
12650
|
});
|
|
12618
12651
|
if (commitment !== bytesToBigint2(item.commitment)) {
|
|
12619
12652
|
onReject(o);
|
|
12620
|
-
|
|
12653
|
+
return;
|
|
12621
12654
|
}
|
|
12622
|
-
await add({
|
|
12655
|
+
await add(o, {
|
|
12623
12656
|
kind: "received",
|
|
12624
12657
|
atRisk: false,
|
|
12625
12658
|
amount: o.delivery.amount,
|
|
@@ -12627,11 +12660,10 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12627
12660
|
keypair: owner,
|
|
12628
12661
|
mint,
|
|
12629
12662
|
commitment,
|
|
12630
|
-
leaf: Number(item.leaf)
|
|
12631
|
-
...base(o)
|
|
12663
|
+
leaf: Number(item.leaf)
|
|
12632
12664
|
});
|
|
12633
12665
|
} else if (item.kind === 3 && o.refund) {
|
|
12634
|
-
await add({
|
|
12666
|
+
await add(o, {
|
|
12635
12667
|
kind: "swap_refund",
|
|
12636
12668
|
atRisk: o.refund.legacyExposed,
|
|
12637
12669
|
amount: o.refund.amount,
|
|
@@ -12639,10 +12671,16 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12639
12671
|
keypair: o.refund.legacyExposed ? o.refund.keypair : owner,
|
|
12640
12672
|
mint: NATIVE_SOL_MINT,
|
|
12641
12673
|
commitment: o.refund.commitment,
|
|
12642
|
-
leaf: Number(item.leaf)
|
|
12643
|
-
...base(o)
|
|
12674
|
+
leaf: Number(item.leaf)
|
|
12644
12675
|
});
|
|
12645
12676
|
}
|
|
12677
|
+
};
|
|
12678
|
+
for (const o of own) {
|
|
12679
|
+
try {
|
|
12680
|
+
await direct(o);
|
|
12681
|
+
} catch {
|
|
12682
|
+
onSkip(o);
|
|
12683
|
+
}
|
|
12646
12684
|
}
|
|
12647
12685
|
const processTx = async (o) => {
|
|
12648
12686
|
const item = o.item;
|
|
@@ -12657,8 +12695,9 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12657
12695
|
const sumIn = inputs.reduce((acc, c) => acc + c.amount, 0n);
|
|
12658
12696
|
if (!claimed.has(`${o.seq}:1`) && note.outAmount0 !== void 0 && !isAllZero3(item.comm1)) {
|
|
12659
12697
|
const out1 = sumIn + item.publicAmount - note.outAmount0;
|
|
12660
|
-
|
|
12661
|
-
|
|
12698
|
+
const blinding = out1 > 0n ? changeBlindingOrNull(o.trial.nk, note.noteSalt, 1) : null;
|
|
12699
|
+
if (out1 > 0n && blinding === null) onSkip(o);
|
|
12700
|
+
else if (blinding !== null) {
|
|
12662
12701
|
const owners = [{ keypair: owner, atRisk: false }];
|
|
12663
12702
|
const legacyInput = legacyKeys.get(inputs[0].keypair.publicKey);
|
|
12664
12703
|
if (legacyInput && legacyInput.publicKey !== owner.publicKey) owners.push({ keypair: legacyInput, atRisk: true });
|
|
@@ -12666,7 +12705,7 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12666
12705
|
const commitment = await computeCommitment({ amount: out1, keypair, blinding, mintAddress: mint });
|
|
12667
12706
|
if (commitment !== bytesToBigint2(item.comm1)) continue;
|
|
12668
12707
|
claimed.add(`${o.seq}:1`);
|
|
12669
|
-
await add({ kind: "change", atRisk, amount: out1, blinding, keypair, mint, commitment, leaf: Number(item.leaf1)
|
|
12708
|
+
await add(o, { kind: "change", atRisk, amount: out1, blinding, keypair, mint, commitment, leaf: Number(item.leaf1) });
|
|
12670
12709
|
break;
|
|
12671
12710
|
}
|
|
12672
12711
|
}
|
|
@@ -12679,7 +12718,11 @@ async function matchOwnItems(own, trial, onReject) {
|
|
|
12679
12718
|
for (let i = 0; i < queue.length; i++) {
|
|
12680
12719
|
const o = queue[i];
|
|
12681
12720
|
queued.delete(o);
|
|
12682
|
-
|
|
12721
|
+
try {
|
|
12722
|
+
await processTx(o);
|
|
12723
|
+
} catch {
|
|
12724
|
+
onSkip(o);
|
|
12725
|
+
}
|
|
12683
12726
|
}
|
|
12684
12727
|
return candidates;
|
|
12685
12728
|
}
|
|
@@ -12926,7 +12969,8 @@ async function recoverNotesFromIndex(params) {
|
|
|
12926
12969
|
candidates: 0,
|
|
12927
12970
|
spent: 0,
|
|
12928
12971
|
conflicts: 0,
|
|
12929
|
-
pendingDropped: 0
|
|
12972
|
+
pendingDropped: 0,
|
|
12973
|
+
skipped: 0
|
|
12930
12974
|
};
|
|
12931
12975
|
const rpc = new Proxy(connection, {
|
|
12932
12976
|
get(target, prop, receiver) {
|
|
@@ -12948,12 +12992,25 @@ async function recoverNotesFromIndex(params) {
|
|
|
12948
12992
|
};
|
|
12949
12993
|
}
|
|
12950
12994
|
});
|
|
12951
|
-
const
|
|
12952
|
-
nk
|
|
12995
|
+
const makeTrial = async (nk, legacy) => ({
|
|
12996
|
+
nk,
|
|
12997
|
+
legacy,
|
|
12953
12998
|
owner,
|
|
12954
|
-
decrypt: await createCompactChainNoteDecryptor(
|
|
12955
|
-
open: await createRecipientDeliveryOpener(deriveViewingKeyFromNk(
|
|
12956
|
-
};
|
|
12999
|
+
decrypt: await createCompactChainNoteDecryptor(nk),
|
|
13000
|
+
open: await createRecipientDeliveryOpener(deriveViewingKeyFromNk(nk).privateKey)
|
|
13001
|
+
});
|
|
13002
|
+
const trials = [await makeTrial(viewingKeyNk, false)];
|
|
13003
|
+
const legacyNks = params.legacyViewingKeyNks ?? [];
|
|
13004
|
+
if (!Array.isArray(legacyNks)) throw new Error("legacyViewingKeyNks must be an array of 32-byte keys");
|
|
13005
|
+
if (legacyNks.length > MAX_LEGACY_VIEWING_KEYS) throw new Error(`legacyViewingKeyNks takes at most ${MAX_LEGACY_VIEWING_KEYS} keys`);
|
|
13006
|
+
const seenNks = /* @__PURE__ */ new Set([toHex2(viewingKeyNk)]);
|
|
13007
|
+
for (const nk of legacyNks) {
|
|
13008
|
+
if (!(nk instanceof Uint8Array) || nk.length !== 32) throw new Error("legacyViewingKeyNks entries must be 32 bytes");
|
|
13009
|
+
const key = toHex2(nk);
|
|
13010
|
+
if (seenNks.has(key)) continue;
|
|
13011
|
+
seenNks.add(key);
|
|
13012
|
+
trials.push(await makeTrial(Uint8Array.from(nk), true));
|
|
13013
|
+
}
|
|
12957
13014
|
const poolMints = Array.from(new Set(params.poolMints));
|
|
12958
13015
|
const warnings = [];
|
|
12959
13016
|
const head = await fetchNoteIndexHead(relayUrl, { signal: params.signal });
|
|
@@ -12976,21 +13033,21 @@ async function recoverNotesFromIndex(params) {
|
|
|
12976
13033
|
return account && parsed ? { ...parsed, data: account.data } : null;
|
|
12977
13034
|
});
|
|
12978
13035
|
})());
|
|
12979
|
-
const ctx = { params, head,
|
|
13036
|
+
const ctx = { params, head, trials, owner, stats, poolMints, warnings, readTrees };
|
|
12980
13037
|
let outcome = await runOnce(ctx, cursor);
|
|
12981
13038
|
if (outcome instanceof FullPassSignal) {
|
|
12982
13039
|
warnings.push(`${outcome.reason}; re-syncing from page 0`);
|
|
12983
|
-
Object.assign(stats, { dropped: 0, decrypted: 0, candidates: 0, spent: 0, conflicts: 0, pendingDropped: 0 });
|
|
13040
|
+
Object.assign(stats, { dropped: 0, decrypted: 0, candidates: 0, spent: 0, conflicts: 0, pendingDropped: 0, skipped: 0 });
|
|
12984
13041
|
outcome = await runOnce(ctx, void 0);
|
|
12985
13042
|
if (outcome instanceof FullPassSignal) throw new Error(`note index recovery failed: ${outcome.reason}`);
|
|
12986
13043
|
}
|
|
13044
|
+
if (stats.skipped > 0) warnings.push(`${stats.skipped} note index entries could not be processed and were skipped`);
|
|
12987
13045
|
stats.ms = Date.now() - startedAt;
|
|
12988
13046
|
return { ...outcome, stats, warnings };
|
|
12989
13047
|
}
|
|
12990
13048
|
async function runOnce(ctx, cursor) {
|
|
12991
|
-
const { params, head,
|
|
13049
|
+
const { params, head, trials, owner, stats, poolMints } = ctx;
|
|
12992
13050
|
const { relayUrl, onProgress } = params;
|
|
12993
|
-
const owner = trial.owner;
|
|
12994
13051
|
const poolSet = new Set(poolMints);
|
|
12995
13052
|
const startPage = cursor?.raw.page ?? 0;
|
|
12996
13053
|
const startChain = cursor?.raw.chain ?? ZERO_CHAIN;
|
|
@@ -13061,11 +13118,13 @@ async function runOnce(ctx, cursor) {
|
|
|
13061
13118
|
if (signal.aborted) throw abortError2();
|
|
13062
13119
|
const bytes = await fetchNoteIndexPage(relayUrl, head.epoch, pageNo, { signal });
|
|
13063
13120
|
stats.bytes += bytes.length;
|
|
13064
|
-
const page2 = parseNoteIndexPage(bytes, { epoch: head.epoch, page: pageNo });
|
|
13121
|
+
const page2 = parseNoteIndexPage(bytes, { epoch: head.epoch, page: pageNo }, { skipMalformedItems: true });
|
|
13065
13122
|
if (pageNo < head.completePages && !page2.complete) {
|
|
13066
13123
|
throw new NoteIndexFormatError(`page ${pageNo} is below complete_pages but not complete`);
|
|
13067
13124
|
}
|
|
13068
13125
|
stats.pages++;
|
|
13126
|
+
stats.skipped += page2.skippedMalformed;
|
|
13127
|
+
const seqOf = (i) => pageNo * NOTE_INDEX_PAGE_SIZE + page2.itemPositions[i];
|
|
13069
13128
|
waiting.set(pageNo, { bytes: page2.complete ? bytes : new Uint8Array(0), prevChain: toHex2(page2.prevChain), complete: page2.complete });
|
|
13070
13129
|
advanceChain();
|
|
13071
13130
|
const data = { complete: page2.complete, leaves: /* @__PURE__ */ new Map() };
|
|
@@ -13086,7 +13145,7 @@ async function runOnce(ctx, cursor) {
|
|
|
13086
13145
|
else data.leaves.set(mint, [entry]);
|
|
13087
13146
|
};
|
|
13088
13147
|
page2.items.forEach((item, i) => {
|
|
13089
|
-
const seq =
|
|
13148
|
+
const seq = seqOf(i);
|
|
13090
13149
|
if (item.kind === 1) {
|
|
13091
13150
|
addLeaf(item.mint, item.comm0, item.leaf0, seq);
|
|
13092
13151
|
addLeaf(item.mint, item.comm1, item.leaf1, seq);
|
|
@@ -13103,7 +13162,7 @@ async function runOnce(ctx, cursor) {
|
|
|
13103
13162
|
for (let i = 0; i < page2.items.length; i += ITEM_BATCH) {
|
|
13104
13163
|
if (signal.aborted) throw abortError2();
|
|
13105
13164
|
const batch = page2.items.slice(i, i + ITEM_BATCH);
|
|
13106
|
-
const opened = await Promise.all(batch.map((item) =>
|
|
13165
|
+
const opened = await Promise.all(batch.map((item) => trialItemIsolated(item, trials, stats)));
|
|
13107
13166
|
opened.forEach((o, k) => {
|
|
13108
13167
|
if (!o) return;
|
|
13109
13168
|
stats.decrypted++;
|
|
@@ -13113,7 +13172,7 @@ async function runOnce(ctx, cursor) {
|
|
|
13113
13172
|
stats.dropped++;
|
|
13114
13173
|
return;
|
|
13115
13174
|
}
|
|
13116
|
-
ownItems.push({ page: pageNo, seq:
|
|
13175
|
+
ownItems.push({ page: pageNo, seq: seqOf(i + k), raw, id, item: batch[k], fromCursor: false, ...o, trial: o.trial });
|
|
13117
13176
|
});
|
|
13118
13177
|
stats.items += batch.length;
|
|
13119
13178
|
}
|
|
@@ -13146,10 +13205,10 @@ async function runOnce(ctx, cursor) {
|
|
|
13146
13205
|
throw new NoteIndexFormatError("hash chain does not reach the head's chain_tip");
|
|
13147
13206
|
}
|
|
13148
13207
|
for (const entry of cursor?.items ?? []) {
|
|
13149
|
-
const o = await
|
|
13208
|
+
const o = await trialItemIsolated(entry.item, trials, stats);
|
|
13150
13209
|
if (!o) continue;
|
|
13151
13210
|
const page2 = Math.floor(entry.seq / NOTE_INDEX_PAGE_SIZE);
|
|
13152
|
-
ownItems.push({ page: page2, seq: entry.seq, raw: entry.raw, id: itemIdentity(entry.raw), item: entry.item, fromCursor: true, ...o });
|
|
13211
|
+
ownItems.push({ page: page2, seq: entry.seq, raw: entry.raw, id: itemIdentity(entry.raw), item: entry.item, fromCursor: true, ...o, trial: o.trial });
|
|
13153
13212
|
if (entry.item.kind === 1) {
|
|
13154
13213
|
if (!isAllZero3(entry.item.nullifier0)) nullifiers.add(toHex2(entry.item.nullifier0));
|
|
13155
13214
|
if (!isAllZero3(entry.item.nullifier1)) nullifiers.add(toHex2(entry.item.nullifier1));
|
|
@@ -13158,10 +13217,20 @@ async function runOnce(ctx, cursor) {
|
|
|
13158
13217
|
const knownSpent = cursor?.spent ?? /* @__PURE__ */ new Set();
|
|
13159
13218
|
ownItems.sort((a, b) => a.seq - b.seq);
|
|
13160
13219
|
const rejectedNow = /* @__PURE__ */ new Set();
|
|
13161
|
-
const
|
|
13162
|
-
|
|
13163
|
-
|
|
13164
|
-
|
|
13220
|
+
const skippedNow = /* @__PURE__ */ new Set();
|
|
13221
|
+
const allCandidates = await matchOwnItems(
|
|
13222
|
+
ownItems,
|
|
13223
|
+
owner,
|
|
13224
|
+
(o) => {
|
|
13225
|
+
stats.dropped++;
|
|
13226
|
+
rejectedNow.add(o.id);
|
|
13227
|
+
},
|
|
13228
|
+
(o) => {
|
|
13229
|
+
if (skippedNow.has(o)) return;
|
|
13230
|
+
skippedNow.add(o);
|
|
13231
|
+
stats.skipped++;
|
|
13232
|
+
}
|
|
13233
|
+
);
|
|
13165
13234
|
stats.candidates = allCandidates.length;
|
|
13166
13235
|
const cutoff = params.legacyCutoffLeaf ?? {};
|
|
13167
13236
|
const candidates = allCandidates.filter((c) => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloak.dev/sdk",
|
|
3
3
|
"description": "Shield, send, and swap on Solana privately — TypeScript SDK with UTXO-based zero-knowledge transactions",
|
|
4
|
-
"version": "0.2.6
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|