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

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 (37) hide show
  1. package/dist/account.d.ts +0 -7
  2. package/dist/account.d.ts.map +1 -1
  3. package/dist/connectors/fuel-connector.d.ts +0 -10
  4. package/dist/connectors/fuel-connector.d.ts.map +1 -1
  5. package/dist/index.global.js +12 -70
  6. package/dist/index.global.js.map +1 -1
  7. package/dist/index.js +107 -173
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +6 -71
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/providers/transaction-request/transaction-request.d.ts +1 -20
  12. package/dist/providers/transaction-request/transaction-request.d.ts.map +1 -1
  13. package/dist/providers/utils/auto-retry-fetch.d.ts.map +1 -1
  14. package/dist/providers/utils/index.d.ts +0 -1
  15. package/dist/providers/utils/index.d.ts.map +1 -1
  16. package/dist/test-utils/asset-id.d.ts +9 -0
  17. package/dist/test-utils/asset-id.d.ts.map +1 -0
  18. package/dist/test-utils/index.d.ts +4 -0
  19. package/dist/test-utils/index.d.ts.map +1 -1
  20. package/dist/test-utils/launchNode.d.ts +8 -1
  21. package/dist/test-utils/launchNode.d.ts.map +1 -1
  22. package/dist/test-utils/setup-test-provider-and-wallets.d.ts +33 -0
  23. package/dist/test-utils/setup-test-provider-and-wallets.d.ts.map +1 -0
  24. package/dist/test-utils/test-message.d.ts +28 -0
  25. package/dist/test-utils/test-message.d.ts.map +1 -0
  26. package/dist/test-utils/wallet-config.d.ts +49 -0
  27. package/dist/test-utils/wallet-config.d.ts.map +1 -0
  28. package/dist/test-utils.global.js +328 -70
  29. package/dist/test-utils.global.js.map +1 -1
  30. package/dist/test-utils.js +332 -164
  31. package/dist/test-utils.js.map +1 -1
  32. package/dist/test-utils.mjs +235 -71
  33. package/dist/test-utils.mjs.map +1 -1
  34. package/dist/wallet/base-wallet-unlocked.d.ts.map +1 -1
  35. package/package.json +17 -16
  36. package/dist/providers/utils/sleep.d.ts +0 -3
  37. package/dist/providers/utils/sleep.d.ts.map +0 -1
@@ -1486,15 +1486,6 @@ 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
-
1498
1489
  // src/providers/transaction-request/errors.ts
