Skip to content

Commit

Permalink
split p256 validator and webauthn validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ququzone committed Oct 7, 2023
1 parent c066535 commit a2c8e73
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 30 deletions.
19 changes: 1 addition & 18 deletions contracts/validators/p256/P256Validator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,7 @@ contract P256Validator is BaseValidator {
override
returns (uint256 validationData)
{
bytes memory sig;
bytes32 messageHash;
{
(
bytes memory realSig,
bytes memory authenticatorData,
string memory clientDataJSONPre,
string memory clientDataJSONPost
) = abi.decode(signature, (bytes, bytes, string, string));

string memory clientDataJSON =
string.concat(clientDataJSONPre, Base64.encode(bytes.concat(userOpHash)), clientDataJSONPost);
bytes32 clientDataHash = sha256(bytes(clientDataJSON));
messageHash = sha256(bytes.concat(authenticatorData, clientDataHash));
sig = realSig;
}

if (impl.validateSignature(messageHash, sig, pks[account])) {
if (impl.validateSignature(sha256(abi.encode(userOpHash)), signature, pks[account])) {
return 0;
}
return Contants.SIG_VALIDATION_FAILED;
Expand Down
60 changes: 60 additions & 0 deletions contracts/validators/p256/WebauthnValidator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "../../common/Contants.sol";
import "../BaseValidator.sol";
import "./ISecp256r1.sol";
import "./Base64.sol";

contract WebauthnValidator is BaseValidator {
string public constant override NAME = "Webauthn Validator";
string public constant override VERSION = "0.0.1";

event PkChanged(address indexed account, bytes oldPk, bytes newPk);

ISecp256r1 public immutable impl;
mapping(address => bytes) public pks;

constructor(ISecp256r1 _impl) {
impl = _impl;
}

function validateSignature(address account, bytes32 userOpHash, bytes calldata signature)
external
payable
override
returns (uint256 validationData)
{
bytes memory sig;
bytes32 messageHash;
{
(
bytes memory realSig,
bytes memory authenticatorData,
string memory clientDataJSONPre,
string memory clientDataJSONPost
) = abi.decode(signature, (bytes, bytes, string, string));

string memory clientDataJSON =
string.concat(clientDataJSONPre, Base64.encode(bytes.concat(userOpHash)), clientDataJSONPost);
bytes32 clientDataHash = sha256(bytes(clientDataJSON));
messageHash = sha256(bytes.concat(authenticatorData, clientDataHash));
sig = realSig;
}

if (impl.validateSignature(messageHash, sig, pks[account])) {
return 0;
}
return Contants.SIG_VALIDATION_FAILED;
}

function enable(bytes calldata data) external payable override {
bytes memory old = pks[msg.sender];
pks[msg.sender] = data;
emit PkChanged(msg.sender, old, data);
}

function validCaller(address, bytes calldata) external pure override returns (bool) {
revert("not implemented");
}
}
6 changes: 6 additions & 0 deletions deploy/validator_secp256r1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const deploy: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
log: true,
deterministicDeployment: true,
})
await deploy('WebauthnValidator', {
from: deployer,
args: [secp256r1.address],
log: true,
deterministicDeployment: true,
})
}

deploy.tags = ['secp256r1', 'validator']
Expand Down
24 changes: 12 additions & 12 deletions deployments/iotex_testnet/P256Validator.json

Large diffs are not rendered by default.

Loading

0 comments on commit a2c8e73

Please sign in to comment.