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.mjs CHANGED
@@ -63,6 +63,8 @@ __export(constants_exports, {
63
63
  StarknetChainId: () => _StarknetChainId,
64
64
  SupportedRpcVersion: () => _SupportedRpcVersion,
65
65
  TEXT_TO_FELT_MAX_LEN: () => TEXT_TO_FELT_MAX_LEN,
66
+ TYPED_DATA_MAX_HASHED_FELTS: () => TYPED_DATA_MAX_HASHED_FELTS,
67
+ TYPED_DATA_MAX_TYPES: () => TYPED_DATA_MAX_TYPES,
66
68
  TransactionHashPrefix: () => _TransactionHashPrefix,
67
69
  UDC: () => UDC,
68
70
  ZERO: () => ZERO
@@ -293,6 +295,8 @@ var UDC = {
293
295
  var OutsideExecutionCallerAny = "0x414e595f43414c4c4552";
294
296
  var SNIP9_V1_INTERFACE_ID = "0x68cfd18b92d1907b8ba3cc324900277f5a3622099431ea85dd8089255e4181";
295
297
  var SNIP9_V2_INTERFACE_ID = "0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872";
298
+ var TYPED_DATA_MAX_TYPES = 64;
299
+ var TYPED_DATA_MAX_HASHED_FELTS = 13500;
296
300
  var HARDENING_BYTE = 128;
297
301
  var HARDENING_4BYTES = 2147483648n;
298
302
  var _NetworkName = {
@@ -370,8 +374,14 @@ var DEFAULT_GLOBAL_CONFIG = {
370
374
  blake: void 0
371
375
  };
372
376
  var RPC_DEFAULT_NODES = {
373
- SN_MAIN: [`https://api.zan.top/public/starknet-mainnet/rpc/`],
374
- SN_SEPOLIA: [`https://api.zan.top/public/starknet-sepolia/rpc/`]
377
+ SN_MAIN: [
378
+ `https://api.zan.top/public/starknet-mainnet/rpc/`,
379
+ `https://mainnet.nodes.starknet.org/rpc/`
380
+ ],
381
+ SN_SEPOLIA: [
382
+ `https://api.zan.top/public/starknet-sepolia/rpc/`,
383
+ `https://sepolia.nodes.starknet.org/rpc/`
384
+ ]
375
385
  };
376
386
  var PAYMASTER_RPC_NODES = {
377
387
  SN_MAIN: [`https://starknet.paymaster.avnu.fi`],
@@ -15897,6 +15907,23 @@ var revisionConfiguration = {
15897
15907
  presetTypes: {}
15898
15908
  }
15899
15909
  };
15910
+ var feltsLeft;
15911
+ function spend(felts) {
15912
+ if (feltsLeft === void 0) return;
15913
+ feltsLeft -= felts;
15914
+ if (feltsLeft < 0) {
15915
+ throw new Error(`Cannot encode data: more than ${TYPED_DATA_MAX_HASHED_FELTS} felts to hash`);
15916
+ }
15917
+ }
15918
+ function withBudget(encode) {
15919
+ if (feltsLeft !== void 0) return encode();
15920
+ feltsLeft = TYPED_DATA_MAX_HASHED_FELTS;
15921
+ try {
15922
+ return encode();
15923
+ } finally {
15924
+ feltsLeft = void 0;
15925
+ }
15926
+ }
15900
15927
  function assertRange(data, type, { min, max }) {
15901
15928
  const value = BigInt(data);
15902
15929
  assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
@@ -15937,35 +15964,30 @@ function isMerkleTreeType(type) {
15937
15964
  return type.type === "merkletree";
15938
15965
  }
15939
15966
  function getDependencies(types, type, dependencies = [], contains = "", revision = api_exports.TypedDataRevision.LEGACY) {
15940
- let dependencyTypes = [type];
15941
- if (type[type.length - 1] === "*") {
15942
- dependencyTypes = [type.slice(0, -1)];
15943
- } else if (revision === api_exports.TypedDataRevision.ACTIVE) {
15944
- if (type === "enum") {
15945
- dependencyTypes = [contains];
15946
- } else if (type.match(/^\(.*\)$/)) {
15947
- dependencyTypes = type.slice(1, -1).split(",").map((depType) => depType[depType.length - 1] === "*" ? depType.slice(0, -1) : depType);
15948
- }
15949
- }
15950
- return dependencyTypes.filter((t) => !dependencies.includes(t) && types[t]).reduce(
15951
- // This comment prevents prettier from rolling everything here into a single line.
15952
- (p, depType) => [
15953
- ...p,
15954
- ...[
15955
- depType,
15956
- ...types[depType].reduce(
15957
- (previous, t) => [
15958
- ...previous,
15959
- ...getDependencies(types, t.type, previous, t.contains, revision).filter(
15960
- (dependency) => !previous.includes(dependency)
15961
- )
15962
- ],
15963
- []
15964
- )
15965
- ].filter((dependency) => !p.includes(dependency))
15966
- ],
15967
- []
15968
- );
15967
+ const pointedTypes = (fieldType, fieldContains = "") => {
15968
+ if (fieldType.endsWith("*")) return [fieldType.slice(0, -1)];
15969
+ if (revision === api_exports.TypedDataRevision.ACTIVE && fieldType === "enum") return [fieldContains];
15970
+ if (revision === api_exports.TypedDataRevision.ACTIVE && /^\(.*\)$/.test(fieldType)) {
15971
+ return fieldType.slice(1, -1).split(",").map((param) => param.endsWith("*") ? param.slice(0, -1) : param);
15972
+ }
15973
+ return [fieldType];
15974
+ };
15975
+ const found = /* @__PURE__ */ new Set();
15976
+ const inProgress = /* @__PURE__ */ new Set();
15977
+ const visit = (depType) => {
15978
+ if (inProgress.has(depType)) {
15979
+ throw new Error(`Cannot encode type ${depType}: it refers to itself`);
15980
+ }
15981
+ if (!types[depType] || found.has(depType)) return;
15982
+ found.add(depType);
15983
+ inProgress.add(depType);
15984
+ types[depType].forEach(
15985
+ (field) => pointedTypes(field.type, field.contains).forEach(visit)
15986
+ );
15987
+ inProgress.delete(depType);
15988
+ };
15989
+ pointedTypes(type, contains).filter((depType) => !dependencies.includes(depType)).forEach(visit);
15990
+ return Array.from(found);
15969
15991
  }
15970
15992
  function getMerkleTreeType(types, ctx) {
15971
15993
  if (ctx.parent && ctx.key) {
@@ -15983,6 +16005,11 @@ function getMerkleTreeType(types, ctx) {
15983
16005
  return "raw";
15984
16006
  }
15985
16007
  function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY) {
16008
+ if (Object.keys(types).length > TYPED_DATA_MAX_TYPES) {
16009
+ throw new Error(
16010
+ `Cannot encode type ${type}: more than ${TYPED_DATA_MAX_TYPES} types are declared`
16011
+ );
16012
+ }
15986
16013
  const allTypes = revision === api_exports.TypedDataRevision.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
15987
16014
  const [primary, ...dependencies] = getDependencies(
15988
16015
  allTypes,
@@ -15993,7 +16020,7 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
15993
16020
  );
15994
16021
  const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
15995
16022
  const esc = revisionConfiguration[revision].escapeTypeString;
15996
- return newTypes.map((dependency) => {
16023
+ const encoded = newTypes.map((dependency) => {
15997
16024
  const dependencyElements = allTypes[dependency].map((t) => {
15998
16025
  const targetType = t.type === "enum" && revision === api_exports.TypedDataRevision.ACTIVE ? t.contains : t.type;
15999
16026
  const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
@@ -16001,139 +16028,148 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
16001
16028
  });
16002
16029
  return `${esc(dependency)}(${dependencyElements})`;
16003
16030
  }).join("");
16031
+ spend(Math.ceil(encoded.length / TEXT_TO_FELT_MAX_LEN));
16032
+ return encoded;
16004
16033
  }
16005
16034
  function getTypeHash(types, type, revision = api_exports.TypedDataRevision.LEGACY) {
16006
16035
  return getSelectorFromName(encodeType(types, type, revision));
16007
16036
  }
16008
16037
  function encodeValue(types, type, data, ctx = {}, revision = api_exports.TypedDataRevision.LEGACY) {
16009
- if (types[type]) {
16010
- return [type, getStructHash(types, type, data, revision)];
16011
- }
16012
- if (revisionConfiguration[revision].presetTypes[type]) {
16013
- return [
16014
- type,
16015
- getStructHash(
16016
- revisionConfiguration[revision].presetTypes,
16017
- type,
16018
- data,
16019
- revision
16020
- )
16021
- ];
16022
- }
16023
- if (type.endsWith("*")) {
16024
- const hashes = data.map(
16025
- (entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
16038
+ return withBudget(() => {
16039
+ spend(
16040
+ typeof data === "string" ? Math.max(1, Math.ceil(data.length / TEXT_TO_FELT_MAX_LEN) - 2) : 1
16026
16041
  );
16027
- return [type, revisionConfiguration[revision].hashMethod(hashes)];
16028
- }
16029
- switch (type) {
16030
- case "enum": {
16031
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16032
- const [variantKey, variantData] = Object.entries(data)[0];
16033
- const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
16034
- const enumType = types[parentType.contains];
16035
- const variantType = enumType.find((t) => t.name === variantKey);
16036
- const variantIndex = enumType.indexOf(variantType);
16037
- const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
16038
- if (!subtype) return subtype;
16039
- const subtypeData = variantData[index];
16040
- return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
16041
- });
16042
- return [
16042
+ if (types[type]) {
16043
+ return [type, getStructHash(types, type, data, revision)];
16044
+ }
16045
+ if (revisionConfiguration[revision].presetTypes[type]) {
16046
+ return [
16047
+ type,
16048
+ getStructHash(
16049
+ revisionConfiguration[revision].presetTypes,
16043
16050
  type,
16044
- revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
16045
- ];
16046
- }
16047
- return [type, getHex(data)];
16051
+ data,
16052
+ revision
16053
+ )
16054
+ ];
16048
16055
  }
16049
- case "merkletree": {
16050
- const merkleTreeType = getMerkleTreeType(types, ctx);
16051
- const structHashes = data.map((struct) => {
16052
- return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
16053
- });
16054
- const { root } = new MerkleTree(
16055
- structHashes,
16056
- revisionConfiguration[revision].hashMerkleMethod
16056
+ if (type.endsWith("*")) {
16057
+ const hashes = data.map(
16058
+ (entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
16057
16059
  );
16058
- return ["felt", root];
16059
- }
16060
- case "selector": {
16061
- return ["felt", prepareSelector(data)];
16062
- }
16063
- case "string": {
16064
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16065
- const byteArray = byteArrayFromString(data);
16066
- const elements = [
16067
- byteArray.data.length,
16068
- ...byteArray.data,
16069
- byteArray.pending_word,
16070
- byteArray.pending_word_len
16071
- ];
16072
- return [type, revisionConfiguration[revision].hashMethod(elements)];
16060
+ return [type, revisionConfiguration[revision].hashMethod(hashes)];
16061
+ }
16062
+ switch (type) {
16063
+ case "enum": {
16064
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16065
+ const [variantKey, variantData] = Object.entries(data)[0];
16066
+ const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
16067
+ const enumType = types[parentType.contains];
16068
+ const variantType = enumType.find((t) => t.name === variantKey);
16069
+ const variantIndex = enumType.indexOf(variantType);
16070
+ const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
16071
+ if (!subtype) return subtype;
16072
+ const subtypeData = variantData[index];
16073
+ return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
16074
+ });
16075
+ return [
16076
+ type,
16077
+ revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
16078
+ ];
16079
+ }
16080
+ return [type, getHex(data)];
16073
16081
  }
16074
- return [type, getHex(data)];
16075
- }
16076
- case "i128": {
16077
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16078
- const value = BigInt(data);
16079
- assertRange(value, type, RANGE_I128);
16080
- return [type, getHex(value < 0n ? PRIME + value : value)];
16082
+ case "merkletree": {
16083
+ const merkleTreeType = getMerkleTreeType(types, ctx);
16084
+ const structHashes = data.map((struct) => {
16085
+ return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
16086
+ });
16087
+ const { root } = new MerkleTree(
16088
+ structHashes,
16089
+ revisionConfiguration[revision].hashMerkleMethod
16090
+ );
16091
+ return ["felt", root];
16081
16092
  }
16082
- return [type, getHex(data)];
16083
- }
16084
- case "timestamp":
16085
- case "u128": {
16086
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16087
- assertRange(data, type, RANGE_U128);
16093
+ case "selector": {
16094
+ return ["felt", prepareSelector(data)];
16088
16095
  }
16089
- return [type, getHex(data)];
16090
- }
16091
- case "felt":
16092
- case "shortstring": {
16093
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16094
- assertRange(getHex(data), type, RANGE_FELT);
16096
+ case "string": {
16097
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16098
+ const byteArray = byteArrayFromString(data);
16099
+ const elements = [
16100
+ byteArray.data.length,
16101
+ ...byteArray.data,
16102
+ byteArray.pending_word,
16103
+ byteArray.pending_word_len
16104
+ ];
16105
+ return [type, revisionConfiguration[revision].hashMethod(elements)];
16106
+ }
16107
+ return [type, getHex(data)];
16095
16108
  }
16096
- return [type, getHex(data)];
16097
- }
16098
- case "ClassHash":
16099
- case "ContractAddress": {
16100
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16101
- assertRange(data, type, RANGE_FELT);
16109
+ case "i128": {
16110
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16111
+ const value = BigInt(data);
16112
+ assertRange(value, type, RANGE_I128);
16113
+ return [type, getHex(value < 0n ? PRIME + value : value)];
16114
+ }
16115
+ return [type, getHex(data)];
16102
16116
  }
16103
- return [type, getHex(data)];
16104
- }
16105
- case "bool": {
16106
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16107
- assert(isBoolean(data), `Type mismatch for ${type} ${data}`);
16117
+ case "timestamp":
16118
+ case "u128": {
16119
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16120
+ assertRange(data, type, RANGE_U128);
16121
+ }
16122
+ return [type, getHex(data)];
16108
16123
  }
16109
- return [type, getHex(data)];
16110
- }
16111
- default: {
16112
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16113
- throw new Error(`Unsupported type: ${type}`);
16124
+ case "felt":
16125
+ case "shortstring": {
16126
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16127
+ assertRange(getHex(data), type, RANGE_FELT);
16128
+ }
16129
+ return [type, getHex(data)];
16130
+ }
16131
+ case "ClassHash":
16132
+ case "ContractAddress": {
16133
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16134
+ assertRange(data, type, RANGE_FELT);
16135
+ }
16136
+ return [type, getHex(data)];
16137
+ }
16138
+ case "bool": {
16139
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16140
+ assert(isBoolean(data), `Type mismatch for ${type} ${data}`);
16141
+ }
16142
+ return [type, getHex(data)];
16143
+ }
16144
+ default: {
16145
+ if (revision === api_exports.TypedDataRevision.ACTIVE) {
16146
+ throw new Error(`Unsupported type: ${type}`);
16147
+ }
16148
+ return [type, getHex(data)];
16114
16149
  }
16115
- return [type, getHex(data)];
16116
16150
  }
16117
- }
16151
+ });
16118
16152
  }
16119
16153
  function encodeData(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16120
- const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
16121
- const [returnTypes, values] = targetType.reduce(
16122
- ([ts, vs], field) => {
16123
- if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
16124
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
16125
- }
16126
- const value = data[field.name];
16127
- const ctx = { parent: type, key: field.name };
16128
- const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
16129
- return [
16130
- [...ts, t],
16131
- [...vs, encodedValue]
16132
- ];
16133
- },
16134
- [["felt"], [getTypeHash(types, type, revision)]]
16135
- );
16136
- return [returnTypes, values];
16154
+ return withBudget(() => {
16155
+ const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
16156
+ const [returnTypes, values] = targetType.reduce(
16157
+ ([ts, vs], field) => {
16158
+ if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
16159
+ throw new Error(`Cannot encode data: missing data for '${field.name}'`);
16160
+ }
16161
+ const value = data[field.name];
16162
+ const ctx = { parent: type, key: field.name };
16163
+ const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
16164
+ return [
16165
+ [...ts, t],
16166
+ [...vs, encodedValue]
16167
+ ];
16168
+ },
16169
+ [["felt"], [getTypeHash(types, type, revision)]]
16170
+ );
16171
+ return [returnTypes, values];
16172
+ });
16137
16173
  }
16138
16174
  function getStructHash(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16139
16175
  return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
@@ -16144,13 +16180,15 @@ function getMessageHash(typedData, accountAddress) {
16144
16180
  }
16145
16181
  const revision = identifyRevision(typedData);
16146
16182
  const { domain, hashMethod } = revisionConfiguration[revision];
16147
- const message = [
16148
- utf8ToBigInt("StarkNet Message"),
16149
- getStructHash(typedData.types, domain, typedData.domain, revision),
16150
- accountAddress,
16151
- getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
16152
- ];
16153
- return hashMethod(message);
16183
+ return withBudget(() => {
16184
+ const message = [
16185
+ utf8ToBigInt("StarkNet Message"),
16186
+ getStructHash(typedData.types, domain, typedData.domain, revision),
16187
+ accountAddress,
16188
+ getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
16189
+ ];
16190
+ return hashMethod(message);
16191
+ });
16154
16192
  }
16155
16193
  function verifyMessage(message, signature, fullPublicKey, accountAddress) {
16156
16194
  const isTypedData = validateTypedData(message);