starknet 11.4.3 → 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,18 @@
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
+
10
+ # [11.5.0](https://github.com/starknet-io/starknet.js/compare/v11.4.3...v11.5.0) (2026-11-02)
11
+
12
+ ### Features
13
+
14
+ - **provider:** add Starknet Foundation public nodes to default RPC nodes ([bafe60f](https://github.com/starknet-io/starknet.js/commit/bafe60f6c3f4c84883cca4147e65ad229ff0a704))
15
+
1
16
  ## [11.4.3](https://github.com/starknet-io/starknet.js/compare/v11.4.2...v11.4.3) (2026-10-27)
2
17
 
3
18
  ### Bug Fixes
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: {
@@ -8376,8 +8408,8 @@ declare const DEFAULT_GLOBAL_CONFIG: {
8376
8408
  blake: ((uint8Array: Uint8Array) => Uint8Array) | undefined;
8377
8409
  };
8378
8410
  declare const RPC_DEFAULT_NODES: {
8379
- readonly SN_MAIN: readonly ["https://api.zan.top/public/starknet-mainnet/rpc/"];
8380
- readonly SN_SEPOLIA: readonly ["https://api.zan.top/public/starknet-sepolia/rpc/"];
8411
+ readonly SN_MAIN: readonly ["https://api.zan.top/public/starknet-mainnet/rpc/", "https://mainnet.nodes.starknet.org/rpc/"];
8412
+ readonly SN_SEPOLIA: readonly ["https://api.zan.top/public/starknet-sepolia/rpc/", "https://sepolia.nodes.starknet.org/rpc/"];
8381
8413
  };
8382
8414
  declare const PAYMASTER_RPC_NODES: {
8383
8415
  readonly SN_MAIN: readonly ["https://starknet.paymaster.avnu.fi"];
@@ -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 = {
@@ -1328,8 +1332,14 @@ var starknet = (() => {
1328
1332
  blake: void 0
1329
1333
  };
1330
1334
  var RPC_DEFAULT_NODES = {
1331
- SN_MAIN: [`https://api.zan.top/public/starknet-mainnet/rpc/`],
1332
- SN_SEPOLIA: [`https://api.zan.top/public/starknet-sepolia/rpc/`]
1335
+ SN_MAIN: [
1336
+ `https://api.zan.top/public/starknet-mainnet/rpc/`,
1337
+ `https://mainnet.nodes.starknet.org/rpc/`
1338
+ ],
1339
+ SN_SEPOLIA: [
1340
+ `https://api.zan.top/public/starknet-sepolia/rpc/`,
1341
+ `https://sepolia.nodes.starknet.org/rpc/`
1342
+ ]
1333
1343
  };
1334
1344
  var PAYMASTER_RPC_NODES = {
1335
1345
  SN_MAIN: [`https://starknet.paymaster.avnu.fi`],
@@ -21658,6 +21668,23 @@ ${indent}}` : "}";
21658
21668
  presetTypes: {}
21659
21669
  }
21660
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
+ }
21661
21688
  function assertRange(data, type, { min, max }) {
21662
21689
  const value = BigInt(data);
21663
21690
  assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
@@ -21698,35 +21725,30 @@ ${indent}}` : "}";
21698
21725
  return type.type === "merkletree";
21699
21726
  }
21700
21727
  function getDependencies(types, type, dependencies = [], contains = "", revision = TypedDataRevision2.LEGACY) {
21701
- let dependencyTypes = [type];
21702
- if (type[type.length - 1] === "*") {
21703
- dependencyTypes = [type.slice(0, -1)];
21704
- } else if (revision === TypedDataRevision2.ACTIVE) {
21705
- if (type === "enum") {
21706
- dependencyTypes = [contains];
21707
- } else if (type.match(/^\(.*\)$/)) {
21708
- dependencyTypes = type.slice(1, -1).split(",").map((depType) => depType[depType.length - 1] === "*" ? depType.slice(0, -1) : depType);
21709
- }
21710
- }
21711
- return dependencyTypes.filter((t) => !dependencies.includes(t) && types[t]).reduce(
21712
- // This comment prevents prettier from rolling everything here into a single line.
21713
- (p, depType) => [
21714
- ...p,
21715
- ...[
21716
- depType,
21717
- ...types[depType].reduce(
21718
- (previous, t) => [
21719
- ...previous,
21720
- ...getDependencies(types, t.type, previous, t.contains, revision).filter(
21721
- (dependency) => !previous.includes(dependency)
21722
- )
21723
- ],
21724
- []
21725
- )
21726
- ].filter((dependency) => !p.includes(dependency))
21727
- ],
21728
- []
21729
- );
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);
21730
21752
  }
21731
21753
  function getMerkleTreeType(types, ctx) {
21732
21754
  if (ctx.parent && ctx.key) {
@@ -21744,6 +21766,11 @@ ${indent}}` : "}";
21744
21766
  return "raw";
21745
21767
  }
21746
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
+ }
21747
21774
  const allTypes = revision === TypedDataRevision2.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
21748
21775
  const [primary, ...dependencies] = getDependencies(
21749
21776
  allTypes,
@@ -21754,7 +21781,7 @@ ${indent}}` : "}";
21754
21781
  );
21755
21782
  const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
21756
21783
  const esc = revisionConfiguration[revision].escapeTypeString;
21757
- return newTypes.map((dependency) => {
21784
+ const encoded = newTypes.map((dependency) => {
21758
21785
  const dependencyElements = allTypes[dependency].map((t) => {
21759
21786
  const targetType = t.type === "enum" && revision === TypedDataRevision2.ACTIVE ? t.contains : t.type;
21760
21787
  const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
@@ -21762,139 +21789,148 @@ ${indent}}` : "}";
21762
21789
  });
21763
21790
  return `${esc(dependency)}(${dependencyElements})`;
21764
21791
  }).join("");
21792
+ spend(Math.ceil(encoded.length / TEXT_TO_FELT_MAX_LEN));
21793
+ return encoded;
21765
21794
  }
21766
21795
  function getTypeHash(types, type, revision = TypedDataRevision2.LEGACY) {
21767
21796
  return getSelectorFromName(encodeType(types, type, revision));
21768
21797
  }
21769
21798
  function encodeValue(types, type, data, ctx = {}, revision = TypedDataRevision2.LEGACY) {
21770
- if (types[type]) {
21771
- return [type, getStructHash(types, type, data, revision)];
21772
- }
21773
- if (revisionConfiguration[revision].presetTypes[type]) {
21774
- return [
21775
- type,
21776
- getStructHash(
21777
- revisionConfiguration[revision].presetTypes,
21778
- type,
21779
- data,
21780
- revision
21781
- )
21782
- ];
21783
- }
21784
- if (type.endsWith("*")) {
21785
- const hashes = data.map(
21786
- (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
21787
21802
  );
21788
- return [type, revisionConfiguration[revision].hashMethod(hashes)];
21789
- }
21790
- switch (type) {
21791
- case "enum": {
21792
- if (revision === TypedDataRevision2.ACTIVE) {
21793
- const [variantKey, variantData] = Object.entries(data)[0];
21794
- const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
21795
- const enumType = types[parentType.contains];
21796
- const variantType = enumType.find((t) => t.name === variantKey);
21797
- const variantIndex = enumType.indexOf(variantType);
21798
- const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
21799
- if (!subtype) return subtype;
21800
- const subtypeData = variantData[index];
21801
- return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
21802
- });
21803
- 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,
21804
21811
  type,
21805
- revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
21806
- ];
21807
- }
21808
- return [type, getHex(data)];
21812
+ data,
21813
+ revision
21814
+ )
21815
+ ];
21809
21816
  }
21810
- case "merkletree": {
21811
- const merkleTreeType = getMerkleTreeType(types, ctx);
21812
- const structHashes = data.map((struct) => {
21813
- return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
21814
- });
21815
- const { root } = new MerkleTree(
21816
- structHashes,
21817
- 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]
21818
21820
  );
21819
- return ["felt", root];
21820
- }
21821
- case "selector": {
21822
- return ["felt", prepareSelector(data)];
21823
- }
21824
- case "string": {
21825
- if (revision === TypedDataRevision2.ACTIVE) {
21826
- const byteArray = byteArrayFromString(data);
21827
- const elements = [
21828
- byteArray.data.length,
21829
- ...byteArray.data,
21830
- byteArray.pending_word,
21831
- byteArray.pending_word_len
21832
- ];
21833
- 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)];
21834
21842
  }
21835
- return [type, getHex(data)];
21836
- }
21837
- case "i128": {
21838
- if (revision === TypedDataRevision2.ACTIVE) {
21839
- const value = BigInt(data);
21840
- assertRange(value, type, RANGE_I128);
21841
- 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];
21842
21853
  }
21843
- return [type, getHex(data)];
21844
- }
21845
- case "timestamp":
21846
- case "u128": {
21847
- if (revision === TypedDataRevision2.ACTIVE) {
21848
- assertRange(data, type, RANGE_U128);
21854
+ case "selector": {
21855
+ return ["felt", prepareSelector(data)];
21849
21856
  }
21850
- return [type, getHex(data)];
21851
- }
21852
- case "felt":
21853
- case "shortstring": {
21854
- if (revision === TypedDataRevision2.ACTIVE) {
21855
- 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)];
21856
21869
  }
21857
- return [type, getHex(data)];
21858
- }
21859
- case "ClassHash":
21860
- case "ContractAddress": {
21861
- if (revision === TypedDataRevision2.ACTIVE) {
21862
- 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)];
21863
21877
  }
21864
- return [type, getHex(data)];
21865
- }
21866
- case "bool": {
21867
- if (revision === TypedDataRevision2.ACTIVE) {
21868
- 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)];
21869
21884
  }
21870
- return [type, getHex(data)];
21871
- }
21872
- default: {
21873
- if (revision === TypedDataRevision2.ACTIVE) {
21874
- 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)];
21875
21910
  }
21876
- return [type, getHex(data)];
21877
21911
  }
21878
- }
21912
+ });
21879
21913
  }
21880
21914
  function encodeData(types, type, data, revision = TypedDataRevision2.LEGACY) {
21881
- const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
21882
- const [returnTypes, values] = targetType.reduce(
21883
- ([ts, vs], field) => {
21884
- if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
21885
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
21886
- }
21887
- const value = data[field.name];
21888
- const ctx = { parent: type, key: field.name };
21889
- const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
21890
- return [
21891
- [...ts, t],
21892
- [...vs, encodedValue]
21893
- ];
21894
- },
21895
- [["felt"], [getTypeHash(types, type, revision)]]
21896
- );
21897
- 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
+ });
21898
21934
  }
21899
21935
  function getStructHash(types, type, data, revision = TypedDataRevision2.LEGACY) {
21900
21936
  return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
@@ -21905,13 +21941,15 @@ ${indent}}` : "}";
21905
21941
  }
21906
21942
  const revision = identifyRevision(typedData);
21907
21943
  const { domain, hashMethod } = revisionConfiguration[revision];
21908
- const message = [
21909
- utf8ToBigInt("StarkNet Message"),
21910
- getStructHash(typedData.types, domain, typedData.domain, revision),
21911
- accountAddress,
21912
- getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
21913
- ];
21914
- 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
+ });
21915
21953
  }
21916
21954
  function verifyMessage(message, signature, fullPublicKey, accountAddress) {
21917
21955
  const isTypedData = validateTypedData(message);