@otoplo/wallet-common 0.5.2 → 0.6.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/dist/index.d.ts +869 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +206 -194
- package/dist/index.js.map +1 -1
- package/package.json +7 -8
- package/src/services/rostrum.ts +42 -22
- package/src/services/wallet.ts +57 -68
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/persistence/index.d.ts +0 -3
- package/dist/types/persistence/index.d.ts.map +0 -1
- package/dist/types/persistence/repository.d.ts +0 -43
- package/dist/types/persistence/repository.d.ts.map +0 -1
- package/dist/types/persistence/wallet-db.d.ts +0 -44
- package/dist/types/persistence/wallet-db.d.ts.map +0 -1
- package/dist/types/services/asset.d.ts +0 -21
- package/dist/types/services/asset.d.ts.map +0 -1
- package/dist/types/services/cache.d.ts +0 -14
- package/dist/types/services/cache.d.ts.map +0 -1
- package/dist/types/services/discovery.d.ts +0 -18
- package/dist/types/services/discovery.d.ts.map +0 -1
- package/dist/types/services/index.d.ts +0 -10
- package/dist/types/services/index.d.ts.map +0 -1
- package/dist/types/services/key.d.ts +0 -17
- package/dist/types/services/key.d.ts.map +0 -1
- package/dist/types/services/market.d.ts +0 -33
- package/dist/types/services/market.d.ts.map +0 -1
- package/dist/types/services/rostrum.d.ts +0 -39
- package/dist/types/services/rostrum.d.ts.map +0 -1
- package/dist/types/services/session.d.ts +0 -48
- package/dist/types/services/session.d.ts.map +0 -1
- package/dist/types/services/transaction.d.ts +0 -41
- package/dist/types/services/transaction.d.ts.map +0 -1
- package/dist/types/services/tx-transformer.d.ts +0 -22
- package/dist/types/services/tx-transformer.d.ts.map +0 -1
- package/dist/types/services/wallet.d.ts +0 -107
- package/dist/types/services/wallet.d.ts.map +0 -1
- package/dist/types/types/dapp.types.d.ts +0 -19
- package/dist/types/types/dapp.types.d.ts.map +0 -1
- package/dist/types/types/db.types.d.ts +0 -138
- package/dist/types/types/db.types.d.ts.map +0 -1
- package/dist/types/types/index.d.ts +0 -6
- package/dist/types/types/index.d.ts.map +0 -1
- package/dist/types/types/notification.types.d.ts +0 -21
- package/dist/types/types/notification.types.d.ts.map +0 -1
- package/dist/types/types/rostrum.types.d.ts +0 -132
- package/dist/types/types/rostrum.types.d.ts.map +0 -1
- package/dist/types/types/wallet.types.d.ts +0 -80
- package/dist/types/types/wallet.types.d.ts.map +0 -1
- package/dist/types/utils/asset.d.ts +0 -5
- package/dist/types/utils/asset.d.ts.map +0 -1
- package/dist/types/utils/common.d.ts +0 -64
- package/dist/types/utils/common.d.ts.map +0 -1
- package/dist/types/utils/enums.d.ts +0 -16
- package/dist/types/utils/enums.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts +0 -7
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/keypath.d.ts +0 -5
- package/dist/types/utils/keypath.d.ts.map +0 -1
- package/dist/types/utils/seed.d.ts +0 -5
- package/dist/types/utils/seed.d.ts.map +0 -1
- package/dist/types/utils/vault.d.ts +0 -10
- package/dist/types/utils/vault.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -2,16 +2,18 @@ import { Address as e, AddressType as t, BufferUtils as n, BufferWriter as r, Co
|
|
|
2
2
|
import y from "js-big-decimal";
|
|
3
3
|
import { ConnectionStatus as te, ElectrumClient as ne, TransportScheme as b } from "@otoplo/electrum-client";
|
|
4
4
|
import { generateMnemonic as re, mnemonicToSeedSync as ie, validateMnemonic as ae } from "@scure/bip39";
|
|
5
|
-
import { DAppProvider as oe } from "wallet-comms-sdk";
|
|
6
5
|
import { wordlist as x } from "@scure/bip39/wordlists/english.js";
|
|
6
|
+
import { DAppProvider as oe } from "wallet-comms-sdk";
|
|
7
7
|
// src/utils/enums.ts
|
|
8
|
-
|
|
8
|
+
let se = /* @__PURE__ */ function(e) {
|
|
9
9
|
return e[e.MAIN = 0] = "MAIN", e[e.VAULT = 1] = "VAULT", e[e.DAPP = 2] = "DAPP", e;
|
|
10
|
-
}({}),
|
|
10
|
+
}({}), ce = /* @__PURE__ */ function(e) {
|
|
11
11
|
return e[e.RECEIVE = 0] = "RECEIVE", e[e.CHANGE = 1] = "CHANGE", e;
|
|
12
|
-
}({}),
|
|
12
|
+
}({}), le = /* @__PURE__ */ function(e) {
|
|
13
13
|
return e.SignMessage = "signMessage", e.AddToken = "addToken", e.SignTransaction = "signTransaction", e.SendTransaction = "sendTransaction", e;
|
|
14
|
-
}({})
|
|
14
|
+
}({});
|
|
15
|
+
// src/utils/common.ts
|
|
16
|
+
const ue = -1, de = 9223372036854775807n, S = {
|
|
15
17
|
usd: {
|
|
16
18
|
symbol: "$",
|
|
17
19
|
name: "US Dollar"
|
|
@@ -48,46 +50,46 @@ var S = /* @__PURE__ */ function(e) {
|
|
|
48
50
|
symbol: "¥",
|
|
49
51
|
name: "Japanese Yen"
|
|
50
52
|
}
|
|
51
|
-
},
|
|
52
|
-
function
|
|
53
|
+
}, C = Object.keys(S);
|
|
54
|
+
function w() {
|
|
53
55
|
return s.defaultNetwork == s.testnet;
|
|
54
56
|
}
|
|
55
|
-
function
|
|
56
|
-
return
|
|
57
|
+
function fe(e) {
|
|
58
|
+
return w() ? e == "508c843a4b98fb25f57cf9ebafb245a5c16468f06519cdd467059a91e7b79d52" : e == "edc7144fe1ba4edd0edf35d7eea90f6cb1dba42314aa85da8207e97c5339c801";
|
|
57
59
|
}
|
|
58
|
-
function
|
|
60
|
+
function T(n, r = t.PayToScriptTemplate) {
|
|
59
61
|
return e.isValid(n, s.defaultNetwork, r);
|
|
60
62
|
}
|
|
61
|
-
function
|
|
62
|
-
return `https://${
|
|
63
|
+
function pe() {
|
|
64
|
+
return `https://${w() ? "testnet-" : ""}explorer.nexa.org`;
|
|
63
65
|
}
|
|
64
|
-
function
|
|
66
|
+
function E() {
|
|
65
67
|
return Math.floor(Date.now() / 1e3);
|
|
66
68
|
}
|
|
67
|
-
function
|
|
69
|
+
function D(e) {
|
|
68
70
|
return !e || e.length === 0;
|
|
69
71
|
}
|
|
70
|
-
function
|
|
72
|
+
function me(e, t = 0) {
|
|
71
73
|
if (!e || e.length <= t) return e || "";
|
|
72
74
|
let n = Math.floor((t - 3) / 2), r = e.slice(0, n), i = e.slice(e.length - n);
|
|
73
75
|
return r + "..." + i;
|
|
74
76
|
}
|
|
75
|
-
function
|
|
77
|
+
function he(e) {
|
|
76
78
|
return e.length === 0 ? "" : e.charAt(0).toUpperCase() + e.substring(1);
|
|
77
79
|
}
|
|
78
|
-
function
|
|
80
|
+
function O(t) {
|
|
79
81
|
return i.isHexa(t) ? n.hexToBuffer(t) : e.fromString(t).data;
|
|
80
82
|
}
|
|
81
|
-
function
|
|
82
|
-
return i.isHexa(e) ? e : n.bufferToHex(
|
|
83
|
+
function k(e) {
|
|
84
|
+
return i.isHexa(e) ? e : n.bufferToHex(O(e));
|
|
83
85
|
}
|
|
84
|
-
function
|
|
85
|
-
return i.isHexa(n) ? new e(
|
|
86
|
+
function ge(n) {
|
|
87
|
+
return i.isHexa(n) ? new e(O(n), s.defaultNetwork, t.GroupIdAddress).toString() : n;
|
|
86
88
|
}
|
|
87
|
-
function
|
|
89
|
+
function A(e) {
|
|
88
90
|
return e == null || e === "" ? "" : BigInt(e) <= 0n ? "0" : e.toString();
|
|
89
91
|
}
|
|
90
|
-
function
|
|
92
|
+
function j(e) {
|
|
91
93
|
let t = BigInt(0), n = BigInt(0);
|
|
92
94
|
return e.forEach((e) => {
|
|
93
95
|
t += BigInt(e.confirmed), n += BigInt(e.unconfirmed);
|
|
@@ -96,7 +98,7 @@ function P(e) {
|
|
|
96
98
|
unconfirmed: n.toString()
|
|
97
99
|
};
|
|
98
100
|
}
|
|
99
|
-
function
|
|
101
|
+
function M(e) {
|
|
100
102
|
let t = {};
|
|
101
103
|
return e.forEach((e) => {
|
|
102
104
|
for (let n in e) t[n] ? (t[n].confirmed = (BigInt(t[n].confirmed) + BigInt(e[n].confirmed)).toString(), t[n].unconfirmed = (BigInt(t[n].unconfirmed) + BigInt(e[n].unconfirmed)).toString()) : t[n] = {
|
|
@@ -105,7 +107,7 @@ function F(e) {
|
|
|
105
107
|
};
|
|
106
108
|
}), t;
|
|
107
109
|
}
|
|
108
|
-
|
|
110
|
+
const N = {
|
|
109
111
|
".gif": {
|
|
110
112
|
media: "image",
|
|
111
113
|
mime: "image/gif"
|
|
@@ -175,21 +177,25 @@ var I = {
|
|
|
175
177
|
mime: "audio/mp4"
|
|
176
178
|
}
|
|
177
179
|
};
|
|
178
|
-
function
|
|
180
|
+
function P(e) {
|
|
179
181
|
let t = e.lastIndexOf(".");
|
|
180
|
-
|
|
182
|
+
if (t === -1) return "unknown";
|
|
183
|
+
let n = e.slice(t).toLowerCase();
|
|
184
|
+
return N[n]?.media ?? "unknown";
|
|
181
185
|
}
|
|
182
|
-
function
|
|
186
|
+
function F(e) {
|
|
183
187
|
let t = e.lastIndexOf(".");
|
|
184
|
-
|
|
188
|
+
if (t === -1) return "application/octet-stream";
|
|
189
|
+
let n = e.slice(t).toLowerCase();
|
|
190
|
+
return N[n]?.mime ?? "application/octet-stream";
|
|
185
191
|
}
|
|
186
|
-
function
|
|
192
|
+
function I(e) {
|
|
187
193
|
return new Promise((t) => setTimeout(t, e));
|
|
188
194
|
}
|
|
189
|
-
function
|
|
195
|
+
function L(e) {
|
|
190
196
|
return new y(e).stripTrailingZero().getPrettyValue();
|
|
191
197
|
}
|
|
192
|
-
function
|
|
198
|
+
function _e(e, t, n, r = "en-US") {
|
|
193
199
|
let i = y.multiply(e, t);
|
|
194
200
|
return new Intl.NumberFormat(r, {
|
|
195
201
|
style: "currency",
|
|
@@ -197,7 +203,7 @@ function ve(e, t, n, r = "en-US") {
|
|
|
197
203
|
}).format(Number(i));
|
|
198
204
|
}
|
|
199
205
|
// src/persistence/wallet-db.ts
|
|
200
|
-
var
|
|
206
|
+
var ve = class {
|
|
201
207
|
constructor(e) {
|
|
202
208
|
this.repository = e;
|
|
203
209
|
}
|
|
@@ -246,7 +252,7 @@ var ye = class {
|
|
|
246
252
|
accountId: e,
|
|
247
253
|
tokenIdHex: t.tokenIdHex,
|
|
248
254
|
type: "token",
|
|
249
|
-
addedTime:
|
|
255
|
+
addedTime: E()
|
|
250
256
|
};
|
|
251
257
|
await this.repository.upsertToken(t), await this.saveAsset(n);
|
|
252
258
|
}
|
|
@@ -305,7 +311,7 @@ var ye = class {
|
|
|
305
311
|
async getAccounts() {
|
|
306
312
|
return (await this.repository.getAccounts()).map((e) => ({
|
|
307
313
|
...e,
|
|
308
|
-
type:
|
|
314
|
+
type: 2,
|
|
309
315
|
balance: JSON.parse(e.balance),
|
|
310
316
|
tokensBalance: JSON.parse(e.tokensBalance)
|
|
311
317
|
}));
|
|
@@ -341,7 +347,7 @@ var ye = class {
|
|
|
341
347
|
async getReceiveAddresses() {
|
|
342
348
|
return (await this.repository.getReceiveAddresses()).map((e) => ({
|
|
343
349
|
...e,
|
|
344
|
-
type:
|
|
350
|
+
type: 0,
|
|
345
351
|
balance: JSON.parse(e.balance),
|
|
346
352
|
tokensBalance: JSON.parse(e.tokensBalance)
|
|
347
353
|
}));
|
|
@@ -349,7 +355,7 @@ var ye = class {
|
|
|
349
355
|
async getChangeAddresses() {
|
|
350
356
|
return (await this.repository.getChangeAddresses()).map((e) => ({
|
|
351
357
|
...e,
|
|
352
|
-
type:
|
|
358
|
+
type: 0,
|
|
353
359
|
balance: JSON.parse(e.balance),
|
|
354
360
|
tokensBalance: JSON.parse(e.tokensBalance)
|
|
355
361
|
}));
|
|
@@ -360,7 +366,7 @@ var ye = class {
|
|
|
360
366
|
async getVaults() {
|
|
361
367
|
return (await this.repository.getVaults()).map((e) => ({
|
|
362
368
|
...e,
|
|
363
|
-
type:
|
|
369
|
+
type: 1,
|
|
364
370
|
balance: JSON.parse(e.balance),
|
|
365
371
|
tokensBalance: JSON.parse(e.tokensBalance)
|
|
366
372
|
}));
|
|
@@ -386,7 +392,7 @@ var ye = class {
|
|
|
386
392
|
removeSetting(e) {
|
|
387
393
|
return this.repository.deleteSetting(e);
|
|
388
394
|
}
|
|
389
|
-
},
|
|
395
|
+
}, ye = class {
|
|
390
396
|
constructor(e) {
|
|
391
397
|
this.tokens = /* @__PURE__ */ new Map(), this.nfts = /* @__PURE__ */ new Map(), this.walletDb = e;
|
|
392
398
|
}
|
|
@@ -394,25 +400,34 @@ var ye = class {
|
|
|
394
400
|
this.tokens.clear(), this.nfts.clear();
|
|
395
401
|
}
|
|
396
402
|
async getToken(e) {
|
|
397
|
-
if (e =
|
|
403
|
+
if (e = k(e), this.tokens.has(e)) return this.tokens.get(e);
|
|
398
404
|
let t = await this.walletDb.getToken(e);
|
|
399
405
|
return t && this.tokens.set(e, t), t;
|
|
400
406
|
}
|
|
401
407
|
async getNft(e) {
|
|
402
|
-
if (e =
|
|
408
|
+
if (e = k(e), this.nfts.has(e)) return this.nfts.get(e);
|
|
403
409
|
let t = await this.walletDb.getNft(e);
|
|
404
410
|
return t && this.nfts.set(e, t), t;
|
|
405
411
|
}
|
|
406
412
|
removeToken(e) {
|
|
407
|
-
this.tokens.delete(
|
|
413
|
+
this.tokens.delete(k(e));
|
|
408
414
|
}
|
|
409
415
|
removeNft(e) {
|
|
410
|
-
this.nfts.delete(
|
|
416
|
+
this.nfts.delete(k(e));
|
|
411
417
|
}
|
|
412
|
-
},
|
|
418
|
+
}, be = class e {
|
|
413
419
|
constructor(e) {
|
|
414
420
|
this.walletDb = e;
|
|
415
421
|
}
|
|
422
|
+
onUpdate(e) {
|
|
423
|
+
this.updateCallback = e;
|
|
424
|
+
}
|
|
425
|
+
notify(e) {
|
|
426
|
+
this.updateCallback?.(e);
|
|
427
|
+
}
|
|
428
|
+
isConnected() {
|
|
429
|
+
return this.client?.status === te.CONNECTED;
|
|
430
|
+
}
|
|
416
431
|
getFeatures() {
|
|
417
432
|
return this.execute("server.features");
|
|
418
433
|
}
|
|
@@ -488,13 +503,13 @@ var ye = class {
|
|
|
488
503
|
return this.execute("blockchain.transaction.broadcast", e);
|
|
489
504
|
}
|
|
490
505
|
subscribeHeaders(e) {
|
|
491
|
-
return this.client.subscribe((t) => {
|
|
492
|
-
let n =
|
|
493
|
-
e(typeof n
|
|
506
|
+
return this.client.subscribe(([t]) => {
|
|
507
|
+
let n = t?.height;
|
|
508
|
+
e(typeof n == "number" ? n : 0);
|
|
494
509
|
}, "blockchain.headers.subscribe");
|
|
495
510
|
}
|
|
496
511
|
subscribeAddress(e, t) {
|
|
497
|
-
return this.client.subscribe(t, "blockchain.address.subscribe", e);
|
|
512
|
+
return this.client.subscribe(([e, n]) => t(e, n), "blockchain.address.subscribe", e);
|
|
498
513
|
}
|
|
499
514
|
async getLatency() {
|
|
500
515
|
try {
|
|
@@ -506,18 +521,24 @@ var ye = class {
|
|
|
506
521
|
}
|
|
507
522
|
async connect(e) {
|
|
508
523
|
try {
|
|
509
|
-
e ||= await this.getCurrentInstance(), this.client = new ne(
|
|
524
|
+
await this.disconnect(), e ||= await this.getCurrentInstance(), this.client = new ne({
|
|
525
|
+
application: "com.otoplo.wallet",
|
|
526
|
+
version: "1.4.3",
|
|
527
|
+
host: e.host,
|
|
528
|
+
port: e.port,
|
|
529
|
+
scheme: e.scheme
|
|
530
|
+
}), this.client.on("connected", () => this.notify({ type: "connected" })), this.client.on("disconnected", () => this.notify({ type: "disconnected" })), await this.client.connect();
|
|
510
531
|
} catch (e) {
|
|
511
532
|
throw e instanceof Error ? console.info(e.message) : console.error(e), e;
|
|
512
533
|
}
|
|
513
534
|
}
|
|
514
|
-
async disconnect(
|
|
515
|
-
|
|
516
|
-
return await this.client?.disconnect(e) ?? !1;
|
|
517
|
-
} catch (e) {
|
|
518
|
-
return console.error(e), !1;
|
|
519
|
-
}
|
|
535
|
+
async disconnect() {
|
|
536
|
+
await this.client?.disconnect();
|
|
520
537
|
}
|
|
538
|
+
/**
|
|
539
|
+
* Walk a cursor-paginated rostrum query: start with "start" and follow the returned cursor until it is null.
|
|
540
|
+
* If the cursor expires mid-walk (e.g. reorg / mempool change), the walk is restarted from the beginning.
|
|
541
|
+
*/
|
|
521
542
|
async paginate(e, t, n, r = 3) {
|
|
522
543
|
for (let i = 0;; i++) try {
|
|
523
544
|
let r = [], i = "start";
|
|
@@ -532,16 +553,13 @@ var ye = class {
|
|
|
532
553
|
}
|
|
533
554
|
}
|
|
534
555
|
async execute(e, ...t) {
|
|
535
|
-
await this.waitForConnection();
|
|
536
|
-
let n = await this.client.request(e, ...t);
|
|
537
|
-
if (n instanceof Error) throw n;
|
|
538
|
-
return n;
|
|
556
|
+
return await this.waitForConnection(), this.client.request(e, ...t);
|
|
539
557
|
}
|
|
540
558
|
waitForConnection(e = 5e3) {
|
|
541
559
|
let t = Date.now();
|
|
542
560
|
return new Promise((n, r) => {
|
|
543
561
|
let i = () => {
|
|
544
|
-
if (this.
|
|
562
|
+
if (this.isConnected()) return n();
|
|
545
563
|
if (Date.now() - t > e) return r(/* @__PURE__ */ Error("Rostrum Connection timeout"));
|
|
546
564
|
setTimeout(i, 250);
|
|
547
565
|
};
|
|
@@ -559,7 +577,7 @@ var ye = class {
|
|
|
559
577
|
return t ? JSON.parse(t) : e.getPredefinedInstances()[0];
|
|
560
578
|
}
|
|
561
579
|
static getPredefinedInstances() {
|
|
562
|
-
return
|
|
580
|
+
return w() ? [{
|
|
563
581
|
scheme: b.WSS,
|
|
564
582
|
host: "testnet-electrum.nexa.org",
|
|
565
583
|
port: 30004,
|
|
@@ -578,10 +596,10 @@ var ye = class {
|
|
|
578
596
|
}
|
|
579
597
|
};
|
|
580
598
|
// src/utils/keypath.ts
|
|
581
|
-
function
|
|
599
|
+
function R(e, t, n) {
|
|
582
600
|
return `${e}'/${t}/${n}`;
|
|
583
601
|
}
|
|
584
|
-
function
|
|
602
|
+
function z(e) {
|
|
585
603
|
let t = e.split("/");
|
|
586
604
|
return {
|
|
587
605
|
account: parseInt(t[0].replace("'", "")),
|
|
@@ -590,7 +608,7 @@ function R(e) {
|
|
|
590
608
|
};
|
|
591
609
|
}
|
|
592
610
|
// src/services/key.ts
|
|
593
|
-
var
|
|
611
|
+
var B = class {
|
|
594
612
|
constructor() {
|
|
595
613
|
this.accountKeys = /* @__PURE__ */ new Map(), this.walletKeys = /* @__PURE__ */ new Map();
|
|
596
614
|
}
|
|
@@ -620,16 +638,16 @@ var z = class {
|
|
|
620
638
|
}
|
|
621
639
|
getKey(e) {
|
|
622
640
|
if (!this.seed) throw Error("KeysManager not initialized");
|
|
623
|
-
let t = typeof e == "string" ? e :
|
|
641
|
+
let t = typeof e == "string" ? e : R(e.account, e.type, e.index), n = this.walletKeys.get(t);
|
|
624
642
|
if (!n) {
|
|
625
|
-
let { account: r, type: i, index: a } = typeof e == "string" ?
|
|
643
|
+
let { account: r, type: i, index: a } = typeof e == "string" ? z(e) : e;
|
|
626
644
|
n = this.getAccountKey(r).deriveChild(i, !1).deriveChild(a, !1), this.walletKeys.set(t, n);
|
|
627
645
|
}
|
|
628
646
|
return n;
|
|
629
647
|
}
|
|
630
648
|
};
|
|
631
649
|
// src/utils/asset.ts
|
|
632
|
-
function
|
|
650
|
+
function xe(e, t) {
|
|
633
651
|
return e && typeof e == "string" ? e.startsWith("http") ? e.replace("http://", "https://") : e.startsWith("ipfs://") ? e : `${new URL(t).origin}${e}` : "";
|
|
634
652
|
}
|
|
635
653
|
function Se(e) {
|
|
@@ -645,7 +663,7 @@ function H(e, t) {
|
|
|
645
663
|
return `${U()}/assets/${e}/${t}`;
|
|
646
664
|
}
|
|
647
665
|
function U() {
|
|
648
|
-
return `https://${
|
|
666
|
+
return `https://${w() ? "testapi" : "api"}.otoplo.com`;
|
|
649
667
|
}
|
|
650
668
|
// src/services/asset.ts
|
|
651
669
|
var we = class {
|
|
@@ -722,8 +740,8 @@ var we = class {
|
|
|
722
740
|
let n = Object.keys(t), r = await this.walletDb.getAssets(e, "nft"), i = r?.filter((e) => !n.includes(e.tokenIdHex)) ?? [];
|
|
723
741
|
for (let t of i) await this.deleteNft(e, t.tokenIdHex);
|
|
724
742
|
for (let [n, i] of Object.entries(t)) {
|
|
725
|
-
let t =
|
|
726
|
-
!a && !o || a && o || (!a && o ? await this.deleteNft(e, t) : await this.handleNftReceive(e, t,
|
|
743
|
+
let t = k(n), a = i && BigInt(i.confirmed) + BigInt(i.unconfirmed) > 0n, o = r?.some((e) => e.tokenIdHex == t);
|
|
744
|
+
!a && !o || a && o || (!a && o ? await this.deleteNft(e, t) : await this.handleNftReceive(e, t, E()));
|
|
727
745
|
}
|
|
728
746
|
}
|
|
729
747
|
async saveToken(e, t) {
|
|
@@ -757,7 +775,9 @@ var we = class {
|
|
|
757
775
|
throw Error(`Unexpected Error: ${e}`);
|
|
758
776
|
}
|
|
759
777
|
}
|
|
760
|
-
}
|
|
778
|
+
};
|
|
779
|
+
// src/utils/vault.ts
|
|
780
|
+
const W = 274710;
|
|
761
781
|
function G() {
|
|
762
782
|
return d.empty().add(c.OP_FROMALTSTACK).add(c.OP_DROP).add(c.OP_FROMALTSTACK).add(c.OP_CHECKLOCKTIMEVERIFY).add(c.OP_DROP).add(c.OP_FROMALTSTACK).add(c.OP_CHECKSIGVERIFY);
|
|
763
783
|
}
|
|
@@ -828,17 +848,17 @@ var Ee = class {
|
|
|
828
848
|
address: e.addresses[0],
|
|
829
849
|
nexaAmount: e.value_satoshi.toString(),
|
|
830
850
|
assetId: e.token_id_hex ?? "",
|
|
831
|
-
assetAmount:
|
|
851
|
+
assetAmount: A(e.groupQuantity)
|
|
832
852
|
};
|
|
833
853
|
t.includes(n.address) ? r.push(n) : i.push(n);
|
|
834
854
|
}
|
|
835
855
|
for (let e of n.vout) {
|
|
836
|
-
if (
|
|
856
|
+
if (D(e.scriptPubKey.addresses)) continue;
|
|
837
857
|
let n = {
|
|
838
858
|
address: e.scriptPubKey.addresses[0],
|
|
839
859
|
nexaAmount: e.value_satoshi.toString(),
|
|
840
860
|
assetId: e.scriptPubKey.token_id_hex ?? "",
|
|
841
|
-
assetAmount:
|
|
861
|
+
assetAmount: A(e.scriptPubKey.groupQuantity)
|
|
842
862
|
};
|
|
843
863
|
t.includes(n.address) ? a.push(n) : o.push(n);
|
|
844
864
|
}
|
|
@@ -849,7 +869,7 @@ var Ee = class {
|
|
|
849
869
|
accountId: 0,
|
|
850
870
|
txId: n.txid,
|
|
851
871
|
txIdem: n.txidem,
|
|
852
|
-
time: c ? n.time :
|
|
872
|
+
time: c ? n.time : E(),
|
|
853
873
|
height: c ? n.height : 0,
|
|
854
874
|
type: s,
|
|
855
875
|
fee: n.fee_satoshi.toString(),
|
|
@@ -874,12 +894,12 @@ var Ee = class {
|
|
|
874
894
|
return this.finalizeTransaction(i, Array.from(a.values()));
|
|
875
895
|
}
|
|
876
896
|
prepareTransaction(n, r, i, a) {
|
|
877
|
-
if (!
|
|
897
|
+
if (!T(n) && !T(n, t.PayToPublicKeyHash)) throw Error("Invalid Address.");
|
|
878
898
|
if (i && BigInt(r) < 1n || !i && parseInt(r) < h.DUST_AMOUNT) throw Error("The amount is too low.");
|
|
879
899
|
if (i && BigInt(r) > 9223372036854775807n || !i && parseInt(r) > h.MAX_MONEY) throw Error("The amount is too high.");
|
|
880
900
|
let o = new g();
|
|
881
901
|
if (a && o.addData(a), i) {
|
|
882
|
-
if (!
|
|
902
|
+
if (!T(i, t.GroupIdAddress)) throw Error("Invalid Token ID");
|
|
883
903
|
if (e.getOutputType(n) === 0) throw Error("Token must be sent to script template address");
|
|
884
904
|
o.to(n, h.DUST_AMOUNT, i, BigInt(r));
|
|
885
905
|
} else o.to(n, r);
|
|
@@ -887,7 +907,7 @@ var Ee = class {
|
|
|
887
907
|
}
|
|
888
908
|
async populateNexaInputsAndChange(e, t, n, r) {
|
|
889
909
|
let i = t.filter((e) => BigInt(e.balance.confirmed) + BigInt(e.balance.unconfirmed) > 0n);
|
|
890
|
-
if (
|
|
910
|
+
if (D(i)) throw Error("Not enough Nexa balance.");
|
|
891
911
|
let a = /* @__PURE__ */ new Map(), o = r.isConsolidate ? 0 : Number(e.transaction.outputs.find((e) => e.value > 0n).value);
|
|
892
912
|
for (let t of i) {
|
|
893
913
|
let i = await this.rostrumService.getNexaUtxos(t.address);
|
|
@@ -934,8 +954,8 @@ var Ee = class {
|
|
|
934
954
|
throw Error(JSON.stringify(s));
|
|
935
955
|
}
|
|
936
956
|
async populateTokenInputsAndChange(e, t, n, r, i) {
|
|
937
|
-
let a =
|
|
938
|
-
if (
|
|
957
|
+
let a = k(r), o = t.filter((e) => Object.keys(e.tokensBalance).includes(a));
|
|
958
|
+
if (D(o)) throw Error("Not enough token balance.");
|
|
939
959
|
let s = /* @__PURE__ */ new Map(), c = 0n;
|
|
940
960
|
for (let t of o) {
|
|
941
961
|
let a = await this.rostrumService.getTokenUtxos(t.address, r);
|
|
@@ -1145,10 +1165,10 @@ var je = class {
|
|
|
1145
1165
|
}
|
|
1146
1166
|
});
|
|
1147
1167
|
});
|
|
1148
|
-
t.onSignMessage((e) => r(
|
|
1168
|
+
t.onSignMessage((e) => r("signMessage", e)), t.onAddToken((e) => r("addToken", e)), t.onSendTransaction((e) => r("sendTransaction", e)), t.onSignTransaction((e) => r("signTransaction", e)), t.onGetAccount(() => {
|
|
1149
1169
|
let t = {
|
|
1150
|
-
account:
|
|
1151
|
-
type:
|
|
1170
|
+
account: 2,
|
|
1171
|
+
type: 0,
|
|
1152
1172
|
index: e.id
|
|
1153
1173
|
};
|
|
1154
1174
|
return {
|
|
@@ -1212,7 +1232,7 @@ var je = class {
|
|
|
1212
1232
|
address: e.address,
|
|
1213
1233
|
nexaAmount: i.amount.toString(),
|
|
1214
1234
|
assetId: n,
|
|
1215
|
-
assetAmount:
|
|
1235
|
+
assetAmount: A(i.group_quantity) || i.amount.toString()
|
|
1216
1236
|
}), e.involvedAssets.add(n), r.scriptSig.isEmpty() && e.inputsToSign.add(t);
|
|
1217
1237
|
}
|
|
1218
1238
|
r.scriptSig.findPlaceholder() > 0 && e.inputsToSign.add(t);
|
|
@@ -1221,11 +1241,11 @@ var je = class {
|
|
|
1221
1241
|
for (let t = 0; t < e.txDetails.tx.outputs.length; t++) {
|
|
1222
1242
|
let n = e.txDetails.tx.outputs[t].toObject();
|
|
1223
1243
|
if (n.address !== e.address) continue;
|
|
1224
|
-
let r = n.groupId ?
|
|
1244
|
+
let r = n.groupId ? k(n.groupId) : "NEXA", i = {
|
|
1225
1245
|
address: n.address,
|
|
1226
1246
|
nexaAmount: n.value.toString(),
|
|
1227
1247
|
assetId: r,
|
|
1228
|
-
assetAmount:
|
|
1248
|
+
assetAmount: A(n.groupAmount) || n.value.toString()
|
|
1229
1249
|
};
|
|
1230
1250
|
e.involvedAssets.add(r), e.myOutputs.set(t, i);
|
|
1231
1251
|
}
|
|
@@ -1291,19 +1311,19 @@ var je = class {
|
|
|
1291
1311
|
t !== "NEXA" && (n.asset = await this.assetService.getAssetInfo(t)), n.amount > 0n ? e.txDetails.receive.push(n) : n.amount < 0n && e.txDetails.send.push(n);
|
|
1292
1312
|
}
|
|
1293
1313
|
}
|
|
1294
|
-
}, $ =
|
|
1314
|
+
}, $ = class {
|
|
1295
1315
|
constructor(e, t) {
|
|
1296
1316
|
this.rostrumService = e, this.keyManager = t;
|
|
1297
1317
|
}
|
|
1298
1318
|
async discoverWalletIndex(e, t) {
|
|
1299
1319
|
let n = 0, r = -1, i = [];
|
|
1300
1320
|
for (;;) {
|
|
1301
|
-
let a = await Promise.all(Array.from({ length:
|
|
1302
|
-
if (a.some((e) => e)) r = n, i = a, n +=
|
|
1321
|
+
let a = await Promise.all(Array.from({ length: B.GAP_LIMIT }, (r, i) => this.rostrumService.isAddressUsed(this.keyManager.deriveAddress(e, t, n + i))));
|
|
1322
|
+
if (a.some((e) => e)) r = n, i = a, n += B.GAP_LIMIT;
|
|
1303
1323
|
else break;
|
|
1304
1324
|
}
|
|
1305
1325
|
if (r === -1) return -1;
|
|
1306
|
-
for (let e =
|
|
1326
|
+
for (let e = B.GAP_LIMIT - 1; e >= 0; e--) if (i[e]) return r + e;
|
|
1307
1327
|
return r;
|
|
1308
1328
|
}
|
|
1309
1329
|
async discoverVaults() {
|
|
@@ -1313,13 +1333,17 @@ var je = class {
|
|
|
1313
1333
|
n && t.set(n.address, n);
|
|
1314
1334
|
}), t;
|
|
1315
1335
|
}
|
|
1336
|
+
/**
|
|
1337
|
+
* Scan vault key indexes and ask rostrum for scripts matching the vault template + the key's constraint.
|
|
1338
|
+
* Indexes are checked in parallel batches, stopping after VAULT_GAP_LIMIT consecutive indexes with no scripts.
|
|
1339
|
+
*/
|
|
1316
1340
|
async checkForVaults() {
|
|
1317
1341
|
let e = /* @__PURE__ */ new Set(), t = n.bufferToHex(K()), r = 0;
|
|
1318
|
-
for (let i = 0; r <
|
|
1319
|
-
let a = await Promise.all(Array.from({ length:
|
|
1342
|
+
for (let i = 0; r < 5; i += 5) {
|
|
1343
|
+
let a = await Promise.all(Array.from({ length: 5 }, (e, r) => {
|
|
1320
1344
|
let a = this.keyManager.getKey({
|
|
1321
|
-
account:
|
|
1322
|
-
type:
|
|
1345
|
+
account: 1,
|
|
1346
|
+
type: 0,
|
|
1323
1347
|
index: i + r
|
|
1324
1348
|
}), o = n.bufferToHex(J(a.publicKey));
|
|
1325
1349
|
return this.rostrumService.listTemplateScripts(t, o);
|
|
@@ -1331,8 +1355,8 @@ var je = class {
|
|
|
1331
1355
|
parseVaultDetails(t) {
|
|
1332
1356
|
let n = d.fromHex(t), i = new r().writeVarLengthBuf(n.toBuffer()).toBuffer(), a = new e(i).toString(), o = n.getVisibleArgs(), s = Number(f.decode(o.chunks[0].buf)), c = p.isSmallIntOp(o.chunks[1].opcodenum) ? p.decodeOP_N(o.chunks[1].opcodenum) : Number(f.decode(o.chunks[1].buf)), l = [s, c];
|
|
1333
1357
|
if (a == X(this.keyManager.getKey({
|
|
1334
|
-
account:
|
|
1335
|
-
type:
|
|
1358
|
+
account: 1,
|
|
1359
|
+
type: 0,
|
|
1336
1360
|
index: c
|
|
1337
1361
|
}).publicKey, l)) return {
|
|
1338
1362
|
address: a,
|
|
@@ -1345,25 +1369,25 @@ var je = class {
|
|
|
1345
1369
|
},
|
|
1346
1370
|
tokensBalance: {},
|
|
1347
1371
|
height: 0,
|
|
1348
|
-
type:
|
|
1372
|
+
type: 1
|
|
1349
1373
|
};
|
|
1350
1374
|
}
|
|
1351
|
-
},
|
|
1375
|
+
}, Ne = class e {
|
|
1352
1376
|
static {
|
|
1353
|
-
this.DEBOUNCE_MS =
|
|
1377
|
+
this.DEBOUNCE_MS = 1e3;
|
|
1354
1378
|
}
|
|
1355
1379
|
constructor(e, t, n, r, i, a) {
|
|
1356
|
-
this.keyManager = e, this.walletDb = t, this.rostrumService = n, this.assetService = r, this.transactionService = i, this.sessionManager = a, this.isConnected = !1, this.isSubscribed = !1, this.onSubscribeEvent = (e) => {
|
|
1357
|
-
if (
|
|
1358
|
-
let
|
|
1359
|
-
|
|
1360
|
-
address:
|
|
1361
|
-
result:
|
|
1380
|
+
this.keyManager = e, this.walletDb = t, this.rostrumService = n, this.assetService = r, this.transactionService = i, this.sessionManager = a, this.updatesStarted = !1, this.isConnected = !1, this.isSubscribed = !1, this.onSubscribeEvent = (e, t) => {
|
|
1381
|
+
if (typeof t != "string") return;
|
|
1382
|
+
let n = this.getAllAddresses().find((t) => t.address === e);
|
|
1383
|
+
n && n.statusHash !== t && this.registerUpdate({
|
|
1384
|
+
address: n,
|
|
1385
|
+
result: t
|
|
1362
1386
|
});
|
|
1363
|
-
}, this.discoveryService = new
|
|
1387
|
+
}, this.discoveryService = new $(this.rostrumService, this.keyManager), this.accounts = /* @__PURE__ */ new Map(), this.accountsAddressToId = /* @__PURE__ */ new Map(), this.receiveAddresses = [], this.changeAddresses = [], this.vaults = /* @__PURE__ */ new Map(), this.pendingUpdates = /* @__PURE__ */ new Map();
|
|
1364
1388
|
}
|
|
1365
1389
|
clear() {
|
|
1366
|
-
this.accounts.clear(), this.accountsAddressToId.clear(), this.receiveAddresses = [], this.changeAddresses = [], this.vaults.clear(), this.
|
|
1390
|
+
this.accounts.clear(), this.accountsAddressToId.clear(), this.receiveAddresses = [], this.changeAddresses = [], this.vaults.clear(), this.stopUpdates(), this.notify({ type: "clear_wallet" });
|
|
1367
1391
|
}
|
|
1368
1392
|
onUpdate(e) {
|
|
1369
1393
|
this.updateCallback = e;
|
|
@@ -1393,20 +1417,20 @@ var je = class {
|
|
|
1393
1417
|
let t = this.accounts.get(e);
|
|
1394
1418
|
return [{
|
|
1395
1419
|
address: t.address,
|
|
1396
|
-
keyPath:
|
|
1420
|
+
keyPath: R(2, 0, t.id),
|
|
1397
1421
|
balance: t.balance,
|
|
1398
1422
|
tokensBalance: t.tokensBalance
|
|
1399
1423
|
}];
|
|
1400
1424
|
}
|
|
1401
1425
|
return this.getMainAddresses().filter((e) => e.used).map((e) => ({
|
|
1402
1426
|
address: e.address,
|
|
1403
|
-
keyPath:
|
|
1427
|
+
keyPath: R(0, e.space, e.idx),
|
|
1404
1428
|
balance: e.balance,
|
|
1405
1429
|
tokensBalance: e.tokensBalance
|
|
1406
1430
|
}));
|
|
1407
1431
|
}
|
|
1408
1432
|
async reloadRostrum(e) {
|
|
1409
|
-
e && await this.rostrumService.setCurrentInstance(e), await this.disconnectRostrum(), await this.initRostrum(), await this.subscribeAllAddresses();
|
|
1433
|
+
e && await this.rostrumService.setCurrentInstance(e), await this.disconnectRostrum(), await this.initRostrum(), this.startUpdates(), await this.subscribeAllAddresses();
|
|
1410
1434
|
}
|
|
1411
1435
|
async initRostrum() {
|
|
1412
1436
|
this.isConnected ||= (await this.rostrumService.connect(), await this.rostrumService.subscribeHeaders((e) => this.notify({
|
|
@@ -1415,16 +1439,16 @@ var je = class {
|
|
|
1415
1439
|
})), !0);
|
|
1416
1440
|
}
|
|
1417
1441
|
async disconnectRostrum() {
|
|
1418
|
-
await this.rostrumService.disconnect(
|
|
1442
|
+
await this.rostrumService.disconnect(), this.isConnected = !1, this.isSubscribed = !1, this.stopUpdates();
|
|
1419
1443
|
}
|
|
1420
1444
|
async discoverWallet() {
|
|
1421
|
-
let e = this.discoveryService.discoverWalletIndex(
|
|
1445
|
+
let e = this.discoveryService.discoverWalletIndex(0, 0), t = this.discoveryService.discoverWalletIndex(0, 1), n = this.discoveryService.discoverWalletIndex(2, 0), r = this.discoveryService.discoverVaults(), [i, a, o, s] = await Promise.all([
|
|
1422
1446
|
e,
|
|
1423
1447
|
t,
|
|
1424
1448
|
n,
|
|
1425
1449
|
r
|
|
1426
1450
|
]);
|
|
1427
|
-
this.receiveAddresses = this.deriveAddresses(
|
|
1451
|
+
this.receiveAddresses = this.deriveAddresses(0, 0, i + B.GAP_LIMIT + 1), this.changeAddresses = this.deriveAddresses(1, 0, a + B.GAP_LIMIT + 1), this.accounts = this.deriveAccounts(0, o < 0 ? 1 : o + 1), this.accounts.forEach((e) => this.accountsAddressToId.set(e.address, e.id)), this.vaults = s, await this.saveAddresses(this.receiveAddresses), await this.saveAddresses(this.changeAddresses), await this.saveAccounts(this.accounts), await this.saveVaults(this.vaults), this.initState();
|
|
1428
1452
|
}
|
|
1429
1453
|
async loadWallet() {
|
|
1430
1454
|
this.receiveAddresses = await this.walletDb.getReceiveAddresses(), this.changeAddresses = await this.walletDb.getChangeAddresses();
|
|
@@ -1433,14 +1457,18 @@ var je = class {
|
|
|
1433
1457
|
let t = await this.walletDb.getVaults();
|
|
1434
1458
|
this.vaults = new Map(t.map((e) => [e.address, e])), this.initState();
|
|
1435
1459
|
}
|
|
1436
|
-
|
|
1437
|
-
|
|
1460
|
+
/**
|
|
1461
|
+
* First sync after discovery: subscriptions only queue updates, which are then processed
|
|
1462
|
+
* in one batch before the update loop starts.
|
|
1463
|
+
*/
|
|
1464
|
+
async initialSync() {
|
|
1465
|
+
await this.subscribeAllAddresses(), await this.processPendingUpdates(!0), this.startUpdates();
|
|
1438
1466
|
}
|
|
1439
1467
|
reconnectSync() {
|
|
1440
|
-
return Promise.all([this.sessionManager.reload(this.accounts), this.subscribeAllAddresses()]);
|
|
1468
|
+
return this.startUpdates(), Promise.all([this.sessionManager.reload(this.accounts), this.subscribeAllAddresses()]);
|
|
1441
1469
|
}
|
|
1442
|
-
async subscribeAllAddresses(
|
|
1443
|
-
this.isSubscribed || (this.isSubscribed = !0, await this.subscribeAddresses(this.getAllAddresses()
|
|
1470
|
+
async subscribeAllAddresses() {
|
|
1471
|
+
this.isSubscribed || (this.isSubscribed = !0, await this.subscribeAddresses(this.getAllAddresses()));
|
|
1444
1472
|
}
|
|
1445
1473
|
initState() {
|
|
1446
1474
|
let e = this.getMainAddresses();
|
|
@@ -1450,8 +1478,8 @@ var je = class {
|
|
|
1450
1478
|
id: -1,
|
|
1451
1479
|
name: "HD Wallet",
|
|
1452
1480
|
address: this.getReceiveAddress(),
|
|
1453
|
-
balance:
|
|
1454
|
-
tokensBalance:
|
|
1481
|
+
balance: j(e.map((e) => e.balance)),
|
|
1482
|
+
tokensBalance: M(e.map((e) => e.tokensBalance)),
|
|
1455
1483
|
sessions: {}
|
|
1456
1484
|
}
|
|
1457
1485
|
});
|
|
@@ -1480,7 +1508,7 @@ var je = class {
|
|
|
1480
1508
|
let r = [];
|
|
1481
1509
|
for (let i = t; i < t + n; i++) {
|
|
1482
1510
|
let t = {
|
|
1483
|
-
address: this.keyManager.deriveAddress(
|
|
1511
|
+
address: this.keyManager.deriveAddress(0, e, i),
|
|
1484
1512
|
space: e,
|
|
1485
1513
|
idx: i,
|
|
1486
1514
|
used: !1,
|
|
@@ -1491,7 +1519,7 @@ var je = class {
|
|
|
1491
1519
|
unconfirmed: "0"
|
|
1492
1520
|
},
|
|
1493
1521
|
tokensBalance: {},
|
|
1494
|
-
type:
|
|
1522
|
+
type: 0
|
|
1495
1523
|
};
|
|
1496
1524
|
r.push(t);
|
|
1497
1525
|
}
|
|
@@ -1503,7 +1531,7 @@ var je = class {
|
|
|
1503
1531
|
let e = {
|
|
1504
1532
|
id: r,
|
|
1505
1533
|
name: `Account ${r + 1}`,
|
|
1506
|
-
address: this.keyManager.deriveAddress(
|
|
1534
|
+
address: this.keyManager.deriveAddress(2, 0, r),
|
|
1507
1535
|
height: 0,
|
|
1508
1536
|
hidden: 0,
|
|
1509
1537
|
statusHash: "",
|
|
@@ -1512,7 +1540,7 @@ var je = class {
|
|
|
1512
1540
|
unconfirmed: "0"
|
|
1513
1541
|
},
|
|
1514
1542
|
tokensBalance: {},
|
|
1515
|
-
type:
|
|
1543
|
+
type: 2
|
|
1516
1544
|
};
|
|
1517
1545
|
n.set(e.id, e);
|
|
1518
1546
|
}
|
|
@@ -1527,66 +1555,50 @@ var je = class {
|
|
|
1527
1555
|
async saveVaults(e) {
|
|
1528
1556
|
for (let t of e.values()) await this.walletDb.saveVault(t);
|
|
1529
1557
|
}
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
});
|
|
1540
|
-
if (r.push(t), !this.discoveryResolver) {
|
|
1541
|
-
let e = new Promise((e) => {
|
|
1542
|
-
this.discoveryResolver = e;
|
|
1543
|
-
});
|
|
1544
|
-
r.push(e);
|
|
1545
|
-
}
|
|
1546
|
-
}
|
|
1547
|
-
this.registerUpdate({
|
|
1548
|
-
address: e,
|
|
1549
|
-
result: n
|
|
1550
|
-
});
|
|
1551
|
-
}
|
|
1552
|
-
await Promise.all(r);
|
|
1558
|
+
/**
|
|
1559
|
+
* The subscribe callback (onSubscribeEvent) receives each address's initial status,
|
|
1560
|
+
* so changed addresses are registered for update from there.
|
|
1561
|
+
*/
|
|
1562
|
+
async subscribeAddresses(e) {
|
|
1563
|
+
await Promise.all(e.map((e) => this.rostrumService.subscribeAddress(e.address, this.onSubscribeEvent)));
|
|
1564
|
+
}
|
|
1565
|
+
registerUpdate(e) {
|
|
1566
|
+
this.pendingUpdates.set(e.address.address, e), this.updatesStarted && this.scheduleUpdates();
|
|
1553
1567
|
}
|
|
1554
|
-
|
|
1555
|
-
|
|
1568
|
+
scheduleUpdates() {
|
|
1569
|
+
clearTimeout(this.updateTimer), this.updateTimer = setTimeout(() => {
|
|
1556
1570
|
this.processPendingUpdates();
|
|
1557
1571
|
}, e.DEBOUNCE_MS);
|
|
1558
1572
|
}
|
|
1559
|
-
|
|
1573
|
+
startUpdates() {
|
|
1574
|
+
this.updatesStarted || (this.updatesStarted = !0, this.pendingUpdates.size > 0 && this.scheduleUpdates());
|
|
1575
|
+
}
|
|
1576
|
+
stopUpdates() {
|
|
1577
|
+
this.updatesStarted = !1, clearTimeout(this.updateTimer), this.updateTimer = void 0, this.pendingUpdates.clear();
|
|
1578
|
+
}
|
|
1579
|
+
async processPendingUpdates(e = !1) {
|
|
1560
1580
|
if (this.pendingUpdates.size !== 0) try {
|
|
1561
1581
|
this.notify({
|
|
1562
1582
|
type: "sync_wallet",
|
|
1563
1583
|
loading: !0
|
|
1564
1584
|
}), console.log(`Processing ${this.pendingUpdates.size} pending updates...`);
|
|
1565
|
-
let
|
|
1585
|
+
let t = Array.from(this.pendingUpdates);
|
|
1566
1586
|
this.pendingUpdates.clear();
|
|
1567
|
-
let
|
|
1568
|
-
for (let [,
|
|
1569
|
-
let
|
|
1570
|
-
|
|
1571
|
-
} else if (this.isVaultAddress(
|
|
1572
|
-
let
|
|
1573
|
-
|
|
1587
|
+
let n = [], r = [], i = [];
|
|
1588
|
+
for (let [, e] of t) if (this.isAccountAddress(e.address)) {
|
|
1589
|
+
let t = this.fetchAndUpdateAccount(e.address, e.result);
|
|
1590
|
+
n.push(t);
|
|
1591
|
+
} else if (this.isVaultAddress(e.address)) {
|
|
1592
|
+
let t = this.fetchAndUpdateVault(e.address, e.result);
|
|
1593
|
+
r.push(t);
|
|
1574
1594
|
} else {
|
|
1575
|
-
let
|
|
1576
|
-
|
|
1595
|
+
let t = this.fetchAndUpdateAddress(e.address, e.result);
|
|
1596
|
+
i.push(t);
|
|
1577
1597
|
}
|
|
1578
|
-
let
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
e && (e(), this.addressResolvers.delete(t));
|
|
1583
|
-
}
|
|
1584
|
-
i = this.addressResolvers.size === 0;
|
|
1585
|
-
}
|
|
1586
|
-
let a = await Promise.all(t);
|
|
1587
|
-
await Promise.all(n);
|
|
1588
|
-
let o = await Promise.all(r), s = [this.postProcessWalletUpdate(o), this.postProcessAccountsUpdate(a)];
|
|
1589
|
-
i && s.push(this.recoverTokens()), await Promise.all(s), i && this.discoveryResolver && (this.discoveryResolver(), this.discoveryResolver = void 0);
|
|
1598
|
+
let a = await Promise.all(n);
|
|
1599
|
+
await Promise.all(r);
|
|
1600
|
+
let o = await Promise.all(i), s = [this.postProcessWalletUpdate(o), this.postProcessAccountsUpdate(a)];
|
|
1601
|
+
e && s.push(this.recoverTokens()), await Promise.all(s);
|
|
1590
1602
|
} catch (e) {
|
|
1591
1603
|
console.error("Error processing pending updates:", e);
|
|
1592
1604
|
} finally {
|
|
@@ -1601,8 +1613,8 @@ var je = class {
|
|
|
1601
1613
|
let t = /* @__PURE__ */ new Map();
|
|
1602
1614
|
for (let { txs: n } of e) for (let e of n) t.set(e.tx_hash, e);
|
|
1603
1615
|
let n = Array.from(t.values());
|
|
1604
|
-
await this.checkGapLimit(
|
|
1605
|
-
let r = this.getMainAddresses(), i =
|
|
1616
|
+
await this.checkGapLimit(0), await this.checkGapLimit(1);
|
|
1617
|
+
let r = this.getMainAddresses(), i = j(r.map((e) => e.balance)), a = M(r.map((e) => e.tokensBalance));
|
|
1606
1618
|
this.notify({
|
|
1607
1619
|
type: "account_balance_updated",
|
|
1608
1620
|
accountId: -1,
|
|
@@ -1635,25 +1647,25 @@ var je = class {
|
|
|
1635
1647
|
i.push(a), await Promise.all(i);
|
|
1636
1648
|
}
|
|
1637
1649
|
async fetchAndUpdateAddress(e, t) {
|
|
1638
|
-
e.
|
|
1650
|
+
e.balance = await this.rostrumService.getBalance(e.address), e.tokensBalance = await this.rostrumService.getTokensBalance(e.address);
|
|
1639
1651
|
let n = await this.transactionService.fetchTransactionsHistory(e.address, e.height);
|
|
1640
|
-
return e.height = n.lastHeight, e.used ||= n.txs.length > 0, await this.walletDb.saveAddress(e), {
|
|
1652
|
+
return e.height = n.lastHeight, e.used ||= n.txs.length > 0, e.statusHash = t, await this.walletDb.saveAddress(e), {
|
|
1641
1653
|
address: e,
|
|
1642
1654
|
txs: n.txs
|
|
1643
1655
|
};
|
|
1644
1656
|
}
|
|
1645
1657
|
async fetchAndUpdateAccount(e, t) {
|
|
1646
|
-
e.
|
|
1658
|
+
e.balance = await this.rostrumService.getBalance(e.address), e.tokensBalance = await this.rostrumService.getTokensBalance(e.address);
|
|
1647
1659
|
let n = await this.transactionService.fetchTransactionsHistory(e.address, e.height);
|
|
1648
|
-
return e.height = n.lastHeight, await this.walletDb.saveAccount(e), {
|
|
1660
|
+
return e.height = n.lastHeight, e.statusHash = t, await this.walletDb.saveAccount(e), {
|
|
1649
1661
|
address: e,
|
|
1650
1662
|
txs: n.txs
|
|
1651
1663
|
};
|
|
1652
1664
|
}
|
|
1653
1665
|
async fetchAndUpdateVault(e, t) {
|
|
1654
|
-
e.
|
|
1666
|
+
e.balance = await this.rostrumService.getBalance(e.address);
|
|
1655
1667
|
let n = await this.transactionService.fetchTransactionsHistory(e.address, e.height);
|
|
1656
|
-
return e.height = n.lastHeight, await this.walletDb.saveVault(e), this.notify({
|
|
1668
|
+
return e.height = n.lastHeight, e.statusHash = t, await this.walletDb.saveVault(e), this.notify({
|
|
1657
1669
|
type: "vault_balance_updated",
|
|
1658
1670
|
address: e.address,
|
|
1659
1671
|
balance: e.balance
|
|
@@ -1663,16 +1675,16 @@ var je = class {
|
|
|
1663
1675
|
};
|
|
1664
1676
|
}
|
|
1665
1677
|
async checkGapLimit(e) {
|
|
1666
|
-
let t = e ===
|
|
1667
|
-
if (r <
|
|
1668
|
-
let i =
|
|
1678
|
+
let t = e === 0 ? this.receiveAddresses : this.changeAddresses, n = e === 0 ? "Receive" : "Change", r = t.filter((e) => !e.used).length;
|
|
1679
|
+
if (r < B.GAP_LIMIT) {
|
|
1680
|
+
let i = B.GAP_LIMIT - r, a = t[t.length - 1].idx;
|
|
1669
1681
|
console.log(`Deriving ${i} more ${n} addresses...`);
|
|
1670
1682
|
let o = this.deriveAddresses(e, a + 1, i);
|
|
1671
|
-
e ===
|
|
1683
|
+
e === 0 ? this.receiveAddresses.push(...o) : this.changeAddresses.push(...o), await this.saveAddresses(o), await this.subscribeAddresses(o);
|
|
1672
1684
|
}
|
|
1673
1685
|
}
|
|
1674
1686
|
async addNewAccount(e, t) {
|
|
1675
|
-
let n = this.keyManager.deriveAddress(
|
|
1687
|
+
let n = this.keyManager.deriveAddress(2, 0, e), r = {
|
|
1676
1688
|
id: e,
|
|
1677
1689
|
name: t,
|
|
1678
1690
|
address: n,
|
|
@@ -1684,7 +1696,7 @@ var je = class {
|
|
|
1684
1696
|
unconfirmed: "0"
|
|
1685
1697
|
},
|
|
1686
1698
|
tokensBalance: {},
|
|
1687
|
-
type:
|
|
1699
|
+
type: 2
|
|
1688
1700
|
};
|
|
1689
1701
|
this.accounts.set(e, r), this.accountsAddressToId.set(n, e), await this.walletDb.saveAccount(r), this.notify({
|
|
1690
1702
|
type: "new_account",
|
|
@@ -1741,7 +1753,7 @@ var je = class {
|
|
|
1741
1753
|
unconfirmed: "0"
|
|
1742
1754
|
},
|
|
1743
1755
|
tokensBalance: {},
|
|
1744
|
-
type:
|
|
1756
|
+
type: 1
|
|
1745
1757
|
};
|
|
1746
1758
|
await this.addVault(r);
|
|
1747
1759
|
}
|
|
@@ -1763,20 +1775,20 @@ var je = class {
|
|
|
1763
1775
|
}
|
|
1764
1776
|
async recoverTokens() {
|
|
1765
1777
|
let e = [];
|
|
1766
|
-
e.push(this.assetService.syncTokens(-1,
|
|
1778
|
+
e.push(this.assetService.syncTokens(-1, M(this.getMainAddresses().map((e) => e.tokensBalance))));
|
|
1767
1779
|
for (let t of this.accounts.values()) e.push(this.assetService.syncTokens(t.id, t.tokensBalance));
|
|
1768
1780
|
await Promise.all(e);
|
|
1769
1781
|
}
|
|
1770
1782
|
isVaultAddress(e) {
|
|
1771
|
-
return e.type ==
|
|
1783
|
+
return e.type == 1;
|
|
1772
1784
|
}
|
|
1773
1785
|
isAccountAddress(e) {
|
|
1774
|
-
return e.type ==
|
|
1786
|
+
return e.type == 2;
|
|
1775
1787
|
}
|
|
1776
1788
|
isMainAddress(e) {
|
|
1777
|
-
return e.type ==
|
|
1789
|
+
return e.type == 0;
|
|
1778
1790
|
}
|
|
1779
|
-
},
|
|
1791
|
+
}, Pe = class e {
|
|
1780
1792
|
static {
|
|
1781
1793
|
this.POLL_INTERVAL_MS = 12e4;
|
|
1782
1794
|
}
|
|
@@ -1798,7 +1810,7 @@ var je = class {
|
|
|
1798
1810
|
async init() {
|
|
1799
1811
|
this.stop();
|
|
1800
1812
|
let t = await this.walletDb.getSetting(e.CURRENCY_SETTING);
|
|
1801
|
-
this.currency =
|
|
1813
|
+
this.currency = C.includes(t) ? t : "usd", this.notify({
|
|
1802
1814
|
type: "currency_updated",
|
|
1803
1815
|
currency: this.currency
|
|
1804
1816
|
}), this.running = !0, this.poll();
|
|
@@ -1857,6 +1869,6 @@ var je = class {
|
|
|
1857
1869
|
}), a;
|
|
1858
1870
|
}
|
|
1859
1871
|
};
|
|
1860
|
-
export {
|
|
1872
|
+
export { se as AccountType, we as AssetService, S as CURRENCIES, ue as HD_WALLET_ID, B as KeyManager, ce as KeySpace, de as MAX_INT64, Pe as MarketService, be as RostrumService, je as SessionManager, le as SessionRequestType, Ee as TransactionService, Me as TransactionTransformer, W as VAULT_FIRST_BLOCK, ye as WalletCache, ve as WalletDB, Ne as WalletManager, _e as calculateFiatValue, he as capitalizeFirstLetter, C as currencyCodes, E as currentTimestamp, Oe as encryptMnemonic, Te as estimateDateByFutureBlock, De as generateNewMnemonic, X as generateVaultAddress, q as generateVaultConstraint, Y as generateVaultVisibleArgs, O as getAddressBuffer, H as getAssetFileUrl, V as getAssetMetadataUrl, pe as getExplorerUrl, P as getFileMediaType, F as getFileMimeType, J as getVaultConstraintHash, G as getVaultTemplate, K as getVaultTemplateHash, fe as isGenesisHashValid, Z as isMnemonicValid, D as isNullOrEmpty, w as isTestnet, T as isValidNexaAddress, R as keyPathToString, Se as parseTokenDataUrl, L as prettifyAmount, I as sleep, z as stringToKeyPath, j as sumBalance, M as sumTokensBalance, A as tokenAmountToAssetAmount, ge as tokenHexToAddr, k as tokenIdToHex, xe as transformTokenIconUrl, me as truncateStringMiddle, Ae as validateAndDecryptMnemonic };
|
|
1861
1873
|
|
|
1862
1874
|
//# sourceMappingURL=index.js.map
|