-
Notifications
You must be signed in to change notification settings - Fork 8
/
MetaTxLib.sol
508 lines (482 loc) · 19.3 KB
/
MetaTxLib.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import {IERC1271} from '@openzeppelin/contracts/interfaces/IERC1271.sol';
import {ILensERC721} from 'contracts/interfaces/ILensERC721.sol';
import {Types} from 'contracts/libraries/constants/Types.sol';
import {Errors} from 'contracts/libraries/constants/Errors.sol';
import {Typehash} from 'contracts/libraries/constants/Typehash.sol';
import {StorageLib} from 'contracts/libraries/StorageLib.sol';
/**
* @title MetaTxLib
* @author Lens Protocol
*
* NOTE: the functions in this contract operate under the assumption that the passed signer is already validated
* to either be the originator or one of their delegated executors.
*
* @dev User nonces are incremented from this library as well.
*/
library MetaTxLib {
string constant EIP712_DOMAIN_VERSION = '2';
bytes32 constant EIP712_DOMAIN_VERSION_HASH = keccak256(bytes(EIP712_DOMAIN_VERSION));
bytes4 constant EIP1271_MAGIC_VALUE = 0x1626ba7e;
/**
* @dev We store the domain separator and LensHub Proxy address as constants to save gas.
*
* keccak256(
* abi.encode(
* keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
* keccak256('Lens Protocol Profiles'), // Contract Name
* keccak256('2'), // Version Hash
* 137, // Polygon Chain ID
* address(0xDb46d1Dc155634FbC732f92E853b10B288AD5a1d) // Verifying Contract Address - LensHub Address
* )
* );
*/
bytes32 constant LENS_HUB_CACHED_POLYGON_DOMAIN_SEPARATOR =
0xbf9544cf7d7a0338fc4f071be35409a61e51e9caef559305410ad74e16a05f2d;
address constant LENS_HUB_ADDRESS = 0xDb46d1Dc155634FbC732f92E853b10B288AD5a1d;
function validateSetProfileMetadataURISignature(
Types.EIP712Signature calldata signature,
uint256 profileId,
string calldata metadataURI
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.SET_PROFILE_METADATA_URI,
profileId,
keccak256(bytes(metadataURI)),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validateSetFollowModuleSignature(
Types.EIP712Signature calldata signature,
uint256 profileId,
address followModule,
bytes calldata followModuleInitData
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.SET_FOLLOW_MODULE,
profileId,
followModule,
keccak256(followModuleInitData),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validateChangeDelegatedExecutorsConfigSignature(
Types.EIP712Signature calldata signature,
uint256 delegatorProfileId,
address[] calldata delegatedExecutors,
bool[] calldata approvals,
uint64 configNumber,
bool switchToGivenConfig
) external {
uint256 nonce = _getAndIncrementNonce(signature.signer);
uint256 deadline = signature.deadline;
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.CHANGE_DELEGATED_EXECUTORS_CONFIG,
delegatorProfileId,
abi.encodePacked(delegatedExecutors),
abi.encodePacked(approvals),
configNumber,
switchToGivenConfig,
nonce,
deadline
)
)
),
signature
);
}
function validateSetProfileImageURISignature(
Types.EIP712Signature calldata signature,
uint256 profileId,
string calldata imageURI
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.SET_PROFILE_IMAGE_URI,
profileId,
keccak256(bytes(imageURI)),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validatePostSignature(Types.EIP712Signature calldata signature, Types.PostParams calldata postParams)
external
{
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.POST,
postParams.profileId,
keccak256(bytes(postParams.contentURI)),
postParams.actionModules,
_hashActionModulesInitDatas(postParams.actionModulesInitDatas),
postParams.referenceModule,
keccak256(postParams.referenceModuleInitData),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function _hashActionModulesInitDatas(bytes[] memory actionModulesInitDatas) private pure returns (bytes32) {
bytes32[] memory actionModulesInitDatasHashes = new bytes32[](actionModulesInitDatas.length);
uint256 i;
while (i < actionModulesInitDatas.length) {
actionModulesInitDatasHashes[i] = keccak256(abi.encode(actionModulesInitDatas[i]));
unchecked {
++i;
}
}
return keccak256(abi.encodePacked(actionModulesInitDatasHashes));
}
// We need this to deal with stack too deep:
struct ReferenceParamsForAbiEncode {
bytes32 typehash;
uint256 profileId;
bytes32 contentURIHash;
uint256 pointedProfileId;
uint256 pointedPubId;
uint256[] referrerProfileIds;
uint256[] referrerPubIds;
bytes32 referenceModuleDataHash;
address[] actionModules;
bytes32 actionModulesInitDataHash;
address referenceModule;
bytes32 referenceModuleInitDataHash;
uint256 nonce;
uint256 deadline;
}
function _abiEncode(ReferenceParamsForAbiEncode memory referenceParamsForAbiEncode)
private
pure
returns (bytes memory)
{
// This assembly workaround allows us to avoid Stack Too Deep error when encoding all the params of the struct.
// We remove the first 32 bytes of the encoded struct, which is the offset of the struct.
// The rest of the encoding is the same, so we can just return it.
bytes memory encodedStruct = abi.encode(referenceParamsForAbiEncode);
assembly {
let lengthWithoutOffset := sub(mload(encodedStruct), 32) // Calculates length without offset.
encodedStruct := add(encodedStruct, 32) // Skips the offset by shifting the memory pointer.
mstore(encodedStruct, lengthWithoutOffset) // Stores new length, which now excludes the offset.
}
return encodedStruct;
// The code above is the equivalent of:
//
// return abi.encode(
// referenceParamsForAbiEncode.typehash,
// referenceParamsForAbiEncode.profileId,
// referenceParamsForAbiEncode.contentURIHash,
// referenceParamsForAbiEncode.pointedProfileId,
// referenceParamsForAbiEncode.pointedPubId,
// referenceParamsForAbiEncode.referrerProfileIds,
// referenceParamsForAbiEncode.referrerPubIds,
// referenceParamsForAbiEncode.referenceModuleDataHash,
// referenceParamsForAbiEncode.actionModules,
// referenceParamsForAbiEncode.actionModulesInitDataHash,
// referenceParamsForAbiEncode.referenceModule,
// referenceParamsForAbiEncode.referenceModuleInitDataHash,
// referenceParamsForAbiEncode.nonce,
// referenceParamsForAbiEncode.deadline
// );
}
function validateCommentSignature(
Types.EIP712Signature calldata signature,
Types.CommentParams calldata commentParams
) external {
bytes32 contentURIHash = keccak256(bytes(commentParams.contentURI));
bytes32 referenceModuleDataHash = keccak256(commentParams.referenceModuleData);
bytes32 actionModulesInitDataHash = _hashActionModulesInitDatas(commentParams.actionModulesInitDatas);
bytes32 referenceModuleInitDataHash = keccak256(commentParams.referenceModuleInitData);
uint256 nonce = _getAndIncrementNonce(signature.signer);
uint256 deadline = signature.deadline;
bytes memory encodedAbi = _abiEncode(
ReferenceParamsForAbiEncode(
Typehash.COMMENT,
commentParams.profileId,
contentURIHash,
commentParams.pointedProfileId,
commentParams.pointedPubId,
commentParams.referrerProfileIds,
commentParams.referrerPubIds,
referenceModuleDataHash,
commentParams.actionModules,
actionModulesInitDataHash,
commentParams.referenceModule,
referenceModuleInitDataHash,
nonce,
deadline
)
);
_validateRecoveredAddress(_calculateDigest(keccak256(encodedAbi)), signature);
}
function validateQuoteSignature(Types.EIP712Signature calldata signature, Types.QuoteParams calldata quoteParams)
external
{
bytes32 contentURIHash = keccak256(bytes(quoteParams.contentURI));
bytes32 referenceModuleDataHash = keccak256(quoteParams.referenceModuleData);
bytes32 actionModulesInitDataHash = _hashActionModulesInitDatas(quoteParams.actionModulesInitDatas);
bytes32 referenceModuleInitDataHash = keccak256(quoteParams.referenceModuleInitData);
uint256 nonce = _getAndIncrementNonce(signature.signer);
uint256 deadline = signature.deadline;
bytes memory encodedAbi = _abiEncode(
ReferenceParamsForAbiEncode(
Typehash.QUOTE,
quoteParams.profileId,
contentURIHash,
quoteParams.pointedProfileId,
quoteParams.pointedPubId,
quoteParams.referrerProfileIds,
quoteParams.referrerPubIds,
referenceModuleDataHash,
quoteParams.actionModules,
actionModulesInitDataHash,
quoteParams.referenceModule,
referenceModuleInitDataHash,
nonce,
deadline
)
);
_validateRecoveredAddress(_calculateDigest(keccak256(encodedAbi)), signature);
}
function validateMirrorSignature(Types.EIP712Signature calldata signature, Types.MirrorParams calldata mirrorParams)
external
{
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.MIRROR,
mirrorParams.profileId,
mirrorParams.pointedProfileId,
mirrorParams.pointedPubId,
mirrorParams.referrerProfileIds,
mirrorParams.referrerPubIds,
keccak256(mirrorParams.referenceModuleData),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validateBurnSignature(Types.EIP712Signature calldata signature, uint256 tokenId) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(Typehash.BURN, tokenId, _getAndIncrementNonce(signature.signer), signature.deadline)
)
),
signature
);
}
function validateFollowSignature(
Types.EIP712Signature calldata signature,
uint256 followerProfileId,
uint256[] calldata idsOfProfilesToFollow,
uint256[] calldata followTokenIds,
bytes[] calldata datas
) external {
uint256 dataLength = datas.length;
bytes32[] memory dataHashes = new bytes32[](dataLength);
uint256 i;
while (i < dataLength) {
dataHashes[i] = keccak256(datas[i]);
unchecked {
++i;
}
}
uint256 nonce = _getAndIncrementNonce(signature.signer);
uint256 deadline = signature.deadline;
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.FOLLOW,
followerProfileId,
keccak256(abi.encodePacked(idsOfProfilesToFollow)),
keccak256(abi.encodePacked(followTokenIds)),
keccak256(abi.encodePacked(dataHashes)),
nonce,
deadline
)
)
),
signature
);
}
function validateUnfollowSignature(
Types.EIP712Signature calldata signature,
uint256 unfollowerProfileId,
uint256[] calldata idsOfProfilesToUnfollow
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.UNFOLLOW,
unfollowerProfileId,
keccak256(abi.encodePacked(idsOfProfilesToUnfollow)),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validateSetBlockStatusSignature(
Types.EIP712Signature calldata signature,
uint256 byProfileId,
uint256[] calldata idsOfProfilesToSetBlockStatus,
bool[] calldata blockStatus
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.SET_BLOCK_STATUS,
byProfileId,
keccak256(abi.encodePacked(idsOfProfilesToSetBlockStatus)),
keccak256(abi.encodePacked(blockStatus)),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validateLegacyCollectSignature(
Types.EIP712Signature calldata signature,
Types.CollectParams calldata collectParams
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.LEGACY_COLLECT,
collectParams.publicationCollectedProfileId,
collectParams.publicationCollectedId,
collectParams.collectorProfileId,
collectParams.referrerProfileId,
collectParams.referrerPubId,
keccak256(collectParams.collectModuleData),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function validateActSignature(
Types.EIP712Signature calldata signature,
Types.PublicationActionParams calldata publicationActionParams
) external {
_validateRecoveredAddress(
_calculateDigest(
keccak256(
abi.encode(
Typehash.ACT,
publicationActionParams.publicationActedProfileId,
publicationActionParams.publicationActedId,
publicationActionParams.actorProfileId,
publicationActionParams.referrerProfileIds,
publicationActionParams.referrerPubIds,
publicationActionParams.actionModuleAddress,
keccak256(publicationActionParams.actionModuleData),
_getAndIncrementNonce(signature.signer),
signature.deadline
)
)
),
signature
);
}
function calculateDomainSeparator() internal view returns (bytes32) {
if (address(this) == LENS_HUB_ADDRESS) {
return LENS_HUB_CACHED_POLYGON_DOMAIN_SEPARATOR;
}
return
keccak256(
abi.encode(
Typehash.EIP712_DOMAIN,
keccak256(bytes(ILensERC721(address(this)).name())),
EIP712_DOMAIN_VERSION_HASH,
block.chainid,
address(this)
)
);
}
/**
* @dev Wrapper for ecrecover to reduce code size, used in meta-tx specific functions.
*/
function _validateRecoveredAddress(bytes32 digest, Types.EIP712Signature calldata signature) private view {
if (signature.deadline < block.timestamp) revert Errors.SignatureExpired();
// If the expected address is a contract, check the signature there.
if (signature.signer.code.length != 0) {
bytes memory concatenatedSig = abi.encodePacked(signature.r, signature.s, signature.v);
if (IERC1271(signature.signer).isValidSignature(digest, concatenatedSig) != EIP1271_MAGIC_VALUE) {
revert Errors.SignatureInvalid();
}
} else {
address recoveredAddress = ecrecover(digest, signature.v, signature.r, signature.s);
if (recoveredAddress == address(0) || recoveredAddress != signature.signer) {
revert Errors.SignatureInvalid();
}
}
}
/**
* @dev Calculates EIP712 digest based on the current DOMAIN_SEPARATOR.
*
* @param hashedMessage The message hash from which the digest should be calculated.
*
* @return bytes32 A 32-byte output representing the EIP712 digest.
*/
function _calculateDigest(bytes32 hashedMessage) private view returns (bytes32) {
return keccak256(abi.encodePacked('\x19\x01', calculateDomainSeparator(), hashedMessage));
}
/**
* @dev This fetches a user's signing nonce and increments it, akin to `sigNonces++`.
*
* @param user The user address to fetch and post-increment the signing nonce for.
*
* @return uint256 The signing nonce for the given user prior to being incremented.
*/
function _getAndIncrementNonce(address user) private returns (uint256) {
unchecked {
return StorageLib.nonces()[user]++;
}
}
}