Skip to content

Commit

Permalink
Merge branch 'master' of github.com:safe-global/safe-modules into ref…
Browse files Browse the repository at this point in the history
…actor-146-npm-workspaces
  • Loading branch information
akshay-ap committed Jan 11, 2024
2 parents 0e84234 + b37e394 commit f8151a4
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 28 deletions.
11 changes: 1 addition & 10 deletions modules/4337/.solcover.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
const fs = require('fs')
const path = require('path')

const testDir = path.join(__dirname, 'contracts', 'test')
const testContracts = fs
.readdirSync(testDir)
.filter((file) => file.endsWith('.sol'))
.map((file) => path.join('test', file))

module.exports = {
skipFiles: testContracts,
skipFiles: ['test'],
mocha: {
grep: '@skip-on-coverage', // Find everything with this tag
invert: true, // Run the grep's inverse set.
Expand Down
18 changes: 10 additions & 8 deletions modules/4337/contracts/test/SafeMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract SafeMock {

function _signatureSplit(bytes memory signature) internal pure returns (uint8 v, bytes32 r, bytes32 s) {
// solhint-disable-next-line no-inline-assembly
assembly {
assembly ("memory-safe") {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
Expand Down Expand Up @@ -71,22 +71,24 @@ contract SafeMock {
// solhint-disable-next-line payable-fallback,no-complex-fallback
fallback() external payable {
// solhint-disable-next-line no-inline-assembly
assembly {
assembly ("memory-safe") {
let handler := sload(fallbackHandler.slot)
if iszero(handler) {
return(0, 0)
}
calldatacopy(0, 0, calldatasize())

let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
// The msg.sender address is shifted to the left by 12 bytes to remove the padding
// Then the address without padding is stored right after the calldata
mstore(calldatasize(), shl(96, caller()))
mstore(add(ptr, calldatasize()), shl(96, caller()))
// Add 20 bytes for the address appended add the end
let success := call(gas(), handler, 0, 0, add(calldatasize(), 20), 0, 0)
returndatacopy(0, 0, returndatasize())
let success := call(gas(), handler, 0, ptr, add(calldatasize(), 20), 0, 0)
returndatacopy(ptr, 0, returndatasize())
if iszero(success) {
revert(0, returndatasize())
revert(ptr, returndatasize())
}
return(0, returndatasize())
return(ptr, returndatasize())
}
}

Expand Down
6 changes: 2 additions & 4 deletions modules/4337/contracts/test/SafeSignerLaunchpad.sol
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,10 @@ contract SafeSignerLaunchpad is IAccount, SafeStorage {

function _isContract(address account) internal view returns (bool) {
uint256 size;
/* solhint-disable no-inline-assembly */
/// @solidity memory-safe-assembly
assembly {
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
size := extcodesize(account)
}
/* solhint-enable no-inline-assembly */
return size > 0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/4337/contracts/test/TestEntryPoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ contract SenderCreator {
address factory = address(bytes20(initCode[0:20]));
bytes memory initCallData = initCode[20:];
bool success;
/* solhint-disable no-inline-assembly */
assembly {
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
success := call(gas(), factory, 0, add(initCallData, 0x20), mload(initCallData), 0, 32)
sender := mload(0)
}
Expand Down
6 changes: 2 additions & 4 deletions modules/4337/contracts/test/TestUniqueSigner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ contract TestUniqueSignerFactory is IUniqueSignerFactory {

function _hasNoCode(address account) internal view returns (bool) {
uint256 size;
/* solhint-disable no-inline-assembly */
/// @solidity memory-safe-assembly
assembly {
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
size := extcodesize(account)
}
/* solhint-enable no-inline-assembly */
return size == 0;
}
}
89 changes: 89 additions & 0 deletions modules/4337/contracts/test/WebAuthnSigner.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: LGPL-3.0-only
/* solhint-disable one-contract-per-file */
pragma solidity >=0.8.0;

import {FCL_WebAuthn} from "./FCL/FCL_Webauthn.sol";
import {IUniqueSignerFactory} from "./SafeSignerLaunchpad.sol";

struct SignatureData {
bytes authenticatorData;
bytes clientData;
uint256 challengeOffset;
uint256[2] rs;
}

function checkSignature(bytes memory data, bytes calldata signature, uint256 x, uint256 y) view returns (bytes4 magicValue) {
SignatureData calldata signaturePointer;
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
signaturePointer := signature.offset
}

if (
FCL_WebAuthn.checkSignature(
signaturePointer.authenticatorData,
0x01, // require user presence
signaturePointer.clientData,
keccak256(data),
signaturePointer.challengeOffset,
signaturePointer.rs,
x,
y
)
) {
magicValue = WebAuthnSigner.isValidSignature.selector;
}
}

contract WebAuthnSigner {
uint256 public immutable X;
uint256 public immutable Y;

constructor(uint256 x, uint256 y) {
X = x;
Y = y;
}

function isValidSignature(bytes memory data, bytes calldata signature) external view returns (bytes4 magicValue) {
return checkSignature(data, signature, X, Y);
}
}

contract WebAuthnSignerFactory is IUniqueSignerFactory {
function getSigner(bytes calldata data) public view returns (address signer) {
(uint256 x, uint256 y) = abi.decode(data, (uint256, uint256));
signer = _getSigner(x, y);
}

function createSigner(bytes calldata data) external returns (address signer) {
(uint256 x, uint256 y) = abi.decode(data, (uint256, uint256));
signer = _getSigner(x, y);
if (_hasNoCode(signer)) {
WebAuthnSigner created = new WebAuthnSigner{salt: bytes32(0)}(x, y);
require(address(created) == signer);
}
}

function isValidSignatureForSigner(
bytes memory data,
bytes calldata signature,
bytes calldata signerData
) external view override returns (bytes4 magicValue) {
(uint256 x, uint256 y) = abi.decode(signerData, (uint256, uint256));
magicValue = checkSignature(data, signature, x, y);
}

function _getSigner(uint256 x, uint256 y) internal view returns (address) {
bytes32 codeHash = keccak256(abi.encodePacked(type(WebAuthnSigner).creationCode, x, y));
return address(uint160(uint256(keccak256(abi.encodePacked(hex"ff", address(this), bytes32(0), codeHash)))));
}

function _hasNoCode(address account) internal view returns (bool) {
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
size := extcodesize(account)
}
return size == 0;
}
}
Loading

0 comments on commit f8151a4

Please sign in to comment.