Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Nonce and N Checking #208

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ For example, let _Wallet A_ be the `executor` of _Wallet B_. Alice is the `signe

### Replayable Scripts

Replayable scripts are Quark scripts that can re-executed N times using the same signature of a _Quark operation_. More specifically, replayable scripts generate a nonce chain where the original signer knows a secret and hashes that secret N times. The signer can reveal a single "submission token" to replay the script which is easily verified on-chain. When the signer reveals the last submission token (the original secret) and submits it on-chain, no more replays are allowed (assuming the secret was chosen as a strong random). The signer can always cancel replays by submitting a nop non-replayable script on-chain or simply forgetting the secret. Note: the chain can be arbitrarily long and does not expend any additional gas on-chain for being longer (except if a script wants to know its position in the chain).
Replayable scripts are Quark scripts that can be re-executed N times using the same signature of a _Quark operation_. More specifically, replayable scripts generate a nonce chain where the original signer knows a secret and hashes that secret N times. The signer can reveal a single "submission token" to replay the script which is easily verified on-chain. When the signer reveals the last submission token (the original secret) and submits it on-chain, no more replays are allowed (assuming the secret was chosen as a strong random). The signer can always cancel replays by submitting a nop non-replayable script on-chain or simply forgetting the secret. Note: the chain can be arbitrarily long and does not expend any additional gas on-chain for being longer (except if a script wants to know its position in the chain).

```
Nonce hash chain:
Expand Down
4 changes: 2 additions & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[profile.default]
solc = "0.8.23"
evm_version = "paris"
solc = "0.8.27"
evm_version = "cancun"

libs = [ "./lib" ]

Expand Down
2 changes: 1 addition & 1 deletion script/DeployCodeJarFactory.s.sol
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;

import "forge-std/Script.sol";
import "forge-std/console.sol";
Expand Down
2 changes: 1 addition & 1 deletion script/DeployQuarkWalletFactory.s.sol
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;

import "forge-std/Script.sol";
import "forge-std/console.sol";
Expand Down
4 changes: 2 additions & 2 deletions src/codejar/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[profile.default]
solc = "0.8.23"
evm_version = "paris"
solc = "0.8.27"
evm_version = "cancun"

libs = [ "../../lib" ]

Expand Down
2 changes: 1 addition & 1 deletion src/codejar/src/CodeJar.sol
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
Expand Down
2 changes: 1 addition & 1 deletion src/codejar/src/CodeJarFactory.sol
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;

import {CodeJar} from "codejar/src/CodeJar.sol";

Expand Down
4 changes: 2 additions & 2 deletions src/quark-core-scripts/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[profile.default]
solc = "0.8.23"
evm_version = "paris"
solc = "0.8.27"
evm_version = "cancun"

libs = [ "../../lib" ]

Expand Down
41 changes: 41 additions & 0 deletions src/quark-core-scripts/src/Cancel.sol
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());
}
}
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/ConditionalMulticall.sol
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;

import "quark-core-scripts/src/lib/ConditionalChecker.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/Ethcall.sol
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 Ethcall Core Script
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/Multicall.sol
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 Multicall Core Script
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/Paycall.sol
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;

import "quark-core-scripts/src/vendor/chainlink/AggregatorV3Interface.sol";
import "openzeppelin/token/ERC20/utils/SafeERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/Quotecall.sol
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;

import "quark-core-scripts/src/vendor/chainlink/AggregatorV3Interface.sol";
import "openzeppelin/token/ERC20/utils/SafeERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/UniswapFlashLoan.sol
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;

import "openzeppelin/token/ERC20/utils/SafeERC20.sol";
import "v3-core/contracts/interfaces/IUniswapV3Pool.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/UniswapFlashSwapExactOut.sol
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;

import "openzeppelin/token/ERC20/utils/SafeERC20.sol";
import "v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/lib/ConditionalChecker.sol
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;

library ConditionalChecker {
enum CheckType {
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/lib/UniswapFactoryAddress.sol
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;

library UniswapFactoryAddress {
// Reference: https://docs.uniswap.org/contracts/v3/reference/deployments
Expand Down
2 changes: 1 addition & 1 deletion src/quark-core-scripts/src/vendor/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"newLines": 6,
"lines": [
" // SPDX-License-Identifier: GPL-2.0-or-later",
"-pragma solidity 0.8.23;",
"-pragma solidity 0.8.27;",
"+pragma solidity >=0.5.0;",
" ",
" /// @title Provides functions for deriving a pool address from the factory, tokens, and the fee",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.23;
pragma solidity 0.8.27;

/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee
library PoolAddress {
Expand Down
4 changes: 2 additions & 2 deletions src/quark-core/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[profile.default]
solc = "0.8.23"
evm_version = "paris"
solc = "0.8.27"
evm_version = "cancun"

libs = [ "../../lib" ]

Expand Down
46 changes: 35 additions & 11 deletions src/quark-core/src/QuarkNonceManager.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.23;
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, bool exhausted);
error InvalidNonce(address wallet, bytes32 nonce);
error InvalidSubmissionToken(address wallet, bytes32 nonce, bytes32 submissionToken);

event NonceSubmitted(address wallet, bytes32 nonce, bytes32 submissionToken);
event NonceCanceled(address wallet, bytes32 nonce);

/// @notice Represents the unclaimed bytes32 value.
bytes32 public constant FREE = bytes32(uint256(0));
bytes32 public constant FREE = QuarkNonceManagerMetadata.FREE;

/// @notice A token that implies a Quark Operation is no longer replayable.
bytes32 public constant EXHAUSTED = bytes32(type(uint256).max);
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 NonceCanceled(msg.sender, nonce);
}

/**
* @notice Attempts a first or subsequent submission of a given nonce from a wallet.
* @param nonce The nonce of the chain to submit.
Expand All @@ -34,21 +53,26 @@ contract QuarkNonceManager {
if (lastTokenSubmission == EXHAUSTED) {
revert NonReplayableNonce(msg.sender, nonce, submissionToken, true);
}
// Defense-in-deptch check for `submissionToken != FREE`
if (submissionToken == FREE) {
// 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 && (isReplayable ? submissionToken == nonce : submissionToken == EXHAUSTED);
bool validFirstPlay = lastTokenSubmission == FREE && submissionToken == nonce;

/* let validFirstPlayOrReplay = validFirstPlay or validReplay [with short-circuiting] */
bool validFirstPlayOrReplay =
validFirstPlay || keccak256(abi.encodePacked(submissionToken)) == lastTokenSubmission;

/* validToken = validFirstPlay or ( validReplay ); */
bool validToken = validFirstPlay || keccak256(abi.encodePacked(submissionToken)) == lastTokenSubmission;
if (!validToken) {
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 cancelations)
// 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);
}
Expand Down
Loading
Loading