starknet 5.3.0 → 5.4.1

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.js CHANGED
@@ -2508,24 +2508,22 @@ var weierstrass = __toESM(require("@noble/curves/abstract/weierstrass"));
2508
2508
  // src/utils/json.ts
2509
2509
  var json_exports = {};
2510
2510
  __export(json_exports, {
2511
- default: () => json_default,
2512
- parse: () => parse,
2511
+ parse: () => parse2,
2513
2512
  parseAlwaysAsBig: () => parseAlwaysAsBig,
2514
- stringify: () => stringify,
2513
+ stringify: () => stringify2,
2515
2514
  stringifyAlwaysAsBig: () => stringifyAlwaysAsBig
2516
2515
  });
2517
- var import_json_bigint = __toESM(require("json-bigint"));
2518
- var json = (alwaysParseAsBig) => {
2519
- return (0, import_json_bigint.default)({
2520
- alwaysParseAsBig,
2521
- useNativeBigInt: true,
2522
- protoAction: "preserve",
2523
- constructorAction: "preserve"
2524
- });
2516
+ var json = __toESM(require("lossless-json"));
2517
+ var parseIntAsNumberOrBigInt = (x) => {
2518
+ if (!json.isInteger(x))
2519
+ return parseFloat(x);
2520
+ const v = parseInt(x, 10);
2521
+ return Number.isSafeInteger(v) ? v : BigInt(x);
2525
2522
  };
2526
- var { parse, stringify } = json(false);
2527
- var { parse: parseAlwaysAsBig, stringify: stringifyAlwaysAsBig } = json(true);
2528
- var json_default = { parse, stringify };
2523
+ var parse2 = (x) => json.parse(String(x), null, parseIntAsNumberOrBigInt);
2524
+ var parseAlwaysAsBig = (x) => json.parse(String(x), null, json.parseNumberAndBigInt);
2525
+ var stringify2 = (...p) => json.stringify(...p);
2526
+ var stringifyAlwaysAsBig = stringify2;
2529
2527
 
2530
2528
  // src/utils/hash.ts
2531
2529
  var poseidon = __toESM(require("@noble/curves/abstract/poseidon"));
@@ -2659,11 +2657,11 @@ function formatSpaces(json2) {
2659
2657
  function computeHintedClassHash(compiledContract) {
2660
2658
  const { abi, program } = compiledContract;
2661
2659
  const contractClass = { abi, program };
2662
- const serializedJson = formatSpaces(stringify(contractClass, nullSkipReplacer));
2660
+ const serializedJson = formatSpaces(stringify2(contractClass, nullSkipReplacer));
2663
2661
  return addHexPrefix(starkCurve.keccak(utf8ToArray(serializedJson)).toString(16));
2664
2662
  }
2665
2663
  function computeLegacyContractClassHash(contract) {
2666
- const compiledContract = typeof contract === "string" ? parse(contract) : contract;
2664
+ const compiledContract = typeof contract === "string" ? parse2(contract) : contract;
2667
2665
  const apiVersion = toHex(API_VERSION);
2668
2666
  const externalEntryPointsHash = computeHashOnElements(
2669
2667
  compiledContract.entry_points_by_type.EXTERNAL.flatMap((e) => [e.selector, e.offset])
@@ -2726,7 +2724,7 @@ function hashEntryPointSierra(data) {
2726
2724
  return (0, import_micro_starknet.poseidonHashMany)(base);
2727
2725
  }
2728
2726
  function hashAbi(sierra) {
2729
- const indentString = formatSpaces(stringify(sierra.abi, null));
2727
+ const indentString = formatSpaces(stringify2(sierra.abi, null));
2730
2728
  return BigInt(addHexPrefix(starkCurve.keccak(utf8ToArray(indentString)).toString(16)));
2731
2729
  }
2732
2730
  function computeSierraContractClassHash(sierra) {
@@ -2749,7 +2747,7 @@ function computeSierraContractClassHash(sierra) {
2749
2747
  );
2750
2748
  }
2751
2749
  function computeContractClassHash(contract) {
2752
- const compiledContract = typeof contract === "string" ? parse(contract) : contract;
2750
+ const compiledContract = typeof contract === "string" ? parse2(contract) : contract;
2753
2751
  if ("sierra_program" in compiledContract) {
2754
2752
  return computeSierraContractClassHash(compiledContract);
2755
2753
  }
@@ -2757,9 +2755,8 @@ function computeContractClassHash(contract) {
2757
2755
  }
2758
2756
 
2759
2757
  // src/utils/contract.ts
2760
- var import_json_bigint2 = require("json-bigint");
2761
2758
  function isSierra(contract) {
2762
- const compiledContract = typeof contract === "string" ? (0, import_json_bigint2.parse)(contract) : contract;
2759
+ const compiledContract = typeof contract === "string" ? parse2(contract) : contract;
2763
2760
  return "sierra_program" in compiledContract;
2764
2761
  }
2765
2762
  function extractContractHashes(payload) {
@@ -2787,7 +2784,6 @@ __export(stark_exports, {
2787
2784
  estimatedFeeToMaxFee: () => estimatedFeeToMaxFee,
2788
2785
  formatSignature: () => formatSignature,
2789
2786
  makeAddress: () => makeAddress,
2790
- parseSignature: () => parseSignature,
2791
2787
  randomAddress: () => randomAddress,
2792
2788
  signatureToDecimalArray: () => signatureToDecimalArray,
2793
2789
  signatureToHexArray: () => signatureToHexArray
@@ -2795,7 +2791,7 @@ __export(stark_exports, {
2795
2791
  var import_micro_starknet2 = require("micro-starknet");
2796
2792
  var import_pako = require("pako");
2797
2793
  function compressProgram(jsonProgram) {
2798
- const stringified = typeof jsonProgram === "string" ? jsonProgram : stringify(jsonProgram);
2794
+ const stringified = typeof jsonProgram === "string" ? jsonProgram : stringify2(jsonProgram);
2799
2795
  const compressedProgram = (0, import_pako.gzip)(stringified);
2800
2796
  return btoaUniversal(compressedProgram);
2801
2797
  }
@@ -2808,12 +2804,15 @@ function makeAddress(input) {
2808
2804
  }
2809
2805
  function formatSignature(sig) {
2810
2806
  if (!sig)
2811
- return [];
2807
+ throw Error("formatSignature: provided signature is undefined");
2808
+ if (Array.isArray(sig)) {
2809
+ return sig.map((it) => toHex(it));
2810
+ }
2812
2811
  try {
2813
2812
  const { r, s } = sig;
2814
2813
  return [toHex(r), toHex(s)];
2815
2814
  } catch (e) {
2816
- return [];
2815
+ throw new Error("Signature need to be weierstrass.SignatureType or an array for custom");
2817
2816
  }
2818
2817
  }
2819
2818
  function signatureToDecimalArray(sig) {
@@ -2822,12 +2821,6 @@ function signatureToDecimalArray(sig) {
2822
2821
  function signatureToHexArray(sig) {
2823
2822
  return bigNumberishArrayToHexadecimalStringArray(formatSignature(sig));
2824
2823
  }
2825
- function parseSignature(sig) {
2826
- if (!sig)
2827
- return void 0;
2828
- const [r, s] = sig;
2829
- return new import_micro_starknet2.Signature(toBigInt(r), toBigInt(s));
2830
- }
2831
2824
  function compileCalldata(args) {
2832
2825
  const compiledData = Object.values(args).flatMap((value) => {
2833
2826
  if (Array.isArray(value))
@@ -2865,13 +2858,13 @@ function parseCalldata(calldata = []) {
2865
2858
  function createSierraContractClass(contract) {
2866
2859
  const result = { ...contract };
2867
2860
  delete result.sierra_program_debug_info;
2868
- result.abi = formatSpaces(stringify(contract.abi));
2869
- result.sierra_program = formatSpaces(stringify(contract.sierra_program));
2861
+ result.abi = formatSpaces(stringify2(contract.abi));
2862
+ result.sierra_program = formatSpaces(stringify2(contract.sierra_program));
2870
2863
  result.sierra_program = compressProgram(result.sierra_program);
2871
2864
  return result;
2872
2865
  }
2873
2866
  function parseContract(contract) {
2874
- const parsedContract = typeof contract === "string" ? parse(contract) : contract;
2867
+ const parsedContract = typeof contract === "string" ? parse2(contract) : contract;
2875
2868
  if (!isSierra(contract)) {
2876
2869
  return {
2877
2870
  ...parsedContract,
@@ -2921,8 +2914,8 @@ var RPCResponseParser = class {
2921
2914
  };
2922
2915
 
2923
2916
  // src/provider/errors.ts
2924
- var import_ts_custom_error = require("ts-custom-error");
2925
- var LibraryError = class extends import_ts_custom_error.CustomError {
2917
+ var import_custom_error = require("ts-custom-error/dist/custom-error");
2918
+ var LibraryError = class extends import_custom_error.CustomError {
2926
2919
  };
2927
2920
  var GatewayError = class extends LibraryError {
2928
2921
  constructor(message, errorCode) {
@@ -3143,7 +3136,7 @@ var RpcProvider = class {
3143
3136
  fetch(method, params) {
3144
3137
  return fetchPonyfill_default(this.nodeUrl, {
3145
3138
  method: "POST",
3146
- body: stringify({ method, jsonrpc: "2.0", params, id: 0 }),
3139
+ body: stringify2({ method, jsonrpc: "2.0", params, id: 0 }),
3147
3140
  headers: this.headers
3148
3141
  });
3149
3142
  }
@@ -3462,7 +3455,7 @@ var SequencerAPIResponseParser = class extends ResponseParser {
3462
3455
  max_fee: "max_fee" in res.transaction ? res.transaction.max_fee : void 0,
3463
3456
  nonce: res.transaction.nonce,
3464
3457
  sender_address: "sender_address" in res.transaction ? res.transaction.sender_address : void 0,
3465
- signature: "signature" in res.transaction ? parseSignature(res.transaction.signature) : void 0,
3458
+ signature: "signature" in res.transaction ? res.transaction.signature : void 0,
3466
3459
  transaction_hash: "transaction_hash" in res.transaction ? res.transaction.transaction_hash : void 0,
3467
3460
  version: "version" in res.transaction ? res.transaction.version : void 0
3468
3461
  };
@@ -3730,20 +3723,20 @@ var SequencerProvider = class {
3730
3723
  try {
3731
3724
  const response = await fetchPonyfill_default(url, {
3732
3725
  method,
3733
- body: stringify(options == null ? void 0 : options.body),
3726
+ body: stringify2(options == null ? void 0 : options.body),
3734
3727
  headers
3735
3728
  });
3736
3729
  const textResponse = await response.text();
3737
3730
  if (!response.ok) {
3738
3731
  let responseBody;
3739
3732
  try {
3740
- responseBody = parse(textResponse);
3733
+ responseBody = parse2(textResponse);
3741
3734
  } catch {
3742
3735
  throw new HttpError(response.statusText, response.status);
3743
3736
  }
3744
3737
  throw new GatewayError(responseBody.message, responseBody.code);
3745
3738
  }
3746
- const parseChoice = (options == null ? void 0 : options.parseAlwaysAsBigInt) ? parseAlwaysAsBig : parse;
3739
+ const parseChoice = (options == null ? void 0 : options.parseAlwaysAsBigInt) ? parseAlwaysAsBig : parse2;
3747
3740
  return parseChoice(textResponse);
3748
3741
  } catch (error) {
3749
3742
  if (error instanceof Error && !(error instanceof LibraryError))
@@ -4812,7 +4805,10 @@ var transaction_exports = {};
4812
4805
  __export(transaction_exports, {
4813
4806
  fromCallsToExecuteCalldata: () => fromCallsToExecuteCalldata,
4814
4807
  fromCallsToExecuteCalldataWithNonce: () => fromCallsToExecuteCalldataWithNonce,
4815
- transformCallsToMulticallArrays: () => transformCallsToMulticallArrays
4808
+ fromCallsToExecuteCalldata_cairo1: () => fromCallsToExecuteCalldata_cairo1,
4809
+ getExecuteCalldata: () => getExecuteCalldata,
4810
+ transformCallsToMulticallArrays: () => transformCallsToMulticallArrays,
4811
+ transformCallsToMulticallArrays_cairo1: () => transformCallsToMulticallArrays_cairo1
4816
4812
  });
4817
4813
  var transformCallsToMulticallArrays = (calls) => {
4818
4814
  const callArray = [];
@@ -4846,6 +4842,27 @@ var fromCallsToExecuteCalldata = (calls) => {
4846
4842
  var fromCallsToExecuteCalldataWithNonce = (calls, nonce) => {
4847
4843
  return [...fromCallsToExecuteCalldata(calls), toBigInt(nonce).toString()];
4848
4844
  };
4845
+ var transformCallsToMulticallArrays_cairo1 = (calls) => {
4846
+ const callArray = calls.map((call) => ({
4847
+ to: toBigInt(call.contractAddress).toString(10),
4848
+ selector: toBigInt(getSelectorFromName(call.entrypoint)).toString(10),
4849
+ calldata: bigNumberishArrayToDecimalStringArray(call.calldata || [])
4850
+ }));
4851
+ return callArray;
4852
+ };
4853
+ var fromCallsToExecuteCalldata_cairo1 = (calls) => {
4854
+ const callArray = transformCallsToMulticallArrays_cairo1(calls);
4855
+ return [
4856
+ callArray.length.toString(),
4857
+ ...callArray.map(({ to, selector, calldata }) => [to, selector, calldata.length.toString(), ...calldata]).flat()
4858
+ ];
4859
+ };
4860
+ var getExecuteCalldata = (calls, cairoVersion = "0") => {
4861
+ if (cairoVersion === "1") {
4862
+ return fromCallsToExecuteCalldata_cairo1(calls);
4863
+ }
4864
+ return fromCallsToExecuteCalldata(calls);
4865
+ };
4849
4866
 
4850
4867
  // src/utils/typedData/index.ts
4851
4868
  var typedData_exports = {};
@@ -5072,7 +5089,7 @@ var Signer = class {
5072
5089
  if (abis && abis.length !== transactions.length) {
5073
5090
  throw new Error("ABI must be provided for each transaction or no transaction");
5074
5091
  }
5075
- const calldata = fromCallsToExecuteCalldata(transactions);
5092
+ const calldata = getExecuteCalldata(transactions, transactionsDetail.cairoVersion);
5076
5093
  const msgHash = calculateTransactionHash(
5077
5094
  transactionsDetail.walletAddress,
5078
5095
  transactionsDetail.version,
@@ -5163,7 +5180,7 @@ var Account = class extends Provider {
5163
5180
  async estimateFee(calls, estimateFeeDetails) {
5164
5181
  return this.estimateInvokeFee(calls, estimateFeeDetails);
5165
5182
  }
5166
- async estimateInvokeFee(calls, { nonce: providedNonce, blockIdentifier, skipValidate } = {}) {
5183
+ async estimateInvokeFee(calls, { nonce: providedNonce, blockIdentifier, skipValidate, cairoVersion } = {}) {
5167
5184
  const transactions = Array.isArray(calls) ? calls : [calls];
5168
5185
  const nonce = toBigInt(providedNonce ?? await this.getNonce());
5169
5186
  const version = toBigInt(feeTransactionVersion);
@@ -5173,7 +5190,8 @@ var Account = class extends Provider {
5173
5190
  nonce,
5174
5191
  maxFee: ZERO,
5175
5192
  version,
5176
- chainId
5193
+ chainId,
5194
+ cairoVersion: cairoVersion ?? "0"
5177
5195
  };
5178
5196
  const invocation = await this.buildInvocation(transactions, signerDetails);
5179
5197
  const response = await super.getInvokeEstimateFee(
@@ -5188,13 +5206,20 @@ var Account = class extends Provider {
5188
5206
  suggestedMaxFee
5189
5207
  };
5190
5208
  }
5191
- async estimateDeclareFee({ contract, classHash: providedClassHash, casm, compiledClassHash }, { blockIdentifier, nonce: providedNonce, skipValidate } = {}) {
5209
+ async estimateDeclareFee({ contract, classHash: providedClassHash, casm, compiledClassHash }, { blockIdentifier, nonce: providedNonce, skipValidate, cairoVersion } = {}) {
5192
5210
  const nonce = toBigInt(providedNonce ?? await this.getNonce());
5193
5211
  const version = !isSierra(contract) ? toBigInt(feeTransactionVersion) : transactionVersion_2;
5194
5212
  const chainId = await this.getChainId();
5195
5213
  const declareContractTransaction = await this.buildDeclarePayload(
5196
5214
  { classHash: providedClassHash, contract, casm, compiledClassHash },
5197
- { nonce, chainId, version, walletAddress: this.address, maxFee: ZERO }
5215
+ {
5216
+ nonce,
5217
+ chainId,
5218
+ version,
5219
+ walletAddress: this.address,
5220
+ maxFee: ZERO,
5221
+ cairoVersion: cairoVersion ?? "0"
5222
+ }
5198
5223
  );
5199
5224
  const response = await super.getDeclareEstimateFee(
5200
5225
  declareContractTransaction,
@@ -5213,13 +5238,20 @@ var Account = class extends Provider {
5213
5238
  addressSalt = 0,
5214
5239
  constructorCalldata = [],
5215
5240
  contractAddress: providedContractAddress
5216
- }, { blockIdentifier, skipValidate } = {}) {
5241
+ }, { blockIdentifier, skipValidate, cairoVersion } = {}) {
5217
5242
  const version = toBigInt(feeTransactionVersion);
5218
5243
  const nonce = ZERO;
5219
5244
  const chainId = await this.getChainId();
5220
5245
  const payload = await this.buildAccountDeployPayload(
5221
5246
  { classHash, addressSalt, constructorCalldata, contractAddress: providedContractAddress },
5222
- { nonce, chainId, version, walletAddress: this.address, maxFee: ZERO }
5247
+ {
5248
+ nonce,
5249
+ chainId,
5250
+ version,
5251
+ walletAddress: this.address,
5252
+ maxFee: ZERO,
5253
+ cairoVersion: cairoVersion ?? "0"
5254
+ }
5223
5255
  );
5224
5256
  const response = await super.getDeployAccountEstimateFee(
5225
5257
  { ...payload },
@@ -5237,7 +5269,7 @@ var Account = class extends Provider {
5237
5269
  const calls = this.buildUDCContractPayload(payload);
5238
5270
  return this.estimateInvokeFee(calls, transactionsDetail);
5239
5271
  }
5240
- async estimateFeeBulk(transactions, { nonce: providedNonce, blockIdentifier } = {}) {
5272
+ async estimateFeeBulk(transactions, { nonce: providedNonce, blockIdentifier, cairoVersion } = {}) {
5241
5273
  const nonce = toBigInt(providedNonce ?? await this.getNonce());
5242
5274
  const version = toBigInt(feeTransactionVersion);
5243
5275
  const chainId = await this.getChainId();
@@ -5248,7 +5280,8 @@ var Account = class extends Provider {
5248
5280
  nonce: toBigInt(Number(nonce) + index),
5249
5281
  maxFee: ZERO,
5250
5282
  version,
5251
- chainId
5283
+ chainId,
5284
+ cairoVersion: cairoVersion ?? "0"
5252
5285
  };
5253
5286
  const txPayload = transaction.payload;
5254
5287
  let res;
@@ -5306,7 +5339,7 @@ var Account = class extends Provider {
5306
5339
  });
5307
5340
  }
5308
5341
  async buildInvocation(call, signerDetails) {
5309
- const calldata = fromCallsToExecuteCalldata(call);
5342
+ const calldata = getExecuteCalldata(call, signerDetails.cairoVersion);
5310
5343
  const signature = await this.signer.signTransaction(call, signerDetails);
5311
5344
  return {
5312
5345
  contractAddress: this.address,
@@ -5323,15 +5356,17 @@ var Account = class extends Provider {
5323
5356
  );
5324
5357
  const version = toBigInt(transactionVersion);
5325
5358
  const chainId = await this.getChainId();
5359
+ const cairoVersion = transactionsDetail.cairoVersion ?? "0";
5326
5360
  const signerDetails = {
5327
5361
  walletAddress: this.address,
5328
5362
  nonce,
5329
5363
  maxFee,
5330
5364
  version,
5331
- chainId
5365
+ chainId,
5366
+ cairoVersion
5332
5367
  };
5333
5368
  const signature = await this.signer.signTransaction(transactions, signerDetails, abis);
5334
- const calldata = fromCallsToExecuteCalldata(transactions);
5369
+ const calldata = getExecuteCalldata(transactions, cairoVersion);
5335
5370
  return this.invokeFunction(
5336
5371
  { contractAddress: this.address, calldata, signature },
5337
5372
  {
@@ -5356,7 +5391,8 @@ var Account = class extends Provider {
5356
5391
  details.chainId = await this.getChainId();
5357
5392
  const declareContractTransaction = await this.buildDeclarePayload(declareContractPayload, {
5358
5393
  ...details,
5359
- walletAddress: this.address
5394
+ walletAddress: this.address,
5395
+ cairoVersion: transactionsDetail.cairoVersion ?? "0"
5360
5396
  });
5361
5397
  return this.declareContract(declareContractTransaction, details);
5362
5398
  }
@@ -5466,7 +5502,7 @@ var Account = class extends Provider {
5466
5502
  entrypoint: "isValidSignature",
5467
5503
  calldata: compileCalldata({
5468
5504
  hash: toBigInt(hash).toString(),
5469
- signature: [toHex(signature.r), toHex(signature.s)]
5505
+ signature: formatSignature(signature)
5470
5506
  })
5471
5507
  });
5472
5508
  return true;
@@ -5497,7 +5533,7 @@ var Account = class extends Provider {
5497
5533
  feeEstimate = { suggestedMaxFee: ZERO, overall_fee: ZERO };
5498
5534
  break;
5499
5535
  }
5500
- return feeEstimate.suggestedMaxFee.toString();
5536
+ return feeEstimate.suggestedMaxFee;
5501
5537
  }
5502
5538
  async buildDeclarePayload(payload, { nonce, chainId, version, walletAddress, maxFee }) {
5503
5539
  const { classHash, contract, compiledClassHash } = extractContractHashes(payload);
@@ -5565,7 +5601,7 @@ var Account = class extends Provider {
5565
5601
  });
5566
5602
  return calls;
5567
5603
  }
5568
- async simulateTransaction(calls, { nonce: providedNonce, blockIdentifier, skipValidate } = {}) {
5604
+ async simulateTransaction(calls, { nonce: providedNonce, blockIdentifier, skipValidate, cairoVersion } = {}) {
5569
5605
  const transactions = Array.isArray(calls) ? calls : [calls];
5570
5606
  const nonce = toBigInt(providedNonce ?? await this.getNonce());
5571
5607
  const version = toBigInt(feeTransactionVersion);
@@ -5575,7 +5611,8 @@ var Account = class extends Provider {
5575
5611
  nonce,
5576
5612
  maxFee: ZERO,
5577
5613
  version,
5578
- chainId
5614
+ chainId,
5615
+ cairoVersion: cairoVersion ?? "0"
5579
5616
  };
5580
5617
  const invocation = await this.buildInvocation(transactions, signerDetails);
5581
5618
  const response = await super.getSimulateTransaction(