@ninsei-labs/sdk 0.55.4 → 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 +60 -0
- package/LICENSE-POLICY.md +9 -2
- package/README.md +51 -24
- package/THIRD_PARTY_NOTICES.md +174 -0
- package/assets/vendors/MANIFEST.json +18 -0
- package/assets/vendors/sdk/sdk-browser.js +2059 -2
- package/engine/atomic/dleq2.js +187 -22
- 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/store.js +25 -4
- package/engine/core/swap.js +54 -52
- 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 +24 -26
- 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 +3 -6
- package/index.d.ts +57 -27
- package/package.json +25 -8
- package/src/actions.mjs +52 -9
- package/src/adapters/evm-wallet.mjs +24 -5
- package/src/adapters/index.d.ts +66 -0
- package/src/adapters/index.mjs +0 -6
- package/src/codes.mjs +14 -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 +4 -3
- package/src/legs/kyber.mjs +23 -0
- package/src/lock.mjs +2 -1
- package/src/order.mjs +108 -20
- package/src/primitives.mjs +4 -5
- package/src/probes.mjs +9 -3
- package/src/provider/envFile.mjs +11 -1
- package/src/public/networks.d.ts +45 -0
- package/src/public/networks.mjs +10 -1
- package/src/public/order.d.ts +4 -0
- package/src/public/recovery.d.ts +40 -54
- package/src/public/recovery.mjs +2 -8
- package/src/public/xmr.d.ts +14 -3
- package/src/public/xmr.mjs +1 -1
- package/src/quoteEip712Spec.mjs +9 -1
- package/src/quotes.mjs +118 -49
- package/src/record.mjs +3 -3
- package/src/recovery-key.mjs +161 -237
- package/src/relayFeatures.mjs +25 -13
- package/src/relayFirmClient.mjs +69 -6
- package/src/relayQuote.mjs +87 -10
- package/src/relayRequest.mjs +12 -3
- package/src/relayView.mjs +3 -3
- package/src/swap-flow.mjs +153 -67
- package/src/swaps.mjs +346 -174
- package/src/sweep-restore.mjs +14 -6
- package/src/sweep.mjs +34 -11
- 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/adapters/node-test-evm-wallet.mjs +0 -357
package/engine/core/swap.js
CHANGED
|
@@ -50,8 +50,8 @@ export const TERMINAL = {
|
|
|
50
50
|
refunded_eth: { phase: "refunded_eth", label: "ETH refunded" },
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
export function createSwap(input) {
|
|
54
|
-
const s = loadState();
|
|
53
|
+
export function createSwap(input, store = null) {
|
|
54
|
+
const s = store ? store.loadState() : loadState();
|
|
55
55
|
const now = Date.now();
|
|
56
56
|
const swap = {
|
|
57
57
|
id: shortId(),
|
|
@@ -61,7 +61,7 @@ export function createSwap(input) {
|
|
|
61
61
|
// THERE IS NO QUOTE KIND IN THE RECORD. The former field mode ("floating"/"fixed") was a choice of price
|
|
62
62
|
// kind, and in version 4 there is no second kind: a firm quote is exactly one - the order quote.
|
|
63
63
|
network: input.network || DEFAULT_CHAIN,
|
|
64
|
-
moneroNetwork: MONERO.networkType,
|
|
64
|
+
moneroNetwork: input.moneroNetwork || MONERO.networkType,
|
|
65
65
|
realStart: now,
|
|
66
66
|
simStartWall: now,
|
|
67
67
|
createdAt: new Date(now).toISOString(),
|
|
@@ -121,7 +121,7 @@ export function createSwap(input) {
|
|
|
121
121
|
|
|
122
122
|
s.swaps[swap.id] = swap;
|
|
123
123
|
s.activeSwapId = swap.id;
|
|
124
|
-
saveState(s);
|
|
124
|
+
if (store) store.saveState(s); else saveState(s);
|
|
125
125
|
return swap;
|
|
126
126
|
}
|
|
127
127
|
|
|
@@ -132,7 +132,7 @@ export function createSwap(input) {
|
|
|
132
132
|
//
|
|
133
133
|
// WHAT THE REVERSE DEAL DOES DIFFERENTLY. On the buy flow the PAGE funds the escrow, and the counterparty's half
|
|
134
134
|
// is only learned after settlement. On the sell flow the PROVIDER funds the escrow, the page merely requests the
|
|
135
|
-
// order, and it computes ITS OWN halves (atomic/order
|
|
135
|
+
// order, and it computes ITS OWN halves (atomic/order.js, newMakerMaterial). So fields are named by side:
|
|
136
136
|
// our half is `half`, and the DEPOSITOR's (provider's) point and view half are counterSpendPoint/counterView*.
|
|
137
137
|
// The page does not know the depositor's SPEND half and must not: the contract publishes it on settlement, and
|
|
138
138
|
// only then does the sweep of our own XMR open up (see refundHalfRevealed).
|
|
@@ -146,7 +146,7 @@ export function createReverseSwap(input = {}) {
|
|
|
146
146
|
scenario: "happy",
|
|
147
147
|
speed: Number(input.speed) || 60,
|
|
148
148
|
network: input.network || DEFAULT_CHAIN,
|
|
149
|
-
moneroNetwork: MONERO.networkType,
|
|
149
|
+
moneroNetwork: input.moneroNetwork || MONERO.networkType,
|
|
150
150
|
realStart: now,
|
|
151
151
|
simStartWall: now,
|
|
152
152
|
createdAt: new Date(now).toISOString(),
|
|
@@ -179,7 +179,7 @@ export function createReverseSwap(input = {}) {
|
|
|
179
179
|
counterViewHalf: input.counterViewHalf || null,
|
|
180
180
|
counterViewPoint: input.counterViewPoint || null,
|
|
181
181
|
counterSpendPoint: input.counterSpendPoint || null,
|
|
182
|
-
moneroNetwork: MONERO.networkType,
|
|
182
|
+
moneroNetwork: input.moneroNetwork || MONERO.networkType,
|
|
183
183
|
birthHeight: input.escrowBirthHeight || null,
|
|
184
184
|
readyBy: input.readyBy || null,
|
|
185
185
|
t1: input.t1 || null,
|
|
@@ -235,16 +235,16 @@ export function halvesWalletOf(swap) {
|
|
|
235
235
|
};
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
export function forgetSwap(id) {
|
|
239
|
-
const s = loadState();
|
|
238
|
+
export function forgetSwap(id, store = null) {
|
|
239
|
+
const s = store ? store.loadState() : loadState();
|
|
240
240
|
if (!s.swaps[id]) return false;
|
|
241
241
|
delete s.swaps[id];
|
|
242
|
-
saveState(s);
|
|
242
|
+
if (store) store.saveState(s); else saveState(s);
|
|
243
243
|
return true;
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
export function getSwap(id) {
|
|
247
|
-
return loadState().swaps[id] || null;
|
|
246
|
+
export function getSwap(id, store = null) {
|
|
247
|
+
return (store ? store.loadState() : loadState()).swaps[id] || null;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
export function allSwaps() {
|
|
@@ -289,9 +289,9 @@ export function serverRowToSwap(row) {
|
|
|
289
289
|
|
|
290
290
|
// Add server records the browser lacks. Returns the number added: the caller decides from it whether to redraw
|
|
291
291
|
// the list, rather than guessing.
|
|
292
|
-
export function addServerSwaps(rows) {
|
|
292
|
+
export function addServerSwaps(rows, store = null) {
|
|
293
293
|
if (!Array.isArray(rows) || rows.length === 0) return 0;
|
|
294
|
-
const s = loadState();
|
|
294
|
+
const s = store ? store.loadState() : loadState();
|
|
295
295
|
let added = 0;
|
|
296
296
|
for (const row of rows) {
|
|
297
297
|
if (!row || !row.id || !row.escrow) continue;
|
|
@@ -299,7 +299,7 @@ export function addServerSwaps(rows) {
|
|
|
299
299
|
s.swaps[row.id] = serverRowToSwap(row);
|
|
300
300
|
added += 1;
|
|
301
301
|
}
|
|
302
|
-
if (added) saveState(s);
|
|
302
|
+
if (added) { if (store) store.saveState(s); else saveState(s); }
|
|
303
303
|
return added;
|
|
304
304
|
}
|
|
305
305
|
|
|
@@ -313,14 +313,14 @@ export function activeSwaps() {
|
|
|
313
313
|
// fields - escrow address, salt, deadlines, secret - and did NOT save it: the edits lived in memory and vanished
|
|
314
314
|
// with the tab, while the progress screen read the OLD record from storage. That is exactly why the interface
|
|
315
315
|
// showed an address not on chain even when funding had succeeded.
|
|
316
|
-
export function saveSwap(swap) {
|
|
317
|
-
return persist(swap);
|
|
316
|
+
export function saveSwap(swap, store = null) {
|
|
317
|
+
return persist(swap, store);
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
-
function persist(swap) {
|
|
321
|
-
const s = loadState();
|
|
320
|
+
function persist(swap, store = null) {
|
|
321
|
+
const s = store ? store.loadState() : loadState();
|
|
322
322
|
s.swaps[swap.id] = swap;
|
|
323
|
-
saveState(s);
|
|
323
|
+
if (store) store.saveState(s); else saveState(s);
|
|
324
324
|
return swap;
|
|
325
325
|
}
|
|
326
326
|
|
|
@@ -337,8 +337,8 @@ function findEvent(swap, type) {
|
|
|
337
337
|
// User actions
|
|
338
338
|
// ---------------------------------------------------------------------------
|
|
339
339
|
|
|
340
|
-
export function confirmReady(id) {
|
|
341
|
-
const swap = getSwap(id);
|
|
340
|
+
export function confirmReady(id, store = null) {
|
|
341
|
+
const swap = getSwap(id, store);
|
|
342
342
|
if (!swap || swap.settlement) return swap;
|
|
343
343
|
const view = derive(swap);
|
|
344
344
|
if (!view.can.ready) throw new Error("ready() is not available in this phase");
|
|
@@ -346,7 +346,7 @@ export function confirmReady(id) {
|
|
|
346
346
|
event(swap, "ready", atSim);
|
|
347
347
|
pushLog(swap, atSim, "ready() called by you", "maker may now claim the ETH escrow");
|
|
348
348
|
if (!findEvent(swap, "funded_note")) event(swap, "funded_note", 0);
|
|
349
|
-
return persist(swap);
|
|
349
|
+
return persist(swap, store);
|
|
350
350
|
}
|
|
351
351
|
|
|
352
352
|
// THE COUNTERPARTY'S SPEND HALF - THE ONE PLACE THAT SHOWS WHETHER THERE IS ANYTHING TO ASSEMBLE. The Monero
|
|
@@ -363,8 +363,8 @@ export function refundHalfRevealed(swap) {
|
|
|
363
363
|
// stayed on the swap's one-time address, and the contract revealed the DEPOSITOR's half on refund. Then the same
|
|
364
364
|
// sweep takes our coins back, and the settlement is marked recovered: the money returned to the user rather
|
|
365
365
|
// than leaving through the swap.
|
|
366
|
-
export function sweepNow(id) {
|
|
367
|
-
const swap = getSwap(id);
|
|
366
|
+
export function sweepNow(id, store = null) {
|
|
367
|
+
const swap = getSwap(id, store);
|
|
368
368
|
if (!swap) return swap;
|
|
369
369
|
// The deal already settled: no second sweep - EXCEPT a reverse-flow refund, which is how our own XMR are taken
|
|
370
370
|
// back.
|
|
@@ -379,11 +379,11 @@ export function sweepNow(id) {
|
|
|
379
379
|
swap.settlement = { kind: "success", phase: TERMINAL.success.phase, atSim, txid: tx.txid, by: "you",
|
|
380
380
|
...(fromRefund ? { recovered: true } : {}) };
|
|
381
381
|
pushLog(swap, atSim, "XMR swept to your address", tx.txid);
|
|
382
|
-
return persist(swap);
|
|
382
|
+
return persist(swap, store);
|
|
383
383
|
}
|
|
384
384
|
|
|
385
|
-
export function refundEth(id, by = "you") {
|
|
386
|
-
const swap = getSwap(id);
|
|
385
|
+
export function refundEth(id, by = "you", store = null) {
|
|
386
|
+
const swap = getSwap(id, store);
|
|
387
387
|
if (!swap || swap.settlement) return swap;
|
|
388
388
|
const view = derive(swap);
|
|
389
389
|
if (!view.can.refund) throw new Error("refund is not available in this phase");
|
|
@@ -391,7 +391,7 @@ export function refundEth(id, by = "you") {
|
|
|
391
391
|
const tx = chain.refundEscrow({ swapId: swap.id, by });
|
|
392
392
|
swap.settlement = { kind: "refund_eth", phase: TERMINAL.refunded_eth.phase, atSim, txHash: tx.txHash, by };
|
|
393
393
|
pushLog(swap, atSim, `ETH refunded to your wallet (${by})`, tx.txHash);
|
|
394
|
-
return persist(swap);
|
|
394
|
+
return persist(swap, store);
|
|
395
395
|
}
|
|
396
396
|
|
|
397
397
|
// ---------------------------------------------------------------------------
|
|
@@ -519,25 +519,33 @@ export function derive(swap, now = nowReal()) {
|
|
|
519
519
|
}
|
|
520
520
|
} else if (live && readyAt === null && chainReadyByMs !== null && nowReal() >= chainReadyByMs
|
|
521
521
|
&& (chainT1Ms === null || nowReal() < chainT1Ms)) {
|
|
522
|
-
//
|
|
523
|
-
//
|
|
524
|
-
//
|
|
525
|
-
// depositor's half, so the maker takes his XMR back without waiting for t1. That is the answer to the dead
|
|
526
|
-
// zone: before, the money and the half waited for t1, and the Monero leg was unavailable to both sides.
|
|
522
|
+
// THE DEAD ZONE IS ABOUT THE REFUND, NOT THE CLAIM. `refund()` rejects a call before `t1` when
|
|
523
|
+
// `block.timestamp >= readyBy` and `ready` is false; the claim is open at `readyBy` even without the mark. The
|
|
524
|
+
// screen must not offer a refund that reverts.
|
|
527
525
|
phase = "dead_zone";
|
|
528
|
-
phaseLabel = "Ready deadline passed - the refund
|
|
526
|
+
phaseLabel = "Ready deadline passed - the refund opens at T1";
|
|
529
527
|
can.ready = false;
|
|
530
|
-
can.refund =
|
|
531
|
-
can.refundByAnyone =
|
|
532
|
-
note = "The ready() deadline has passed without your confirmation
|
|
533
|
-
"refund is
|
|
534
|
-
"
|
|
535
|
-
(chainT1Ms !== null ? "T1
|
|
528
|
+
can.refund = false;
|
|
529
|
+
can.refundByAnyone = false;
|
|
530
|
+
note = "The ready() deadline has passed without your confirmation. The maker can claim now, and the escrow " +
|
|
531
|
+
"refuses a refund before T1 while the mark is absent. The refund opens at T1 and returns your ETH and " +
|
|
532
|
+
"publishes your half. " +
|
|
533
|
+
(chainT1Ms !== null ? "T1: " + new Date(chainT1Ms).toISOString() + "." : "");
|
|
536
534
|
} else if (live && readyAt === null) {
|
|
537
535
|
// Live mode: Monero-leg states come from the backend. The simulated EVM-leg timeouts (T0/T1, watchtower)
|
|
538
536
|
// are deliberately NOT shown here: the contract does not exist yet, and a "refund ETH" button would be a
|
|
539
537
|
// lie. The "EVM leg - simulation" mark hangs in the UI separately (derive().simulated).
|
|
540
|
-
if (
|
|
538
|
+
if (chainT1Ms !== null && nowReal() >= chainT1Ms) {
|
|
539
|
+
// THE OTHER SIDE OF THE DEAD ZONE. From T1 the refund is open even without the ready mark; before this
|
|
540
|
+
// branch existed, a live order without the mark stayed "received, waiting" forever after T1.
|
|
541
|
+
phase = "refund_available";
|
|
542
|
+
phaseLabel = "Claim deadline passed - the refund is open";
|
|
543
|
+
can.ready = false;
|
|
544
|
+
can.refund = true;
|
|
545
|
+
can.refundByAnyone = true;
|
|
546
|
+
note = "The claim deadline passed without the ready mark. The refund is open to anyone who can present " +
|
|
547
|
+
"the depositor's half - that half is yours in the recovery file. It returns your ETH and publishes your half.";
|
|
548
|
+
} else if (xmr.status === XMR_STATUS.LOCKED) {
|
|
541
549
|
// THE MONEY IS AT THE ADDRESS BUT LOCKED BY unlock_time. This is a NAMED state, not "waiting for arrival":
|
|
542
550
|
// otherwise the screen would be silent and the amount would look zero. A readiness mark is impossible here
|
|
543
551
|
// - claiming ETH against locked XMR would give the maker ETH for money that cannot be paid with.
|
|
@@ -644,16 +652,10 @@ export function derive(swap, now = nowReal()) {
|
|
|
644
652
|
can.refund = realNow >= escrowT0; // an old order: from t0, and only the locker
|
|
645
653
|
can.refundByAnyone = realNow >= chainT1; // from t1 - anyone
|
|
646
654
|
} else {
|
|
647
|
-
// A NEW ORDER
|
|
648
|
-
//
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
// mark, the contract refuses: an honest refusal instead of a promise we would not keep.
|
|
652
|
-
const chainReadyBy = swap.escrow && swap.escrow.readyBy ? Number(swap.escrow.readyBy) * 1000 : null;
|
|
653
|
-
const earlyRefund = chainReadyBy !== null && realNow >= chainReadyBy && readyAt === null;
|
|
654
|
-
can.refund = realNow >= chainT1 || earlyRefund;
|
|
655
|
-
can.refundByAnyone = can.refund; // and from t1 a refund is open to all at once
|
|
656
|
-
if (earlyRefund && realNow < chainT1) refundAfterSim = null; // the refund is already open: no countdown
|
|
655
|
+
// A NEW ORDER HAS ONE BOUNDARY. The escrow's `refund()` rejects every call before T1, including the
|
|
656
|
+
// window after readyBy without the mark; from T1 it is open to anyone. There is no early refund to offer.
|
|
657
|
+
can.refund = realNow >= chainT1;
|
|
658
|
+
can.refundByAnyone = can.refund; // from t1 a refund is open to all at once
|
|
657
659
|
}
|
|
658
660
|
} else if (!isLiveNetwork(swap)) {
|
|
659
661
|
can.refund = sim >= t1At; // a demo deal: as before, from the simulator
|
package/engine/evm/asyncRoute.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// refuses native as an order asset). So a wrapped provider is NEVER chosen as the sole provider for a native need:
|
|
19
19
|
// it is a LIQUIDITY leg, and the route to native is a COMPOSITION of two orders - the liquidity leg (token -> WETH)
|
|
20
20
|
// and a native leg on a native-capable provider (WETH -> native coin, CoW). The choice returns a route record or a
|
|
21
|
-
// composition; the composition is EXECUTED
|
|
21
|
+
// composition; the composition is EXECUTED by the package provider layer, which pays the second leg's own fee out of the
|
|
22
22
|
// reserve so the person is not sent into a composition that leaves them short of the deposit.
|
|
23
23
|
//
|
|
24
24
|
// IT MIRRORS THE PACKAGE'S PROVIDER LAYER ON PURPOSE. The move to async lives in the SDK package
|
package/engine/evm/claimGas.js
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
// cost plus an EXPLICIT margin for base-fee growth between ticket and claim.
|
|
13
13
|
|
|
14
14
|
// THE CLAIM GAS LIMIT IS ONE NUMBER ON BOTH SIDES. MEASURED: an honest claim costs 120 721 gas on a live chain,
|
|
15
|
-
// more than the old 120 000, which cut off legitimate claims. The limit has a margin: 200 000.
|
|
15
|
+
// more than the old 120 000, which cut off legitimate claims. The limit has a margin: 200 000. The Arbitrum L1
|
|
16
|
+
// share of a tipped claim was not part of that measurement.
|
|
16
17
|
export const CLAIM_GAS_LIMIT = 200_000n;
|
|
17
18
|
|
|
18
19
|
// THE TIP FLOOR IS NETWORK POLICY, NOT A CONSTANT IN THE SENDING CODE. A fixed 1 gwei minimum inflated
|
package/engine/evm/escrow.js
CHANGED
|
@@ -142,26 +142,31 @@ export function encodeRead(name) {
|
|
|
142
142
|
return sel;
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
const strictWord = (returnedHex, what, words) => {
|
|
146
|
+
if (typeof returnedHex !== "string" || !/^0x[0-9a-fA-F]*$/.test(returnedHex)) {
|
|
147
|
+
throw new Error("invalid " + what + ": " + returnedHex);
|
|
148
|
+
}
|
|
149
|
+
const hex = returnedHex.slice(2);
|
|
150
|
+
if (hex.length !== words * 64) throw new Error("short answer " + what + ": " + returnedHex);
|
|
151
|
+
return hex.toLowerCase();
|
|
152
|
+
};
|
|
153
|
+
|
|
145
154
|
// Parse a bytes32 answer: one word.
|
|
146
155
|
export function decodeBytes32(returnedHex, what = "value") {
|
|
147
|
-
|
|
148
|
-
if (hex.length < 64) throw new Error("short answer " + what + ": " + returnedHex);
|
|
149
|
-
return "0x" + hex.slice(0, 64).toLowerCase();
|
|
156
|
+
return "0x" + strictWord(returnedHex, what, 1);
|
|
150
157
|
}
|
|
151
158
|
|
|
152
159
|
// Parse the t1() answer: one word, uint64 inside uint256.
|
|
153
160
|
export function decodeUint64(returnedHex, what = "value") {
|
|
154
|
-
const hex =
|
|
155
|
-
|
|
156
|
-
const v = BigInt("0x" + hex.slice(0, 64));
|
|
161
|
+
const hex = strictWord(returnedHex, what, 1);
|
|
162
|
+
const v = BigInt("0x" + hex);
|
|
157
163
|
if (v > 18446744073709551615n) throw new Error("answer " + what + " does not fit in uint64: " + v);
|
|
158
164
|
return Number(v);
|
|
159
165
|
}
|
|
160
166
|
|
|
161
167
|
// Parse the status() answer: four 32-byte words - three flags and the balance.
|
|
162
168
|
export function decodeStatus(returnedHex) {
|
|
163
|
-
const hex =
|
|
164
|
-
if (hex.length < 256) throw new Error("short status() answer: " + returnedHex);
|
|
169
|
+
const hex = strictWord(returnedHex, "status()", 4);
|
|
165
170
|
const word = (i) => hex.slice(i * 64, (i + 1) * 64);
|
|
166
171
|
return {
|
|
167
172
|
isFunded: BigInt("0x" + word(0)) !== 0n,
|
package/engine/evm/factory.js
CHANGED
|
@@ -5,21 +5,20 @@
|
|
|
5
5
|
// Calls to the NinseiSwap escrow factory contract.
|
|
6
6
|
//
|
|
7
7
|
// There are no EVM libraries in the project and we are not adding any. BOTH FACTORY FUNCTIONS NOW TAKE THE
|
|
8
|
-
// SIGNED QUOTE (OrderQuote.Quote)
|
|
9
|
-
// and the registry address are PART of the quote, not a contract setting: the factory no longer has feeBps()/
|
|
8
|
+
// SIGNED QUOTE (OrderQuote.Quote) and the signature as three words (r, s, v). The depositor fields are inside
|
|
9
|
+
// the quote now, so they are not separate arguments. The fee and the registry address are PART of the quote, not a contract setting: the factory no longer has feeBps()/
|
|
10
10
|
// feeFor() (the router was removed), so there is nothing to ask. All arguments are static, so calldata is a
|
|
11
11
|
// selector plus 32-byte words, built here in one place and checked mechanically.
|
|
12
12
|
//
|
|
13
|
-
// cast sig "predict((address,address,address,address,address,address,uint256,uint64,uint64,bytes32,bytes32,bytes32,uint256,uint256,uint256,address,uint64,uint256,uint256,uint256
|
|
14
|
-
// cast sig "createOrderAndFund(<same tuple>,...)"
|
|
13
|
+
// cast sig "predict((address,address,address,address,address,address,uint256,uint64,uint64,bytes32,bytes32,bytes32,uint256,uint256,uint256,address,uint64,uint256,uint256,uint256,bytes32,bytes32,bytes32),bytes32,bytes32,uint8)" -> 0x76e3fdc8
|
|
14
|
+
// cast sig "createOrderAndFund(<same tuple>,...)" -> 0x50db6721
|
|
15
15
|
|
|
16
16
|
export const FACTORY_METHODS = {
|
|
17
|
-
// THE SELECTORS FOLLOW THE
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
predict: "0x3e949279",
|
|
17
|
+
// THE SELECTORS FOLLOW THE 23-FIELD QUOTE. The depositor fields joined the signed tuple, so both selectors
|
|
18
|
+
// changed again; the old 20-field values (0x3e949279 / 0x3a504547) address a function this factory no longer has.
|
|
19
|
+
predict: "0x76e3fdc8",
|
|
21
20
|
// create the escrow AND deposit funds in ONE transaction (a value transfer with the call).
|
|
22
|
-
createOrderAndFund: "
|
|
21
|
+
createOrderAndFund: "0x50db6721",
|
|
23
22
|
};
|
|
24
23
|
|
|
25
24
|
// The OrderCreated event topic. It grew the registry address, the provider and the signing key, and now carries
|
|
@@ -40,10 +39,8 @@ export const ORDER_CREATED_TOPICS = Object.freeze([ORDER_CREATED_TOPIC, ORDER_CR
|
|
|
40
39
|
export const QUOTE_FIELDS = [
|
|
41
40
|
"provider", "quoteKey", "registry", "cashier", "locker", "claimer", "amount", "readyBy", "t1", "salt",
|
|
42
41
|
"commitHalfClaimer", "edPointClaimer", "chainId", "feeBps", "fee", "feeRecipient", "validUntil", "nonce", "xmrAmount",
|
|
43
|
-
"claimTip",
|
|
42
|
+
"claimTip", "commitHalfLocker", "edPointLocker", "edViewPointLocker",
|
|
44
43
|
];
|
|
45
|
-
// The depositor fields are NOT in the quote: they go as separate arguments (into the escrow init code).
|
|
46
|
-
const LOCKER_HALF_FIELDS = ["commitHalfLocker", "edPointLocker", "edViewPointLocker"];
|
|
47
44
|
const ADDR_FIELDS = ["provider", "quoteKey", "registry", "cashier", "locker", "claimer", "feeRecipient"];
|
|
48
45
|
const B32_FIELDS = ["salt", "commitHalfClaimer", "edPointClaimer", "commitHalfLocker", "edPointLocker", "edViewPointLocker"];
|
|
49
46
|
|
|
@@ -99,6 +96,9 @@ function quoteWords(quote) {
|
|
|
99
96
|
feeRecipient: word(quote.feeRecipient, 32), validUntil: word(quote.validUntil, 32), nonce: word(quote.nonce, 32),
|
|
100
97
|
xmrAmount: word(quote.xmrAmount, 32),
|
|
101
98
|
claimTip: word(quote.claimTip, 32),
|
|
99
|
+
commitHalfLocker: word(asHex32(quote.commitHalfLocker), 32),
|
|
100
|
+
edPointLocker: word(asHex32(quote.edPointLocker), 32),
|
|
101
|
+
edViewPointLocker: word(asHex32(quote.edViewPointLocker), 32),
|
|
102
102
|
};
|
|
103
103
|
return QUOTE_FIELDS.map((n) => map[n]).join("");
|
|
104
104
|
}
|
|
@@ -113,15 +113,8 @@ function signatureWords(signature) {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
// The common part of predict and createOrderAndFund.
|
|
116
|
-
function lockerHalfWords(quote) {
|
|
117
|
-
return LOCKER_HALF_FIELDS.map((name) => {
|
|
118
|
-
if (quote[name] === undefined || quote[name] === null || quote[name] === "") throw new Error("the quote has no depositor field: " + name);
|
|
119
|
-
return word(asHex32(quote[name]), 32);
|
|
120
|
-
}).join("");
|
|
121
|
-
}
|
|
122
|
-
|
|
123
116
|
function quoteCallArgs(quote) {
|
|
124
|
-
return quoteWords(quote) + signatureWords(quote.signature)
|
|
117
|
+
return quoteWords(quote) + signatureWords(quote.signature);
|
|
125
118
|
}
|
|
126
119
|
|
|
127
120
|
// PREDICT THE ADDRESS. The argument set matches createOrderAndFund BYTE FOR BYTE (the address is the code hash of
|
|
@@ -136,11 +129,18 @@ export function encodeCreateOrderAndFund(quote) {
|
|
|
136
129
|
return FACTORY_METHODS.createOrderAndFund + quoteCallArgs(quote);
|
|
137
130
|
}
|
|
138
131
|
|
|
132
|
+
const strictWord = (returnedHex, what, words = 1) => {
|
|
133
|
+
if (typeof returnedHex !== "string" || !/^0x[0-9a-fA-F]*$/.test(returnedHex)) {
|
|
134
|
+
throw new Error("invalid " + what + ": " + returnedHex);
|
|
135
|
+
}
|
|
136
|
+
const hex = returnedHex.slice(2);
|
|
137
|
+
if (hex.length !== words * 64) throw new Error("short answer: " + returnedHex);
|
|
138
|
+
return hex.toLowerCase();
|
|
139
|
+
};
|
|
140
|
+
|
|
139
141
|
// predict() answer - one address in a 32-byte word.
|
|
140
142
|
export function decodeAddress(returnedHex) {
|
|
141
|
-
|
|
142
|
-
if (hex.length < 64) throw new Error("short answer: " + returnedHex);
|
|
143
|
-
return "0x" + hex.slice(24, 64);
|
|
143
|
+
return "0x" + strictWord(returnedHex, "predict()").slice(24, 64);
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
|
|
@@ -174,9 +174,7 @@ export function decodeImplementation(returnedHex) {
|
|
|
174
174
|
|
|
175
175
|
// implementationCodeHash() - one bytes32 word.
|
|
176
176
|
export function decodeImplementationCodeHash(returnedHex) {
|
|
177
|
-
|
|
178
|
-
if (hex.length < 64) throw new Error("short answer from implementationCodeHash(): " + returnedHex);
|
|
179
|
-
return "0x" + hex.slice(0, 64).toLowerCase();
|
|
177
|
+
return "0x" + strictWord(returnedHex, "implementationCodeHash()");
|
|
180
178
|
}
|
|
181
179
|
|
|
182
180
|
// VERDICT ON THE IMPLEMENTATION - the same three conditions the factory checks:
|
package/engine/evm/funding.js
CHANGED
|
@@ -213,7 +213,7 @@ async function waitReceipt(receiptReader, hash, { tries = 12, delayMs = 1500 } =
|
|
|
213
213
|
|
|
214
214
|
// The full funding path. Steps are returned outward so the interface can show each one: predict is a free call,
|
|
215
215
|
// createOrder and lock are transactions in the user's wallet.
|
|
216
|
-
export async function fundOrder({ factory, locker, claimer, commitHalfLocker, commitHalfClaimer, edPointLocker, edPointClaimer, edViewPointLocker, salt: providedSalt, amountWei, amountLabel, claimWindowSeconds, readyWindowSeconds, readyBy, t1, quote, call, send, receipt, onStep }) {
|
|
216
|
+
export async function fundOrder({ factory, locker, claimer, commitHalfLocker, commitHalfClaimer, edPointLocker, edPointClaimer, edViewPointLocker, salt: providedSalt, amountWei, amountLabel, claimWindowSeconds, readyWindowSeconds, readyBy, t1, quote, call, send, receipt, onStep, onPredicted }) {
|
|
217
217
|
if (!factory) throw new Error("no escrow factory address set for this network");
|
|
218
218
|
if (!locker) throw new Error("no EVM wallet connected: there is nobody to deposit");
|
|
219
219
|
if (!claimer) throw new Error("the order recipient (claimer) is not set");
|
|
@@ -245,7 +245,9 @@ export async function fundOrder({ factory, locker, claimer, commitHalfLocker, co
|
|
|
245
245
|
// code. Here that is guaranteed because both predict and createOrderAndFund are built from THE SAME terms: they
|
|
246
246
|
// can diverge only if one of the two places is edited alone. They already did once (the preimage was missing from
|
|
247
247
|
// the prediction); the cure is a shared source of fields, not care.
|
|
248
|
-
// THE DEPOSITOR'S FIELDS GO AS SEPARATE FACTORY ARGUMENTS
|
|
248
|
+
// THE DEPOSITOR'S FIELDS GO AS SEPARATE FACTORY ARGUMENTS, BUT THEY ARE IN THE SIGNED SET. Since quote protocol v10
|
|
249
|
+
// the factory signs them; the overwrite below must reproduce the signed values, or the digest check rejects the
|
|
250
|
+
// creation.
|
|
249
251
|
const quoteEnc = { ...quote, commitHalfLocker: terms.commitHalfLocker, edPointLocker: terms.edPointLocker, edViewPointLocker: terms.edViewPointLocker };
|
|
250
252
|
// QUOTE AND TERMS ARE ABOUT ONE THING: a divergence moves the escrow address and signs the wrong order.
|
|
251
253
|
// THE COUNTERPARTY'S SPEND HALF IS PART OF THAT ONE THING. The proof is checked against the counterparty side,
|
|
@@ -284,7 +286,6 @@ export async function fundOrder({ factory, locker, claimer, commitHalfLocker, co
|
|
|
284
286
|
}
|
|
285
287
|
const escrowState = escrowStateVerdict({ status: stateRead.status, error: stateRead.error, address: escrow });
|
|
286
288
|
if (escrowState.blocks) throw new Error("signature not started: " + escrowState.reason);
|
|
287
|
-
|
|
288
289
|
// 2) ONE TRANSACTION: creating the order TOGETHER with the deposit.
|
|
289
290
|
//
|
|
290
291
|
// It used to be two steps (createOrder, then lock) with two defects, both seen live: the wallet estimated the
|
|
@@ -312,6 +313,10 @@ export async function fundOrder({ factory, locker, claimer, commitHalfLocker, co
|
|
|
312
313
|
// WHICH FACTORY DOOR THE DEPOSIT GOES THROUGH. There is ONE path: createOrderAndFund, where the wallet that
|
|
313
314
|
// sends the transaction becomes the locker. The sender may not change the order terms - the quote signature
|
|
314
315
|
// covers THE SAME digest the escrow checks.
|
|
316
|
+
// THE PREDICTED ADDRESS IS WRITTEN TO THE CALLER'S RECORD BEFORE THE TRANSACTION. A receipt can take longer than
|
|
317
|
+
// the process that sent it; a record saved only after the wait leaves nothing to resume from, and the retry asks
|
|
318
|
+
// for a second deposit. The callback runs after every refusal and immediately before the send.
|
|
319
|
+
if (typeof onPredicted === "function") await onPredicted({ escrow, salt, terms });
|
|
315
320
|
const createData = encodeCreateOrderAndFund(quoteEnc);
|
|
316
321
|
const createHash = await send({
|
|
317
322
|
to: factory,
|
package/engine/evm/index.js
CHANGED
|
@@ -38,10 +38,8 @@ export * from "./permit.js";
|
|
|
38
38
|
// THE ASYNCHRONOUS ROUTE, EXECUTED - through the same engine seam as the decision that chooses it. The page reaches
|
|
39
39
|
// it through the bridge; the SDK carries the same module in its engine mirror (tools/build-sdk-engine.mjs).
|
|
40
40
|
export * from "./asyncRoute.js";
|
|
41
|
-
|
|
42
|
-
//
|
|
43
|
-
// so the page and the SDK carry one implementation each.
|
|
44
|
-
export * from "./asyncLegs.js";
|
|
41
|
+
// THE EXECUTOR IS THE PACKAGE'S PROVIDER LAYER (src/legs/cow.mjs and its siblings). The generated mirror that
|
|
42
|
+
// used to execute the route here carried a second submit/status path; it was removed with the order worker.
|
|
45
43
|
|
|
46
44
|
// currentProvider - the wallet provider goes OUT to the SDK core (evm-wallet.mjs) so signing uses the same
|
|
47
45
|
// key as the page. Screens do not need it; they read status()/isConnected().
|
package/engine/evm/session.js
CHANGED
|
@@ -109,7 +109,7 @@ async function loadLib() {
|
|
|
109
109
|
if (!lib) {
|
|
110
110
|
// A dynamic import of our own file: CSP script-src 'self' allows it,
|
|
111
111
|
// and the 2 MB bundle loads only when the user actually presses Connect.
|
|
112
|
-
lib = await import("../../assets/vendors/walletconnect/wc-browser.js?v=
|
|
112
|
+
lib = await import("../../assets/vendors/walletconnect/wc-browser.js?v=fb64f27c");
|
|
113
113
|
}
|
|
114
114
|
return lib;
|
|
115
115
|
}
|
|
@@ -654,11 +654,11 @@ export async function switchChain(chainSlugOrId) {
|
|
|
654
654
|
//
|
|
655
655
|
// Needed to learn the escrow address BEFORE it is created (factory.predict) and to read state
|
|
656
656
|
// (escrow.status) while polling. Requires no keys, asks the wallet nothing - only the provider.
|
|
657
|
-
export async function readContract({ to, data } = {}) {
|
|
657
|
+
export async function readContract({ to, data, block = "latest" } = {}) {
|
|
658
658
|
if (!provider) throw new Error("wallet not connected: nothing to read the contract with");
|
|
659
659
|
if (!to) throw new Error("no contract address given");
|
|
660
660
|
if (!data) throw new Error("no call data given");
|
|
661
|
-
const result = await provider.request({ method: "eth_call", params: [{ to, data }, "latest"] });
|
|
661
|
+
const result = await provider.request({ method: "eth_call", params: [{ to, data }, block || "latest"] });
|
|
662
662
|
if (typeof result !== "string" || !result.startsWith("0x")) {
|
|
663
663
|
throw new Error("unexpected answer to a contract call: " + String(result).slice(0, 60));
|
|
664
664
|
}
|