forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.ts
More file actions
33 lines (32 loc) · 1.13 KB
/
Copy pathsync.ts
File metadata and controls
33 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as StellarSdk from "@stellar/stellar-sdk";
import { computeNullifierHash } from "./poseidon2";
import { queryContract, POOL_CONTRACT_ID } from "./stellar";
import { getNotes, markNoteSpent } from "./notes";
/**
* Checks every unspent note against the on-chain nullifier set and marks any
* that have already been withdrawn (by this device or another). Returns the
* number of notes newly marked as spent.
*/
export async function syncSpentNotes(): Promise<number> {
const unspent = getNotes().filter((n) => !n.spent);
let count = 0;
for (const note of unspent) {
const poolId = note.poolId || POOL_CONTRACT_ID;
if (!poolId) continue;
try {
const nullifierHash = await computeNullifierHash(note.nullifier);
const val = await queryContract(poolId, "is_nullifier_used", [
StellarSdk.xdr.ScVal.scvBytes(
Buffer.from(nullifierHash.replace(/^0x/, ""), "hex"),
),
]);
if (val && StellarSdk.scValToNative(val) === true) {
markNoteSpent(note.commitment);
count++;
}
} catch {
// Best-effort — skip notes that fail to check
}
}
return count;
}