-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUtils.ts
More file actions
840 lines (760 loc) · 22.9 KB
/
Copy pathUtils.ts
File metadata and controls
840 lines (760 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
import {
type Address,
type Client,
type Hash,
type Hex,
type PublicClient,
type TypedData,
type TypedDataDomain,
type TypedDataParameter,
concat,
createPublicClient,
decodeAbiParameters,
decodeFunctionResult,
encodeAbiParameters,
encodeFunctionData,
erc20Abi,
hexToBytes,
isAddress,
isHex,
keccak256,
numberToHex,
pad,
padHex,
parseAbi,
parseAbiParameters,
publicActions,
stringToBytes,
toBytes,
toHex
} from "viem"
import type { Transport } from "viem"
import type { Chain } from "viem/chains"
import {
BICONOMY_TOKEN_PAYMASTER,
MOCK_MULTI_MODULE_ADDRESS,
MODULE_ENABLE_MODE_TYPE_HASH,
NEXUS_DOMAIN_NAME,
NEXUS_DOMAIN_TYPEHASH,
NEXUS_DOMAIN_VERSION
} from "../../account/utils/Constants"
import { MEEVersion, getNexusDomainVersion } from "../../constants"
import type { MEEVersionConfig } from "./getVersion"
import { EIP1271Abi } from "../../constants/abi"
import {
type AnyData,
type ModuleType,
moduleTypeIds
} from "../../modules/utils/Types"
import type { AccountMetadata, EIP712DomainReturn } from "./Types"
import { isVersionOlder, versionIsAtLeast } from "./getVersion"
import type { MeeVersionsWithChainId } from "./getVersion"
/**
* Type guard to check if a value is null or undefined.
*
* @param value - The value to check
* @returns True if the value is null or undefined
*/
export const isNullOrUndefined = (value: AnyData): value is undefined => {
return value === null || value === undefined
}
/**
* Checks if the provided value can be safely converted to a BigInt.
*
* @param value - The value to check for BigInt compatibility.
* @returns True if the value can be converted to BigInt, false otherwise.
*/
export const isBigInt = (value: AnyData) => {
try {
// Attempt to convert the value to BigInt.
BigInt(value)
return true
} catch {
// If conversion fails, value is not a valid BigInt.
return false
}
}
export const toBytes32 = (
value: bigint | boolean | Address | Hex | number
): Hex => {
if (typeof value === "boolean") {
return padHex(toHex(value ? 1n : 0n), { size: 32 })
}
if (typeof value === "bigint") {
return padHex(toHex(value), { size: 32 })
}
if (typeof value === "number") {
return padHex(toHex(BigInt(value)), { size: 32 })
}
if (isAddress(value)) {
return padHex(value, { size: 32 })
}
if (isHex(value)) {
return padHex(value, { size: 32 })
}
throw new Error(
"Invalid value: must be boolean, bigint, address, or hex string"
)
}
/**
* Removes all HTTP/HTTPS URLs from the provided string.
*
* @param value - The string to sanitize.
* @returns The sanitized string with URLs removed.
*/
export const sanitizeUrl = (value: string) => {
// Replace all occurrences of http:// or https:// followed by non-whitespace characters with an empty string.
return value.replace(/https?:\/\/[^\s]+/g, "")
}
/**
* Validates if a string is a valid RPC URL.
*
* @param url - The URL to validate
* @returns True if the URL is a valid RPC endpoint
*/
export const isValidRpcUrl = (url: string): boolean => {
const regex = /^(http:\/\/|wss:\/\/|https:\/\/).*/
return regex.test(url)
}
/**
* Compares two addresses for equality, case-insensitive.
*
* @param a - First address
* @param b - Second address
* @returns True if addresses are equal
*/
export const addressEquals = (a?: string, b?: string): boolean =>
!!a && !!b && a?.toLowerCase() === b.toLowerCase()
export const isNativeToken = (tokenAddress: Address): boolean =>
addressEquals(tokenAddress, "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") ||
addressEquals(tokenAddress, "0x0000000000000000000000000000000000000000")
/**
* Parameters for wrapping a signature according to EIP-6492.
*/
export type SignWith6492Params = {
/** The factory contract address */
factoryAddress: Address
/** The factory initialization calldata */
factoryCalldata: Hex
/** The original signature to wrap */
signature: Hash
}
/**
* The EIP-6492 magic suffix used to identify wrapped signatures.
*/
export const ERC6492_MAGIC_BYTES: Hex =
"0x6492649264926492649264926492649264926492649264926492649264926492"
/**
* Wraps a signature according to EIP-6492 specification.
*
* @param params - Parameters including factory address, calldata, and signature
* @returns The wrapped signature
*/
export const wrapSignatureWith6492 = ({
factoryAddress,
factoryCalldata,
signature
}: SignWith6492Params): Hash => {
// wrap the signature as follows: https://eips.ethereum.org/EIPS/eip-6492
// concat(
// abi.encode(
// (create2Factory, factoryCalldata, originalERC1271Signature),
// (address, bytes, bytes)),
// magicBytes
// )
return concat([
encodeAbiParameters(parseAbiParameters("address, bytes, bytes"), [
factoryAddress,
factoryCalldata,
signature
]),
ERC6492_MAGIC_BYTES
])
}
/**
* Result of unwrapping an EIP-6492 signature.
*/
export type UnwrapSignature6492Result = {
/** Whether the signature was wrapped with EIP-6492 */
isWrapped: boolean
/** The original signature (unwrapped if it was wrapped) */
originalSignature: Hex
/** The factory contract address (only present if wrapped) */
factoryAddress?: Address
/** The factory initialization calldata (only present if wrapped) */
factoryCalldata?: Hex
}
/**
* Unwraps an EIP-6492 signature if it's wrapped, otherwise returns the original signature.
*
* EIP-6492 signatures are used to validate signatures for contracts that haven't been deployed yet.
* They wrap the original signature with factory deployment information.
*
* @param wrappedSignature - The potentially wrapped signature to unwrap
* @returns An object containing the unwrapped signature and deployment information if wrapped
*
* @example
* ```ts
* const result = unwrapSignature6492(signature)
* if (result.isWrapped) {
* console.log('Factory:', result.factoryAddress)
* console.log('Original signature:', result.originalSignature)
* } else {
* console.log('Not wrapped, using signature as-is')
* }
* ```
*/
export const unwrapSignature6492 = (
wrappedSignature: Hex
): UnwrapSignature6492Result => {
// Check if signature ends with EIP-6492 magic bytes
if (!wrappedSignature.endsWith(ERC6492_MAGIC_BYTES.slice(2))) {
return {
isWrapped: false,
originalSignature: wrappedSignature
}
}
// Remove magic bytes (32 bytes = 64 hex chars)
const signatureWithoutMagic = wrappedSignature.slice(0, -64) as Hex
// Decode the ABI-encoded data: (address, bytes, bytes)
const decoded = decodeAbiParameters(
parseAbiParameters("address, bytes, bytes"),
signatureWithoutMagic
)
return {
isWrapped: true,
factoryAddress: decoded[0],
factoryCalldata: decoded[1],
originalSignature: decoded[2]
}
}
/**
* Calculates the percentage of a partial value relative to a total value.
*
* @param partialValue - The partial value
* @param totalValue - The total value
* @returns The percentage as a number
*/
export function percentage(partialValue: number, totalValue: number) {
return (100 * partialValue) / totalValue
}
/**
* Converts a percentage to a factor (e.g., 50% -> 1.5).
*
* @param percentage - The percentage value (1-100)
* @returns The converted factor
* @throws If percentage is outside valid range
*/
export function convertToFactor(percentage: number | undefined): number {
// Check if the input is within the valid range
if (percentage) {
if (percentage < 1 || percentage > 100) {
throw new Error("The percentage value should be between 1 and 100.")
}
// Calculate the factor
const factor = percentage / 100 + 1
return factor
}
return 1
}
/**
* Generates installation data and hash for module installation.
*
* @param accountOwner - The account owner address
* @param modules - Array of modules with their types and configurations
* @param domainName - Optional domain name. Defaults to `NEXUS_DOMAIN_NAME`.
* @param domainVersion - Optional domain version. **Defaults to the legacy
* `NEXUS_DOMAIN_VERSION` ("1.2.0"), which is wrong for any MEE ≥ V2_2_0.**
* Callers should pass `getNexusDomainVersion(meeVersionOrConfig)` whenever
* the MEE version is known so the install-mode signature is verifiable on
* the Nexus 1.3.x line.
* @returns Tuple of [installData, hash]
*/
export function makeInstallDataAndHash(
accountOwner: Address,
modules: { type: ModuleType; config: Hex }[],
domainName = NEXUS_DOMAIN_NAME,
domainVersion = NEXUS_DOMAIN_VERSION
): [string, string] {
const types = modules.map((module) => BigInt(moduleTypeIds[module.type]))
const initDatas = modules.map((module) =>
toHex(concat([toBytes(BigInt(moduleTypeIds[module.type])), module.config]))
)
const multiInstallData = encodeAbiParameters(
[{ type: "uint256[]" }, { type: "bytes[]" }],
[types, initDatas]
)
const structHash = keccak256(
encodeAbiParameters(
[{ type: "bytes32" }, { type: "address" }, { type: "bytes32" }],
[
MODULE_ENABLE_MODE_TYPE_HASH,
MOCK_MULTI_MODULE_ADDRESS,
keccak256(multiInstallData)
]
)
)
const hashToSign = _hashTypedData(
structHash,
domainName,
domainVersion,
accountOwner
)
return [multiInstallData, hashToSign]
}
export function _hashTypedData(
structHash: Hex,
name: string,
version: string,
verifyingContract: Address
): string {
const DOMAIN_SEPARATOR = keccak256(
encodeAbiParameters(
[
{ type: "bytes32" },
{ type: "bytes32" },
{ type: "bytes32" },
{ type: "address" }
],
[
keccak256(stringToBytes(NEXUS_DOMAIN_TYPEHASH)),
keccak256(stringToBytes(name)),
keccak256(stringToBytes(version)),
verifyingContract
]
)
)
return keccak256(
concat([
stringToBytes("\x19\x01"),
hexToBytes(DOMAIN_SEPARATOR),
hexToBytes(structHash)
])
)
}
export function getTypesForEIP712Domain({
domain
}: {
domain?: TypedDataDomain | undefined
}): TypedDataParameter[] {
return [
typeof domain?.name === "string" && { name: "name", type: "string" },
domain?.version && { name: "version", type: "string" },
typeof domain?.chainId === "number" && {
name: "chainId",
type: "uint256"
},
domain?.verifyingContract && {
name: "verifyingContract",
type: "address"
},
domain?.salt && { name: "salt", type: "bytes32" }
].filter(Boolean) as TypedDataParameter[]
}
/**
* Retrieves account metadata including name, version, and chain ID.
*
* Tries `eip712Domain()` on-chain first. If the SCA is not yet deployed
* (counterfactual) or the call fails for any reason, falls back to the static
* domain name + version. When `meeVersionOrConfig` is supplied, the fallback
* version is derived from that config's `accountId` (e.g. MEE v2.2.2 →
* `"1.3.2"`); otherwise it falls back to the legacy `NEXUS_DOMAIN_VERSION`
* constant, which only reflects Nexus 1.2.0 and is wrong for any MEE ≥ 2.2.x.
* Pass `meeVersionOrConfig` whenever you know which MEE version the account
* is being used with.
*
* @param client - The viem Client instance
* @param accountAddress - The account address to query
* @param meeVersionOrConfig - Optional MEE version (or resolved config) used
* to derive the correct fallback domain version. Strongly recommended when
* the account may not yet be deployed.
* @returns Promise resolving to account metadata
*/
export const getAccountMeta = async (
client: Client,
accountAddress: Address,
meeVersionOrConfig?: MEEVersion | MEEVersionConfig
): Promise<AccountMetadata> => {
try {
const domain = await client.request({
method: "eth_call",
params: [
{
to: accountAddress,
data: encodeFunctionData({
abi: EIP1271Abi,
functionName: "eip712Domain"
})
},
"latest"
]
})
if (domain !== "0x") {
const decoded = decodeFunctionResult({
abi: EIP1271Abi,
functionName: "eip712Domain",
data: domain
})
return {
name: decoded?.[1],
version: decoded?.[2],
chainId: decoded?.[3]
}
}
} catch (error) {}
return {
name: NEXUS_DOMAIN_NAME,
version: meeVersionOrConfig
? getNexusDomainVersion(meeVersionOrConfig)
: NEXUS_DOMAIN_VERSION,
chainId: client.chain
? BigInt(client.chain.id)
: BigInt(await client.extend(publicActions).getChainId())
}
}
/**
* Wraps a typed data hash with EIP-712 domain separator.
*
* @param typedHash - The hash to wrap
* @param appDomainSeparator - The domain separator
* @returns The wrapped hash
*/
export const eip712WrapHash = (typedHash: Hex, appDomainSeparator: Hex): Hex =>
keccak256(concat(["0x1901", appDomainSeparator, typedHash]))
export type TypedDataWith712 = {
EIP712Domain: TypedDataParameter[]
} & TypedData
export function typeToString(typeDef: TypedDataWith712): string[] {
return Object.entries(typeDef).map(([key, fields]) => {
const fieldStrings = (fields ?? [])
.map((field) => `${field.type} ${field.name}`)
.join(",")
return `${key}(${fieldStrings})`
})
}
/** @ignore */
export function bigIntReplacer(_key: string, value: AnyData) {
return typeof value === "bigint" ? value.toString() : value
}
export function numberTo3Bytes(key: bigint): Uint8Array {
// todo: check range
const buffer = new Uint8Array(3)
buffer[0] = Number((key >> 16n) & 0xffn)
buffer[1] = Number((key >> 8n) & 0xffn)
buffer[2] = Number(key & 0xffn)
return buffer
}
export function toHexString(byteArray: Uint8Array): string {
return Array.from(byteArray)
.map((byte) => byte.toString(16).padStart(2, "0")) // Convert each byte to hex and pad to 2 digits
.join("") // Join all hex values together into a single string
}
export const getAccountDomainStructFields = async (
publicClient: PublicClient,
accountAddress: Address
) => {
const accountDomainStructFields = (await publicClient.readContract({
address: accountAddress,
abi: parseAbi([
"function eip712Domain() public view returns (bytes1 fields, string memory name, string memory version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] memory extensions)"
]),
functionName: "eip712Domain"
})) as EIP712DomainReturn
const [, name, version, chainId, verifyingContract, salt] =
accountDomainStructFields
const params = parseAbiParameters([
"bytes32",
"bytes32",
"uint256",
"address",
"bytes32"
])
return encodeAbiParameters(params, [
keccak256(toBytes(name)),
keccak256(toBytes(version)),
chainId,
verifyingContract,
salt
])
}
export const inProduction = () => {
try {
return process?.env?.environment === "production"
} catch (e) {
return true
}
}
export const playgroundTrue = () => {
try {
return process?.env?.RUN_PLAYGROUND === "true"
} catch (e) {
return false
}
}
type TenderlyDetails = {
accountSlug: string
projectSlug: string
apiKey: string
}
export const getTenderlyDetails = (): TenderlyDetails | null => {
try {
const accountSlug = process?.env?.TENDERLY_ACCOUNT_SLUG
const projectSlug = process?.env?.TENDERLY_PROJECT_SLUG
const apiKey = process?.env?.TENDERLY_API_KEY
if (!accountSlug || !projectSlug || !apiKey) {
return null
}
return {
accountSlug,
projectSlug,
apiKey
}
} catch (e) {
return null
}
}
/**
* Safely multiplies a bigint by a number, rounding appropriately.
*
* @param bI - The bigint to multiply
* @param multiplier - The multiplication factor
* @returns The multiplied bigint
*/
export const safeMultiplier = (bI: bigint, multiplier: number): bigint =>
BigInt(Math.round(Number(bI) * multiplier))
export type EthersWallet = {
signTransaction: (...args: AnyData[]) => Promise<AnyData>
signMessage: (...args: AnyData[]) => Promise<AnyData>
signTypedData: (...args: AnyData[]) => Promise<AnyData>
getAddress: () => Promise<AnyData>
address: Address
provider: AnyData
}
export const getAllowance = async (
client: PublicClient,
owner: Address,
tokenAddress: Address,
spender = BICONOMY_TOKEN_PAYMASTER
): Promise<bigint> => {
const approval = await client.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "allowance",
args: [owner, spender]
})
return approval as bigint
}
export function parseRequestArguments(input: string[]) {
const fieldsToOmit = [
"callGasLimit",
"preVerificationGas",
"maxFeePerGas",
"maxPriorityFeePerGas",
"paymasterAndData",
"verificationGasLimit"
]
// Skip the first element which is just "Request Arguments:"
const argsString = input.slice(1).join("")
// Split by newlines and filter out empty lines
const lines = argsString.split("\n").filter((line) => line.trim())
// Create an object from the key-value pairs
const result = lines.reduce(
(acc, line) => {
// Remove extra spaces and split by ':'
const [key, value] = line.split(":").map((s) => s.trim())
// Clean up the key (remove trailing spaces and colons)
const cleanKey = key.trim()
// Clean up the value (remove 'gwei' and other units)
const cleanValue: string | number = value.replace("gwei", "").trim()
if (fieldsToOmit.includes(cleanKey)) {
return acc
}
acc[cleanKey] = cleanValue
return acc
},
{} as Record<string, string | number>
)
return result
}
/**
* Checks if a chain supports Cancun.
*
* @param transport - The transport to use
* @param chain - The chain to check
* @returns True if the chain supports Cancun, false otherwise
*/
export async function supportsCancun({
transport,
chain
}: {
transport: Transport
chain: Chain
}): Promise<boolean> {
const cancunSupportedChains: Record<string, boolean> = {
"1": true,
"11155111": true,
"8453": true,
"84532": true,
"137": true,
"80002": true,
"42161": true,
"421614": true,
"10": true,
"11155420": true,
"56": true,
"97": true,
"146": true,
"57054": true,
"534352": true,
"534351": true,
"100": true,
"10200": true,
"43114": true,
"43113": true,
"33139": true,
"33111": true,
"999": true,
"1116": true,
"267": true,
"1329": true,
"1328": true,
"130": true,
"1301": true,
"747474": true,
"1135": true,
"480": true,
"4801": true,
"20993": true,
"10143": true,
"143": true,
"88882": false,
"531050204": true,
"5010405": true
}
if (cancunSupportedChains[chain.id.toString()]) {
return cancunSupportedChains[chain.id.toString()]
}
const client = createPublicClient({
chain,
transport
})
// Fetch the latest block with full transactions
const block = await client.getBlock({
blockTag: "latest"
})
// Check for Cancun-specific block fields
if (block.blobGasUsed !== undefined || block.excessBlobGas !== undefined) {
return true
}
return false
}
/**
* Calculate the storage slot for nonces in EntryPoint V7
* Following exact Solidity storage layout rules from docs
*/
export function calculateNonceStorageSlot(sender: Address, key = 0n): Hex {
const BASE_SLOT = 1 // nonceSequenceNumber is at slot 1
// Step 1: Calculate slot for first mapping level
// keccak256(abi.encode(address_key, uint256_slot))
// Both address and slot are padded to 32 bytes
const firstLevelSlot = keccak256(
concat([
pad(sender, { size: 32 }), // address padded to 32 bytes
pad(numberToHex(BASE_SLOT), { size: 32 }) // slot padded to 32 bytes
])
)
// Step 2: Calculate slot for second mapping level
// keccak256(abi.encode(uint192_key, bytes32_slot))
const finalSlot = keccak256(
concat([
pad(numberToHex(key), { size: 32 }), // uint192 key padded to 32 bytes
firstLevelSlot // already 32 bytes
])
)
return finalSlot
}
/**
* Calculates the storage slot for "initializedSlots" mapping using a namespace and slot.
*
* This follows Solidity's rules for mapping storage layout. Used, for example, by
* contracts that store initialization flags for a (namespace, slot) pair at mapping slot 0.
*
* Storage structure (Solidity pseudocode):
* mapping(bytes32 namespace => mapping(bytes32 slot => bool)) initializedSlots;
*
* The formula:
* keccak256(
* abi.encode(
* keccak256(abi.encode(namespace, slot)),
* uint256(BASE_SLOT)
* )
* )
*
* Both keys and slot are padded to 32 bytes. BASE_SLOT = 0 here.
*
* @param namespace - 32-byte hex string namespace identifier
* @param slot - 32-byte hex string representing the individual slot
* @returns The storage slot (as Hex) for (namespace, slot) in "initializedSlots"
*/
export function calculateNameSpaceStorageInitializationSlot(
namespace: Hex,
slot: Hex
): Hex {
const BASE_SLOT = 0 // 'initializedSlots' mapping lives at storage slot 0
// Step 1: keccak256(abi.encode(namespace, slot))
// Both are padded to 32 bytes as per Solidity's abi.encode
const namespacedSlot = keccak256(
concat([pad(namespace, { size: 32 }), pad(slot, { size: 32 })])
)
// Step 2: keccak256(abi.encode(namespacedSlot, BASE_SLOT))
// namespacedSlot is already 32 bytes; BASE_SLOT is padded to 32 bytes
const mappingSlot = keccak256(
concat([
pad(namespacedSlot, { size: 32 }),
pad(numberToHex(BASE_SLOT), { size: 32 }) // slot padding
])
)
return mappingSlot
}
/**
* Checks if the account has consistent MEE versions across deployments for signing the quote.
*
* Version < 2.2.0 uses personal_sign, version >= 2.2.0 uses EIP-712 signing.
* Mixing these two signing methods is not allowed as it would result in incompatible signatures.
*
* For EIP-712 versions (>= 2.2.0), all MEE versions must be the same to ensure consistent
* signature validation across chains.
*
* @param meeVersions - Array of chain IDs with their corresponding MEE versions
* @throws Error if versions are inconsistent or mixing signing methods
*/
export function validateConsistentMeeVersions(
meeVersions: MeeVersionsWithChainId
): void {
let hasPersonalSignVersion = false
let hasEIP712Version = false
// Track version consistency while iterating (fail-fast approach)
for (const { version: meeVersionConfig } of meeVersions) {
const meeVersion = meeVersionConfig.version
// Check version type as we go
const isCurrentVersionPersonalSign = isVersionOlder(
meeVersion,
MEEVersion.V2_2_1
)
const isCurrentVersionEIP712 = versionIsAtLeast(
meeVersion,
MEEVersion.V2_2_1
)
// Update flags
hasPersonalSignVersion =
hasPersonalSignVersion || isCurrentVersionPersonalSign
hasEIP712Version = hasEIP712Version || isCurrentVersionEIP712
// Early exit: Cannot mix personal_sign and EIP-712 signing methods
if (hasPersonalSignVersion && hasEIP712Version) {
throw new Error(
"MEE versions on all chains should be whether less than 2.2.0 or greater than or equal to 2.2.0. " +
"Otherwise Multichain account won't be able to consume the same signature across all chains involved in the quote request."
)
}
}
}