starknet 5.1.1 → 5.2.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.js CHANGED
@@ -110,8 +110,11 @@ __export(hash_exports, {
110
110
  calculateDeployTransactionHash: () => calculateDeployTransactionHash,
111
111
  calculateTransactionHash: () => calculateTransactionHash,
112
112
  calculateTransactionHashCommon: () => calculateTransactionHashCommon,
113
+ computeCompiledClassHash: () => computeCompiledClassHash,
113
114
  computeContractClassHash: () => computeContractClassHash,
114
115
  computeHashOnElements: () => computeHashOnElements,
116
+ computeLegacyContractClassHash: () => computeLegacyContractClassHash,
117
+ computeSieraContractClassHash: () => computeSieraContractClassHash,
115
118
  default: () => computeHintedClassHash,
116
119
  feeTransactionVersion: () => feeTransactionVersion,
117
120
  getSelector: () => getSelector,
@@ -123,6 +126,8 @@ __export(hash_exports, {
123
126
  });
124
127
  var import_keccak = require("ethereum-cryptography/keccak.js");
125
128
  var import_utils = require("ethereum-cryptography/utils.js");
129
+ var import_json_keys_sort = require("json-keys-sort");
130
+ var import_micro_starknet = require("micro-starknet");
126
131
 
127
132
  // src/constants.ts
128
133
  var constants_exports = {};
@@ -2477,7 +2482,7 @@ __export(ec_exports, {
2477
2482
  starkCurve: () => starkCurve,
2478
2483
  weierstrass: () => weierstrass
2479
2484
  });
2480
- var starkCurve = __toESM(require("@noble/curves/stark"));
2485
+ var starkCurve = __toESM(require("micro-starknet"));
2481
2486
  var weierstrass = __toESM(require("@noble/curves/abstract/weierstrass"));
2482
2487
 
2483
2488
  // src/utils/json.ts
@@ -2615,32 +2620,28 @@ function nullSkipReplacer(key, value) {
2615
2620
  }
2616
2621
  return value === null ? void 0 : value;
2617
2622
  }
2623
+ function formatSpaces(json2) {
2624
+ let insideQuotes = false;
2625
+ let newString = "";
2626
+ for (const char of json2) {
2627
+ if (char === '"' && newString.endsWith("\\") === false) {
2628
+ insideQuotes = !insideQuotes;
2629
+ }
2630
+ if (insideQuotes) {
2631
+ newString += char;
2632
+ } else {
2633
+ newString += char === ":" ? ": " : char === "," ? ", " : char;
2634
+ }
2635
+ }
2636
+ return newString;
2637
+ }
2618
2638
  function computeHintedClassHash(compiledContract) {
2619
2639
  const { abi, program } = compiledContract;
2620
2640
  const contractClass = { abi, program };
2621
- const serialisedJson = stringify(contractClass, nullSkipReplacer).split("").reduce(
2622
- ([insideQuotes, newString], char) => {
2623
- if (char === '"' && newString[newString.length - 1] !== "\\") {
2624
- insideQuotes = !insideQuotes;
2625
- }
2626
- if (insideQuotes) {
2627
- newString += char;
2628
- return [insideQuotes, newString];
2629
- }
2630
- if (char === ":" && !insideQuotes) {
2631
- newString += ": ";
2632
- } else if (char === "," && !insideQuotes) {
2633
- newString += ", ";
2634
- } else {
2635
- newString += char;
2636
- }
2637
- return [insideQuotes, newString];
2638
- },
2639
- [false, ""]
2640
- )[1];
2641
- return addHexPrefix(starkCurve.keccak(utf8ToArray(serialisedJson)).toString(16));
2641
+ const serializedJson = formatSpaces(stringify(contractClass, nullSkipReplacer));
2642
+ return addHexPrefix(starkCurve.keccak(utf8ToArray(serializedJson)).toString(16));
2642
2643
  }
2643
- function computeContractClassHash(contract) {
2644
+ function computeLegacyContractClassHash(contract) {
2644
2645
  const compiledContract = typeof contract === "string" ? parse(contract) : contract;
2645
2646
  const apiVersion = toHex(API_VERSION);
2646
2647
  const externalEntryPointsHash = computeHashOnElements(
@@ -2667,6 +2668,97 @@ function computeContractClassHash(contract) {
2667
2668
  dataHash
2668
2669
  ]);
2669
2670
  }
2671
+ function hashBuiltins(builtins) {
2672
+ return (0, import_micro_starknet.poseidonHashMany)(
2673
+ builtins.flatMap((it) => {
2674
+ return BigInt(encodeShortString(it));
2675
+ })
2676
+ );
2677
+ }
2678
+ function hashEntryPoint(data) {
2679
+ const base = data.flatMap((it) => {
2680
+ return [BigInt(it.selector), BigInt(it.offset), hashBuiltins(it.builtins)];
2681
+ });
2682
+ return (0, import_micro_starknet.poseidonHashMany)(base);
2683
+ }
2684
+ function parseHints(hints) {
2685
+ return hints.reduce((cum, [hint_id, hint_codes]) => {
2686
+ cum[hint_id] = hint_codes.map((it) => ({
2687
+ code: it,
2688
+ accessible_scopes: [],
2689
+ flow_tracking_data: { ap_tracking: { group: 0, offset: 0 }, reference_ids: {} }
2690
+ }));
2691
+ return cum;
2692
+ }, {});
2693
+ }
2694
+ function hintedProgram(casm) {
2695
+ const sortedHintedProgram = (0, import_json_keys_sort.sort)({
2696
+ program: {
2697
+ prime: casm.prime,
2698
+ data: casm.bytecode,
2699
+ builtins: [],
2700
+ hints: parseHints(casm.hints),
2701
+ compiler_version: casm.compiler_version
2702
+ }
2703
+ });
2704
+ const serialized = formatSpaces(stringify(sortedHintedProgram));
2705
+ return BigInt(addHexPrefix(starkCurve.keccak(utf8ToArray(serialized)).toString(16)));
2706
+ }
2707
+ function computeCompiledClassHash(casm) {
2708
+ const COMPILED_CLASS_VERSION = "COMPILED_CLASS_V1";
2709
+ const compiledClassVersion = BigInt(encodeShortString(COMPILED_CLASS_VERSION));
2710
+ const externalEntryPointsHash = hashEntryPoint(casm.entry_points_by_type.EXTERNAL);
2711
+ const l1Handlers = hashEntryPoint(casm.entry_points_by_type.L1_HANDLER);
2712
+ const constructor = hashEntryPoint(casm.entry_points_by_type.CONSTRUCTOR);
2713
+ const hintedCompiledClassHash = hintedProgram(casm);
2714
+ const bytecode = (0, import_micro_starknet.poseidonHashMany)(casm.bytecode.map((it) => BigInt(it)));
2715
+ return toHex(
2716
+ (0, import_micro_starknet.poseidonHashMany)([
2717
+ compiledClassVersion,
2718
+ externalEntryPointsHash,
2719
+ l1Handlers,
2720
+ constructor,
2721
+ hintedCompiledClassHash,
2722
+ bytecode
2723
+ ])
2724
+ );
2725
+ }
2726
+ function hashEntryPointSiera(data) {
2727
+ const base = data.flatMap((it) => {
2728
+ return [BigInt(it.selector), BigInt(it.function_idx)];
2729
+ });
2730
+ return (0, import_micro_starknet.poseidonHashMany)(base);
2731
+ }
2732
+ function hashAbi(siera) {
2733
+ const indentString = stringify(siera.abi, null, 2);
2734
+ return BigInt(addHexPrefix(starkCurve.keccak(utf8ToArray(indentString)).toString(16)));
2735
+ }
2736
+ function computeSieraContractClassHash(siera) {
2737
+ const CONTRACT_CLASS_VERSION = "CONTRACT_CLASS_V0.1.0";
2738
+ const compiledClassVersion = BigInt(encodeShortString(CONTRACT_CLASS_VERSION));
2739
+ const externalEntryPointsHash = hashEntryPointSiera(siera.entry_points_by_type.EXTERNAL);
2740
+ const l1Handlers = hashEntryPointSiera(siera.entry_points_by_type.L1_HANDLER);
2741
+ const constructor = hashEntryPointSiera(siera.entry_points_by_type.CONSTRUCTOR);
2742
+ const abiHash = hashAbi(siera);
2743
+ const sieraProgram = (0, import_micro_starknet.poseidonHashMany)(siera.sierra_program.map((it) => BigInt(it)));
2744
+ return toHex(
2745
+ (0, import_micro_starknet.poseidonHashMany)([
2746
+ compiledClassVersion,
2747
+ externalEntryPointsHash,
2748
+ l1Handlers,
2749
+ constructor,
2750
+ abiHash,
2751
+ sieraProgram
2752
+ ])
2753
+ );
2754
+ }
2755
+ function computeContractClassHash(contract) {
2756
+ const compiledContract = typeof contract === "string" ? parse(contract) : contract;
2757
+ if ("sierra_program" in compiledContract) {
2758
+ return computeSieraContractClassHash(compiledContract);
2759
+ }
2760
+ return computeLegacyContractClassHash(compiledContract);
2761
+ }
2670
2762
 
2671
2763
  // src/utils/stark.ts
2672
2764
  var stark_exports = {};
@@ -2681,7 +2773,7 @@ __export(stark_exports, {
2681
2773
  signatureToDecimalArray: () => signatureToDecimalArray,
2682
2774
  signatureToHexArray: () => signatureToHexArray
2683
2775
  });
2684
- var import_stark = require("@noble/curves/stark");
2776
+ var import_micro_starknet2 = require("micro-starknet");
2685
2777
  var import_pako = require("pako");
2686
2778
  function compressProgram(jsonProgram) {
2687
2779
  const stringified = typeof jsonProgram === "string" ? jsonProgram : stringify(jsonProgram);
@@ -2689,8 +2781,8 @@ function compressProgram(jsonProgram) {
2689
2781
  return btoaUniversal(compressedProgram);
2690
2782
  }
2691
2783
  function randomAddress() {
2692
- const randomKeyPair = import_stark.utils.randomPrivateKey();
2693
- return (0, import_stark.getStarkKey)(randomKeyPair);
2784
+ const randomKeyPair = import_micro_starknet2.utils.randomPrivateKey();
2785
+ return (0, import_micro_starknet2.getStarkKey)(randomKeyPair);
2694
2786
  }
2695
2787
  function makeAddress(input) {
2696
2788
  return addHexPrefix(input).toLowerCase();
@@ -2715,7 +2807,7 @@ function parseSignature(sig) {
2715
2807
  if (!sig)
2716
2808
  return void 0;
2717
2809
  const [r, s] = sig;
2718
- return new import_stark.Signature(toBigInt(r), toBigInt(s));
2810
+ return new import_micro_starknet2.Signature(toBigInt(r), toBigInt(s));
2719
2811
  }
2720
2812
  function compileCalldata(args) {
2721
2813
  const compiledData = Object.values(args).flatMap((value) => {
@@ -5432,7 +5524,7 @@ var AccountInterface = class extends ProviderInterface {
5432
5524
  };
5433
5525
 
5434
5526
  // src/utils/address.ts
5435
- var import_bytes = require("@ethersproject/bytes");
5527
+ var import_utils5 = require("@noble/curves/abstract/utils");
5436
5528
  function addAddressPadding(address) {
5437
5529
  return addHexPrefix(removeHexPrefix(toHex(address)).padStart(64, "0"));
5438
5530
  }
@@ -5446,7 +5538,8 @@ function validateAndParseAddress(address) {
5446
5538
  }
5447
5539
  function getChecksumAddress(address) {
5448
5540
  const chars = removeHexPrefix(validateAndParseAddress(address)).toLowerCase().split("");
5449
- const hashed = (0, import_bytes.arrayify)(keccakBn(address), { hexPad: "left" });
5541
+ const hex = removeHexPrefix(keccakBn(address));
5542
+ const hashed = (0, import_utils5.hexToBytes)(hex.padStart(64, "0"));
5450
5543
  for (let i = 0; i < chars.length; i += 2) {
5451
5544
  if (hashed[i >> 1] >> 4 >= 8) {
5452
5545
  chars[i] = chars[i].toUpperCase();