starknet 5.1.0 → 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.mjs CHANGED
@@ -48,8 +48,11 @@ __export(hash_exports, {
48
48
  calculateDeployTransactionHash: () => calculateDeployTransactionHash,
49
49
  calculateTransactionHash: () => calculateTransactionHash,
50
50
  calculateTransactionHashCommon: () => calculateTransactionHashCommon,
51
+ computeCompiledClassHash: () => computeCompiledClassHash,
51
52
  computeContractClassHash: () => computeContractClassHash,
52
53
  computeHashOnElements: () => computeHashOnElements,
54
+ computeLegacyContractClassHash: () => computeLegacyContractClassHash,
55
+ computeSieraContractClassHash: () => computeSieraContractClassHash,
53
56
  default: () => computeHintedClassHash,
54
57
  feeTransactionVersion: () => feeTransactionVersion,
55
58
  getSelector: () => getSelector,
@@ -61,6 +64,8 @@ __export(hash_exports, {
61
64
  });
62
65
  import { keccak256 } from "ethereum-cryptography/keccak.js";
63
66
  import { hexToBytes } from "ethereum-cryptography/utils.js";
67
+ import { sort } from "json-keys-sort";
68
+ import { poseidonHashMany } from "micro-starknet";
64
69
 
65
70
  // src/constants.ts
66
71
  var constants_exports = {};
@@ -2415,7 +2420,7 @@ __export(ec_exports, {
2415
2420
  starkCurve: () => starkCurve,
2416
2421
  weierstrass: () => weierstrass
2417
2422
  });
2418
- import * as starkCurve from "@noble/curves/stark";
2423
+ import * as starkCurve from "micro-starknet";
2419
2424
  import * as weierstrass from "@noble/curves/abstract/weierstrass";
2420
2425
 
2421
2426
  // src/utils/json.ts
@@ -2553,32 +2558,28 @@ function nullSkipReplacer(key, value) {
2553
2558
  }
2554
2559
  return value === null ? void 0 : value;
2555
2560
  }
2561
+ function formatSpaces(json2) {
2562
+ let insideQuotes = false;
2563
+ let newString = "";
2564
+ for (const char of json2) {
2565
+ if (char === '"' && newString.endsWith("\\") === false) {
2566
+ insideQuotes = !insideQuotes;
2567
+ }
2568
+ if (insideQuotes) {
2569
+ newString += char;
2570
+ } else {
2571
+ newString += char === ":" ? ": " : char === "," ? ", " : char;
2572
+ }
2573
+ }
2574
+ return newString;
2575
+ }
2556
2576
  function computeHintedClassHash(compiledContract) {
2557
2577
  const { abi, program } = compiledContract;
2558
2578
  const contractClass = { abi, program };
2559
- const serialisedJson = stringify(contractClass, nullSkipReplacer).split("").reduce(
2560
- ([insideQuotes, newString], char) => {
2561
- if (char === '"' && newString[newString.length - 1] !== "\\") {
2562
- insideQuotes = !insideQuotes;
2563
- }
2564
- if (insideQuotes) {
2565
- newString += char;
2566
- return [insideQuotes, newString];
2567
- }
2568
- if (char === ":" && !insideQuotes) {
2569
- newString += ": ";
2570
- } else if (char === "," && !insideQuotes) {
2571
- newString += ", ";
2572
- } else {
2573
- newString += char;
2574
- }
2575
- return [insideQuotes, newString];
2576
- },
2577
- [false, ""]
2578
- )[1];
2579
- return addHexPrefix(starkCurve.keccak(utf8ToArray(serialisedJson)).toString(16));
2579
+ const serializedJson = formatSpaces(stringify(contractClass, nullSkipReplacer));
2580
+ return addHexPrefix(starkCurve.keccak(utf8ToArray(serializedJson)).toString(16));
2580
2581
  }
2581
- function computeContractClassHash(contract) {
2582
+ function computeLegacyContractClassHash(contract) {
2582
2583
  const compiledContract = typeof contract === "string" ? parse(contract) : contract;
2583
2584
  const apiVersion = toHex(API_VERSION);
2584
2585
  const externalEntryPointsHash = computeHashOnElements(
@@ -2605,6 +2606,97 @@ function computeContractClassHash(contract) {
2605
2606
  dataHash
2606
2607
  ]);
2607
2608
  }
2609
+ function hashBuiltins(builtins) {
2610
+ return poseidonHashMany(
2611
+ builtins.flatMap((it) => {
2612
+ return BigInt(encodeShortString(it));
2613
+ })
2614
+ );
2615
+ }
2616
+ function hashEntryPoint(data) {
2617
+ const base = data.flatMap((it) => {
2618
+ return [BigInt(it.selector), BigInt(it.offset), hashBuiltins(it.builtins)];
2619
+ });
2620
+ return poseidonHashMany(base);
2621
+ }
2622
+ function parseHints(hints) {
2623
+ return hints.reduce((cum, [hint_id, hint_codes]) => {
2624
+ cum[hint_id] = hint_codes.map((it) => ({
2625
+ code: it,
2626
+ accessible_scopes: [],
2627
+ flow_tracking_data: { ap_tracking: { group: 0, offset: 0 }, reference_ids: {} }
2628
+ }));
2629
+ return cum;
2630
+ }, {});
2631
+ }
2632
+ function hintedProgram(casm) {
2633
+ const sortedHintedProgram = sort({
2634
+ program: {
2635
+ prime: casm.prime,
2636
+ data: casm.bytecode,
2637
+ builtins: [],
2638
+ hints: parseHints(casm.hints),
2639
+ compiler_version: casm.compiler_version
2640
+ }
2641
+ });
2642
+ const serialized = formatSpaces(stringify(sortedHintedProgram));
2643
+ return BigInt(addHexPrefix(starkCurve.keccak(utf8ToArray(serialized)).toString(16)));
2644
+ }
2645
+ function computeCompiledClassHash(casm) {
2646
+ const COMPILED_CLASS_VERSION = "COMPILED_CLASS_V1";
2647
+ const compiledClassVersion = BigInt(encodeShortString(COMPILED_CLASS_VERSION));
2648
+ const externalEntryPointsHash = hashEntryPoint(casm.entry_points_by_type.EXTERNAL);
2649
+ const l1Handlers = hashEntryPoint(casm.entry_points_by_type.L1_HANDLER);
2650
+ const constructor = hashEntryPoint(casm.entry_points_by_type.CONSTRUCTOR);
2651
+ const hintedCompiledClassHash = hintedProgram(casm);
2652
+ const bytecode = poseidonHashMany(casm.bytecode.map((it) => BigInt(it)));
2653
+ return toHex(
2654
+ poseidonHashMany([
2655
+ compiledClassVersion,
2656
+ externalEntryPointsHash,
2657
+ l1Handlers,
2658
+ constructor,
2659
+ hintedCompiledClassHash,
2660
+ bytecode
2661
+ ])
2662
+ );
2663
+ }
2664
+ function hashEntryPointSiera(data) {
2665
+ const base = data.flatMap((it) => {
2666
+ return [BigInt(it.selector), BigInt(it.function_idx)];
2667
+ });
2668
+ return poseidonHashMany(base);
2669
+ }
2670
+ function hashAbi(siera) {
2671
+ const indentString = stringify(siera.abi, null, 2);
2672
+ return BigInt(addHexPrefix(starkCurve.keccak(utf8ToArray(indentString)).toString(16)));
2673
+ }
2674
+ function computeSieraContractClassHash(siera) {
2675
+ const CONTRACT_CLASS_VERSION = "CONTRACT_CLASS_V0.1.0";
2676
+ const compiledClassVersion = BigInt(encodeShortString(CONTRACT_CLASS_VERSION));
2677
+ const externalEntryPointsHash = hashEntryPointSiera(siera.entry_points_by_type.EXTERNAL);
2678
+ const l1Handlers = hashEntryPointSiera(siera.entry_points_by_type.L1_HANDLER);
2679
+ const constructor = hashEntryPointSiera(siera.entry_points_by_type.CONSTRUCTOR);
2680
+ const abiHash = hashAbi(siera);
2681
+ const sieraProgram = poseidonHashMany(siera.sierra_program.map((it) => BigInt(it)));
2682
+ return toHex(
2683
+ poseidonHashMany([
2684
+ compiledClassVersion,
2685
+ externalEntryPointsHash,
2686
+ l1Handlers,
2687
+ constructor,
2688
+ abiHash,
2689
+ sieraProgram
2690
+ ])
2691
+ );
2692
+ }
2693
+ function computeContractClassHash(contract) {
2694
+ const compiledContract = typeof contract === "string" ? parse(contract) : contract;
2695
+ if ("sierra_program" in compiledContract) {
2696
+ return computeSieraContractClassHash(compiledContract);
2697
+ }
2698
+ return computeLegacyContractClassHash(compiledContract);
2699
+ }
2608
2700
 
2609
2701
  // src/utils/stark.ts
2610
2702
  var stark_exports = {};
@@ -2619,7 +2711,7 @@ __export(stark_exports, {
2619
2711
  signatureToDecimalArray: () => signatureToDecimalArray,
2620
2712
  signatureToHexArray: () => signatureToHexArray
2621
2713
  });
2622
- import { Signature, getStarkKey, utils } from "@noble/curves/stark";
2714
+ import { Signature, getStarkKey, utils } from "micro-starknet";
2623
2715
  import { gzip } from "pako";
2624
2716
  function compressProgram(jsonProgram) {
2625
2717
  const stringified = typeof jsonProgram === "string" ? jsonProgram : stringify(jsonProgram);
@@ -5370,7 +5462,7 @@ var AccountInterface = class extends ProviderInterface {
5370
5462
  };
5371
5463
 
5372
5464
  // src/utils/address.ts
5373
- import { arrayify } from "@ethersproject/bytes";
5465
+ import { hexToBytes as hexToBytes2 } from "@noble/curves/abstract/utils";
5374
5466
  function addAddressPadding(address) {
5375
5467
  return addHexPrefix(removeHexPrefix(toHex(address)).padStart(64, "0"));
5376
5468
  }
@@ -5384,7 +5476,8 @@ function validateAndParseAddress(address) {
5384
5476
  }
5385
5477
  function getChecksumAddress(address) {
5386
5478
  const chars = removeHexPrefix(validateAndParseAddress(address)).toLowerCase().split("");
5387
- const hashed = arrayify(keccakBn(address), { hexPad: "left" });
5479
+ const hex = removeHexPrefix(keccakBn(address));
5480
+ const hashed = hexToBytes2(hex.padStart(64, "0"));
5388
5481
  for (let i = 0; i < chars.length; i += 2) {
5389
5482
  if (hashed[i >> 1] >> 4 >= 8) {
5390
5483
  chars[i] = chars[i].toUpperCase();