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/CHANGELOG.md +9 -0
- package/dist/index.d.ts +47 -2
- package/dist/index.global.js +183 -151
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +183 -151
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +183 -151
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
16155
|
-
|
|
16156
|
-
|
|
16157
|
-
|
|
16158
|
-
|
|
16159
|
-
|
|
16160
|
-
|
|
16161
|
-
|
|
16162
|
-
|
|
16163
|
-
|
|
16164
|
-
|
|
16165
|
-
|
|
16166
|
-
|
|
16167
|
-
|
|
16168
|
-
|
|
16169
|
-
|
|
16170
|
-
|
|
16171
|
-
|
|
16172
|
-
|
|
16173
|
-
|
|
16174
|
-
|
|
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
|
-
|
|
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
|
-
|
|
16224
|
-
|
|
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
|
-
|
|
16242
|
-
|
|
16243
|
-
|
|
16244
|
-
|
|
16245
|
-
|
|
16246
|
-
|
|
16247
|
-
|
|
16248
|
-
|
|
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
|
-
|
|
16259
|
-
|
|
16260
|
-
|
|
16261
|
-
|
|
16259
|
+
data,
|
|
16260
|
+
revision
|
|
16261
|
+
)
|
|
16262
|
+
];
|
|
16262
16263
|
}
|
|
16263
|
-
|
|
16264
|
-
const
|
|
16265
|
-
|
|
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 [
|
|
16273
|
-
}
|
|
16274
|
-
|
|
16275
|
-
|
|
16276
|
-
|
|
16277
|
-
|
|
16278
|
-
|
|
16279
|
-
|
|
16280
|
-
|
|
16281
|
-
|
|
16282
|
-
|
|
16283
|
-
|
|
16284
|
-
|
|
16285
|
-
|
|
16286
|
-
|
|
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
|
-
|
|
16289
|
-
|
|
16290
|
-
|
|
16291
|
-
|
|
16292
|
-
|
|
16293
|
-
|
|
16294
|
-
|
|
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
|
-
|
|
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
|
-
|
|
16304
|
-
|
|
16305
|
-
|
|
16306
|
-
|
|
16307
|
-
|
|
16308
|
-
|
|
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
|
-
|
|
16311
|
-
|
|
16312
|
-
|
|
16313
|
-
|
|
16314
|
-
|
|
16315
|
-
|
|
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
|
-
|
|
16318
|
-
|
|
16319
|
-
|
|
16320
|
-
|
|
16321
|
-
|
|
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
|
-
|
|
16324
|
-
|
|
16325
|
-
|
|
16326
|
-
|
|
16327
|
-
|
|
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
|
-
|
|
16335
|
-
|
|
16336
|
-
|
|
16337
|
-
|
|
16338
|
-
|
|
16339
|
-
|
|
16340
|
-
|
|
16341
|
-
|
|
16342
|
-
|
|
16343
|
-
|
|
16344
|
-
[
|
|
16345
|
-
|
|
16346
|
-
|
|
16347
|
-
|
|
16348
|
-
|
|
16349
|
-
|
|
16350
|
-
|
|
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
|
-
|
|
16362
|
-
|
|
16363
|
-
|
|
16364
|
-
|
|
16365
|
-
|
|
16366
|
-
|
|
16367
|
-
|
|
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);
|