Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #95 from getwax/55-move-low-level-webauthn-logic-t…
Browse files Browse the repository at this point in the history
…o-primitives-directory

55 move low level webauthn logic to primitives directory
  • Loading branch information
JohnGuilding authored Sep 29, 2023
2 parents c06b95a + 26222ff commit b84a486
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 16 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/primitives.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: primitives

on:
push:
branches:
- main
pull_request:
paths:
- primitives/**

defaults:
run:
working-directory: ./primitives

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "account-integrations/kernel/lib/kernel"]
path = account-integrations/kernel/lib/kernel
url = https://github.com/zerodevapp/kernel
[submodule "primitives/lib/forge-std"]
path = primitives/lib/forge-std
url = https://github.com/foundry-rs/forge-std
2 changes: 2 additions & 0 deletions account-integrations/safe/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ src = "src"
out = "out"
libs = ["lib"]

allow_paths = ["../../primitives"]

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
1 change: 1 addition & 0 deletions account-integrations/safe/lib/primitives
3 changes: 2 additions & 1 deletion account-integrations/safe/remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ forge-std/=lib/forge-std/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
account-abstraction/=lib/account-abstraction/
safe-contracts/=lib/safe-contracts/
safe-contracts/=lib/safe-contracts/
wax/primitives/=lib/primitives/
7 changes: 3 additions & 4 deletions account-integrations/safe/src/SafeWebAuthnPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ pragma solidity >=0.8.0 <0.9.0;

import {BaseAccount} from "account-abstraction/contracts/core/BaseAccount.sol";
import {IEntryPoint, UserOperation} from "account-abstraction/contracts/interfaces/IEntryPoint.sol";

import {FCL_WebAuthn} from "./lib/FCL_Webauthn.sol";
import {WebAuthn} from "wax/primitives/src/WebAuthn.sol";

interface ISafe {
function enableModule(address module) external;
Expand All @@ -17,7 +16,7 @@ interface ISafe {
) external returns (bool success);
}

contract SafeWebAuthnPlugin is BaseAccount {
contract SafeWebAuthnPlugin is BaseAccount, WebAuthn {
address public immutable myAddress;
address private immutable _entryPoint;
uint256[2] private _publicKey;
Expand Down Expand Up @@ -145,7 +144,7 @@ contract SafeWebAuthnPlugin is BaseAccount {
i += ((dataLen >> 5) + 1) << 5; // advance index (round up to next slot)
}

bool verified = FCL_WebAuthn.checkSignature(
bool verified = verifySignature(
authenticatorData,
s.authenticatorDataFlagMask,
clientData,
Expand Down
5 changes: 4 additions & 1 deletion account-integrations/safe/src/utils/TypechainTypesHelper.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

/** Helper file to import Safe contracts so that hardhat can generate required typechain types for integration tests */
/**
* Helper file to import contracts so that hardhat can generate required typechain types
*/
import {SimulateTxAccessor} from "safe-contracts/contracts/accessors/SimulateTxAccessor.sol";
import {SafeProxyFactory} from "safe-contracts/contracts/proxies/SafeProxyFactory.sol";
import {TokenCallbackHandler} from "safe-contracts/contracts/handler/TokenCallbackHandler.sol";
Expand All @@ -13,3 +15,4 @@ import {SignMessageLib} from "safe-contracts/contracts/libraries/SignMessageLib.
import {SafeL2} from "safe-contracts/contracts/SafeL2.sol";
import {Safe} from "safe-contracts/contracts/Safe.sol";
import {EntryPoint} from "account-abstraction/contracts/core/EntryPoint.sol";
import {WebAuthn} from "wax/primitives/src/WebAuthn.sol";
1 change: 0 additions & 1 deletion account-integrations/safe/test/forge/utils/TestHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.12;

import "forge-std/Test.sol";
import {EntryPoint, UserOperation} from "account-abstraction/contracts/core/EntryPoint.sol";
import {Webauthn} from "../../../src/WebAuthn.sol";

/* solhint-disable private-vars-leading-underscore */

Expand Down
14 changes: 14 additions & 0 deletions primitives/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
1 change: 1 addition & 0 deletions primitives/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Cryptographic Primitives

