mainnet-js 2.6.7 → 2.7.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.
Files changed (98) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.6.7.js → mainnet-2.7.0.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
@@ -1,5 +1,3 @@
1
- import { ClusterOrder } from "electrum-cash";
2
-
3
1
  export const ELECTRUM_CASH_PROTOCOL_VERSION = "1.5";
4
2
 
5
3
  export const networkTickerMap = {
@@ -43,24 +41,3 @@ export const defaultServers = {
43
41
  testnet: testnetServers,
44
42
  regtest: regtestServers,
45
43
  };
46
-
47
- export const clusterParams = {
48
- mainnet: {
49
- confidence: 1,
50
- distribution: 1,
51
- order: ClusterOrder.RANDOM,
52
- timeout: 45000,
53
- },
54
- testnet: {
55
- confidence: 1,
56
- distribution: 1,
57
- order: ClusterOrder.RANDOM,
58
- timeout: 30000,
59
- },
60
- regtest: {
61
- confidence: 1,
62
- distribution: 1,
63
- order: ClusterOrder.PRIORITY,
64
- timeout: 3000,
65
- },
66
- };
@@ -1,17 +1,13 @@
1
1
  import { default as ElectrumNetworkProvider } from "./ElectrumNetworkProvider.js";
2
- import { ElectrumCluster, ElectrumClient } from "electrum-cash";
2
+ import { ElectrumClient } from "@electrum-cash/network";
3
+ import { ElectrumWebSocket } from "@electrum-cash/web-socket";
3
4
  import { default as NetworkProvider } from "./NetworkProvider.js";
4
- import {
5
- getConfidence,
6
- getDefaultServers,
7
- getUserAgent,
8
- } from "./configuration.js";
5
+ import { getDefaultServers, getUserAgent } from "./configuration.js";
9
6
  import { parseElectrumUrl } from "./util.js";
10
- import { ElectrumHostParams, ElectrumClusterParams } from "./interface.js";
7
+ import { ElectrumHostParams } from "./interface.js";
11
8
  import { Network } from "../interface.js";
12
9
  import {
13
10
  networkTickerMap,
14
- clusterParams,
15
11
  ELECTRUM_CASH_PROTOCOL_VERSION,
16
12
  } from "./constant.js";
17
13
 
@@ -42,7 +38,7 @@ export function getNetworkProvider(
42
38
  network: Network = Network.MAINNET,
43
39
  servers?: string[] | string,
44
40
  manualConnectionManagement?: boolean,
45
- options?: ElectrumClusterParams
41
+ options?: ElectrumHostParams
46
42
  ): NetworkProvider {
47
43
  const globalContext =
48
44
  servers === undefined &&
@@ -55,7 +51,6 @@ export function getNetworkProvider(
55
51
  }
56
52
  }
57
53
 
58
- let useCluster;
59
54
  manualConnectionManagement = manualConnectionManagement
60
55
  ? manualConnectionManagement
61
56
  : false;
@@ -63,31 +58,13 @@ export function getNetworkProvider(
63
58
  // If the user has passed a single string, assume a single client connection
64
59
  if (typeof servers === "string") {
65
60
  servers = [servers as string];
66
- useCluster = false;
67
- }
68
-
69
- // Otherwise, assume a list of servers has been passed
70
- else {
71
- servers = servers;
72
- useCluster = servers.length > 1;
73
61
  }
74
62
 
75
63
  // There were server(s)
76
64
  if (servers) {
77
- let clusterOrClient;
78
- // There were multiple servers
79
- if (useCluster) {
80
- let clusterParam = clusterParams[network];
81
- clusterParam["confidence"] = getConfidence();
82
- clusterParam = Object.assign({}, clusterParam, options);
83
- clusterOrClient = getCluster(servers, clusterParam, network);
84
- }
85
- // The server is a single string in an array
86
- else {
87
- clusterOrClient = getClient(servers, network);
88
- }
65
+ const client = getClient(servers[0], network, options);
89
66
  let provider = new ElectrumNetworkProvider(
90
- clusterOrClient,
67
+ client,
91
68
  network,
92
69
  manualConnectionManagement
93
70
  );
@@ -102,38 +79,14 @@ export function getNetworkProvider(
102
79
  }
103
80
  }
104
81
 
105
- // Create a cluster give a list of servers and parameters
106
- function getCluster(servers: string[], params, network: Network) {
107
- let electrum = getElectrumCluster(params, network);
108
-
109
- for (const s of servers) {
110
- let url = parseElectrumUrl(s);
111
- try {
112
- electrum.addServer(url.host, url.port, url.scheme, false);
113
- } catch (error) {
114
- console.log(
115
- `Error connecting ${url.host}:${url.port} over ${url.scheme}`
116
- );
117
- }
118
- }
119
- return electrum;
120
- }
121
-
122
- // create a client with a list of servers
123
- function getClient(servers: string[], network: Network) {
124
- let url = parseElectrumUrl(servers[0]);
125
- return getElectrumClient(url, 120000, network);
126
- }
127
-
128
- function getElectrumCluster(params: ElectrumClusterParams, network: Network) {
129
- return new ElectrumCluster(
130
- getUserAgent(),
131
- ELECTRUM_CASH_PROTOCOL_VERSION,
132
- params.confidence,
133
- params.distribution,
134
- params.order,
135
- params.timeout
136
- );
82
+ // create a client with a server
83
+ function getClient(
84
+ server: string,
85
+ network: Network,
86
+ options?: ElectrumHostParams
87
+ ) {
88
+ let url = parseElectrumUrl(server);
89
+ return getElectrumClient(url, options?.timeout ?? 120000, network);
137
90
  }
138
91
 
139
92
  function getElectrumClient(
@@ -141,12 +94,19 @@ function getElectrumClient(
141
94
  timeout: number,
142
95
  network: Network
143
96
  ) {
144
- return new ElectrumClient(
145
- getUserAgent(),
146
- ELECTRUM_CASH_PROTOCOL_VERSION,
97
+ if (params.scheme?.includes("tcp")) {
98
+ throw Error("TCP connections are not supported.");
99
+ }
100
+
101
+ const webSocket = new ElectrumWebSocket(
147
102
  params.host,
148
103
  params.port,
149
- params.scheme,
104
+ params.scheme === "wss",
150
105
  timeout
151
106
  );
107
+ return new ElectrumClient(
108
+ getUserAgent(),
109
+ ELECTRUM_CASH_PROTOCOL_VERSION,
110
+ webSocket
111
+ );
152
112
  }
@@ -1,4 +1,4 @@
1
- import { ElectrumClient } from "electrum-cash";
1
+ import { ElectrumClient } from "@electrum-cash/network";
2
2
  import { getNetworkProvider } from ".";
3
3
  import { Network } from "../interface";
4
4
 
@@ -6,9 +6,7 @@ test("Should get a transaction with bare electrum", async () => {
6
6
  const electrum = new ElectrumClient(
7
7
  "Electrum client example",
8
8
  "1.4.1",
9
- "bch.imaginary.cash",
10
- 50004,
11
- "wss"
9
+ "bch.imaginary.cash"
12
10
  );
13
11
  await electrum.connect();
14
12
  const transactionID =
@@ -1,4 +1,3 @@
1
- import { ClusterOrder } from "electrum-cash";
2
1
  import { NFTCapability } from "../interface.js";
3
2
 
4
3
  export interface BlockHeader {
@@ -9,14 +8,8 @@ export interface BlockHeader {
9
8
  export interface ElectrumHostParams {
10
9
  host: string;
11
10
  port: number;
12
- scheme: "tcp" | "tcp_tls" | "ws" | "wss" | undefined;
13
- }
14
-
15
- export interface ElectrumClusterParams {
16
- confidence: number;
17
- distribution: number;
18
- order: ClusterOrder;
19
- timeout: number;
11
+ timeout?: number;
12
+ scheme: "tcp" | "tcp_tls" | "ws" | "wss";
20
13
  }
21
14
 
22
15
  export interface ElectrumTokenData {
@@ -1,4 +1,3 @@
1
- import { ElectrumTransport, TransportScheme } from "electrum-cash";
2
1
  import { ElectrumHostParams } from "./interface.js";
3
2
 
4
3
  export function parseElectrumUrl(givenUrl: string): ElectrumHostParams {
@@ -10,22 +9,22 @@ export function parseElectrumUrl(givenUrl: string): ElectrumHostParams {
10
9
  }
11
10
 
12
11
  function getElectrumScheme(protocol: string) {
13
- let transport: TransportScheme;
12
+ let transport: string;
14
13
  switch (protocol) {
15
14
  case "http:":
16
- transport = ElectrumTransport.TCP.Scheme;
15
+ transport = "tcp";
17
16
  break;
18
17
  case "https:":
19
- transport = ElectrumTransport.TCP_TLS.Scheme;
18
+ transport = "tcp_tls";
20
19
  break;
21
20
  case "ws:":
22
- transport = ElectrumTransport.WS.Scheme;
21
+ transport = "ws";
23
22
  break;
24
23
  case "wss:":
25
- transport = ElectrumTransport.WSS.Scheme;
24
+ transport = "wss";
26
25
  break;
27
26
  default:
28
27
  throw Error("Electrum transport protocol not understood.");
29
28
  }
30
- return transport as "tcp" | "tcp_tls" | "ws" | "wss" | undefined;
29
+ return transport as "tcp" | "tcp_tls" | "ws" | "wss";
31
30
  }
@@ -35,7 +35,7 @@ describe("Exchange rate tests", () => {
35
35
  const bob = await RegTestWallet.newRandom();
36
36
  const balance = (await alice.getBalance()) as BalanceResponse;
37
37
  let cbCounter = 0;
38
- const cancelWatchFn = alice.watchBalanceUsd(async (newBalance) => {
38
+ const cancelWatchFn = await alice.watchBalanceUsd(async (newBalance) => {
39
39
  cbCounter++;
40
40
  if (cbCounter === 1) {
41
41
  expect(newBalance.usd!).toBeGreaterThan(balance.usd!);
@@ -130,4 +130,5 @@ export async function getRateFromExchange(symbol: string): Promise<number> {
130
130
  throw Error(`Currency '${symbol}' is not supported.`);
131
131
  }
132
132
 
133
- await ExchangeRate.get("usd");
133
+ // do not await and do not throw in case we are offline
134
+ ExchangeRate.get("usd").catch(() => {});
@@ -1,7 +1,13 @@
1
- const json = (param) => {
1
+ export const json = (param: any) => {
2
2
  return JSON.stringify(
3
3
  param,
4
4
  (key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
5
5
  );
6
6
  };
7
7
  export default json;
8
+
9
+ describe("json tests", () => {
10
+ test("Should serialize bigint", async () => {
11
+ expect(json(1n)).toBe('"1"');
12
+ });
13
+ });
@@ -107,7 +107,8 @@ export async function buildP2pkhNonHdTransaction({
107
107
 
108
108
  const tokenValidationResult = verifyTransactionTokens(
109
109
  result.transaction,
110
- sourceOutputs
110
+ sourceOutputs,
111
+ { maximumTokenCommitmentLength: 40 }
111
112
  );
112
113
  if (tokenValidationResult !== true && fee > 0) {
113
114
  throw tokenValidationResult;