@fuel-ts/account 0.0.0-rc-2037-20240411020051 → 0.0.0-rc-2034-20240411020813

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.

Potentially problematic release.


This version of @fuel-ts/account might be problematic. Click here for more details.

package/dist/index.mjs CHANGED
@@ -1023,6 +1023,7 @@ var MemoryCache = class {
1023
1023
  };
1024
1024
 
1025
1025
  // src/providers/transaction-request/input.ts
1026
+ import { BYTES_32, UTXO_ID_LEN } from "@fuel-ts/abi-coder";
1026
1027
  import { ZeroBytes32 } from "@fuel-ts/address/configs";
1027
1028
  import { ErrorCode as ErrorCode3, FuelError as FuelError3 } from "@fuel-ts/errors";
1028
1029
  import { bn as bn2, toNumber } from "@fuel-ts/math";
@@ -1036,8 +1037,8 @@ var inputify = (value) => {
1036
1037
  const predicateData = arrayify(value.predicateData ?? "0x");
1037
1038
  return {
1038
1039
  type: InputType.Coin,
1039
- txID: hexlify3(arrayify(value.id).slice(0, 32)),
1040
- outputIndex: arrayify(value.id)[32],
1040
+ txID: hexlify3(arrayify(value.id).slice(0, BYTES_32)),
1041
+ outputIndex: toNumber(arrayify(value.id).slice(BYTES_32, UTXO_ID_LEN)),
1041
1042
  owner: hexlify3(value.owner),
1042
1043
  amount: bn2(value.amount),
1043
1044
  assetId: hexlify3(value.assetId),
@@ -1155,8 +1156,10 @@ var outputify = (value) => {
1155
1156
  };
1156
1157
 
1157
1158
  // src/providers/transaction-request/transaction-request.ts
1159
+ import { UTXO_ID_LEN as UTXO_ID_LEN2 } from "@fuel-ts/abi-coder";
1158
1160
  import { Address, addressify } from "@fuel-ts/address";
1159
1161
  import { BaseAssetId as BaseAssetId2, ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1162
+ import { randomBytes } from "@fuel-ts/crypto";
1160
1163
  import { bn as bn7 } from "@fuel-ts/math";
1161
1164
  import {
1162
1165
  PolicyType,
@@ -1904,8 +1907,8 @@ var BaseTransactionRequest = class {
1904
1907
  * @param predicate - Predicate bytes.
1905
1908
  * @param predicateData - Predicate data bytes.
1906
1909
  */
1907
- addCoinInput(coin) {
1908
- const { assetId, owner, amount, id, predicate } = coin;
1910
+ addCoinInput(coin, predicate) {
1911
+ const { assetId, owner, amount } = coin;
1909
1912
  let witnessIndex;
1910
1913
  if (predicate) {
1911
1914
  witnessIndex = 0;
@@ -1916,14 +1919,14 @@ var BaseTransactionRequest = class {
1916
1919
  }
1917
1920
  }
1918
1921
  const input = {
1919
- id,
1922
+ ...coin,
1920
1923
  type: InputType2.Coin,
1921
1924
  owner: owner.toB256(),
1922
1925
  amount,
1923
1926
  assetId,
1924
1927
  txPointer: "0x00000000000000000000000000000000",
1925
1928
  witnessIndex,
1926
- predicate
1929
+ predicate: predicate?.bytes
1927
1930
  };
1928
1931
  this.pushInput(input);
1929
1932
  this.addChangeOutput(owner, assetId);
@@ -1936,8 +1939,8 @@ var BaseTransactionRequest = class {
1936
1939
  * @param predicate - Predicate bytes.
1937
1940
  * @param predicateData - Predicate data bytes.
1938
1941
  */
1939
- addMessageInput(message) {
1940
- const { recipient, sender, amount, predicate, nonce } = message;
1942
+ addMessageInput(message, predicate) {
1943
+ const { recipient, sender, amount } = message;
1941
1944
  const assetId = BaseAssetId2;
1942
1945
  let witnessIndex;
1943
1946
  if (predicate) {
@@ -1949,13 +1952,13 @@ var BaseTransactionRequest = class {
1949
1952
  }
1950
1953
  }
1951
1954
  const input = {
1952
- nonce,
1955
+ ...message,
1953
1956
  type: InputType2.Message,
1954
1957
  sender: sender.toB256(),
1955
1958
  recipient: recipient.toB256(),
1956
1959
  amount,
1957
1960
  witnessIndex,
1958
- predicate
1961
+ predicate: predicate?.bytes
1959
1962
  };
1960
1963
  this.pushInput(input);
1961
1964
  this.addChangeOutput(recipient, assetId);
@@ -1986,6 +1989,32 @@ var BaseTransactionRequest = class {
1986
1989
  resources.forEach((resource) => this.addResource(resource));
1987
1990
  return this;
1988
1991
  }
1992
+ /**
1993
+ * Adds multiple resources to the transaction by adding coin/message inputs and change
1994
+ * outputs from the related assetIds.
1995
+ *
1996
+ * @param resources - The resources to add.
1997
+ * @returns This transaction.
1998
+ */
1999
+ addPredicateResource(resource, predicate) {
2000
+ if (isCoin(resource)) {
2001
+ this.addCoinInput(resource, predicate);
2002
+ } else {
2003
+ this.addMessageInput(resource, predicate);
2004
+ }
2005
+ return this;
2006
+ }
2007
+ /**
2008
+ * Adds multiple predicate coin/message inputs to the transaction and change outputs
2009
+ * from the related assetIds.
2010
+ *
2011
+ * @param resources - The resources to add.
2012
+ * @returns This transaction.
2013
+ */
2014
+ addPredicateResources(resources, predicate) {
2015
+ resources.forEach((resource) => this.addPredicateResource(resource, predicate));
2016
+ return this;
2017
+ }
1989
2018
  /**
1990
2019
  * Adds a coin output to the transaction.
1991
2020
  *
@@ -2084,12 +2113,6 @@ var BaseTransactionRequest = class {
2084
2113
  * @param quantities - CoinQuantity Array.
2085
2114
  */
2086
2115
  fundWithFakeUtxos(quantities, resourcesOwner) {
2087
- let idCounter = 0;
2088
- const generateId = () => {
2089
- const counterString = String(idCounter++);
2090
- const id = ZeroBytes324.slice(0, -counterString.length).concat(counterString);
2091
- return id;
2092
- };
2093
2116
  const findAssetInput = (assetId) => this.inputs.find((input) => {
2094
2117
  if ("assetId" in input) {
2095
2118
  return input.assetId === assetId;
@@ -2099,12 +2122,12 @@ var BaseTransactionRequest = class {
2099
2122
  const updateAssetInput = (assetId, quantity) => {
2100
2123
  const assetInput = findAssetInput(assetId);
2101
2124
  if (assetInput && "assetId" in assetInput) {
2102
- assetInput.id = generateId();
2125
+ assetInput.id = hexlify7(randomBytes(UTXO_ID_LEN2));
2103
2126
  assetInput.amount = quantity;
2104
2127
  } else {
2105
2128
  this.addResources([
2106
2129
  {
2107
- id: generateId(),
2130
+ id: hexlify7(randomBytes(UTXO_ID_LEN2)),
2108
2131
  amount: quantity,
2109
2132
  assetId,
2110
2133
  owner: resourcesOwner || Address.fromRandom(),
@@ -3981,6 +4004,36 @@ var _Provider = class {
3981
4004
  missingContractIds
3982
4005
  };
3983
4006
  }
4007
+ /**
4008
+ * Estimates the transaction gas and fee based on the provided transaction request.
4009
+ * @param transactionRequest - The transaction request object.
4010
+ * @returns An object containing the estimated minimum gas, minimum fee, maximum gas, and maximum fee.
4011
+ */
4012
+ estimateTxGasAndFee(params) {
4013
+ const { transactionRequest } = params;
4014
+ const { gasPriceFactor, minGasPrice, maxGasPerTx } = this.getGasConfig();
4015
+ const chainInfo = this.getChain();
4016
+ const gasPrice = transactionRequest.gasPrice.eq(0) ? minGasPrice : transactionRequest.gasPrice;
4017
+ transactionRequest.gasPrice = gasPrice;
4018
+ const minGas = transactionRequest.calculateMinGas(chainInfo);
4019
+ const minFee = calculatePriceWithFactor(minGas, gasPrice, gasPriceFactor).normalizeZeroToOne();
4020
+ if (transactionRequest.type === TransactionType8.Script) {
4021
+ if (transactionRequest.gasLimit.eq(0)) {
4022
+ transactionRequest.gasLimit = minGas;
4023
+ transactionRequest.gasLimit = maxGasPerTx.sub(
4024
+ transactionRequest.calculateMaxGas(chainInfo, minGas)
4025
+ );
4026
+ }
4027
+ }
4028
+ const maxGas = transactionRequest.calculateMaxGas(chainInfo, minGas);
4029
+ const maxFee = calculatePriceWithFactor(maxGas, gasPrice, gasPriceFactor).normalizeZeroToOne();
4030
+ return {
4031
+ minGas,
4032
+ minFee,
4033
+ maxGas,
4034
+ maxFee
4035
+ };
4036
+ }
3984
4037
  /**
3985
4038
  * Executes a signed transaction without applying the states changes
3986
4039
  * on the chain.
@@ -4028,17 +4081,16 @@ var _Provider = class {
4028
4081
  signatureCallback
4029
4082
  } = {}) {
4030
4083
  const txRequestClone = clone3(transactionRequestify(transactionRequestLike));
4031
- const chainInfo = this.getChain();
4032
- const { gasPriceFactor, minGasPrice, maxGasPerTx } = this.getGasConfig();
4033
- const gasPrice = max(txRequestClone.gasPrice, minGasPrice);
4084
+ const { minGasPrice } = this.getGasConfig();
4085
+ const setGasPrice = max(txRequestClone.gasPrice, minGasPrice);
4034
4086
  const isScriptTransaction = txRequestClone.type === TransactionType8.Script;
4035
4087
  const coinOutputsQuantities = txRequestClone.getCoinOutputsQuantities();
4036
4088
  const allQuantities = mergeQuantities(coinOutputsQuantities, forwardingQuantities);
4037
4089
  txRequestClone.fundWithFakeUtxos(allQuantities, resourcesOwner?.address);
4090
+ if (isScriptTransaction) {
4091
+ txRequestClone.gasLimit = bn15(0);
4092
+ }
4038
4093
  if (estimatePredicates) {
4039
- if (isScriptTransaction) {
4040
- txRequestClone.gasLimit = bn15(0);
4041
- }
4042
4094
  if (resourcesOwner && "populateTransactionPredicateData" in resourcesOwner) {
4043
4095
  resourcesOwner.populateTransactionPredicateData(txRequestClone);
4044
4096
  }
@@ -4047,36 +4099,34 @@ var _Provider = class {
4047
4099
  if (signatureCallback && isScriptTransaction) {
4048
4100
  await signatureCallback(txRequestClone);
4049
4101
  }
4050
- const minGas = txRequestClone.calculateMinGas(chainInfo);
4051
- const maxGas = txRequestClone.calculateMaxGas(chainInfo, minGas);
4102
+ let { maxFee, maxGas, minFee, minGas } = this.estimateTxGasAndFee({
4103
+ transactionRequest: txRequestClone
4104
+ });
4052
4105
  let receipts = [];
4053
4106
  let missingContractIds = [];
4054
4107
  let outputVariables = 0;
4108
+ let gasUsed = bn15(0);
4055
4109
  if (isScriptTransaction && estimateTxDependencies) {
4056
4110
  txRequestClone.gasPrice = bn15(0);
4057
- txRequestClone.gasLimit = bn15(maxGasPerTx.sub(maxGas).toNumber() * 0.9);
4058
4111
  const result = await this.estimateTxDependencies(txRequestClone);
4059
4112
  receipts = result.receipts;
4060
4113
  outputVariables = result.outputVariables;
4061
4114
  missingContractIds = result.missingContractIds;
4115
+ gasUsed = isScriptTransaction ? getGasUsedFromReceipts(receipts) : gasUsed;
4116
+ txRequestClone.gasLimit = gasUsed;
4117
+ txRequestClone.gasPrice = setGasPrice;
4118
+ ({ maxFee, maxGas, minFee, minGas } = this.estimateTxGasAndFee({
4119
+ transactionRequest: txRequestClone
4120
+ }));
4062
4121
  }
4063
- const gasUsed = isScriptTransaction ? getGasUsedFromReceipts(receipts) : minGas;
4064
- const usedFee = calculatePriceWithFactor(
4065
- gasUsed,
4066
- gasPrice,
4067
- gasPriceFactor
4068
- ).normalizeZeroToOne();
4069
- const minFee = calculatePriceWithFactor(minGas, gasPrice, gasPriceFactor).normalizeZeroToOne();
4070
- const maxFee = calculatePriceWithFactor(maxGas, gasPrice, gasPriceFactor).normalizeZeroToOne();
4071
4122
  return {
4072
4123
  requiredQuantities: allQuantities,
4073
4124
  receipts,
4074
4125
  gasUsed,
4075
4126
  minGasPrice,
4076
- gasPrice,
4127
+ gasPrice: setGasPrice,
4077
4128
  minGas,
4078
4129
  maxGas,
4079
- usedFee,
4080
4130
  minFee,
4081
4131
  maxFee,
4082
4132
  estimatedInputs: txRequestClone.inputs,
@@ -5170,7 +5220,7 @@ import { hexlify as hexlify15 } from "@fuel-ts/utils";
5170
5220
 
5171
5221
  // src/signer/signer.ts
5172
5222
  import { Address as Address4 } from "@fuel-ts/address";
5173
- import { randomBytes } from "@fuel-ts/crypto";
5223
+ import { randomBytes as randomBytes2 } from "@fuel-ts/crypto";
5174
5224
  import { hash } from "@fuel-ts/hasher";
5175
5225
  import { toBytes } from "@fuel-ts/math";
5176
5226
  import { hexlify as hexlify13, concat as concat3, arrayify as arrayify15 } from "@fuel-ts/utils";
@@ -5263,7 +5313,7 @@ var Signer = class {
5263
5313
  * @returns random 32-byte hashed
5264
5314
  */
5265
5315
  static generatePrivateKey(entropy) {
5266
- return entropy ? hash(concat3([randomBytes(32), arrayify15(entropy)])) : randomBytes(32);
5316
+ return entropy ? hash(concat3([randomBytes2(32), arrayify15(entropy)])) : randomBytes2(32);
5267
5317
  }
5268
5318
  /**
5269
5319
  * Extended publicKey from a compact publicKey
@@ -5282,7 +5332,7 @@ import { Address as Address5 } from "@fuel-ts/address";
5282
5332
  import {
5283
5333
  bufferFromString,
5284
5334
  keccak256,
5285
- randomBytes as randomBytes2,
5335
+ randomBytes as randomBytes3,
5286
5336
  scrypt,
5287
5337
  stringFromBuffer,
5288
5338
  decryptJsonWalletData,
@@ -5305,7 +5355,7 @@ var removeHexPrefix = (hexString) => {
5305
5355
  async function encryptKeystoreWallet(privateKey, address, password) {
5306
5356
  const privateKeyBuffer = bufferFromString(removeHexPrefix(privateKey), "hex");
5307
5357
  const ownerAddress = Address5.fromAddressOrString(address);
5308
- const salt = randomBytes2(DEFAULT_KEY_SIZE);
5358
+ const salt = randomBytes3(DEFAULT_KEY_SIZE);
5309
5359
  const key = scrypt({
5310
5360
  password: bufferFromString(password),
5311
5361
  salt,
@@ -5314,7 +5364,7 @@ async function encryptKeystoreWallet(privateKey, address, password) {
5314
5364
  r: DEFAULT_KDF_PARAMS_R,
5315
5365
  p: DEFAULT_KDF_PARAMS_P
5316
5366
  });
5317
- const iv = randomBytes2(DEFAULT_IV_SIZE);
5367
+ const iv = randomBytes3(DEFAULT_IV_SIZE);
5318
5368
  const ciphertext = await encryptJsonWalletData(privateKeyBuffer, key, iv);
5319
5369
  const data = Uint8Array.from([...key.subarray(16, 32), ...ciphertext]);
5320
5370
  const macHashUint8Array = keccak256(data);
@@ -5496,7 +5546,7 @@ import { arrayify as arrayify18, hexlify as hexlify17, concat as concat5 } from
5496
5546
  import { toBeHex, dataSlice as dataSlice2, encodeBase58 as encodeBase582, decodeBase58, computeHmac as computeHmac2, ripemd160 } from "ethers";
5497
5547
 
5498
5548
  // src/mnemonic/mnemonic.ts
5499
- import { randomBytes as randomBytes3 } from "@fuel-ts/crypto";
5549
+ import { randomBytes as randomBytes4 } from "@fuel-ts/crypto";
5500
5550
  import { ErrorCode as ErrorCode18, FuelError as FuelError18 } from "@fuel-ts/errors";
5501
5551
  import { sha256 as sha2563 } from "@fuel-ts/hasher";
5502
5552
  import { arrayify as arrayify17, hexlify as hexlify16, concat as concat4 } from "@fuel-ts/utils";
@@ -7857,7 +7907,7 @@ var Mnemonic = class {
7857
7907
  * @returns A randomly generated mnemonic
7858
7908
  */
7859
7909
  static generate(size = 32, extraEntropy = "") {
7860
- const entropy = extraEntropy ? sha2563(concat4([randomBytes3(size), arrayify17(extraEntropy)])) : randomBytes3(size);
7910
+ const entropy = extraEntropy ? sha2563(concat4([randomBytes4(size), arrayify17(extraEntropy)])) : randomBytes4(size);
7861
7911
  return Mnemonic.entropyToMnemonic(entropy);
7862
7912
  }
7863
7913
  };
@@ -8715,7 +8765,6 @@ var Predicate = class extends Account {
8715
8765
  if (input.type === InputType7.Coin && hexlify19(input.owner) === this.address.toB256()) {
8716
8766
  input.predicate = this.bytes;
8717
8767
  input.predicateData = this.getPredicateData(policies.length);
8718
- input.witnessIndex = 0;
8719
8768
  }
8720
8769
  });
8721
8770
  return request;
@@ -8753,20 +8802,6 @@ var Predicate = class extends Account {
8753
8802
  const transactionRequest = this.populateTransactionPredicateData(transactionRequestLike);
8754
8803
  return super.simulateTransaction(transactionRequest);
8755
8804
  }
8756
- /**
8757
- * Retrieves resources satisfying the spend query for the account.
8758
- *
8759
- * @param quantities - IDs of coins to exclude.
8760
- * @param excludedIds - IDs of resources to be excluded from the query.
8761
- * @returns A promise that resolves to an array of Resources.
8762
- */
8763
- async getResourcesToSpend(quantities, excludedIds) {
8764
- const resources = await super.getResourcesToSpend(quantities, excludedIds);
8765
- return resources.map((resource) => ({
8766
- ...resource,
8767
- predicate: hexlify19(this.bytes)
8768
- }));
8769
- }
8770
8805
  getPredicateData(policiesLength) {
8771
8806
  if (!this.predicateData.length) {
8772
8807
  return new Uint8Array();