Skip to content

Commit

Permalink
Update ECDSAOwnedDKIMRegistry.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Oct 21, 2023
1 parent 5f9d19c commit e50d626
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
18 changes: 11 additions & 7 deletions packages/contracts/src/utils/ECDSAOwnedDKIMRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,32 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
using Strings for *;
using ECDSA for *;

mapping(string => uint256) public nonceOfDomain;
// mapping(string => uint256) public nonceOfDomain;
uint signValidityDuration;
DKIMRegistry public dkimRegistry;
address public signer;

constructor(address _signer) {
constructor(address _signer, uint _signValidityDuration) {
dkimRegistry = new DKIMRegistry();
signer = _signer;
signValidityDuration = _signValidityDuration;
}

function setDKIMPublicKeyHash(
string memory selector,
string memory domainName,
uint timestamp,
bytes32 publicKeyHash,
bytes memory signature
) public {
require(bytes(selector).length != 0, "Invalid selector");
require(bytes(domainName).length != 0, "Invalid domain name");
require(publicKeyHash != bytes32(0), "Invalid public key hash");
string memory signedMsg = computeSignedMsg(selector, domainName, publicKeyHash);
require(block.timestamp - timestamp <= signValidityDuration, "Signature expired");
string memory signedMsg = computeSignedMsg(selector, domainName, publicKeyHash, timestamp);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
address recoveredSigner = digest.recover(signature);
require(recoveredSigner == signer, "Invalid signature");
nonceOfDomain[domainName]++;
dkimRegistry.setDKIMPublicKeyHash(domainName, publicKeyHash);
}

Expand All @@ -44,7 +47,8 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
function computeSignedMsg(
string memory selector,
string memory domainName,
bytes32 publicKeyHash
bytes32 publicKeyHash,
uint256 timestamp
) public view returns (string memory) {
return string.concat(
"chain_id=",
Expand All @@ -53,8 +57,8 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
selector,
";domain=",
domainName,
";nonce=",
nonceOfDomain[domainName].toHexString(32),
";timestamp=",
timestamp.toString(),
";public_key_hash=",
uint256(publicKeyHash).toHexString(),
";"
Expand Down
91 changes: 46 additions & 45 deletions packages/contracts/test/ECDSAOwnedDKIMRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,128 +15,129 @@ contract ECDSAOwnedDKIMRegistryTest is Test {

string public selector = "12345";
string public domainName = "example.com";
uint public signValidityDuration = 1 days;
bytes32 public publicKeyHash = bytes32(uint256(1));

function setUp() public {
address signer = vm.addr(1);
registry = new ECDSAOwnedDKIMRegistry(signer);
registry = new ECDSAOwnedDKIMRegistry(signer,signValidityDuration);
}

function test_SetDKIMPublicKeyHash() public {
vm.chainId(1);
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash);
uint timestamp = block.timestamp;
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash, timestamp);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, digest);
bytes memory signature = abi.encodePacked(r, s, v);
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
registry.setDKIMPublicKeyHash(
selector,
domainName,
timestamp,
publicKeyHash,
signature
);
require(registry.getDKIMPublicKeyHash(domainName) == publicKeyHash, "Invalid public key hash");
require(registry.nonceOfDomain(domainName) == 1, "Invalid nonce");
}

function test_SetDKIMPublicKeyHashMultiDomain() public {
vm.chainId(1);
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash);
uint timestamp = block.timestamp;
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash, timestamp);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, digest);
bytes memory signature = abi.encodePacked(r, s, v);
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
registry.setDKIMPublicKeyHash(
selector,
domainName,
timestamp,
publicKeyHash,
signature
);
require(registry.getDKIMPublicKeyHash(domainName) == publicKeyHash, "Invalid public key hash");
require(registry.nonceOfDomain(domainName) == 1, "Invalid nonce");

selector = "67890";
domainName = "example2.com";
timestamp = block.timestamp;
publicKeyHash = bytes32(uint256(2));
signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash);
signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash, timestamp);
digest = bytes(signedMsg).toEthSignedMessageHash();
(v, r, s) = vm.sign(1, digest);
signature = abi.encodePacked(r, s, v);
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
registry.setDKIMPublicKeyHash(
selector,
domainName,
timestamp,
publicKeyHash,
signature
);
require(registry.getDKIMPublicKeyHash(domainName) == publicKeyHash, "Invalid public key hash");
require(registry.nonceOfDomain(domainName) == 1, "Invalid nonce");
}

function test_RevertIfNonceInvalid() public {
vm.chainId(1);
string memory signedMsg = string.concat(
"chain_id=",
block.chainid.toString(),
";selector=",
selector,
";domain=",
domainName,
";nonce=",
1.toHexString(32),
";public_key_hash=",
uint256(publicKeyHash).toHexString(),
";"
);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, digest);
bytes memory signature = abi.encodePacked(r, s, v);
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
vm.expectRevert("Invalid signature");
registry.setDKIMPublicKeyHash(
selector,
domainName,
publicKeyHash,
signature
);
require(registry.getDKIMPublicKeyHash(domainName) == bytes32(0), "Invalid public key hash");
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
// function test_RevertIfNonceInvalid() public {
// vm.chainId(1);
// string memory signedMsg = string.concat(
// "chain_id=",
// block.chainid.toString(),
// ";selector=",
// selector,
// ";domain=",
// domainName,
// ";nonce=",
// 1.toHexString(32),
// ";public_key_hash=",
// uint256(publicKeyHash).toHexString(),
// ";"
// );
// bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
// (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, digest);
// bytes memory signature = abi.encodePacked(r, s, v);
// require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
// vm.expectRevert("Invalid signature");
// registry.setDKIMPublicKeyHash(
// selector,
// domainName,
// publicKeyHash,
// signature
// );
// require(registry.getDKIMPublicKeyHash(domainName) == bytes32(0), "Invalid public key hash");
// require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");

}
// }

function test_RevertIfSignatureInvalid() public {
vm.chainId(1);
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash);
uint timestamp = block.timestamp;
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash, timestamp);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(2, digest);
bytes memory signature = abi.encodePacked(r, s, v);
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
vm.expectRevert("Invalid signature");
registry.setDKIMPublicKeyHash(
selector,
domainName,
timestamp,
publicKeyHash,
signature
);
require(registry.getDKIMPublicKeyHash(domainName) == bytes32(0), "Invalid public key hash");
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
}

function test_RevertIfChainIdInvalid() public {
vm.chainId(10);
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash);
uint timestamp = block.timestamp;
string memory signedMsg = registry.computeSignedMsg(selector, domainName, publicKeyHash, timestamp);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(2, digest);
bytes memory signature = abi.encodePacked(r, s, v);
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
vm.expectRevert("Invalid signature");
registry.setDKIMPublicKeyHash(
selector,
domainName,
timestamp,
publicKeyHash,
signature
);
require(registry.getDKIMPublicKeyHash(domainName) == bytes32(0), "Invalid public key hash");
require(registry.nonceOfDomain(domainName) == 0, "Invalid nonce");
}
}

0 comments on commit e50d626

Please sign in to comment.