@ninsei-labs/sdk 0.57.2 → 0.59.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 +22 -0
- package/README.md +82 -32
- package/THIRD_PARTY_NOTICES.md +7 -4
- package/assets/vendors/MANIFEST.json +1 -9
- package/assets/vendors/sdk/sdk-browser.js +4 -6
- package/engine/core/config.js +5 -24
- package/engine/core/deployments.generated.js +33 -0
- package/engine/core/rfqSource.js +6 -133
- package/engine/core/swap.js +1 -1
- package/engine/evm/index.js +3 -2
- package/engine/evm/session.js +46 -277
- package/engine/monero/address.js +15 -33
- package/engine/monero/keys.js +124 -0
- package/engine/monero/mnemonic.js +104 -0
- package/engine/monero/scan.js +79 -17
- package/engine/monero/wallet.js +31 -68
- package/engine/monero/wordlist.en.js +210 -0
- package/engine/recovery/restore.js +1 -1
- package/engine/sdk/bridge.js +7 -182
- package/engine/ui/walletModal.js +7 -27
- package/index.d.ts +248 -254
- package/package.json +6 -7
- package/src/codes.mjs +12 -0
- package/src/config.mjs +16 -54
- package/src/engine.mjs +3 -0
- package/src/index.mjs +25 -46
- package/src/order.mjs +2 -2
- package/src/public/networks.d.ts +9 -0
- package/src/public/networks.mjs +1 -0
- package/src/public/recovery.d.ts +17 -1
- package/src/public/recovery.mjs +1 -0
- package/src/public/xmr.d.ts +8 -0
- package/src/public/xmr.mjs +1 -1
- package/src/quotePolicy.mjs +58 -0
- package/src/quotes.mjs +30 -753
- package/src/readyWindow.mjs +76 -0
- package/src/record.mjs +4 -0
- package/src/recovery-key.mjs +39 -2
- package/src/recovery.mjs +11 -28
- package/src/relayClaim.mjs +132 -0
- package/src/relayFirmClient.mjs +3 -0
- package/src/relayQuote.mjs +5 -1
- package/src/swap-flow.mjs +49 -5
- package/src/swaps.mjs +530 -317
- package/src/sweep-action.mjs +73 -0
- package/src/sweep.mjs +7 -1
- package/src/version.mjs +1 -1
- package/src/witness-slice.mjs +84 -0
- package/src/xmr-arrival.mjs +378 -9
- package/src/xmrAmountPolicy.mjs +35 -0
- package/assets/vendors/monero/monero-js-browser.js +0 -58
- package/assets/vendors/monero-js/LICENSE +0 -9
- package/assets/vendors/walletconnect/wc-browser.js +0 -6202
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// MONERO MNEMONIC, WITHOUT A VENDORED LIBRARY.
|
|
2
|
+
//
|
|
3
|
+
// Monero uses its own 1626-word list and a checksum over the first three letters of the 24 payload words. The
|
|
4
|
+
// algorithm is fixed and small; `@noble` primitives are not involved. Keeping it here removes the old monero-js
|
|
5
|
+
// bundle from the package and leaves one wordlist, one encoder and one decoder.
|
|
6
|
+
import { randomBytes } from "../../src/primitives.mjs";
|
|
7
|
+
import { WORDLIST } from "./wordlist.en.js";
|
|
8
|
+
|
|
9
|
+
const WORD_COUNT = 24;
|
|
10
|
+
const CHECKSUM_WORD_COUNT = 25;
|
|
11
|
+
const ENTROPY_BYTES = 32;
|
|
12
|
+
const PREFIX = 3;
|
|
13
|
+
const WORD_INDEX = new Map(WORDLIST.map((word, index) => [word, index]));
|
|
14
|
+
|
|
15
|
+
const CRC_TABLE = (() => {
|
|
16
|
+
const table = new Uint32Array(256);
|
|
17
|
+
for (let n = 0; n < 256; n += 1) {
|
|
18
|
+
let c = n;
|
|
19
|
+
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
20
|
+
table[n] = c >>> 0;
|
|
21
|
+
}
|
|
22
|
+
return table;
|
|
23
|
+
})();
|
|
24
|
+
|
|
25
|
+
function crc32(bytes) {
|
|
26
|
+
let c = 0xffffffff;
|
|
27
|
+
for (const byte of bytes) c = CRC_TABLE[(c ^ byte) & 0xff] ^ (c >>> 8);
|
|
28
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function wordsToChunk(one, two, three) {
|
|
32
|
+
const n = WORDLIST.length;
|
|
33
|
+
const a = WORD_INDEX.get(one);
|
|
34
|
+
const b = WORD_INDEX.get(two);
|
|
35
|
+
const c = WORD_INDEX.get(three);
|
|
36
|
+
if (a === undefined || b === undefined || c === undefined) throw new Error("invalid mnemonic word");
|
|
37
|
+
// The word order encodes a value in a mixed radix; the inverse is in chunkToWords.
|
|
38
|
+
return a + n * (((b - a) % n + n) % n) + n * n * (((c - b) % n + n) % n);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function chunkToWords(value) {
|
|
42
|
+
const n = WORDLIST.length;
|
|
43
|
+
const one = value % n;
|
|
44
|
+
const two = (Math.floor(value / n) + one) % n;
|
|
45
|
+
const three = (Math.floor(Math.floor(value / n) / n) + two) % n;
|
|
46
|
+
return [WORDLIST[one], WORDLIST[two], WORDLIST[three]];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function checksumWord(words) {
|
|
50
|
+
const prefix = words.map((word) => word.slice(0, PREFIX)).join("");
|
|
51
|
+
return words[crc32(new TextEncoder().encode(prefix)) % words.length];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function validateWords(words) {
|
|
55
|
+
return (words.length === WORD_COUNT || words.length === CHECKSUM_WORD_COUNT)
|
|
56
|
+
&& words.every((word) => WORD_INDEX.has(word));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function encode(entropy) {
|
|
60
|
+
const bytes = entropy instanceof Uint8Array ? entropy : Uint8Array.from(entropy);
|
|
61
|
+
if (bytes.length !== ENTROPY_BYTES) throw new Error("invalid entropy length");
|
|
62
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
63
|
+
const words = [];
|
|
64
|
+
for (let i = 0; i < bytes.length; i += 4) words.push(...chunkToWords(view.getUint32(i, true)));
|
|
65
|
+
return words.join(" ");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function encodeWithChecksum(entropy) {
|
|
69
|
+
const words = encode(entropy).split(" ");
|
|
70
|
+
words.push(checksumWord(words));
|
|
71
|
+
return words.join(" ");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function generate() {
|
|
75
|
+
return encode(randomBytes(ENTROPY_BYTES));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function generateWithChecksum() {
|
|
79
|
+
return encodeWithChecksum(randomBytes(ENTROPY_BYTES));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function decode(mnemonic) {
|
|
83
|
+
const words = String(mnemonic || "").trim().split(/\s+/).filter(Boolean);
|
|
84
|
+
if (!validateWords(words)) throw new Error("invalid mnemonic phrase (length or words)");
|
|
85
|
+
if (words.length === CHECKSUM_WORD_COUNT) {
|
|
86
|
+
const payload = words.slice(0, WORD_COUNT);
|
|
87
|
+
if (words[WORD_COUNT] !== checksumWord(payload)) throw new Error("invalid mnemonic checksum");
|
|
88
|
+
}
|
|
89
|
+
const entropy = new Uint8Array(ENTROPY_BYTES);
|
|
90
|
+
const view = new DataView(entropy.buffer);
|
|
91
|
+
const count = Math.min(words.length, WORD_COUNT);
|
|
92
|
+
for (let i = 0; i < count / 3; i += 1) {
|
|
93
|
+
view.setUint32(i * 4, wordsToChunk(words[i * 3], words[i * 3 + 1], words[i * 3 + 2]), true);
|
|
94
|
+
}
|
|
95
|
+
return entropy;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function isValid(mnemonic) {
|
|
99
|
+
try { decode(mnemonic); return true; } catch { return false; }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function toSeed(mnemonic) {
|
|
103
|
+
return decode(mnemonic);
|
|
104
|
+
}
|
package/engine/monero/scan.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
1
|
+
// THE MONERO INDEXER FOR THE SDK. The browser bundle carries a generated copy; this module is the source the package
|
|
2
|
+
// checks and tests use. Edit it here and regenerate the browser copy with `pnpm run build:sdk:docker` when a bundle
|
|
3
|
+
// change is needed.
|
|
4
|
+
//
|
|
5
5
|
// MONERO INDEXER: we find arrivals at the swap address ourselves by parsing blocks.
|
|
6
6
|
// WHY. Before, arrivals were learned from monero-wallet-rpc through a watch-only wallet, a path that hit a defect
|
|
7
7
|
// (the wallet+keys pair is created and never reopens) and where one instance held one wallet, so scanning 25
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
26
26
|
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
27
27
|
import { UNLOCK_TIME_BLOCK_MAX, unlockStateOf, laterBoundary } from "./unlock.js";
|
|
28
|
+
import { decodeAddressKeys } from "./arrivalRead.js";
|
|
28
29
|
export { UNLOCK_TIME_BLOCK_MAX, unlockStateOf, laterBoundary };
|
|
29
30
|
|
|
30
31
|
export function keccak(bytes) { return keccak_256(bytes); }
|
|
@@ -125,6 +126,31 @@ export function viewKeyFormForAddress(viewSecret, expectedViewPubHex) {
|
|
|
125
126
|
return false;
|
|
126
127
|
}
|
|
127
128
|
|
|
129
|
+
// THE KEY MATERIAL MUST BELONG TO THE ADDRESS IT IS SCANNED FOR. A public `scanRange` used to try both byte-order
|
|
130
|
+
// readings of the view key and could count a payment to another address as ours. This is the ONE binding rule: the
|
|
131
|
+
// scanner and both facade paths call it, so a host cannot get a different answer by choosing another entry point.
|
|
132
|
+
// A subaddress or integrated address is refused here; the joint address this SDK builds is primary.
|
|
133
|
+
export function watchedAddressVerdict({ address, viewSecret, spendPub } = {}) {
|
|
134
|
+
let keys;
|
|
135
|
+
try {
|
|
136
|
+
keys = decodeAddressKeys(address);
|
|
137
|
+
} catch {
|
|
138
|
+
return { ok: false, code: "bad-input", why: "not-a-monero-address", field: "address" };
|
|
139
|
+
}
|
|
140
|
+
if (!keys.network) return { ok: false, code: "bad-input", why: "not-a-primary-address", field: "address" };
|
|
141
|
+
const wantedSpend = String(spendPub === undefined || spendPub === null ? "" : spendPub).replace(/^0x/, "").toLowerCase();
|
|
142
|
+
if (!wantedSpend || wantedSpend !== keys.spendPub.toLowerCase()) {
|
|
143
|
+
return { ok: false, code: "xmr-address-mismatch", field: "spendPub" };
|
|
144
|
+
}
|
|
145
|
+
const form = viewKeyFormForAddress(viewSecret, keys.viewPub);
|
|
146
|
+
if (!form) return { ok: false, code: "xmr-address-mismatch", field: "viewSecret" };
|
|
147
|
+
const secret = viewSecretCandidates(viewSecret).find(([name]) => name === form)?.[1];
|
|
148
|
+
if (secret === undefined || secret === null) {
|
|
149
|
+
return { ok: false, code: "xmr-address-mismatch", field: "viewSecret" };
|
|
150
|
+
}
|
|
151
|
+
return { ok: true, address, keys, form, viewSecret: secret, spendPub: keys.spendPub };
|
|
152
|
+
}
|
|
153
|
+
|
|
128
154
|
export function keyDerivation(txPublicKeyHex, viewSecretHex) {
|
|
129
155
|
const txPub = pointFromHex(txPublicKeyHex);
|
|
130
156
|
// The view key is accepted both as LE bytes from monero-rpc and as a number if already parsed.
|
|
@@ -272,12 +298,22 @@ export function findOurOutputs({ txPublicKey = null, txPublicKeys = null, additi
|
|
|
272
298
|
const usable = viewSecretCandidates(viewSecretHex).filter(([, v]) => v !== null);
|
|
273
299
|
const first = usable[0] || ["unparsed", null];
|
|
274
300
|
for (const [form, secret] of usable) {
|
|
275
|
-
|
|
301
|
+
// A MALFORMED EXTRA KEY IS NOT A REASON TO STOP LOOKING AT THE OTHER KEYS. Monero's wallet skips a key that is
|
|
302
|
+
// not a curve point and keeps scanning; a transaction with one bad tag-0x01 key otherwise blinds every arrival
|
|
303
|
+
// whose range includes its block.
|
|
304
|
+
const primaryDerivations = [];
|
|
305
|
+
for (const key of primaryKeys) {
|
|
306
|
+
try { primaryDerivations.push(keyDerivation(key, secret)); }
|
|
307
|
+
catch { /* skip this candidate; the other keys may still be ours */ }
|
|
308
|
+
}
|
|
276
309
|
const found = [];
|
|
277
310
|
for (const o of outputs) {
|
|
278
311
|
// The main keys first, then only THIS output's additional key.
|
|
279
312
|
const candidates = primaryDerivations.slice();
|
|
280
|
-
if (additional[o.index])
|
|
313
|
+
if (additional[o.index]) {
|
|
314
|
+
try { candidates.push(keyDerivation(additional[o.index], secret)); }
|
|
315
|
+
catch { /* a malformed per-output key is skipped, like the primary list */ }
|
|
316
|
+
}
|
|
281
317
|
for (const derivation of candidates) {
|
|
282
318
|
const scalar = hashToScalar(derivation, varint(o.index));
|
|
283
319
|
const derived = derivedOutputKey(derivation, o.index, spendPubHex);
|
|
@@ -302,13 +338,17 @@ export function findOurOutputs({ txPublicKey = null, txPublicKeys = null, additi
|
|
|
302
338
|
}
|
|
303
339
|
}
|
|
304
340
|
if (found.length) return { derivation: found[0].derivation, outputs: found, viewKeyForm: form };
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
341
|
+
}
|
|
342
|
+
let fallback = null;
|
|
343
|
+
if (first[1] !== null && primaryKeys.length) {
|
|
344
|
+
try { fallback = bytesToHex(keyDerivation(primaryKeys[0], first[1])); } catch { fallback = null; }
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
derivation: fallback,
|
|
348
|
+
outputs: [],
|
|
349
|
+
viewKeyForm: first[0],
|
|
350
|
+
};
|
|
351
|
+
}
|
|
312
352
|
|
|
313
353
|
// ============================================================================================
|
|
314
354
|
// SCANNING A HEIGHT RANGE. This is the indexing itself: read blocks in order, take their transaction hashes
|
|
@@ -401,7 +441,11 @@ export async function fetchTransactions(daemonUrl, hashes, fetchImpl = globalThi
|
|
|
401
441
|
|
|
402
442
|
// Parsing one transaction against the watched addresses. watched: [{ address, viewSecret, spendPub }] - viewSecret
|
|
403
443
|
// is taken as a number, LE-hex or BE-hex. The result carries viewKeyForm: the form name that matched.
|
|
404
|
-
|
|
444
|
+
//
|
|
445
|
+
// THE KEY BINDING IS A PROPERTY OF THE WATCHED ENTRY, NOT OF THE TRANSACTION. `scanRange` binds every entry once
|
|
446
|
+
// before its block loop and passes the result here; the public `matchTransaction` binds the entries it is given.
|
|
447
|
+
// Re-binding inside the loop made every transaction repeat base58 decoding, keccak and an ed25519 multiplication.
|
|
448
|
+
function matchBoundTransaction(tx, boundWatched, { height = null, tipHeight = null, nowSec = null } = {}) {
|
|
405
449
|
const out = [];
|
|
406
450
|
// BOTH KEY LISTS ARE PART OF THE TRANSACTION. A subaddress output carries its own key under tag 0x04; using only
|
|
407
451
|
// `txPublicKeyFromExtra` drops exactly those outputs.
|
|
@@ -422,10 +466,10 @@ export function matchTransaction(tx, watched, { height = null, tipHeight = null,
|
|
|
422
466
|
const ecdhMasks = ecdh.map((e) => e.mask);
|
|
423
467
|
const outPk = ((tx.rct_signatures || {}).outPk || []);
|
|
424
468
|
const rctVersion = (tx.rct_signatures || {}).type || 2;
|
|
425
|
-
for (const w of
|
|
469
|
+
for (const { entry: w, bound } of boundWatched) {
|
|
426
470
|
const r = findOurOutputs({
|
|
427
471
|
txPublicKeys: keys.txPublicKeys, additionalPublicKeys: keys.additionalPublicKeys,
|
|
428
|
-
viewSecretHex:
|
|
472
|
+
viewSecretHex: bound.viewSecret, spendPubHex: bound.spendPub,
|
|
429
473
|
outputs, ecdhAmounts, ecdhMasks, outPk, rctVersion: Number(rctVersion) || 1,
|
|
430
474
|
});
|
|
431
475
|
if (r.outputs.length) out.push({ address: w.address, index: w.index ?? null, outputs: r.outputs, derivation: r.derivation, viewKeyForm: r.viewKeyForm, unlockTime: Number(tx.unlock_time) || 0, unlock });
|
|
@@ -433,6 +477,21 @@ export function matchTransaction(tx, watched, { height = null, tipHeight = null,
|
|
|
433
477
|
return out;
|
|
434
478
|
}
|
|
435
479
|
|
|
480
|
+
// THE PUBLIC FORM BINDS EACH WATCHED ENTRY BEFORE IT LOOKS AT THE TRANSACTION. An entry that is not a primary
|
|
481
|
+
// address with this spend key is skipped; the binding cannot be supplied by the caller.
|
|
482
|
+
function bindWatched(watched) {
|
|
483
|
+
const bound = [];
|
|
484
|
+
for (const entry of watched) {
|
|
485
|
+
const verdict = watchedAddressVerdict(entry);
|
|
486
|
+
if (verdict.ok) bound.push({ entry, bound: verdict });
|
|
487
|
+
}
|
|
488
|
+
return bound;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
export function matchTransaction(tx, watched, options = {}) {
|
|
492
|
+
return matchBoundTransaction(tx, bindWatched(watched), options);
|
|
493
|
+
}
|
|
494
|
+
|
|
436
495
|
// Range scan. Returns per address the sum (receivedAtomic - only the UNLOCKED), hashes, the locked sum
|
|
437
496
|
// (lockedAtomic) with its boundary (lockedUntil) and the best (highest) height of a found output.
|
|
438
497
|
export async function scanRange({ daemonUrl, fromHeight, toHeight, watched, log = () => {}, concurrency = 4, nowSec = null, tipHeight = null, fetchImpl = globalThis.fetch }) {
|
|
@@ -440,6 +499,9 @@ export async function scanRange({ daemonUrl, fromHeight, toHeight, watched, log
|
|
|
440
499
|
if (!(from >= 0) || !(to >= from)) throw new Error("bad height range: " + fromHeight + ".." + toHeight);
|
|
441
500
|
const acc = new Map();
|
|
442
501
|
for (const w of watched) acc.set(w.address, { address: w.address, receivedAtomic: 0n, lockedAtomic: 0n, lockedUntil: null, lockedTxids: [], txids: [], bestHeight: null, outputs: 0, viewKeyForm: null });
|
|
502
|
+
// BIND ONCE PER WATCHED ADDRESS. The binding does not depend on the transaction, and an invalid entry is skipped
|
|
503
|
+
// here exactly as the public matcher skips it.
|
|
504
|
+
const boundWatched = bindWatched(watched);
|
|
443
505
|
const heights = [];
|
|
444
506
|
for (let h = from; h <= to; h++) heights.push(h);
|
|
445
507
|
const missingHeights = [];
|
|
@@ -455,7 +517,7 @@ export async function scanRange({ daemonUrl, fromHeight, toHeight, watched, log
|
|
|
455
517
|
if (!hashes.length) continue;
|
|
456
518
|
const txs = await fetchTransactions(daemonUrl, hashes, fetchImpl);
|
|
457
519
|
for (const tx of txs) {
|
|
458
|
-
for (const hit of
|
|
520
|
+
for (const hit of matchBoundTransaction(tx, boundWatched, { height: h, tipHeight: tipHeight === null || tipHeight === undefined ? to : tipHeight, nowSec })) {
|
|
459
521
|
const a = acc.get(hit.address);
|
|
460
522
|
if (!a) continue;
|
|
461
523
|
if (hit.viewKeyForm) a.viewKeyForm = hit.viewKeyForm; // what matched - this goes to the log
|
package/engine/monero/wallet.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// Everything below this header is identical to the source.
|
|
1
|
+
// SDK ENGINE MODULE - the demo's sdk/ mirror copies this file byte for byte (tools/build-sdk-engine.mjs).
|
|
2
|
+
// The page's own www/js module is a separate copy; edit this package source and regenerate the mirror.
|
|
4
3
|
|
|
5
4
|
// The one-shot Monero wallet of a swap.
|
|
6
5
|
//
|
|
7
|
-
// Keys are
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
// stagenet (24/36) also works.
|
|
6
|
+
// Keys are derived by the package's own Monero module (engine/monero/keys.js) from @noble/curves; the 25-word
|
|
7
|
+
// mnemonic is encoded/decoded by engine/monero/mnemonic.js. The old vendored monero-js bundle is gone. The address
|
|
8
|
+
// string is built by our encoder (monero/address.js), which knows mainnet, stagenet and testnet prefixes.
|
|
11
9
|
//
|
|
12
10
|
// Keys NEVER leave the browser: only the view key goes out (to the backend, for a watch-only wallet), and a
|
|
13
11
|
// separate explicit function does it - recovery/restore.js::watchOnlyShareFromWallet. The wallet is one-shot,
|
|
@@ -15,81 +13,51 @@
|
|
|
15
13
|
|
|
16
14
|
import { MONERO } from "../core/config.js";
|
|
17
15
|
import { randomHex, toHex } from "../core/format.js";
|
|
16
|
+
import { keccak256 } from "../../src/primitives.mjs";
|
|
18
17
|
import { addressFromKeys } from "./address.js";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
function base58(bytes) {
|
|
23
|
-
let num = 0n;
|
|
24
|
-
for (const b of bytes) num = num * 256n + BigInt(b);
|
|
25
|
-
let out = "";
|
|
26
|
-
while (num > 0n) {
|
|
27
|
-
out = B58[Number(num % 58n)] + out;
|
|
28
|
-
num /= 58n;
|
|
29
|
-
}
|
|
30
|
-
return out;
|
|
31
|
-
}
|
|
18
|
+
import * as moneroMnemonic from "./mnemonic.js";
|
|
19
|
+
import * as moneroKeys from "./keys.js";
|
|
32
20
|
|
|
33
21
|
// A pseudo-address for mock mode: the right length (95) and network prefix, but WITHOUT a checksum.
|
|
34
22
|
// The UI always marks it as mock, so nobody sends XMR there.
|
|
35
23
|
|
|
36
24
|
// ---------------------------------------------------------------------------
|
|
37
|
-
//
|
|
25
|
+
// Monero mnemonic + keys + addresses (the package's own implementation)
|
|
38
26
|
// ---------------------------------------------------------------------------
|
|
39
27
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (moneroJsError) throw new Error(moneroJsError);
|
|
46
|
-
try {
|
|
47
|
-
const mod = await import("../../assets/vendors/monero/monero-js-browser.js?v=05c682ea");
|
|
48
|
-
moneroJsModule = mod.default || mod;
|
|
49
|
-
return moneroJsModule;
|
|
50
|
-
} catch (e) {
|
|
51
|
-
// The reason is not replaced: "bundle not built", "not uploaded" and "blocked by CSP" are different problems
|
|
52
|
-
moneroJsError = "monero-js unavailable: " + e.message +
|
|
53
|
-
" (run npm run build:vendor; for CSP see the deploy notes)";
|
|
54
|
-
throw new Error(moneroJsError);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async function withMoneroJs({ mnemonic: phrase, network = MONERO.networkType } = {}) {
|
|
59
|
-
const lib = await loadMoneroJs();
|
|
60
|
-
await lib.wallet.initEcc();
|
|
61
|
-
|
|
62
|
-
// A supplied phrase must be valid. An invalid phrase used to be silently replaced by a fresh one, so
|
|
63
|
-
// recovering from a corrupt file gave a DIFFERENT wallet: the error looked like success and the funds stayed
|
|
64
|
-
// out of reach. Generation happens only when there is no phrase at all.
|
|
28
|
+
// A supplied phrase must be valid. An invalid phrase used to be silently replaced by a fresh one, so recovering
|
|
29
|
+
// from a corrupt file gave a DIFFERENT wallet: the error looked like success and the funds stayed out of reach.
|
|
30
|
+
// Generation happens only when there is no phrase at all.
|
|
31
|
+
async function withMoneroKeys({ mnemonic: phrase, network = MONERO.networkType } = {}) {
|
|
32
|
+
await moneroKeys.initEcc();
|
|
65
33
|
let words;
|
|
66
34
|
if (phrase) {
|
|
67
|
-
if (!
|
|
35
|
+
if (!moneroMnemonic.isValid(phrase)) {
|
|
68
36
|
throw new Error("the mnemonic failed its checksum check (file or form corrupted?)");
|
|
69
37
|
}
|
|
70
38
|
words = String(phrase).trim();
|
|
71
39
|
} else {
|
|
72
|
-
words =
|
|
40
|
+
words = moneroMnemonic.generateWithChecksum();
|
|
73
41
|
}
|
|
74
|
-
const seed =
|
|
75
|
-
const w =
|
|
42
|
+
const seed = moneroMnemonic.toSeed(words); // 32 bytes: for Monero the seed == the decoded mnemonic
|
|
43
|
+
const w = moneroKeys.fromSeed(seed);
|
|
76
44
|
|
|
77
45
|
const net = network || "mainnet";
|
|
78
|
-
const deps = { keccak256
|
|
46
|
+
const deps = { keccak256 };
|
|
79
47
|
const spendPub = toHex(w.publicSpendKey);
|
|
80
48
|
const viewPub = toHex(w.publicViewKey);
|
|
81
49
|
const sub = w.subaddress(1, 0);
|
|
82
|
-
//
|
|
83
|
-
// addressMainnet is kept for cross-checking: on mainnet the two must match
|
|
50
|
+
// The address of our own network is built here; mainnet is kept for cross-checking.
|
|
84
51
|
const address = addressFromKeys({ network: net, kind: "primary", spendPub, viewPub }, deps);
|
|
85
52
|
const subaddress = addressFromKeys(
|
|
86
53
|
{ network: net, kind: "subaddress", spendPub: toHex(sub.publicSpendKey), viewPub: toHex(sub.publicViewKey) },
|
|
87
54
|
deps
|
|
88
55
|
);
|
|
56
|
+
const addressMainnet = addressFromKeys({ network: "mainnet", kind: "primary", spendPub, viewPub }, deps);
|
|
89
57
|
|
|
90
58
|
return {
|
|
91
|
-
source: "monero-
|
|
92
|
-
library: "
|
|
59
|
+
source: "ninsei-monero-keys",
|
|
60
|
+
library: "Ninsei Monero keys (@noble/curves + @noble/hashes)",
|
|
93
61
|
network: net,
|
|
94
62
|
mnemonic: words,
|
|
95
63
|
mnemonicWords: words.split(" ").length,
|
|
@@ -100,31 +68,26 @@ async function withMoneroJs({ mnemonic: phrase, network = MONERO.networkType } =
|
|
|
100
68
|
publicViewKey: viewPub,
|
|
101
69
|
address,
|
|
102
70
|
subaddress,
|
|
103
|
-
addressMainnet
|
|
71
|
+
addressMainnet,
|
|
104
72
|
createdAt: Date.now(),
|
|
105
73
|
};
|
|
106
74
|
}
|
|
107
75
|
|
|
108
76
|
// ---------------------------------------------------------------------------
|
|
109
|
-
// monero-ts
|
|
110
|
-
// ---------------------------------------------------------------------------
|
|
111
|
-
// A second wallet mode lived here - a WebAssembly wallet2 (woodser/monero-ts) built into the vendors. It was
|
|
112
|
-
// removed for two reasons: the WASM wallet needs SharedArrayBuffer, i.e. COOP/COEP, which on the demo domain
|
|
113
|
-
// would block third-party resources; and the bundle moved to the sweep app, which has its own COOP/COEP domain.
|
|
114
|
-
// While the branch of code stayed, it pulled an import along a path that no longer existed - dead, yet looking
|
|
115
|
-
// alive. Keys in the demo are computed by monero-js, and that is the only path.
|
|
116
|
-
|
|
77
|
+
// The old monero-js / monero-ts browser paths are gone.
|
|
117
78
|
// ---------------------------------------------------------------------------
|
|
79
|
+
// Keys are derived by engine/monero/keys.js from @noble/curves. The monero-ts WASM wallet was removed earlier:
|
|
80
|
+
// it needs SharedArrayBuffer, i.e. COOP/COEP, which on the demo domain would block third-party resources; the
|
|
81
|
+
// sweep app has its own domain and wallet adapter.
|
|
118
82
|
|
|
119
83
|
export async function createSwapWallet({ network = MONERO.networkType, seed } = {}) {
|
|
120
|
-
return
|
|
84
|
+
return withMoneroKeys({ mnemonic: seed, network });
|
|
121
85
|
}
|
|
122
86
|
|
|
123
|
-
// Whether the key
|
|
87
|
+
// Whether the key module is available (the Demo controls panel shows this as one line).
|
|
124
88
|
export async function probeMode() {
|
|
125
89
|
try {
|
|
126
|
-
|
|
127
|
-
await lib.wallet.initEcc();
|
|
90
|
+
await moneroKeys.initEcc();
|
|
128
91
|
return { available: true, info: "mnemonic + keys + addresses (" + MONERO.networkType + ")" };
|
|
129
92
|
} catch (e) {
|
|
130
93
|
return { available: false, reason: e.message };
|