-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create SafeWebAuthnSignerProxy (#370)
Fixes #312 Changes in PR: - [x] Create `SafeWebAuthnSignerSingleton`. This contract has no storage, no immutables, and no constants. - [x] Create `SafeWebAuthnSignerProxy` and corresponding proxy factory i.e. `SafeWebAuthnSignerProxyFactory` - Create immutable `singleton` contract when deploying proxy factory - `SafeWebAuthnSignerProxyFactory.isValidSignatureForSigner(...)` forwards to singleton contract instead directly calling `WebAuthn` library - `SafeWebAuthnSignerProxy` uses no storage to avoid 4337 storage access restriction violation. - Add unit test to verify that `SafeWebAuthnSignerProxy` forwards every call to singleton address - [x] Report gas usage metrics - [x] Update github workflow to execute benchmarking tests separately. (This is required to get accurate gas usage numbers) - [x] Update documentation - [x] Replace `SafeWebAuthnSigner` with `SafeWebAuthnSignerProxy`, same for factory contract - [x] Update all tests to use `SafeWebAuthnSignerProxy`. Hence, the large diff in this PR. Benchmark number from this PR: ``` Gas Benchmarking Proxy [@bench] SafeWebAuthnSignerProxy ⛽ deployment: 164012 ✔ Benchmark signer deployment cost (10[38](https://github.com/safe-global/safe-modules/actions/runs/8739938588/job/23982409583?pr=370#step:4:39)ms) ⛽ verification (FreshCryptoLib): 216248 ✔ Benchmark signer verification cost with FreshCryptoLib verifier (201ms) ⛽ verification (daimo-eth): 346151 ✔ Benchmark signer verification cost with daimo-eth verifier (186ms) ⛽ verification (Dummy): 14[40](https://github.com/safe-global/safe-modules/actions/runs/8739938588/job/23982409583?pr=370#step:4:41)2 ✔ Benchmark signer verification cost with Dummy verifier ``` Benchmark number from main: ``` Gas Benchmarking [@bench] SafeWebAuthnSigner ⛽ deployment: 491288 ✔ Benchmark signer deployment cost (654ms) ⛽ verification (FreshCryptoLib): 209630 ✔ Benchmark signer verification cost with FreshCryptoLib verifier (138ms) ⛽ verification (daimo-eth): 336881 ✔ Benchmark signer verification cost with daimo-eth verifier (108ms) ⛽ verification (Dummy): 11266 ✔ Benchmark signer verification cost with Dummy verifier ``` --------- Co-authored-by: Nicholas Rodrigues Lordello <nick@safe.global>
- Loading branch information
Showing
14 changed files
with
231 additions
and
96 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity >=0.8.0; | ||
import {P256} from "./libraries/WebAuthn.sol"; | ||
|
||
/** | ||
* @title WebAuthn Safe Signature Validator | ||
* @dev A proxy contracy that points to Safe signature validator implementation for a WebAuthn P-256 credential. | ||
* @custom:security-contact bounty@safe.global | ||
*/ | ||
contract SafeWebAuthnSignerProxy { | ||
/** | ||
* @notice The x coordinate of the P-256 public key of the WebAuthn credential. | ||
*/ | ||
uint256 internal immutable _X; | ||
/** | ||
* @notice The y coordinate of the P-256 public key of the WebAuthn credential. | ||
*/ | ||
uint256 internal immutable _Y; | ||
/** | ||
* @notice The P-256 verifiers used for ECDSA signature validation. | ||
*/ | ||
P256.Verifiers internal immutable _VERIFIERS; | ||
|
||
/** | ||
* @notice The contract address to which proxy contract forwards the call via delegatecall. | ||
*/ | ||
address internal immutable _SINGLETON; | ||
|
||
/** | ||
* @notice Creates a new WebAuthn Safe Signer Proxy. | ||
* @param singleton Address of the singleton contract to which the proxy forwards the call via delegatecall. | ||
* @param x The x coordinate of the P-256 public key of the WebAuthn credential. | ||
* @param y The y coordinate of the P-256 public key of the WebAuthn credential. | ||
* @param verifiers The P-256 verifiers used for ECDSA signature validation. | ||
*/ | ||
constructor(address singleton, uint256 x, uint256 y, P256.Verifiers verifiers) { | ||
_SINGLETON = singleton; | ||
_X = x; | ||
_Y = y; | ||
_VERIFIERS = verifiers; | ||
} | ||
|
||
/** | ||
* @dev Fallback function forwards all transactions and returns all received return data. | ||
*/ | ||
// solhint-disable-next-line no-complex-fallback | ||
fallback() external payable { | ||
bytes memory data = abi.encodePacked(msg.data, _X, _Y, _VERIFIERS); | ||
address singleton = _SINGLETON; | ||
|
||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
let dataSize := mload(data) | ||
let dataLocation := add(data, 0x20) | ||
|
||
let success := delegatecall(gas(), singleton, dataLocation, dataSize, 0, 0) | ||
returndatacopy(0, 0, returndatasize()) | ||
if iszero(success) { | ||
revert(0, returndatasize()) | ||
} | ||
return(0, returndatasize()) | ||
} | ||
} | ||
} |
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,35 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity >=0.8.0; | ||
|
||
import {SignatureValidator} from "./base/SignatureValidator.sol"; | ||
import {P256, WebAuthn} from "./libraries/WebAuthn.sol"; | ||
/** | ||
* @title WebAuthn Safe Signature Validator Singleton | ||
* @dev A contract that represents a WebAuthn signer. | ||
* @custom:security-contact bounty@safe.global | ||
*/ | ||
contract SafeWebAuthnSignerSingleton is SignatureValidator { | ||
/** | ||
* @inheritdoc SignatureValidator | ||
*/ | ||
function _verifySignature(bytes32 message, bytes calldata signature) internal view virtual override returns (bool success) { | ||
(uint256 x, uint256 y, P256.Verifiers verifiers) = getConfiguration(); | ||
|
||
success = WebAuthn.verifySignature(message, signature, WebAuthn.USER_VERIFICATION, x, y, verifiers); | ||
} | ||
|
||
/** | ||
* @notice Returns the x coordinate, y coordinate, and P-256 verifiers used for ECDSA signature validation. The values are expected to be passed by the SafeWebAuthnSignerProxy contract in msg.data. | ||
* @return x The x coordinate of the P-256 public key. | ||
* @return y The y coordinate of the P-256 public key. | ||
* @return verifiers The P-256 verifiers. | ||
*/ | ||
function getConfiguration() public pure returns (uint256 x, uint256 y, P256.Verifiers verifiers) { | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly ("memory-safe") { | ||
x := calldataload(sub(calldatasize(), 88)) | ||
y := calldataload(sub(calldatasize(), 56)) | ||
verifiers := shr(64, calldataload(sub(calldatasize(), 24))) | ||
} | ||
} | ||
} |
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.