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/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 = {
@@ -578,8 +582,14 @@ var DEFAULT_GLOBAL_CONFIG = {
578
582
  blake: void 0
579
583
  };
580
584
  var RPC_DEFAULT_NODES = {
581
- SN_MAIN: [`https://api.zan.top/public/starknet-mainnet/rpc/`],
582
- SN_SEPOLIA: [`https://api.zan.top/public/starknet-sepolia/rpc/`]
585
+ SN_MAIN: [
586
+ `https://api.zan.top/public/starknet-mainnet/rpc/`,
587
+ `https://mainnet.nodes.starknet.org/rpc/`
588
+ ],
589
+ SN_SEPOLIA: [
590
+ `https://api.zan.top/public/starknet-sepolia/rpc/`,
591
+ `https://sepolia.nodes.starknet.org/rpc/`
592
+ ]
583
593
  };
584
594
  var PAYMASTER_RPC_NODES = {
585
595
  SN_MAIN: [`https://starknet.paymaster.avnu.fi`],
@@ -16105,6 +16115,23 @@ var revisionConfiguration = {
16105
16115
  presetTypes: {}
16106
16116
  }
16107
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
+ }
16108
16135
  function assertRange(data, type, { min, max }) {
16109
16136
  const value = BigInt(data);
16110
16137
  assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
@@ -16145,35 +16172,30 @@ function isMerkleTreeType(type) {
16145
16172
  return type.type === "merkletree";
16146
16173
  }
16147
16174
  function getDependencies(types, type, dependencies = [], contains = "", revision = api_exports.TypedDataRevision.LEGACY) {
16148
- let dependencyTypes = [type];
16149
- if (type[type.length - 1] === "*") {
16150
- dependencyTypes = [type.slice(0, -1)];
16151
- } else if (revision === api_exports.TypedDataRevision.ACTIVE) {
16152
- if (type === "enum") {
16153
- dependencyTypes = [contains];
16154
- } else if (type.match(/^\(.*\)$/)) {
16155
- dependencyTypes = type.slice(1, -1).split(",").map((depType) => depType[depType.length - 1] === "*" ? depType.slice(0, -1) : depType);
16156
- }
16157
- }
16158
- return dependencyTypes.filter((t) => !dependencies.includes(t) && types[t]).reduce(
16159
- // This comment prevents prettier from rolling everything here into a single line.
16160
- (p, depType) => [
16161
- ...p,
16162
- ...[
16163
- depType,
16164
- ...types[depType].reduce(
16165
- (previous, t) => [
16166
- ...previous,
16167
- ...getDependencies(types, t.type, previous, t.contains, revision).filter(
16168
- (dependency) => !previous.includes(dependency)
16169
- )
16170
- ],
16171
- []
16172
- )
16173
- ].filter((dependency) => !p.includes(dependency))
16174
- ],
16175
- []
16176
- );
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);
16177
16199
  }
16178
16200
  function getMerkleTreeType(types, ctx) {
16179
16201
  if (ctx.parent && ctx.key) {
@@ -16191,6 +16213,11 @@ function getMerkleTreeType(types, ctx) {
16191
16213
  return "raw";
16192
16214
  }
16193
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
+ }
16194
16221
  const allTypes = revision === api_exports.TypedDataRevision.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
16195
16222
  const [primary, ...dependencies] = getDependencies(
16196
16223
  allTypes,
@@ -16201,7 +16228,7 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
16201
16228
  );
16202
16229
  const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
16203
16230
  const esc = revisionConfiguration[revision].escapeTypeString;
16204
- return newTypes.map((dependency) => {
16231
+ const encoded = newTypes.map((dependency) => {
16205
16232
  const dependencyElements = allTypes[dependency].map((t) => {
16206
16233
  const targetType = t.type === "enum" && revision === api_exports.TypedDataRevision.ACTIVE ? t.contains : t.type;
16207
16234
  const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
@@ -16209,139 +16236,148 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
16209
16236
  });
16210
16237
  return `${esc(dependency)}(${dependencyElements})`;
16211
16238
  }).join("");
16239
+ spend(Math.ceil(encoded.length / TEXT_TO_FELT_MAX_LEN));
16240
+ return encoded;
16212
16241
  }
16213
16242
  function getTypeHash(types, type, revision = api_exports.TypedDataRevision.LEGACY) {
16214
16243
  return getSelectorFromName(encodeType(types, type, revision));
16215
16244
  }
16216
16245
  function encodeValue(types, type, data, ctx = {}, revision = api_exports.TypedDataRevision.LEGACY) {
16217
- if (types[type]) {
16218
- return [type, getStructHash(types, type, data, revision)];
16219
- }
16220
- if (revisionConfiguration[revision].presetTypes[type]) {
16221
- return [
16222
- type,
16223
- getStructHash(
16224
- revisionConfiguration[revision].presetTypes,
16225
- type,
16226
- data,
16227
- revision
16228
- )
16229
- ];
16230
- }
16231
- if (type.endsWith("*")) {
16232
- const hashes = data.map(
16233
- (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
16234
16249
  );
16235
- return [type, revisionConfiguration[revision].hashMethod(hashes)];
16236
- }
16237
- switch (type) {
16238
- case "enum": {
16239
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16240
- const [variantKey, variantData] = Object.entries(data)[0];
16241
- const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
16242
- const enumType = types[parentType.contains];
16243
- const variantType = enumType.find((t) => t.name === variantKey);
16244
- const variantIndex = enumType.indexOf(variantType);
16245
- const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
16246
- if (!subtype) return subtype;
16247
- const subtypeData = variantData[index];
16248
- return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
16249
- });
16250
- 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,
16251
16258
  type,
16252
- revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
16253
- ];
16254
- }
16255
- return [type, getHex(data)];
16259
+ data,
16260
+ revision
16261
+ )
16262
+ ];
16256
16263
  }
