mainnet-js 3.0.0-next.0 → 3.0.0-next.2

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 (127) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-3.0.0-next.0.js → mainnet-3.0.0-next.2.js} +21 -41
  3. package/dist/module/cache/walletCache.d.ts +10 -4
  4. package/dist/module/cache/walletCache.d.ts.map +1 -1
  5. package/dist/module/cache/walletCache.js +12 -13
  6. package/dist/module/cache/walletCache.js.map +1 -1
  7. package/dist/module/chain.d.ts +1 -1
  8. package/dist/module/chain.js +1 -1
  9. package/dist/module/chain.js.map +1 -1
  10. package/dist/module/constant.d.ts +1 -1
  11. package/dist/module/constant.d.ts.map +1 -1
  12. package/dist/module/constant.js +1 -1
  13. package/dist/module/constant.js.map +1 -1
  14. package/dist/module/enum.d.ts +1 -7
  15. package/dist/module/enum.d.ts.map +1 -1
  16. package/dist/module/enum.js +0 -6
  17. package/dist/module/enum.js.map +1 -1
  18. package/dist/module/history/getHistory.d.ts.map +1 -1
  19. package/dist/module/history/getHistory.js +17 -47
  20. package/dist/module/history/getHistory.js.map +1 -1
  21. package/dist/module/history/interface.d.ts +1 -1
  22. package/dist/module/history/interface.d.ts.map +1 -1
  23. package/dist/module/interface.d.ts +6 -5
  24. package/dist/module/interface.d.ts.map +1 -1
  25. package/dist/module/interface.js.map +1 -1
  26. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  27. package/dist/module/network/ElectrumNetworkProvider.js +2 -4
  28. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  29. package/dist/module/network/getRelayFeeCache.js +2 -2
  30. package/dist/module/network/getRelayFeeCache.js.map +1 -1
  31. package/dist/module/network/interface.d.ts +2 -2
  32. package/dist/module/network/interface.d.ts.map +1 -1
  33. package/dist/module/rate/ExchangeRate.d.ts +2 -1
  34. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  35. package/dist/module/rate/ExchangeRate.js +4 -1
  36. package/dist/module/rate/ExchangeRate.js.map +1 -1
  37. package/dist/module/transaction/Wif.d.ts +9 -9
  38. package/dist/module/transaction/Wif.d.ts.map +1 -1
  39. package/dist/module/transaction/Wif.js +35 -35
  40. package/dist/module/transaction/Wif.js.map +1 -1
  41. package/dist/module/transaction/allocateFee.d.ts +3 -3
  42. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  43. package/dist/module/transaction/allocateFee.js +5 -6
  44. package/dist/module/transaction/allocateFee.js.map +1 -1
  45. package/dist/module/util/amountInSatoshi.d.ts +1 -1
  46. package/dist/module/util/amountInSatoshi.d.ts.map +1 -1
  47. package/dist/module/util/amountInSatoshi.js +3 -9
  48. package/dist/module/util/amountInSatoshi.js.map +1 -1
  49. package/dist/module/util/asSendRequestObject.d.ts.map +1 -1
  50. package/dist/module/util/asSendRequestObject.js +10 -7
  51. package/dist/module/util/asSendRequestObject.js.map +1 -1
  52. package/dist/module/util/convert.d.ts +3 -0
  53. package/dist/module/util/convert.d.ts.map +1 -1
  54. package/dist/module/util/convert.js +12 -0
  55. package/dist/module/util/convert.js.map +1 -1
  56. package/dist/module/util/index.d.ts +2 -3
  57. package/dist/module/util/index.d.ts.map +1 -1
  58. package/dist/module/util/index.js +2 -3
  59. package/dist/module/util/index.js.map +1 -1
  60. package/dist/module/util/satoshiToAmount.d.ts +1 -1
  61. package/dist/module/util/satoshiToAmount.d.ts.map +1 -1
  62. package/dist/module/util/satoshiToAmount.js +3 -9
  63. package/dist/module/util/satoshiToAmount.js.map +1 -1
  64. package/dist/module/util/sumSendRequestAmounts.d.ts.map +1 -1
  65. package/dist/module/util/sumSendRequestAmounts.js +3 -4
  66. package/dist/module/util/sumSendRequestAmounts.js.map +1 -1
  67. package/dist/module/util/sumUtxoValue.d.ts +1 -1
  68. package/dist/module/util/sumUtxoValue.js +3 -3
  69. package/dist/module/util/sumUtxoValue.js.map +1 -1
  70. package/dist/module/wallet/Base.d.ts +28 -30
  71. package/dist/module/wallet/Base.d.ts.map +1 -1
  72. package/dist/module/wallet/Base.js +82 -119
  73. package/dist/module/wallet/Base.js.map +1 -1
  74. package/dist/module/wallet/HDWallet.d.ts +1 -1
  75. package/dist/module/wallet/Util.js +1 -1
  76. package/dist/module/wallet/Util.js.map +1 -1
  77. package/dist/module/wallet/Wif.d.ts +1 -1
  78. package/dist/module/wallet/interface.d.ts +2 -3
  79. package/dist/module/wallet/interface.d.ts.map +1 -1
  80. package/dist/module/wallet/model.d.ts +53 -41
  81. package/dist/module/wallet/model.d.ts.map +1 -1
  82. package/dist/module/wallet/model.js +14 -22
  83. package/dist/module/wallet/model.js.map +1 -1
  84. package/dist/tsconfig.tsbuildinfo +1 -1
  85. package/package.json +1 -1
  86. package/src/cache/walletCache.ts +23 -36
  87. package/src/chain.ts +1 -1
  88. package/src/constant.ts +1 -1
  89. package/src/enum.ts +0 -6
  90. package/src/history/getHistory.test.ts +20 -34
  91. package/src/history/getHistory.ts +17 -49
  92. package/src/history/interface.ts +1 -1
  93. package/src/interface.ts +6 -5
  94. package/src/network/Connection.test.ts +3 -3
  95. package/src/network/ElectrumNetworkProvider.ts +2 -4
  96. package/src/network/Rpc.test.ts +2 -3
  97. package/src/network/getRelayFeeCache.ts +2 -2
  98. package/src/network/interface.ts +2 -2
  99. package/src/rate/ExchangeRate.test.ts +2 -44
  100. package/src/rate/ExchangeRate.ts +5 -2
  101. package/src/transaction/Wif.ts +50 -45
  102. package/src/transaction/allocateFee.test.ts +110 -131
  103. package/src/transaction/allocateFee.ts +14 -15
  104. package/src/util/amountInSatoshi.test.ts +1 -9
  105. package/src/util/amountInSatoshi.ts +6 -10
  106. package/src/util/asSendRequestObject.ts +12 -7
  107. package/src/util/convert.ts +18 -0
  108. package/src/util/index.ts +2 -7
  109. package/src/util/satoshiToAmount.test.ts +1 -1
  110. package/src/util/satoshiToAmount.ts +4 -10
  111. package/src/util/sumSendRequestAmounts.ts +3 -4
  112. package/src/util/sumUtxoValue.ts +4 -4
  113. package/src/wallet/Base.ts +95 -164
  114. package/src/wallet/Cashtokens.test.headless.js +228 -173
  115. package/src/wallet/Cashtokens.test.ts +506 -406
  116. package/src/wallet/HDWallet.test.ts +223 -68
  117. package/src/wallet/Util.ts +1 -1
  118. package/src/wallet/Wif.test.ts +108 -133
  119. package/src/wallet/interface.ts +2 -3
  120. package/src/wallet/model.test.ts +2 -5
  121. package/src/wallet/model.ts +64 -71
  122. package/dist/module/util/balanceObjectFromSatoshi.d.ts +0 -8
  123. package/dist/module/util/balanceObjectFromSatoshi.d.ts.map +0 -1
  124. package/dist/module/util/balanceObjectFromSatoshi.js +0 -35
  125. package/dist/module/util/balanceObjectFromSatoshi.js.map +0 -1
  126. package/src/util/balanceObjectFromSatoshi.test.ts +0 -58
  127. package/src/util/balanceObjectFromSatoshi.ts +0 -52
