@otoplo/wallet-common 0.4.1 → 0.4.3

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.js CHANGED
@@ -11,50 +11,83 @@ var S = /* @__PURE__ */ function(e) {
11
11
  return e[e.RECEIVE = 0] = "RECEIVE", e[e.CHANGE = 1] = "CHANGE", e;
12
12
  }({}), w = /* @__PURE__ */ function(e) {
13
13
  return e.SignMessage = "signMessage", e.AddToken = "addToken", e.SignTransaction = "signTransaction", e.SendTransaction = "sendTransaction", e;
14
- }({}), se = -1, ce = 9223372036854775807n, T = 274710, le = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76";
15
- function E() {
14
+ }({}), se = -1, ce = 9223372036854775807n, T = {
15
+ usd: {
16
+ symbol: "$",
17
+ name: "US Dollar"
18
+ },
19
+ eur: {
20
+ symbol: "€",
21
+ name: "Euro"
22
+ },
23
+ gbp: {
24
+ symbol: "£",
25
+ name: "British Pound"
26
+ },
27
+ aud: {
28
+ symbol: "A$",
29
+ name: "Australian Dollar"
30
+ },
31
+ cad: {
32
+ symbol: "CA$",
33
+ name: "Canadian Dollar"
34
+ },
35
+ chf: {
36
+ symbol: "CHF",
37
+ name: "Swiss Franc"
38
+ },
39
+ inr: {
40
+ symbol: "₹",
41
+ name: "Indian Rupee"
42
+ },
43
+ cny: {
44
+ symbol: "CN¥",
45
+ name: "Chinese Yuan"
46
+ },
47
+ jpy: {
48
+ symbol: "¥",
49
+ name: "Japanese Yen"
50
+ }
51
+ }, E = Object.keys(T);
52
+ function D() {
16
53
  return c.defaultNetwork == c.testnet;
17
54
  }
