mainnet-js 2.7.34 → 3.0.0-next.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 (195) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.7.34.js → mainnet-3.0.0-next.1.js} +728 -708
  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 +45 -0
  12. package/dist/module/cache/walletCache.d.ts.map +1 -0
  13. package/dist/module/cache/walletCache.js +140 -0
  14. package/dist/module/cache/walletCache.js.map +1 -0
  15. package/dist/module/chain.d.ts +1 -1
  16. package/dist/module/chain.js +1 -1
  17. package/dist/module/chain.js.map +1 -1
  18. package/dist/module/constant.d.ts +1 -1
  19. package/dist/module/constant.d.ts.map +1 -1
  20. package/dist/module/constant.js +1 -1
  21. package/dist/module/constant.js.map +1 -1
  22. package/dist/module/enum.d.ts +1 -7
  23. package/dist/module/enum.d.ts.map +1 -1
  24. package/dist/module/enum.js +0 -6
  25. package/dist/module/enum.js.map +1 -1
  26. package/dist/module/history/{electrumTransformer.d.ts → getHistory.d.ts} +3 -3
  27. package/dist/module/history/getHistory.d.ts.map +1 -0
  28. package/dist/module/history/{electrumTransformer.js → getHistory.js} +17 -14
  29. package/dist/module/history/getHistory.js.map +1 -0
  30. package/dist/module/index.d.ts +3 -1
  31. package/dist/module/index.d.ts.map +1 -1
  32. package/dist/module/index.js +3 -1
  33. package/dist/module/index.js.map +1 -1
  34. package/dist/module/interface.d.ts +7 -2
  35. package/dist/module/interface.d.ts.map +1 -1
  36. package/dist/module/interface.js.map +1 -1
  37. package/dist/module/message/interface.d.ts +2 -2
  38. package/dist/module/message/interface.d.ts.map +1 -1
  39. package/dist/module/message/interface.js +0 -3
  40. package/dist/module/message/interface.js.map +1 -1
  41. package/dist/module/message/signed.d.ts +5 -5
  42. package/dist/module/message/signed.d.ts.map +1 -1
  43. package/dist/module/message/signed.js +8 -8
  44. package/dist/module/message/signed.js.map +1 -1
  45. package/dist/module/network/ElectrumNetworkProvider.d.ts +2 -2
  46. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  47. package/dist/module/network/ElectrumNetworkProvider.js +3 -2
  48. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  49. package/dist/module/network/NetworkProvider.d.ts +2 -2
  50. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  51. package/dist/module/network/constant.js +4 -4
  52. package/dist/module/network/constant.js.map +1 -1
  53. package/dist/module/network/getRelayFeeCache.js +2 -2
  54. package/dist/module/network/getRelayFeeCache.js.map +1 -1
  55. package/dist/module/rate/ExchangeRate.d.ts +2 -1
  56. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  57. package/dist/module/rate/ExchangeRate.js +4 -1
  58. package/dist/module/rate/ExchangeRate.js.map +1 -1
  59. package/dist/module/transaction/Wif.d.ts +25 -23
  60. package/dist/module/transaction/Wif.d.ts.map +1 -1
  61. package/dist/module/transaction/Wif.js +26 -25
  62. package/dist/module/transaction/Wif.js.map +1 -1
  63. package/dist/module/transaction/allocateFee.d.ts +3 -3
  64. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  65. package/dist/module/transaction/allocateFee.js +5 -6
  66. package/dist/module/transaction/allocateFee.js.map +1 -1
  67. package/dist/module/util/amountInSatoshi.d.ts +1 -1
  68. package/dist/module/util/amountInSatoshi.d.ts.map +1 -1
  69. package/dist/module/util/amountInSatoshi.js +3 -9
  70. package/dist/module/util/amountInSatoshi.js.map +1 -1
  71. package/dist/module/util/asSendRequestObject.d.ts.map +1 -1
  72. package/dist/module/util/asSendRequestObject.js +10 -7
  73. package/dist/module/util/asSendRequestObject.js.map +1 -1
  74. package/dist/module/util/checkUtxos.d.ts +2 -2
  75. package/dist/module/util/checkUtxos.d.ts.map +1 -1
  76. package/dist/module/util/checkUtxos.js +11 -12
  77. package/dist/module/util/checkUtxos.js.map +1 -1
  78. package/dist/module/util/convert.d.ts +3 -0
  79. package/dist/module/util/convert.d.ts.map +1 -1
  80. package/dist/module/util/convert.js +12 -0
  81. package/dist/module/util/convert.js.map +1 -1
  82. package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
  83. package/dist/module/util/deriveCashaddr.js +6 -0
  84. package/dist/module/util/deriveCashaddr.js.map +1 -1
  85. package/dist/module/util/deriveNetwork.js +1 -1
  86. package/dist/module/util/deriveNetwork.js.map +1 -1
  87. package/dist/module/util/hd.d.ts +3 -0
  88. package/dist/module/util/hd.d.ts.map +1 -0
  89. package/dist/module/util/hd.js +11 -0
  90. package/dist/module/util/hd.js.map +1 -0
  91. package/dist/module/util/index.d.ts +3 -3
  92. package/dist/module/util/index.d.ts.map +1 -1
  93. package/dist/module/util/index.js +3 -3
  94. package/dist/module/util/index.js.map +1 -1
  95. package/dist/module/util/satoshiToAmount.d.ts +1 -1
  96. package/dist/module/util/satoshiToAmount.d.ts.map +1 -1
  97. package/dist/module/util/satoshiToAmount.js +3 -9
  98. package/dist/module/util/satoshiToAmount.js.map +1 -1
  99. package/dist/module/util/sumSendRequestAmounts.d.ts.map +1 -1
  100. package/dist/module/util/sumSendRequestAmounts.js +3 -4
  101. package/dist/module/util/sumSendRequestAmounts.js.map +1 -1
  102. package/dist/module/util/sumUtxoValue.d.ts +3 -3
  103. package/dist/module/util/sumUtxoValue.d.ts.map +1 -1
  104. package/dist/module/util/sumUtxoValue.js +2 -2
  105. package/dist/module/util/sumUtxoValue.js.map +1 -1
  106. package/dist/module/wallet/Base.d.ts +45 -103
  107. package/dist/module/wallet/Base.d.ts.map +1 -1
  108. package/dist/module/wallet/Base.js +99 -298
  109. package/dist/module/wallet/Base.js.map +1 -1
  110. package/dist/module/wallet/HDWallet.d.ts +164 -0
  111. package/dist/module/wallet/HDWallet.d.ts.map +1 -0
  112. package/dist/module/wallet/HDWallet.js +486 -0
  113. package/dist/module/wallet/HDWallet.js.map +1 -0
  114. package/dist/module/wallet/Util.js +1 -1
  115. package/dist/module/wallet/Util.js.map +1 -1
  116. package/dist/module/wallet/Watch.d.ts +151 -0
  117. package/dist/module/wallet/Watch.d.ts.map +1 -0
  118. package/dist/module/wallet/Watch.js +307 -0
  119. package/dist/module/wallet/Watch.js.map +1 -0
  120. package/dist/module/wallet/Wif.d.ts +23 -29
  121. package/dist/module/wallet/Wif.d.ts.map +1 -1
  122. package/dist/module/wallet/Wif.js +204 -267
  123. package/dist/module/wallet/Wif.js.map +1 -1
  124. package/dist/module/wallet/createWallet.d.ts +7 -1
  125. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  126. package/dist/module/wallet/createWallet.js +26 -17
  127. package/dist/module/wallet/createWallet.js.map +1 -1
  128. package/dist/module/wallet/interface.d.ts +5 -6
  129. package/dist/module/wallet/interface.d.ts.map +1 -1
  130. package/dist/module/wallet/model.d.ts +15 -19
  131. package/dist/module/wallet/model.d.ts.map +1 -1
  132. package/dist/module/wallet/model.js +5 -25
  133. package/dist/module/wallet/model.js.map +1 -1
  134. package/dist/tsconfig.tsbuildinfo +1 -1
  135. package/package.json +1 -1
  136. package/src/cache/MemoryCache.ts +5 -5
  137. package/src/cache/index.ts +1 -0
  138. package/src/cache/walletCache.ts +239 -0
  139. package/src/chain.ts +1 -1
  140. package/src/constant.ts +1 -1
  141. package/src/enum.ts +0 -6
  142. package/src/history/{electrumTransformer.test.ts → getHistory.test.ts} +26 -53
  143. package/src/history/{electrumTransformer.ts → getHistory.ts} +31 -15
  144. package/src/index.ts +3 -1
  145. package/src/interface.ts +8 -2
  146. package/src/message/interface.ts +2 -28
  147. package/src/message/signed.test.ts +36 -48
  148. package/src/message/signed.ts +9 -12
  149. package/src/network/Connection.test.ts +3 -3
  150. package/src/network/ElectrumNetworkProvider.ts +5 -5
  151. package/src/network/NetworkProvider.ts +2 -2
  152. package/src/network/Rpc.test.ts +3 -4
  153. package/src/network/constant.ts +4 -4
  154. package/src/network/getRelayFeeCache.ts +2 -2
  155. package/src/rate/ExchangeRate.test.ts +2 -44
  156. package/src/rate/ExchangeRate.ts +5 -2
  157. package/src/transaction/Wif.ts +59 -52
  158. package/src/transaction/allocateFee.test.ts +110 -131
  159. package/src/transaction/allocateFee.ts +14 -15
  160. package/src/util/amountInSatoshi.test.ts +1 -9
  161. package/src/util/amountInSatoshi.ts +6 -10
  162. package/src/util/asSendRequestObject.ts +12 -7
  163. package/src/util/checkUtxos.ts +21 -26
  164. package/src/util/convert.ts +18 -0
  165. package/src/util/deriveCashaddr.ts +8 -0
  166. package/src/util/deriveNetwork.ts +1 -1
  167. package/src/util/derivePublicKeyHash.test.ts +0 -13
  168. package/src/util/hd.ts +16 -0
  169. package/src/util/index.ts +3 -7
  170. package/src/util/satoshiToAmount.test.ts +1 -1
  171. package/src/util/satoshiToAmount.ts +4 -10
  172. package/src/util/sumSendRequestAmounts.ts +3 -4
  173. package/src/util/sumUtxoValue.ts +7 -7
  174. package/src/wallet/Base.ts +147 -420
  175. package/src/wallet/Cashtokens.test.headless.js +11 -11
  176. package/src/wallet/Cashtokens.test.ts +36 -37
  177. package/src/wallet/HDWallet.test.ts +515 -0
  178. package/src/wallet/HDWallet.ts +764 -0
  179. package/src/wallet/Util.ts +1 -1
  180. package/src/wallet/Watch.ts +447 -0
  181. package/src/wallet/Wif.bip39.test.ts +1 -1
  182. package/src/wallet/Wif.test.ts +108 -133
  183. package/src/wallet/Wif.ts +258 -283
  184. package/src/wallet/createWallet.ts +28 -18
  185. package/src/wallet/interface.ts +5 -6
  186. package/src/wallet/model.test.ts +4 -7
  187. package/src/wallet/model.ts +19 -51
  188. package/dist/module/history/electrumTransformer.d.ts.map +0 -1
  189. package/dist/module/history/electrumTransformer.js.map +0 -1
  190. package/dist/module/util/balanceObjectFromSatoshi.d.ts +0 -8
  191. package/dist/module/util/balanceObjectFromSatoshi.d.ts.map +0 -1
  192. package/dist/module/util/balanceObjectFromSatoshi.js +0 -35
  193. package/dist/module/util/balanceObjectFromSatoshi.js.map +0 -1
  194. package/src/util/balanceObjectFromSatoshi.test.ts +0 -58
  195. package/src/util/balanceObjectFromSatoshi.ts +0 -52