@@ -6,7 +6,7 @@ import {
6
6
  import { FeePaidByEnum } from "../wallet/enum.js";
7
7
  import { DUST_UTXO_THRESHOLD } from "../constant.js";
8
8
 
9
- export function checkFeeForDust(value: number) {
9
+ export function checkFeeForDust(value: bigint) {
10
10
  if (value < DUST_UTXO_THRESHOLD) {
11
11
  throw Error("Fee strategy would result in dust output");
12
12
  }
@@ -14,11 +14,11 @@ export function checkFeeForDust(value: number) {
14
14
 
15
15
  export function checkSatsAvailable(
16
16
  sendRequestArray: Array<SendRequest>,
17
- fee: number
17
+ fee: bigint
18
18
  ) {
19
19
  let amountAvailable = sendRequestArray.reduce(function (sum, r) {
20
20
  return sum + (r.value - DUST_UTXO_THRESHOLD);
21
- }, 0);
21
+ }, 0n);
22
22
  if (amountAvailable < fee) {
23
23
  throw Error("Insufficient funds for transaction given fee");
24
24
  }
@@ -39,22 +39,21 @@ export function checkForNonStandardSendRequest(
39
39
  }
40
40
 
41
41
  export function sortSendRequests(sendRequestArray: Array<SendRequest>) {
42
- return sendRequestArray.sort(
43
- (a: SendRequest, b: SendRequest) => a.value - b.value
42
+ return sendRequestArray.sort((a: SendRequest, b: SendRequest) =>
43
+ Number(a.value - b.value)
44
44
  );
45
45
  }
46
46
 
47
- function distributeFees(requests: Array<SendRequest>, fee: number) {
47
+ function distributeFees(requests: Array<SendRequest>, fee: bigint) {
48
48
  checkSatsAvailable(requests, fee);
49
- fee = Number(fee);
50
49
  for (let r = 0; r < requests.length; r++) {
51
50
  if (fee > 0) {
52
51
  checkForNonStandardSendRequest(requests[r]);
53
- let perRequestFee = Math.floor(fee / (requests.length - r));
54
- perRequestFee += fee % (requests.length - r);
52
+ let perRequestFee = fee / BigInt(requests.length - r);
53
+ perRequestFee += fee % BigInt(requests.length - r);
55
54
  if (requests[r].value - perRequestFee < DUST_UTXO_THRESHOLD) {
56
55
  fee -= requests[r].value;
57
- requests[r].value = 0;
56
+ requests[r].value = 0n;
58
57
  } else {
59
58
  fee -= perRequestFee;
60
59
  requests[r].value -= perRequestFee;
@@ -66,7 +65,7 @@ function distributeFees(requests: Array<SendRequest>, fee: number) {
66
65
 
67
66
  function firstPays(
68
67
  requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
69
- fee: number
68
+ fee: bigint
70
69
  ) {
71
70
  let payer = requests.shift()!;
72
71
  payer = checkForNonStandardSendRequest(payer);
@@ -77,7 +76,7 @@ function firstPays(
77
76
  }
78
77
  function lastPays(
79
78
  requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
80
- fee: number
79
+ fee: bigint
81
80
  ) {
82
81
  let payer = requests.pop()!;
83
82
  payer = checkForNonStandardSendRequest(payer);
@@ -88,7 +87,7 @@ function lastPays(
88
87
  }
89
88
  function anyPays(
90
89
  requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
91
- fee: number
90
+ fee: bigint
92
91
  ) {
93
92
  for (let r of requests) {
94
93
  checkForNonStandardSendRequest(r);
@@ -100,7 +99,7 @@ function anyPays(
100
99
 
101
100
  function changeThenFallback(
102
101
  requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
103
- fee: number,
102
+ fee: bigint,
104
103
  change: bigint,
105
104
  fallbackFn: Function
106
105
  ) {
@@ -113,7 +112,7 @@ function changeThenFallback(
113
112
 
114
113
  export function allocateFee(
115
114
  requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
116
- fee: number,
115
+ fee: bigint,
117
116
  feePaidBy: FeePaidByEnum,
118
117
  change: bigint
119
118
  ): Array<SendRequest> {
@@ -13,15 +13,7 @@ test("Get price of Bch, BCH, bch in sat", async () => {
13
13
 
14
14
  test("Get price of sat(s)", async () => {
15
15
  let rate = await amountInSatoshi(1, "sat");
16
- expect(rate).toBe(1);
17
- rate = await amountInSatoshi(1, "sats");
18
- expect(rate).toBe(1);
19
- rate = await amountInSatoshi(1, "Satoshi");
20
- expect(rate).toBe(1);
21
- rate = await amountInSatoshi(1, "SATOSHIS");
22
- expect(rate).toBe(1);
23
- rate = await amountInSatoshi(1, "satoshis");
24
- expect(rate).toBe(1);
16
+ expect(rate).toBe(1n);
25
17
  });
26
18
 
27
19
  test("Get price of USD, Usd, usd", async () => {
@@ -15,23 +15,19 @@ import { sanitizeUnit } from "../util/sanitizeUnit.js";
15
15
  export async function amountInSatoshi(
16
16
  value: number,
17
17
  rawUnit: any
18
- ): Promise<number> {
18
+ ): Promise<bigint> {
19
19
  const unit = sanitizeUnit(rawUnit);
20
20
  switch (unit) {
21
21
  case UnitEnum.BCH:
22
- return Math.round(value * bchParam.subUnits);
23
- case UnitEnum.SATOSHI:
24
- return value;
22
+ return BigInt(Math.round(value * Number(bchParam.subUnits)));
25
23
  case UnitEnum.SAT:
26
- return value;
27
- case UnitEnum.SATS:
28
- return value;
29
- case UnitEnum.SATOSHIS:
30
- return value;
24
+ return BigInt(value);
31
25
  default:
32
26
  const Currency_over_BCH = await ExchangeRate.get(rawUnit);
33
27
  const SAT_over_BCH = bchParam.subUnits;
34
28
 
35
- return Math.round(Number(value * (SAT_over_BCH / Currency_over_BCH)));
29
+ return BigInt(
30
+ Math.round(Number(value * (Number(SAT_over_BCH) / Currency_over_BCH)))
31
+ );
36
32
  }
37
33
  }
@@ -1,5 +1,4 @@
1
1
  import { base64ToBin } from "@bitauth/libauth";
2
- import { UnitEnum } from "../enum.js";
3
2
  import {
4
3
  OpReturnData,
5
4
  SendRequest,
@@ -31,12 +30,14 @@ export function asSendRequestObject(
31
30
  )
32
31
  );
33
32
  } else {
34
- // ['cashaddr', 120, 'sats'],
33
+ // ['cashaddr', 120] or ['cashaddr', 120n],
35
34
  resp.push(
36
35
  new SendRequest({
37
36
  cashaddr: r[0] as string,
38
- value: r[1] as number,
39
- unit: r[2] as UnitEnum,
37
+ value:
38
+ typeof r[1] === "number"
39
+ ? BigInt(Math.floor[1])
40
+ : (r[1] as bigint),
40
41
  })
41
42
  );
42
43
  }
@@ -56,9 +57,7 @@ export function asSendRequestObject(
56
57
  }
57
58
 
58
59
  function convertToClass(object: SendRequest | TokenSendRequest | OpReturnData) {
59
- if (object.hasOwnProperty("unit")) {
60
- return new SendRequest(object as SendRequest);
61
- } else if (object.hasOwnProperty("tokenId")) {
60
+ if (object.hasOwnProperty("category")) {
62
61
  return new TokenSendRequest(object as TokenSendRequest);
63
62
  } else if (object.hasOwnProperty("buffer")) {
64
63
  return OpReturnData.fromUint8Array((object as OpReturnData).buffer);
@@ -70,6 +69,12 @@ function convertToClass(object: SendRequest | TokenSendRequest | OpReturnData) {
70
69
  return OpReturnData.fromUint8Array(
71
70
  Uint8Array.from(base64ToBin((object as any).dataBuffer))
72
71
  );
72
+ } else if (
73
+ object.hasOwnProperty("cashaddr") &&
74
+ object.hasOwnProperty("value") &&
75
+ object.hasOwnProperty("category") === false
76
+ ) {
77
+ return new SendRequest(object as SendRequest);
73
78
  }
74
79
 
75
80
  throw new Error("Unsupported send object");
@@ -1,3 +1,4 @@
1
+ import { bchParam } from "../chain.js";
1
2
  import { amountInSatoshi } from "./amountInSatoshi.js";
2
3
  import { satoshiToAmount } from "./satoshiToAmount.js";
3
4
 
@@ -30,3 +31,20 @@ export async function convertObject({
30
31
  }) {
31
32
  return await convert(value, from, to);
32
33
  }
34
+
35
+ // sats -> bch
36
+ export function toBch(sats: bigint): number {
37
+ return Number(sats) / Number(bchParam.subUnits);
38
+ }
39
+
40
+ // bch -> sats
41
+ export function toSat(bch: string | number): bigint {
42
+ return BigInt(Math.round(Number(bch) * Number(bchParam.subUnits)));
43
+ }
44
+
45
+ export async function toCurrency(
46
+ sats: bigint,
47
+ currency: string
48
+ ): Promise<number> {
49
+ return convert(Number(sats), "sat", currency);
50
+ }
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,17 +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";
38
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));
@@ -2,20 +2,20 @@ import { Utxo } from "../interface.js";
2
2
 
3
3
  export function sumUtxoValue(utxos: Utxo[]) {
4
4
  if (utxos.length > 0) {
5
- const balanceArray: number[] = utxos.map((o: Utxo) => {
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
15
  export function sumTokenAmounts(utxos: Utxo[], tokenId: string): bigint {
16
16
  if (utxos.length > 0) {
17
17
  const tokenArray: bigint[] = utxos
18
- .filter((utxo) => utxo.token?.tokenId === tokenId)
18
+ .filter((utxo) => utxo.token?.category === tokenId)
19
19
  .map((o: Utxo) => {
20
20
  return o.token?.amount || 0n;
21
21
  });