forked from ensdomains/evmgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from unruggable-labs/debug-verifier
Add TrustedVerifier, DoubleNitroVerifier, better npm package
- Loading branch information
Showing
59 changed files
with
1,253 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract ReadBytesAt { | ||
// NOTE: pure seems like the wrong mutability annotation | ||
function readBytesAt(uint256 slot) external pure returns (bytes memory) { | ||
bytes storage v; | ||
assembly { | ||
v.slot := slot | ||
} | ||
return v; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import {IGatewayVerifier} from './IGatewayVerifier.sol'; | ||
import {IVerifierHooks} from './IVerifierHooks.sol'; | ||
import {GatewayRequest, GatewayVM, ProofSequence} from './GatewayVM.sol'; | ||
import {ECDSA} from '@openzeppelin/contracts/utils/cryptography/ECDSA.sol'; | ||
import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol'; | ||
|
||
contract TrustedVerifier is IGatewayVerifier { | ||
event GatewayChanged(address indexed fetcher); | ||
|
||
struct Config { | ||
mapping(address signer => bool) signers; | ||
string[] urls; | ||
uint256 expSec; | ||
IVerifierHooks hooks; | ||
} | ||
|
||
mapping(address fetcher => Config) _configs; | ||
|
||
modifier _canModifyFetcher(address op, address fetcher) { | ||
if (fetcher == op) { | ||
require(fetcher.code.length != 0, 'Trusted: not code'); | ||
} else { | ||
try Ownable(fetcher).owner() returns (address a) { | ||
require(a == op, 'Trusted: not owner'); | ||
} catch { | ||
revert('Trusted: not Ownable'); | ||
} | ||
} | ||
_; | ||
} | ||
|
||
function setConfig( | ||
address fetcher, | ||
string[] memory urls, | ||
uint256 expSec, | ||
IVerifierHooks hooks | ||
) external _canModifyFetcher(msg.sender, fetcher) { | ||
Config storage c = _configs[fetcher]; | ||
c.urls = urls; | ||
c.expSec = expSec; | ||
c.hooks = hooks; | ||
emit GatewayChanged(fetcher); | ||
} | ||
|
||
function setSigner( | ||
address fetcher, | ||
address signer, | ||
bool allow | ||
) external _canModifyFetcher(msg.sender, fetcher) { | ||
_configs[fetcher].signers[signer] = allow; | ||
emit GatewayChanged(fetcher); | ||
} | ||
|
||
function getConfig( | ||
address sender | ||
) | ||
external | ||
view | ||
returns (string[] memory urls, uint256 expSec, IVerifierHooks hooks) | ||
{ | ||
Config storage c = _configs[sender]; | ||
urls = c.urls; | ||
expSec = c.expSec; | ||
hooks = c.hooks; | ||
} | ||
|
||
function isSigner( | ||
address sender, | ||
address signer | ||
) external view returns (bool) { | ||
return _configs[sender].signers[signer]; | ||
} | ||
|
||
function gatewayURLs() external view returns (string[] memory) { | ||
return _configs[msg.sender].urls; | ||
} | ||
|
||
function getLatestContext() external view returns (bytes memory) { | ||
return abi.encode(block.timestamp, msg.sender); | ||
} | ||
|
||
struct GatewayProof { | ||
bytes signature; | ||
uint64 signedAt; | ||
bytes32 stateRoot; | ||
bytes[] proofs; | ||
bytes order; | ||
} | ||
|
||
function getStorageValues( | ||
bytes memory context, | ||
GatewayRequest memory req, | ||
bytes memory proof | ||
) external view returns (bytes[] memory, uint8 exitCode) { | ||
(uint256 t, address sender) = abi.decode(context, (uint256, address)); | ||
GatewayProof memory p = abi.decode(proof, (GatewayProof)); | ||
bytes32 hash = keccak256( | ||
// https://github.com/ethereum/eips/issues/191 | ||
abi.encodePacked( | ||
hex'1900', // magic + version(0) | ||
address(0), | ||
p.signedAt, | ||
p.stateRoot | ||
) | ||
); | ||
address signer = ECDSA.recover(hash, p.signature); | ||
Config storage c = _configs[sender]; | ||
require(c.signers[signer], 'Trusted: signer'); | ||
uint256 dt = p.signedAt > t ? p.signedAt - t : t - p.signedAt; | ||
require(dt <= c.expSec, 'Trusted: expired'); | ||
return | ||
GatewayVM.evalRequest( | ||
req, | ||
ProofSequence(0, p.stateRoot, p.proofs, p.order, c.hooks) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import {NitroVerifier} from './NitroVerifier.sol'; | ||
import {IVerifierHooks} from '../IVerifierHooks.sol'; | ||
import {GatewayRequest, GatewayVM, ProofSequence} from '../GatewayVM.sol'; | ||
import {RLPReader, RLPReaderExt} from '../RLPReaderExt.sol'; | ||
import {IRollupCore, Node} from './IRollupCore.sol'; | ||
|
||
contract DoubleNitroVerifier is NitroVerifier { | ||
address immutable _rollup2; | ||
GatewayRequest _nodeRequest; | ||
|
||
constructor( | ||
string[] memory urls, | ||
uint256 window, | ||
IVerifierHooks hooks, | ||
IRollupCore rollup, | ||
uint256 minBlocks, | ||
address rollup2, | ||
GatewayRequest memory nodeRequest | ||
) NitroVerifier(urls, window, hooks, rollup, minBlocks) { | ||
_rollup2 = rollup2; | ||
_nodeRequest = nodeRequest; | ||
} | ||
|
||
struct GatewayProof2 { | ||
uint64 nodeNum; | ||
bytes32 sendRoot; | ||
bytes rlpEncodedBlock; | ||
bytes[] proofs; | ||
bytes order; | ||
bytes32 sendRoot2; | ||
bytes rlpEncodedBlock2; | ||
bytes[] proofs2; | ||
bytes order2; | ||
} | ||
|
||
function getStorageValues( | ||
bytes memory context, | ||
GatewayRequest memory req, | ||
bytes memory proof | ||
) external view override returns (bytes[] memory, uint8 exitCode) { | ||
GatewayProof2 memory p = abi.decode(proof, (GatewayProof2)); | ||
Node memory node = _verifyNode(context, p.nodeNum); | ||
bytes32 stateRoot = _verifyStateRoot( | ||
node.confirmData, | ||
p.rlpEncodedBlock, | ||
p.sendRoot | ||
); | ||
(bytes[] memory outputs, ) = GatewayVM.evalRequest( | ||
_nodeRequest, | ||
ProofSequence(0, stateRoot, p.proofs, p.order, _hooks) | ||
); | ||
// outputs[0] = node | ||
// outputs[1] = confirmData | ||
// outputs[2] = createdAtBlock (not used yet) | ||
bytes32 stateRoot2 = _verifyStateRoot( | ||
bytes32(outputs[1]), // confirmData | ||
p.rlpEncodedBlock2, | ||
p.sendRoot2 | ||
); | ||
return | ||
GatewayVM.evalRequest( | ||
req, | ||
ProofSequence(0, stateRoot2, p.proofs2, p.order2, _hooks) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.