mainnet-js 2.7.33 → 3.0.0-next.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 (126) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.7.33.js → mainnet-3.0.0-next.0.js} +718 -678
  3. package/dist/module/cache/MemoryCache.d.ts +0 -1
  4. package/dist/module/cache/MemoryCache.d.ts.map +1 -1
  5. package/dist/module/cache/MemoryCache.js +5 -7
  6. package/dist/module/cache/MemoryCache.js.map +1 -1
  7. package/dist/module/cache/index.d.ts +1 -0
  8. package/dist/module/cache/index.d.ts.map +1 -1
  9. package/dist/module/cache/index.js +1 -0
  10. package/dist/module/cache/index.js.map +1 -1
  11. package/dist/module/cache/walletCache.d.ts +39 -0
  12. package/dist/module/cache/walletCache.d.ts.map +1 -0
  13. package/dist/module/cache/walletCache.js +141 -0
  14. package/dist/module/cache/walletCache.js.map +1 -0
  15. package/dist/module/history/{electrumTransformer.d.ts → getHistory.d.ts} +3 -3
  16. package/dist/module/history/getHistory.d.ts.map +1 -0
  17. package/dist/module/history/{electrumTransformer.js → getHistory.js} +47 -14
  18. package/dist/module/history/getHistory.js.map +1 -0
  19. package/dist/module/index.d.ts +3 -1
  20. package/dist/module/index.d.ts.map +1 -1
  21. package/dist/module/index.js +3 -1
  22. package/dist/module/index.js.map +1 -1
  23. package/dist/module/interface.d.ts +7 -1
  24. package/dist/module/interface.d.ts.map +1 -1
  25. package/dist/module/interface.js.map +1 -1
  26. package/dist/module/message/interface.d.ts +2 -2
  27. package/dist/module/message/interface.d.ts.map +1 -1
  28. package/dist/module/message/interface.js +0 -3
  29. package/dist/module/message/interface.js.map +1 -1
  30. package/dist/module/message/signed.d.ts +5 -5
  31. package/dist/module/message/signed.d.ts.map +1 -1
  32. package/dist/module/message/signed.js +8 -8
  33. package/dist/module/message/signed.js.map +1 -1
  34. package/dist/module/network/ElectrumNetworkProvider.d.ts +2 -2
  35. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  36. package/dist/module/network/ElectrumNetworkProvider.js +2 -1
  37. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  38. package/dist/module/network/NetworkProvider.d.ts +2 -2
  39. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  40. package/dist/module/network/constant.js +4 -4
  41. package/dist/module/network/constant.js.map +1 -1
  42. package/dist/module/transaction/Wif.d.ts +21 -19
  43. package/dist/module/transaction/Wif.d.ts.map +1 -1
  44. package/dist/module/transaction/Wif.js +18 -15
  45. package/dist/module/transaction/Wif.js.map +1 -1
  46. package/dist/module/util/checkUtxos.d.ts +2 -2
  47. package/dist/module/util/checkUtxos.d.ts.map +1 -1
  48. package/dist/module/util/checkUtxos.js +11 -12
  49. package/dist/module/util/checkUtxos.js.map +1 -1
  50. package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
  51. package/dist/module/util/deriveCashaddr.js +6 -0
  52. package/dist/module/util/deriveCashaddr.js.map +1 -1
  53. package/dist/module/util/deriveNetwork.js +1 -1
  54. package/dist/module/util/deriveNetwork.js.map +1 -1
  55. package/dist/module/util/hd.d.ts +3 -0
  56. package/dist/module/util/hd.d.ts.map +1 -0
  57. package/dist/module/util/hd.js +11 -0
  58. package/dist/module/util/hd.js.map +1 -0
  59. package/dist/module/util/index.d.ts +1 -0
  60. package/dist/module/util/index.d.ts.map +1 -1
  61. package/dist/module/util/index.js +1 -0
  62. package/dist/module/util/index.js.map +1 -1
  63. package/dist/module/util/sumUtxoValue.d.ts +3 -3
  64. package/dist/module/util/sumUtxoValue.d.ts.map +1 -1
  65. package/dist/module/util/sumUtxoValue.js.map +1 -1
  66. package/dist/module/wallet/Base.d.ts +37 -93
  67. package/dist/module/wallet/Base.d.ts.map +1 -1
  68. package/dist/module/wallet/Base.js +83 -250
  69. package/dist/module/wallet/Base.js.map +1 -1
  70. package/dist/module/wallet/HDWallet.d.ts +164 -0
  71. package/dist/module/wallet/HDWallet.d.ts.map +1 -0
  72. package/dist/module/wallet/HDWallet.js +486 -0
  73. package/dist/module/wallet/HDWallet.js.map +1 -0
  74. package/dist/module/wallet/Watch.d.ts +151 -0
  75. package/dist/module/wallet/Watch.d.ts.map +1 -0
  76. package/dist/module/wallet/Watch.js +307 -0
  77. package/dist/module/wallet/Watch.js.map +1 -0
  78. package/dist/module/wallet/Wif.d.ts +23 -29
  79. package/dist/module/wallet/Wif.d.ts.map +1 -1
  80. package/dist/module/wallet/Wif.js +204 -267
  81. package/dist/module/wallet/Wif.js.map +1 -1
  82. package/dist/module/wallet/createWallet.d.ts +7 -1
  83. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  84. package/dist/module/wallet/createWallet.js +26 -17
  85. package/dist/module/wallet/createWallet.js.map +1 -1
  86. package/dist/module/wallet/interface.d.ts +3 -3
  87. package/dist/module/wallet/interface.d.ts.map +1 -1
  88. package/dist/module/wallet/model.d.ts +3 -3
  89. package/dist/module/wallet/model.d.ts.map +1 -1
  90. package/dist/module/wallet/model.js +2 -18
  91. package/dist/module/wallet/model.js.map +1 -1
  92. package/dist/tsconfig.tsbuildinfo +1 -1
  93. package/package.json +1 -1
  94. package/src/cache/MemoryCache.ts +5 -5
  95. package/src/cache/index.ts +1 -0
  96. package/src/cache/walletCache.ts +252 -0
  97. package/src/history/{electrumTransformer.test.ts → getHistory.test.ts} +6 -19
  98. package/src/history/{electrumTransformer.ts → getHistory.ts} +63 -15
  99. package/src/index.ts +3 -1
  100. package/src/interface.ts +8 -1
  101. package/src/message/interface.ts +2 -28
  102. package/src/message/signed.test.ts +36 -48
  103. package/src/message/signed.ts +9 -12
  104. package/src/network/ElectrumNetworkProvider.ts +4 -4
  105. package/src/network/NetworkProvider.ts +2 -2
  106. package/src/network/Rpc.test.ts +1 -1
  107. package/src/network/constant.ts +4 -4
  108. package/src/transaction/Wif.ts +41 -35
  109. package/src/util/checkUtxos.ts +21 -26
  110. package/src/util/deriveCashaddr.ts +8 -0
  111. package/src/util/deriveNetwork.ts +1 -1
  112. package/src/util/derivePublicKeyHash.test.ts +0 -13
  113. package/src/util/hd.ts +16 -0
  114. package/src/util/index.ts +1 -0
  115. package/src/util/sumUtxoValue.ts +5 -5
  116. package/src/wallet/Base.ts +123 -332
  117. package/src/wallet/HDWallet.test.ts +372 -0
  118. package/src/wallet/HDWallet.ts +764 -0
  119. package/src/wallet/Watch.ts +447 -0
  120. package/src/wallet/Wif.ts +258 -283
  121. package/src/wallet/createWallet.ts +28 -18
  122. package/src/wallet/interface.ts +3 -3
  123. package/src/wallet/model.test.ts +2 -2
  124. package/src/wallet/model.ts +6 -23
  125. package/dist/module/history/electrumTransformer.d.ts.map +0 -1
  126. package/dist/module/history/electrumTransformer.js.map +0 -1
