@fuel-ts/account 0.0.0-rc-1356-20240322130951 → 0.0.0-rc-1815-20240322131329

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.

Files changed (39) hide show
  1. package/dist/account.d.ts +7 -0
  2. package/dist/account.d.ts.map +1 -1
  3. package/dist/connectors/fuel-connector.d.ts +10 -0
  4. package/dist/connectors/fuel-connector.d.ts.map +1 -1
  5. package/dist/index.global.js +75 -13
  6. package/dist/index.global.js.map +1 -1
  7. package/dist/index.js +178 -108
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +76 -7
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/providers/provider.d.ts +3 -2
  12. package/dist/providers/provider.d.ts.map +1 -1
  13. package/dist/providers/transaction-request/transaction-request.d.ts +20 -1
  14. package/dist/providers/transaction-request/transaction-request.d.ts.map +1 -1
  15. package/dist/providers/utils/auto-retry-fetch.d.ts.map +1 -1
  16. package/dist/providers/utils/index.d.ts +1 -0
  17. package/dist/providers/utils/index.d.ts.map +1 -1
  18. package/dist/providers/utils/sleep.d.ts +3 -0
  19. package/dist/providers/utils/sleep.d.ts.map +1 -0
  20. package/dist/test-utils/index.d.ts +0 -4
  21. package/dist/test-utils/index.d.ts.map +1 -1
  22. package/dist/test-utils/launchNode.d.ts +1 -8
  23. package/dist/test-utils/launchNode.d.ts.map +1 -1
  24. package/dist/test-utils.global.js +75 -329
  25. package/dist/test-utils.global.js.map +1 -1
  26. package/dist/test-utils.js +169 -333
  27. package/dist/test-utils.js.map +1 -1
  28. package/dist/test-utils.mjs +76 -236
  29. package/dist/test-utils.mjs.map +1 -1
  30. package/dist/wallet/base-wallet-unlocked.d.ts.map +1 -1
  31. package/package.json +16 -17
  32. package/dist/test-utils/asset-id.d.ts +0 -9
  33. package/dist/test-utils/asset-id.d.ts.map +0 -1
  34. package/dist/test-utils/setup-test-provider-and-wallets.d.ts +0 -33
  35. package/dist/test-utils/setup-test-provider-and-wallets.d.ts.map +0 -1
  36. package/dist/test-utils/test-message.d.ts +0 -28
  37. package/dist/test-utils/test-message.d.ts.map +0 -1
  38. package/dist/test-utils/wallet-config.d.ts +0 -49
  39. package/dist/test-utils/wallet-config.d.ts.map +0 -1
@@ -1486,6 +1486,15 @@ function normalizeJSON(root) {
1486
1486
  return normalize(clone(root));
1487
1487
  }
1488
1488
 
1489
+ // src/providers/utils/sleep.ts
1490
+ function sleep(time) {
1491
+ return new Promise((resolve) => {
1492
+ setTimeout(() => {
1493
+ resolve(true);
1494
+ }, time);
1495
+ });
1496
+ }
1497
+
1489
1498
  // src/providers/transaction-request/errors.ts