@@ -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
@@ -8,7 +8,7 @@ export {
8
8
  binToBase64,
9
9
  sha256,
10
10
  } from "@bitauth/libauth";
11
- export { convert, convertObject } from "./convert.js";
11
+ export * from "./convert.js";
12
12
  export { delay } from "./delay.js";
13
13
  export { derivedNetwork } from "./deriveNetwork.js";
14
14
  export { derivePublicKeyHash } from "./derivePublicKeyHash.js";
@@ -22,16 +22,12 @@ export {
22
22
  export { getRuntimePlatform, RuntimePlatform } from "./getRuntimePlatform.js";
23
23
  export { getUsdRate } from "./getUsdRate.js";
24
24
  export { hash160 } from "./hash160.js";
25
- export { ExchangeRate } from "../rate/ExchangeRate.js";
25
+ export { ExchangeRate, ExchageRatePromise } from "../rate/ExchangeRate.js";
26
26
  export { sanitizeAddress } from "./sanitizeAddress.js";
27
27
  export { sanitizeUnit } from "./sanitizeUnit.js";
28
28
  export { getWeakRandomInt } from "./randomInt.js";
29
29
  export { getXPubKey } from "../util/getXPubKey.js";
30
30
  export { sumUtxoValue } from "./sumUtxoValue.js";
31
- export {
32
- BalanceResponse,
33
- balanceFromSatoshi,
34
- balanceResponseFromSatoshi,
35
- } from "./balanceObjectFromSatoshi.js";
36
31
  export * from "./sumUtxoValue.js";
37
32
  export { decodeHeader } from "./header.js";
33
+ export * from "./checkUtxos.js";
@@ -1,6 +1,6 @@
1
1
  import { satoshiToAmount } from "./satoshiToAmount";
2
2
 
3
3
  test("get a zero satoshis", async () => {
4
- let zero = await satoshiToAmount(0, "sat");
4
+ let zero = await satoshiToAmount(0n, "sat");
5
5
  expect(zero).toBe(0);
6
6
  });
@@ -12,27 +12,21 @@ import { sanitizeUnit } from "../util/sanitizeUnit.js";
12
12
  * @returns a promise to the value in the unit of account given by rawUnit
13
13
  */
14
14
  export async function satoshiToAmount(
15
- value: number,
15
+ value: bigint,
16
16
  rawUnit: any
17
17
  ): Promise<number> {
18
18
  const unit = sanitizeUnit(rawUnit);
19
19
  switch (unit) {
20
20
  case UnitEnum.BCH:
21
- return value / bchParam.subUnits;
22
- case UnitEnum.SATOSHI:
23
- return value;
21
+ return Number(value) / Number(bchParam.subUnits);
24
22
  case UnitEnum.SAT:
25
- return value;
26
- case UnitEnum.SATS:
27
- return value;
28
- case UnitEnum.SATOSHIS:
29
- return value;
23
+ return Number(value);
30
24
  default:
31
25
  const Currency_over_BCH = await ExchangeRate.get(rawUnit);
32
26
  // truncate currency amounts to fixed precision (2),
33
27
  // then return the fixed value string as a float.
34
28
  const currencyValue = Number(
35
- value * (Currency_over_BCH / bchParam.subUnits)
29
+ Number(value) * (Currency_over_BCH / Number(bchParam.subUnits))
36
30
  ).toFixed(2);
37
31
  return Number.parseFloat(currencyValue);
38
32
  }
@@ -3,7 +3,6 @@ import {
3
3
  SendRequest,
4
4
  TokenSendRequest,
5
5
  } from "../wallet/model.js";
6
- import { amountInSatoshi } from "./amountInSatoshi.js";
7
6
 
8
7
  // This function sums a list of send request objects
9
8
  export async function sumSendRequestAmounts(
@@ -13,10 +12,10 @@ export async function sumSendRequestAmounts(
13
12
  const balanceArray: (BigInt | Error)[] = await Promise.all(
14
13
  requests.map(async (r: SendRequest | TokenSendRequest | OpReturnData) => {
15
14
  if (r instanceof SendRequest) {
16
- return BigInt(await amountInSatoshi(r.value, r.unit));
15
+ return r.value;
17
16
  } else if (r instanceof TokenSendRequest) {
18
- return BigInt(r.value || 1000);
19
- } else return BigInt(0);
17
+ return r.value || 1000n;
18
+ } else return 0n;
20
19
  })
21
20
  );
22
21
  const balance = balanceArray.reduce(sumBalance, BigInt(0));
@@ -1,22 +1,22 @@
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: bigint[] = utxos.map((o: Utxo) => {
6
6
  return o.satoshis;
7
7
  });
8
- const balance = balanceArray.reduce((a: number, b: number) => a + b, 0);
8
+ const balance = balanceArray.reduce((a: bigint, b: bigint) => a + b, 0n);
9
9
  return balance;
10
10
  } else {
11
- return 0;
11
+ return 0n;
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);