mainnet-js 2.2.9 → 2.3.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.
@@ -16,6 +16,7 @@ import {
16
16
  utf8ToBin,
17
17
  } from "@bitauth/libauth";
18
18
  import { mine } from "../mine";
19
+ import json from "../../polyfill/json";
19
20
  import ElectrumNetworkProvider from "../network/ElectrumNetworkProvider";
20
21
 
21
22
  beforeAll(async () => {
@@ -1069,9 +1070,9 @@ describe(`Wallet extrema behavior regression testing`, () => {
1069
1070
  expect(encodedTransaction.length).toBeGreaterThan(0);
1070
1071
 
1071
1072
  // check transaction was not submitted
1072
- expect(JSON.stringify(aliceUtxos)).toBe(
1073
- JSON.stringify(await aliceWallet.getAddressUtxos())
1074
- );
1073
+ // BigInts can't be serialized as strings
1074
+ //
1075
+ expect(json(aliceUtxos)).toBe(json(await aliceWallet.getAddressUtxos()));
1075
1076
 
1076
1077
  const decoded = decodeTransaction(encodedTransaction);
1077
1078
  if (typeof decoded === "string") {
@@ -1112,9 +1113,7 @@ describe(`Wallet extrema behavior regression testing`, () => {
1112
1113
  expect(encodedTransaction.length).toBeGreaterThan(0);
1113
1114
 
1114
1115
  // check transaction was not submitted
1115
- expect(JSON.stringify(aliceUtxos)).toBe(
1116
- JSON.stringify(await aliceWallet.getAddressUtxos())
1117
- );
1116
+ expect(json(aliceUtxos)).toBe(json(await aliceWallet.getAddressUtxos()));
1118
1117
 
1119
1118
  const decoded = decodeTransaction(encodedTransaction);
1120
1119
  if (typeof decoded === "string") {
@@ -1136,9 +1135,7 @@ describe(`Wallet extrema behavior regression testing`, () => {
1136
1135
  expect(encodedTransaction.length).toBeGreaterThan(0);
1137
1136
 
1138
1137
  // check transaction was not submitted
1139
- expect(JSON.stringify(aliceUtxos)).toBe(
1140
- JSON.stringify(await aliceWallet.getAddressUtxos())
1141
- );
1138
+ expect(json(aliceUtxos)).toBe(json(await aliceWallet.getAddressUtxos()));
1142
1139
 
1143
1140
  const decoded = decodeTransaction(encodedTransaction);
1144
1141
  if (typeof decoded === "string") {
package/src/wallet/Wif.ts CHANGED
@@ -774,9 +774,9 @@ export class Wallet extends BaseWallet {
774
774
  // can be cancelled by calling the function returned from this one
775
775
  public watchTokenBalance(
776
776
  tokenId: string,
777
- callback: (balance: number) => void
777
+ callback: (balance: bigint) => void
778
778
  ): CancelWatchFn {
779
- let previous: number | undefined = undefined;
779
+ let previous: bigint | undefined = undefined;
780
780
  return (this.provider! as ElectrumNetworkProvider).watchAddressStatus(
781
781
  this.getDepositAddress(),
782
782
  async (_status: string) => {
@@ -793,12 +793,12 @@ export class Wallet extends BaseWallet {
793
793
  // this call halts the execution
794
794
  public async waitForTokenBalance(
795
795
  tokenId: string,
796
- amount: number
797
- ): Promise<number> {
796
+ amount: bigint
797
+ ): Promise<bigint> {
798
798
  return new Promise(async (resolve) => {
799
799
  const watchCancel = this.watchTokenBalance(
800
800
  tokenId,
801
- async (balance: number) => {
801
+ async (balance: bigint) => {
802
802
  if (balance >= amount) {
803
803
  await watchCancel();
804
804
  resolve(balance);
@@ -1107,7 +1107,7 @@ export class Wallet extends BaseWallet {
1107
1107
  inputs: UtxoI[],
1108
1108
  outputs: SendRequestType[]
1109
1109
  ) => {
1110
- // Do NOT allow for implicit token burn if the total amount sent is less than user had
1110
+ // Allow for implicit token burn if the total amount sent is less than user had
1111
1111
  // allow for token genesis, creating more tokens than we had before (0)
1112
1112
  if (!checkTokenQuantities) {
1113
1113
  return;
@@ -1125,14 +1125,14 @@ export class Wallet extends BaseWallet {
1125
1125
  );
1126
1126
  const inputAmountSum = tokenInputs.reduce(
1127
1127
  (prev, cur) => prev + cur.token!.amount,
1128
- 0
1128
+ 0n
1129
1129
  );
1130
1130
  const tokenOutputs = allTokenOutputs.filter(
1131
1131
  (val) => val.tokenId === tokenId
1132
1132
  );
1133
1133
  const outputAmountSum = tokenOutputs.reduce(
1134
1134
  (prev, cur) => prev + cur.amount,
1135
- 0
1135
+ 0n
1136
1136
  );
1137
1137
 
1138
1138
  const diff = inputAmountSum - outputAmountSum;
@@ -1140,8 +1140,8 @@ export class Wallet extends BaseWallet {
1140
1140
  throw new Error("Not enough token amount to send");
1141
1141
  }
1142
1142
  if (diff >= 0) {
1143
- let available = 0;
1144
- let change = 0;
1143
+ let available = 0n;
1144
+ let change = 0n;
1145
1145
  const ensureUtxos: UtxoI[] = [];
1146
1146
  for (const token of tokenInputs.filter((val) => val.token?.amount)) {
1147
1147
  ensureUtxos.push(token);
@@ -1552,9 +1552,9 @@ export class Wallet extends BaseWallet {
1552
1552
  }
1553
1553
  const newAmount =
1554
1554
  deductTokenAmount && nftUtxos[0].token!.amount > 0
1555
- ? nftUtxos[0].token!.amount - mintRequests.length
1555
+ ? nftUtxos[0].token!.amount - BigInt(mintRequests.length)
1556
1556
  : nftUtxos[0].token!.amount;
1557
- const safeNewAmount = Math.max(0, newAmount);
1557
+ const safeNewAmount = newAmount < 0n ? 0n : newAmount;
1558
1558
  const mintingInput = new TokenSendRequest({
1559
1559
  cashaddr: this.tokenaddr!,
1560
1560
  tokenId: tokenId,
@@ -1626,23 +1626,24 @@ export class Wallet extends BaseWallet {
1626
1626
  }
1627
1627
 
1628
1628
  const totalFungibleAmount = tokenUtxos.reduce(
1629
- (prev, cur) => prev + (cur.token?.amount || 0),
1630
- 0
1629
+ (prev, cur) => prev + (cur.token?.amount || 0n),
1630
+ 0n
1631
1631
  );
1632
- const fungibleBurnAmount =
1633
- burnRequest.amount && burnRequest.amount > 0 ? burnRequest.amount! : 0;
1632
+ let fungibleBurnAmount =
1633
+ burnRequest.amount && burnRequest.amount > 0 ? burnRequest.amount! : 0n;
1634
+ fungibleBurnAmount = BigInt(fungibleBurnAmount);
1634
1635
  const hasNFT = burnRequest.capability || burnRequest.commitment;
1635
1636
 
1636
1637
  let utxoIds: UtxoI[] = [];
1637
1638
  let changeSendRequests: TokenSendRequest[];
1638
1639
  if (hasNFT) {
1639
1640
  // does not have FT tokens, let us destroy the token completely
1640
- if (totalFungibleAmount === 0) {
1641
+ if (totalFungibleAmount === 0n) {
1641
1642
  changeSendRequests = [];
1642
1643
  utxoIds.push(tokenUtxos[0]);
1643
1644
  } else {
1644
1645
  // add utxos to spend from
1645
- let available = 0;
1646
+ let available = 0n;
1646
1647
  for (const token of tokenUtxos.filter((val) => val.token?.amount)) {
1647
1648
  utxoIds.push(token);
1648
1649
  available += token.token?.amount!;
@@ -1653,7 +1654,7 @@ export class Wallet extends BaseWallet {
1653
1654
 
1654
1655
  // if there are FT, reduce their amount
1655
1656
  const newAmount = totalFungibleAmount - fungibleBurnAmount;
1656
- const safeNewAmount = Math.max(0, newAmount);
1657
+ const safeNewAmount = newAmount < 0n ? 0n : newAmount;
1657
1658
  changeSendRequests = [
1658
1659
  new TokenSendRequest({
1659
1660
  cashaddr: burnRequest.cashaddr || this.tokenaddr!,
@@ -1666,13 +1667,13 @@ export class Wallet extends BaseWallet {
1666
1667
  ];
1667
1668
  }
1668
1669
  } else {
1669
- // if we are burning last fughible tokens, let us destroy the token completely
1670
+ // if we are burning last fungible tokens, let us destroy the token completely
1670
1671
  if (totalFungibleAmount === fungibleBurnAmount) {
1671
1672
  changeSendRequests = [];
1672
1673
  utxoIds.push(tokenUtxos[0]);
1673
1674
  } else {
1674
1675
  // add utxos to spend from
1675
- let available = 0;
1676
+ let available = 0n;
1676
1677
  for (const token of tokenUtxos.filter((val) => val.token?.amount)) {
1677
1678
  utxoIds.push(token);
1678
1679
  available += token.token?.amount!;
@@ -1683,7 +1684,7 @@ export class Wallet extends BaseWallet {
1683
1684
 
1684
1685
  // reduce the FT amount
1685
1686
  const newAmount = totalFungibleAmount - fungibleBurnAmount;
1686
- const safeNewAmount = Math.max(0, newAmount);
1687
+ const safeNewAmount = newAmount < 0n ? 0n : newAmount;
1687
1688
  changeSendRequests = [
1688
1689
  new TokenSendRequest({
1689
1690
  cashaddr: burnRequest.cashaddr || this.tokenaddr!,
@@ -1722,9 +1723,9 @@ export class Wallet extends BaseWallet {
1722
1723
  * getTokenBalance Gets fungible token balance
1723
1724
  * for NFT token balance see @ref getNftTokenBalance
1724
1725
  * @param {string} tokenId tokenId to get balance for
1725
- * @returns {number} fungible token balance
1726
+ * @returns {bigint} fungible token balance
1726
1727
  */
1727
- public async getTokenBalance(tokenId: string): Promise<number> {
1728
+ public async getTokenBalance(tokenId: string): Promise<bigint> {
1728
1729
  const utxos = (await this.getTokenUtxos(tokenId)).filter(
1729
1730
  (val) => val.token?.amount
1730
1731
  );
@@ -1732,7 +1733,7 @@ export class Wallet extends BaseWallet {
1732
1733
  }
1733
1734
 
1734
1735
  /**
1735
- * getNftTokenBalance Gets non-fungible token (NFT) balance for a particula tokenId
1736
+ * getNftTokenBalance Gets non-fungible token (NFT) balance for a particular tokenId
1736
1737
  * disregards fungible token balances
1737
1738
  * for fungible token balance see @ref getTokenBalance
1738
1739
  * @param {string} tokenId tokenId to get balance for
@@ -1749,14 +1750,14 @@ export class Wallet extends BaseWallet {
1749
1750
  * getAllTokenBalances Gets all fungible token balances in this wallet
1750
1751
  * @returns {Object} a map [tokenId => balance] for all tokens in this wallet
1751
1752
  */
1752
- public async getAllTokenBalances(): Promise<{ [tokenId: string]: number }> {
1753
+ public async getAllTokenBalances(): Promise<{ [tokenId: string]: bigint }> {
1753
1754
  const result = {};
1754
1755
  const utxos = (await this.getTokenUtxos()).filter(
1755
1756
  (val) => val.token?.amount
1756
1757
  );
1757
1758
  for (const utxo of utxos) {
1758
1759
  if (!result[utxo.token!.tokenId]) {
1759
- result[utxo.token!.tokenId] = 0;
1760
+ result[utxo.token!.tokenId] = 0n;
1760
1761
  }
1761
1762
  result[utxo.token!.tokenId] += utxo.token!.amount;
1762
1763
  }
@@ -47,7 +47,7 @@ export class SendRequest {
47
47
  }
48
48
 
49
49
  export class TokenGenesisRequest {
50
- amount?: number; // fungible token amount
50
+ amount?: bigint; // fungible token amount
51
51
  capability?: NFTCapability;
52
52
  commitment?: string;
53
53
  cashaddr?: string;
@@ -60,7 +60,7 @@ export class TokenGenesisRequest {
60
60
  cashaddr,
61
61
  value,
62
62
  }: {
63
- amount?: number;
63
+ amount?: bigint;
64
64
  capability?: NFTCapability;
65
65
  commitment?: string;
66
66
  cashaddr?: string;
@@ -78,7 +78,7 @@ export class TokenBurnRequest {
78
78
  tokenId: string;
79
79
  capability?: NFTCapability;
80
80
  commitment?: string;
81
- amount?: number; // fungible token amount
81
+ amount?: bigint; // fungible token amount
82
82
  cashaddr?: string;
83
83
 
84
84
  constructor({
@@ -91,13 +91,13 @@ export class TokenBurnRequest {
91
91
  tokenId: string;
92
92
  capability?: NFTCapability;
93
93
  commitment?: string;
94
- amount?: number;
94
+ amount?: number | bigint;
95
95
  cashaddr?: string;
96
96
  }) {
97
97
  this.tokenId = tokenId;
98
98
  this.capability = capability;
99
99
  this.commitment = commitment;
100
- this.amount = amount;
100
+ this.amount = amount ? BigInt(amount) : 0n;
101
101
  this.cashaddr = cashaddr;
102
102
  }
103
103
  }
@@ -105,7 +105,7 @@ export class TokenBurnRequest {
105
105
  export class TokenSendRequest {
106
106
  cashaddr: string; // cashaddr or tokenaddr to send tokens to
107
107
  value?: number; // satoshi value
108
- amount: number; // fungible token amount
108
+ amount: bigint; // fungible token amount
109
109
  tokenId: string;
110
110
  capability?: NFTCapability;
111
111
  commitment?: string;
@@ -120,7 +120,7 @@ export class TokenSendRequest {
120
120
  }: {
121
121
  cashaddr: string;
122
122
  value?: number;
123
- amount?: number;
123
+ amount?: number | bigint;
124
124
  tokenId: string;
125
125
  capability?: NFTCapability;
126
126
  commitment?: string;
@@ -129,7 +129,7 @@ export class TokenSendRequest {
129
129
 
130
130
  this.cashaddr = cashaddr;
131
131
  this.value = value;
132
- this.amount = amount || 0;
132
+ this.amount = amount ? BigInt(amount) : 0n;
133
133
  this.tokenId = tokenId;
134
134
  this.capability = capability;
135
135
  this.commitment = commitment;
@@ -371,7 +371,7 @@ export const fromUtxoId = (utxoId: string): UtxoI => {
371
371
  token: tokenId
372
372
  ? {
373
373
  tokenId,
374
- amount: parseInt(amount),
374
+ amount: BigInt(amount),
375
375
  capability: capability || undefined,
376
376
  commitment: commitment || undefined,
377
377
  }