@inco/lightning 0.1.20

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +69 -0
  3. package/dumps/incoLightning_0_1_23__547622051.dump.json +1 -0
  4. package/dumps/incoLightning_0_1_23__547622051.env +15 -0
  5. package/dumps/incoLightning_0_1_23__830342853.dump.json +1 -0
  6. package/dumps/incoLightning_0_1_23__830342853.env +15 -0
  7. package/dumps/incoLightning_0_1_24__266705097.dump.json +1 -0
  8. package/dumps/incoLightning_0_1_24__266705097.env +15 -0
  9. package/dumps/incoLightning_0_1_25__986372984.dump.json +1 -0
  10. package/dumps/incoLightning_0_1_25__986372984.env +15 -0
  11. package/foundry.toml +20 -0
  12. package/package.json +27 -0
  13. package/remappings.txt +4 -0
  14. package/src/DeployUtils.sol +109 -0
  15. package/src/IncoLightning.sol +45 -0
  16. package/src/Lib.sol +389 -0
  17. package/src/Lib.template.sol +464 -0
  18. package/src/Types.sol +74 -0
  19. package/src/libs/incoLightning_0_1_22__761766708.sol +389 -0
  20. package/src/libs/incoLightning_0_1_23__547622051.sol +389 -0
  21. package/src/libs/incoLightning_0_1_23__830342853.sol +389 -0
  22. package/src/libs/incoLightning_0_1_24__266705097.sol +389 -0
  23. package/src/libs/incoLightning_0_1_25__986372984.sol +389 -0
  24. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +105 -0
  25. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +12 -0
  26. package/src/lightning-parts/DecryptionHandler.sol +164 -0
  27. package/src/lightning-parts/EncryptedInput.sol +61 -0
  28. package/src/lightning-parts/EncryptedOperations.sol +610 -0
  29. package/src/lightning-parts/TrivialEncryption.sol +43 -0
  30. package/src/lightning-parts/primitives/EventCounter.sol +32 -0
  31. package/src/lightning-parts/primitives/HandleGeneration.sol +107 -0
  32. package/src/lightning-parts/primitives/HandleMetadata.sol +38 -0
  33. package/src/lightning-parts/primitives/SignatureVerifier.sol +47 -0
  34. package/src/lightning-parts/test/HandleMetadata.t.sol +87 -0
  35. package/src/pasted-dependencies/CreateX.sol +1293 -0
  36. package/src/pasted-dependencies/ICreateX.sol +187 -0
  37. package/src/test/AddTwo.sol +48 -0
  38. package/src/test/FakeIncoInfra/FakeComputeServer.sol +137 -0
  39. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +77 -0
  40. package/src/test/FakeIncoInfra/KVStore.sol +35 -0
  41. package/src/test/FakeIncoInfra/MockOpHandler.sol +140 -0
  42. package/src/test/FakeIncoInfra/getOpForSelector.sol +71 -0
  43. package/src/test/IncoTest.sol +48 -0
  44. package/src/test/TestAddTwo.t.sol +31 -0
  45. package/src/test/TestDeploy.t.sol +39 -0
  46. package/src/test/TestExtractDataOfEventTooLarge.t.sol +43 -0
  47. package/src/test/TestFakeInfra.t.sol +301 -0
  48. package/src/test/TestVersion.t.sol +36 -0
  49. package/src/version/IncoLightningConfig.sol +13 -0
  50. package/src/version/Version.sol +76 -0
