Skip to content

Commit

Permalink
Pubkey Resolver natspec and test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevieraykatz committed Nov 15, 2024
1 parent d9ce766 commit d54b4b4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
38 changes: 25 additions & 13 deletions src/L2/resolver/PubkeyResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,61 @@ import {IPubkeyResolver} from "ens-contracts/resolvers/profiles/IPubkeyResolver.

import {ResolverBase} from "./ResolverBase.sol";

/// @title Pubkey Resolver
///
/// @notice Adaptation of the ENS PubkeyResolver.sol profile contract, with EIP-7201 storage compliance.
/// https://github.com/ensdomains/ens-contracts/blob/staging/contracts/resolvers/profiles/PubkeyResolver.sol
///
/// @author Coinbase (https://github.com/base-org/basenames)
abstract contract PubkeyResolver is IPubkeyResolver, ResolverBase {
/// @notice Tuple containing the x and y coordinates of a public key.
struct PublicKey {
bytes32 x;
bytes32 y;
}

struct PubkeyResolverStorage {
/// @notice Public keys by node and version.
mapping(uint64 version => mapping(bytes32 node => PublicKey pubkey)) versionable_pubkeys;
}

/// @notice EIP-7201 storage location.
// keccak256(abi.encode(uint256(keccak256("pubkey.resolver.storage")) - 1)) & ~bytes32(uint256(0xff));
bytes32 constant PUBKEY_RESOLVER_STORAGE = 0x59a318c6a4da81295c2a32b42a02c3db057bb9422e2eb1f6e516ee3694b1ef00;

/**
* Sets the SECP256k1 public key associated with an ENS node.
* @param node The ENS node to query
* @param x the X coordinate of the curve point for the public key.
* @param y the Y coordinate of the curve point for the public key.
*/
/// @notice Sets the SECP256k1 public key associated with an ENS node.
///
/// @param node The ENS node to query.
///
/// @param x the X coordinate of the curve point for the public key.
/// @param y the Y coordinate of the curve point for the public key.
function setPubkey(bytes32 node, bytes32 x, bytes32 y) external virtual authorised(node) {
_getPubkeyResolverStorage().versionable_pubkeys[_getResolverBaseStorage().recordVersions[node]][node] =
PublicKey(x, y);
emit PubkeyChanged(node, x, y);
}

/**
* Returns the SECP256k1 public key associated with an ENS node.
* Defined in EIP 619.
* @param node The ENS node to query
* @return x The X coordinate of the curve point for the public key.
* @return y The Y coordinate of the curve point for the public key.
*/
/// @notice Returns the SECP256k1 public key associated with an ENS node.
///
/// @dev See EIP-619.
///
/// @param node The ENS node to query.
///
/// @return x The X coordinate of the curve point for the public key.
/// @return y The Y coordinate of the curve point for the public key.
function pubkey(bytes32 node) external view virtual override returns (bytes32 x, bytes32 y) {
uint64 currentRecordVersion = _getResolverBaseStorage().recordVersions[node];
PubkeyResolverStorage storage $ = _getPubkeyResolverStorage();
return
($.versionable_pubkeys[currentRecordVersion][node].x, $.versionable_pubkeys[currentRecordVersion][node].y);
}

/// @notice ERC-165 compliance.
function supportsInterface(bytes4 interfaceID) public view virtual override returns (bool) {
return interfaceID == type(IPubkeyResolver).interfaceId || super.supportsInterface(interfaceID);
}

/// @notice EIP-7201 storage pointer fetch helper.
function _getPubkeyResolverStorage() internal pure returns (PubkeyResolverStorage storage $) {
assembly {
$.slot := PUBKEY_RESOLVER_STORAGE
Expand Down
4 changes: 2 additions & 2 deletions test/UpgradeableL2Resolver/SetName.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ contract SetName is UpgradeableL2ResolverBase {
vm.prank(notUser);
resolver.setName(node, name);
}

function test_setsTheName() public {
vm.prank(user);
resolver.setName(node, name);
string memory retName = resolver.name(node);
assertEq(keccak256(bytes(name)), keccak256(bytes(retName)));
}
}
}
25 changes: 25 additions & 0 deletions test/UpgradeableL2Resolver/SetPubkey.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {UpgradeableL2ResolverBase} from "./UpgradeableL2ResolverBase.t.sol";
import {ResolverBase} from "src/L2/resolver/ResolverBase.sol";
import {PubkeyResolver} from "src/L2/resolver/PubkeyResolver.sol";

contract SetPubkey is UpgradeableL2ResolverBase {
bytes32 x = 0x65a2fa44daad46eab0278703edb6c4dcf5e30b8a9aec09fdc71a56f52aa392e4;
bytes32 y = 0x4a7a9e4604aa36898209997288e902ac544a555e4b5e0a9efef2b59233f3f437;

function test_reverts_forUnauthorizedUser() public {
vm.expectRevert(abi.encodeWithSelector(ResolverBase.NotAuthorized.selector, node, notUser));
vm.prank(notUser);
resolver.setPubkey(node, x, y);
}

function test_setsThePubkey() public {
vm.prank(user);
resolver.setPubkey(node, x, y);
(bytes32 retX, bytes32 retY) = resolver.pubkey(node);
assertEq(retX, x);
assertEq(retY, y);
}
}

0 comments on commit d54b4b4

Please sign in to comment.