mainnet-js 2.3.16 → 2.4.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 (30) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.3.16.js → mainnet-2.4.0.js} +3 -3
  3. package/dist/module/history/electrumTransformer.d.ts +11 -3
  4. package/dist/module/history/electrumTransformer.d.ts.map +1 -1
  5. package/dist/module/history/electrumTransformer.js +199 -195
  6. package/dist/module/history/electrumTransformer.js.map +1 -1
  7. package/dist/module/history/interface.d.ts +19 -13
  8. package/dist/module/history/interface.d.ts.map +1 -1
  9. package/dist/module/network/ElectrumNetworkProvider.d.ts +2 -2
  10. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  11. package/dist/module/network/ElectrumNetworkProvider.js +6 -6
  12. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  13. package/dist/module/network/NetworkProvider.d.ts +1 -1
  14. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  15. package/dist/module/util/header.d.ts.map +1 -1
  16. package/dist/module/util/header.js.map +1 -1
  17. package/dist/module/wallet/Wif.d.ts +25 -4
  18. package/dist/module/wallet/Wif.d.ts.map +1 -1
  19. package/dist/module/wallet/Wif.js +29 -7
  20. package/dist/module/wallet/Wif.js.map +1 -1
  21. package/dist/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +1 -1
  23. package/src/history/electrumTransformer.test.ts +112 -55
  24. package/src/history/electrumTransformer.ts +279 -284
  25. package/src/history/interface.ts +19 -13
  26. package/src/network/ElectrumNetworkProvider.ts +32 -9
  27. package/src/network/NetworkProvider.ts +5 -1
  28. package/src/util/header.test.ts +14 -8
  29. package/src/util/header.ts +1 -1
  30. package/src/wallet/Wif.ts +53 -19
@@ -5,7 +5,13 @@ import {
5
5
  ConnectionStatus,
6
6
  } from "electrum-cash";
7
7
  import { default as NetworkProvider } from "./NetworkProvider.js";
8
- import { HexHeaderI, TxI, UtxoI, ElectrumBalanceI, HeaderI } from "../interface.js";
8
+ import {
9
+ HexHeaderI,
10
+ TxI,
11
+ UtxoI,
12
+ ElectrumBalanceI,
13
+ HeaderI,
14
+ } from "../interface.js";
9
15
  import { Network } from "../interface.js";
10
16
  import { delay } from "../util/delay.js";
11
17
  import { ElectrumRawTransaction, ElectrumUtxo } from "./interface.js";
@@ -107,7 +113,10 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
107
113
  }
108
114
 
109
115
  static rawHeaderCache = {};
