@ninsei-labs/sdk 0.60.0 → 0.62.0

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 (96) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +81 -18
  3. package/assets/vendors/MANIFEST.json +1 -1
  4. package/assets/vendors/sdk/sdk-browser.js +4 -4
  5. package/engine/atomic/dleq2.js +0 -4
  6. package/engine/atomic/halfenc.js +6 -10
  7. package/engine/atomic/halves.js +0 -4
  8. package/engine/atomic/order.js +0 -4
  9. package/engine/atomic/orderContext.js +0 -4
  10. package/engine/atomic/orderHalfCommit.js +0 -4
  11. package/engine/core/chainSource.js +0 -4
  12. package/engine/core/clock.js +0 -4
  13. package/engine/core/config.js +17 -42
  14. package/engine/core/finality.js +0 -4
  15. package/engine/core/format.js +1 -5
  16. package/engine/core/preSignGuards.js +0 -4
  17. package/engine/core/rfqSource.js +0 -3
  18. package/engine/core/sellWindow.js +1 -5
  19. package/engine/core/store.js +11 -7
  20. package/engine/core/swap.js +3 -7
  21. package/engine/evm/amounts.js +2 -6
  22. package/engine/evm/asyncRoute.js +3 -7
  23. package/engine/evm/auth.js +0 -4
  24. package/engine/evm/claimGas.js +2 -6
  25. package/engine/evm/dex.js +1 -5
  26. package/engine/evm/escrow.js +0 -4
  27. package/engine/evm/factory.js +1 -5
  28. package/engine/evm/fees.js +62 -4
  29. package/engine/evm/funding.js +73 -71
  30. package/engine/evm/gasReserve.js +10 -4
  31. package/engine/evm/index.js +7 -11
  32. package/engine/evm/injected.js +0 -4
  33. package/engine/evm/permit.js +0 -4
  34. package/engine/evm/prices.js +3 -4
  35. package/engine/evm/session.js +1 -5
  36. package/engine/mock/chain.js +0 -4
  37. package/engine/monero/address.js +4 -5
  38. package/engine/monero/arrivalRead.js +15 -2
  39. package/engine/monero/halvesSweep.js +0 -4
  40. package/engine/monero/keyImage.js +146 -0
  41. package/engine/monero/keys.js +1 -0
  42. package/engine/monero/mnemonic.js +2 -1
  43. package/engine/monero/node.js +1 -4
  44. package/engine/monero/scan.js +12 -2
  45. package/engine/monero/swapKeys.js +0 -4
  46. package/engine/monero/unlock.js +2 -6
  47. package/engine/monero/wallet.js +0 -3
  48. package/engine/recovery/recoveryFile.js +31 -89
  49. package/engine/recovery/restore.js +0 -4
  50. package/engine/sdk/bridge.js +24 -58
  51. package/engine/ui/dom.js +0 -4
  52. package/engine/ui/walletModal.js +0 -4
  53. package/index.d.ts +436 -42
  54. package/package.json +22 -3
  55. package/src/actions.mjs +72 -6
  56. package/src/addressGuard.mjs +16 -0
  57. package/src/codes.mjs +26 -9
  58. package/src/config.mjs +56 -3
  59. package/src/decline.mjs +23 -74
  60. package/src/engine.mjs +17 -26
  61. package/src/firmRequest.mjs +80 -0
  62. package/src/index.mjs +17 -28
  63. package/src/legs/cow.mjs +2 -2
  64. package/src/legs/evm.mjs +20 -12
  65. package/src/legs/kyber-spec.mjs +2 -2
  66. package/src/legs/kyber.mjs +1 -1
  67. package/src/legs/shape.mjs +1 -1
  68. package/src/legs/uniswaprelay.mjs +16 -4
  69. package/src/legs/uniswapx.mjs +29 -7
  70. package/src/levelsWalkSpec.mjs +1 -1
  71. package/src/lock.mjs +18 -37
  72. package/src/provider/envFile.mjs +3 -3
  73. package/src/provider/sign.mjs +3 -3
  74. package/src/public/evm.d.ts +84 -0
  75. package/src/public/evm.mjs +20 -0
  76. package/src/public/order.mjs +1 -1
  77. package/src/public/provider.mjs +1 -1
  78. package/src/public/recovery.d.ts +4 -3
  79. package/src/public/recovery.mjs +1 -1
  80. package/src/public/xmr.d.ts +2 -2
  81. package/src/quoteEip712Spec.mjs +4 -4
  82. package/src/quotes.mjs +52 -11
  83. package/src/recovery-key.mjs +7 -3
  84. package/src/relayClaim.mjs +17 -3
  85. package/src/relayFirmClient.mjs +4 -0
  86. package/src/relayQuote.mjs +1 -0
  87. package/src/sim.mjs +1 -1
  88. package/src/swap-flow.mjs +28 -41
  89. package/src/swaps.mjs +217 -123
  90. package/src/sweep-inputs.mjs +61 -0
  91. package/src/sweep-reconcile.mjs +71 -0
  92. package/src/sweep-restore.mjs +38 -9
  93. package/src/sweep.mjs +164 -10
  94. package/src/version.mjs +1 -1
  95. package/src/wallet.mjs +1 -1
  96. package/src/xmr-arrival.mjs +1053 -57