1499
1490
  var NoWitnessAtIndexError = class extends Error {
1500
1491
  constructor(index) {
@@ -1624,27 +1615,13 @@ var BaseTransactionRequest = class {
1624
1615
  this.outputs.push(output);
1625
1616
  return this.outputs.length - 1;
1626
1617
  }
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
- }
1639
1618
  /**
1640
1619
  * @hidden
1641
1620
  *
1642
1621
  * Creates an empty witness without any side effects and returns the index
1643
- *
1644
- * @returns The index of the created witness.
1645
1622
  */
1646
- addEmptyWitness() {
1647
- this.addWitness(concat([ZeroBytes324, ZeroBytes324]));
1623
+ createWitness() {
1624
+ this.witnesses.push(concat([ZeroBytes324, ZeroBytes324]));
1648
1625
  return this.witnesses.length - 1;
1649
1626
  }
1650
1627
  /**
@@ -1673,21 +1650,6 @@ var BaseTransactionRequest = class {
1673
1650
  }
1674
1651
  this.witnesses[index] = witness;
1675
1652
  }
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
- }
1691
1653
  /**
1692
1654
  * Gets the coin inputs for a transaction.
1693
1655
  *
@@ -1753,7 +1715,7 @@ var BaseTransactionRequest = class {
1753
1715
  } else {
1754
1716
  witnessIndex = this.getCoinInputWitnessIndexByOwner(owner);
1755
1717
  if (typeof witnessIndex !== "number") {
1756
- witnessIndex = this.addEmptyWitness();
1718
+ witnessIndex = this.createWitness();
1757
1719
  }
1758
1720
  }
1759
1721
  const input = {
@@ -1787,7 +1749,7 @@ var BaseTransactionRequest = class {
1787
1749
  } else {
1788
1750
  witnessIndex = this.getCoinInputWitnessIndexByOwner(recipient);
1789
1751
  if (typeof witnessIndex !== "number") {
1790
- witnessIndex = this.addEmptyWitness();
1752
+ witnessIndex = this.createWitness();
1791
1753
  }
1792
1754
  }
1793
1755
  const input = {
@@ -3309,6 +3271,7 @@ import { BigNumberCoder } from "@fuel-ts/abi-coder";
3309
3271
  import { ReceiptType as ReceiptType5 } from "@fuel-ts/transactions";
3310
3272
 
3311
3273
  // src/providers/utils/auto-retry-fetch.ts
3274
+ import { sleep } from "@fuel-ts/utils";
3312
3275
  function getWaitDelay(options, retryAttemptNum) {
3313
3276
  const duration = options.baseDelay ?? 150;
3314
3277
  switch (options.backoff) {
@@ -4714,21 +4677,6 @@ var Account = class extends AbstractAccount {
4714
4677
  }
4715
4678
  return this._connector.signMessage(this.address.toString(), message);
4716
4679
  }
4717
- /**
4718
- * Signs a transaction with the wallet's private key.
4719
- *
4720
- * @param transactionRequestLike - The transaction request to sign.
4721
- * @returns A promise that resolves to the signature of the transaction.
4722
- */
4723
- async signTransaction(transactionRequestLike) {
4724
- if (!this._connector) {
4725
- throw new FuelError14(
4726
- ErrorCode14.MISSING_CONNECTOR,
4727
- "A connector is required to sign transactions."
4728
- );
4729
- }
4730
- return this._connector.signTransaction(this.address.toString(), transactionRequestLike);
4731
- }
4732
4680
  /**
4733
4681
  * Sends a transaction to the network.
4734
4682
  *
@@ -5043,7 +4991,7 @@ var BaseWalletUnlocked = class extends Account {
5043
4991
  */
5044
4992
  async signTransaction(transactionRequestLike) {
5045
4993
  const transactionRequest = transactionRequestify(transactionRequestLike);
5046
- const chainId = this.provider.getChainId();
4994
+ const chainId = this.provider.getChain().consensusParameters.chainId.toNumber();
5047
4995
  const hashedTransaction = transactionRequest.getTransactionId(chainId);
5048
4996
  const signature = await this.signer().sign(hashedTransaction);
5049
4997
  return hexlify15(signature);
@@ -7914,7 +7862,8 @@ var launchNode = async ({
7914
7862
  useSystemFuelCore = false,
7915
7863
  loggingEnabled = true,
7916
7864
  debugEnabled = false,
7917
- basePath
7865
+ basePath,
7866
+ chainConfig = defaultChainConfig
7918
7867
  }) => (
7919
7868
  // eslint-disable-next-line no-async-promise-executor
7920
7869
  new Promise(async (resolve, reject) => {
@@ -7951,17 +7900,17 @@ var launchNode = async ({
7951
7900
  mkdirSync(tempDirPath, { recursive: true });
7952
7901
  }
7953
7902
  const tempChainConfigFilePath = path.join(tempDirPath, "chainConfig.json");
7954
- let chainConfig = defaultChainConfig;
7903
+ let generatedChainConfig = chainConfig;
7955
7904
  if (!process.env.GENESIS_SECRET) {
7956
7905
  const pk = Signer.generatePrivateKey();
7957
7906
  const signer = new Signer(pk);
7958
7907
  process.env.GENESIS_SECRET = hexlify18(pk);
7959
- chainConfig = {
7960
- ...defaultChainConfig,
7908
+ generatedChainConfig = {
7909
+ ...generatedChainConfig,
7961
7910
  initial_state: {
7962
- ...defaultChainConfig.initial_state,
7911
+ ...generatedChainConfig.initial_state,
7963
7912
  coins: [
7964
- ...defaultChainConfig.initial_state.coins,
7913
+ ...generatedChainConfig.initial_state.coins,
7965
7914
  {
7966
7915
  owner: signer.address.toHexString(),
7967
7916
  amount: toHex2(1e9),
@@ -7971,7 +7920,7 @@ var launchNode = async ({
7971
7920
  }
7972
7921
  };
7973
7922
  }
7974
- writeFileSync(tempChainConfigFilePath, JSON.stringify(chainConfig), "utf8");
7923
+ writeFileSync(tempChainConfigFilePath, JSON.stringify(generatedChainConfig), "utf8");
7975
7924
  chainConfigPathToUse = tempChainConfigFilePath;
7976
7925
  }
7977
7926
  const child = spawn(
@@ -8009,16 +7958,21 @@ var launchNode = async ({
8009
7958
  }
8010
7959
  };
8011
7960
  child.stderr.on("data", (chunk) => {
8012
- if (chunk.indexOf(graphQLStartSubstring) !== -1) {
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(":");
8013
7966
  resolve({
8014
7967
  cleanup: () => killNode(cleanupConfig),
8015
- ip: ipToUse,
8016
- port: portToUse,
7968
+ ip: realIp,
7969
+ port: realPort,
7970
+ url: `http://${realIp}:${realPort}/graphql`,
8017
7971
  chainConfigPath: chainConfigPathToUse
8018
7972
  });
8019
7973
  }
8020
- if (/error/i.test(chunk)) {
8021
- reject(chunk.toString());
7974
+ if (/error/i.test(text)) {
7975
+ reject(text.toString());
8022
7976
  }
8023
7977
  });
8024
7978
  process.on("exit", () => killNode(cleanupConfig));
@@ -8050,11 +8004,221 @@ var launchNodeAndGetWallets = async ({
8050
8004
  };
8051
8005
  return { wallets, stop: cleanup, provider };
8052
8006
  };
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
+ };
8053
8213
  export {
8214
+ AssetId,
8215
+ TestMessage,
8216
+ WalletConfig,
8054
8217
  generateTestWallet,
8055
8218
  killNode,
8056
8219
  launchNode,
8057
8220
  launchNodeAndGetWallets,
8058
- seedTestWallet
8221
+ seedTestWallet,
8222
+ setupTestProviderAndWallets
8059
8223
  };
8060
8224
  //# sourceMappingURL=test-utils.mjs.map