diff --git a/packages/did-eth-registry/test/EthereumDIDRegistry.t.sol b/packages/did-eth-registry/test/EthereumDIDRegistry.t.sol index 9eb9e55..810faf3 100644 --- a/packages/did-eth-registry/test/EthereumDIDRegistry.t.sol +++ b/packages/did-eth-registry/test/EthereumDIDRegistry.t.sol @@ -43,8 +43,8 @@ contract EthereumDIDRegistryTest is Test { vm.prank(owner); registry.changeOwner(owner, newOwner); vm.prank(newOwner); - registry.changeOwner(newOwner, newOwner2); - assertEq(registry.identityOwner(owner), newOwner, "should change owner"); + registry.changeOwner(owner, newOwner2); + assertEq(registry.identityOwner(owner), newOwner2, "should change owner twice"); } function testChangeOwnerOriginalAddressIsBadActor() public { diff --git a/packages/did-eth-registry/test/EthereumDIDRegistryAttribute.t.sol b/packages/did-eth-registry/test/EthereumDIDRegistryAttribute.t.sol index 1892b7e..6d2830c 100644 --- a/packages/did-eth-registry/test/EthereumDIDRegistryAttribute.t.sol +++ b/packages/did-eth-registry/test/EthereumDIDRegistryAttribute.t.sol @@ -76,14 +76,7 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.changeOwner(SIGNER, SIGNER2); vm.expectEmit(); emit DIDAttributeChanged(SIGNER, "key", "value", 1 weeks + 1, block.number); - (uint8 v, bytes32 r, bytes32 s) = signAttribute( - SIGNER, - SIGNER2, - address(registry), - "key", - "value", - PRIVATE_KEY2 - ); + (uint8 v, bytes32 r, bytes32 s) = signAttribute(SIGNER, address(registry), "key", "value", PRIVATE_KEY2); registry.setAttributeSigned(SIGNER, v, r, s, "key", "value", 1 weeks); } @@ -216,7 +209,7 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.setAttribute(SIGNER, "key", "value", 1 weeks); vm.prank(SIGNER); registry.changeOwner(SIGNER, SIGNER2); - (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER2, address(registry), "key", "value", PRIVATE_KEY2); + (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, address(registry), "key", "value", PRIVATE_KEY2); vm.expectEmit(); emit DIDAttributeChanged(SIGNER, "key", "value", 0, block.number); registry.revokeAttributeSigned(SIGNER, v, r, s, "key", "value"); @@ -225,7 +218,6 @@ contract EthereumDIDRegistryDelegateTest is Test { /** * @dev Sign data with a private key * @param _identity Identity address - * @param _delegate Delegate address * @param _registry DID Registry address * @param _privateKey Private key * @param _message Message to sign @@ -235,7 +227,6 @@ contract EthereumDIDRegistryDelegateTest is Test { */ function signData( address _identity, - address _delegate, address _registry, bytes memory _privateKey, bytes memory _message @@ -248,7 +239,7 @@ contract EthereumDIDRegistryDelegateTest is Test { bytes32 s ) { - address idOwner = registry.identityOwner(_delegate); + address idOwner = registry.identityOwner(_identity); uint256 ownerNonce = registry.nonce(idOwner); return VmDigest.signData(vm, _identity, _registry, _privateKey, _message, ownerNonce); } @@ -280,68 +271,7 @@ contract EthereumDIDRegistryDelegateTest is Test { ) { bytes memory message = abi.encodePacked(bytes("setAttribute"), _key, _value, uint256(1 weeks)); - return signData(_identity, _identity, _registry, _privateKey, message); - } - - /** - * @dev Sign attribute with a private key - * @param _identity Identity address - * @param _registry DID Registry address - * @param _key Attribute key - * @param _value Attribute value - * @param _privateKey Private key - * @return v Signature v - * @return r Signature r - * @return s Signature s - */ - function signAttribute( - address _identity, - address _delegate, - address _registry, - bytes32 _key, - bytes memory _value, - bytes memory _privateKey - ) - internal - view - returns ( - uint8 v, - bytes32 r, - bytes32 s - ) - { - bytes memory message = abi.encodePacked(bytes("setAttribute"), _key, _value, uint256(1 weeks)); - return signData(_identity, _delegate, _registry, _privateKey, message); - } - - /** - * @dev Sign revoke with a private key - * @param _identity Identity address - * @param _registry DID Registry address - * @param _key Attribute key - * @param _value Attribute value - * @param _privateKey Private key - * @return v Signature v - * @return r Signature r - * @return s Signature s - */ - function signRevoke( - address _identity, - address _registry, - bytes32 _key, - bytes memory _value, - bytes memory _privateKey - ) - internal - view - returns ( - uint8 v, - bytes32 r, - bytes32 s - ) - { - bytes memory message = abi.encodePacked(bytes("revokeAttribute"), _key, _value); - return signData(_identity, _identity, _registry, _privateKey, message); + return signData(_identity, _registry, _privateKey, message); } /** @@ -357,7 +287,6 @@ contract EthereumDIDRegistryDelegateTest is Test { */ function signRevoke( address _identity, - address _delegate, address _registry, bytes32 _key, bytes memory _value, @@ -372,6 +301,6 @@ contract EthereumDIDRegistryDelegateTest is Test { ) { bytes memory message = abi.encodePacked(bytes("revokeAttribute"), _key, _value); - return signData(_identity, _delegate, _registry, _privateKey, message); + return signData(_identity, _registry, _privateKey, message); } } diff --git a/packages/did-eth-registry/test/EthereumDIDRegistryDelegate.t.sol b/packages/did-eth-registry/test/EthereumDIDRegistryDelegate.t.sol index e1cf127..b96f043 100644 --- a/packages/did-eth-registry/test/EthereumDIDRegistryDelegate.t.sol +++ b/packages/did-eth-registry/test/EthereumDIDRegistryDelegate.t.sol @@ -69,7 +69,7 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); } - function testAddDelegateOwnerIdentityChanged() public { + function testAddDelegateDoesNotChangeOwner() public { assertEq(registry.owners(SIGNER), address(0x0)); vm.prank(SIGNER); registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); @@ -91,17 +91,8 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); } - function testAddDelegateNoTakeBack() public { - vm.prank(SIGNER); - registry.changeOwner(SIGNER, SIGNER2); - vm.prank(SIGNER); - vm.expectRevert("bad_actor"); - registry.addDelegate(SIGNER, "attestor", SIGNER3, 1 days); - } - function testAddDelegateSigned() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER3); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); assertTrue(registry.validDelegate(SIGNER, "attestor", SIGNER2)); } @@ -111,14 +102,12 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.changeOwner(SIGNER, SIGNER3); assertEq(registry.owners(SIGNER), SIGNER3); (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER3); vm.expectRevert("bad_signature"); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); } function testAddDelegateSignedExpires() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER3); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); assertTrue(registry.validDelegate(SIGNER, "attestor", SIGNER2)); vm.warp(1 days + 1); @@ -127,7 +116,6 @@ contract EthereumDIDRegistryDelegateTest is Test { function testAddDelegateSignedBadSignatureKey() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER2); vm.expectRevert("bad_signature"); registry.addDelegateSigned(SIGNER2, v, r, s, "attestor", SIGNER2, 1 days); } @@ -135,20 +123,17 @@ contract EthereumDIDRegistryDelegateTest is Test { function testAddDelegateSignedWrongSignature() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY2); vm.expectRevert("bad_signature"); - vm.prank(SIGNER3); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); } function testAddDelegateSignedWrongSignatureData() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER, address(registry), PRIVATE_KEY); vm.expectRevert("bad_signature"); - vm.prank(SIGNER3); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); } function testAddDelegateSignedChangedBlockNumber() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); uint256 transactionBlock = registry.changed(SIGNER); assertEq(block.number, transactionBlock, "should record block number"); @@ -158,13 +143,11 @@ contract EthereumDIDRegistryDelegateTest is Test { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); vm.expectEmit(); emit DIDDelegateChanged(SIGNER, "attestor", SIGNER2, 1 days + 1, 0); - vm.prank(SIGNER); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); } function testAddDelegateSignedExpectNonce() public { (uint8 v, bytes32 r, bytes32 s) = signDelegate(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); uint256 nonce = registry.nonce(SIGNER); assertEq(nonce, 1, "should increment nonce"); @@ -182,7 +165,6 @@ contract EthereumDIDRegistryDelegateTest is Test { message, ownerNonce ); - vm.prank(SIGNER); vm.expectRevert("bad_signature"); registry.addDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2, 1 days); } @@ -254,7 +236,6 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); assertTrue(registry.validDelegate(SIGNER, "attestor", SIGNER2)); (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2); assertFalse(registry.validDelegate(SIGNER, "attestor", SIGNER2)); } @@ -266,7 +247,6 @@ contract EthereumDIDRegistryDelegateTest is Test { assertEq(block.number, registry.changed(SIGNER), "add should record block number"); vm.roll(block.number + 100); (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2); assertEq(block.number, registry.changed(SIGNER), "revoke should record block number"); assertNotEq(block.number, startBlock, "block number must be different across calls"); @@ -280,7 +260,6 @@ contract EthereumDIDRegistryDelegateTest is Test { vm.expectEmit(); emit DIDDelegateChanged(SIGNER, "attestor", SIGNER2, startTime, startBlock); (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); - vm.prank(SIGNER3); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2); } @@ -289,10 +268,8 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER2, address(registry), PRIVATE_KEY); vm.expectRevert("bad_signature"); - vm.prank(SIGNER3); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER3); vm.expectRevert("bad_signature"); - vm.prank(SIGNER3); registry.revokeDelegateSigned(SIGNER3, v, r, s, "attestor", SIGNER); } @@ -301,7 +278,7 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER2, address(registry), PRIVATE_KEY2); vm.expectRevert("bad_signature"); - vm.prank(SIGNER); + vm.prank(SIGNER3); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2); } @@ -310,7 +287,6 @@ contract EthereumDIDRegistryDelegateTest is Test { registry.addDelegate(SIGNER, "attestor", SIGNER2, 1 days); (uint8 v, bytes32 r, bytes32 s) = signRevoke(SIGNER, SIGNER, address(registry), PRIVATE_KEY); vm.expectRevert("bad_signature"); - vm.prank(SIGNER); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2); } @@ -328,7 +304,6 @@ contract EthereumDIDRegistryDelegateTest is Test { message, ownerNonce ); - vm.prank(SIGNER); vm.expectRevert("bad_signature"); registry.revokeDelegateSigned(SIGNER, v, r, s, "attestor", SIGNER2); } @@ -336,7 +311,6 @@ contract EthereumDIDRegistryDelegateTest is Test { /** * @dev Sign data with private key * @param _identity Identity address - * @param _delegate Delegate address * @param _registry DID Registry address * @param _privateKey Private key * @param _message Message to sign @@ -346,7 +320,6 @@ contract EthereumDIDRegistryDelegateTest is Test { */ function signData( address _identity, - address _delegate, address _registry, bytes memory _privateKey, bytes memory _message @@ -359,7 +332,7 @@ contract EthereumDIDRegistryDelegateTest is Test { bytes32 s ) { - address idOwner = registry.identityOwner(_delegate); + address idOwner = registry.identityOwner(_identity); uint256 ownerNonce = registry.nonce(idOwner); return VmDigest.signData(vm, _identity, _registry, _privateKey, _message, ownerNonce); } @@ -389,7 +362,7 @@ contract EthereumDIDRegistryDelegateTest is Test { ) { bytes memory message = abi.encodePacked(bytes("addDelegate"), bytes32("attestor"), _delegate, uint256(1 days)); - return signData(_identity, _delegate, _registry, _privateKey, message); + return signData(_identity, _registry, _privateKey, message); } /** @@ -417,6 +390,6 @@ contract EthereumDIDRegistryDelegateTest is Test { ) { bytes memory message = abi.encodePacked(bytes("revokeDelegate"), bytes32("attestor"), _delegate); - return signData(_identity, _delegate, _registry, _privateKey, message); + return signData(_identity, _registry, _privateKey, message); } } diff --git a/packages/did-eth-registry/test/VmDigest.sol b/packages/did-eth-registry/test/VmDigest.sol index ea234d3..24fd231 100644 --- a/packages/did-eth-registry/test/VmDigest.sol +++ b/packages/did-eth-registry/test/VmDigest.sol @@ -44,14 +44,13 @@ library VmDigest { } /** - * @dev Convert bytes to uint256 + * @dev Convert bytes to uint256. This is intended for test purposes only. * @param _bytes Bytes to convert * @return uint256 */ function toUint256(bytes memory _bytes) internal pure returns (uint256) { - require(_bytes.length >= 32, "toUint256_outOfBounds"); + require(_bytes.length <= 32, "toUint256_outOfBounds"); uint256 result; - // for test purposes only // solhint-disable-next-line no-inline-assembly assembly { result := mload(add(_bytes, 0x20)) diff --git a/packages/did-eth-registry/test/VmDigest.t.sol b/packages/did-eth-registry/test/VmDigest.t.sol new file mode 100644 index 0000000..f8d2b23 --- /dev/null +++ b/packages/did-eth-registry/test/VmDigest.t.sol @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.15; + +import { Test } from "forge-std/Test.sol"; +import { VmDigest } from "./VmDigest.sol"; + +contract EthereumDIDRegistryTest is Test { + function testToUint256() public { + bytes memory b1 = hex"10"; + bytes memory b2 = hex"1000"; + bytes memory b3 = hex"100000"; + bytes memory b4 = hex"10000000"; + bytes memory b5 = hex"1000000000"; + bytes memory b6 = hex"100000000000"; + bytes memory b7 = hex"10000000000000"; + bytes memory b8 = hex"1000000000000000"; + bytes memory b9 = hex"100000000000000000"; + bytes memory b10 = hex"10000000000000000000"; + bytes memory b11 = hex"1000000000000000000000"; + bytes memory b12 = hex"100000000000000000000000"; + bytes memory b13 = hex"10000000000000000000000000"; + bytes memory b14 = hex"1000000000000000000000000000"; + + assertEq(VmDigest.toUint256(b1), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b2), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b3), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b4), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b5), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b6), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b7), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b8), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b9), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b10), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b11), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b12), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b13), 0x1 << 252, "should convert bytes to uint256"); + assertEq(VmDigest.toUint256(b14), 0x1 << 252, "should convert bytes to uint256"); + } + + function testToUint256With32ByteValue() public { + bytes memory b32 = hex"1000000000000000000000000000000000000000000000000000000000000000"; + assertEq(VmDigest.toUint256(b32), 0x1 << 252, "should convert bytes to uint256"); + } + + function testToUint256Requires32BytesOrLess() public { + bytes memory b33 = hex"100000000000000000000000000000000000000000000000000000000000000000"; + vm.expectRevert("toUint256_outOfBounds"); + VmDigest.toUint256(b33); + } +}