@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.
- package/CHANGELOG.md +13 -0
- package/LICENSE-POLICY.md +9 -2
- package/README.md +17 -4
- package/THIRD_PARTY_NOTICES.md +174 -0
- package/assets/vendors/MANIFEST.json +1 -5
- package/assets/vendors/sdk/sdk-browser.js +5 -4
- package/engine/atomic/dleq2.js +116 -15
- package/engine/atomic/order.js +21 -3
- package/engine/core/config.js +17 -24
- package/engine/core/deployments.generated.js +103 -0
- package/engine/core/swap.js +7 -7
- package/engine/evm/asyncRoute.js +1 -1
- package/engine/evm/claimGas.js +2 -1
- package/engine/evm/escrow.js +13 -8
- package/engine/evm/factory.js +11 -6
- package/engine/evm/funding.js +8 -3
- package/engine/evm/index.js +2 -4
- package/engine/evm/session.js +3 -3
- package/engine/monero/scan.js +101 -22
- package/engine/recovery/restore.js +8 -8
- package/engine/sdk/bridge.js +2 -5
- package/index.d.ts +48 -11
- package/package.json +4 -2
- package/src/actions.mjs +8 -5
- package/src/codes.mjs +5 -0
- package/src/config.mjs +1 -0
- package/src/engine.mjs +6 -6
- package/src/feeEstimate.mjs +11 -4
- package/src/http.mjs +4 -1
- package/src/index.mjs +1 -1
- package/src/legs/kyber.mjs +23 -0
- package/src/lock.mjs +1 -0
- package/src/order.mjs +108 -20
- package/src/primitives.mjs +4 -5
- package/src/probes.mjs +9 -3
- package/src/public/networks.d.ts +45 -0
- package/src/public/networks.mjs +10 -1
- package/src/public/xmr.mjs +1 -1
- package/src/quotes.mjs +97 -23
- package/src/recovery-key.mjs +32 -14
- package/src/relayFeatures.mjs +25 -13
- package/src/relayFirmClient.mjs +33 -7
- package/src/relayQuote.mjs +87 -10
- package/src/relayView.mjs +3 -3
- package/src/swap-flow.mjs +100 -58
- package/src/swaps.mjs +234 -64
- package/src/sweep-restore.mjs +8 -4
- package/src/sweep.mjs +5 -1
- package/src/version.mjs +1 -1
- package/src/xmr-arrival.mjs +55 -24
- package/assets/vendors/atomic/atomic-browser.js +0 -24
- package/engine/atomic/order-client.js +0 -56
- package/engine/atomic/order-worker.js +0 -156
- package/engine/evm/asyncExec.js +0 -577
- package/engine/evm/asyncLegs.js +0 -308
package/src/xmr-arrival.mjs
CHANGED
|
@@ -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
|
|
27
|
-
//
|
|
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
|
-
|
|
132
|
-
|
|
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
|
|
147
|
-
*
|
|
148
|
-
*
|
|
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, {
|
|
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
|
-
|
|
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
|
-
|
|
161
|
-
|
|
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)
|
|
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
|
|
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
|
-
|
|
245
|
+
const completedOperators = new Set();
|
|
216
246
|
for (const { node } of live) {
|
|
217
247
|
try {
|
|
218
|
-
located = await locate(node, {
|
|
219
|
-
|
|
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
|
-
//
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
|
|
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.
|