@@ -0,0 +1,389 @@
1
+ // AUTOGENERATED FILE. DO NOT EDIT.
2
+ // This file was generated by the IncoLightning library generator.
3
+ // The original template is located at Lib.template.sol
4
+
5
+ /// SPDX-License-Identifier: No License
6
+ pragma solidity ^0.8;
7
+
8
+ import "../IncoLightning.sol";
9
+ import { ebool, euint256, ETypes } from "../Types.sol";
10
+
11
+ IncoLightning constant inco = IncoLightning(0xc3c81F7Bf86Af7ed3a5f5Bb1924A063a1c126cDd);
12
+ address constant deployedBy = 0x8202D2D747784Cb7D48868E44C42C4bf162a70BC;
13
+ uint256 constant defaultDecryptionDelayLimit = 2 hours;
14
+
15
+ library e {
16
+ function sanitize(euint256 a) internal returns (euint256) {
17
+ if (euint256.unwrap(a) == bytes32(0)) {
18
+ return asEuint256(0);
19
+ }
20
+ return a;
21
+ }
22
+
23
+ function sanitize(ebool a) internal returns (ebool) {
24
+ if (ebool.unwrap(a) == bytes32(0)) {
25
+ return asEbool(false);
26
+ }
27
+ return a;
28
+ }
29
+
30
+ function s(euint256 a) internal returns (euint256) {
31
+ return sanitize(a);
32
+ }
33
+
34
+ function s(ebool a) internal returns (ebool) {
35
+ return sanitize(a);
36
+ }
37
+
38
+ function add(euint256 a, euint256 b) internal returns (euint256) {
39
+ return inco.eAdd(s(a), s(b));
40
+ }
41
+
42
+ function add(euint256 a, uint256 b) internal returns (euint256) {
43
+ return inco.eAdd(s(a), asEuint256(b));
44
+ }
45
+
46
+ function add(uint256 a, euint256 b) internal returns (euint256) {
47
+ return inco.eAdd(asEuint256(a), s(b));
48
+ }
49
+
50
+ function sub(euint256 a, euint256 b) internal returns (euint256) {
51
+ return inco.eSub(s(a), s(b));
52
+ }
53
+
54
+ function sub(euint256 a, uint256 b) internal returns (euint256) {
55
+ return inco.eSub(s(a), asEuint256(b));
56
+ }
57
+
58
+ function sub(uint256 a, euint256 b) internal returns (euint256) {
59
+ return inco.eSub(asEuint256(a), s(b));
60
+ }
61
+
62
+ function mul(euint256 a, euint256 b) internal returns (euint256) {
63
+ return inco.eMul(s(a), s(b));
64
+ }
65
+
66
+ function mul(euint256 a, uint256 b) internal returns (euint256) {
67
+ return inco.eMul(s(a), asEuint256(b));
68
+ }
69
+
70
+ function mul(uint256 a, euint256 b) internal returns (euint256) {
71
+ return inco.eMul(asEuint256(a), s(b));
72
+ }
73
+
74
+ function div(euint256 a, euint256 b) internal returns (euint256) {
75
+ return inco.eDiv(s(a), s(b));
76
+ }
77
+
78
+ function div(euint256 a, uint256 b) internal returns (euint256) {
79
+ return inco.eDiv(s(a), asEuint256(b));
80
+ }
81
+
82
+ function div(uint256 a, euint256 b) internal returns (euint256) {
83
+ return inco.eDiv(asEuint256(a), s(b));
84
+ }
85
+
86
+ function rem(euint256 a, euint256 b) internal returns (euint256) {
87
+ return inco.eRem(s(a), s(b));
88
+ }
89
+
90
+ function rem(euint256 a, uint256 b) internal returns (euint256) {
91
+ return inco.eRem(s(a), asEuint256(b));
92
+ }
93
+
94
+ function rem(uint256 a, euint256 b) internal returns (euint256) {
95
+ return inco.eRem(asEuint256(a), s(b));
96
+ }
97
+
98
+ function and(euint256 a, euint256 b) internal returns (euint256) {
99
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
100
+ }
101
+
102
+ function and(euint256 a, uint256 b) internal returns (euint256) {
103
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
104
+ }
105
+
106
+ function and(uint256 a, euint256 b) internal returns (euint256) {
107
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
108
+ }
109
+
110
+ function and(ebool a, ebool b) internal returns (ebool) {
111
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
112
+ }
113
+
114
+ function and(ebool a, bool b) internal returns (ebool) {
115
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
116
+ }
117
+
118
+ function and(bool a, ebool b) internal returns (ebool) {
119
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
120
+ }
121
+
122
+ function or(euint256 a, euint256 b) internal returns (euint256) {
123
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
124
+ }
125
+
126
+ function or(euint256 a, uint256 b) internal returns (euint256) {
127
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
128
+ }
129
+
130
+ function or(uint256 a, euint256 b) internal returns (euint256) {
131
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
132
+ }
133
+
134
+ function or(ebool a, ebool b) internal returns (ebool) {
135
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
136
+ }
137
+
138
+ function or(ebool a, bool b) internal returns (ebool) {
139
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
140
+ }
141
+
142
+ function or(bool a, ebool b) internal returns (ebool) {
143
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
144
+ }
145
+
146
+ function xor(euint256 a, euint256 b) internal returns (euint256) {
147
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
148
+ }
149
+
150
+ function xor(euint256 a, uint256 b) internal returns (euint256) {
151
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
152
+ }
153
+
154
+ function xor(uint256 a, euint256 b) internal returns (euint256) {
155
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
156
+ }
157
+
158
+ function xor(ebool a, ebool b) internal returns (ebool) {
159
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
160
+ }
161
+
162
+ function xor(ebool a, bool b) internal returns (ebool) {
163
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
164
+ }
165
+
166
+ function xor(bool a, ebool b) internal returns (ebool) {
167
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
168
+ }
169
+
170
+ function shl(euint256 a, euint256 b) internal returns (euint256) {
171
+ return inco.eShl(s(a), s(b));
172
+ }
173
+
174
+ function shl(euint256 a, uint256 b) internal returns (euint256) {
175
+ return inco.eShl(s(a), asEuint256(b));
176
+ }
177
+
178
+ function shl(uint256 a, euint256 b) internal returns (euint256) {
179
+ return inco.eShl(asEuint256(a), s(b));
180
+ }
181
+
182
+ function shr(euint256 a, euint256 b) internal returns (euint256) {
183
+ return inco.eShr(s(a), s(b));
184
+ }
185
+
186
+ function shr(euint256 a, uint256 b) internal returns (euint256) {
187
+ return inco.eShr(s(a), asEuint256(b));
188
+ }
189
+
190
+ function shr(uint256 a, euint256 b) internal returns (euint256) {
191
+ return inco.eShr(asEuint256(a), s(b));
192
+ }
193
+
194
+ function rotl(euint256 a, euint256 b) internal returns (euint256) {
195
+ return inco.eRotl(s(a), s(b));
196
+ }
197
+
198
+ function rotl(euint256 a, uint256 b) internal returns (euint256) {
199
+ return inco.eRotl(s(a), asEuint256(b));
200
+ }
201
+
202
+ function rotl(uint256 a, euint256 b) internal returns (euint256) {
203
+ return inco.eRotl(asEuint256(a), s(b));
204
+ }
205
+
206
+ function rotr(euint256 a, euint256 b) internal returns (euint256) {
207
+ return inco.eRotr(s(a), s(b));
208
+ }
209
+
210
+ function rotr(euint256 a, uint256 b) internal returns (euint256) {
211
+ return inco.eRotr(s(a), asEuint256(b));
212
+ }
213
+
214
+ function rotr(uint256 a, euint256 b) internal returns (euint256) {
215
+ return inco.eRotr(asEuint256(a), s(b));
216
+ }
217
+
218
+ function eq(euint256 a, euint256 b) internal returns (ebool) {
219
+ return inco.eEq(s(a), s(b));
220
+ }
221
+
222
+ function eq(euint256 a, uint256 b) internal returns (ebool) {
223
+ return inco.eEq(s(a), asEuint256(b));
224
+ }
225
+
226
+ function eq(uint256 a, euint256 b) internal returns (ebool) {
227
+ return inco.eEq(asEuint256(a), s(b));
228
+ }
229
+
230
+ function ne(euint256 a, euint256 b) internal returns (ebool) {
231
+ return inco.eNe(s(a), s(b));
232
+ }
233
+
234
+ function ne(euint256 a, uint256 b) internal returns (ebool) {
235
+ return inco.eNe(s(a), asEuint256(b));
236
+ }
237
+
238
+ function ne(uint256 a, euint256 b) internal returns (ebool) {
239
+ return inco.eNe(asEuint256(a), s(b));
240
+ }
241
+
242
+ function ge(euint256 a, euint256 b) internal returns (ebool) {
243
+ return inco.eGe(s(a), s(b));
244
+ }
245
+
246
+ function ge(euint256 a, uint256 b) internal returns (ebool) {
247
+ return inco.eGe(s(a), asEuint256(b));
248
+ }
249
+
250
+ function ge(uint256 a, euint256 b) internal returns (ebool) {
251
+ return inco.eGe(asEuint256(a), s(b));
252
+ }
253
+
254
+ function gt(euint256 a, euint256 b) internal returns (ebool) {
255
+ return inco.eGt(s(a), s(b));
256
+ }
257
+
258
+ function gt(euint256 a, uint256 b) internal returns (ebool) {
259
+ return inco.eGt(s(a), asEuint256(b));
260
+ }
261
+
262
+ function gt(uint256 a, euint256 b) internal returns (ebool) {
263
+ return inco.eGt(asEuint256(a), s(b));
264
+ }
265
+
266
+ function le(euint256 a, euint256 b) internal returns (ebool) {
267
+ return inco.eLe(s(a), s(b));
268
+ }
269
+
270
+ function le(euint256 a, uint256 b) internal returns (ebool) {
271
+ return inco.eLe(s(a), asEuint256(b));
272
+ }
273
+
274
+ function le(uint256 a, euint256 b) internal returns (ebool) {
275
+ return inco.eLe(asEuint256(a), s(b));
276
+ }
277
+
278
+ function lt(euint256 a, euint256 b) internal returns (ebool) {
279
+ return inco.eLt(s(a), s(b));
280
+ }
281
+
282
+ function lt(euint256 a, uint256 b) internal returns (ebool) {
283
+ return inco.eLt(s(a), asEuint256(b));
284
+ }
285
+
286
+ function lt(uint256 a, euint256 b) internal returns (ebool) {
287
+ return inco.eLt(asEuint256(a), s(b));
288
+ }
289
+
290
+ function min(euint256 a, euint256 b) internal returns (euint256) {
291
+ return inco.eMin(s(a), s(b));
292
+ }
293
+
294
+ function min(euint256 a, uint256 b) internal returns (euint256) {
295
+ return inco.eMin(s(a), asEuint256(b));
296
+ }
297
+
298
+ function min(uint256 a, euint256 b) internal returns (euint256) {
299
+ return inco.eMin(asEuint256(a), s(b));
300
+ }
301
+
302
+ function max(euint256 a, euint256 b) internal returns (euint256) {
303
+ return inco.eMax(s(a), s(b));
304
+ }
305
+
306
+ function max(euint256 a, uint256 b) internal returns (euint256) {
307
+ return inco.eMax(s(a), asEuint256(b));
308
+ }
309
+
310
+ function max(uint256 a, euint256 b) internal returns (euint256) {
311
+ return inco.eMax(asEuint256(a), s(b));
312
+ }
313
+
314
+ function not(ebool a) internal returns (ebool) {
315
+ return inco.eNot(s(a));
316
+ }
317
+
318
+ function rand() internal returns (euint256) {
319
+ return euint256.wrap(inco.eRand(ETypes.Uint256));
320
+ }
321
+
322
+ function randBounded(uint256 upperBound) internal returns (euint256) {
323
+ return euint256.wrap(inco.eRandBounded(euint256.unwrap(asEuint256(upperBound)), ETypes.Uint256));
324
+ }
325
+
326
+ function randBounded(euint256 upperBound) internal returns (euint256) {
327
+ return euint256.wrap(inco.eRandBounded(euint256.unwrap(s(upperBound)), ETypes.Uint256));
328
+ }
329
+
330
+ function asEuint256(uint256 a) internal returns (euint256) {
331
+ return inco.asEuint256(a);
332
+ }
333
+
334
+ function asEbool(bool a) internal returns (ebool) {
335
+ return inco.asEbool(a);
336
+ }
337
+
338
+ function asEbool(euint256 a) internal returns (ebool) {
339
+ return ebool.wrap(inco.eCast(euint256.unwrap(a), ETypes.Bool));
340
+ }
341
+
342
+ function asEuint256(ebool a) internal returns (euint256) {
343
+ return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
344
+ }
345
+
346
+ function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
347
+ return inco.newEuint256(ciphertext, user);
348
+ }
349
+
350
+ function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
351
+ return inco.newEbool(ciphertext, user);
352
+ }
353
+
354
+ function allow(euint256 a, address to) internal {
355
+ inco.allow(euint256.unwrap(a), to);
356
+ }
357
+
358
+ function allow(ebool a, address to) internal {
359
+ inco.allow(ebool.unwrap(a), to);
360
+ }
361
+
362
+ function allowThis(euint256 a) internal {
363
+ allow(a, address(this));
364
+ }
365
+
366
+ function allowThis(ebool a) internal {
367
+ allow(a, address(this));
368
+ }
369
+
370
+ function isAllowed(address user, euint256 a) internal view returns (bool) {
371
+ return inco.isAllowed(euint256.unwrap(a), user);
372
+ }
373
+
374
+ function select(ebool control, euint256 ifTrue, euint256 ifFalse) internal returns (euint256) {
375
+ return euint256.wrap(inco.eIfThenElse(s(control), euint256.unwrap(s(ifTrue)), euint256.unwrap(s(ifFalse))));
376
+ }
377
+
378
+ function select(ebool control, ebool ifTrue, ebool ifFalse) internal returns (ebool) {
379
+ return ebool.wrap(inco.eIfThenElse(s(control), ebool.unwrap(s(ifTrue)), ebool.unwrap(s(ifFalse))));
380
+ }
381
+
382
+ function requestDecryption(euint256 a, bytes4 callbackSelector, bytes memory callbackData) internal returns (uint256 requestId) {
383
+ requestId = inco.requestDecryption(callbackSelector, block.timestamp + defaultDecryptionDelayLimit, euint256.unwrap(s(a)), callbackData);
384
+ }
385
+
386
+ function requestDecryption(ebool a, bytes4 callbackSelector, bytes memory callbackData) internal returns (uint256 requestId) {
387
+ requestId = inco.requestDecryption(callbackSelector, block.timestamp + defaultDecryptionDelayLimit, ebool.unwrap(s(a)), callbackData);
388
+ }
389
+ }
@@ -0,0 +1,105 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {SenderNotAllowedForHandle} from "../../Types.sol";
5
+
6
+ contract AccessControlListStorage {
7
+ struct ACLStorage {
8
+ mapping(bytes32 handle => mapping(address account => bool isAllowed)) persistedAllowedPairs;
9
+ }
10
+
11
+ bytes32 private constant ACLStorageLocation = keccak256("inco.storage.ACL");
12
+
13
+ function getACLStorage() internal pure returns (ACLStorage storage $) {
14
+ bytes32 loc = ACLStorageLocation;
15
+ assembly {
16
+ $.slot := loc
17
+ }
18
+ }
19
+ }
20
+
21
+ // todo should add allowance events ?
22
+ contract BaseAccessControlList is AccessControlListStorage {
23
+ /// @dev persistent
24
+ function allow(bytes32 handle, address account) public {
25
+ require(
26
+ isAllowed(handle, msg.sender),
27
+ SenderNotAllowedForHandle(handle, msg.sender)
28
+ );
29
+ allowInternal(handle, account);
30
+ }
31
+
32
+ /// @dev persistent
33
+ function allowInternal(bytes32 handle, address account) internal {
34
+ ACLStorage storage $ = getACLStorage();
35
+ $.persistedAllowedPairs[handle][account] = true;
36
+ // todo emit event ?
37
+ }
38
+
39
+ // todo current transient allowance is unsafe, make storage account bound + how to clean between UserOps
40
+ function allowTransient(bytes32 handle, address account) public {
41
+ require(
42
+ isAllowed(handle, msg.sender),
43
+ SenderNotAllowedForHandle(handle, msg.sender)
44
+ );
45
+ allowTransientInternal(handle, account);
46
+ }
47
+
48
+ /// @dev no ownership check is performed
49
+ function allowTransientInternal(bytes32 handle, address account) internal {
50
+ bytes32 key = keccak256(abi.encodePacked(handle, account));
51
+ assembly {
52
+ tstore(key, 1)
53
+ let length := tload(0)
54
+ let lengthPlusOne := add(length, 1)
55
+ tstore(lengthPlusOne, key)
56
+ tstore(0, lengthPlusOne)
57
+ }
58
+ }
59
+
60
+ function allowedTransient(
61
+ bytes32 handle,
62
+ address account
63
+ ) public view returns (bool) {
64
+ bool isAllowedTransient;
65
+ bytes32 key = keccak256(abi.encodePacked(handle, account));
66
+ assembly {
67
+ isAllowedTransient := tload(key)
68
+ }
69
+ return isAllowedTransient;
70
+ }
71
+
72
+ function cleanTransientStorage() external {
73
+ assembly {
74
+ let length := tload(0)
75
+ tstore(0, 0)
76
+ let lengthPlusOne := add(length, 1)
77
+ for {
78
+ let i := 1
79
+ } lt(i, lengthPlusOne) {
80
+ i := add(i, 1)
81
+ } {
82
+ let handle := tload(i)
83
+ tstore(i, 0)
84
+ tstore(handle, 0)
85
+ }
86
+ }
87
+ }
88
+
89
+ function persistAllowed(
90
+ bytes32 handle,
91
+ address account
92
+ ) public view returns (bool) {
93
+ ACLStorage storage $ = getACLStorage();
94
+ return $.persistedAllowedPairs[handle][account];
95
+ }
96
+
97
+ function isAllowed(
98
+ bytes32 handle,
99
+ address account
100
+ ) public view returns (bool) {
101
+ return
102
+ allowedTransient(handle, account) ||
103
+ persistAllowed(handle, account);
104
+ }
105
+ }
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {TestUtils} from "@inco/shared/src/TestUtils.sol";
5
+ import {BaseAccessControlList} from "../BaseAccessControlList.sol";
6
+
7
+ contract TestBaseAccessControl is BaseAccessControlList, TestUtils {
8
+ function testHandleZeroIsDisallowed() public view {
9
+ bytes32 handle = bytes32(0);
10
+ assert(!isAllowed(handle, alice));
11
+ }
12
+ }
@@ -0,0 +1,164 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
5
+ import {SenderNotAllowedForHandle} from "../Types.sol";
6
+ import {SignatureVerifier} from "./primitives/SignatureVerifier.sol";
7
+ import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
8
+ import {EventCounter} from "./primitives/EventCounter.sol";
9
+
10
+ struct Request {
11
+ uint256 maxTimestamp;
12
+ address callbackContract;
13
+ bytes4 callbackSelector;
14
+ // todo #450 should contain the handle
15
+ bool fulfilled;
16
+ bytes data;
17
+ }
18
+
19
+ struct DecryptionResult {
20
+ bytes32 abiEncodedResult;
21
+ uint256 requestId;
22
+ }
23
+
24
+ event DecryptionRequested(
25
+ uint256 indexed requestId,
26
+ bytes32 indexed handle,
27
+ uint256 maxTimestamp,
28
+ uint256 eventId
29
+ );
30
+
31
+ contract DecryptionHandlerStorage {
32
+ struct DecryptionStorage {
33
+ uint256 requestIdCounter;
34
+ mapping(uint256 => Request) request;
35
+ }
36
+
37
+ bytes32 private constant DecryptionStorageLocation =
38
+ keccak256("inco.storage.DecryptionHandler");
39
+
40
+ function getDecryptionStorage()
41
+ internal
42
+ pure
43
+ returns (DecryptionStorage storage $)
44
+ {
45
+ bytes32 loc = DecryptionStorageLocation;
46
+ assembly {
47
+ $.slot := loc
48
+ }
49
+ }
50
+ }
51
+
52
+ abstract contract DecryptionHandlerEip712Checker is EIP712, SignatureVerifier {
53
+ bytes32 constant DecryptionResultStructHash =
54
+ keccak256(
55
+ "DecryptionResult(bytes32 abiEncodedResult,uint256 requestId)"
56
+ );
57
+
58
+ function decryptionResultDigest(
59
+ DecryptionResult memory result
60
+ ) public view returns (bytes32) {
61
+ return
62
+ _hashTypedDataV4(
63
+ keccak256(
64
+ abi.encode(
65
+ DecryptionResultStructHash,
66
+ result.abiEncodedResult,
67
+ result.requestId
68
+ )
69
+ )
70
+ );
71
+ }
72
+
73
+ function isValidResult(
74
+ DecryptionResult memory result,
75
+ bytes memory signature
76
+ ) internal view returns (bool) {
77
+ return isValidSignature(decryptionResultDigest(result), signature);
78
+ }
79
+ }
80
+
81
+ abstract contract DecryptionHandler is
82
+ BaseAccessControlList,
83
+ EventCounter,
84
+ DecryptionHandlerStorage,
85
+ DecryptionHandlerEip712Checker
86
+ {
87
+ // The Covalidator callback machinery relies on these exact error signatures which are hardcoded in eth_callback.go
88
+ error RequestAlreadyFulfilled();
89
+ error RequestExpired();
90
+ error InvalidResultSignature();
91
+
92
+ event RequestFulfilled(
93
+ uint256 indexed requestId,
94
+ bool success,
95
+ uint256 eventId
96
+ );
97
+
98
+ /// @dev callback function MUST be of form `function name(bytes32 requestId, bytes32 result, bytes memory data)`
99
+ function requestDecryption(
100
+ // todo support multiple handles per request
101
+ bytes4 callbackSelector,
102
+ uint256 maxTimestamp,
103
+ bytes32 handle,
104
+ bytes memory data
105
+ ) external returns (uint256 requestId) {
106
+ require(
107
+ isAllowed(handle, msg.sender),
108
+ SenderNotAllowedForHandle(handle, msg.sender)
109
+ );
110
+
111
+ DecryptionStorage storage $ = getDecryptionStorage();
112
+ requestId = $.requestIdCounter++;
113
+ $.request[requestId] = Request({
114
+ maxTimestamp: maxTimestamp,
115
+ callbackContract: msg.sender,
116
+ callbackSelector: callbackSelector,
117
+ fulfilled: false,
118
+ data: data
119
+ });
120
+
121
+ emit DecryptionRequested(
122
+ requestId,
123
+ handle,
124
+ maxTimestamp,
125
+ getNewEventId()
126
+ );
127
+ }
128
+
129
+ function fulfillRequest(
130
+ DecryptionResult memory result,
131
+ bytes memory /* signature */
132
+ ) external {
133
+ DecryptionStorage storage $ = getDecryptionStorage();
134
+ Request storage request = $.request[result.requestId];
135
+
136
+ require(!request.fulfilled, RequestAlreadyFulfilled());
137
+ require(request.maxTimestamp >= block.timestamp, RequestExpired());
138
+ // require(isValidResult(result, signature), InvalidResultSignature());
139
+ require(
140
+ msg.sender == opSignerPubkeyAddress(),
141
+ InvalidResultSignature()
142
+ ); // todo beta implem so backend does not need to implement EIP712 signing yet
143
+ request.fulfilled = true;
144
+
145
+ bytes memory callbackCalldata = abi.encodeWithSelector(
146
+ request.callbackSelector,
147
+ result.requestId,
148
+ result.abiEncodedResult,
149
+ request.data
150
+ );
151
+ (bool success, ) = request.callbackContract.call(callbackCalldata);
152
+ emit RequestFulfilled(result.requestId, success, getNewEventId());
153
+ }
154
+
155
+ function getRequest(
156
+ uint256 requestId
157
+ ) external view returns (Request memory) {
158
+ return getDecryptionStorage().request[requestId];
159
+ }
160
+
161
+ function getRequestIdCounter() external view returns (uint256) {
162
+ return getDecryptionStorage().requestIdCounter;
163
+ }
164
+ }