16257
- case "merkletree": {
16258
- const merkleTreeType = getMerkleTreeType(types, ctx);
16259
- const structHashes = data.map((struct) => {
16260
- return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
16261
- });
16262
- const { root } = new MerkleTree(
16263
- structHashes,
16264
- 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]
16265
16267
  );
16266
- return ["felt", root];
16267
- }
16268
- case "selector": {
16269
- return ["felt", prepareSelector(data)];
16270
- }
16271
- case "string": {
16272
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16273
- const byteArray = byteArrayFromString(data);
16274
- const elements = [
16275
- byteArray.data.length,
16276
- ...byteArray.data,
16277
- byteArray.pending_word,
16278
- byteArray.pending_word_len
16279
- ];
16280
- 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)];
16281
16289
  }
16282
- return [type, getHex(data)];
16283
- }
16284
- case "i128": {
16285
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16286
- const value = BigInt(data);
16287
- assertRange(value, type, RANGE_I128);
16288
- 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];
16289
16300
  }
16290
- return [type, getHex(data)];
16291
- }
16292
- case "timestamp":
16293
- case "u128": {
16294
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16295
- assertRange(data, type, RANGE_U128);
16301
+ case "selector": {
16302
+ return ["felt", prepareSelector(data)];
16296
16303
  }
16297
- return [type, getHex(data)];
16298
- }
16299
- case "felt":
16300
- case "shortstring": {
16301
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16302
- 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)];
16303
16316
  }
16304
- return [type, getHex(data)];
16305
- }
16306
- case "ClassHash":
16307
- case "ContractAddress": {
16308
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16309
- 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)];
16310
16324
  }
16311
- return [type, getHex(data)];
16312
- }
16313
- case "bool": {
16314
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16315
- 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)];
16316
16331
  }
16317
- return [type, getHex(data)];
16318
- }
16319
- default: {
16320
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16321
- 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)];
16322
16357
  }
16323
- return [type, getHex(data)];
16324
16358
  }
16325
- }
16359
+ });
16326
16360
  }
16327
16361
  function encodeData(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16328
- const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
16329
- const [returnTypes, values] = targetType.reduce(
16330
- ([ts, vs], field) => {
16331
- if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
16332
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
16333
- }
16334
- const value = data[field.name];
16335
- const ctx = { parent: type, key: field.name };
16336
- const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
16337
- return [
16338
- [...ts, t],
16339
- [...vs, encodedValue]
16340
- ];
16341
- },
16342
- [["felt"], [getTypeHash(types, type, revision)]]
16343
- );
16344
- 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
+ });
16345
16381
  }
16346
16382
  function getStructHash(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16347
16383
  return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
@@ -16352,13 +16388,15 @@ function getMessageHash(typedData, accountAddress) {
16352
16388
  }
16353
16389
  const revision = identifyRevision(typedData);
16354
16390
  const { domain, hashMethod } = revisionConfiguration[revision];
16355
- const message = [
16356
- utf8ToBigInt("StarkNet Message"),
16357
- getStructHash(typedData.types, domain, typedData.domain, revision),
16358
- accountAddress,
16359
- getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
16360
- ];
16361
- 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
+ });
16362
16400
  }
16363
16401
  function verifyMessage(message, signature, fullPublicKey, accountAddress) {
16364
16402
  const isTypedData = validateTypedData(message);