-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a moderately-sized redesign of Quark v1. The main goal of this redesign is to improve the security of replayable operations by making them only submittable by submitters that have access to a secret submission token. A secondary goal of this redesign is to simplify a bunch of the flows to reduce gas overhead and code complexity. Changes: - Introduce new replay mechanism that requires a submitter to provide a `submissionToken`. Previously, replayable operations were submittable by any address - Remove isolated storage in the `QuarkStateManager`, which allows us to remove much of the complicated codepaths related to `setActiveNonceAndCallback`, `activeNonceScript`, `nonceScriptAddress`, and `walletStorage` - Rename `QuarkStateManager` to `QuarkNonceManager` since its job is now to only manage nonces - Adjust `QuarkOperation` struct to have a `isReplayable` field - Bump up Solidity version to 0.8.27 and EVM version to `cancun` - Use `TSTORE/TLOAD` to save gas --------- Co-authored-by: Geoff Hayes <[email protected]>
- Loading branch information
1 parent
da0cfcd
commit 1afeb4e
Showing
100 changed files
with
3,079 additions
and
1,185 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
pragma solidity 0.8.23; | ||
pragma solidity 0.8.27; | ||
|
||
/** | ||
* @title Code Jar | ||
|
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,41 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
pragma solidity 0.8.27; | ||
|
||
import {IQuarkWallet} from "quark-core/src/QuarkWallet.sol"; | ||
import {QuarkNonceManager} from "quark-core/src/QuarkNonceManager.sol"; | ||
|
||
/** | ||
* @title Cancel Core Script | ||
* @notice Core transaction script that can be used to cancel quark operations. | ||
* @author Legend Labs, Inc. | ||
*/ | ||
contract Cancel { | ||
/** | ||
* @notice May cancel a script by being run as a no-op (no operation). | ||
*/ | ||
function nop() external pure {} | ||
|
||
/** | ||
* @notice Cancels a script by calling into nonce manager to cancel the script's nonce. | ||
* @param nonce The nonce of the quark operation to cancel (exhaust) | ||
*/ | ||
function cancel(bytes32 nonce) external { | ||
nonceManager().cancel(nonce); | ||
} | ||
|
||
/** | ||
* @notice Cancels many scripts by calling into nonce manager to cancel each script's nonce. | ||
* @param nonces A list of nonces of the quark operations to cancel (exhaust) | ||
*/ | ||
function cancelMany(bytes32[] calldata nonces) external { | ||
QuarkNonceManager manager = nonceManager(); | ||
for (uint256 i = 0; i < nonces.length; ++i) { | ||
bytes32 nonce = nonces[i]; | ||
manager.cancel(nonce); | ||
} | ||
} | ||
|
||
function nonceManager() internal view returns (QuarkNonceManager) { | ||
return QuarkNonceManager(IQuarkWallet(address(this)).nonceManager()); | ||
} | ||
} |
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
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
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
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
2 changes: 1 addition & 1 deletion
2
src/quark-core-scripts/src/vendor/uniswap_v3_periphery/PoolAddress.sol
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,78 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
pragma solidity 0.8.27; | ||
|
||
import {IQuarkWallet} from "quark-core/src/interfaces/IQuarkWallet.sol"; | ||
|
||
library QuarkNonceManagerMetadata { | ||
/// @notice Represents the unclaimed bytes32 value. | ||
bytes32 internal constant FREE = bytes32(uint256(0)); | ||
|
||
/// @notice A token that implies a Quark Operation is no longer replayable. | ||
bytes32 internal constant EXHAUSTED = bytes32(type(uint256).max); | ||
} | ||
|
||
/** | ||
* @title Quark Nonce Manager | ||
* @notice Contract for managing nonces for Quark wallets | ||
* @author Compound Labs, Inc. | ||
*/ | ||
contract QuarkNonceManager { | ||
error NonReplayableNonce(address wallet, bytes32 nonce, bytes32 submissionToken); | ||
error InvalidNonce(address wallet, bytes32 nonce); | ||
error InvalidSubmissionToken(address wallet, bytes32 nonce, bytes32 submissionToken); | ||
|
||
event NonceSubmitted(address wallet, bytes32 nonce, bytes32 submissionToken); | ||
|
||
/// @notice Represents the unclaimed bytes32 value. | ||
bytes32 public constant FREE = QuarkNonceManagerMetadata.FREE; | ||
|
||
/// @notice A token that implies a Quark Operation is no longer replayable. | ||
bytes32 public constant EXHAUSTED = QuarkNonceManagerMetadata.EXHAUSTED; | ||
|
||
/// @notice Mapping from nonces to last used submission token. | ||
mapping(address wallet => mapping(bytes32 nonce => bytes32 lastToken)) public submissions; | ||
|
||
/** | ||
* @notice Ensures a given nonce is canceled for sender. An un-used nonce will not be usable in the future, and a replayable nonce will no longer be replayable. This is a no-op for already canceled operations. | ||
* @param nonce The nonce of the chain to cancel. | ||
*/ | ||
function cancel(bytes32 nonce) external { | ||
submissions[msg.sender][nonce] = EXHAUSTED; | ||
emit NonceSubmitted(msg.sender, nonce, EXHAUSTED); | ||
} | ||
|
||
/** | ||
* @notice Attempts a first or subsequent submission of a given nonce from a wallet. | ||
* @param nonce The nonce of the chain to submit. | ||
* @param isReplayable True only if the operation has been marked as replayable. Otherwise, submission token must be the EXHAUSTED value. | ||
* @param submissionToken The token for this submission. For single-use operations, set `submissionToken` to `uint256(-1)`. For first-use replayable operations, set `submissionToken` = `nonce`. Otherwise, the next submission token from the nonce-chain. | ||
*/ | ||
function submit(bytes32 nonce, bool isReplayable, bytes32 submissionToken) external { | ||
bytes32 lastTokenSubmission = submissions[msg.sender][nonce]; | ||
if (lastTokenSubmission == EXHAUSTED) { | ||
revert NonReplayableNonce(msg.sender, nonce, submissionToken); | ||
} | ||
// Defense-in-depth check for `nonce != FREE` and `nonce != EXHAUSTED` | ||
if (nonce == FREE || nonce == EXHAUSTED) { | ||
revert InvalidNonce(msg.sender, nonce); | ||
} | ||
// Defense-in-depth check for `submissionToken != FREE` and `submissionToken != EXHAUSTED` | ||
if (submissionToken == FREE || submissionToken == EXHAUSTED) { | ||
revert InvalidSubmissionToken(msg.sender, nonce, submissionToken); | ||
} | ||
|
||
bool validFirstPlay = lastTokenSubmission == FREE && submissionToken == nonce; | ||
|
||
/* let validFirstPlayOrReplay = validFirstPlay or validReplay [with short-circuiting] */ | ||
bool validFirstPlayOrReplay = | ||
validFirstPlay || keccak256(abi.encodePacked(submissionToken)) == lastTokenSubmission; | ||
|
||
if (!validFirstPlayOrReplay) { | ||
revert InvalidSubmissionToken(msg.sender, nonce, submissionToken); | ||
} | ||
|
||
// Note: even with a valid submission token, we always set non-replayables to exhausted (e.g. for cancellations) | ||
submissions[msg.sender][nonce] = isReplayable ? submissionToken : EXHAUSTED; | ||
emit NonceSubmitted(msg.sender, nonce, submissionToken); | ||
} | ||
} |
Oops, something went wrong.