-
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.
Quark Nonce Manager [Renaming] (#207)
This patch mostly renames `QuarkStateManager` to `QuarkNonceManager`. Additionally, we start to use the term `submissionToken`, since it can cover first plays or replays both as "submissions". We choose the term `FREE` for the 0-token and `EXHAUSTED` for non-replayable tokens. Overall, this should provide a clearer and more consistent naming throughout the changeset.
- Loading branch information
Showing
35 changed files
with
589 additions
and
408 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,56 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
pragma solidity 0.8.23; | ||
|
||
import {IQuarkWallet} from "quark-core/src/interfaces/IQuarkWallet.sol"; | ||
|
||
/** | ||
* @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 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 = bytes32(uint256(0)); | ||
|
||
/// @notice A token that implies a Quark Operation is no longer replayable. | ||
bytes32 public constant EXHAUSTED = bytes32(type(uint256).max); | ||
|
||
/// @notice Mapping from nonces to last used submission token. | ||
mapping(address wallet => mapping(bytes32 nonce => bytes32 lastToken)) public nonceSubmissions; | ||
|
||
/** | ||
* @notice Returns the nonce token (last submission token) for a given nonce. For finalized scripts, this will be `uint256(-1)`. For unclaimed nonces, this will be `uint256(0)`. Otherwise, it will be the next value in the replay chain. | ||
* @param wallet The wallet for which to get the nonce token. | ||
* @param nonce The nonce for the given request. | ||
* @return submissionToken The last used submission token, or 0 if unused or -1 if finalized. | ||
*/ | ||
function getNonceSubmission(address wallet, bytes32 nonce) external view returns (bytes32 submissionToken) { | ||
return nonceSubmissions[wallet][nonce]; | ||
} | ||
|
||
/** | ||
* @notice Attempts a first or subsequent submission of a given nonce from a wallet. | ||
* @param nonce The nonce of the chain to submit. | ||
* @param submissionToken The submission token of the submission. For single-use operations, set `submissionToken` to `uint256(-1)`. For first-use replayable operations, set `submissionToken` = `nonce`. | ||
*/ | ||
function submitNonceToken(bytes32 nonce, bytes32 submissionToken) external { | ||
bytes32 lastTokenSubmission = nonceSubmissions[msg.sender][nonce]; | ||
if (lastTokenSubmission == EXHAUSTED) { | ||
revert NonReplayableNonce(msg.sender, nonce, submissionToken); | ||
} | ||
|
||
bool validFirstPlayOrReplay = | ||
lastTokenSubmission == FREE || keccak256(abi.encodePacked(submissionToken)) == lastTokenSubmission; | ||
if (!validFirstPlayOrReplay) { | ||
revert InvalidSubmissionToken(msg.sender, nonce, submissionToken); | ||
} | ||
|
||
nonceSubmissions[msg.sender][nonce] = submissionToken; | ||
emit NonceSubmitted(msg.sender, nonce, submissionToken); | ||
} | ||
} |
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 was deleted.
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
Oops, something went wrong.