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

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