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.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 = {
@@ -15903,6 +15907,23 @@ var revisionConfiguration = {
15903
15907
  presetTypes: {}
15904
15908
  }
15905
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
+ }
15906
15927
  function assertRange(data, type, { min, max }) {
15907
15928
  const value = BigInt(data);
15908
15929
  assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
@@ -15943,35 +15964,30 @@ function isMerkleTreeType(type) {
15943
15964
  return type.type === "merkletree";
15944
15965
  }
15945
15966
  function getDependencies(types, type, dependencies = [], contains = "", revision = api_exports.TypedDataRevision.LEGACY) {
15946
- let dependencyTypes = [type];
15947
- if (type[type.length - 1] === "*") {
15948
- dependencyTypes = [type.slice(0, -1)];
15949
- } else if (revision === api_exports.TypedDataRevision.ACTIVE) {
15950
- if (type === "enum") {
15951
- dependencyTypes = [contains];
15952
- } else if (type.match(/^\(.*\)$/)) {
15953
- dependencyTypes = type.slice(1, -1).split(",").map((depType) => depType[depType.length - 1] === "*" ? depType.slice(0, -1) : depType);
15954
- }
15955
- }
15956
- return dependencyTypes.filter((t) => !dependencies.includes(t) && types[t]).reduce(
15957
- // This comment prevents prettier from rolling everything here into a single line.
15958
- (p, depType) => [
15959
- ...p,
15960
- ...[
15961
- depType,
15962
- ...types[depType].reduce(
15963
- (previous, t) => [
15964
- ...previous,
15965
- ...getDependencies(types, t.type, previous, t.contains, revision).filter(
15966
- (dependency) => !previous.includes(dependency)
15967
- )
15968
- ],
15969
- []
15970
- )
15971
- ].filter((dependency) => !p.includes(dependency))
15972
- ],
15973
- []
15974
- );
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);
15975
15991
  }
15976
15992
  function getMerkleTreeType(types, ctx) {
15977
15993
  if (ctx.parent && ctx.key) {
@@ -15989,6 +16005,11 @@ function getMerkleTreeType(types, ctx) {
15989
16005
  return "raw";
15990
16006
  }
15991
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
+ }
15992
16013
  const allTypes = revision === api_exports.TypedDataRevision.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
15993
16014
  const [primary, ...dependencies] = getDependencies(
15994
16015
  allTypes,
@@ -15999,7 +16020,7 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
15999
16020
  );
16000
16021
  const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
16001
16022
  const esc = revisionConfiguration[revision].escapeTypeString;
16002
- return newTypes.map((dependency) => {
16023
+ const encoded = newTypes.map((dependency) => {
16003
16024
  const dependencyElements = allTypes[dependency].map((t) => {
16004
16025
  const targetType = t.type === "enum" && revision === api_exports.TypedDataRevision.ACTIVE ? t.contains : t.type;
16005
16026
  const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
@@ -16007,139 +16028,148 @@ function encodeType(types, type, revision = api_exports.TypedDataRevision.LEGACY
16007
16028
  });
16008
16029
  return `${esc(dependency)}(${dependencyElements})`;
16009
16030
  }).join("");
16031
+ spend(Math.ceil(encoded.length / TEXT_TO_FELT_MAX_LEN));
16032
+ return encoded;
16010
16033
  }
16011
16034
  function getTypeHash(types, type, revision = api_exports.TypedDataRevision.LEGACY) {
16012
16035
  return getSelectorFromName(encodeType(types, type, revision));
16013
16036
  }
16014
16037
  function encodeValue(types, type, data, ctx = {}, revision = api_exports.TypedDataRevision.LEGACY) {
16015
- if (types[type]) {
16016
- return [type, getStructHash(types, type, data, revision)];
16017
- }
16018
- if (revisionConfiguration[revision].presetTypes[type]) {
16019
- return [
16020
- type,
16021
- getStructHash(
16022
- revisionConfiguration[revision].presetTypes,
16023
- type,
16024
- data,
16025
- revision
16026
- )
16027
- ];
16028
- }
16029
- if (type.endsWith("*")) {
16030
- const hashes = data.map(
16031
- (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
16032
16041
  );
16033
- return [type, revisionConfiguration[revision].hashMethod(hashes)];
16034
- }
16035
- switch (type) {
16036
- case "enum": {
16037
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16038
- const [variantKey, variantData] = Object.entries(data)[0];
16039
- const parentType = types[ctx.parent].find((t) => t.name === ctx.key);
16040
- const enumType = types[parentType.contains];
16041
- const variantType = enumType.find((t) => t.name === variantKey);
16042
- const variantIndex = enumType.indexOf(variantType);
16043
- const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
16044
- if (!subtype) return subtype;
16045
- const subtypeData = variantData[index];
16046
- return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
16047
- });
16048
- 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,
16049
16050
  type,
16050
- revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
16051
- ];
16052
- }
16053
- return [type, getHex(data)];
16051
+ data,
16052
+ revision
16053
+ )
16054
+ ];
16054
16055
  }
