@inco/lightning 0.6.1 → 0.6.4

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 (43) hide show
  1. package/package.json +1 -1
  2. package/src/DeployUtils.sol +1 -1
  3. package/src/Errors.sol +1 -1
  4. package/src/Lib.alphanet.sol +24 -3
  5. package/src/Lib.demonet.sol +24 -3
  6. package/src/Lib.devnet.sol +24 -3
  7. package/src/Lib.sol +24 -3
  8. package/src/Lib.template.sol +97 -20
  9. package/src/Lib.testnet.sol +24 -3
  10. package/src/libs/incoLightning_alphanet_v0_297966649.sol +24 -3
  11. package/src/libs/incoLightning_alphanet_v1_725458969.sol +24 -3
  12. package/src/libs/incoLightning_demonet_v0_863421733.sol +24 -3
  13. package/src/libs/incoLightning_devnet_v0_340846814.sol +24 -3
  14. package/src/libs/incoLightning_devnet_v1_904635675.sol +24 -3
  15. package/src/libs/incoLightning_testnet_v0_183408998.sol +24 -3
  16. package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +1 -1
  17. package/src/lightning-parts/EncryptedInput.sol +52 -16
  18. package/src/lightning-parts/EncryptedOperations.sol +86 -90
  19. package/src/lightning-parts/TEELifecycle.sol +153 -66
  20. package/src/lightning-parts/TEELifecycle.types.sol +7 -0
  21. package/src/lightning-parts/interfaces/ITEELifecycle.sol +13 -2
  22. package/src/lightning-parts/primitives/HandleGeneration.sol +23 -57
  23. package/src/lightning-parts/primitives/interfaces/IHandleGeneration.sol +3 -34
  24. package/src/lightning-parts/test/HandleMetadata.t.sol +41 -4
  25. package/src/lightning-parts/test/InputsFee.t.sol +14 -21
  26. package/src/lightning-parts/test/TestDecryptionAttestationInSynchronousFlow.t.sol +6 -2
  27. package/src/shared/IOwnable.sol +10 -0
  28. package/src/shared/IUUPSUpgradable.sol +10 -0
  29. package/src/shared/JsonUtils.sol +16 -0
  30. package/src/shared/TestUtils.sol +50 -0
  31. package/src/shared/TypeUtils.sol +12 -0
  32. package/src/test/FakeIncoInfra/FakeComputeServer.sol +1 -1
  33. package/src/test/FakeIncoInfra/FakeDecryptionAttester.sol +36 -34
  34. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +33 -18
  35. package/src/test/FakeIncoInfra/KVStore.sol +1 -1
  36. package/src/test/FakeIncoInfra/MockOpHandler.sol +5 -5
  37. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +1 -1
  38. package/src/test/IncoTest.sol +1 -1
  39. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +73 -20
  40. package/src/test/TestAddTwo.t.sol +1 -1
  41. package/src/test/TestFakeInfra.t.sol +13 -3
  42. package/src/version/Version.sol +4 -0
  43. package/src/version/interfaces/IVersion.sol +1 -0
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -388,17 +388,38 @@ library e {
388
388
  return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
389
389
  }
390
390
 
391
- /// @dev costs the inco fee
391
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
392
+ /// @dev costs the inco fee
393
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
394
+ return newEuint256(ciphertext, msg.sender);
395
+ }
396
+
397
+ /// @notice Creates a new encrypted uint256 for the given user.
398
+ /// @dev costs the inco fee
392
399
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
393
400
  return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
394
401
  }
395
402
 
396
- /// @dev costs the inco fee
403
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
404
+ /// @dev costs the inco fee
405
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
406
+ return newEbool(ciphertext, msg.sender);
407
+ }
408
+
409
+ /// @notice Creates a new encrypted bool for the given user.
410
+ /// @dev costs the inco fee
397
411
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
398
412
  return inco.newEbool{value: inco.getFee()}(ciphertext, user);
399
413
  }
400
414
 
401
- /// @dev costs the inco fee
415
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
416
+ /// @dev costs the inco fee
417
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
418
+ return newEaddress(ciphertext, msg.sender);
419
+ }
420
+
421
+ /// @notice Creates a new encrypted address for the given user.
422
+ /// @dev costs the inco fee
402
423
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
403
424
  return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
404
425
  }
