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/dist/index.js CHANGED
@@ -272,6 +272,8 @@ __export(constants_exports, {
272
272
  StarknetChainId: () => _StarknetChainId,
273
273
  SupportedRpcVersion: () => _SupportedRpcVersion,
274
274
  TEXT_TO_FELT_MAX_LEN: () => TEXT_TO_FELT_MAX_LEN,
275
+ TYPED_DATA_MAX_HASHED_FELTS: () => TYPED_DATA_MAX_HASHED_FELTS,
276
+ TYPED_DATA_MAX_TYPES: () => TYPED_DATA_MAX_TYPES,
275
277
  TransactionHashPrefix: () => _TransactionHashPrefix,
276
278
  UDC: () => UDC,
277
279
  ZERO: () => ZERO
@@ -501,6 +503,8 @@ var UDC = {
501
503
  var OutsideExecutionCallerAny = "0x414e595f43414c4c4552";
502
504
  var SNIP9_V1_INTERFACE_ID = "0x68cfd18b92d1907b8ba3cc324900277f5a3622099431ea85dd8089255e4181";
503
505
  var SNIP9_V2_INTERFACE_ID = "0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872";
506
+ var TYPED_DATA_MAX_TYPES = 64;
507
+ var TYPED_DATA_MAX_HASHED_FELTS = 13500;
504
508
  var HARDENING_BYTE = 128;
505
509
  var HARDENING_4BYTES = 2147483648n;
506
510
  var _NetworkName = {
@@ -16111,6 +16115,23 @@ var revisionConfiguration = {
16111
16115
  presetTypes: {}
16112
16116
  }
16113
16117
  };
16118
+ var feltsLeft;
16119
+ function spend(felts) {
16120
+ if (feltsLeft === void 0) return;
16121
+ feltsLeft -= felts;
16122
+ if (feltsLeft < 0) {
16123
+ throw new Error(`Cannot encode data: more than ${TYPED_DATA_MAX_HASHED_FELTS} felts to hash`);
16124
+ }
16125
+ }
16126
+ function withBudget(encode) {
16127
+ if (feltsLeft !== void 0) return encode();
16128
+ feltsLeft = TYPED_DATA_MAX_HASHED_FELTS;
16129
+ try {
16130
+ return encode();
16131
+ } finally {
16132
+ feltsLeft = void 0;
16133
+ }
16134
+ }
16114
16135
  function assertRange(data, type, { min, max }) {
16115
16136
  const value = BigInt(data);
16116
16137
  assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
@@ -16151,35 +16172,30 @@ function isMerkleTreeType(type) {
16151
16172
  return type.type === "merkletree";
16152
16173
  }
16153
16174
  function getDependencies(types, type, dependencies = [], contains = "", revision = api_exports.TypedDataRevision.LEGACY) {
16154
- let dependencyTypes = [type];
16155
- if (type[type.length - 1] === "*") {
16156
- dependencyTypes = [type.slice(0, -1)];
16157
- } else if (revision === api_exports.TypedDataRevision.ACTIVE) {
16158
- if (type === "enum") {
16159
- dependencyTypes = [contains];
16160
- } else if (type.match(/^\(.*\)$/)) {
16161
- dependencyTypes = type.slice(1, -1).split(",").map((depType) => depType[depType.length - 1] === "*" ? depType.slice(0, -1) : depType);
16162
- }
16163
- }
16164
- return dependencyTypes.filter((t) => !dependencies.includes(t) && types[t]).reduce(
16165
- // This comment prevents prettier from rolling everything here into a single line.
16166
- (p, depType) => [
16167
- ...p,
16168
- ...[
16169
- depType,
16170
- ...types[depType].reduce(
16171
- (previous, t) => [
16172
- ...previous,
16173
- ...getDependencies(types, t.type, previous, t.contains, revision).filter(
16174
- (dependency) => !previous.includes(dependency)
16175
- )
16176
- ],
16177
- []
16178
- )
16179
- ].filter((dependency) => !p.includes(dependency))
16180
- ],
16181
- []
16182
- );
16175
+ const pointedTypes = (fieldType, fieldContains = "") => {
16176
+ if (fieldType.endsWith("*")) return [fieldType.slice(0, -1)];
16177
+ if (revision === api_exports.TypedDataRevision.ACTIVE && fieldType === "enum") return [fieldContains];
16178
+ if (revision === api_exports.TypedDataRevision.ACTIVE && /^\(.*\)$/.test(fieldType)) {
16179
+ return fieldType.slice(1, -1).split(",").map((param) => param.endsWith("*") ? param.slice(0, -1) : param);
16180
+ }
16181
+ return [fieldType];
16182
+ };
16183
+ const found = /* @__PURE__ */ new Set();
16184
+ const inProgress = /* @__PURE__ */ new Set();
16185
+ const visit = (depType) => {
16186
+ if (inProgress.has(depType)) {
16187
+ throw new Error(`Cannot encode type ${depType}: it refers to itself`);
16188
+ }
16189
+ if (!types[depType] || found.has(depType)) return;
16190
+ found.add(depType);
16191
+ inProgress.add(depType);
16192
+ types[depType].forEach(
16193
+ (field) => pointedTypes(field.type, field.contains).forEach(visit)
16194
+ );
16195
+ inProgress.delete(depType);
16196
+ };
16197
+ pointedTypes(type, contains).filter((depType) => !dependencies.includes(depType)).forEach(visit);
16198
+ return Array.from(found);
16183
16199
  }
16184
16200
  function getMerkleTreeType(types, ctx) {
16185
16201
  if (ctx.parent && ctx.key) {
@@ -16197,6 +16213,11 @@ function getMerkleTreeType(types, ctx) {
16197
16213
  return "raw";
16198
16214
  }
16199
16215
  function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY) {
16216
+ if (Object.keys(types).length > TYPED_DATA_MAX_TYPES) {
16217
+ throw new Error(
16218
+ `Cannot encode type ${type}: more than ${TYPED_DATA_MAX_TYPES} types are declared`
16219
+ );
16220
+ }
16200
16221
  const allTypes = revision === api_exports.TypedDataRevision.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
16201
16222
  const [primary, ...dependencies] = getDependencies(
16202
16223
  allTypes,
@@ -16207,7 +16228,7 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
16207
16228
  );
16208
16229
  const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
16209
16230
  const esc = revisionConfiguration[revision].escapeTypeString;
16210
- return newTypes.map((dependency) => {
16231
+ const encoded = newTypes.map((dependency) => {
16211
16232
  const dependencyElements = allTypes[dependency].map((t) => {
16212
16233
  const targetType = t.type === "enum" && revision === api_exports.TypedDataRevision.ACTIVE ? t.contains : t.type;
16213
16234
  const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
@@ -16215,139 +16236,148 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
16215
16236
  });
16216
16237
  return `${esc(dependency)}(${dependencyElements})`;
16217
16238
  }).join("");
16239
+ spend(Math.ceil(encoded.length / TEXT_TO_FELT_MAX_LEN));
16240
+ return encoded;
16218
16241
  }
16219
16242
  function getTypeHash(types, type, revision = api_exports.TypedDataRevision.LEGACY) {
16220
16243
  return getSelectorFromName(encodeType(types, type, revision));
16221
16244
  }
16222
16245
  function encodeValue(types, type, data, ctx = {}, revision = api_exports.TypedDataRevision.LEGACY) {
16223
- if (types[type]) {
16224
- return [type, getStructHash(types, type, data, revision)];
16225
- }
16226
- if (revisionConfiguration[revision].presetTypes[type]) {
16227
- return [
16228
- type,
16229
- getStructHash(
16230
- revisionConfiguration[revision].presetTypes,
16231
- type,
16232
- data,
16233
- revision
16234
- )
16235
- ];
16236
- }
16237
- if (type.endsWith("*")) {
16238
- const hashes = data.map(
16239
- (entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
16246
+ return withBudget(() => {
16247
+ spend(
16248
+ typeof data === "string" ? Math.max(1, Math.ceil(data.length / TEXT_TO_FELT_MAX_LEN) - 2) : 1
16240
16249
  );
16241
- return [type, revisionConfiguration[revision].hashMethod(hashes)];
16242
- }
16243
- switch (type) {
16244
- case "enum": {
16245
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16246
- const [variantKey, variantData] = Object.entries(data)[0];
16247
- const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
16248
- const enumType = types[parentType.contains];
16249
- const variantType = enumType.find((t) => t.name === variantKey);
16250
- const variantIndex = enumType.indexOf(variantType);
16251
- const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
16252
- if (!subtype) return subtype;
16253
- const subtypeData = variantData[index];
16254
- return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
16255
- });
16256
- return [
16250
+ if (types[type]) {
16251
+ return [type, getStructHash(types, type, data, revision)];
16252
+ }
16253
+ if (revisionConfiguration[revision].presetTypes[type]) {
16254
+ return [
16255
+ type,
16256
+ getStructHash(
16257
+ revisionConfiguration[revision].presetTypes,
16257
16258
  type,
16258
- revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
16259
- ];
16260
- }
16261
- return [type, getHex(data)];
16259
+ data,
16260
+ revision
16261
+ )
16262
+ ];
16262
16263
  }
16263
- case "merkletree": {
16264
- const merkleTreeType = getMerkleTreeType(types, ctx);
16265
- const structHashes = data.map((struct) => {
16266
- return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
16267
- });
16268
- const { root } = new MerkleTree(
16269
- structHashes,
16270
- revisionConfiguration[revision].hashMerkleMethod
16264
+ if (type.endsWith("*")) {
16265
+ const hashes = data.map(
16266
+ (entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
16271
16267
  );
16272
- return ["felt", root];
16273
- }
16274
- case "selector": {
16275
- return ["felt", prepareSelector(data)];
16276
- }
16277
- case "string": {
16278
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16279
- const byteArray = byteArrayFromString(data);
16280
- const elements = [
16281
- byteArray.data.length,
16282
- ...byteArray.data,
16283
- byteArray.pending_word,
16284
- byteArray.pending_word_len
16285
- ];
16286
- return [type, revisionConfiguration[revision].hashMethod(elements)];
16268
+ return [type, revisionConfiguration[revision].hashMethod(hashes)];
16269
+ }
16270
+ switch (type) {
16271
+ case "enum": {
16272
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16273
+ const [variantKey, variantData] = Object.entries(data)[0];
16274
+ const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
16275
+ const enumType = types[parentType.contains];
16276
+ const variantType = enumType.find((t) => t.name === variantKey);
16277
+ const variantIndex = enumType.indexOf(variantType);
16278
+ const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
16279
+ if (!subtype) return subtype;
16280
+ const subtypeData = variantData[index];
16281
+ return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
16282
+ });
16283
+ return [
16284
+ type,
16285
+ revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
16286
+ ];
16287
+ }
16288
+ return [type, getHex(data)];
16287
16289
  }
16288
- return [type, getHex(data)];
16289
- }
16290
- case "i128": {
16291
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16292
- const value = BigInt(data);
16293
- assertRange(value, type, RANGE_I128);
16294
- return [type, getHex(value < 0n ? PRIME + value : value)];
16290
+ case "merkletree": {
16291
+ const merkleTreeType = getMerkleTreeType(types, ctx);
16292
+ const structHashes = data.map((struct) => {
16293
+ return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
16294
+ });
16295
+ const { root } = new MerkleTree(
16296
+ structHashes,
16297
+ revisionConfiguration[revision].hashMerkleMethod
16298
+ );
16299
+ return ["felt", root];
16295
16300
  }
16296
- return [type, getHex(data)];
16297
- }
16298
- case "timestamp":
16299
- case "u128": {
16300
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16301
- assertRange(data, type, RANGE_U128);
16301
+ case "selector": {
16302
+ return ["felt", prepareSelector(data)];
16302
16303
  }
16303
- return [type, getHex(data)];
16304
- }
16305
- case "felt":
16306
- case "shortstring": {
16307
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16308
- assertRange(getHex(data), type, RANGE_FELT);
16304
+ case "string": {
16305
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16306
+ const byteArray = byteArrayFromString(data);
16307
+ const elements = [
16308
+ byteArray.data.length,
16309
+ ...byteArray.data,
16310
+ byteArray.pending_word,
16311
+ byteArray.pending_word_len
16312
+ ];
16313
+ return [type, revisionConfiguration[revision].hashMethod(elements)];
16314
+ }
16315
+ return [type, getHex(data)];
16309
16316
  }
16310
- return [type, getHex(data)];
16311
- }
16312
- case "ClassHash":
16313
- case "ContractAddress": {
16314
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16315
- assertRange(data, type, RANGE_FELT);
16317
+ case "i128": {
16318
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16319
+ const value = BigInt(data);
16320
+ assertRange(value, type, RANGE_I128);
16321
+ return [type, getHex(value < 0n ? PRIME + value : value)];
16322
+ }
16323
+ return [type, getHex(data)];
16316
16324
  }
16317
- return [type, getHex(data)];
16318
- }
16319
- case "bool": {
16320
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16321
- assert(isBoolean(data), `Type mismatch for ${type} ${data}`);
16325
+ case "timestamp":
16326
+ case "u128": {
16327
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16328
+ assertRange(data, type, RANGE_U128);
16329
+ }
16330
+ return [type, getHex(data)];
16322
16331
  }
16323
- return [type, getHex(data)];
16324
- }
16325
- default: {
16326
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16327
- throw new Error(`Unsupported type: ${type}`);
16332
+ case "felt":
16333
+ case "shortstring": {
16334
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16335
+ assertRange(getHex(data), type, RANGE_FELT);
16336
+ }
16337
+ return [type, getHex(data)];
16338
+ }
16339
+ case "ClassHash":
16340
+ case "ContractAddress": {
16341
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16342
+ assertRange(data, type, RANGE_FELT);
16343
+ }
16344
+ return [type, getHex(data)];
16345
+ }
16346
+ case "bool": {
16347
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16348
+ assert(isBoolean(data), `Type mismatch for ${type} ${data}`);
16349
+ }
16350
+ return [type, getHex(data)];
16351
+ }
16352
+ default: {
16353
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16354
+ throw new Error(`Unsupported type: ${type}`);
16355
+ }
16356
+ return [type, getHex(data)];
16328
16357
  }
16329
- return [type, getHex(data)];
16330
16358
  }
16331
- }
16359
+ });
16332
16360
  }
