@ninsei-labs/sdk 0.56.1 → 0.57.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE-POLICY.md +9 -2
  3. package/README.md +17 -4
  4. package/THIRD_PARTY_NOTICES.md +174 -0
  5. package/assets/vendors/MANIFEST.json +1 -5
  6. package/assets/vendors/sdk/sdk-browser.js +5 -4
  7. package/engine/atomic/dleq2.js +116 -15
  8. package/engine/atomic/order.js +21 -3
  9. package/engine/core/config.js +17 -24
  10. package/engine/core/deployments.generated.js +103 -0
  11. package/engine/core/swap.js +7 -7
  12. package/engine/evm/asyncRoute.js +1 -1
  13. package/engine/evm/claimGas.js +2 -1
  14. package/engine/evm/escrow.js +13 -8
  15. package/engine/evm/factory.js +11 -6
  16. package/engine/evm/funding.js +8 -3
  17. package/engine/evm/index.js +2 -4
  18. package/engine/evm/session.js +3 -3
  19. package/engine/monero/scan.js +101 -22
  20. package/engine/recovery/restore.js +8 -8
  21. package/engine/sdk/bridge.js +2 -5
  22. package/index.d.ts +48 -11
  23. package/package.json +4 -2
  24. package/src/actions.mjs +8 -5
  25. package/src/codes.mjs +5 -0
  26. package/src/config.mjs +1 -0
  27. package/src/engine.mjs +6 -6
  28. package/src/feeEstimate.mjs +11 -4
  29. package/src/http.mjs +4 -1
  30. package/src/index.mjs +1 -1
  31. package/src/legs/kyber.mjs +23 -0
  32. package/src/lock.mjs +1 -0
  33. package/src/order.mjs +108 -20
  34. package/src/primitives.mjs +4 -5
  35. package/src/probes.mjs +9 -3
  36. package/src/public/networks.d.ts +45 -0
  37. package/src/public/networks.mjs +10 -1
  38. package/src/public/xmr.mjs +1 -1
  39. package/src/quotes.mjs +97 -23
  40. package/src/recovery-key.mjs +32 -14
  41. package/src/relayFeatures.mjs +25 -13
  42. package/src/relayFirmClient.mjs +33 -7
  43. package/src/relayQuote.mjs +87 -10
  44. package/src/relayView.mjs +3 -3
  45. package/src/swap-flow.mjs +100 -58
  46. package/src/swaps.mjs +234 -64
  47. package/src/sweep-restore.mjs +8 -4
  48. package/src/sweep.mjs +5 -1
  49. package/src/version.mjs +1 -1
  50. package/src/xmr-arrival.mjs +55 -24
  51. package/assets/vendors/atomic/atomic-browser.js +0 -24
  52. package/engine/atomic/order-client.js +0 -56
  53. package/engine/atomic/order-worker.js +0 -156
  54. package/engine/evm/asyncExec.js +0 -577
  55. package/engine/evm/asyncLegs.js +0 -308
@@ -23,9 +23,9 @@
23
23
  // hashes), never for a single transaction: otherwise a node links the user's IP to the trade. Requests carry
24
24
  // `Content-Type: text/plain` (verified on every node in the default list - it avoids the CORS preflight).
25
25
  //
26
- // WHAT THE MAKER GIVES IS ONLY A HINT. The maker's node may name the payout txid; the backend may relay it. It
27
- // decides nothing - a fabricated txid simply fails the check. Without a txid the client scans blocks from the
28
- // swap's birth height (a few dozen blocks).
26
+ // WHAT THE MAKER GIVES IS ONLY A REPORTED HINT. The maker's node may name the payout txid; the backend may relay
27
+ // it, but it does not narrow the walk and cannot hide another transfer. Every matching transfer is judged; the
28
+ // client scans blocks from the swap's birth height (a few dozen blocks).
29
29
  import { SdkError } from "./errors.mjs";
30
30
  import * as engine from "./engine.mjs";
31
31
 
