starknet 10.1.0 → 10.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.
package/dist/index.mjs CHANGED
@@ -1851,6 +1851,24 @@ var CairoBytes31 = class _CairoBytes31 {
1851
1851
  }
1852
1852
  };
1853
1853
 
1854
+ // src/utils/ec.ts
1855
+ var ec_exports = {};
1856
+ __export(ec_exports, {
1857
+ starkCurve: () => starkCurve,
1858
+ weierstrass: () => weierstrass
1859
+ });
1860
+ import * as starkCurve from "@scure/starknet";
1861
+ import * as weierstrass from "@noble/curves/abstract/weierstrass";
1862
+
1863
+ // src/utils/hash/pedersenCore.ts
1864
+ function computePedersenHash(a, b) {
1865
+ return starkCurve.pedersen(BigInt(a), BigInt(b));
1866
+ }
1867
+ function computeHashOnElements(data) {
1868
+ return [...data, data.length].reduce((x, y) => starkCurve.pedersen(BigInt(x), BigInt(y)), 0).toString();
1869
+ }
1870
+ var computePedersenHashOnElements = computeHashOnElements;
1871
+
1854
1872
  // src/utils/errors/rpc.ts
1855
1873
  var errorCodes = {
1856
1874
  FAILED_TO_RECEIVE_TXN: 1,
@@ -2145,6 +2163,31 @@ var CairoByteArray = class _CairoByteArray {
2145
2163
  const allBytes = concatenateArrayBuffer(this.toElements());
2146
2164
  return buffer_default.from(allBytes);
2147
2165
  }
2166
+ /**
2167
+ * Compute the Pedersen hash of this ByteArray, following OpenZeppelin's `hash_byte_array` algorithm.
2168
+ *
2169
+ * Serializes the ByteArray to its felt252 components (data array length, each data chunk,
2170
+ * pending_word, pending_word_len), then chains Pedersen hash over all elements starting
2171
+ * from 0, and finalizes with the total element count.
2172
+ *
2173
+ * @returns {string} hex-string felt252 Pedersen hash of the ByteArray
2174
+ * @example
2175
+ * ```typescript
2176
+ * const ba = new CairoByteArray('Hello');
2177
+ * const result = ba.hash();
2178
+ * // result = 0x15d19ad651ffaf8e90a13938db2081fa3ff01de0712e00cbe69891bace66c51
2179
+ * ```
2180
+ */
2181
+ hash() {
2182
+ this.assertInitialized();
2183
+ const serialized = [
2184
+ addHexPrefix(this.data.length.toString(16)),
2185
+ ...this.data.flatMap((bytes31) => bytes31.toApiRequest()),
2186
+ ...this.pending_word.toApiRequest(),
2187
+ ...this.pending_word_len.toApiRequest()
2188
+ ];
2189
+ return computeHashOnElements(serialized);
2190
+ }
2148
2191
  /**
2149
2192
  * returns an array of all the data chunks and the pending word
2150
2193
  * when concatenated, represents the original bytes sequence
@@ -4705,7 +4748,7 @@ __export(hash_exports, {
4705
4748
  computeCompiledClassHashBlake: () => computeCompiledClassHashBlake,
4706
4749
  computeCompiledClassHashPoseidon: () => computeCompiledClassHashPoseidon,
4707
4750
  computeContractClassHash: () => computeContractClassHash,
4708
- computeHashOnElements: () => computeHashOnElements2,
4751
+ computeHashOnElements: () => computeHashOnElements,
4709
4752
  computeHintedClassHash: () => computeHintedClassHash,
4710
4753
  computeLegacyContractClassHash: () => computeLegacyContractClassHash,
4711
4754
  computePedersenHash: () => computePedersenHash,
@@ -4845,24 +4888,13 @@ __export(v2_exports, {
4845
4888
  calculateL2MessageTxHash: () => calculateL2MessageTxHash,
4846
4889
  calculateTransactionHash: () => calculateTransactionHash,
4847
4890
  calculateTransactionHashCommon: () => calculateTransactionHashCommon2,
4848
- computeHashOnElements: () => computeHashOnElements
4891
+ computeHashOnElements: () => computeHashOnElements2
4849
4892
  });
4850
-
4851
- // src/utils/ec.ts
4852
- var ec_exports = {};
4853
- __export(ec_exports, {
4854
- starkCurve: () => starkCurve,
4855
- weierstrass: () => weierstrass
4856
- });
4857
- import * as starkCurve from "@scure/starknet";
4858
- import * as weierstrass from "@noble/curves/abstract/weierstrass";
4859
-
4860
- // src/utils/hash/transactionHash/v2.ts
4861
- function computeHashOnElements(data) {
4893
+ function computeHashOnElements2(data) {
4862
4894
  return [...data, data.length].reduce((x, y) => starkCurve.pedersen(toBigInt(x), toBigInt(y)), 0).toString();
4863
4895
  }
4864
4896
  function calculateTransactionHashCommon2(txHashPrefix, version, contractAddress, entryPointSelector, calldata, maxFee, chainId, additionalData = []) {
4865
- const calldataHash = computeHashOnElements(calldata);
4897
+ const calldataHash = computeHashOnElements2(calldata);
4866
4898
  const dataToHash = [
4867
4899
  txHashPrefix,
4868
4900
  version,
@@ -4873,7 +4905,7 @@ function calculateTransactionHashCommon2(txHashPrefix, version, contractAddress,
4873
4905
  chainId,
4874
4906
  ...additionalData
4875
4907
  ];
4876
- return computeHashOnElements(dataToHash);
4908
+ return computeHashOnElements2(dataToHash);
4877
4909
  }
4878
4910
  function calculateDeclareTransactionHash2(classHash, senderAddress, version, maxFee, chainId, nonce, compiledClassHash) {
4879
4911
  return calculateTransactionHashCommon2(
@@ -5032,18 +5064,11 @@ function flattenEntryPointData(data, encodedBuiltinsArray) {
5032
5064
  }
5033
5065
 
5034
5066
  // src/utils/hash/classHash/pedersen.ts
5035
- function computePedersenHash(a, b) {
5036
- return starkCurve.pedersen(BigInt(a), BigInt(b));
5037
- }
5038
- function computeHashOnElements2(data) {
5039
- return [...data, data.length].reduce((x, y) => starkCurve.pedersen(BigInt(x), BigInt(y)), 0).toString();
5040
- }
5041
- var computePedersenHashOnElements = computeHashOnElements2;
5042
5067
  function calculateContractAddressFromHash(salt, classHash, constructorCalldata, deployerAddress) {
5043
5068
  const compiledCalldata = CallData.compile(constructorCalldata);
5044
- const constructorCalldataHash = computeHashOnElements2(compiledCalldata);
5069
+ const constructorCalldataHash = computeHashOnElements(compiledCalldata);
5045
5070
  const CONTRACT_ADDRESS_PREFIX = felt("0x535441524b4e45545f434f4e54524143545f41444452455353");
5046
- const hash = computeHashOnElements2([
5071
+ const hash = computeHashOnElements([
5047
5072
  CONTRACT_ADDRESS_PREFIX,
5048
5073
  deployerAddress,
5049
5074
  salt,
@@ -5061,21 +5086,21 @@ function computeHintedClassHash(compiledContract) {
5061
5086
  function computeLegacyContractClassHash(contract) {
5062
5087
  const compiledContract = isString(contract) ? parse2(contract) : contract;
5063
5088
  const apiVersion = toHex(API_VERSION);
5064
- const externalEntryPointsHash = computeHashOnElements2(
5089
+ const externalEntryPointsHash = computeHashOnElements(
5065
5090
  compiledContract.entry_points_by_type.EXTERNAL.flatMap((e) => [e.selector, e.offset])
5066
5091
  );
5067
- const l1HandlerEntryPointsHash = computeHashOnElements2(
5092
+ const l1HandlerEntryPointsHash = computeHashOnElements(
5068
5093
  compiledContract.entry_points_by_type.L1_HANDLER.flatMap((e) => [e.selector, e.offset])
5069
5094
  );
5070
- const constructorEntryPointHash = computeHashOnElements2(
5095
+ const constructorEntryPointHash = computeHashOnElements(
5071
5096
  compiledContract.entry_points_by_type.CONSTRUCTOR.flatMap((e) => [e.selector, e.offset])
5072
5097
  );
5073
- const builtinsHash = computeHashOnElements2(
5098
+ const builtinsHash = computeHashOnElements(
5074
5099
  compiledContract.program.builtins.map((s) => encodeShortString(s))
5075
5100
  );
5076
5101
  const hintedClassHash = computeHintedClassHash(compiledContract);
5077
- const dataHash = computeHashOnElements2(compiledContract.program.data);
5078
- return computeHashOnElements2([
5102
+ const dataHash = computeHashOnElements(compiledContract.program.data);
5103
+ return computeHashOnElements([
5079
5104
  apiVersion,
5080
5105
  externalEntryPointsHash,
5081
5106
  l1HandlerEntryPointsHash,
@@ -6418,6 +6443,12 @@ var RpcChannel = class {
6418
6443
  });
6419
6444
  return this.waitMode ? this.waitForTransaction((await promise).transaction_hash) : promise;
6420
6445
  }
6446
+ async invokeSignedTx(transaction) {
6447
+ const promise = this.fetchEndpoint("starknet_addInvokeTransaction", {
6448
+ invoke_transaction: transaction
6449
+ });
6450
+ return this.waitMode ? this.waitForTransaction((await promise).transaction_hash) : promise;
6451
+ }
6421
6452
  async declare(declareTransaction, details) {
6422
6453
  const transaction = await this.buildTransaction(
6423
6454
  {
@@ -7027,6 +7058,12 @@ var RpcChannel2 = class {
7027
7058
  });
7028
7059
  return this.waitMode ? this.waitForTransaction((await promise).transaction_hash) : promise;
7029
7060
  }
7061
+ async invokeSignedTx(transaction) {
7062
+ const promise = this.fetchEndpoint("starknet_addInvokeTransaction", {
7063
+ invoke_transaction: transaction
7064
+ });
7065
+ return this.waitMode ? this.waitForTransaction((await promise).transaction_hash) : promise;
7066
+ }
7030
7067
  async declare(declareTransaction, details) {
7031
7068
  const transaction = await this.buildTransaction(
7032
7069
  {
@@ -9717,6 +9754,34 @@ var RpcProvider = class {
9717
9754
  async invokeFunction(functionInvocation, details) {
9718
9755
  return this.channel.invoke(functionInvocation, details);
9719
9756
  }
9757
+ /**
9758
+ * Submit a pre-signed INVOKE_TXN_V3 transaction to the network.
9759
+ *
9760
+ * Broadcasts a transaction previously built and signed by `Account.getSignedTransaction()`.
9761
+ * Fees are already included in the signed transaction and will not be re-estimated.
9762
+ *
9763
+ * @param transaction - A fully signed `RPC.INVOKE_TXN_V3` object, as returned by `Account.getSignedTransaction()`
9764
+ *
9765
+ * @returns The transaction hash if `waitMode` is disabled (default), or the transaction receipt if `waitMode` is enabled.
9766
+ *
9767
+ * @remarks
9768
+ * - The transaction must be signed before calling this method ; use `Account.getSignedTransaction()` to produce it.
9769
+ * - Resubmitting the same signed transaction (same nonce) will be rejected by the network.
9770
+ * - If `waitMode` is enabled on the provider, this method waits for the transaction to be included in a block before returning.
9771
+ *
9772
+ * @example
9773
+ * ```typescript
9774
+ * const signedTx = await account.getSignedTransaction([
9775
+ * { contractAddress: erc20Address, entrypoint: 'transfer', calldata: [recipient, amount, 0] }
9776
+ * ]);
9777
+ * // inspect or store signedTx, then submit when ready:
9778
+ * const { transaction_hash } = await provider.invokeSignedTx(signedTx);
9779
+ * await provider.waitForTransaction(transaction_hash);
9780
+ * ```
9781
+ */
9782
+ async invokeSignedTx(transaction) {
9783
+ return this.channel.invokeSignedTx(transaction);
9784
+ }
9720
9785
  async declareContract(transaction, details) {
9721
9786
  return this.channel.declare(transaction, details);
9722
9787
  }
@@ -11599,7 +11664,12 @@ var Account = class {
11599
11664
  returnInitialReads
11600
11665
  });