16333
16361
  function encodeData(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16334
- const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
16335
- const [returnTypes, values] = targetType.reduce(
16336
- ([ts, vs], field) => {
16337
- if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
16338
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
16339
- }
16340
- const value = data[field.name];
16341
- const ctx = { parent: type, key: field.name };
16342
- const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
16343
- return [
16344
- [...ts, t],
16345
- [...vs, encodedValue]
16346
- ];
16347
- },
16348
- [["felt"], [getTypeHash(types, type, revision)]]
16349
- );
16350
- return [returnTypes, values];
16362
+ return withBudget(() => {
16363
+ const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
16364
+ const [returnTypes, values] = targetType.reduce(
16365
+ ([ts, vs], field) => {
16366
+ if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
16367
+ throw new Error(`Cannot encode data: missing data for '${field.name}'`);
16368
+ }
16369
+ const value = data[field.name];
16370
+ const ctx = { parent: type, key: field.name };
16371
+ const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
16372
+ return [
16373
+ [...ts, t],
16374
+ [...vs, encodedValue]
16375
+ ];
16376
+ },
16377
+ [["felt"], [getTypeHash(types, type, revision)]]
16378
+ );
16379
+ return [returnTypes, values];
16380
+ });
16351
16381
  }
16352
16382
  function getStructHash(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16353
16383
  return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
@@ -16358,13 +16388,15 @@ function getMessageHash(typedData, accountAddress) {
16358
16388
  }
16359
16389
  const revision = identifyRevision(typedData);
16360
16390
  const { domain, hashMethod } = revisionConfiguration[revision];
16361
- const message = [
16362
- utf8ToBigInt("StarkNet Message"),
16363
- getStructHash(typedData.types, domain, typedData.domain, revision),
16364
- accountAddress,
16365
- getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
16366
- ];
16367
- return hashMethod(message);
16391
+ return withBudget(() => {
16392
+ const message = [
16393
+ utf8ToBigInt("StarkNet Message"),
16394
+ getStructHash(typedData.types, domain, typedData.domain, revision),
16395
+ accountAddress,
16396
+ getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
16397
+ ];
16398
+ return hashMethod(message);
16399
+ });
16368
16400
  }
16369
16401
  function verifyMessage(message, signature, fullPublicKey, accountAddress) {
16370
16402
  const isTypedData = validateTypedData(message);