1490
1499
  var NoWitnessAtIndexError = class extends Error {
1491
1500
  constructor(index) {
@@ -1615,13 +1624,27 @@ var BaseTransactionRequest = class {
1615
1624
  this.outputs.push(output);
1616
1625
  return this.outputs.length - 1;
1617
1626
  }
1627
+ /**
1628
+ * @hidden
1629
+ *
1630
+ * Pushes a witness to the list and returns the index
1631
+ *
1632
+ * @param signature - The signature to add to the witness.
1633
+ * @returns The index of the created witness.
1634
+ */
1635
+ addWitness(signature) {
1636
+ this.witnesses.push(signature);
1637
+ return this.witnesses.length - 1;
1638
+ }
1618
1639
  /**
1619
1640
  * @hidden
1620
1641
  *
1621
1642
  * Creates an empty witness without any side effects and returns the index
1643
+ *
1644
+ * @returns The index of the created witness.
1622
1645
  */
1623
- createWitness() {
1624
- this.witnesses.push(concat([ZeroBytes324, ZeroBytes324]));
1646
+ addEmptyWitness() {
1647
+ this.addWitness(concat([ZeroBytes324, ZeroBytes324]));
1625
1648
  return this.witnesses.length - 1;
1626
1649
  }
1627
1650
  /**
@@ -1650,6 +1673,21 @@ var BaseTransactionRequest = class {
1650
1673
  }
1651
1674
  this.witnesses[index] = witness;
1652
1675
  }
1676
+ /**
1677
+ * Helper function to add an external signature to the transaction.
1678
+ *
1679
+ * @param account - The account/s to sign to the transaction.
1680
+ * @returns The transaction with the signature witness added.
1681
+ */
1682
+ async addAccountWitnesses(account) {
1683
+ const accounts = Array.isArray(account) ? account : [account];
1684
+ await Promise.all(
1685
+ accounts.map(async (acc) => {
1686
+ this.addWitness(await acc.signTransaction(this));
1687
+ })
1688
+ );
1689
+ return this;
1690
+ }
1653
1691
  /**
1654
1692
  * Gets the coin inputs for a transaction.
1655
1693
  *
@@ -1715,7 +1753,7 @@ var BaseTransactionRequest = class {
1715
1753
  } else {
1716
1754
  witnessIndex = this.getCoinInputWitnessIndexByOwner(owner);
1717
1755
  if (typeof witnessIndex !== "number") {
1718
- witnessIndex = this.createWitness();
1756
+ witnessIndex = this.addEmptyWitness();
1719
1757
  }
1720
1758
  }
1721
1759
  const input = {
@@ -1749,7 +1787,7 @@ var BaseTransactionRequest = class {
1749
1787
  } else {
1750
1788
  witnessIndex = this.getCoinInputWitnessIndexByOwner(recipient);
1751
1789
  if (typeof witnessIndex !== "number") {
1752
- witnessIndex = this.createWitness();
1790
+ witnessIndex = this.addEmptyWitness();
1753
1791
  }
1754
1792
  }
1755
1793
  const input = {
@@ -3271,7 +3309,6 @@ import { BigNumberCoder } from "@fuel-ts/abi-coder";
3271
3309
  import { ReceiptType as ReceiptType5 } from "@fuel-ts/transactions";
3272
3310
 
3273
3311
  // src/providers/utils/auto-retry-fetch.ts
3274
- import { sleep } from "@fuel-ts/utils";
3275
3312
  function getWaitDelay(options, retryAttemptNum) {
3276
3313
  const duration = options.baseDelay ?? 150;
3277
3314
  switch (options.backoff) {
@@ -3777,7 +3814,8 @@ var _Provider = class {
3777
3814
  async getTransactionCost(transactionRequestLike, forwardingQuantities = [], {
3778
3815
  estimateTxDependencies = true,
3779
3816
  estimatePredicates = true,
3780
- resourcesOwner
3817
+ resourcesOwner,
3818
+ signatureCallback
3781
3819
  } = {}) {
3782
3820
  const txRequestClone = clone3(transactionRequestify(transactionRequestLike));
3783
3821
  const chainInfo = this.getChain();
@@ -3796,6 +3834,9 @@ var _Provider = class {
3796
3834
  }
3797
3835
  await this.estimatePredicates(txRequestClone);
3798
3836
  }
3837
+ if (signatureCallback && isScriptTransaction) {
3838
+ await signatureCallback(txRequestClone);
3839
+ }
3799
3840
  const minGas = txRequestClone.calculateMinGas(chainInfo);
3800
3841
  const maxGas = txRequestClone.calculateMaxGas(chainInfo, minGas);
3801
3842
  let receipts = [];
@@ -4677,6 +4718,21 @@ var Account = class extends AbstractAccount {
4677
4718
  }
4678
4719
  return this._connector.signMessage(this.address.toString(), message);
4679
4720
  }
4721
+ /**
4722
+ * Signs a transaction with the wallet's private key.
4723
+ *
4724
+ * @param transactionRequestLike - The transaction request to sign.
4725
+ * @returns A promise that resolves to the signature of the transaction.
4726
+ */
4727
+ async signTransaction(transactionRequestLike) {
4728
+ if (!this._connector) {
4729
+ throw new FuelError14(
4730
+ ErrorCode14.MISSING_CONNECTOR,
4731
+ "A connector is required to sign transactions."
4732
+ );
4733
+ }
4734
+ return this._connector.signTransaction(this.address.toString(), transactionRequestLike);
4735
+ }
4680
4736
  /**
4681
4737
  * Sends a transaction to the network.
4682
4738
  *
@@ -4991,7 +5047,7 @@ var BaseWalletUnlocked = class extends Account {
4991
5047
  */
4992
5048
  async signTransaction(transactionRequestLike) {
4993
5049
  const transactionRequest = transactionRequestify(transactionRequestLike);
4994
- const chainId = this.provider.getChain().consensusParameters.chainId.toNumber();
5050
+ const chainId = this.provider.getChainId();
4995
5051
  const hashedTransaction = transactionRequest.getTransactionId(chainId);
4996
5052
  const signature = await this.signer().sign(hashedTransaction);
4997
5053
  return hexlify15(signature);
@@ -7862,8 +7918,7 @@ var launchNode = async ({
7862
7918
  useSystemFuelCore = false,
7863
7919
  loggingEnabled = true,
7864
7920
  debugEnabled = false,
7865
- basePath,
7866
- chainConfig = defaultChainConfig
7921
+ basePath
7867
7922
  }) => (
7868
7923
  // eslint-disable-next-line no-async-promise-executor
7869
7924
  new Promise(async (resolve, reject) => {
@@ -7900,17 +7955,17 @@ var launchNode = async ({
7900
7955
  mkdirSync(tempDirPath, { recursive: true });
7901
7956
  }
7902
7957
  const tempChainConfigFilePath = path.join(tempDirPath, "chainConfig.json");
7903
- let generatedChainConfig = chainConfig;
7958
+ let chainConfig = defaultChainConfig;
7904
7959
  if (!process.env.GENESIS_SECRET) {
7905
7960
  const pk = Signer.generatePrivateKey();
7906
7961
  const signer = new Signer(pk);
7907
7962
  process.env.GENESIS_SECRET = hexlify18(pk);
7908
- generatedChainConfig = {
7909
- ...generatedChainConfig,
7963
+ chainConfig = {
7964
+ ...defaultChainConfig,
7910
7965
  initial_state: {
7911
- ...generatedChainConfig.initial_state,
7966
+ ...defaultChainConfig.initial_state,
7912
7967
  coins: [
7913
- ...generatedChainConfig.initial_state.coins,
7968
+ ...defaultChainConfig.initial_state.coins,
7914
7969
  {
7915
7970
  owner: signer.address.toHexString(),
7916
7971
  amount: toHex2(1e9),
@@ -7920,7 +7975,7 @@ var launchNode = async ({
7920
7975
  }
7921
7976
  };
7922
7977
  }
7923
- writeFileSync(tempChainConfigFilePath, JSON.stringify(generatedChainConfig), "utf8");
7978
+ writeFileSync(tempChainConfigFilePath, JSON.stringify(chainConfig), "utf8");
7924
7979
  chainConfigPathToUse = tempChainConfigFilePath;
7925
7980
  }
7926
7981
  const child = spawn(
@@ -7958,21 +8013,16 @@ var launchNode = async ({
7958
8013
  }
7959
8014
  };
7960
8015
  child.stderr.on("data", (chunk) => {
7961
- const text = typeof chunk === "string" ? chunk : chunk.toString();
7962
- if (text.indexOf(graphQLStartSubstring) !== -1) {
7963
- const rows = text.split("\n");
7964
- const rowWithUrl = rows.find((row) => row.indexOf(graphQLStartSubstring) !== -1);
7965
- const [realIp, realPort] = rowWithUrl.split(" ").at(-1).trim().split(":");
8016
+ if (chunk.indexOf(graphQLStartSubstring) !== -1) {
7966
8017
  resolve({
7967
8018
  cleanup: () => killNode(cleanupConfig),
7968
- ip: realIp,
7969
- port: realPort,
7970
- url: `http://${realIp}:${realPort}/graphql`,
8019
+ ip: ipToUse,
8020
+ port: portToUse,
7971
8021
  chainConfigPath: chainConfigPathToUse
7972
8022
  });
7973
8023
  }
7974
- if (/error/i.test(text)) {
7975
- reject(text.toString());
8024
+ if (/error/i.test(chunk)) {
8025
+ reject(chunk.toString());
7976
8026
  }
7977
8027
  });
7978
8028
  process.on("exit", () => killNode(cleanupConfig));
@@ -8004,221 +8054,11 @@ var launchNodeAndGetWallets = async ({
8004
8054
  };
8005
8055
  return { wallets, stop: cleanup, provider };
8006
8056
  };
8007
-
8008
- // src/test-utils/setup-test-provider-and-wallets.ts
8009
- import { defaultChainConfig as defaultChainConfig2 } from "@fuel-ts/utils";
8010
- import { mergeDeepRight } from "ramda";
8011
-
8012
- // src/test-utils/asset-id.ts
8013
- import { BaseAssetId as BaseAssetId5 } from "@fuel-ts/address/configs";
8014
- import { randomBytes as randomBytes5 } from "@fuel-ts/crypto";
8015
- import { hexlify as hexlify19 } from "@fuel-ts/utils";
8016
- var _AssetId = class {
8017
- constructor(value) {
8018
- this.value = value;
8019
- }
8020
- static random() {
8021
- return new _AssetId(hexlify19(randomBytes5(32)));
8022
- }
8023
- };
8024
- var AssetId = _AssetId;
8025
- __publicField(AssetId, "BaseAssetId", new _AssetId(BaseAssetId5));
8026
- __publicField(AssetId, "A", new _AssetId(
8027
- "0x0101010101010101010101010101010101010101010101010101010101010101"
8028
- ));
8029
- __publicField(AssetId, "B", new _AssetId(
8030
- "0x0202020202020202020202020202020202020202020202020202020202020202"
8031
- ));
8032
-
8033
- // src/test-utils/wallet-config.ts
8034
- import { randomBytes as randomBytes6 } from "@fuel-ts/crypto";
8035
- import { FuelError as FuelError19 } from "@fuel-ts/errors";
8036
- import { toHex as toHex3 } from "@fuel-ts/math";
8037
- var WalletConfig = class {
8038
- initialState;
8039
- options;
8040
- wallets;
8041
- generateWallets = () => {
8042
- const generatedWallets = [];
8043
- for (let index = 1; index <= this.options.count; index++) {
8044
- generatedWallets.push(new WalletUnlocked(randomBytes6(32)));
8045
- }
8046
- return generatedWallets;
8047
- };
8048
- constructor(config) {
8049
- WalletConfig.guard(config);
8050
- this.options = config;
8051
- const { assets: assets2, coinsPerAsset, amountPerCoin, messages } = this.options;
8052
- this.wallets = this.generateWallets();
8053
- this.initialState = {
8054
- messages: WalletConfig.createMessages(this.wallets, messages),
8055
- coins: WalletConfig.createCoins(this.wallets, assets2, coinsPerAsset, amountPerCoin)
8056
- };
8057
- }
8058
- apply(chainConfig) {
8059
- return {
8060
- ...chainConfig,
8061
- initial_state: {
8062
- ...chainConfig?.initial_state,
8063
- coins: this.initialState.coins.concat(chainConfig?.initial_state?.coins || []),
8064
- messages: this.initialState.messages.concat(chainConfig?.initial_state?.messages ?? [])
8065
- }
8066
- };
8067
- }
8068
- static createMessages(wallets, messages) {
8069
- return messages.map((msg) => wallets.map((wallet) => msg.toChainMessage(wallet.address))).flatMap((x) => x);
8070
- }
8071
- static createCoins(wallets, assets2, coinsPerAsset, amountPerCoin) {
8072
- const coins = [];
8073
- let assetIds = [AssetId.BaseAssetId.value];
8074
- if (Array.isArray(assets2)) {
8075
- assetIds = assetIds.concat(assets2.map((a) => a.value));
8076
- } else {
8077
- for (let index = 0; index < assets2 - 1; index++) {
8078
- assetIds.push(AssetId.random().value);
8079
- }
8080
- }
8081
- wallets.map((wallet) => wallet.address.toHexString()).forEach((walletAddress) => {
8082
- assetIds.forEach((assetId) => {
8083
- for (let index = 0; index < coinsPerAsset; index++) {
8084
- coins.push({
8085
- amount: toHex3(amountPerCoin, 8),
8086
- asset_id: assetId,
8087
- owner: walletAddress
8088
- });
8089
- }
8090
- });
8091
- });
8092
- return coins;
8093
- }
8094
- static guard({
8095
- count: wallets,
8096
- assets: assets2,
8097
- coinsPerAsset,
8098
- amountPerCoin
8099
- }) {
8100
- if (Array.isArray(wallets) && wallets.length === 0 || typeof wallets === "number" && wallets <= 0) {
8101
- throw new FuelError19(
8102
- FuelError19.CODES.INVALID_INPUT_PARAMETERS,
8103
- "Number of wallets must be greater than zero."
8104
- );
8105
- }
8106
- if (Array.isArray(assets2) && assets2.length === 0 || typeof assets2 === "number" && assets2 <= 0) {
8107
- throw new FuelError19(
8108
- FuelError19.CODES.INVALID_INPUT_PARAMETERS,
8109
- "Number of assets per wallet must be greater than zero."
8110
- );
8111
- }
8112
- if (coinsPerAsset <= 0) {
8113
- throw new FuelError19(
8114
- FuelError19.CODES.INVALID_INPUT_PARAMETERS,
8115
- "Number of coins per asset must be greater than zero."
8116
- );
8117
- }
8118
- if (amountPerCoin <= 0) {
8119
- throw new FuelError19(
8120
- FuelError19.CODES.INVALID_INPUT_PARAMETERS,
8121
- "Amount per coin must be greater than zero."
8122
- );
8123
- }
8124
- }
8125
- };
8126
-
8127
- // src/test-utils/setup-test-provider-and-wallets.ts
8128
- var defaultWalletConfigOptions = {
8129
- count: 2,
8130
- assets: [AssetId.A, AssetId.B],
8131
- coinsPerAsset: 1,
8132
- amountPerCoin: 1e10,
8133
- messages: []
8134
- };
8135
- async function setupTestProviderAndWallets({
8136
- walletConfig: walletConfigOptions = {},
8137
- providerOptions,
8138
- nodeOptions = {}
8139
- } = {}) {
8140
- Symbol.dispose ??= Symbol("Symbol.dispose");
8141
- const walletConfig = new WalletConfig({
8142
- ...defaultWalletConfigOptions,
8143
- ...walletConfigOptions
8144
- });
8145
- const { cleanup, url } = await launchNode({
8146
- ...nodeOptions,
8147
- chainConfig: mergeDeepRight(defaultChainConfig2, walletConfig.apply(nodeOptions?.chainConfig)),
8148
- port: "0"
8149
- });
8150
- let provider;
8151
- try {
8152
- provider = await Provider.create(url, providerOptions);
8153
- } catch (err) {
8154
- cleanup();
8155
- throw err;
8156
- }
8157
- const wallets = walletConfig.wallets;
8158
- wallets.forEach((wallet) => {
8159
- wallet.connect(provider);
8160
- });
8161
- return {
8162
- provider,
8163
- wallets,
8164
- cleanup,
8165
- [Symbol.dispose]: cleanup
8166
- };
8167
- }
8168
-
8169
- // src/test-utils/test-message.ts
8170
- import { Address as Address6 } from "@fuel-ts/address";
8171
- import { randomBytes as randomBytes7 } from "@fuel-ts/crypto";
8172
- import { BN as BN3 } from "@fuel-ts/math";
8173
- import { hexlify as hexlify20 } from "@fuel-ts/utils";
8174
- var TestMessage = class {
8175
- sender;
8176
- recipient;
8177
- nonce;
8178
- amount;
8179
- data;
8180
- da_height;
8181
- /**
8182
- * A helper class to create messages for testing purposes.
8183
- *
8184
- * Used in tandem with `WalletConfig`.
8185
- * It can also be used standalone and passed into the initial state of a chain via the `.toChainMessage` method.
8186
- */
8187
- constructor({
8188
- sender = Address6.fromRandom(),
8189
- recipient = Address6.fromRandom(),
8190
- nonce = hexlify20(randomBytes7(32)),
8191
- amount = 1e6,
8192
- data = "02",
8193
- da_height = "0x00"
8194
- } = {}) {
8195
- this.sender = sender;
8196
- this.recipient = recipient;
8197
- this.nonce = nonce;
8198
- this.amount = amount;
8199
- this.data = data;
8200
- this.da_height = da_height;
8201
- }
8202
- toChainMessage(recipient) {
8203
- return {
8204
- sender: this.sender.toB256(),
8205
- recipient: recipient?.toB256() ?? this.recipient.toB256(),
8206
- nonce: this.nonce,
8207
- amount: new BN3(this.amount).toHex(8),
8208
- data: this.data,
8209
- da_height: this.da_height
8210
- };
8211
- }
8212
- };
8213
8057
  export {
8214
- AssetId,
8215
- TestMessage,
8216
- WalletConfig,
8217
8058
  generateTestWallet,
8218
8059
  killNode,
8219
8060
  launchNode,
8220
8061
  launchNodeAndGetWallets,
8221
- seedTestWallet,
8222
- setupTestProviderAndWallets
8062
+ seedTestWallet
8223
8063
  };
8224
8064
  //# sourceMappingURL=test-utils.mjs.map