@@ -47,7 +47,7 @@ const failureOf = (error, node) => {
47
47
  const name = String(params.name || (error && error.name) || "");
48
48
  if (params.status !== undefined && params.status !== null) code = "http";
49
49
  else if (/^(TimeoutError|AbortError)$/.test(name) || /timeout|abort/i.test(why) || /timeout|abort/i.test(name)) code = "timeout";
50
- else if (why === "bad-json" || why === "no-height" || why === "no-block") code = "bad-answer";
50
+ else if (why === "bad-json" || why === "no-height" || why === "no-block" || why === "busy" || why === "partial-transactions") code = "bad-answer";
51
51
  const out = { url: node.url, operator: node.operator, code };
52
52
  if (params.status !== undefined && params.status !== null) out.status = params.status;
53
53
  if (why) out.why = why;
@@ -106,6 +106,10 @@ async function getInfo(node, fetchImpl, timeoutMs) {
106
106
  const j = await call(node, "/json_rpc", { jsonrpc: "2.0", id: "info", method: "get_info" }, fetchImpl, timeoutMs);
107
107
  const r = j && j.result;
108
108
  if (!r || !Number.isFinite(Number(r.height))) throw new SdkError("xmr-no-nodes", { node: node.url, why: "no-height" });
109
+ // A BUSY NODE HAS NOT FINISHED ITS OWN VIEW. Its height is not evidence that the walk was complete.
110
+ if (r.busy === true || r.busy_syncing === true || (typeof r.status === "string" && r.status && r.status !== "OK")) {
111
+ throw new SdkError("xmr-no-nodes", { node: node.url, why: "busy" });
112
+ }
109
113
  return { height: Number(r.height), synced: r.synchronized !== false, nettype: r.nettype || null };
110
114
  }
111
115
 
@@ -127,15 +131,27 @@ async function getBlock(node, height, fetchImpl, timeoutMs) {
127
131
  async function getBlockTxs(node, hashes, fetchImpl, timeoutMs) {
128
132
  if (!hashes.length) return [];
129
133
  const j = await call(node, "/get_transactions", { txs_hashes: hashes, decode_as_json: true }, fetchImpl, timeoutMs);
130
- const wrappers = j.txs || [];
131
- return (j.txs_as_json || []).map((s, i) => {
132
- const body = typeof s === "string" ? JSON.parse(s) : s;
134
+ const wrappers = Array.isArray(j.txs) ? j.txs : [];
135
+ const bodies = Array.isArray(j.txs_as_json) ? j.txs_as_json : [];
136
+ // A PARTIAL ANSWER IS NOT AN EMPTY BLOCK. If the node returns fewer transactions than the block named, the walk
137
+ // cannot conclude "no payment": that answer is unknown and must stop the refund path.
138
+ if (bodies.length !== hashes.length || (wrappers.length && wrappers.length !== hashes.length)) {
139
+ throw new SdkError("xmr-no-nodes", { node: node.url, why: "partial-transactions", want: hashes.length, got: bodies.length });
140
+ }
141
+ const out = bodies.map((s, i) => {
142
+ let body;
143
+ try { body = typeof s === "string" ? JSON.parse(s) : s; }
144
+ catch { throw new SdkError("xmr-no-nodes", { node: node.url, why: "bad-json" }); }
133
145
  const w = wrappers[i] || {};
134
146
  body.tx_hash = String(w.tx_hash || body.tx_hash || "").replace(/^0x/, "").toLowerCase();
135
147
  body.block_height = w.block_height ?? null;
136
148
  body.confirmations = w.confirmations ?? null;
137
149
  return body;
138
- });
150
+ }).filter((body) => hashes.includes(String(body.tx_hash || "").toLowerCase()));
151
+ if (out.length !== hashes.length) {
152
+ throw new SdkError("xmr-no-nodes", { node: node.url, why: "partial-transactions", want: hashes.length, got: out.length });
153
+ }
154
+ return out;
139
155
  }
140
156
 
141
157
  // The amount of our own outputs in a matched transaction (a swap can be paid in several outputs). An output whose
@@ -143,29 +159,43 @@ async function getBlockTxs(node, hashes, fetchImpl, timeoutMs) {
143
159
  const sumOutputs = (hit) => hit.outputs.reduce((n, o) => n + (o.amountVerified ? (o.amountAtomic || 0n) : 0n), 0n);
144
160
 
145
161
  /**
146
- * FIND THE TRANSACTION, IF IT IS THERE. Blocks are walked from the swap's birth height upwards and the whole block
147
- * is asked for each height, so a public node never learns which transaction is ours. A txid hint only shortens the
148
- * work: a block without that hash is still fetched whole, but its transactions are not parsed.
162
+ * FIND THE BEST MATCHING TRANSFER. Blocks are walked from the swap's birth height upwards and the whole block is
163
+ * asked for each height, so a public node never learns which transaction is ours. Every matching transfer is
164
+ * judged: a full unlocked payout wins over dust or a locked one. The reported hint is not an input here.
149
165
  */
150
- async function locate(node, { txid, address, viewSecret, spendPub, fromHeight, toHeight, nowSec, fetchImpl, timeoutMs }) {
166
+ async function locate(node, { expectedAtomic, address, viewSecret, spendPub, fromHeight, toHeight, nowSec, fetchImpl, timeoutMs }) {
151
167
  const from = Math.max(0, intOf(fromHeight) ?? 0);
152
168
  const to = intOf(toHeight);
153
169
  if (!Number.isFinite(to) || to < from) throw new SdkError("bad-input", { field: "fromHeight", got: fromHeight });
170
+ let best = null;
154
171
  for (let h = from; h <= to; h++) {
155
172
  const { txHashes } = await getBlock(node, h, fetchImpl, timeoutMs);
156
173
  if (!txHashes.length) continue;
157
- if (txid && !txHashes.includes(txid)) continue; // the hint says it is not in this block
174
+ // EVERY BLOCK IS WALKED. The reported hint cannot hide a full payout in another block.
158
175
  const txs = await getBlockTxs(node, txHashes, fetchImpl, timeoutMs);
159
176
  for (const tx of txs) {
160
- if (txid && tx.tx_hash !== txid) continue;
161
- const hit = engine.xmrScan.matchTransaction(tx, [{ address, viewSecret, spendPub }], { height: h, nowSec });
177
+ const hit = engine.xmrScan.matchTransaction(tx, [{ address, viewSecret, spendPub }], {
178
+ height: h, tipHeight: Math.max(0, to), nowSec,
179
+ });
162
180
  // LOCATE FINDS AN OUTPUT, NOT AN AMOUNT. The amount can be forged or unbacked; the verification step below
163
181
  // refuses it. Requiring a positive verified sum here made two nodes that agreed on a forged amount look like
164
182
  // "no transaction at all".
165
- if (hit.length) return { txid: tx.tx_hash, height: h };
183
+ if (!hit.length) continue;
184
+ const amountAtomic = sumOutputs(hit[0]);
185
+ const amountVerified = hit[0].outputs.every((o) => o.amountVerified === true);
186
+ const locked = hit[0].unlock && hit[0].unlock.locked === true;
187
+ // JUDGE EVERY MATCHING TRANSFER. A dust transfer before the full payout is a real candidate but a worse
188
+ // one; a full unlocked payout wins over a full locked one. The verification step still compares the chosen
189
+ // transaction across operators.
190
+ const score = (amountAtomic >= expectedAtomic ? 4 : 0) + (amountVerified ? 2 : 0) + (locked ? 0 : 1);
191
+ const candidate = { txid: tx.tx_hash, height: h };
192
+ if (score === 7) return candidate;
193
+ if (!best || score > best.score || (score === best.score && amountAtomic > best.amountAtomic)) {
194
+ best = { ...candidate, score, amountAtomic };
195
+ }
166
196
  }
167
197
  }
168
- return null;
198
+ return best;
169
199
  }
170
200
 
171
201
  /**
@@ -212,11 +242,11 @@ export async function verifyArrival(request) {
212
242
 
213
243
  // 2. LOCATE: from the birth height, by whole blocks. The first reachable node does the walk.
214
244
  let located = null;
215
- let completedWalk = false;
245
+ const completedOperators = new Set();
216
246
  for (const { node } of live) {
217
247
  try {
218
- located = await locate(node, { txid: txidHint, address: request.address, viewSecret: request.viewSecret, spendPub: request.spendPub, fromHeight: request.fromHeight, toHeight: top, nowSec, fetchImpl, timeoutMs });
219
- completedWalk = true;
248
+ located = await locate(node, { expectedAtomic, address: request.address, viewSecret: request.viewSecret, spendPub: request.spendPub, fromHeight: request.fromHeight, toHeight: top, nowSec, fetchImpl, timeoutMs });
249
+ if (!located) completedOperators.add(node.operator);
220
250
  } catch (error) {
221
251
  failed.push(failureOf(error, node));
222
252
  located = null;
@@ -224,10 +254,11 @@ export async function verifyArrival(request) {
224
254
  if (located) break;
225
255
  }
226
256
  if (!located) {
227
- // ONE NODE FINISHED THE WALK AND FOUND NOTHING: that is a complete answer, even when another node failed.
228
- // Only when NO node finished the walk is every node unreachable.
229
- if (!completedWalk) throw new SdkError("xmr-no-nodes", { tried: nodes.map((n) => n.url), nodes: failed });
230
- return { seen: false, state: "pending", txid: txidHint, tip, checked: live.map((l) => ({ url: l.node.url, operator: l.node.operator, height: l.info.height })), failed };
257
+ // ABSENCE IS A CLAIM ABOUT MONEY, SO IT NEEDS TWO OPERATORS. One node can withhold a transaction and make
258
+ // this walk look empty; a refund signed on that answer would take the depositor's ETH from a maker who paid.
259
+ // Only when no node finished the walk is every node unreachable.
260
+ if (!completedOperators.size) throw new SdkError("xmr-no-nodes", { tried: nodes.map((n) => n.url), nodes: failed });
261
+ return { seen: false, state: "pending", absent: completedOperators.size >= 2, txid: txidHint, tip, completedOperators: [...completedOperators], checked: live.map((l) => ({ url: l.node.url, operator: l.node.operator, height: l.info.height })), failed };
231
262
  }
232
263
 
233
264
  // 3. VERIFY ON EVERY NODE: the SAME block hash, the SAME transaction inside it, the confirmations and the money.