16055
- case "merkletree": {
16056
- const merkleTreeType = getMerkleTreeType(types, ctx);
16057
- const structHashes = data.map((struct) => {
16058
- return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
16059
- });
16060
- const { root } = new MerkleTree(
16061
- structHashes,
16062
- 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]
16063
16059
  );
16064
- return ["felt", root];
16065
- }
16066
- case "selector": {
16067
- return ["felt", prepareSelector(data)];
16068
- }
16069
- case "string": {
16070
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16071
- const byteArray = byteArrayFromString(data);
16072
- const elements = [
16073
- byteArray.data.length,
16074
- ...byteArray.data,
16075
- byteArray.pending_word,
16076
- byteArray.pending_word_len
16077
- ];
16078
- 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)];
16079
16081
  }
16080
- return [type, getHex(data)];
16081
- }
16082
- case "i128": {
16083
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16084
- const value = BigInt(data);
16085
- assertRange(value, type, RANGE_I128);
16086
- 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];
16087
16092
  }
16088
- return [type, getHex(data)];
16089
- }
16090
- case "timestamp":
16091
- case "u128": {
16092
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16093
- assertRange(data, type, RANGE_U128);
16093
+ case "selector": {
16094
+ return ["felt", prepareSelector(data)];
16094
16095
  }
16095
- return [type, getHex(data)];
16096
- }
16097
- case "felt":
16098
- case "shortstring": {
16099
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16100
- 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)];
16101
16108
  }
16102
- return [type, getHex(data)];
16103
- }
16104
- case "ClassHash":
16105
- case "ContractAddress": {
16106
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16107
- 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)];
16108
16116
  }
16109
- return [type, getHex(data)];
16110
- }
16111
- case "bool": {
16112
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16113
- 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)];
16114
16123
  }
16115
- return [type, getHex(data)];
16116
- }
16117
- default: {
16118
- if (revision === api_exports.TypedDataRevision.ACTIVE) {
16119
- 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)];
16120
16149
  }
16121
- return [type, getHex(data)];
16122
16150
  }
16123
- }
16151
+ });
16124
16152
  }
16125
16153
  function encodeData(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16126
- const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
16127
- const [returnTypes, values] = targetType.reduce(
16128
- ([ts, vs], field) => {
16129
- if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
16130
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
16131
- }
16132
- const value = data[field.name];
16133
- const ctx = { parent: type, key: field.name };
16134
- const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
16135
- return [
16136
- [...ts, t],
16137
- [...vs, encodedValue]
16138
- ];
16139
- },
16140
- [["felt"], [getTypeHash(types, type, revision)]]
16141
- );
16142
- 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
+ });
16143
16173
  }
16144
16174
  function getStructHash(types, type, data, revision = api_exports.TypedDataRevision.LEGACY) {
16145
16175
  return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
@@ -16150,13 +16180,15 @@ function getMessageHash(typedData, accountAddress) {
16150
16180
  }
16151
16181
  const revision = identifyRevision(typedData);
16152
16182
  const { domain, hashMethod } = revisionConfiguration[revision];
16153
- const message = [
16154
- utf8ToBigInt("StarkNet Message"),
16155
- getStructHash(typedData.types, domain, typedData.domain, revision),
16156
- accountAddress,
16157
- getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
16158
- ];
16159
- 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
+ });
16160
16192
  }
16161
16193
  function verifyMessage(message, signature, fullPublicKey, accountAddress) {
16162
16194
  const isTypedData = validateTypedData(message);