110
- async getHeader(height: number, verbose: boolean = false): Promise<HeaderI | HexHeaderI> {
116
+ async getHeader(
117
+ height: number,
118
+ verbose: boolean = false
119
+ ): Promise<HeaderI | HexHeaderI> {
111
120
  const key = `header-${this.network}-${height}-${verbose}`;
112
121
 
113
122
  if (Config.UseLocalStorageCache) {
@@ -119,7 +128,10 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
119
128
  ElectrumNetworkProvider.rawTransactionCache[key];
120
129
  }
121
130
 
122
- const result = await this.performRequest("blockchain.header.get", height) as HexHeaderI;
131
+ const result = (await this.performRequest(
132
+ "blockchain.header.get",
133
+ height
134
+ )) as HexHeaderI;
123
135
  if (Config.UseLocalStorageCache) {
124
136
  localStorage.setItem(key, JSON.stringify(result));
125
137
  } else {
@@ -243,10 +255,16 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
243
255
  }
244
256
 
245
257
  // Get transaction history of a given cashaddr
246
- async getHistory(cashaddr: string): Promise<TxI[]> {
258
+ async getHistory(
259
+ cashaddr: string,
260
+ fromHeight: number = 0,
261
+ toHeight: number = -1
262
+ ): Promise<TxI[]> {
247
263
  const result = (await this.performRequest(
248
264
  "blockchain.address.get_history",
249
- cashaddr
265
+ cashaddr,
266
+ fromHeight,
267
+ toHeight
250
268
  )) as TxI[];
251
269
 
252
270
  return result;
@@ -340,9 +358,12 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
340
358
  });
341
359
  }
342
360
 
343
- // Wait for the next block or a block at given blockchain height.
344
- public watchBlocks(callback: (header: HexHeaderI) => void): CancelWatchFn {
345
- let acknowledged = false;
361
+ // watch for block headers and block height, if `skipCurrentHeight` is set, the notification about current block will not arrive
362
+ public watchBlocks(
363
+ callback: (header: HexHeaderI) => void,
364
+ skipCurrentHeight: boolean = true
365
+ ): CancelWatchFn {
366
+ let acknowledged = !skipCurrentHeight;
346
367
  const waitForBlockCallback = (_header: HexHeaderI | HexHeaderI[]) => {
347
368
  if (!acknowledged) {
348
369
  acknowledged = true;
@@ -372,7 +393,9 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
372
393
  }
373
394
 
374
395
  // subscribe to notifications sent when new block is found, the block header is sent to callback
375
- async subscribeToHeaders(callback: (header: HexHeaderI) => void): Promise<void> {
396
+ async subscribeToHeaders(
397
+ callback: (header: HexHeaderI) => void
398
+ ): Promise<void> {
376
399
  await this.subscribeRequest("blockchain.headers.subscribe", callback);
377
400
  }
378
401
 
@@ -71,7 +71,11 @@ export default interface NetworkProvider {
71
71
  * @throws {Error} When failing to get history.
72
72
  * @returns Array of transactions.
73
73
  */
74
- getHistory(cashaddr: string): Promise<TxI[]>;
74
+ getHistory(
75
+ cashaddr: string,
76
+ fromHeight?: number,
77
+ toHeight?: number
78
+ ): Promise<TxI[]>;
75
79
 
76
80
  /**
77
81
  * Wait for the next block or a block at given blockchain height.
@@ -9,20 +9,26 @@ afterAll(async () => {
9
9
  await disconnectProviders();
10
10
  });
11
11
 
12
- describe('header tests', () => {
13
- it('decodeHeader', async () => {
12
+ describe("header tests", () => {
13
+ it("decodeHeader", async () => {
14
14
  const wallet = await Wallet.newRandom();
15
- const hexHeader = await wallet.provider!.getHeader(854724) as HexHeaderI;
15
+ const hexHeader = (await wallet.provider!.getHeader(854724)) as HexHeaderI;
16
16
  expect(hexHeader.height).toBe(854724);
17
- expect(hexHeader.hex).toBe("0080c4339674a81d4e35a5b590b15a6b69f93b7b22bd14845b3517000000000000000000128a8b776c82fda87f60c6fdb0de26f021cdf39ffd835dc309eb1fc6bbfaac343e2f96662d5202184db2428f");
17
+ expect(hexHeader.hex).toBe(
18
+ "0080c4339674a81d4e35a5b590b15a6b69f93b7b22bd14845b3517000000000000000000128a8b776c82fda87f60c6fdb0de26f021cdf39ffd835dc309eb1fc6bbfaac343e2f96662d5202184db2428f"
19
+ );
18
20
 
19
- const header = await wallet.provider!.getHeader(854724, true) as HeaderI;
21
+ const header = (await wallet.provider!.getHeader(854724, true)) as HeaderI;
20
22
  expect(header.version).toBe(868515840);
21
- expect(header.previousBlockHash).toBe("00000000000000000017355b8414bd227b3bf9696b5ab190b5a5354e1da87496");
22
- expect(header.merkleRoot).toBe("34acfabbc61feb09c35d83fd9ff3cd21f026deb0fdc6607fa8fd826c778b8a12");
23
+ expect(header.previousBlockHash).toBe(
24
+ "00000000000000000017355b8414bd227b3bf9696b5ab190b5a5354e1da87496"
25
+ );
26
+ expect(header.merkleRoot).toBe(
27
+ "34acfabbc61feb09c35d83fd9ff3cd21f026deb0fdc6607fa8fd826c778b8a12"
28
+ );
23
29
  expect(header.timestamp).toBe(1721118526);
24
30
  expect(header.bits).toBe(402805293);
25
31
  expect(header.nonce).toBe(2403512909);
26
32
  expect(header.height).toBe(854724);
27
33
  });
28
- });
34
+ });
@@ -13,4 +13,4 @@ export const decodeHeader = (hexHeader: HexHeaderI): HeaderI => {
13
13
  result.height = hexHeader.height;
14
14
 
15
15
  return result;
16
- }
16
+ };
package/src/wallet/Wif.ts CHANGED
@@ -2,7 +2,6 @@
2
2
  // Stable
3
3
  import {
4
4
  decodeCashAddress,
5
- decodeCashAddressVersionByte,
6
5
  encodeHdPublicKey,
7
6
  HdKeyNetwork,
8
7
  secp256k1,
@@ -104,14 +103,14 @@ import { amountInSatoshi } from "../util/amountInSatoshi.js";
104
103
  import { getXPubKey } from "../util/getXPubKey.js";
105
104
  import { DERIVATION_PATHS, DUST_UTXO_THRESHOLD } from "../constant.js";
106
105
 
107
- import { TransactionHistoryI } from "../history/interface.js";
108
106
  import { getAddressHistory } from "../history/electrumTransformer.js";
109
- import { IdentitySnapshot, Registry } from "./bcmr-v2.schema.js";
107
+ import { IdentitySnapshot } from "./bcmr-v2.schema.js";
110
108
  import { BCMR } from "./Bcmr.js";
111
109
  import { qrAddress } from "../qr/Qr.js";
112
110
  import { ImageI } from "../qr/interface.js";
113
111
  import { Config } from "../config.js";
114
112
  import { checkUtxos } from "../util/checkUtxos.js";
113
+ import { TransactionHistoryItem } from "../history/interface.js";
115
114
 
116
115
  //#endregion Imports
117
116
 
@@ -1282,25 +1281,53 @@ export class Wallet extends BaseWallet {
1282
1281
  }
1283
1282
 
1284
1283
  // gets transaction history of this wallet
1285
- public async getRawHistory(): Promise<TxI[]> {
1286
- return await this.provider!.getHistory(this.cashaddr!);
1284
+ public async getRawHistory(
1285
+ fromHeight: number = 0,
1286
+ toHeight: number = -1
1287
+ ): Promise<TxI[]> {
1288
+ return await this.provider!.getHistory(
1289
+ this.cashaddr!,
1290
+ fromHeight,
1291
+ toHeight
1292
+ );
1287
1293
  }
1288
1294
 
1289
- // gets transaction history of this wallet
1290
- public async getHistory(
1291
- unit: UnitEnum,
1292
- start?: number,
1293
- count?: number,
1294
- collapseChange?: boolean
1295
- ): Promise<TransactionHistoryI> {
1296
- return getAddressHistory(
1297
- this.cashaddr!,
1298
- this.provider!,
1295
+ /**
1296
+ * getHistory gets transaction history of this wallet with most data decoded and ready to present to user
1297
+ * @note balance calculations are valid only if querying to the blockchain tip (`toHeight` === -1, `count` === -1)
1298
+ * @note this method is heavy on network calls, if invoked in browser use of cache is advised, @see `Config.UseLocalStorageCache`
1299
+ * @note this method tries to recreate the history tab view of Electron Cash wallet, however, it may not be 100% accurate if the tnransaction value changes are the same in the same block (ordering)
1300
+ *
1301
+ * @param unit optional, BCH or currency unit to present balance and balance changes. If unit is currency like USD or EUR, balances will be subject to possible rounding errors. Default 0
1302
+ * @param fromHeight optional, if set, history will be limited. Default 0
1303
+ * @param toHeight optional, if set, history will be limited. Default -1, meaning that all history items will be returned, including mempool
1304
+ * @param start optional, if set, the result set will be paginated with offset `start`
1305
+ * @param count optional, if set, the result set will be paginated with `count`. Default -1, meaning that all history items will be returned
1306
+ *
1307
+ * @returns an array of transaction history items, with input values and addresses encoded in cashaddress format. @see `TransactionHistoryItem` type
1308
+ */
1309
+ public async getHistory({
1310
+ unit = "sat",
1311
+ fromHeight = 0,
1312
+ toHeight = -1,
1313
+ start = 0,
1314
+ count = -1,
1315
+ }: {
1316
+ unit?: UnitEnum;
1317
+ fromHeight?: number;
1318
+ toHeight?: number;
1319
+ start?: number;
1320
+ count?: number;
1321
+ }): Promise<TransactionHistoryItem[]> {
1322
+ return getAddressHistory({
1323
+ address: this.cashaddr!,
1324
+ provider: this.provider!,
1299
1325
  unit,
1326
+ fromHeight,
1327
+ toHeight,
1300
1328
  start,
1301
1329
  count,
1302
- collapseChange
1303
- );
1330
+ });
1304
1331
  }
1305
1332
 
1306
1333
  // gets last transaction of this wallet
@@ -1393,11 +1420,18 @@ export class Wallet extends BaseWallet {
1393
1420
  * watchBlocks Watch network blocks
1394
1421
  *
1395
1422
  * @param callback callback with a block header object
1423
+ * @param skipCurrentHeight if set, the notification about current block will not arrive
1396
1424
  *
1397
1425
  * @returns a function which will cancel watching upon evaluation
1398
1426
  */
1399
- public watchBlocks(callback: (header: HexHeaderI) => void): CancelWatchFn {
1400
- return (this.provider! as ElectrumNetworkProvider).watchBlocks(callback);
1427
+ public watchBlocks(
1428
+ callback: (header: HexHeaderI) => void,
1429
+ skipCurrentHeight: boolean = true
1430
+ ): CancelWatchFn {
1431
+ return (this.provider! as ElectrumNetworkProvider).watchBlocks(
1432
+ callback,
1433
+ skipCurrentHeight
1434
+ );
1401
1435
  }
1402
1436
 
1403
1437
  /**