@@ -74,7 +74,7 @@ contract TestAdvancedAccessControl is IncoTest {
74
74
  super.setUp();
75
75
  someContract = new SomeContractWithConfidentialData();
76
76
  vm.deal(address(someContract), 100 ether);
77
- bytes memory secretCt = fakePrepareEuint256Ciphertext(42);
77
+ bytes memory secretCt = fakePrepareEuint256Ciphertext(42, alice, address(someContract));
78
78
  vm.prank(alice);
79
79
  someContract.saveAPersonalSecret(secretCt);
80
80
  secretHandle = euint256.unwrap(someContract.secret());
@@ -8,6 +8,18 @@ import {IEncryptedInput} from "./interfaces/IEncryptedInput.sol";
8
8
  import {HandleAlreadyExists} from "../Errors.sol";
9
9
  import {Fee} from "./Fee.sol";
10
10
 
11
+ // This error ordinarily indicates an unintentional mismatch between the client-side context and the ambient on-chain
12
+ // context
13
+ error ExternalHandleDoesNotMatchComputedHandle(
14
+ bytes32 externalHandle,
15
+ bytes32 computedHandle,
16
+ // These values are provided to help the caller debug a mismatch, they are the inputs to the context of computedHandle
17
+ uint256 chainId,
18
+ address aclAddress,
19
+ address userAddress,
20
+ address contractAddress
21
+ );
22
+
11
23
  abstract contract EncryptedInput is
12
24
  IEncryptedInput,
13
25
  BaseAccessControlList,
@@ -18,33 +30,32 @@ abstract contract EncryptedInput is
18
30
  bytes32 indexed result,
19
31
  address indexed contractAddress,
20
32
  address indexed user,
21
- ETypes inputType,
22
33
  bytes ciphertext,
23
34
  uint256 eventId
24
35
  );
25
36
 
26
37
  function newEuint256(
27
- bytes memory ciphertext,
38
+ bytes memory input,
28
39
  address user
29
40
  ) external payable returns (euint256 newValue) {
30
- return euint256.wrap(newInput(ciphertext, user, ETypes.Uint256));
41
+ return euint256.wrap(newInput(input, user, ETypes.Uint256));
31
42
  }
32
43
 
33
44
  function newEbool(
34
- bytes memory ciphertext,
45
+ bytes memory input,
35
46
  address user
36
47
  ) external payable returns (ebool newValue) {
37
- return ebool.wrap(newInput(ciphertext, user, ETypes.Bool));
48
+ return ebool.wrap(newInput(input, user, ETypes.Bool));
38
49
  }
39
50
 
40
51
  function newEaddress(
41
- bytes memory ciphertext,
52
+ bytes memory input,
42
53
  address user
43
54
  ) external payable returns (eaddress newValue) {
44
55
  return
45
56
  eaddress.wrap(
46
- newInput(ciphertext, user, ETypes.AddressOrUint160OrBytes20)
47
- );
57
+ newInput(input, user, ETypes.AddressOrUint160OrBytes20)
58
+ );
48
59
  }
49
60
 
50
61
  function newInput(
@@ -63,20 +74,45 @@ abstract contract EncryptedInput is
63
74
  newHandle = _newInput(ciphertext, user, inputType);
64
75
  }
65
76
 
77
+ /// @notice Creates a new input with a prepended handle as a checksum.
78
+ /// @param input The input that contains the handle prepended to the ciphertext.
79
+ /// @param user The user address associated with the input.
66
80
  function _newInput(
67
- bytes memory ciphertext,
81
+ bytes memory input,
68
82
  address user,
69
83
  ETypes inputType
70
- ) private returns (bytes32 newHandle) {
71
- newHandle = getInputHandle(ciphertext, user, msg.sender, inputType);
84
+ ) private returns (bytes32 handle) {
85
+ // Since there is no sensible way to handle abi.decode errors (https://github.com/argotorg/solidity/issues/10381)
86
+ // at least fail early on a conservative minimum length
87
+ require(input.length >= 64, "Input too short, should be at least 64 bytes");
88
+ // Remove external handle prepended to input as a checksum
89
+ (bytes32 externalHandle, bytes memory ciphertext) = abi.decode(input, (bytes32, bytes));
90
+ handle = getInputHandle(ciphertext, user, msg.sender, inputType);
91
+ require(
92
+ handle == externalHandle,
93
+ ExternalHandleDoesNotMatchComputedHandle({
94
+ externalHandle: externalHandle,
95
+ computedHandle: handle,
96
+ chainId: block.chainid,
97
+ aclAddress: address(this),
98
+ userAddress: user,
99
+ contractAddress: msg.sender
100
+ })
101
+ );
72
102
  // We assume that providing the same handle (which via HADU implies same plaintext, same context, and same
73
103
  // instance of encryption)
74
- require(!isAllowed(newHandle, user), HandleAlreadyExists());
104
+ require(!isAllowed(handle, user), HandleAlreadyExists(handle));
75
105
  // We allow to user since this is harmless and it is convenient to use the allow mapping to track existing
76
- allowInternal(newHandle, user);
77
- allowTransientInternal(newHandle, msg.sender);
106
+ allowInternal(handle, user);
107
+ allowTransientInternal(handle, msg.sender);
78
108
  uint256 id = getNextEventId();
79
- emit NewInput(newHandle, msg.sender, user, inputType, ciphertext, id);
80
- setDigest(abi.encodePacked(newHandle, id));
109
+ emit NewInput({
110
+ result: handle,
111
+ contractAddress: msg.sender,
112
+ user: user,
113
+ ciphertext: ciphertext,
114
+ eventId: id
115
+ });
116
+ setDigest(abi.encodePacked(handle, id));
81
117
  }
82
118
  }