Skip to content
Merged
7 changes: 7 additions & 0 deletions contracts/simplex/INamePolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ interface INamePolicy {
uint256 offer,
string calldata label
) external;

function consumeRenewal(
string calldata label,
bytes32 labelhash,
uint256 duration,
address holder
) external;
}

interface INamePolicyBinding {
Expand Down
11 changes: 7 additions & 4 deletions contracts/simplex/SimplexController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ contract SimplexController is
IPriceOracle.Price memory price = _rentPrice(label, labelhash, duration);
if (msg.value < price.base) revert InsufficientValue();

_renewCore(label, labelhash, duration, price.base, referrer, MAX_REGISTRATION_DURATION);
_renewCore(label, labelhash, duration, price.base, referrer, MAX_REGISTRATION_DURATION, msg.sender);

if (msg.value > price.base) {
(bool ok, ) = payable(msg.sender).call{value: msg.value - price.base}("");
Expand All @@ -570,11 +570,12 @@ contract SimplexController is
function renewSponsored(
string calldata label,
uint256 duration,
bytes32 referrer
bytes32 referrer,
address holder
) external onlySponsor nonReentrant {
bytes32 labelhash = keccak256(bytes(label));
// zero cost, and no feed read — see `registerSponsored`
_renewCore(label, labelhash, duration, 0, referrer, _sponsoredHorizon(labelhash));
_renewCore(label, labelhash, duration, 0, referrer, _sponsoredHorizon(labelhash), holder);
}

function _sponsoredHorizon(bytes32 labelhash) private view returns (uint256) {
Expand All @@ -589,11 +590,13 @@ contract SimplexController is
uint256 duration,
uint256 cost,
bytes32 referrer,
uint256 horizon
uint256 horizon,
address holder
) private returns (uint256 expires) {
if (duration < MIN_RENEWAL_DURATION) revert DurationTooShort(duration);
if (INamePolicy(namePolicy).isReserved(labelhash))
revert NameReserved(label);
INamePolicy(namePolicy).consumeRenewal(label, labelhash, duration, holder);

expires = base.renew(uint256(labelhash), duration);
if (expires > block.timestamp + horizon) revert DurationTooLong(duration);
Expand Down
137 changes: 136 additions & 1 deletion contracts/simplex/SimplexNamePolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ contract SimplexNamePolicy is INamePolicy {
keccak256(
"AcceptGeneralTermsForClaim(uint256 tokenId,address from,uint256 offer,bytes32 termsHash,uint256 nonce,uint256 deadline)"
);
bytes32 public constant ACCEPT_GENERAL_TERMS_FOR_RENEWAL_TYPEHASH =
keccak256(
"AcceptGeneralTermsForRenewal(uint256 tokenId,uint256 expires,uint256 duration,bytes32 termsHash,uint256 nonce,uint256 deadline)"
);
bytes32 public constant ACCEPT_COMMUNITY_TERMS_TYPEHASH =
keccak256(
"AcceptCommunityTerms(bytes32 commitment,bytes32 termsHash,uint256 nonce,uint256 deadline)"
Expand All @@ -62,6 +66,10 @@ contract SimplexNamePolicy is INamePolicy {
keccak256(
"AcceptCommunityTermsForClaim(uint256 tokenId,address from,uint256 offer,bytes32 termsHash,uint256 nonce,uint256 deadline)"
);
bytes32 public constant ACCEPT_COMMUNITY_TERMS_FOR_RENEWAL_TYPEHASH =
keccak256(
"AcceptCommunityTermsForRenewal(uint256 tokenId,uint256 expires,uint256 duration,bytes32 termsHash,uint256 nonce,uint256 deadline)"
);

event ReservedNameAdded(string name, Reason reason);
event ReservedNameReasonChanged(string name, Reason reason);
Expand Down Expand Up @@ -89,6 +97,8 @@ contract SimplexNamePolicy is INamePolicy {
error ZeroTerms();
error TermsMismatch(bytes32 accepted, bytes32 current);
error TermsNotAccepted(string name);
error NotHolder(string name);
error ExpiryMismatch(uint256 named, uint256 current);
error SignatureExpired();
error InvalidNonce();
error InvalidSignature();
Expand Down Expand Up @@ -388,6 +398,84 @@ contract SimplexNamePolicy is INamePolicy {
_acceptForClaim(tokenId, from, offer, acceptedTermsHash, communityTermsHash, recipient);
}

function acceptGeneralTermsForRenewal(
uint256 tokenId,
uint256 expires,
uint256 duration,
bytes32 acceptedTermsHash
) external {
_acceptForRenewal(tokenId, expires, duration, acceptedTermsHash, generalTermsHash, msg.sender);
}

function acceptGeneralTermsForRenewalWithSig(
uint256 tokenId,
uint256 expires,
uint256 duration,
bytes32 acceptedTermsHash,
address holder,
uint256 nonce,
uint256 deadline,
bytes calldata sig
) external {
_consumeIntent(
holder,
keccak256(
abi.encode(
ACCEPT_GENERAL_TERMS_FOR_RENEWAL_TYPEHASH,
tokenId,
expires,
duration,
acceptedTermsHash,
nonce,
deadline
)
),
nonce,
deadline,
sig
);
_acceptForRenewal(tokenId, expires, duration, acceptedTermsHash, generalTermsHash, holder);
}

function acceptCommunityTermsForRenewal(
uint256 tokenId,
uint256 expires,
uint256 duration,
bytes32 acceptedTermsHash
) external {
_acceptForRenewal(tokenId, expires, duration, acceptedTermsHash, communityTermsHash, msg.sender);
}

function acceptCommunityTermsForRenewalWithSig(
uint256 tokenId,
uint256 expires,
uint256 duration,
bytes32 acceptedTermsHash,
address holder,
uint256 nonce,
uint256 deadline,
bytes calldata sig
) external {
_consumeIntent(
holder,
keccak256(
abi.encode(
ACCEPT_COMMUNITY_TERMS_FOR_RENEWAL_TYPEHASH,
tokenId,
expires,
duration,
acceptedTermsHash,
nonce,
deadline
)
),
nonce,
deadline,
sig
);
_acceptForRenewal(tokenId, expires, duration, acceptedTermsHash, communityTermsHash, holder);
}

function consumeRegistration(
string calldata label,
bytes32 labelhash,
Expand Down Expand Up @@ -417,6 +505,22 @@ contract SimplexNamePolicy is INamePolicy {
_confirmAcceptance(_names[bytes32(tokenId)].community, accepted, label, to);
}

function consumeRenewal(
string calldata label,
bytes32 labelhash,
uint256 duration,
address holder
) external {
if (msg.sender != address(controller)) revert NotController();
if (address(this) == _self) revert NotProxy();
uint256 tokenId = uint256(labelhash);
uint256 expires = registrar.nameExpires(tokenId);
if (!_holds(holder, tokenId, expires)) revert NotHolder(label);
bytes32 accepted = acceptedRenewalTerms[tokenId][holder][expires][duration];
delete acceptedRenewalTerms[tokenId][holder][expires][duration];
_confirmAcceptance(_names[labelhash].community, accepted, label, holder);
}

function DOMAIN_SEPARATOR() public view returns (bytes32) {
return
keccak256(
Expand Down Expand Up @@ -464,6 +568,35 @@ contract SimplexNamePolicy is INamePolicy {
acceptedClaimTerms[tokenId][from][recipient][offer] = acceptedTermsHash;
}

function _acceptForRenewal(
uint256 tokenId,
uint256 expires,
uint256 duration,
bytes32 acceptedTermsHash,
bytes32 currentTermsHash,
address holder
) private {
if (!_isCurrentTerms(acceptedTermsHash, currentTermsHash))
revert TermsMismatch(acceptedTermsHash, currentTermsHash);
uint256 currentExpires = registrar.nameExpires(tokenId);
if (expires != currentExpires) revert ExpiryMismatch(expires, currentExpires);
acceptedRenewalTerms[tokenId][holder][expires][duration] = acceptedTermsHash;
}

function _holds(
address account,
uint256 tokenId,
uint256 expires
) private view returns (bool) {
if (expires > block.timestamp)
return registrar.ownerOf(tokenId) == account;
uint256 balance = registrar.balanceOf(account);
for (uint256 i = 0; i < balance; ++i)
if (registrar.tokenOfOwnerByIndex(account, i) == tokenId)
return true;
return false;
}

function _confirmAcceptance(
bool community,
bytes32 accepted,
Expand Down Expand Up @@ -510,5 +643,7 @@ contract SimplexNamePolicy is INamePolicy {
string public communityTermsLink;
string public generalTermsLink;
bytes32 public generalTermsHash;
uint256[42] private __gap;
mapping(uint256 => mapping(address => mapping(uint256 => mapping(uint256 => bytes32))))
public acceptedRenewalTerms;
uint256[41] private __gap;
}
5 changes: 3 additions & 2 deletions contracts/simplex/SimplexSponsor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ contract SimplexSponsor {
function renewWithCredit(
string calldata label,
uint256 duration,
bytes32 referrer
bytes32 referrer,
address holder
) external {
// Base only, matching the payable `renew`, which charges `price.base`.
// The premium is the expired-name auction price and is owed on
// re-registration, never on renewing a name you already hold.
_spendAllowance(controller.rentPriceUSD(label, duration).base);
controller.renewSponsored(label, duration, referrer);
controller.renewSponsored(label, duration, referrer, holder);
}

function DOMAIN_SEPARATOR() public view returns (bytes32) {
Expand Down
7 changes: 4 additions & 3 deletions docs/upgrades.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ start:
| `SimplexResolver` | 0: `ResolverBase`; 1-11: the resolver profiles; 12-13: `PublicResolver` | 14 | – |
| `SubnameRegistrar` | – | 0 | – |
| `SimplexController` | 0-150: `Initializable`, `ContextUpgradeable`, `OwnableUpgradeable`, `Ownable2StepUpgradeable` | 151 | `uint256[43]` |
| `SimplexNamePolicy` | – | 0 | `uint256[42]` |
| `SimplexNamePolicy` | – | 0 | `uint256[41]` |
| `SimplexSponsor` | – | 0 | – |
| `UniversalResolver` | – | – (immutables only) | – |

Expand Down Expand Up @@ -378,8 +378,9 @@ by `scripts/prepare-upgrade.mjs`, unless its `INTENDED` input names that
immutable for an intended change. Its own variables are
`_names` (each name's community flag and reservation reason, in one slot),
`communityTermsHash`, `acceptedTerms`, `acceptedClaimTerms`, `nonces`,
`communityTermsLink`, `generalTermsLink`, `generalTermsHash`, then
`uint256[42] __gap`; the invariants above apply to it unchanged.
`communityTermsLink`, `generalTermsLink`, `generalTermsHash`,
`acceptedRenewalTerms`, then `uint256[41] __gap`; the invariants above apply to
it unchanged.

The controller's `MIN_REGISTRATION`, `MAX_REGISTRATION` and
`MAX_SPONSORED_REGISTRATION` are compiled into the policy, and the controller's
Expand Down
Loading