-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVRFCoreV1.sol
More file actions
218 lines (175 loc) · 6.57 KB
/
VRFCoreV1.sol
File metadata and controls
218 lines (175 loc) · 6.57 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {IMailbox} from "hyperlane-xyz/interfaces/IMailbox.sol";
import {IVRFProofVerifier} from "./interfaces/IVRFProofVerifier.sol";
import {IMessageRecipient} from "./interfaces/IMessageRecipient.sol";
/// @custom:oz-upgrades
contract VRFCoreV1 is
UUPSUpgradeable,
AccessControlUpgradeable,
IMessageRecipient
{
using EnumerableSet for EnumerableSet.Bytes32Set;
using EnumerableSet for EnumerableSet.AddressSet;
bytes32 public constant RELAYER_ROLE = keccak256("RELAYER_ROLE");
bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
mapping(bytes32 => RequestData) requests;
EnumerableSet.Bytes32Set pendingRequests;
IMailbox public mailbox;
IVRFProofVerifier public proofVerifier;
struct RequestData {
uint32 origin;
bytes32 sender;
uint256 timestamp;
RequestStatus status;
// Execution Data
address executor;
uint256[2] executorPublicKey;
bytes proof;
bytes randomness;
}
enum RequestStatus {
PENDING_EXECUTION,
PENDING_VALIDATION,
VALIDATED,
FAILED
}
// === ERRORS ===
error RequestAlreadyProcessed();
error InvalidRequestStatus(RequestStatus got, RequestStatus expected);
error InvalidValidator(address validator);
error InvalidExecutorSignature(address executor);
error InvalidProof();
// === EVENTS ===
event RequestReceived(bytes32 indexed requestId);
event VRFExecuted(bytes32 indexed requestId);
event ReceivedMessage(uint32 _origin, bytes32 _sender, string _data);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(
address _admin,
address _proofVerifier,
address _mailbox
) external initializer {
__UUPSUpgradeable_init();
__AccessControl_init();
_grantRole(DEFAULT_ADMIN_ROLE, _admin);
_grantRole(RELAYER_ROLE, _mailbox);
proofVerifier = IVRFProofVerifier(_proofVerifier);
mailbox = IMailbox(_mailbox);
}
function _authorizeUpgrade(
address /* newImplementation */
) internal override onlyRole(DEFAULT_ADMIN_ROLE) {}
function addRelayer(address relayer) public onlyRole(DEFAULT_ADMIN_ROLE) {
_grantRole(RELAYER_ROLE, relayer);
}
function addExecutor(address executor) public onlyRole(DEFAULT_ADMIN_ROLE) {
_grantRole(EXECUTOR_ROLE, executor);
}
// Implement Hyperlane IMessageRecipient
function handle(
uint32 _origin,
bytes32 _sender,
bytes calldata _message
) external payable onlyRole(RELAYER_ROLE) {
emit ReceivedMessage(_origin, _sender, string(_message));
bytes32 requestId = abi.decode(_message, (bytes32));
handleCrossChainRequest(requestId, _origin, _sender);
}
function handleCrossChainRequest(bytes32 requestId, uint32 origin, bytes32 sender) internal {
if (requests[requestId].timestamp != 0) {
revert RequestAlreadyProcessed();
}
requests[requestId].origin = origin;
requests[requestId].sender = sender;
requests[requestId].timestamp = block.timestamp;
requests[requestId].status = RequestStatus.PENDING_EXECUTION;
pendingRequests.add(requestId);
emit RequestReceived(requestId);
}
function executeVRF(
bytes32 requestId,
uint256[2] calldata publicKey,
bytes calldata randomness,
bytes calldata proof,
bytes calldata signature
) public payable onlyRole(EXECUTOR_ROLE) {
RequestData storage req = requests[requestId];
if (req.status != RequestStatus.PENDING_EXECUTION) {
revert InvalidRequestStatus(
req.status,
RequestStatus.PENDING_EXECUTION
);
}
if (
!verifyExecutorSignature(
requestId,
randomness,
proof,
signature,
msg.sender
)
) {
revert InvalidExecutorSignature(msg.sender);
}
if (!proofVerifier.verify(publicKey, proof, randomness)) {
revert InvalidProof();
}
req.executor = msg.sender;
req.executorPublicKey = publicKey;
req.randomness = randomness;
req.proof = proof;
req.status = RequestStatus.VALIDATED;
pendingRequests.remove(requestId);
emit VRFExecuted(requestId);
bytes memory message = abi.encode(requestId, randomness, publicKey, proof);
mailbox.dispatch{value: msg.value}(req.origin, req.sender, message);
}
function verifyExecutorSignature(
bytes32 requestId,
bytes memory randomness,
bytes memory proof,
bytes memory signature,
address signer
) public pure returns (bool) {
bytes memory packed = abi.encodePacked(requestId, randomness, proof);
bytes32 hashed = keccak256(packed);
bytes32 ethSignedMessageHash = keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", hashed)
);
return recoverSigner(ethSignedMessageHash, signature) == signer;
}
function recoverSigner(
bytes32 ethSignedMessageHash,
bytes memory signature
) public pure returns (address) {
(bytes32 r, bytes32 s, uint8 v) = splitSignature(signature);
return ecrecover(ethSignedMessageHash, v, r, s);
}
function splitSignature(
bytes memory sig
) public pure returns (bytes32 r, bytes32 s, uint8 v) {
require(sig.length == 65, "invalid signature length");
assembly {
/*
First 32 bytes stores the length of the signature
add(sig, 32) = pointer of sig + 32
effectively, skips first 32 bytes of signature
mload(p) loads next 32 bytes starting at the memory address p into memory
*/
// first 32 bytes, after the length prefix
r := mload(add(sig, 32))
// second 32 bytes
s := mload(add(sig, 64))
// final byte (first byte of the next 32 bytes)
v := byte(0, mload(add(sig, 96)))
}
// implicitly return (r, s, v)
}
}