mainnet-js 2.6.7 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.6.7.js → mainnet-2.7.1.js} +446 -156
  3. package/dist/module/cli.js +0 -4
  4. package/dist/module/cli.js.map +1 -1
  5. package/dist/module/index.d.ts +1 -3
  6. package/dist/module/index.d.ts.map +1 -1
  7. package/dist/module/index.js +1 -3
  8. package/dist/module/index.js.map +1 -1
  9. package/dist/module/network/ElectrumNetworkProvider.d.ts +21 -27
  10. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  11. package/dist/module/network/ElectrumNetworkProvider.js +92 -102
  12. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  13. package/dist/module/network/NetworkProvider.d.ts +9 -22
  14. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  15. package/dist/module/network/constant.d.ts +0 -21
  16. package/dist/module/network/constant.d.ts.map +1 -1
  17. package/dist/module/network/constant.js +0 -21
  18. package/dist/module/network/constant.js.map +1 -1
  19. package/dist/module/network/default.d.ts +2 -2
  20. package/dist/module/network/default.d.ts.map +1 -1
  21. package/dist/module/network/default.js +15 -45
  22. package/dist/module/network/default.js.map +1 -1
  23. package/dist/module/network/interface.d.ts +2 -8
  24. package/dist/module/network/interface.d.ts.map +1 -1
  25. package/dist/module/network/util.d.ts.map +1 -1
  26. package/dist/module/network/util.js +4 -5
  27. package/dist/module/network/util.js.map +1 -1
  28. package/dist/module/rate/ExchangeRate.js +2 -1
  29. package/dist/module/rate/ExchangeRate.js.map +1 -1
  30. package/dist/module/transaction/Wif.d.ts.map +1 -1
  31. package/dist/module/transaction/Wif.js +1 -1
  32. package/dist/module/transaction/Wif.js.map +1 -1
  33. package/dist/module/wallet/Base.d.ts +282 -88
  34. package/dist/module/wallet/Base.d.ts.map +1 -1
  35. package/dist/module/wallet/Base.js +1058 -215
  36. package/dist/module/wallet/Base.js.map +1 -1
  37. package/dist/module/wallet/Util.d.ts +7 -54
  38. package/dist/module/wallet/Util.d.ts.map +1 -1
  39. package/dist/module/wallet/Util.js +12 -79
  40. package/dist/module/wallet/Util.js.map +1 -1
  41. package/dist/module/wallet/Wif.d.ts +46 -251
  42. package/dist/module/wallet/Wif.d.ts.map +1 -1
  43. package/dist/module/wallet/Wif.js +126 -1026
  44. package/dist/module/wallet/Wif.js.map +1 -1
  45. package/dist/module/wallet/createWallet.d.ts +2 -1
  46. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  47. package/dist/module/wallet/createWallet.js +2 -3
  48. package/dist/module/wallet/createWallet.js.map +1 -1
  49. package/dist/module/wallet/interface.d.ts +2 -4
  50. package/dist/module/wallet/interface.d.ts.map +1 -1
  51. package/dist/tsconfig.tsbuildinfo +1 -1
  52. package/package.json +6 -12
  53. package/src/cli.ts +0 -4
  54. package/src/index.ts +1 -5
  55. package/src/network/ElectrumNetworkProvider.ts +133 -188
  56. package/src/network/NetworkProvider.ts +9 -30
  57. package/src/network/Rpc.test.ts +14 -5
  58. package/src/network/constant.ts +0 -23
  59. package/src/network/default.ts +26 -66
  60. package/src/network/electrum.test.ts +2 -4
  61. package/src/network/interface.ts +2 -9
  62. package/src/network/util.ts +6 -7
  63. package/src/rate/ExchangeRate.test.ts +1 -1
  64. package/src/rate/ExchangeRate.ts +2 -1
  65. package/{polyfill/json.js → src/test/json.test.ts} +7 -1
  66. package/src/transaction/Wif.ts +2 -1
  67. package/src/wallet/Base.ts +1520 -273
  68. package/src/wallet/Cashtokens.test.headless.js +1 -1
  69. package/src/wallet/Cashtokens.test.ts +7 -8
  70. package/src/wallet/Util.ts +20 -102
  71. package/src/wallet/Wif.bip39.test.ts +3 -3
  72. package/src/wallet/Wif.test.ts +31 -25
  73. package/src/wallet/Wif.ts +174 -1493
  74. package/src/wallet/Wif.watchOnly.test.ts +5 -5
  75. package/src/wallet/createWallet.ts +11 -10
  76. package/src/wallet/interface.ts +3 -4
  77. package/webpack.config.cjs +4 -55
  78. package/dist/module/qr/Qr.d.ts +0 -9
  79. package/dist/module/qr/Qr.d.ts.map +0 -1
  80. package/dist/module/qr/Qr.js +0 -22
  81. package/dist/module/qr/Qr.js.map +0 -1
  82. package/dist/module/qr/interface.d.ts +0 -6
  83. package/dist/module/qr/interface.d.ts.map +0 -1
  84. package/dist/module/qr/interface.js +0 -2
  85. package/dist/module/qr/interface.js.map +0 -1
  86. package/dist/module/util/eventsource.d.ts +0 -3
  87. package/dist/module/util/eventsource.d.ts.map +0 -1
  88. package/dist/module/util/eventsource.js +0 -11
  89. package/dist/module/util/eventsource.js.map +0 -1
  90. package/polyfill/README.md +0 -1
  91. package/polyfill/eventsource.js +0 -6
  92. package/polyfill/support/types.js +0 -286
  93. package/polyfill/util.cjs +0 -249
  94. package/src/network/default.test.ts +0 -37
  95. package/src/qr/Qr.test.ts +0 -14
  96. package/src/qr/Qr.ts +0 -24
  97. package/src/qr/interface.ts +0 -5
  98. package/src/util/eventsource.ts +0 -12
