@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.
- package/CHANGELOG.md +57 -0
- package/README.md +81 -18
- package/assets/vendors/MANIFEST.json +1 -1
- package/assets/vendors/sdk/sdk-browser.js +4 -4
- package/engine/atomic/dleq2.js +0 -4
- package/engine/atomic/halfenc.js +6 -10
- package/engine/atomic/halves.js +0 -4
- package/engine/atomic/order.js +0 -4
- package/engine/atomic/orderContext.js +0 -4
- package/engine/atomic/orderHalfCommit.js +0 -4
- package/engine/core/chainSource.js +0 -4
- package/engine/core/clock.js +0 -4
- package/engine/core/config.js +17 -42
- package/engine/core/finality.js +0 -4
- package/engine/core/format.js +1 -5
- package/engine/core/preSignGuards.js +0 -4
- package/engine/core/rfqSource.js +0 -3
- package/engine/core/sellWindow.js +1 -5
- package/engine/core/store.js +11 -7
- package/engine/core/swap.js +3 -7
- package/engine/evm/amounts.js +2 -6
- package/engine/evm/asyncRoute.js +3 -7
- package/engine/evm/auth.js +0 -4
- package/engine/evm/claimGas.js +2 -6
- package/engine/evm/dex.js +1 -5
- package/engine/evm/escrow.js +0 -4
- package/engine/evm/factory.js +1 -5
- package/engine/evm/fees.js +62 -4
- package/engine/evm/funding.js +73 -71
- package/engine/evm/gasReserve.js +10 -4
- package/engine/evm/index.js +7 -11
- package/engine/evm/injected.js +0 -4
- package/engine/evm/permit.js +0 -4
- package/engine/evm/prices.js +3 -4
- package/engine/evm/session.js +1 -5
- package/engine/mock/chain.js +0 -4
- package/engine/monero/address.js +4 -5
- package/engine/monero/arrivalRead.js +15 -2
- package/engine/monero/halvesSweep.js +0 -4
- package/engine/monero/keyImage.js +146 -0
- package/engine/monero/keys.js +1 -0
- package/engine/monero/mnemonic.js +2 -1
- package/engine/monero/node.js +1 -4
- package/engine/monero/scan.js +12 -2
- package/engine/monero/swapKeys.js +0 -4
- package/engine/monero/unlock.js +2 -6
- package/engine/monero/wallet.js +0 -3
- package/engine/recovery/recoveryFile.js +31 -89
- package/engine/recovery/restore.js +0 -4
- package/engine/sdk/bridge.js +24 -58
- package/engine/ui/dom.js +0 -4
- package/engine/ui/walletModal.js +0 -4
- package/index.d.ts +436 -42
- package/package.json +22 -3
- package/src/actions.mjs +72 -6
- package/src/addressGuard.mjs +16 -0
- package/src/codes.mjs +26 -9
- package/src/config.mjs +56 -3
- package/src/decline.mjs +23 -74
- package/src/engine.mjs +17 -26
- package/src/firmRequest.mjs +80 -0
- package/src/index.mjs +17 -28
- package/src/legs/cow.mjs +2 -2
- package/src/legs/evm.mjs +20 -12
- package/src/legs/kyber-spec.mjs +2 -2
- package/src/legs/kyber.mjs +1 -1
- package/src/legs/shape.mjs +1 -1
- package/src/legs/uniswaprelay.mjs +16 -4
- package/src/legs/uniswapx.mjs +29 -7
- package/src/levelsWalkSpec.mjs +1 -1
- package/src/lock.mjs +18 -37
- package/src/provider/envFile.mjs +3 -3
- package/src/provider/sign.mjs +3 -3
- package/src/public/evm.d.ts +84 -0
- package/src/public/evm.mjs +20 -0
- package/src/public/order.mjs +1 -1
- package/src/public/provider.mjs +1 -1
- package/src/public/recovery.d.ts +4 -3
- package/src/public/recovery.mjs +1 -1
- package/src/public/xmr.d.ts +2 -2
- package/src/quoteEip712Spec.mjs +4 -4
- package/src/quotes.mjs +52 -11
- package/src/recovery-key.mjs +7 -3
- package/src/relayClaim.mjs +17 -3
- package/src/relayFirmClient.mjs +4 -0
- package/src/relayQuote.mjs +1 -0
- package/src/sim.mjs +1 -1
- package/src/swap-flow.mjs +28 -41
- package/src/swaps.mjs +217 -123
- package/src/sweep-inputs.mjs +61 -0
- package/src/sweep-reconcile.mjs +71 -0
- package/src/sweep-restore.mjs +38 -9
- package/src/sweep.mjs +164 -10
- package/src/version.mjs +1 -1
- package/src/wallet.mjs +1 -1
- package/src/xmr-arrival.mjs +1053 -57
package/engine/atomic/dleq2.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/atomic/dleq2.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// DLEQ ACROSS CURVES: a proof that one scalar stands behind points on secp256k1 and ed25519.
|
|
6
2
|
// Ported from go-dleq (prove.go, verify.go) to @noble/curves. The upstream project is
|
|
7
3
|
// github.com/athanorlabs/go-dleq at d6fd7c03e213683cbb286d4d5bbba1c7e7e1cfe1 and is licensed LGPL-3.0-only; see
|
package/engine/atomic/halfenc.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/atomic/halfenc.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Transferring a key half: an encrypted half fit for publishing on chain.
|
|
6
2
|
// WHAT THIS IS. A half x is sent so that: third parties cannot read it (the mask comes from an ECDH shared secret
|
|
7
3
|
// with the recipient); THE RECIPIENT can check that the ciphertext holds the half matching the published point X by
|
|
@@ -115,12 +111,12 @@ export function createHalfEnc({ secp256k1, ed25519, keccak256, randomBytes }) {
|
|
|
115
111
|
// calldata at lock(). The format is fixed, because the commitment depends on it: any other layout changes the
|
|
116
112
|
// hash and the lock is rejected.
|
|
117
113
|
//
|
|
118
|
-
// 0
|
|
119
|
-
// 1..33
|
|
120
|
-
// 34..66
|
|
121
|
-
// 67..98
|
|
122
|
-
// 99
|
|
123
|
-
// 100
|
|
114
|
+
// 0: format version (1)
|
|
115
|
+
// 1..33: encryption recipient public key (33 bytes, compressed secp256k1)
|
|
116
|
+
// 34..66: sender ephemeral point (33 bytes, compressed)
|
|
117
|
+
// 67..98: masked scalar (32 bytes, big-endian)
|
|
118
|
+
// 99: context length in bytes (0..255)
|
|
119
|
+
// 100..: order context in UTF-8
|
|
124
120
|
//
|
|
125
121
|
// Total 100 + context length bytes. JSON is deliberately not used: key order and number formatting would give
|
|
126
122
|
// different bytes for the same content, and the commitment requires uniqueness.
|
package/engine/atomic/halves.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/atomic/halves.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Half of a Monero key for the XMR <-> EVM atomic swap.
|
|
6
2
|
// The Monero spend key is the sum of two halves: ks = ks_a + ks_b (mod l); spending needs both.
|
|
7
3
|
// One half must work on edwards25519 (Monero) and secp256k1 (EVM), so the scalar stays below the smaller
|
package/engine/atomic/order.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/atomic/order.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// ASSEMBLING ONE ORDER SIDE of an atomic swap: a key half, points, a proof, the encrypted envelope and the
|
|
6
2
|
// contract commitment.
|
|
7
3
|
//
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/atomic/orderContext.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// CANONICAL ORDER CONTEXT - what the DLEQ proof binds to.
|
|
6
2
|
// It is built FROM THE ORDER FIELDS and identically everywhere (app, provider node, any verifier), so a
|
|
7
3
|
// proof can be checked from the on-chain order alone (docs 27, section 11).
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/atomic/orderHalfCommit.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// COMMITMENT TO A HALF - one formula for all sides.
|
|
6
2
|
//
|
|
7
3
|
// WHAT THE CHAIN COMPUTES. `keccak256(x || y)` of the secp256k1 POINT of the half `half*G` - that is, the ADDRESS of that point.
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/chainSource.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// DATA SOURCE FOR THE MONERO LEG OF THE SWAP: the demo simulator or the real backend.
|
|
6
2
|
// TWO LEGS, DIFFERENT READINESS. The EVM leg (escrow, claim, refund) is a SIMULATION (no contract yet): every
|
|
7
3
|
// reply is marked mock: true and the UI must show it. The Monero leg (has XMR arrived, how many confirmations)
|
package/engine/core/clock.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/clock.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Demo sim-clock.
|
|
6
2
|
//
|
|
7
3
|
// Idea: real time is compressed by the speed multiplier. speed=60 means one
|
package/engine/core/config.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/config.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// VM-neutral deployment pins generated from deployments/<vm>/<network>.json. No address table is copied below.
|
|
6
2
|
import { DEPLOYMENTS, DEPLOYMENT_CHAINS, DEPLOYMENT_VMS, FACTORY_HISTORY, deploymentFor } from "./deployments.generated.js";
|
|
7
3
|
|
|
@@ -113,14 +109,11 @@ export const DEX_PRICE_GATE = {
|
|
|
113
109
|
|
|
114
110
|
// PRE-SIGNING GUARD THRESHOLDS - ALSO NUMBERS IN DATA, NOT IN LOGIC (www/js/core/preSignGuards.js).
|
|
115
111
|
// Only ORDER DEADLINES live here; the price and coverage thresholds are above, at the DEX leg.
|
|
116
|
-
// minReadyLeadSec - how far the readiness
|
|
117
|
-
//
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
// strictly below the contract window.
|
|
122
|
-
// minClaimWindowSec - how far the settlement deadline (t1) must stand from the readiness deadline: the window in
|
|
123
|
-
// which the claimer claims the ETH cannot be "a few seconds". 10 minutes against the usual hour.
|
|
112
|
+
// minReadyLeadSec - how far the readiness deadline must be ahead of "now" before signing. The threshold is the
|
|
113
|
+
// soft sell window (600 s): how long the provider promises to hold the on-screen price. It
|
|
114
|
+
// must stay below the contract window (1200 s).
|
|
115
|
+
// minClaimWindowSec - how far the settlement deadline (t1) must stand from the readiness deadline. 10 minutes
|
|
116
|
+
// against the usual hour.
|
|
124
117
|
export const SIGNING_GUARDS = {
|
|
125
118
|
minReadyLeadSec: 600,
|
|
126
119
|
minClaimWindowSec: 600,
|
|
@@ -383,16 +376,10 @@ export const CHAINS = [
|
|
|
383
376
|
// (2026-09-23). IMPORTANT ABOUT THIS NETWORK: pools exist but liquidity is unusable. The 0.01% tier serves NO
|
|
384
377
|
// size ("pool cannot serve this size"), 0.05% gives ZERO output at any size, and 0.3% answers at a price that
|
|
385
378
|
// diverges from the Chainlink reference.
|
|
386
|
-
//
|
|
387
|
-
//
|
|
388
|
-
//
|
|
389
|
-
//
|
|
390
|
-
// than the reference. The method is THE SAME CODE AS IN THE INTERFACE (www/js/evm/dex.js: priceGateVerdict,
|
|
391
|
-
// reference from www/js/evm/prices.js) and is rerunnable in one command.
|
|
392
|
-
// (rerun the DEX quote check against arbitrum-sepolia)
|
|
393
|
-
// THE NUMBER DEPENDS ON SIZE (same block, same scale): 1 USDC = 77.5%, 10 USDC = 76.6%, 100 USDC = 67.6%,
|
|
394
|
-
// 1000 USDC = -16.0%. So ONE number stands here WITH A NAMED SIZE. The tool also prints a "how many times
|
|
395
|
-
// cheaper" observation (+208.6% for 100 USDC, i.e. the same 3.09x) - a RECOUNT of the same figure, not a second measurement.
|
|
379
|
+
// One measured number, with a named size and block. Measurement 2026-09-24, block 312238363, 100 USDC: the
|
|
380
|
+
// quote gave 0.116508 WETH, an implied native price of $858.31 against $2650.42 from Chainlink. That is a 67.6%
|
|
381
|
+
// divergence against a 2% threshold. The result depends on size: 1 USDC = 77.5%, 10 USDC = 76.6%, 100 USDC =
|
|
382
|
+
// 67.6%, 1000 USDC = -16.0%. Keep the size with the number.
|
|
396
383
|
uniswap: {
|
|
397
384
|
factory: "0x248ab79bbb9bc29bb72f7cd42f17e054fc40188e",
|
|
398
385
|
quoter: "0x2779a0CC1c3e0E44D2542EC3e79e3864Ae93Ef0B",
|
|
@@ -454,26 +441,14 @@ export const CHAINS = [
|
|
|
454
441
|
explorerName: "",
|
|
455
442
|
gasUsd: 0.0001,
|
|
456
443
|
routeType: "atomic",
|
|
457
|
-
//
|
|
458
|
-
//
|
|
459
|
-
// on
|
|
460
|
-
//
|
|
461
|
-
//
|
|
462
|
-
//
|
|
463
|
-
//
|
|
464
|
-
//
|
|
465
|
-
// the cashier with. The registry is the third CREATE (DEPLOY_REGISTRY=true), 4557 bytes. PREVIOUS record:
|
|
466
|
-
// factory 0x5fbdb2315678afecb367f032d93f642f64180aa3 and cashier 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512,
|
|
467
|
-
// whose code did not match the canonical cashier - the escrow would have rejected such an order.
|
|
468
|
-
// THE ESCROW IMPLEMENTATION CODE HASH: what executes each order's clone, which the interface must check itself
|
|
469
|
-
// rather than trust the factory. The value is keccak256 of NinseiEscrow's runtime code from OUR OWN build (forge
|
|
470
|
-
// inspect NinseiEscrow deployedBytecode --root mvp/contracts and keccak - matched keccak256(type(NinseiEscrow)
|
|
471
|
-
// .runtimeCode) that the factory writes, and the out/ artifact). The implementation has NO immutables, so this is
|
|
472
|
-
// a PROTOCOL CONSTANT - the same hash on any network with that build. It sits per network, not as one global
|
|
473
|
-
// number, because the policy may be absent: a network with an OLD factory (no implementation()) must stay
|
|
474
|
-
// WITHOUT a policy, else reading the pair fails and live quotes there stall. That is the case on
|
|
475
|
-
// arbitrum-sepolia: its factory does not know the build (both getters revert), so it has NO field; it is enabled
|
|
476
|
-
// in the same pass as the new deploy.
|
|
444
|
+
// The factory deployed into the local chain. CREATE derives its address from the sender and its transaction
|
|
445
|
+
// number, so a fresh chain gives the same address while a long-running chain differs. These values were measured
|
|
446
|
+
// on chain after a redeploy for the quote format with xmrAmount: anvil account 0, nonce 60, block 74, 13188
|
|
447
|
+
// bytes of code (keccak 0x9bffb62d50eef21bcfd4b59e1c3d6fb7cbb3e303257edb2f5ba9dc65b3e8719c, matched to the
|
|
448
|
+
// tree build). The cashier is the second CREATE (573 bytes, keccak
|
|
449
|
+
// 0xcca1d8bc53d1d1c98901570800e093d0933f84637c6cdc727153b3ef8d8c3c36, the hash the escrow checks).
|
|
450
|
+
// The escrow implementation code hash is keccak256 of NinseiEscrow runtime code from this build. It is a
|
|
451
|
+
// protocol constant for this build, kept per network because a network with an old factory has no policy.
|
|
477
452
|
escrow: { address: "0xcbEAF3BDe82155F56486Fb5a1072cb8baAf547cc", mode: "live", cashier: "0x1429859428C0aBc9C2C47C8Ee9FBaf82cFA0F20f", implementationCodeHash: "0xfd1ef12c1d96d3ee7060bd5901e06108723783403fad00c9630dcdeb6193674b" },
|
|
478
453
|
// There are no tokens in the sandbox: payment is native. An empty list is a fact about the sandbox, not an oversight.
|
|
479
454
|
tokens: [],
|
package/engine/core/finality.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/finality.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// STATE MATURITY PER NETWORK: at which level the chain treats a record as final.
|
|
6
2
|
// One value for all networks is wrong: on base `safe` is ~25 s, on arbitrum ~13 min (measured on live nodes).
|
|
7
3
|
// On bsc, monad and hyperevm the tags are FORMAL: they answer but lag 0-1 block, so reading by `finalized`
|
package/engine/core/format.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/format.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Formatting of numbers, time and addresses.
|
|
6
2
|
|
|
7
3
|
const nf = (min, max) => new Intl.NumberFormat("en-US", { minimumFractionDigits: min, maximumFractionDigits: max });
|
|
@@ -156,7 +152,7 @@ export function shortId() {
|
|
|
156
152
|
// unnoticed. It used to be named isValidMoneroAddress - and that name was a trap: a function called
|
|
157
153
|
// "address check" that lets a typo through ends up on the money path sooner or later.
|
|
158
154
|
//
|
|
159
|
-
// THE REAL CHECK IS checkAddress() from
|
|
155
|
+
// THE REAL CHECK IS checkAddress() from../monero/address.js: format + base58 decode + keccak256 checksum
|
|
160
156
|
// + network match. The form calls that one. This one only screens out obvious garbage before the heavy decode.
|
|
161
157
|
export function looksLikeMoneroAddress(addr) {
|
|
162
158
|
const a = String(addr || "").trim();
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/preSignGuards.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// PRE-SIGNING GUARDS: AN INADMISSIBLE DEAL DOES NOT START.
|
|
6
2
|
//
|
|
7
3
|
// WHY A SEPARATE MODULE. The guards used to live apart: one in the route registry (the DEX leg's coverage and
|
package/engine/core/rfqSource.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// THE ORDER-QUOTE MIRROR OF www/js/core/rfqSource.js. The SDK copy drops the indicative quote book that the page
|
|
2
|
-
// still uses: the bridge helpers it called are gone. Keep the order-quote path in sync with the source.
|
|
3
|
-
|
|
4
1
|
// THE ORDER QUOTE FOR THE EXECUTOR FALLBACK. This module no longer carries the indicative quote book: the SDK has
|
|
5
2
|
// no book path, and the bridge helpers it called (`sdkBook`, `sdkBookSnapshot`) are gone. What remains is the
|
|
6
3
|
// order quote the executor asks for when it has to fall back to the provider, and its shape check.
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/sellWindow.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// XMR SELL WINDOW: ONE RULE FOR TWO SIDES, NOT TWO NUMBERS.
|
|
6
2
|
//
|
|
7
3
|
// WHY A SEPARATE MODULE. The XMR sell window (a person gives XMR, receives the network native coin) has two
|
|
@@ -19,7 +15,7 @@
|
|
|
19
15
|
// WHY A FUNCTION, NOT TWO NUMBERS IN TWO PLACES. This rule is decided by the NODE (rfq/reverseArrival.mjs -
|
|
20
16
|
// the mark, the alarm, the early refund) and shown by the PAGE (www/js/core/swap.js - the on-screen countdown).
|
|
21
17
|
// A second edition would diverge from the first silently: the person would see one window while the node lived by
|
|
22
|
-
// another. The rfq -> www import direction is already accepted (precedent: rfq/reverseGas.mjs imports
|
|
18
|
+
// another. The rfq -> www import direction is already accepted (precedent: rfq/reverseGas.mjs imports../www/js/evm/claimGas.js).
|
|
23
19
|
export const SELL_WINDOW = {
|
|
24
20
|
softSec: 600, // "ten minutes": the provider's on-screen promise
|
|
25
21
|
hardSec: 1200, // contract window: 10 Monero confirmations, the maximum that goes into termsHash
|
package/engine/core/store.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/store.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Demo state storage.
|
|
6
2
|
//
|
|
7
3
|
// Why it exists at all: litepaper §5.1.7 - "the tab can be closed, the browser will restore the state". The swap
|
|
@@ -64,6 +60,14 @@ const storageFailure = (step, cause) => {
|
|
|
64
60
|
return error;
|
|
65
61
|
};
|
|
66
62
|
|
|
63
|
+
// THE DECLARED STORE IS SYNCHRONOUS. A Promise returned from one of its three methods is not a value the store
|
|
64
|
+
// can read or write; waiting for it would make the caller's save happen after the swap state was decided. Refuse it
|
|
65
|
+
// at the seam instead of letting JSON.parse turn it into a misleading parse error.
|
|
66
|
+
const ensureSync = (value, step) => {
|
|
67
|
+
if (value && typeof value.then === "function") throw storageFailure(step);
|
|
68
|
+
return value;
|
|
69
|
+
};
|
|
70
|
+
|
|
67
71
|
/** Swap the storage for setup and tests. It also releases the exclusive binding, so a later client may bind. */
|
|
68
72
|
export function setStorage(next) {
|
|
69
73
|
checkAdapter(next);
|
|
@@ -90,7 +94,7 @@ export const storage = () => adapter;
|
|
|
90
94
|
function loadStateFrom(source) {
|
|
91
95
|
let raw;
|
|
92
96
|
try {
|
|
93
|
-
raw = source.get(APP.storageKey);
|
|
97
|
+
raw = ensureSync(source.get(APP.storageKey), "read");
|
|
94
98
|
} catch (error) {
|
|
95
99
|
throw storageFailure("read", error);
|
|
96
100
|
}
|
|
@@ -124,7 +128,7 @@ function saveStateTo(source, state) {
|
|
|
124
128
|
throw storageFailure("serialize", error);
|
|
125
129
|
}
|
|
126
130
|
try {
|
|
127
|
-
source.set(APP.storageKey, text);
|
|
131
|
+
ensureSync(source.set(APP.storageKey, text), "write");
|
|
128
132
|
} catch (error) {
|
|
129
133
|
throw storageFailure("write", error);
|
|
130
134
|
}
|
|
@@ -157,7 +161,7 @@ export function update(mutator) {
|
|
|
157
161
|
|
|
158
162
|
export function reset() {
|
|
159
163
|
try {
|
|
160
|
-
adapter.remove(APP.storageKey);
|
|
164
|
+
ensureSync(adapter.remove(APP.storageKey), "remove");
|
|
161
165
|
} catch (error) {
|
|
162
166
|
throw storageFailure("remove", error);
|
|
163
167
|
}
|
package/engine/core/swap.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/core/swap.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// The demo core: the deal state over time.
|
|
6
2
|
//
|
|
7
3
|
// PRINCIPLE: the state is NOT kept as a "current step". Timestamps and events are kept, and the phase is derived
|
|
@@ -125,7 +121,7 @@ export function createSwap(input, store = null) {
|
|
|
125
121
|
return swap;
|
|
126
122
|
}
|
|
127
123
|
|
|
128
|
-
// REVERSE-FLOW DEAL RECORD
|
|
124
|
+
// REVERSE-FLOW DEAL RECORD. The sell screen used to stop at the request, and there was
|
|
129
125
|
// nowhere to show a refund: a reverse deal had no record. It is created here - from exactly what the screen
|
|
130
126
|
// already has (the node ticket plus the computed halves) - and lands in the same store as the buy flow, so the
|
|
131
127
|
// deal card, the recovery file and the XMR sweep all work off it.
|
|
@@ -500,7 +496,7 @@ export function derive(swap, now = nowReal()) {
|
|
|
500
496
|
if (swap.settlement) {
|
|
501
497
|
phase = swap.settlement.phase;
|
|
502
498
|
phaseLabel = Object.values(TERMINAL).find((x) => x.phase === swap.settlement.phase)?.label || "Settled";
|
|
503
|
-
// SWEEPING OUR OWN XMR AFTER A REFUND
|
|
499
|
+
// SWEEPING OUR OWN XMR AFTER A REFUND. On the reverse flow a refund leaves the
|
|
504
500
|
// user's coins on the swap's one-time address: the claimer's ETH came back, and nobody moved the XMR. They can
|
|
505
501
|
// only be taken by assembling the spend key from OUR half and the OTHER half that the chain revealed on
|
|
506
502
|
// settlement. While the revealed half is missing we do NOT grant the right - can.sweep stays false precisely
|
|
@@ -741,7 +737,7 @@ export function derive(swap, now = nowReal()) {
|
|
|
741
737
|
},
|
|
742
738
|
// What is real now and what is a simulation. The UI must show this, not guess.
|
|
743
739
|
// The EVM leg is simulated if and only if the deal's network is not switched to live. It used to stand as a
|
|
744
|
-
// hard true, and on a LIVE deal with a real escrow the screen wrote "EVM leg
|
|
740
|
+
// hard true, and on a LIVE deal with a real escrow the screen wrote "EVM leg... is still simulated" - i.e.
|
|
745
741
|
// lied, although the order existed in the contract. The mark comes from the network settings, not from a
|
|
746
742
|
// field in the deal: the field is set at creation, and deals created earlier still have "mock" there.
|
|
747
743
|
simulated: { evm: !isLiveNetwork(swap), xmr: !xmr.live },
|
package/engine/evm/amounts.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/amounts.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Recalculating balances from the contract raw units into human-readable ones.
|
|
6
2
|
//
|
|
7
3
|
// A separate module, because this is money maths and it must be checked without a browser:
|
|
@@ -29,7 +25,7 @@ export function trimTrailingZeros(text) {
|
|
|
29
25
|
// Amount for display and for the MAX button: truncation (not rounding) and at most 6 decimals.
|
|
30
26
|
//
|
|
31
27
|
// Why truncation: MAX must not insert an amount larger than the real balance - else the form
|
|
32
|
-
// rightly answers "Not enough
|
|
28
|
+
// rightly answers "Not enough..." and the MAX button leads into a dead end. Rounding up did
|
|
33
29
|
// exactly that: 0.0399187 -> 0.039919 > balance.
|
|
34
30
|
// Why 6 decimals: human precision, and exactly what the balance hint shows.
|
|
35
31
|
// Returns a NUMBER (for comparisons); the string for the input field is given by amountInputValue below.
|
|
@@ -50,7 +46,7 @@ export function floorAmount(value, decimals = 6) {
|
|
|
50
46
|
// and a dot - "1e-7" would turn into "17" (caught by a check, not by a user).
|
|
51
47
|
export function amountInputValue(value, decimals = 6) {
|
|
52
48
|
// Truncate first, then format: toFixed ROUNDS itself, and without truncation you got
|
|
53
|
-
// 0.039919 - that is, more than the balance, and MAX hit "Not enough
|
|
49
|
+
// 0.039919 - that is, more than the balance, and MAX hit "Not enough...".
|
|
54
50
|
const cut = floorAmount(value, decimals);
|
|
55
51
|
if (!Number.isFinite(cut) || cut <= 0) return "0";
|
|
56
52
|
// For dust we print all digits: else toFixed would round to the first non-zero digit.
|
package/engine/evm/asyncRoute.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/asyncRoute.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// ASYNCHRONOUS ROUTE PROVIDERS BY NETWORK - "IS THERE A PATH FOR A PERSON WITH NO OWN NATIVE COIN".
|
|
6
2
|
//
|
|
7
3
|
// WHY THIS MODULE EXISTS. The declared routes in the registry (www/js/core/config.js: DEX_ROUTES) are ROUTER
|
|
@@ -131,11 +127,11 @@ const knownChainsFor = (providerId) => asyncRouteChains(providerId || asyncRoute
|
|
|
131
127
|
/**
|
|
132
128
|
* WHICH WAY AN ASYNCHRONOUS NEED GOES FOR A NETWORK - as a VALUE, with a NAMED refusal.
|
|
133
129
|
*
|
|
134
|
-
* { ok: true, kind: "single"
|
|
135
|
-
* { ok: true, kind: "composition", legs: [liquidity, native]
|
|
130
|
+
* { ok: true, kind: "single",...route, need } - one provider carries the need all the way;
|
|
131
|
+
* { ok: true, kind: "composition", legs: [liquidity, native],... } - no single provider carries a NATIVE need,
|
|
136
132
|
* but a liquidity (wrapped) leg and a
|
|
137
133
|
* native-capable leg both serve the network;
|
|
138
|
-
* { ok: false, reason: "no-async-provider" | "no-native-async-provider"
|
|
134
|
+
* { ok: false, reason: "no-async-provider" | "no-native-async-provider",... } - a named refusal.
|
|
139
135
|
*
|
|
140
136
|
* The rule that matters: a provider that does not settle what the person needs is NEVER chosen as the sole provider
|
|
141
137
|
* for that need (a route that ends in WETH must not pass as native). It may only be a leg of a composition, and a
|
package/engine/evm/auth.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/auth.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// WALLET-SIGNATURE LOGIN AND THE SERVER-SIDE SWAP LIST.
|
|
6
2
|
// Why: while swap records live only in localStorage, "my list" is tied to the browser. A server-side list
|
|
7
3
|
// fixes that; binding it to the wallet address keeps other people's swaps hidden.
|
package/engine/evm/claimGas.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/claimGas.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// CLAIM GAS FOR THE XMR SELL (claim): ONE SOURCE OF TRUTH FOR THE CLIENT AND THE NODE.
|
|
6
2
|
// In a reverse swap the person gives XMR and claims ETH via `claim` on the settlement chain. They have no ETH,
|
|
7
3
|
// so the node sends the claim gas. The gas rules used to live in TWO places and the numbers differed ~100x, so
|
|
8
4
|
// a person with one gift could not send the claim. Now this module computes both numbers.
|
|
9
|
-
// WHY ONE MODULE
|
|
10
|
-
//
|
|
5
|
+
// WHY ONE MODULE: the formula must be ONE. The SDK owns the EVM engine, and the node imports it from the package;
|
|
6
|
+
// the browser and the node read the same implementation.
|
|
11
7
|
// WHAT IS ONE HERE: the claim gas limit, the fee rule (2 x baseFee + tip) and the GIFT SIZE - the same claim
|
|
12
8
|
// cost plus an EXPLICIT margin for base-fee growth between ticket and claim.
|
|
13
9
|
|
package/engine/evm/dex.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/dex.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// THE DEX LEG: a Uniswap v3 quote read from the chain. READ ONLY: eth_call, not one transaction.
|
|
6
2
|
//
|
|
7
3
|
// WHAT IS REPLACED HERE. The price breakdown used to hold a mock: "Uniswap v3 USDC/WETH 0.05%" and the spread
|
|
@@ -614,7 +610,7 @@ export function dexLegVerdict(leg, requiredOutWei = null) {
|
|
|
614
610
|
//
|
|
615
611
|
// THERE ARE NO HUBS HERE ANY MORE: a detour via USDC/USDT is also a declared route, not a runtime search.
|
|
616
612
|
//
|
|
617
|
-
// Returns EITHER { ok: true
|
|
613
|
+
// Returns EITHER { ok: true,... } with numbers from the chain, OR { ok: false, why } in words. Numbers and "why
|
|
618
614
|
// not" are not returned together: showing a person both means asking him to guess which of the two answers to
|
|
619
615
|
// believe.
|
|
620
616
|
// ---------------------------------------------------------------------------
|
package/engine/evm/escrow.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/escrow.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Calls to the NinseiSwap escrow contract (one instance = one order).
|
|
6
2
|
//
|
|
7
3
|
// There is no EVM library here: the escrow has three state-moving functions (markReady, claim, refund) and a few
|
package/engine/evm/factory.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/factory.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// Calls to the NinseiSwap escrow factory contract.
|
|
6
2
|
//
|
|
7
3
|
// There are no EVM libraries in the project and we are not adding any. BOTH FACTORY FUNCTIONS NOW TAKE THE
|
|
@@ -22,7 +18,7 @@ export const FACTORY_METHODS = {
|
|
|
22
18
|
};
|
|
23
19
|
|
|
24
20
|
// The OrderCreated event topic. It grew the registry address, the provider and the signing key, and now carries
|
|
25
|
-
// `xmrAmount` in bulk
|
|
21
|
+
// `xmrAmount` in bulk:
|
|
26
22
|
// cast keccak "OrderCreated(address,address,address,bytes32,bytes32,bytes32,bytes32,bytes32,uint256,uint256,uint64,uint64,bytes32,address,address,address)"
|
|
27
23
|
// The topic lives in TWO places and both must match; a check pins them.
|
|
28
24
|
export const ORDER_CREATED_TOPIC =
|
package/engine/evm/fees.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// GENERATED FILE - a byte-for-byte copy of the engine module www/js/evm/fees.js for the SDK package.
|
|
2
|
-
// Edit the source under www/js and regenerate the mirror; a drift guard reddens on any difference.
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
4
|
-
|
|
5
1
|
// The NinseiSwap fee: PART OF THE SIGNED QUOTE (OrderQuote v7), not a contract setting.
|
|
6
2
|
//
|
|
7
3
|
// WHERE THE RATE LIVES. Before, it and the recipient were set by the FACTORY (or router) in immutable fields, and
|
|
@@ -34,6 +30,68 @@ export function feeWeiFor(amountWei, feeBps) {
|
|
|
34
30
|
return (amount * bps) / 10000n; // DOWN, as in Solidity: amount * feeBps / 10000
|
|
35
31
|
}
|
|
36
32
|
|
|
33
|
+
// THE INVERSE OF A BUY'S TOTAL. A buy pays `p + floor(p*bps/10000)` and the person names the total; this returns the
|
|
34
|
+
// largest principal that still fits. The inequality is solved exactly, so a total that does not divide evenly keeps
|
|
35
|
+
// the rounding down of the fee formula instead of gaining a wei.
|
|
36
|
+
export function principalForTotal(totalWei, feeBps) {
|
|
37
|
+
const total = BigInt(totalWei);
|
|
38
|
+
const bps = BigInt(Number(feeBps));
|
|
39
|
+
if (total < 0n || !Number.isFinite(Number(feeBps)) || bps < 0n || bps > BigInt(MAX_FEE_BPS)) {
|
|
40
|
+
throw new Error("principal: invalid total or fee rate");
|
|
41
|
+
}
|
|
42
|
+
return ((total + 1n) * 10000n - 1n) / (10000n + bps);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const payoutBig = (value, { allowZero = true } = {}) => {
|
|
46
|
+
if (value === null || value === undefined || value === "") return null;
|
|
47
|
+
try {
|
|
48
|
+
const n = BigInt(value);
|
|
49
|
+
return n < 0n || (!allowZero && n === 0n) ? null : n;
|
|
50
|
+
} catch { return null; }
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* THE SALE'S TWO FIGURES BEFORE THE CLAIM. The escrow holds `amount`; `fee` and `claimTip` are separate fields. The
|
|
55
|
+
* guaranteed minimum is `amount - fee - claimTip` (a third party sends the claim and takes the tip); the payout when
|
|
56
|
+
* the seller sends it is `amount - fee`.
|
|
57
|
+
*/
|
|
58
|
+
export function salePayoutBeforeClaim({ amountWei, feeWei, claimTipWei = 0 } = {}) {
|
|
59
|
+
const amount = payoutBig(amountWei, { allowZero: false });
|
|
60
|
+
const fee = payoutBig(feeWei);
|
|
61
|
+
const tip = payoutBig(claimTipWei);
|
|
62
|
+
if (amount === null || fee === null || tip === null || fee > amount || fee + tip > amount) return null;
|
|
63
|
+
return {
|
|
64
|
+
amountWei: amount,
|
|
65
|
+
feeWei: fee,
|
|
66
|
+
claimTipWei: tip,
|
|
67
|
+
guaranteedMinimumWei: amount - fee - tip,
|
|
68
|
+
sellerClaimWei: amount - fee,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* THE SELLER'S ACTUAL PAYOUT AFTER A CONFIRMED CLAIM. `sender` is the contract's `msg.sender`, not `tx.from`: a claim
|
|
74
|
+
* routed through another contract has a different caller. A tip of zero pays `amount - fee` even when the caller is
|
|
75
|
+
* unknown; a positive tip pays `amount - fee` only when the claimer sent it, otherwise `amount - fee - claimTip`, and
|
|
76
|
+
* is unknown when the actual caller is not known. A promised tip is never reported as the seller's payout.
|
|
77
|
+
*/
|
|
78
|
+
export function salePayoutAfterClaim({ amountWei, feeWei, claimTipWei = 0, claimed = false, claimSucceeded = false,
|
|
79
|
+
sender = null, claimer = null } = {}) {
|
|
80
|
+
const amount = payoutBig(amountWei, { allowZero: false });
|
|
81
|
+
const fee = payoutBig(feeWei);
|
|
82
|
+
const tip = payoutBig(claimTipWei);
|
|
83
|
+
if (amount === null || fee === null || tip === null) return { outcome: "unknown", payoutWei: null };
|
|
84
|
+
if (!claimed || !claimSucceeded || fee > amount || fee + tip > amount) {
|
|
85
|
+
return { outcome: "unknown", payoutWei: null };
|
|
86
|
+
}
|
|
87
|
+
if (tip === 0n) return { outcome: "claimed", payoutWei: amount - fee };
|
|
88
|
+
const from = typeof sender === "string" ? sender.trim().toLowerCase() : "";
|
|
89
|
+
const to = typeof claimer === "string" ? claimer.trim().toLowerCase() : "";
|
|
90
|
+
if (!from || !to) return { outcome: "unknown", payoutWei: null };
|
|
91
|
+
if (from === to) return { outcome: "claimed", payoutWei: amount - fee };
|
|
92
|
+
return { outcome: "claimed", payoutWei: amount - fee - tip };
|
|
93
|
+
}
|
|
94
|
+
|
|
37
95
|
const sameAddr = (a, b) => String(a || "").toLowerCase() === String(b || "").toLowerCase();
|
|
38
96
|
const zeroAddr = (a) => !a || /^0x0{40}$/i.test(String(a));
|
|
39
97
|
|