@@ -1,31 +1,26 @@
1
- import { UtxoI } from "../interface.js";
1
+ import { Utxo, UtxoId } from "../interface.js";
2
2
 
3
3
  export async function checkUtxos(
4
- utxos: UtxoI[],
5
- wallet: import("../wallet/Wif.js").Wallet
6
- ): Promise<UtxoI[]> {
7
- if (utxos.some((val) => val.satoshis === 0)) {
8
- const addressUtxos = await wallet.getAddressUtxos(wallet.cashaddr);
9
- const absent = utxos.filter(
10
- (val) =>
11
- !addressUtxos.find(
12
- (utxo) => val.txid === utxo.txid && val.vout === utxo.vout
13
- )
14
- );
15
- if (absent.length) {
16
- const absentString = absent
17
- .map((val) => `${val.txid}:${val.vout}`)
18
- .join(", ");
19
- throw Error(`Utxos [${absentString}] not found in wallet`);
20
- }
21
-
22
- utxos = utxos.map(
23
- (val) =>
24
- addressUtxos.find(
25
- (utxo) => val.txid === utxo.txid && val.vout === utxo.vout
26
- )!
27
- );
4
+ utxoIds: UtxoId[],
5
+ walletOrUtxos: Utxo[] | import("../wallet/Wif.js").Wallet
6
+ ): Promise<Utxo[]> {
7
+ const addressUtxos = Array.isArray(walletOrUtxos)
8
+ ? walletOrUtxos
9
+ : await walletOrUtxos.getUtxos();
10
+ const absent = utxoIds.filter(
11
+ (val) =>
12
+ !addressUtxos.find(
13
+ (utxo) => val.txid === utxo.txid && val.vout === utxo.vout
14
+ )
15
+ );
16
+ if (absent.length) {
17
+ const absentString = absent
18
+ .map((val) => `${val.txid}:${val.vout}`)
19
+ .join(", ");
20
+ throw Error(`Utxos [${absentString}] not found in wallet`);
28
21
  }
29
22
 
30
- return utxos;
23
+ return addressUtxos.filter((val) =>
24
+ utxoIds.find((utxo) => val.txid === utxo.txid && val.vout === utxo.vout)
25
+ );
31
26
  }
@@ -80,6 +80,10 @@ function decodeAddress(address: string): DecodedCashAddress {
80
80
  }
81
81
 
82
82
  export function toCashaddr(address: string): string {
83
+ if (address.indexOf(":q") > 0 || address.indexOf(":p") > 0) {
84
+ return address;
85
+ }
86
+
83
87
  const result = decodeAddress(address);
84
88
 
85
89
  return encodeCashAddress({
@@ -90,6 +94,10 @@ export function toCashaddr(address: string): string {
90
94
  }
91
95
 
92
96
  export function toTokenaddr(address: string): string {
97
+ if (address.indexOf(":z") > 0 || address.indexOf(":r") > 0) {
98
+ return address;
99
+ }
100
+
93
101
  const result = decodeAddress(address);
94
102
 
95
103
  return encodeCashAddress({
@@ -10,7 +10,7 @@ export function derivedNetwork(addresses: string[]) {
10
10
  // Get a unique set of networks requested
11
11
  let networkSet = new Set(networks);
12
12
  if (networkSet.size > 1) {
13
- throw Error("Contract addresses are not on the same network");
13
+ throw Error("Addresses are not on the same network");
14
14
  } else if (networkSet.size === 0) {
15
15
  throw Error("No address network was derived");
16
16
  } else {
@@ -53,16 +53,3 @@ test("Should calculate public key hash consistent with hash160", async () => {
53
53
  expect(binToHex(pkh2)).toEqual("d986ed01b7a22225a70edbf2ba7cfb63a15cb3aa");
54
54
  expect(binToHex(pkh3)).toEqual("d986ed01b7a22225a70edbf2ba7cfb63a15cb3aa");
55
55
  });
56
-
57
- test("Should return the a public key hash of manually set wallet", async () => {
58
- let w = new Wallet();
59
- // @ts-ignore
60
- w.privateKey = hexToBin(
61
- "e284129cc0922579a535bbf4d1a3b25773090d28c909bc0fed73b5e0222cc372"
62
- );
63
- // @ts-ignore
64
- await w.deriveInfo();
65
- let pkh = derivePublicKeyHash(w.getDepositAddress());
66
- let expectedPKH = "d986ed01b7a22225a70edbf2ba7cfb63a15cb3aa";
67
- expect(binToHex(pkh)).toBe(expectedPKH);
68
- });
package/src/util/hd.ts ADDED
@@ -0,0 +1,16 @@
1
+ export const arrayRange = (start: number, stop: number) =>
2
+ Array.from({ length: stop - start }, (_, index) => start + index);
3
+
4
+ export const getNextUnusedIndex = (
5
+ index: number,
6
+ statuses: Array<string | null>
7
+ ): number => {
8
+ if (index === -1) {
9
+ index = statuses.findIndex((status) => status === null);
10
+ if (index === -1) {
11
+ index = statuses.length;
12
+ }
13
+ }
14
+
15
+ return index;
16
+ };
package/src/util/index.ts CHANGED
@@ -35,3 +35,4 @@ export {
35
35
  } from "./balanceObjectFromSatoshi.js";
36
36
  export * from "./sumUtxoValue.js";
37
37
  export { decodeHeader } from "./header.js";
38
+ export * from "./checkUtxos.js";
@@ -1,8 +1,8 @@
1
- import { UtxoI } from "../interface.js";
1
+ import { Utxo } from "../interface.js";
2
2
 
3
- export function sumUtxoValue(utxos: UtxoI[]) {
3
+ export function sumUtxoValue(utxos: Utxo[]) {
4
4
  if (utxos.length > 0) {
5
- const balanceArray: number[] = utxos.map((o: UtxoI) => {
5
+ const balanceArray: number[] = utxos.map((o: Utxo) => {
6
6
  return o.satoshis;
7
7
  });
8
8
  const balance = balanceArray.reduce((a: number, b: number) => a + b, 0);
@@ -12,11 +12,11 @@ export function sumUtxoValue(utxos: UtxoI[]) {
12
12
  }
13
13
  }
14
14
 
15
- export function sumTokenAmounts(utxos: UtxoI[], tokenId: string): bigint {
15
+ export function sumTokenAmounts(utxos: Utxo[], tokenId: string): bigint {
16
16
  if (utxos.length > 0) {
17
17
  const tokenArray: bigint[] = utxos
18
18
  .filter((utxo) => utxo.token?.tokenId === tokenId)
19
- .map((o: UtxoI) => {
19
+ .map((o: Utxo) => {
20
20
  return o.token?.amount || 0n;
21
21
  });
22
22
  const balance = tokenArray.reduce((a: bigint, b: bigint) => a + b, 0n);