11601
11666
  }
11602
- async execute(transactions, transactionsDetail = {}) {
11667
+ /**
11668
+ * Shared preparation logic for execute() and buildExecute().
11669
+ * Runs hooks, estimates fees, and builds accountInvocations.
11670
+ * @private
11671
+ */
11672
+ async prepareInvoke(transactions, transactionsDetail = {}) {
11603
11673
  const hookResult = this.accountPluginManager.runAccountHook("beforeExecute", {
11604
11674
  calls: transactions,
11605
11675
  details: transactionsDetail
@@ -11624,7 +11694,15 @@ var Account = class {
11624
11694
  skipValidate: false
11625
11695
  }
11626
11696
  );
11627
- const invocation = accountInvocations[0];
11697
+ return {
11698
+ invocation: accountInvocations[0],
11699
+ hookedTransactions,
11700
+ hookedDetails,
11701
+ detailsWithTip
11702
+ };
11703
+ }
11704
+ async execute(transactions, transactionsDetail = {}) {
11705
+ const { invocation, hookedTransactions, hookedDetails, detailsWithTip } = await this.prepareInvoke(transactions, transactionsDetail);
11628
11706
  const result = await this.provider.invokeFunction(
11629
11707
  {
11630
11708
  contractAddress: invocation.contractAddress,
@@ -11646,6 +11724,51 @@ var Account = class {
11646
11724
  });
11647
11725
  return result;
11648
11726
  }
11727
+ /**
11728
+ * Build a signed INVOKE_TXN_V3 transaction without submitting it to the network.
11729
+ *
11730
+ * Produces a fully signed transaction object that can be inspected, stored,
11731
+ * or submitted later via `provider.channel.sendTransaction()`.
11732
+ * Main usage is to send a virtual transaction to a proof server.
11733
+ * Fees are estimated automatically if not provided.
11734
+ *
11735
+ * @param transactions - Single call or array of calls to include in the transaction
11736
+ * @param transactionsDetail - Transaction execution options
11737
+ * @returns A fully signed `RPC.INVOKE_TXN_V3` object, ready to broadcast
11738
+ *
11739
+ * @remarks
11740
+ * - Unlike `execute()`, this method does **not** submit the transaction ; the account nonce is unchanged after the call.
11741
+ * - The `afterExecute` plugin hook is intentionally **not** triggered.
11742
+ * - The returned object can be broadcast with `provider.channel.sendTransaction()`.
11743
+ *
11744
+ * @example
11745
+ * ```typescript
11746
+ * const signedTx = await account.getSignedTransaction(
11747
+ * { contractAddress: erc20Address, entrypoint: 'transfer', calldata: [recipient, amount, 0] }
11748
+ * );
11749
+ * ```
11750
+ */
11751
+ async getSignedTransaction(transactions, transactionsDetail = {}) {
11752
+ const { invocation, hookedDetails, detailsWithTip } = await this.prepareInvoke(
11753
+ transactions,
11754
+ transactionsDetail
11755
+ );
11756
+ return this.provider.channel.buildTransaction(
11757
+ {
11758
+ type: api_exports.ETransactionType.INVOKE,
11759
+ contractAddress: invocation.contractAddress,
11760
+ calldata: invocation.calldata,
11761
+ signature: invocation.signature,
11762
+ ...hookedDetails.proofFacts && { proofFacts: hookedDetails.proofFacts },
11763
+ ...hookedDetails.proof && { proof: hookedDetails.proof },
11764
+ ...v3Details(detailsWithTip),
11765
+ resourceBounds: invocation.resourceBounds,
11766
+ nonce: invocation.nonce,
11767
+ version: invocation.version
11768
+ },
11769
+ "transaction"
11770
+ );
11771
+ }
11649
11772
  /**
11650
11773
  * First check if contract is already declared, if not declare it
11651
11774
  * If contract already declared returned transaction_hash is ''.