@@ -220,6 +220,7 @@ function checkOrderQuote(quote, terms, nowSec) {
220
220
  }
221
221
  if (fee !== (amount * BigInt(bps)) / 10000n) throw new SdkError("quote-refused", { step: "answer", why: "fee" });
222
222
  const validUntil = wireNumber(quote.validUntil, "quote.validUntil");
223
+ // A quote must expire before readyBy. After readyBy, its signed price must not authorize a new deposit.
223
224
  if (!Number.isSafeInteger(validUntil) || validUntil <= nowSec) {
224
225
  throw new SdkError("quote-expired", { step: "answer", validUntil: quote.validUntil });
225
226
  }
package/src/sim.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // SANDBOX: failure scenarios and time. Added on requirement 8 of issue #32 - the interface lays out failure
1
+ // SANDBOX: failure scenarios and time. Added on requirement 8 of - the interface lays out failure
2
2
  // screens without waiting for a live testnet.
3
3
  //
4
4
  // What is deliberately absent, and why: scenarios are applied by the ENGINE (`www/js/mock`, the scenario fields
package/src/swap-flow.mjs CHANGED
@@ -17,15 +17,12 @@
17
17
  // no such function - the payable constructor moves the money - and there is no envelope in the order. Nor
18
18
  // does the escrow have a separate funding function: an order is either born with money or not born at all.
19
19
  //
20
- // THE CRYPTOGRAPHY LIVES UNDER www/js/atomic/* (mirrored as ../engine/atomic/*) AND IS CALLED ONLY THROUGH
21
- // THE IN-PROCESS ORDER BUILDER. There is no crypto here - here is the order.
20
+ // THE CRYPTOGRAPHY LIVES UNDER engine/atomic/* AND IS CALLED ONLY THROUGH THE IN-PROCESS ORDER BUILDER.
21
+ // There is no crypto here - here is the order.
22
22
  //
23
- // WHY IT IS IN THE PACKAGE NOW. The page used to import this module straight from www/js/core/swap-flow.js; the
24
- // core (www/js, mirrored under ../engine/) was the second home of the deal flow. That is exactly the debt
25
- // issue #32 pays off: the deal engine is business logic, so it belongs to the package (@ninsei-labs/sdk), and
26
- // the page reaches it through ONE seam (www/js/sdk/bridge.js) by module name, not by pulling a file out of
27
- // www/js. The engine modules it needs are still read through the same verified mirror (./engine.mjs); what moved
28
- // is the RULES, not the crypto.
23
+ // WHY IT IS IN THE PACKAGE. The deal engine is business logic, so it belongs to the package (@ninsei-labs/sdk).
24
+ // The page reaches it through ONE seam (its bridge) by module name. The engine modules it needs are read through
25
+ //./engine.mjs; what lives here is the RULES, not the crypto.
29
26
  import { SdkError } from "./errors.mjs";
30
27
  import * as engine from "./engine.mjs";
31
28
  // THE HALF'S COMMITMENT, COMPUTED LOCALLY. The escrow checks the low 160 bits of `keccak256(x||y)` of `half*G`
@@ -33,7 +30,7 @@ import * as engine from "./engine.mjs";
33
30
  // the half ever leaving this process.
34
31
  import { halfCommitmentHex } from "../engine/atomic/orderHalfCommit.js";
35
32
  import { keccak256, secp256k1 } from "./primitives.mjs";
36
- // THE MAKER'S DECLINE (issue #40) - the escrow's `Declined` event and the neutral vocabulary the state is
33
+ // THE MAKER'S DECLINE - the escrow's `Declined` event and the neutral vocabulary the state is
37
34
  // reported in. It is its own module because TWO surfaces ask the same question (this flow and the swaps watch)
38
35
  // and a second implementation of the same event would be a second truth about it.
39
36
  import { declinedFromLogs, logReaderOf, blockBoundaryOf, DECLINED_TOPIC0 } from "./decline.mjs";
@@ -52,8 +49,8 @@ const {
52
49
  } = engine.escrow;
53
50
  // THE COUNTERPARTY GUARDS: whether a stand-in may be signed on this chain.
54
51
  const { liveOrderSlotsVerdict, counterpartyVerdict } = engine.guards;
