starknet 11.5.0 → 11.5.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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [11.5.1](https://github.com/starknet-io/starknet.js/compare/v11.5.0...v11.5.1) (2026-11-04)
2
+
3
+ ### Bug Fixes
4
+
5
+ - **cairo-types:** refuse huge arrays of elements that read no felt ([3a8873d](https://github.com/starknet-io/starknet.js/commit/3a8873d0bc63c3baeb595b780cbd179dfcdf26e2))
6
+ - **typed-data:** bound the cost of hashing untrusted typed data ([be2cffc](https://github.com/starknet-io/starknet.js/commit/be2cffc5e797b894eab7c3b8d769716138da8fa4))
7
+ - **typed-data:** bound the work of hashing typed data ([0116d8a](https://github.com/starknet-io/starknet.js/commit/0116d8af3e8975ab51eca63a64295c1d5c2ee3cf))
8
+ - **typed-data:** refuse duplicate field names, share the zero-width bound across nesting ([8601fc3](https://github.com/starknet-io/starknet.js/commit/8601fc384f102442ee8e49a6457dc10bc03aa1f0))
9
+
1
10
  # [11.5.0](https://github.com/starknet-io/starknet.js/compare/v11.4.3...v11.5.0) (2026-11-02)
2
11
 
3
12
  ### Features
package/dist/index.d.ts CHANGED
@@ -8284,6 +8284,38 @@ declare const UDC: {
8284
8284
  declare const OutsideExecutionCallerAny = "0x414e595f43414c4c4552";
8285
8285
  declare const SNIP9_V1_INTERFACE_ID = "0x68cfd18b92d1907b8ba3cc324900277f5a3622099431ea85dd8089255e4181";
8286
8286
  declare const SNIP9_V2_INTERFACE_ID = "0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872";
8287
+ /**
8288
+ * The most types that the `types` object of one typed data message (SNIP-12) may declare.
8289
+ *
8290
+ * Real messages declare a few types: an outside execution (SNIP-9) declares 3, the domain
8291
+ * included. 64 leaves a wide margin above that. A higher limit would only let a message make each
8292
+ * struct cost more to hash, or overflow the call stack with a long chain of types. So the hash
8293
+ * functions of `typedData` refuse a message that declares more types than this.
8294
+ */
8295
+ declare const TYPED_DATA_MAX_TYPES = 64;
8296
+ /**
8297
+ * The most work that hashing one typed data message (SNIP-12) may take, counted in felts.
8298
+ *
8299
+ * A message can come from someone you do not trust, and a small message can make the hash take
8300
+ * minutes. So the hash functions of `typedData` give each hash this budget, and throw an error when
8301
+ * the budget runs out.
8302
+ *
8303
+ * The hash spends:
8304
+ * - 1 felt for each value: a struct field, an array element, an enum parameter, a merkle tree leaf;
8305
+ * - for a value written as text (a `string`, a selector name, a number written in digits), 1 felt
8306
+ * for each 31 characters, but the first 2 felts are free. So a felt written in hex or in digits
8307
+ * still costs 1 felt;
8308
+ * - 1 felt for each 31 characters of the type string of each struct.
8309
+ *
8310
+ * The value covers the largest outside execution (SNIP-9 V1 or V2) that Starknet accepts. A
8311
+ * transaction carries at most 5,000 felts of calldata, so at most about 1,660 calls, and an
8312
+ * outside execution V1 hashes them with about 13,300 felts. A higher value would only let a
8313
+ * message keep the CPU busy longer.
8314
+ *
8315
+ * A message of revision 0 hashes with Pedersen, about 15 times slower for each felt than Poseidon
8316
+ * (revision 1). So with the same budget, a revision 0 message can take about 15 times longer.
8317
+ */
8318
+ declare const TYPED_DATA_MAX_HASHED_FELTS = 13500;
8287
8319
  declare const HARDENING_BYTE = 128;
8288
8320
  declare const HARDENING_4BYTES = 2147483648n;
8289
8321
  declare const _NetworkName: {
@@ -8438,10 +8470,12 @@ type constants_SupportedCairoVersion = SupportedCairoVersion;
8438
8470
  type constants_SupportedRpcVersion0_10 = SupportedRpcVersion0_10;
8439
8471
  type constants_SupportedTransactionVersion = SupportedTransactionVersion;
8440
8472
  declare const constants_TEXT_TO_FELT_MAX_LEN: typeof TEXT_TO_FELT_MAX_LEN;
8473
+ declare const constants_TYPED_DATA_MAX_HASHED_FELTS: typeof TYPED_DATA_MAX_HASHED_FELTS;
8474
+ declare const constants_TYPED_DATA_MAX_TYPES: typeof TYPED_DATA_MAX_TYPES;
8441
8475
  declare const constants_UDC: typeof UDC;
8442
8476
  declare const constants_ZERO: typeof ZERO;
8443
8477
  declare namespace constants {
8444
- export { constants_ADDR_BOUND as ADDR_BOUND, constants_API_VERSION as API_VERSION, type constants_ChannelDefaultOptions as ChannelDefaultOptions, type constants_ChannelDefaults as ChannelDefaults, type constants_ChannelMethodOptions as ChannelMethodOptions, constants_DEFAULT_GLOBAL_CONFIG as DEFAULT_GLOBAL_CONFIG, constants_HARDENING_4BYTES as HARDENING_4BYTES, constants_HARDENING_BYTE as HARDENING_BYTE, constants_IS_BROWSER as IS_BROWSER, constants_LegacyUDC as LegacyUDC, constants_MASK_250 as MASK_250, constants_MASK_31 as MASK_31, constants_MAX_STORAGE_ITEM_SIZE as MAX_STORAGE_ITEM_SIZE, _NetworkName as NetworkName, constants_OutsideExecutionCallerAny as OutsideExecutionCallerAny, constants_PAYMASTER_RPC_NODES as PAYMASTER_RPC_NODES, constants_PRIME as PRIME, constants_RANGE_CLASS_HASH as RANGE_CLASS_HASH, constants_RANGE_CONTRACT_ADDRESS as RANGE_CONTRACT_ADDRESS, constants_RANGE_ETH_ADDRESS as RANGE_ETH_ADDRESS, constants_RANGE_FELT as RANGE_FELT, constants_RANGE_I128 as RANGE_I128, constants_RANGE_I16 as RANGE_I16, constants_RANGE_I32 as RANGE_I32, constants_RANGE_I64 as RANGE_I64, constants_RANGE_I8 as RANGE_I8, constants_RANGE_STORAGE_ADDRESS as RANGE_STORAGE_ADDRESS, constants_RANGE_U128 as RANGE_U128, constants_RANGE_U16 as RANGE_U16, constants_RANGE_U32 as RANGE_U32, constants_RANGE_U64 as RANGE_U64, constants_RANGE_U8 as RANGE_U8, constants_RANGE_U96 as RANGE_U96, constants_RPC_DEFAULT_NODES as RPC_DEFAULT_NODES, constants_SNIP9_V1_INTERFACE_ID as SNIP9_V1_INTERFACE_ID, constants_SNIP9_V2_INTERFACE_ID as SNIP9_V2_INTERFACE_ID, constants_SN_VERSION_IMPLEMENTING_BLAKE_FOR_COMPILED_CLASS as SN_VERSION_IMPLEMENTING_BLAKE_FOR_COMPILED_CLASS, constants_SYSTEM_MESSAGES as SYSTEM_MESSAGES, _StarknetChainId as StarknetChainId, type constants_SupportedCairoVersion as SupportedCairoVersion, _SupportedRpcVersion as SupportedRpcVersion, type constants_SupportedRpcVersion0_10 as SupportedRpcVersion0_10, type constants_SupportedTransactionVersion as SupportedTransactionVersion, constants_TEXT_TO_FELT_MAX_LEN as TEXT_TO_FELT_MAX_LEN, _TransactionHashPrefix as TransactionHashPrefix, constants_UDC as UDC, constants_ZERO as ZERO };
8478
+ export { constants_ADDR_BOUND as ADDR_BOUND, constants_API_VERSION as API_VERSION, type constants_ChannelDefaultOptions as ChannelDefaultOptions, type constants_ChannelDefaults as ChannelDefaults, type constants_ChannelMethodOptions as ChannelMethodOptions, constants_DEFAULT_GLOBAL_CONFIG as DEFAULT_GLOBAL_CONFIG, constants_HARDENING_4BYTES as HARDENING_4BYTES, constants_HARDENING_BYTE as HARDENING_BYTE, constants_IS_BROWSER as IS_BROWSER, constants_LegacyUDC as LegacyUDC, constants_MASK_250 as MASK_250, constants_MASK_31 as MASK_31, constants_MAX_STORAGE_ITEM_SIZE as MAX_STORAGE_ITEM_SIZE, _NetworkName as NetworkName, constants_OutsideExecutionCallerAny as OutsideExecutionCallerAny, constants_PAYMASTER_RPC_NODES as PAYMASTER_RPC_NODES, constants_PRIME as PRIME, constants_RANGE_CLASS_HASH as RANGE_CLASS_HASH, constants_RANGE_CONTRACT_ADDRESS as RANGE_CONTRACT_ADDRESS, constants_RANGE_ETH_ADDRESS as RANGE_ETH_ADDRESS, constants_RANGE_FELT as RANGE_FELT, constants_RANGE_I128 as RANGE_I128, constants_RANGE_I16 as RANGE_I16, constants_RANGE_I32 as RANGE_I32, constants_RANGE_I64 as RANGE_I64, constants_RANGE_I8 as RANGE_I8, constants_RANGE_STORAGE_ADDRESS as RANGE_STORAGE_ADDRESS, constants_RANGE_U128 as RANGE_U128, constants_RANGE_U16 as RANGE_U16, constants_RANGE_U32 as RANGE_U32, constants_RANGE_U64 as RANGE_U64, constants_RANGE_U8 as RANGE_U8, constants_RANGE_U96 as RANGE_U96, constants_RPC_DEFAULT_NODES as RPC_DEFAULT_NODES, constants_SNIP9_V1_INTERFACE_ID as SNIP9_V1_INTERFACE_ID, constants_SNIP9_V2_INTERFACE_ID as SNIP9_V2_INTERFACE_ID, constants_SN_VERSION_IMPLEMENTING_BLAKE_FOR_COMPILED_CLASS as SN_VERSION_IMPLEMENTING_BLAKE_FOR_COMPILED_CLASS, constants_SYSTEM_MESSAGES as SYSTEM_MESSAGES, _StarknetChainId as StarknetChainId, type constants_SupportedCairoVersion as SupportedCairoVersion, _SupportedRpcVersion as SupportedRpcVersion, type constants_SupportedRpcVersion0_10 as SupportedRpcVersion0_10, type constants_SupportedTransactionVersion as SupportedTransactionVersion, constants_TEXT_TO_FELT_MAX_LEN as TEXT_TO_FELT_MAX_LEN, constants_TYPED_DATA_MAX_HASHED_FELTS as TYPED_DATA_MAX_HASHED_FELTS, constants_TYPED_DATA_MAX_TYPES as TYPED_DATA_MAX_TYPES, _TransactionHashPrefix as TransactionHashPrefix, constants_UDC as UDC, constants_ZERO as ZERO };
8445
8479
  }
8446
8480
 
8447
8481
  /** What a {@link SubscriptionChannel} needs: a socket to borrow, and how much to buffer. */
@@ -13139,13 +13173,17 @@ declare function isMerkleTreeType(type: StarknetType): type is StarknetMerkleTyp
13139
13173
  * Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
13140
13174
  * in the resulting array.
13141
13175
  *
13176
+ * The walk visits each type once, even when several types point to it, so its cost follows the size of the type
13177
+ * graph.
13178
+ *
13142
13179
  * @param {TypedData['types']} types - The types object containing all defined types.
13143
13180
  * @param {string} type - The name of the type to get dependencies for.
13144
- * @param {string[]} [dependencies=[]] - The array to store dependencies.
13181
+ * @param {string[]} [dependencies=[]] - The type names to leave out when `type` points to them.
13145
13182
  * @param {string} [contains=''] - The type contained within the struct.
13146
13183
  * @param {Revision} [revision=Revision.Legacy] - The revision of the TypedData.
13147
13184
  *
13148
13185
  * @returns {string[]} The array of dependencies.
13186
+ * @throws Will throw an error if a type refers to itself, directly or through other types.
13149
13187
  */
13150
13188
  declare function getDependencies(types: TypedData['types'], type: string, dependencies?: string[], contains?: string, revision?: TypedDataRevision): string[];
13151
13189
  /**
@@ -13156,6 +13194,7 @@ declare function getDependencies(types: TypedData['types'], type: string, depend
13156
13194
  * @param {Revision} [revision=Revision.Legacy] - The revision of the TypedData.
13157
13195
  *
13158
13196
  * @returns {string} The encoded string.
13197
+ * @throws Will throw an error if the types declare more than `constants.TYPED_DATA_MAX_TYPES` types, or if a type refers to itself, directly or through other types.
13159
13198
  *
13160
13199
  * @example
13161
13200
  * ```typescript
@@ -13174,6 +13213,7 @@ declare function encodeType(types: TypedData['types'], type: string, revision?:
13174
13213
  * @param {Revision} [revision=Revision.Legacy] - The revision of the TypedData.
13175
13214
  *
13176
13215
  * @returns {string} The hash.
13216
+ * @throws Will throw an error if the types declare more than `constants.TYPED_DATA_MAX_TYPES` types, or if a type refers to itself, directly or through other types.
13177
13217
  *
13178
13218
  * @example
13179
13219
  * ```typescript
@@ -13195,6 +13235,7 @@ declare function getTypeHash(types: TypedData['types'], type: string, revision?:
13195
13235
  * @param {Revision} [revision=Revision.Legacy] - The revision of the TypedData.
13196
13236
  *
13197
13237
  * @returns {[string, string]} The ABI compatible type and corresponding value.
13238
+ * @throws Will throw an error if the types declare more than `constants.TYPED_DATA_MAX_TYPES` types, if the data needs more than `constants.TYPED_DATA_MAX_HASHED_FELTS` felts to hash, or if a type refers to itself.
13198
13239
  *
13199
13240
  * @example
13200
13241
  * ```typescript
@@ -13218,6 +13259,7 @@ declare function encodeValue(types: TypedData['types'], type: string, data: unkn
13218
13259
  * @param {Revision} [revision=Revision.Legacy] - The revision of the TypedData.
13219
13260
  *
13220
13261
  * @returns {[string[], string[]]} The ABI compatible types and corresponding values.
13262
+ * @throws Will throw an error if the types declare more than `constants.TYPED_DATA_MAX_TYPES` types, if the data needs more than `constants.TYPED_DATA_MAX_HASHED_FELTS` felts to hash, or if a type refers to itself.
13221
13263
  */
13222
13264
  declare function encodeData<T extends TypedData>(types: T['types'], type: string, data: T['message'], revision?: TypedDataRevision): [string[], string[]];
13223
13265
  /**
@@ -13230,6 +13272,7 @@ declare function encodeData<T extends TypedData>(types: T['types'], type: string
13230
13272
  * @param {Revision} [revision=Revision.Legacy] - The revision of the TypedData.
13231
13273
  *
13232
13274
  * @returns {string} The hash of the encoded data.
13275
+ * @throws Will throw an error if the types declare more than `constants.TYPED_DATA_MAX_TYPES` types, if the data needs more than `constants.TYPED_DATA_MAX_HASHED_FELTS` felts to hash, or if a type refers to itself.
13233
13276
  *
13234
13277
  * @example
13235
13278
  * ```typescript
@@ -13253,6 +13296,7 @@ declare function getStructHash<T extends TypedData>(types: T['types'], type: str
13253
13296
  *
13254
13297
  * @returns {string} The hash of the message to sign.
13255
13298
  * @throws Will throw an error if the typedData does not match the JSON schema.
13299
+ * @throws Will throw an error if the typed data declares more than `constants.TYPED_DATA_MAX_TYPES` types, if the message needs more than `constants.TYPED_DATA_MAX_HASHED_FELTS` felts to hash, or if a type refers to itself.
13256
13300
  *
13257
13301
  * @example
13258
13302
  * ```typescript
@@ -13312,6 +13356,7 @@ declare function getMessageHash(typedData: TypedData, accountAddress: BigNumberi
13312
13356
  * @param {BigNumberish} fullPublicKey a number coded on 520 bits (from ec.getFullPublicKey()).
13313
13357
  * @param {BigNumberish} [accountAddress] address of the account that has signed the message. Not needed with a message hash is provided in `message`
13314
13358
  * @returns {boolean} true if the message is verified.
13359
+ * @throws Will throw an error if `message` is a TypedData that declares more than `constants.TYPED_DATA_MAX_TYPES` types, needs more than `constants.TYPED_DATA_MAX_HASHED_FELTS` felts to hash, or has a type which refers to itself.
13315
13360
  * @example
13316
13361
  * ```typescript
13317
13362
  * const myTypedMessage: TypedMessage = .... ;
@@ -286,6 +286,8 @@ var starknet = (() => {
286
286
  StarknetChainId: () => _StarknetChainId,
287
287
  SupportedRpcVersion: () => _SupportedRpcVersion,
288
288
  TEXT_TO_FELT_MAX_LEN: () => TEXT_TO_FELT_MAX_LEN,
289
+ TYPED_DATA_MAX_HASHED_FELTS: () => TYPED_DATA_MAX_HASHED_FELTS,
290
+ TYPED_DATA_MAX_TYPES: () => TYPED_DATA_MAX_TYPES,
289
291
  TransactionHashPrefix: () => _TransactionHashPrefix,
290
292
  UDC: () => UDC,
291
293
  ZERO: () => ZERO
@@ -1251,6 +1253,8 @@ var starknet = (() => {
1251
1253
  var OutsideExecutionCallerAny = "0x414e595f43414c4c4552";
1252
1254
  var SNIP9_V1_INTERFACE_ID = "0x68cfd18b92d1907b8ba3cc324900277f5a3622099431ea85dd8089255e4181";
1253
1255
  var SNIP9_V2_INTERFACE_ID = "0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872";
1256
+ var TYPED_DATA_MAX_TYPES = 64;
1257
+ var TYPED_DATA_MAX_HASHED_FELTS = 13500;
1254
1258
  var HARDENING_BYTE = 128;
1255
1259
  var HARDENING_4BYTES = 2147483648n;
1256
1260
  var _NetworkName = {
@@ -21664,6 +21668,23 @@ ${indent}}` : "}";
21664
21668
  presetTypes: {}
21665
21669
  }
21666
21670
  };
21671
+ var feltsLeft;
21672
+ function spend(felts) {
21673
+ if (feltsLeft === void 0) return;
21674
+ feltsLeft -= felts;
21675
+ if (feltsLeft < 0) {
21676
+ throw new Error(`Cannot encode data: more than ${TYPED_DATA_MAX_HASHED_FELTS} felts to hash`);
21677
+ }
21678
+ }
21679
+ function withBudget(encode) {
21680
+ if (feltsLeft !== void 0) return encode();
21681
+ feltsLeft = TYPED_DATA_MAX_HASHED_FELTS;
21682
+ try {
21683
+ return encode();
21684
+ } finally {
21685
+ feltsLeft = void 0;
21686
+ }
21687
+ }
21667
21688
  function assertRange(data, type, { min, max }) {
21668
21689
  const value = BigInt(data);
21669
21690
  assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
@@ -21704,35 +21725,30 @@ ${indent}}` : "}";
21704
21725
  return type.type === "merkletree";
21705
21726
  }
21706
21727
  function getDependencies(types, type, dependencies = [], contains = "", revision = TypedDataRevision2.LEGACY) {
21707
- let dependencyTypes = [type];
21708
- if (type[type.length - 1] === "*") {
21709
- dependencyTypes = [type.slice(0, -1)];
21710
- } else if (revision === TypedDataRevision2.ACTIVE) {
21711
- if (type === "enum") {
21712
- dependencyTypes = [contains];
21713
- } else if (type.match(/^\(.*\)$/)) {
21714
- dependencyTypes = type.slice(1, -1).split(",").map((depType) => depType[depType.length - 1] === "*" ? depType.slice(0, -1) : depType);
21715
- }
21716
- }
21717
- return dependencyTypes.filter((t) => !dependencies.includes(t) && types[t]).reduce(
21718
- // This comment prevents prettier from rolling everything here into a single line.
21719
- (p, depType) => [
21720
- ...p,
21721
- ...[
21722
- depType,
21723
- ...types[depType].reduce(
21724
- (previous, t) => [
21725
- ...previous,
21726
- ...getDependencies(types, t.type, previous, t.contains, revision).filter(
21727
- (dependency) => !previous.includes(dependency)
21728
- )
21729
- ],
21730
- []
21731
- )
21732
- ].filter((dependency) => !p.includes(dependency))
21733
- ],
21734
- []
21735
- );
21728
+ const pointedTypes = (fieldType, fieldContains = "") => {
21729
+ if (fieldType.endsWith("*")) return [fieldType.slice(0, -1)];
21730
+ if (revision === TypedDataRevision2.ACTIVE && fieldType === "enum") return [fieldContains];
21731
+ if (revision === TypedDataRevision2.ACTIVE && /^\(.*\)$/.test(fieldType)) {
21732
+ return fieldType.slice(1, -1).split(",").map((param) => param.endsWith("*") ? param.slice(0, -1) : param);
21733
+ }
21734
+ return [fieldType];
21735
+ };
21736
+ const found = /* @__PURE__ */ new Set();
21737
+ const inProgress = /* @__PURE__ */ new Set();
21738
+ const visit = (depType) => {
21739
+ if (inProgress.has(depType)) {
21740
+ throw new Error(`Cannot encode type ${depType}: it refers to itself`);
21741
+ }
21742
+ if (!types[depType] || found.has(depType)) return;
21743
+ found.add(depType);
21744
+ inProgress.add(depType);
21745
+ types[depType].forEach(
21746
+ (field) => pointedTypes(field.type, field.contains).forEach(visit)
21747
+ );
21748
+ inProgress.delete(depType);
21749
+ };
21750
+ pointedTypes(type, contains).filter((depType) => !dependencies.includes(depType)).forEach(visit);
21751
+ return Array.from(found);
21736
21752
  }
21737
21753
  function getMerkleTreeType(types, ctx) {
21738
21754
  if (ctx.parent && ctx.key) {
@@ -21750,6 +21766,11 @@ ${indent}}` : "}";
21750
21766
  return "raw";
21751
21767
  }
21752
21768
  function encodeType(types, type, revision = TypedDataRevision2.LEGACY) {
21769
+ if (Object.keys(types).length > TYPED_DATA_MAX_TYPES) {
21770
+ throw new Error(
21771
+ `Cannot encode type ${type}: more than ${TYPED_DATA_MAX_TYPES} types are declared`
21772
+ );
21773
+ }
21753
21774
  const allTypes = revision === TypedDataRevision2.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
21754
21775
  const [primary, ...dependencies] = getDependencies(
21755
21776
  allTypes,
@@ -21760,7 +21781,7 @@ ${indent}}` : "}";
21760
21781
  );
21761
21782
  const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
21762
21783
  const esc = revisionConfiguration[revision].escapeTypeString;
21763
- return newTypes.map((dependency) => {
21784
+ const encoded = newTypes.map((dependency) => {
21764
21785
  const dependencyElements = allTypes[dependency].map((t) => {
21765
21786
  const targetType = t.type === "enum" && revision === TypedDataRevision2.ACTIVE ? t.contains : t.type;
21766
21787
  const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
@@ -21768,139 +21789,148 @@ ${indent}}` : "}";
21768
21789
  });
21769
21790
  return `${esc(dependency)}(${dependencyElements})`;
21770
21791
  }).join("");
21792
+ spend(Math.ceil(encoded.length / TEXT_TO_FELT_MAX_LEN));
21793
+ return encoded;
21771
21794
  }
21772
21795
  function getTypeHash(types, type, revision = TypedDataRevision2.LEGACY) {
21773
21796
  return getSelectorFromName(encodeType(types, type, revision));
21774
21797
  }
21775
21798
  function encodeValue(types, type, data, ctx = {}, revision = TypedDataRevision2.LEGACY) {
21776
- if (types[type]) {
21777
- return [type, getStructHash(types, type, data, revision)];
21778
- }
21779
- if (revisionConfiguration[revision].presetTypes[type]) {
21780
- return [
21781
- type,
21782
- getStructHash(
21783
- revisionConfiguration[revision].presetTypes,
21784
- type,
21785
- data,
21786
- revision
21787
- )
21788
- ];
21789
- }
21790
- if (type.endsWith("*")) {
21791
- const hashes = data.map(
21792
- (entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
21799
+ return withBudget(() => {
21800
+ spend(
21801
+ typeof data === "string" ? Math.max(1, Math.ceil(data.length / TEXT_TO_FELT_MAX_LEN) - 2) : 1
21793
21802
  );
21794
- return [type, revisionConfiguration[revision].hashMethod(hashes)];
21795
- }
21796
- switch (type) {
21797
- case "enum": {
21798
- if (revision === TypedDataRevision2.ACTIVE) {
21799
- const [variantKey, variantData] = Object.entries(data)[0];
21800
- const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
21801
- const enumType = types[parentType.contains];
21802
- const variantType = enumType.find((t) => t.name === variantKey);
21803
- const variantIndex = enumType.indexOf(variantType);
21804
- const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
21805
- if (!subtype) return subtype;
21806
- const subtypeData = variantData[index];
21807
- return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
21808
- });
21809
- return [
21803
+ if (types[type]) {
21804
+ return [type, getStructHash(types, type, data, revision)];
21805
+ }
21806
+ if (revisionConfiguration[revision].presetTypes[type]) {
21807
+ return [
21808
+ type,
21809
+ getStructHash(
21810
+ revisionConfiguration[revision].presetTypes,
21810
21811
  type,
21811
- revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
21812
- ];
21813
- }
21814
- return [type, getHex(data)];
21812
+ data,
21813
+ revision
21814
+ )
21815
+ ];
21815
21816
  }
21816
- case "merkletree": {
21817
- const merkleTreeType = getMerkleTreeType(types, ctx);
21818
- const structHashes = data.map((struct) => {
21819
- return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
21820
- });
21821
- const { root } = new MerkleTree(
21822
- structHashes,
21823
- revisionConfiguration[revision].hashMerkleMethod
21817
+ if (type.endsWith("*")) {
21818
+ const hashes = data.map(
21819
+ (entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
21824
21820
  );
21825
- return ["felt", root];
21826
- }
21827
- case "selector": {
21828
- return ["felt", prepareSelector(data)];
21829
- }
21830
- case "string": {
21831
- if (revision === TypedDataRevision2.ACTIVE) {
21832
- const byteArray = byteArrayFromString(data);
21833
- const elements = [
21834
- byteArray.data.length,
21835
- ...byteArray.data,
21836
- byteArray.pending_word,
21837
- byteArray.pending_word_len
21838
- ];
21839
- return [type, revisionConfiguration[revision].hashMethod(elements)];
21821
+ return [type, revisionConfiguration[revision].hashMethod(hashes)];
21822
+ }
21823
+ switch (type) {
21824
+ case "enum": {
21825
+ if (revision === TypedDataRevision2.ACTIVE) {
21826
+ const [variantKey, variantData] = Object.entries(data)[0];
21827
+ const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
21828
+ const enumType = types[parentType.contains];
21829
+ const variantType = enumType.find((t) => t.name === variantKey);
21830
+ const variantIndex = enumType.indexOf(variantType);
21831
+ const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
21832
+ if (!subtype) return subtype;
21833
+ const subtypeData = variantData[index];
21834
+ return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
21835
+ });
21836
+ return [
21837
+ type,
21838
+ revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
21839
+ ];
21840
+ }
21841
+ return [type, getHex(data)];
21840
21842
  }
21841
- return [type, getHex(data)];
21842
- }
21843
- case "i128": {
21844
- if (revision === TypedDataRevision2.ACTIVE) {
21845
- const value = BigInt(data);
21846
- assertRange(value, type, RANGE_I128);
21847
- return [type, getHex(value < 0n ? PRIME + value : value)];
21843
+ case "merkletree": {
21844
+ const merkleTreeType = getMerkleTreeType(types, ctx);
21845
+ const structHashes = data.map((struct) => {
21846
+ return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
21847
+ });
21848
+ const { root } = new MerkleTree(
21849
+ structHashes,
21850
+ revisionConfiguration[revision].hashMerkleMethod
21851
+ );
21852
+ return ["felt", root];
21848
21853
  }
21849
- return [type, getHex(data)];
21850
- }
21851
- case "timestamp":
21852
- case "u128": {
21853
- if (revision === TypedDataRevision2.ACTIVE) {
21854
- assertRange(data, type, RANGE_U128);
21854
+ case "selector": {
21855
+ return ["felt", prepareSelector(data)];
21855
21856
  }
21856
- return [type, getHex(data)];
21857
- }
21858
- case "felt":
21859
- case "shortstring": {
21860
- if (revision === TypedDataRevision2.ACTIVE) {
21861
- assertRange(getHex(data), type, RANGE_FELT);
21857
+ case "string": {
21858
+ if (revision === TypedDataRevision2.ACTIVE) {
21859
+ const byteArray = byteArrayFromString(data);
21860
+ const elements = [
21861
+ byteArray.data.length,
21862
+ ...byteArray.data,
21863
+ byteArray.pending_word,
21864
+ byteArray.pending_word_len
21865
+ ];
21866
+ return [type, revisionConfiguration[revision].hashMethod(elements)];
21867
+ }
21868
+ return [type, getHex(data)];
21862
21869
  }
21863
- return [type, getHex(data)];
21864
- }
21865
- case "ClassHash":
21866
- case "ContractAddress": {
21867
- if (revision === TypedDataRevision2.ACTIVE) {
21868
- assertRange(data, type, RANGE_FELT);
21870
+ case "i128": {
21871
+ if (revision === TypedDataRevision2.ACTIVE) {
21872
+ const value = BigInt(data);
21873
+ assertRange(value, type, RANGE_I128);
21874
+ return [type, getHex(value < 0n ? PRIME + value : value)];
21875
+ }
21876
+ return [type, getHex(data)];
21869
21877
  }
21870
- return [type, getHex(data)];
21871
- }
21872
- case "bool": {
21873
- if (revision === TypedDataRevision2.ACTIVE) {
21874
- assert(isBoolean(data), `Type mismatch for ${type} ${data}`);
21878
+ case "timestamp":
21879
+ case "u128": {
21880
+ if (revision === TypedDataRevision2.ACTIVE) {
21881
+ assertRange(data, type, RANGE_U128);
21882
+ }
21883
+ return [type, getHex(data)];
21875
21884
  }
21876
- return [type, getHex(data)];
21877
- }
21878
- default: {
21879
- if (revision === TypedDataRevision2.ACTIVE) {
21880
- throw new Error(`Unsupported type: ${type}`);
21885
+ case "felt":
21886
+ case "shortstring": {
21887
+ if (revision === TypedDataRevision2.ACTIVE) {
21888
+ assertRange(getHex(data), type, RANGE_FELT);
21889
+ }
21890
+ return [type, getHex(data)];
21891
+ }
21892
+ case "ClassHash":
21893
+ case "ContractAddress": {
21894
+ if (revision === TypedDataRevision2.ACTIVE) {
21895
+ assertRange(data, type, RANGE_FELT);
21896
+ }
21897
+ return [type, getHex(data)];
21898
+ }
21899
+ case "bool": {
21900
+ if (revision === TypedDataRevision2.ACTIVE) {
21901
+ assert(isBoolean(data), `Type mismatch for ${type} ${data}`);
21902
+ }
21903
+ return [type, getHex(data)];
21904
+ }
21905
+ default: {
21906
+ if (revision === TypedDataRevision2.ACTIVE) {
21907
+ throw new Error(`Unsupported type: ${type}`);
21908
+ }
21909
+ return [type, getHex(data)];
21881
21910
  }
21882
- return [type, getHex(data)];
21883
21911
  }
21884
- }
21912
+ });
21885
21913
  }
21886
21914
  function encodeData(types, type, data, revision = TypedDataRevision2.LEGACY) {
21887
- const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
21888
- const [returnTypes, values] = targetType.reduce(
21889
- ([ts, vs], field) => {
21890
- if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
21891
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
21892
- }
21893
- const value = data[field.name];
21894
- const ctx = { parent: type, key: field.name };
21895
- const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
21896
- return [
21897
- [...ts, t],
21898
- [...vs, encodedValue]
21899
- ];
21900
- },
21901
- [["felt"], [getTypeHash(types, type, revision)]]
21902
- );
21903
- return [returnTypes, values];
21915
+ return withBudget(() => {
21916
+ const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
21917
+ const [returnTypes, values] = targetType.reduce(
21918
+ ([ts, vs], field) => {
21919
+ if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
21920
+ throw new Error(`Cannot encode data: missing data for '${field.name}'`);
21921
+ }
21922
+ const value = data[field.name];
21923
+ const ctx = { parent: type, key: field.name };
21924
+ const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
21925
+ return [
21926
+ [...ts, t],
21927
+ [...vs, encodedValue]
21928
+ ];
21929
+ },
21930
+ [["felt"], [getTypeHash(types, type, revision)]]
21931
+ );
21932
+ return [returnTypes, values];
21933
+ });
21904
21934
  }
21905
21935
  function getStructHash(types, type, data, revision = TypedDataRevision2.LEGACY) {
21906
21936
  return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
@@ -21911,13 +21941,15 @@ ${indent}}` : "}";
21911
21941
  }
21912
21942
  const revision = identifyRevision(typedData);
21913
21943
  const { domain, hashMethod } = revisionConfiguration[revision];
21914
- const message = [
21915
- utf8ToBigInt("StarkNet Message"),
21916
- getStructHash(typedData.types, domain, typedData.domain, revision),
21917
- accountAddress,
21918
- getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
21919
- ];
21920
- return hashMethod(message);
21944
+ return withBudget(() => {
21945
+ const message = [
21946
+ utf8ToBigInt("StarkNet Message"),
21947
+ getStructHash(typedData.types, domain, typedData.domain, revision),
21948
+ accountAddress,
21949
+ getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
21950
+ ];
21951
+ return hashMethod(message);
21952
+ });
21921
21953
  }
21922
21954
  function verifyMessage(message, signature, fullPublicKey, accountAddress) {
21923
21955
  const isTypedData = validateTypedData(message);