-
Notifications
You must be signed in to change notification settings - Fork 122
/
KeyGateway.sol
127 lines (114 loc) · 3.88 KB
/
KeyGateway.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
import {IKeyGateway} from "./interfaces/IKeyGateway.sol";
import {IKeyRegistry} from "./interfaces/IKeyRegistry.sol";
import {EIP712} from "./abstract/EIP712.sol";
import {Nonces} from "./abstract/Nonces.sol";
import {Guardians} from "./abstract/Guardians.sol";
import {Signatures} from "./abstract/Signatures.sol";
/**
* @title Farcaster KeyGateway
*
* @notice See https://github.com/farcasterxyz/contracts/blob/v3.1.0/docs/docs.md for an overview.
*
* @custom:security-contact [email protected]
*/
contract KeyGateway is IKeyGateway, Guardians, Signatures, EIP712, Nonces {
/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IKeyGateway
*/
string public constant VERSION = "2023.11.15";
/**
* @inheritdoc IKeyGateway
*/
bytes32 public constant ADD_TYPEHASH = keccak256(
"Add(address owner,uint32 keyType,bytes key,uint8 metadataType,bytes metadata,uint256 nonce,uint256 deadline)"
);
/*//////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IKeyGateway
*/
IKeyRegistry public immutable keyRegistry;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Configure the address of the KeyRegistry contract.
* Set the initial owner address.
*
* @param _keyRegistry Address of the KeyRegistry contract.
* @param _initialOwner Address of the inital owner.
*/
constructor(
address _keyRegistry,
address _initialOwner
) Guardians(_initialOwner) EIP712("Farcaster KeyGateway", "1") {
keyRegistry = IKeyRegistry(_keyRegistry);
}
/*//////////////////////////////////////////////////////////////
REGISTRATION
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IKeyGateway
*/
function add(
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes calldata metadata
) external whenNotPaused {
keyRegistry.add(msg.sender, keyType, key, metadataType, metadata);
}
/**
* @inheritdoc IKeyGateway
*/
function addFor(
address fidOwner,
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes calldata metadata,
uint256 deadline,
bytes calldata sig
) external whenNotPaused {
_verifyAddSig(fidOwner, keyType, key, metadataType, metadata, deadline, sig);
keyRegistry.add(fidOwner, keyType, key, metadataType, metadata);
}
/*//////////////////////////////////////////////////////////////
SIGNATURE VERIFICATION HELPERS
//////////////////////////////////////////////////////////////*/
function _verifyAddSig(
address fidOwner,
uint32 keyType,
bytes memory key,
uint8 metadataType,
bytes memory metadata,
uint256 deadline,
bytes memory sig
) internal {
_verifySig(
_hashTypedDataV4(
keccak256(
abi.encode(
ADD_TYPEHASH,
fidOwner,
keyType,
keccak256(key),
metadataType,
keccak256(metadata),
_useNonce(fidOwner),
deadline
)
)
),
fidOwner,
deadline,
sig
);
}
}