A place to integrate primitives into a modular verification components, and test in isolation.
After successful development here, the primitive can be integrated into smart accounts.
6 changes: 6 additions & 0 deletions primitives/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions primitives/lib/forge-std
Submodule forge-std added at 74cfb7
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ pragma solidity ^0.8.12;

import {FCL_WebAuthn} from "./lib/FCL_Webauthn.sol";

contract Webauthn {
contract WebAuthn {
function verifySignature(
bytes calldata authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes calldata clientData,
bytes32 messageHash,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] calldata signature,
uint256[2] calldata publicKey
) external returns (bool) {
return
) internal returns (bool verified) {
verified =
FCL_WebAuthn.checkSignature(
authenticatorData,
authenticatorDataFlagMask,
clientData,
messageHash,
clientChallenge,
clientChallengeDataOffset,
signature,
publicKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ pragma solidity ^0.8.12;

import "forge-std/Test.sol";
import {TestHelper} from "./utils/TestHelper.sol";
import {Webauthn} from "../../src/WebAuthn.sol";
import {WebAuthnHarness} from "./utils/WebAuthnHarness.sol";

/* solhint-disable func-name-mixedcase */

contract WebauthnTest is TestHelper {
constructor() TestHelper() {}

Webauthn public webauthn;
WebAuthnHarness public webauthn;

function setUp() public {
webauthn = new Webauthn();
webauthn = new WebAuthnHarness();
}

function test_verifySignature_ValidSignature() public {
Expand All @@ -29,7 +29,7 @@ contract WebauthnTest is TestHelper {
uint256[2] memory publicKey = getWebAuthnPublicKey();

// Act
bool verified = webauthn.verifySignature(
bool verified = webauthn.exposed_verifySignature(
authenticatorData,
authenticatorDataFlagMask,
clientData,
Expand Down
46 changes: 46 additions & 0 deletions primitives/test/utils/TestHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import "forge-std/Test.sol";
import {WebAuthn} from "../../src/WebAuthn.sol";

/* solhint-disable private-vars-leading-underscore */

abstract contract TestHelper is Test {
address internal constant ALICE = address(1);
address internal constant BOB = address(2);
address[] internal testAccounts = [ALICE, BOB];

constructor() {}

function getWebAuthnPublicKey() internal pure returns (uint256[2] memory publicKey) {
publicKey = [
114874632398302156264159990279427641021947882640101801130664833947273521181002,
32136952818958550240756825111900051564117520891182470183735244184006536587423
];
}

function getWebAuthnSignatureValues()
internal
pure
returns (
bytes memory authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes memory clientData,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] memory signature
)
{
authenticatorData = hex"f8e4b678e1c62f7355266eaa4dc1148573440937063a46d848da1e25babbd20b010000004d";
authenticatorDataFlagMask = 0x01;
clientData =
hex"7b2274797065223a22776562617574686e2e676574222c226368616c6c656e6765223a224e546f2d3161424547526e78786a6d6b61544865687972444e5833697a6c7169316f776d4f643955474a30222c226f726967696e223a2268747470733a2f2f66726573682e6c65646765722e636f6d222c2263726f73734f726967696e223a66616c73657d";
clientChallenge = hex"353a3ed5a0441919f1c639a46931de872ac3357de2ce5aa2d68c2639df54189d";
clientChallengeDataOffset = 36;
signature = [
45847212378479006099766816358861726414873720355505495069909394794949093093607,
55835259151215769394881684156457977412783812617123006733908193526332337539398
];
}
}
29 changes: 29 additions & 0 deletions primitives/test/utils/WebAuthnHarness.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import {WebAuthn} from "../../src/WebAuthn.sol";

/** Helper contract to expose internal functions for testing */
contract WebAuthnHarness is WebAuthn {
constructor() WebAuthn() {}

function exposed_verifySignature(
bytes calldata authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes calldata clientData,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] calldata signature,
uint256[2] calldata publicKey
) external returns (bool) {
return verifySignature(
authenticatorData,
authenticatorDataFlagMask,
clientData,
clientChallenge,
clientChallengeDataOffset,
signature,
publicKey
);
}
}

0 comments on commit b84a486

Please sign in to comment.