18
- function ue(e) {
19
- return E() ? e == "508c843a4b98fb25f57cf9ebafb245a5c16468f06519cdd467059a91e7b79d52" : e == "edc7144fe1ba4edd0edf35d7eea90f6cb1dba42314aa85da8207e97c5339c801";
55
+ function le(e) {
56
+ return D() ? e == "508c843a4b98fb25f57cf9ebafb245a5c16468f06519cdd467059a91e7b79d52" : e == "edc7144fe1ba4edd0edf35d7eea90f6cb1dba42314aa85da8207e97c5339c801";
20
57
  }
21
- function D(n, r = t.PayToScriptTemplate) {
58
+ function O(n, r = t.PayToScriptTemplate) {
22
59
  return e.isValid(n, c.defaultNetwork, r);
23
60
  }
24
- function de() {
25
- return `https://${E() ? "testnet-" : ""}explorer.nexa.org`;
61
+ function ue() {
62
+ return `https://${D() ? "testnet-" : ""}explorer.nexa.org`;
26
63
  }
27
- function O() {
64
+ function k() {
28
65
  return Math.floor(Date.now() / 1e3);
29
66
  }
30
- function fe(e, t) {
31
- let n = (t - e) * 2, r = /* @__PURE__ */ new Date();
32
- return r.setMinutes(r.getMinutes() + n), r.toLocaleDateString();
33
- }
34
- function k(e) {
67
+ function A(e) {
35
68
  return !e || e.length === 0;
36
69
  }
37
- function pe(e, t = 0) {
70
+ function de(e, t = 0) {
38
71
  if (!e || e.length <= t) return e || "";
39
72
  let n = Math.floor((t - 3) / 2), r = e.slice(0, n), i = e.slice(e.length - n);
40
73
  return r + "..." + i;
41
74
  }
42
- function me(e) {
75
+ function fe(e) {
43
76
  return e.length === 0 ? "" : e.charAt(0).toUpperCase() + e.substring(1);
44
77
  }
45
- function A(t) {
78
+ function j(t) {
46
79
  return a.isHexa(t) ? r.hexToBuffer(t) : e.fromString(t).data;
47
80
  }
48
- function j(e) {
49
- return a.isHexa(e) ? e : r.bufferToHex(A(e));
81
+ function M(e) {
82
+ return a.isHexa(e) ? e : r.bufferToHex(j(e));
50
83
  }
51
- function he(n) {
52
- return a.isHexa(n) ? new e(A(n), c.defaultNetwork, t.GroupIdAddress).toString() : n;
84
+ function pe(n) {
85
+ return a.isHexa(n) ? new e(j(n), c.defaultNetwork, t.GroupIdAddress).toString() : n;
53
86
  }
54
- function M(e) {
87
+ function N(e) {
55
88
  return e == null || e === "" ? "" : BigInt(e) <= 0n ? "0" : e.toString();
56
89
  }
57
- function N(e) {
90
+ function P(e) {
58
91
  let t = BigInt(0), n = BigInt(0);
59
92
  return e.forEach((e) => {
60
93
  t += BigInt(e.confirmed), n += BigInt(e.unconfirmed);
@@ -63,7 +96,7 @@ function N(e) {
63
96
  unconfirmed: n.toString()
64
97
  };
65
98
  }
66
- function P(e) {
99
+ function F(e) {
67
100
  let t = {};
68
101
  return e.forEach((e) => {
69
102
  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] = {
@@ -72,7 +105,7 @@ function P(e) {
72
105
  };
73
106
  }), t;
74
107
  }
75
- var F = {
108
+ var I = {
76
109
  ".gif": {
77
110
  media: "image",
78
111
  mime: "image/gif"
@@ -142,23 +175,29 @@ var F = {
142
175
  mime: "audio/mp4"
143
176
  }
144
177
  };
145
- function ge(e) {
178
+ function me(e) {
146
179
  let t = e.lastIndexOf(".");
147
- return t === -1 ? "unknown" : F[e.slice(t).toLowerCase()]?.media ?? "unknown";
180
+ return t === -1 ? "unknown" : I[e.slice(t).toLowerCase()]?.media ?? "unknown";
148
181
  }
149
- function _e(e) {
182
+ function he(e) {
150
183
  let t = e.lastIndexOf(".");
151
- return t === -1 ? "application/octet-stream" : F[e.slice(t).toLowerCase()]?.mime ?? "application/octet-stream";
184
+ return t === -1 ? "application/octet-stream" : I[e.slice(t).toLowerCase()]?.mime ?? "application/octet-stream";
152
185
  }
153
- function ve(e) {
186
+ function ge(e) {
154
187
  return new Promise((t) => setTimeout(t, e));
155
188
  }
156
- function ye(e) {
157
- let t = y.getPrettyValue(e);
158
- return t.match(/\./) && (t = t.replace(/\.?0+$/, "")), t;
189
+ function L(e) {
190
+ return new y(e).stripTrailingZero().getPrettyValue();
191
+ }
192
+ function _e(e, t, n, r = "en-US") {
193
+ let i = y.multiply(e, t);
194
+ return new Intl.NumberFormat(r, {
195
+ style: "currency",
196
+ currency: n
197
+ }).format(Number(i));
159
198
  }
160
199
  // src/persistence/wallet-db.ts
161
- var be = class {
200
+ var ve = class {
162
201
  constructor(e) {
163
202
  this.repository = e;
164
203
  }
@@ -207,7 +246,7 @@ var be = class {
207
246
  accountId: e,
208
247
  tokenIdHex: t.tokenIdHex,
209
248
  type: "token",
210
- addedTime: O()
249
+ addedTime: k()
211
250
  };
212
251
  await this.repository.upsertToken(t), await this.saveAsset(n);
213
252
  }
@@ -347,7 +386,7 @@ var be = class {
347
386
  removeSetting(e) {
348
387
  return this.repository.deleteSetting(e);
349
388
  }
350
- }, xe = class {
389
+ }, ye = class {
351
390
  constructor(e) {
352
391
  this.tokens = /* @__PURE__ */ new Map(), this.nfts = /* @__PURE__ */ new Map(), this.walletDb = e;
353
392
  }
@@ -355,22 +394,22 @@ var be = class {
355
394
  this.tokens.clear(), this.nfts.clear();
356
395
  }
357
396
  async getToken(e) {
358
- if (e = j(e), this.tokens.has(e)) return this.tokens.get(e);
397
+ if (e = M(e), this.tokens.has(e)) return this.tokens.get(e);
359
398
  let t = await this.walletDb.getToken(e);
360
399
  return t && this.tokens.set(e, t), t;
361
400
  }
362
401
  async getNft(e) {
363
- if (e = j(e), this.nfts.has(e)) return this.nfts.get(e);
402
+ if (e = M(e), this.nfts.has(e)) return this.nfts.get(e);
364
403
  let t = await this.walletDb.getNft(e);
365
404
  return t && this.nfts.set(e, t), t;
366
405
  }
367
406
  removeToken(e) {
368
- this.tokens.delete(j(e));
407
+ this.tokens.delete(M(e));
369
408
  }
370
409
  removeNft(e) {
371
- this.nfts.delete(j(e));
410
+ this.nfts.delete(M(e));
372
411
  }
373
- }, Se = class e {
412
+ }, be = class e {
374
413
  constructor(e) {
375
414
  this.walletDb = e;
376
415
  }
@@ -487,7 +526,7 @@ var be = class {
487
526
  return t ? JSON.parse(t) : e.getPredefinedInstances()[0];
488
527
  }
489
528
  static getPredefinedInstances() {
490
- return E() ? [{
529
+ return D() ? [{
491
530
  scheme: b.WSS,
492
531
  host: "testnet-electrum.nexa.org",
493
532
  port: 30004,
@@ -506,10 +545,10 @@ var be = class {
506
545
  }
507
546
  };
508
547
  // src/utils/keypath.ts
509
- function I(e, t, n) {
548
+ function R(e, t, n) {
510
549
  return `${e}'/${t}/${n}`;
511
550
  }
512
- function L(e) {
551
+ function z(e) {
513
552
  let t = e.split("/");
514
553
  return {
515
554
  account: parseInt(t[0].replace("'", "")),
@@ -518,7 +557,7 @@ function L(e) {
518
557
  };
519
558
  }
520
559
  // src/services/key.ts
521
- var R = class {
560
+ var B = class {
522
561
  constructor() {
523
562
  this.accountKeys = /* @__PURE__ */ new Map(), this.walletKeys = /* @__PURE__ */ new Map();
524
563
  }
@@ -544,35 +583,35 @@ var R = class {
544
583
  }).privateKey.toAddress().toString();
545
584
  }
546
585
  getKey(e) {
547
- let t = typeof e == "string" ? e : I(e.account, e.type, e.index), n = this.walletKeys.get(t);
586
+ let t = typeof e == "string" ? e : R(e.account, e.type, e.index), n = this.walletKeys.get(t);
548
587
  if (!n) {
549
- let { account: r, type: i, index: a } = typeof e == "string" ? L(e) : e;
588
+ let { account: r, type: i, index: a } = typeof e == "string" ? z(e) : e;
550
589
  n = this.getAccountKey(r).deriveChild(i, !1).deriveChild(a, !1), this.walletKeys.set(t, n);
551
590
  }
552
591
  return n;
553
592
  }
554
593
  };
555
594
  // src/utils/asset.ts
556
- function Ce(e, t) {
595
+ function xe(e, t) {
557
596
  return e && typeof e == "string" ? e.startsWith("http") ? e.replace("http://", "https://") : e.startsWith("ipfs://") ? e : `${new URL(t).origin}${e}` : "";
558
597
  }
559
- function we(e) {
560
- return e ? Te(e) : null;
598
+ function V(e) {
599
+ return e ? Se(e) : null;
561
600
  }
562
- function Te(e, t = "https://ipfs.nebula.markets/") {
601
+ function Se(e, t = "https://ipfs.nebula.markets/") {
563
602
  return e?.startsWith("ipfs://") ? t + e.substring(7) : e;
564
603
  }
565
- function z(e) {
566
- return `${V()}/metadata/${e}`;
604
+ function H(e) {
605
+ return `${W()}/metadata/${e}`;
567
606
  }
568
- function B(e, t) {
569
- return `${V()}/assets/${e}/${t}`;
607
+ function U(e, t) {
608
+ return `${W()}/assets/${e}/${t}`;
570
609
  }
571
- function V() {
572
- return `https://${E() ? "testapi" : "api"}.otoplo.com`;
610
+ function W() {
611
+ return `https://${D() ? "testapi" : "api"}.otoplo.com`;
573
612
  }
574
613
  // src/services/asset.ts
575
- var Ee = class {
614
+ var Ce = class {
576
615
  constructor(e, t) {
577
616
  this.walletDb = e, this.walletCache = t;
578
617
  }
@@ -645,8 +684,8 @@ var Ee = class {
645
684
  let n = Object.keys(t), r = await this.walletDb.getAssets(e, "nft"), i = r?.filter((e) => !n.includes(e.tokenIdHex)) ?? [];
646
685
  for (let t of i) await this.deleteNft(e, t.tokenIdHex);
647
686
  for (let [n, i] of Object.entries(t)) {
648
- let t = j(n), a = i && BigInt(i.confirmed) + BigInt(i.unconfirmed) > 0n, o = r?.some((e) => e.tokenIdHex == t);
649
- !a && !o || a && o || (!a && o ? await this.deleteNft(e, t) : await this.handleNftReceive(e, t, O()));
687
+ let t = M(n), a = i && BigInt(i.confirmed) + BigInt(i.unconfirmed) > 0n, o = r?.some((e) => e.tokenIdHex == t);
688
+ !a && !o || a && o || (!a && o ? await this.deleteNft(e, t) : await this.handleNftReceive(e, t, k()));
650
689
  }
651
690
  }
652
691
  async saveToken(e, t) {
@@ -662,10 +701,10 @@ var Ee = class {
662
701
  await this.walletDb.deleteNft(e, t), this.walletCache.removeNft(t);
663
702
  }
664
703
  getAssetFile(e, t) {
665
- return this.performGet(B(e, t), "raw");
704
+ return this.performGet(U(e, t), "raw");
666
705
  }
667
706
  getAssetMetadata(e) {
668
- return this.performGet(z(e), "json");
707
+ return this.performGet(H(e), "json");
669
708
  }
670
709
  async performGet(e, t) {
671
710
  try {
@@ -680,7 +719,35 @@ var Ee = class {
680
719
  throw Error(`Unexpected Error: ${e}`);
681
720
  }
682
721
  }
683
- }, De = class {
722
+ }, G = 274710, we = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76";
723
+ function K() {
724
+ return f.empty().add(l.OP_FROMALTSTACK).add(l.OP_DROP).add(l.OP_FROMALTSTACK).add(l.OP_CHECKLOCKTIMEVERIFY).add(l.OP_DROP).add(l.OP_FROMALTSTACK).add(l.OP_CHECKSIGVERIFY);
725
+ }
726
+ function q() {
727
+ let e = K();
728
+ return s.sha256ripemd160(e.toBuffer());
729
+ }
730
+ function J(e) {
731
+ return f.empty().add(e.toBuffer());
732
+ }
733
+ function Y(e) {
734
+ let t = J(e);
735
+ return s.sha256ripemd160(t.toBuffer());
736
+ }
737
+ function X(e) {
738
+ return e.map((e) => e <= 16 ? p.smallInt(e) : n.fromNumber(e).toScriptNumBuffer());
739
+ }
740
+ function Z(t, n) {
741
+ if (n.length !== 2) return;
742
+ let r = q(), i = Y(t), a = X(n);
743
+ return e.fromScriptTemplate(r, i, a).toString();
744
+ }
745
+ function Te(e, t) {
746
+ let n = (t - e) * 2, r = /* @__PURE__ */ new Date();
747
+ return r.setMinutes(r.getMinutes() + n), r.toLocaleDateString();
748
+ }
749
+ // src/services/transaction.ts
750
+ var Ee = class {
684
751
  constructor(e, t, n) {
685
752
  this.MAX_INPUTS_OUTPUTS = 250, this.rostrumService = e, this.keyManager = t, this.walletDb = n;
686
753
  }
@@ -696,7 +763,7 @@ var Ee = class {
696
763
  };
697
764
  }
698
765
  async fetchVaultTransactions(e) {
699
- let t = await this.rostrumService.getTransactionsHistory(e, T), n = [];
766
+ let t = await this.rostrumService.getTransactionsHistory(e, G), n = [];
700
767
  for (let r of t) {
701
768
  let t = this.classifyTransaction(r.tx_hash, [e]);
702
769
  n.push(t);
@@ -723,17 +790,17 @@ var Ee = class {
723
790
  address: e.addresses[0],
724
791
  nexaAmount: e.value_satoshi.toString(),
725
792
  assetId: e.token_id_hex ?? "",
726
- assetAmount: M(e.groupQuantity)
793
+ assetAmount: N(e.groupQuantity)
727
794
  };
728
795
  t.includes(n.address) ? r.push(n) : i.push(n);
729
796
  }
730
797
  for (let e of n.vout) {
731
- if (k(e.scriptPubKey.addresses)) continue;
798
+ if (A(e.scriptPubKey.addresses)) continue;
732
799
  let n = {
733
800
  address: e.scriptPubKey.addresses[0],
734
801
  nexaAmount: e.value_satoshi.toString(),
735
802
  assetId: e.scriptPubKey.token_id_hex ?? "",
736
- assetAmount: M(e.scriptPubKey.groupQuantity)
803
+ assetAmount: N(e.scriptPubKey.groupQuantity)
737
804
  };
738
805
  t.includes(n.address) ? a.push(n) : o.push(n);
739
806
  }
@@ -744,7 +811,7 @@ var Ee = class {
744
811
  accountId: 0,
745
812
  txId: n.txid,
746
813
  txIdem: n.txidem,
747
- time: c ? n.time : O(),
814
+ time: c ? n.time : k(),
748
815
  height: c ? n.height : 0,
749
816
  type: s,
750
817
  fee: n.fee_satoshi.toString(),
@@ -769,12 +836,12 @@ var Ee = class {
769
836
  return this.finalizeTransaction(i, Array.from(a.values()));
770
837
  }
771
838
  prepareTransaction(n, r, i, a) {
772
- if (!D(n) && !D(n, t.PayToPublicKeyHash)) throw Error("Invalid Address.");
839
+ if (!O(n) && !O(n, t.PayToPublicKeyHash)) throw Error("Invalid Address.");
773
840
  if (i && BigInt(r) < 1n || !i && parseInt(r) < h.DUST_AMOUNT) throw Error("The amount is too low.");
774
841
  if (i && BigInt(r) > 9223372036854775807n || !i && parseInt(r) > h.MAX_MONEY) throw Error("The amount is too high.");
775
842
  let o = new g();
776
843
  if (a && o.addData(a), i) {
777
- if (!D(i, t.GroupIdAddress)) throw Error("Invalid Token ID");
844
+ if (!O(i, t.GroupIdAddress)) throw Error("Invalid Token ID");
778
845
  if (e.getOutputType(n) === 0) throw Error("Token must be sent to script template address");
779
846
  o.to(n, h.DUST_AMOUNT, i, BigInt(r));
780
847
  } else o.to(n, r);
@@ -782,7 +849,7 @@ var Ee = class {
782
849
  }
783
850
  async populateNexaInputsAndChange(e, t, n, r) {
784
851
  let i = t.filter((e) => BigInt(e.balance.confirmed) + BigInt(e.balance.unconfirmed) > 0n);
785
- if (k(i)) throw Error("Not enough Nexa balance.");
852
+ if (A(i)) throw Error("Not enough Nexa balance.");
786
853
  let a = /* @__PURE__ */ new Map(), o = r.isConsolidate ? 0 : Number(e.transaction.outputs.find((e) => e.value > 0n).value);
787
854
  for (let t of i) {
788
855
  let i = await this.rostrumService.getNexaUtxos(t.address);
@@ -829,8 +896,8 @@ var Ee = class {
829
896
  throw Error(JSON.stringify(s));
830
897
  }
831
898
  async populateTokenInputsAndChange(e, t, n, r, i) {
832
- let a = j(r), o = t.filter((e) => Object.keys(e.tokensBalance).includes(a));
833
- if (k(o)) throw Error("Not enough token balance.");
899
+ let a = M(r), o = t.filter((e) => Object.keys(e.tokensBalance).includes(a));
900
+ if (A(o)) throw Error("Not enough token balance.");
834
901
  let s = /* @__PURE__ */ new Map(), c = 0n;
835
902
  for (let t of o) {
836
903
  let a = await this.rostrumService.getTokenUtxos(t.address, r);
@@ -867,13 +934,13 @@ var Ee = class {
867
934
  }
868
935
  };
869
936
  // src/utils/seed.ts
870
- function Oe(e = 12) {
937
+ function De(e = 12) {
871
938
  return re(x, e === 24 ? 256 : 128);
872
939
  }
873
- function H(e) {
940
+ function Q(e) {
874
941
  return ae(e, x);
875
942
  }
876
- async function U(e, t) {
943
+ async function $(e, t) {
877
944
  let n = await crypto.subtle.importKey("raw", new Uint8Array(e), "PBKDF2", !1, ["deriveKey"]);
878
945
  return crypto.subtle.deriveKey({
879
946
  name: "PBKDF2",
@@ -885,8 +952,8 @@ async function U(e, t) {
885
952
  length: 256
886
953
  }, !1, ["encrypt", "decrypt"]);
887
954
  }
888
- async function ke(e, t) {
889
- let n = r.utf8ToBuffer(t), i = r.getRandomBuffer(16), a = r.getRandomBuffer(12), o = r.utf8ToBuffer(e), s = await U(n, i), c = await crypto.subtle.encrypt({
955
+ async function Oe(e, t) {
956
+ let n = r.utf8ToBuffer(t), i = r.getRandomBuffer(16), a = r.getRandomBuffer(12), o = r.utf8ToBuffer(e), s = await $(n, i), c = await crypto.subtle.encrypt({
890
957
  name: "AES-GCM",
891
958
  iv: new Uint8Array(a)
892
959
  }, s, new Uint8Array(o)), l = r.concat([
@@ -896,112 +963,26 @@ async function ke(e, t) {
896
963
  ]);
897
964
  return r.bufferToBase64(l);
898
965
  }
899
- async function Ae(e, t) {
900
- let n = r.utf8ToBuffer(t), i = r.base64ToBuffer(e), a = i.slice(0, 16), o = i.slice(16, 28), s = i.slice(28), c = await U(n, a), l = await crypto.subtle.decrypt({
966
+ async function ke(e, t) {
967
+ let n = r.utf8ToBuffer(t), i = r.base64ToBuffer(e), a = i.slice(0, 16), o = i.slice(16, 28), s = i.slice(28), c = await $(n, a), l = await crypto.subtle.decrypt({
901
968
  name: "AES-GCM",
902
969
  iv: o
903
970
  }, c, s);
904
971
  return r.bufferToUtf8(new Uint8Array(l));
905
972
  }
906
- async function je(e, t) {
973
+ async function Ae(e, t) {
907
974
  try {
908
975
  if (e) {
909
- let n = await Ae(e, t);
910
- if (n && H(n)) return n;
976
+ let n = await ke(e, t);
977
+ if (n && Q(n)) return n;
911
978
  }
912
979
  return !1;
913
980
  } catch {
914
981
  return !1;
915
982
  }
916
983
  }
917
- // src/utils/market.ts
918
- var W = [
919
- {
920
- code: "usd",
921
- symbol: "$",
922
- name: "US Dollar"
923
- },
924
- {
925
- code: "eur",
926
- symbol: "€",
927
- name: "Euro"
928
- },
929
- {
930
- code: "gbp",
931
- symbol: "£",
932
- name: "British Pound"
933
- },
934
- {
935
- code: "aud",
936
- symbol: "A$",
937
- name: "Australian Dollar"
938
- },
939
- {
940
- code: "cad",
941
- symbol: "C$",
942
- name: "Canadian Dollar"
943
- },
944
- {
945
- code: "chf",
946
- symbol: "Fr",
947
- name: "Swiss Franc"
948
- },
949
- {
950
- code: "inr",
951
- symbol: "₹",
952
- name: "Indian Rupee"
953
- },
954
- {
955
- code: "cny",
956
- symbol: "¥",
957
- name: "Chinese Yuan"
958
- },
959
- {
960
- code: "jpy",
961
- symbol: "¥",
962
- name: "Japanese Yen"
963
- }
964
- ], G = Object.fromEntries(W.map((e) => [e.code, e.symbol])), K = W.map((e) => e.code);
965
- function Me(e) {
966
- return G[e] || e;
967
- }
968
- function Ne(e, t, n) {
969
- let r = y.multiply(e, t), i = y.round(r, 2, y.RoundingModes.HALF_DOWN);
970
- return n ? q(i, n) : i;
971
- }
972
- function q(e, t, n = "en-US") {
973
- return new Intl.NumberFormat(n, {
974
- style: "currency",
975
- currency: t,
976
- maximumFractionDigits: 2,
977
- minimumFractionDigits: 2
978
- }).format(Number(e));
979
- }
980
- // src/utils/vault.ts
981
- function J() {
982
- return f.empty().add(l.OP_FROMALTSTACK).add(l.OP_DROP).add(l.OP_FROMALTSTACK).add(l.OP_CHECKLOCKTIMEVERIFY).add(l.OP_DROP).add(l.OP_FROMALTSTACK).add(l.OP_CHECKSIGVERIFY);
983
- }
984
- function Y() {
985
- let e = J();
986
- return s.sha256ripemd160(e.toBuffer());
987
- }
988
- function X(e) {
989
- return f.empty().add(e.toBuffer());
990
- }
991
- function Z(e) {
992
- let t = X(e);
993
- return s.sha256ripemd160(t.toBuffer());
994
- }
995
- function Q(e) {
996
- return e.map((e) => e <= 16 ? p.smallInt(e) : n.fromNumber(e).toScriptNumBuffer());
997
- }
998
- function $(t, n) {
999
- if (n.length !== 2) return;
1000
- let r = Y(), i = Z(t), a = Q(n);
1001
- return e.fromScriptTemplate(r, i, a).toString();
1002
- }
1003
984
  // src/services/session.ts
1004
- var Pe = class {
985
+ var je = class {
1005
986
  constructor(e, t) {
1006
987
  this.walletDb = e, this.keyManager = t, this.providers = /* @__PURE__ */ new Map(), this.handlers = /* @__PURE__ */ new Map(), this.removeOnClose = !0;
1007
988
  }
@@ -1147,7 +1128,7 @@ var Pe = class {
1147
1128
  };
1148
1129
  });
1149
1130
  }
1150
- }, Fe = class {
1131
+ }, Me = class {
1151
1132
  constructor(e, t, n) {
1152
1133
  this.rostrumService = e, this.keyManager = t, this.assetService = n;
1153
1134
  }
@@ -1193,7 +1174,7 @@ var Pe = class {
1193
1174
  address: e.address,
1194
1175
  nexaAmount: i.amount.toString(),
1195
1176
  assetId: r,
1196
- assetAmount: M(i.group_quantity) || i.amount.toString()
1177
+ assetAmount: N(i.group_quantity) || i.amount.toString()
1197
1178
  }), e.involvedAssets.add(r), n.scriptSig.isEmpty() && e.inputsToSign.add(t);
1198
1179
  }
1199
1180
  n.scriptSig.findPlaceholder() > 0 && e.inputsToSign.add(t);
@@ -1202,11 +1183,11 @@ var Pe = class {
1202
1183
  for (let t = 0; t < e.txDetails.tx.outputs.length; t++) {
1203
1184
  let n = e.txDetails.tx.outputs[t].toObject();
1204
1185
  if (n.address !== e.address) continue;
1205
- let r = n.groupId ? j(n.groupId) : "NEXA", i = {
1186
+ let r = n.groupId ? M(n.groupId) : "NEXA", i = {
1206
1187
  address: n.address,
1207
1188
  nexaAmount: n.value.toString(),
1208
1189
  assetId: r,
1209
- assetAmount: M(n.groupAmount) || n.value.toString()
1190
+ assetAmount: N(n.groupAmount) || n.value.toString()
1210
1191
  };
1211
1192
  e.involvedAssets.add(r), e.myOutputs.set(t, i);
1212
1193
  }
@@ -1272,19 +1253,19 @@ var Pe = class {
1272
1253
  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);
1273
1254
  }
1274
1255
  }
1275
- }, Ie = 274710, Le = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76", Re = class {
1256
+ }, Ne = 274710, Pe = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76", Fe = class {
1276
1257
  constructor(e, t) {
1277
1258
  this.rostrumService = e, this.keyManager = t;
1278
1259
  }
1279
1260
  async discoverWalletIndex(e, t) {
1280
1261
  let n = 0, r = -1, i = [];
1281
1262
  for (;;) {
1282
- let a = await Promise.all(Array.from({ length: R.GAP_LIMIT }, (r, i) => this.rostrumService.isAddressUsed(this.keyManager.deriveAddress(e, t, n + i))));
1283
- if (a.some((e) => e)) r = n, i = a, n += R.GAP_LIMIT;
1263
+ let a = await Promise.all(Array.from({ length: B.GAP_LIMIT }, (r, i) => this.rostrumService.isAddressUsed(this.keyManager.deriveAddress(e, t, n + i))));
1264
+ if (a.some((e) => e)) r = n, i = a, n += B.GAP_LIMIT;
1284
1265
  else break;
1285
1266
  }
1286
1267
  if (r === -1) return -1;
1287
- for (let e = R.GAP_LIMIT - 1; e >= 0; e--) if (i[e]) return r + e;
1268
+ for (let e = B.GAP_LIMIT - 1; e >= 0; e--) if (i[e]) return r + e;
1288
1269
  return r;
1289
1270
  }
1290
1271
  async discoverVaults(e) {
@@ -1297,14 +1278,14 @@ var Pe = class {
1297
1278
  async checkForVaults(e) {
1298
1279
  let t = /* @__PURE__ */ new Set();
1299
1280
  return await Promise.all(e.map(async (e) => {
1300
- if (e.height > 0 && e.height < Ie) return;
1281
+ if (e.height > 0 && e.height < Ne) return;
1301
1282
  let n = await this.rostrumService.getTransaction(e.tx_hash);
1302
- for (let e of n.vout) e.scriptPubKey.hex.startsWith(Le) && t.add(e.scriptPubKey.hex);
1283
+ for (let e of n.vout) e.scriptPubKey.hex.startsWith(Pe) && t.add(e.scriptPubKey.hex);
1303
1284
  })), t;
1304
1285
  }
1305
1286
  parseVaultDetails(t) {
1306
1287
  let r = f.fromHex(t), a = new i().writeVarLengthBuf(r.toBuffer()).toBuffer(), o = new e(a).toString(), s = r.getVisibleArgs(), c = n.fromScriptNumBuffer(s.chunks[0].buf).toNumber(), l = p.isSmallIntOp(s.chunks[1].opcodenum) ? p.decodeOP_N(s.chunks[1].opcodenum) : n.fromScriptNumBuffer(s.chunks[1].buf).toNumber(), u = [c, l];
1307
- if (o == $(this.keyManager.getKey({
1288
+ if (o == Z(this.keyManager.getKey({
1308
1289
  account: S.VAULT,
1309
1290
  type: C.RECEIVE,
1310
1291
  index: l
@@ -1322,7 +1303,7 @@ var Pe = class {
1322
1303
  type: S.VAULT
1323
1304
  };
1324
1305
  }
1325
- }, ze = class e {
1306
+ }, Ie = class e {
1326
1307
  static {
1327
1308
  this.DEBOUNCE_MS = 2e3;
1328
1309
  }
@@ -1334,7 +1315,7 @@ var Pe = class {
1334
1315
  address: r,
1335
1316
  result: n
1336
1317
  });
1337
- }, this.discoveryService = new Re(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(), this.addressResolvers = /* @__PURE__ */ new Map();
1318
+ }, this.discoveryService = new Fe(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(), this.addressResolvers = /* @__PURE__ */ new Map();
1338
1319
  }
1339
1320
  onUpdate(e) {
1340
1321
  this.updateCallback = e;
@@ -1353,8 +1334,8 @@ var Pe = class {
1353
1334
  getMainAddresses() {
1354
1335
  return [...this.receiveAddresses, ...this.changeAddresses];
1355
1336
  }
1356
- getReceiveAddress() {
1357
- return this.receiveAddresses.find((e) => !e.used).address;
1337
+ getReceiveAddress(e = -1) {
1338
+ return e == -1 ? this.receiveAddresses.find((e) => !e.used).address : this.accounts.get(e).address;
1358
1339
  }
1359
1340
  getChangeAddress(e = -1) {
1360
1341
  return e == -1 ? this.changeAddresses.find((e) => !e.used).address : this.accounts.get(e).address;
@@ -1364,14 +1345,14 @@ var Pe = class {
1364
1345
  let t = this.accounts.get(e);
1365
1346
  return [{
1366
1347
  address: t.address,
1367
- keyPath: I(S.DAPP, 0, t.id),
1348
+ keyPath: R(S.DAPP, 0, t.id),
1368
1349
  balance: t.balance,
1369
1350
  tokensBalance: t.tokensBalance
1370
1351
  }];
1371
1352
  }
1372
1353
  return this.getMainAddresses().filter((e) => e.used).map((e) => ({
1373
1354
  address: e.address,
1374
- keyPath: I(S.MAIN, e.space, e.idx),
1355
+ keyPath: R(S.MAIN, e.space, e.idx),
1375
1356
  balance: e.balance,
1376
1357
  tokensBalance: e.tokensBalance
1377
1358
  }));
@@ -1398,7 +1379,7 @@ var Pe = class {
1398
1379
  t,
1399
1380
  n
1400
1381
  ]);
1401
- this.receiveAddresses = this.deriveAddresses(C.RECEIVE, 0, r + R.GAP_LIMIT + 1), this.changeAddresses = this.deriveAddresses(C.CHANGE, 0, i + R.GAP_LIMIT + 1), this.accounts = this.deriveAccounts(0, a < 0 ? 1 : a + 1), this.accounts.forEach((e) => this.accountsAddressToId.set(e.address, e.id)), await this.saveAddresses(this.receiveAddresses), await this.saveAddresses(this.changeAddresses), await this.saveAccounts(this.accounts), this.initState(), await this.initialSync(), this.notify({
1382
+ this.receiveAddresses = this.deriveAddresses(C.RECEIVE, 0, r + B.GAP_LIMIT + 1), this.changeAddresses = this.deriveAddresses(C.CHANGE, 0, i + B.GAP_LIMIT + 1), this.accounts = this.deriveAccounts(0, a < 0 ? 1 : a + 1), this.accounts.forEach((e) => this.accountsAddressToId.set(e.address, e.id)), await this.saveAddresses(this.receiveAddresses), await this.saveAddresses(this.changeAddresses), await this.saveAccounts(this.accounts), this.initState(), await this.initialSync(), this.notify({
1402
1383
  type: "discover_wallet",
1403
1384
  loading: !1
1404
1385
  }), this.notify({
@@ -1430,8 +1411,8 @@ var Pe = class {
1430
1411
  id: -1,
1431
1412
  name: "HD Wallet",
1432
1413
  address: this.getReceiveAddress(),
1433
- balance: N(e.map((e) => e.balance)),
1434
- tokensBalance: P(e.map((e) => e.tokensBalance)),
1414
+ balance: P(e.map((e) => e.balance)),
1415
+ tokensBalance: F(e.map((e) => e.tokensBalance)),
1435
1416
  sessions: {}
1436
1417
  }
1437
1418
  });
@@ -1579,7 +1560,7 @@ var Pe = class {
1579
1560
  for (let { txs: t } of e) for (let e of t) n.set(e.tx_hash, e);
1580
1561
  let r = Array.from(n.values());
1581
1562
  t && this.recoverVaults(r), await this.checkGapLimit(C.RECEIVE), await this.checkGapLimit(C.CHANGE);
1582
- let i = this.getMainAddresses(), a = N(i.map((e) => e.balance)), o = P(i.map((e) => e.tokensBalance));
1563
+ let i = this.getMainAddresses(), a = P(i.map((e) => e.balance)), o = F(i.map((e) => e.tokensBalance));
1583
1564
  this.notify({
1584
1565
  type: "account_balance_updated",
1585
1566
  accountId: -1,
@@ -1644,8 +1625,8 @@ var Pe = class {
1644
1625
  }
1645
1626
  async checkGapLimit(e) {
1646
1627
  let t = e === C.RECEIVE ? this.receiveAddresses : this.changeAddresses, n = e === C.RECEIVE ? "Receive" : "Change", r = t.filter((e) => !e.used).length;
1647
- if (r < R.GAP_LIMIT) {
1648
- let i = R.GAP_LIMIT - r, a = t[t.length - 1].idx;
1628
+ if (r < B.GAP_LIMIT) {
1629
+ let i = B.GAP_LIMIT - r, a = t[t.length - 1].idx;
1649
1630
  console.log(`Deriving ${i} more ${n} addresses...`);
1650
1631
  let o = this.deriveAddresses(e, a + 1, i);
1651
1632
  e === C.RECEIVE ? this.receiveAddresses.push(...o) : this.changeAddresses.push(...o), await this.saveAddresses(o), await this.subscribeAddresses(o);
@@ -1743,7 +1724,7 @@ var Pe = class {
1743
1724
  }
1744
1725
  async recoverTokens() {
1745
1726
  let e = [];
1746
- e.push(this.assetService.syncTokens(-1, P(this.getMainAddresses().map((e) => e.tokensBalance))));
1727
+ e.push(this.assetService.syncTokens(-1, F(this.getMainAddresses().map((e) => e.tokensBalance))));
1747
1728
  for (let t of this.accounts.values()) e.push(this.assetService.syncTokens(t.id, t.tokensBalance));
1748
1729
  await Promise.all(e);
1749
1730
  }
@@ -1756,7 +1737,7 @@ var Pe = class {
1756
1737
  isMainAddress(e) {
1757
1738
  return e.type == S.MAIN;
1758
1739
  }
1759
- }, Be = class e {
1740
+ }, Le = class e {
1760
1741
  static {
1761
1742
  this.POLL_INTERVAL_MS = 12e4;
1762
1743
  }
@@ -1778,7 +1759,7 @@ var Pe = class {
1778
1759
  async init() {
1779
1760
  this.stop();
1780
1761
  let t = await this.walletDb.getSetting(e.CURRENCY_SETTING);
1781
- this.currency = K.includes(t) ? t : "usd", this.notify({
1762
+ this.currency = E.includes(t) ? t : "usd", this.notify({
1782
1763
  type: "currency_updated",
1783
1764
  currency: this.currency
1784
1765
  }), this.running = !0, this.poll();
@@ -1837,6 +1818,6 @@ var Pe = class {
1837
1818
  }), a;
1838
1819
  }
1839
1820
  };
1840
- export { S as AccountType, Ee as AssetService, W as CURRENCIES, R as KeyManager, C as KeySpace, se as MAIN_WALLET_ID, ce as MAX_INT64, Be as MarketService, Se as RostrumService, Pe as SessionManager, w as SessionRequestType, De as TransactionService, Fe as TransactionTransformer, T as VAULT_FIRST_BLOCK, le as VAULT_SCRIPT_PREFIX, xe as WalletCache, be as WalletDB, ze as WalletManager, Ne as calculateFiatValue, me as capitalizeFirstLetter, K as currencyCodes, G as currencySymbols, O as currentTimestamp, ke as encryptMnemonic, fe as estimateDateByFutureBlock, q as formatMoney, Oe as generateNewMnemonic, $ as generateVaultAddress, X as generateVaultConstraint, Q as generateVaultVisibleArgs, A as getAddressBuffer, B as getAssetFileUrl, z as getAssetMetadataUrl, Me as getCurrencySymbol, de as getExplorerUrl, ge as getFileMediaType, _e as getFileMimeType, Z as getVaultConstraintHash, J as getVaultTemplate, Y as getVaultTemplateHash, ue as isGenesisHashValid, H as isMnemonicValid, k as isNullOrEmpty, E as isTestnet, D as isValidNexaAddress, I as keyPathToString, we as parseTokenDataUrl, ye as prettifyAmount, ve as sleep, L as stringToKeyPath, N as sumBalance, P as sumTokensBalance, M as tokenAmountToAssetAmount, he as tokenHexToAddr, j as tokenIdToHex, Ce as transformTokenIconUrl, pe as truncateStringMiddle, je as validateAndDecryptMnemonic };
1821
+ export { S as AccountType, Ce as AssetService, T as CURRENCIES, se as HD_WALLET_ID, B as KeyManager, C as KeySpace, ce as MAX_INT64, Le as MarketService, be as RostrumService, je as SessionManager, w as SessionRequestType, Ee as TransactionService, Me as TransactionTransformer, G as VAULT_FIRST_BLOCK, we as VAULT_SCRIPT_PREFIX, ye as WalletCache, ve as WalletDB, Ie as WalletManager, _e as calculateFiatValue, fe as capitalizeFirstLetter, E as currencyCodes, k as currentTimestamp, Oe as encryptMnemonic, Te as estimateDateByFutureBlock, De as generateNewMnemonic, Z as generateVaultAddress, J as generateVaultConstraint, X as generateVaultVisibleArgs, j as getAddressBuffer, U as getAssetFileUrl, H as getAssetMetadataUrl, ue as getExplorerUrl, me as getFileMediaType, he as getFileMimeType, Y as getVaultConstraintHash, K as getVaultTemplate, q as getVaultTemplateHash, le as isGenesisHashValid, Q as isMnemonicValid, A as isNullOrEmpty, D as isTestnet, O as isValidNexaAddress, R as keyPathToString, V as parseTokenDataUrl, L as prettifyAmount, ge as sleep, z as stringToKeyPath, P as sumBalance, F as sumTokensBalance, N as tokenAmountToAssetAmount, pe as tokenHexToAddr, M as tokenIdToHex, xe as transformTokenIconUrl, de as truncateStringMiddle, Ae as validateAndDecryptMnemonic };
1841
1822
 
1842
1823
  //# sourceMappingURL=index.js.map