@@ -474,7 +474,7 @@ describe(`Wallet should function in the browser`, () => {
474
474
  0
475
475
  );
476
476
 
477
- const cancel = bob.watchTokenBalance(tokenId, (balance) => {
477
+ const cancel = await bob.watchTokenBalance(tokenId, (balance) => {
478
478
  seenBalance = balance;
479
479
  });
480
480
 
@@ -1,13 +1,12 @@
1
- import { RegTestWallet, TestNetWallet, Wallet } from "./Wif";
1
+ import { RegTestWallet, TestNetWallet } from "./Wif";
2
2
  import { initProviders, disconnectProviders } from "../network/Connection";
3
3
  import {
4
- OpReturnData,
5
4
  SendRequest,
6
5
  SendResponse,
7
6
  TokenMintRequest,
8
7
  TokenSendRequest,
9
8
  } from "./model";
10
- import { Network, NFTCapability } from "../interface";
9
+ import { NFTCapability } from "../interface";
11
10
  import {
12
11
  binToHex,
13
12
  binsAreEqual,
@@ -16,8 +15,8 @@ import {
16
15
  utf8ToBin,
17
16
  } from "@bitauth/libauth";
18
17
  import { delay } from "../util";
19
- import json from "../../polyfill/json";
20
18
  import { Config } from "../config";
19
+ import json from "../test/json.test";
21
20
 
22
21
  beforeAll(async () => {
23
22
  await initProviders();
@@ -318,13 +317,13 @@ describe(`Test cashtokens`, () => {
318
317
  const bob = await RegTestWallet.newRandom();
319
318
  await alice.send([
320
319
  new TokenSendRequest({
321
- cashaddr: bob.address!,
320
+ cashaddr: bob.cashaddr!,
322
321
  tokenId: tokenId,
323
322
  capability: NFTCapability.none,
324
323
  commitment: "0a",
325
324
  }),
326
325
  new TokenSendRequest({
327
- cashaddr: bob.address!,
326
+ cashaddr: bob.cashaddr!,
328
327
  tokenId: tokenId,
329
328
  capability: NFTCapability.none,
330
329
  commitment: "0a",
@@ -621,12 +620,12 @@ describe(`Test cashtokens`, () => {
621
620
  0
622
621
  );
623
622
 
624
- const cancel = bob.watchTokenBalance(tokenId, (balance) => {
623
+ const cancel = await bob.watchTokenBalance(tokenId, (balance) => {
625
624
  seenBalance = balance;
626
625
  });
627
626
 
628
627
  let bobTxId = ".";
629
- const txCancel = bob.watchAddressTokenTransactions((tx) => {
628
+ const txCancel = await bob.watchAddressTokenTransactions((tx) => {
630
629
  bobTxId = tx.txid;
631
630
  });
632
631
 
@@ -1,25 +1,17 @@
1
1
  import {
2
- RegTestWallet,
3
- RegTestWatchWallet,
4
- RegTestWifWallet,
5
- TestNetWallet,
6
- TestNetWatchWallet,
7
- TestNetWifWallet,
8
- Wallet,
9
- WatchWallet,
10
- WifWallet,
11
- } from "../wallet/Wif.js";
12
- import {
2
+ Transaction as LibAuthTransaction,
3
+ assertSuccess,
13
4
  binToHex,
14
5
  decodeTransaction as decodeTransactionLibAuth,
15
6
  hexToBin,
16
- lockingBytecodeToCashAddress,
17
- Transaction as LibAuthTransaction,
18
- assertSuccess,
19
7
  isPayToPublicKey,
20
- publicKeyToP2pkhCashAddress,
21
8
  lockingBytecodeToAddressContents,
9
+ lockingBytecodeToCashAddress,
10
+ publicKeyToP2pkhCashAddress,
22
11
  } from "@bitauth/libauth";
12
+ import { bchParam } from "../chain.js";
13
+ import { NetworkType, prefixFromNetworkMap } from "../enum.js";
14
+ import { getNetworkProvider } from "../network/default.js";
23
15
  import {
24
16
  ElectrumRawTransaction,
25
17
  ElectrumRawTransactionVin,
@@ -27,25 +19,24 @@ import {
27
19
  ElectrumRawTransactionVout,
28
20
  ElectrumRawTransactionVoutScriptPubKey,
29
21
  } from "../network/interface.js";
30
- import { bchParam } from "../chain.js";
22
+ import NetworkProvider from "../network/NetworkProvider.js";
31
23
  import { getTransactionHash } from "../util/transaction.js";
32
24
 
33
25
  /**
34
26
  * Class with various wallet utilities.
35
27
  */
36
28
  export class Util {
37
- readonly wallet: Wallet;
38
- static get walletType() {
39
- return Wallet;
40
- }
29
+ readonly network: NetworkType;
30
+ provider: NetworkProvider;
41
31
 
42
32
  /**
43
33
  * Initializes a wallet Util class.
44
34
  *
45
- * @param wallet A wallet object
35
+ * @param network The network type to use. Defaults to mainnet.
46
36
  */
47
- constructor(wallet: Wallet) {
48
- this.wallet = wallet;
37
+ constructor(network = NetworkType.Mainnet) {
38
+ this.network = network;
39
+ this.provider = getNetworkProvider(network);
49
40
  }
50
41
 
51
42
  public async getTransactionHash(rawTransactionHex: string): Promise<string> {
@@ -74,7 +65,7 @@ export class Util {
74
65
  } else {
75
66
  // tx hash, look up the raw transaction
76
67
  txHash = transactionHashOrHex;
77
- transactionHex = await this.wallet.provider!.getRawTransaction(txHash);
68
+ transactionHex = await this.provider.getRawTransaction(txHash);
78
69
  transactionBin = hexToBin(transactionHex);
79
70
  }
80
71
 
@@ -140,12 +131,12 @@ export class Util {
140
131
  publicKey: lockingBytecodeToAddressContents(
141
132
  output.lockingBytecode
142
133
  ).payload,
143
- prefix: this.wallet.networkPrefix,
134
+ prefix: prefixFromNetworkMap[this.network],
144
135
  })
145
136
  : assertSuccess(
146
137
  lockingBytecodeToCashAddress({
147
138
  bytecode: output.lockingBytecode,
148
- prefix: this.wallet.networkPrefix,
139
+ prefix: prefixFromNetworkMap[this.network],
149
140
  })
150
141
  ).address,
151
142
  ],
@@ -168,85 +159,12 @@ export class Util {
168
159
 
169
160
  public static async decodeTransaction(
170
161
  transactionHashOrHex: string,
171
- loadInputValues: boolean = false
162
+ loadInputValues: boolean = false,
163
+ network?: NetworkType
172
164
  ): Promise<ElectrumRawTransaction> {
173
- return new this.walletType().util.decodeTransaction(
165
+ return new this(network).decodeTransaction(
174
166
  transactionHashOrHex,
175
167
  loadInputValues
176
168
  );
177
169
  }
178
170
  }
179
-
180
- //#region Specific wallet classes
181
- /**
182
- * Class to manage a testnet wallet.
183
- */
184
- export class TestNetUtil extends Util {
185
- static get walletType() {
186
- return TestNetWallet;
187
- }
188
- }
189
-
190
- /**
191
- * Class to manage a regtest wallet.
192
- */
193
- export class RegTestUtil extends Util {
194
- static get walletType() {
195
- return RegTestWallet;
196
- }
197
- }
198
-
199
- /**
200
- * Class to manage a bitcoin cash wif wallet.
201
- */
202
- export class WifUtil extends Util {
203
- static get walletType() {
204
- return WifWallet;
205
- }
206
- }
207
-
208
- /**
209
- * Class to manage a testnet wif wallet.
210
- */
211
- export class TestNetWifUtil extends Util {
212
- static get walletType() {
213
- return TestNetWifWallet;
214
- }
215
- }
216
-
217
- /**
218
- * Class to manage a regtest wif wallet.
219
- */
220
- export class RegTestWifUtil extends Util {
221
- static get walletType() {
222
- return RegTestWifWallet;
223
- }
224
- }
225
-
226
- /**
227
- * Class to manage a bitcoin cash watch wallet.
228
- */
229
- export class WatchUtil extends Util {
230
- static get walletType() {
231
- return WatchWallet;
232
- }
233
- }
234
-
235
- /**
236
- * Class to manage a testnet watch wallet.
237
- */
238
- export class TestNetWatchUtil extends Util {
239
- static get walletType() {
240
- return TestNetWatchWallet;
241
- }
242
- }
243
-
244
- /**
245
- * Class to manage a regtest watch wallet.
246
- */
247
- export class RegTestWatchUtil extends Util {
248
- static get walletType() {
249
- return RegTestWatchWallet;
250
- }
251
- }
252
- //#endregion
@@ -14,7 +14,7 @@ describe(`Test bip39 edge cases`, () => {
14
14
  test("Should catch a blank seed", async () => {
15
15
  try {
16
16
  let w = RegTestWallet.fromSeed("");
17
- let cashaddr = (await w).address;
17
+ let cashaddr = (await w).cashaddr;
18
18
  expect(cashaddr).toContain(
19
19
  "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
20
20
  );
@@ -28,7 +28,7 @@ describe(`Test bip39 edge cases`, () => {
28
28
  let w = RegTestWallet.fromSeed(
29
29
  "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"
30
30
  );
31
- let cashaddr = (await w).address;
31
+ let cashaddr = (await w).cashaddr;
32
32
  expect(cashaddr).toContain(
33
33
  "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
34
34
  );
@@ -41,7 +41,7 @@ describe(`Test bip39 edge cases`, () => {
41
41
  let w = RegTestWallet.fromSeed(
42
42
  "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
43
43
  );
44
- let cashaddr = (await w).address;
44
+ let cashaddr = (await w).cashaddr;
45
45
  expect(cashaddr).toContain(
46
46
  "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
47
47
  );
@@ -16,9 +16,9 @@ import {
16
16
  utf8ToBin,
17
17
  } from "@bitauth/libauth";
18
18
  import { mine } from "../mine";
19
- import json from "../../polyfill/json";
20
- import ElectrumNetworkProvider from "../network/ElectrumNetworkProvider";
21
19
  import { Config } from "../config";
20
+ import { CancelFn } from "./interface";
21
+ import json from "../test/json.test";
22
22
 
23
23
  beforeAll(async () => {
24
24
  await initProviders();
@@ -642,9 +642,9 @@ describe(`Wallet subscriptions`, () => {
642
642
  test("Should watch then wait", async () => {
643
643
  const aliceWallet = await RegTestWallet.newRandom();
644
644
 
645
- let cancel = aliceWallet.watchBalance(() => {});
645
+ let cancel = await aliceWallet.watchBalance(() => {});
646
646
 
647
- cancel();
647
+ await cancel();
648
648
  });
649
649
 
650
650
  test("Should wait for balance", async () => {
@@ -677,7 +677,7 @@ describe(`Wallet subscriptions`, () => {
677
677
  },
678
678
  ]);
679
679
 
680
- let cancel = bob.watchBalance(() => {});
680
+ let cancel = await bob.watchBalance(() => {});
681
681
  let balance = await bob.waitForBalance(2000, "sat");
682
682
  expect(balance.sat!).toBe(2000);
683
683
  await cancel();
@@ -801,50 +801,56 @@ describe(`Wallet subscriptions`, () => {
801
801
  }, 0);
802
802
 
803
803
  let aliceWatchResult = false;
804
- const aliceWatchCancel = (
805
- alice.provider! as ElectrumNetworkProvider
806
- ).watchAddressStatus(alice.getDepositAddress(), (_status) => {
807
- aliceWatchCancel();
808
- aliceWatchResult = true;
809
- });
804
+ let aliceWatchCancel: CancelFn;
805
+ aliceWatchCancel = await alice.provider!.watchAddressStatus(
806
+ alice.getDepositAddress(),
807
+ async (_status) => {
808
+ await aliceWatchCancel?.();
809
+ aliceWatchResult = true;
810
+ }
811
+ );
810
812
 
811
813
  let bobWatchResult = false;
812
814
  let bobTransactionId = "";
813
- const bobWatchCancel = bob.watchAddress((txHash) => {
814
- bobWatchCancel();
815
+ let bobWatchCancel: CancelFn;
816
+ bobWatchCancel = await bob.watchAddress(async (txHash) => {
817
+ await bobWatchCancel?.();
815
818
  bobWatchResult = true;
816
819
  bobTransactionId = txHash;
817
820
  });
818
821
 
819
822
  let bobBalanceWatchResult = false;
820
- const bobBalanceWatchCancel = bob.watchBalance((balance) => {
823
+ let bobBalanceWatchCancel: CancelFn;
824
+ bobBalanceWatchCancel = await bob.watchBalance(async (balance) => {
825
+ // skip if balance is zero yet
826
+ if (!balance.bch) {
827
+ return;
828
+ }
829
+
821
830
  expect(balance.bch!).toBe(0.001);
822
- bobBalanceWatchCancel();
831
+ await bobBalanceWatchCancel?.();
823
832
  bobBalanceWatchResult = true;
824
833
  });
825
834
 
826
835
  let blockWatchResult = false;
827
- const blockWatchCancel = bob.watchBlocks((block) => {
836
+ let blockWatchCancel: CancelFn;
837
+ blockWatchCancel = await bob.watchBlocks(async (block) => {
828
838
  expect(block.height).toBeGreaterThan(1);
829
- blockWatchCancel();
839
+ await blockWatchCancel?.();
830
840
  blockWatchResult = true;
831
841
  });
832
842
 
843
+ const blockNumber = await alice.provider!.getBlockHeight();
844
+
833
845
  let blockWaitResult = false;
834
846
  setTimeout(async () => {
835
- const blockNumber = await (
836
- alice.provider! as ElectrumNetworkProvider
837
- ).getBlockHeight();
838
- const result = await alice.waitForBlock();
847
+ const result = await alice.waitForBlock(blockNumber + 1);
839
848
  expect(result.height).toBe(blockNumber + 1);
840
849
  blockWaitResult = true;
841
850
  }, 0);
842
851
 
843
852
  let blockNumberWaitResult = false;
844
853
  setTimeout(async () => {
845
- const blockNumber = await (
846
- alice.provider! as ElectrumNetworkProvider
847
- ).getBlockHeight();
848
854
  const result = await alice.waitForBlock(blockNumber + 2);
849
855
  expect(result.height).toBe(blockNumber + 2);
850
856
  blockNumberWaitResult = true;
@@ -1068,7 +1074,7 @@ describe(`Wallet extrema behavior regression testing`, () => {
1068
1074
  const aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
1069
1075
  const aliceWallet = await RegTestWallet.fromId(aliceWif);
1070
1076
  const bobWallet = await RegTestWallet.newRandom();
1071
- aliceWallet.privateKey = undefined;
1077
+ delete (aliceWallet as any).privateKey;
1072
1078
 
1073
1079
  const aliceUtxos = await aliceWallet.getAddressUtxos();
1074
1080