55
- // THE ORDER CONTEXT, THE ORDER QUOTE AND THE SHARED CLAIM GAS LIMIT - all through the mirror, so the page and
56
- // the package never keep a second copy of these rules.
52
+ // THE ORDER CONTEXT, THE ORDER QUOTE AND THE SHARED CLAIM GAS LIMIT - all through the engine seam, so the page
53
+ // and the package never keep a second copy of these rules.
57
54
  const { orderContextString } = engine.orderContext;
58
55
  const { requestOrderQuote, checkOrderQuoteShape } = engine.rfqSource;
59
56
  const { CLAIM_GAS_LIMIT } = engine.claimGas;
@@ -66,7 +63,7 @@ export function escrowFactoryAddress(chainSlug) {
66
63
  return address;
67
64
  }
68
65
 
69
- // THE INTERFACE FEE CASHIER (#103): the address comes from the network registry, like the factory. The factory
66
+ // THE INTERFACE FEE CASHIER: the address comes from the network registry, like the factory. The factory
70
67
  // no longer creates the cashier - it arrives in the ORDER TERMS, and without it no quote under the order can be
71
68
  // assembled.
72
69
  export function escrowCashierAddress(chainSlug) {
@@ -94,43 +91,32 @@ export async function runAtomicOrder({
94
91
  const cashier = escrowCashierAddress(chainSlug);
95
92
  const chain = chainById(chainSlug || DEFAULT_CHAIN);
96
93
 
97
- // THE DEADLINES AND THE SALT ARE DECIDED HERE, BEFORE THE PROOF, AND NOWHERE ELSE.
94
+ // The deadlines and the salt are decided here, before the proof. The proof is bound to the order terms, which
95
+ // include both. Funding must not choose a different set later: the counterparty would reject the proof, while the
96
+ // local side would look healthy.
98
97
  //
99
- // This is the very reason the context stopped being a string from the caller. The proof is bound to the order
100
- // terms, and the terms include the deadlines and the salt. While the funding module decided them - that is,
101
- // AFTER the computation - the proof was bound to one set of deadlines while a different set went into the
102
- // contract: the check would fail at the counterparty, and on our own side everything would look healthy.
103
- // The salt here is the same in meaning as before: it determines the escrow address, so repeating an order must
104
- // come with the previous salt, otherwise a second escrow is created instead of the same one.
98
+ // The salt determines the escrow address. A repeated order must use the previous salt, or it creates a second
99
+ // escrow instead of the same one.
105
100
  const nowSec = Math.floor(Date.now() / 1000);
106
101
  const readyBy = nowSec + readyWindowSeconds;
107
- // t1 IS THE WINDOW AFTER readyBy, NOT "from now" (issue #88): the owner's claim window equals an hour AFTER the
108
- // readiness deadline. The flat formula "now + claimWindowSeconds" at a 4-hour readyBy gave t1 EARLIER than
109
- // readyBy, that is, an order created already closed (this is what check-atomic-flow caught).
102
+ // t1 is the window after readyBy, not "from now". The claim window starts after the readiness deadline.
110
103
  const t1 = readyBy + claimWindowSeconds;
111
104
  const salt = providedSalt || newSecret();
112
105
 
113
- // THE CONTEXT IS ASSEMBLED FROM THE ORDER FIELDS - by the same code as the provider node's and any verifier's.
114
- // The provider's half is derived from THESE SAME terms, so its proof will agree only here.
115
- // ONE ORDER OBJECT - ONE SOURCE OF FIELDS. Both the proof context and the quote request to the provider are
116
- // assembled from it. A second copy of these fields would diverge exactly where it costs the most: the provider
117
- // would derive its half from one set of terms while the proof was computed over another.
106
+ // The context is assembled from the order fields by the same rule a provider or verifier uses. The proof binds to
107
+ // these terms, so there is one order object for the proof and the quote request. A second copy would diverge where
108
+ // it costs the most.
118
109
  //
119
- // THERE IS NO CLAIMER HERE, AND THAT IS FUNDAMENTAL. The contract pays whoever is recorded as claimer, and at
120
- // claim time the half is revealed by whoever claims. So only the PROVIDER can name the claimer: it holds the
121
- // half and its address must receive the money. While the application substituted its own value, the provider
122
- // got an order with a foreign address and could not claim the ETH for it - the half stayed with it while the
123
- // money went to someone else. That is why the claimer comes in the quote, and the context is assembled AFTER
124
- // the quote.
110
+ // There is no claimer here. The contract pays the recorded claimer, and at claim time the half is revealed by
111
+ // whoever claims. Only the provider can name the claimer: it holds the half and must receive the money. The claimer
112
+ // therefore comes in the quote, and the context is assembled after the quote.
125
113
  const orderBase = {
126
114
  chainId: chain.chainId, factory, cashier, locker,
127
115
  amount: String(amountWei), readyBy: String(readyBy), t1: String(t1), salt,
128
116
  asset: "XMR", // what the user receives; not part of the context (it follows from the pair and network)
129
117
  };
130
- // THERE IS NO HASHLOCK AND ENVELOPE HERE ANY MORE. Under the scheme of doc 27 the order carries BOTH half
131
- // commitments and BOTH public ed25519 points, and the half is published by the settlement itself
132
- // (claim/refund). The secret that used to be placed in the recovery file is now MY HALF: it is exactly what is
133
- // needed to claim the XMR.
118
+ // The order carries both half commitments and both public ed25519 points. Settlement publishes the half. The secret
119
+ // in the recovery file is my half: exactly what is needed to claim the XMR.
134
120
  const say = (stage, detail) => onStage?.(stage, detail);
135
121
  {
136
122
  // THE QUOTE FIRST, THEN YOUR OWN SIDE. The order of steps is set by the fact that the claimer is named by the
@@ -170,6 +156,7 @@ export async function runAtomicOrder({
170
156
  onProgress: (step) => say("verify", step.phase === "sum"
171
157
  ? `summing commitments: ${step.done} of ${step.total}`
172
158
  : `verifying DLEQ proof: ${step.done} of ${step.total}`),
159
+ ...(typeof deps.yieldTo === "function" ? { yieldTo: deps.yieldTo } : {}),
173
160
  };
174
161
  const mine = await buildSide(order, chunkOptions);
175
162
  say("half", "side-ready");
@@ -369,7 +356,7 @@ export async function orderStatus(escrow, deps = {}) {
369
356
  }
370
357
 
371
358
  /**
372
- * THE ESCROW'S `Declined` EVENT - THE MAKER'S OWN CLOSING OF A FUNDED ORDER (issue #40).
359
+ * THE ESCROW'S `Declined` EVENT - THE MAKER'S OWN CLOSING OF A FUNDED ORDER.
373
360
  *
374
361
  * WHY THE EVENT AND NOT `status()`. Read on its own, `status()` cannot tell a decline from an ordinary refund:
375
362
  * both set `refunded` and send the whole balance back to the locker. The escrow keeps NO getter that names a
@@ -542,7 +529,7 @@ async function simulateAction(escrow, data, what, deps) {
542
529
  // A DRY RUN MUST NOT HAND THE HALF TO THE RPC. `eth_call` goes to a public endpoint by default, so simulating
543
530
  // `claim(half)` or `refund(half)` publishes the half to a third party while the escrow is still open and the ETH
544
531
  // has not moved. Whoever keeps that call holds both halves of the Monero key when the person declines the
545
- // transaction in the wallet or the send never lands (sdk#291).
532
+ // transaction in the wallet or the send never lands.
546
533
  //
547
534
  // The simulation added exactly one check, and it is arithmetic: the half's point commitment must equal the
548
535
  // order's. The contract compares the LOW 160 BITS of `keccak256(x||y)` of `half*G`
@@ -576,7 +563,7 @@ async function assertHalfMatchesCommit(escrow, half, slot, what, deps) {
576
563
  // lied here: the current contract has NO hashlock field. Instead it has two half commitments -
577
564
  // commitHalfClaimer and commitHalfLocker (:74-75), and claim compares the half against the first (:217).
578
565
  // In production this is the second half of the Monero spend key, so claiming and revealing the half are one
579
- // action: the half is published by whoever claims, and it is also obtained by the other side (HalfRevealed, :222).
566
+ // action: the half is published by whoever claims, and it is also obtained by the other side (HalfRevealed,:222).
580
567
  export async function claimOrder({ escrow, halfClaimer, expect, onStep, deps = {} }) {
581
568
  if (!escrow) throw new SdkError("bad-input", { field: "escrow" });
582
569
  if (!halfClaimer) throw new SdkError("bad-input", { field: "halfClaimer" });
@@ -591,7 +578,7 @@ export async function claimOrder({ escrow, halfClaimer, expect, onStep, deps = {
591
578
  // the order. No call carries the half until the send.
592
579
  await assertHalfMatchesCommit(escrow, halfClaimer, "commitHalfClaimer", "claim", deps);
593
580
  onStep?.("confirm-claim");
594
- // THE CLAIM GAS LIMIT COMES FROM THE SHARED MODULE, NOT A LITERAL HERE (issue #127): the node lays exactly
581
+ // THE CLAIM GAS LIMIT COMES FROM THE SHARED MODULE, NOT A LITERAL HERE: the node lays exactly
595
582
  // this limit into the gas gift. It is set explicitly and with a margin: claim has the same trap as order
596
583
  // creation - the wallet estimates gas over a state that no longer exists on the chain (the same reason is
597
584
  // named in the contract, NinseiEscrow.sol:137-138), and the call costs tens of thousands of gas.