diff --git a/contracts/simplex/INamePolicy.sol b/contracts/simplex/INamePolicy.sol index 7851a344..bd366557 100644 --- a/contracts/simplex/INamePolicy.sol +++ b/contracts/simplex/INamePolicy.sol @@ -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 { diff --git a/contracts/simplex/SimplexController.sol b/contracts/simplex/SimplexController.sol index c80e7fb9..6aefa86a 100644 --- a/contracts/simplex/SimplexController.sol +++ b/contracts/simplex/SimplexController.sol @@ -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}(""); @@ -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) { @@ -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); diff --git a/contracts/simplex/SimplexNamePolicy.sol b/contracts/simplex/SimplexNamePolicy.sol index d3aa38a0..4c70fa58 100644 --- a/contracts/simplex/SimplexNamePolicy.sol +++ b/contracts/simplex/SimplexNamePolicy.sol @@ -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)" @@ -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); @@ -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(); @@ -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, @@ -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( @@ -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, @@ -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; } diff --git a/contracts/simplex/SimplexSponsor.sol b/contracts/simplex/SimplexSponsor.sol index 970608d0..ef9c916f 100644 --- a/contracts/simplex/SimplexSponsor.sol +++ b/contracts/simplex/SimplexSponsor.sol @@ -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) { diff --git a/docs/upgrades.md b/docs/upgrades.md index c1e3fb6b..4aa3ece3 100644 --- a/docs/upgrades.md +++ b/docs/upgrades.md @@ -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) | – | @@ -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 diff --git a/test/simplex/TestCommunityTerms.test.ts b/test/simplex/TestCommunityTerms.test.ts index c46c3851..7f1d4bf1 100644 --- a/test/simplex/TestCommunityTerms.test.ts +++ b/test/simplex/TestCommunityTerms.test.ts @@ -53,8 +53,11 @@ const KINDS = [ commitWithSig: 'commitUnderGeneralTermsWithSig', acceptForClaim: 'acceptGeneralTermsForClaim', acceptForClaimWithSig: 'acceptGeneralTermsForClaimWithSig', + acceptForRenewal: 'acceptGeneralTermsForRenewal', + acceptForRenewalWithSig: 'acceptGeneralTermsForRenewalWithSig', commitType: 'AcceptGeneralTerms', claimType: 'AcceptGeneralTermsForClaim', + renewalType: 'AcceptGeneralTermsForRenewal', }, { title: 'community terms', @@ -71,8 +74,11 @@ const KINDS = [ commitWithSig: 'commitUnderCommunityTermsWithSig', acceptForClaim: 'acceptCommunityTermsForClaim', acceptForClaimWithSig: 'acceptCommunityTermsForClaimWithSig', + acceptForRenewal: 'acceptCommunityTermsForRenewal', + acceptForRenewalWithSig: 'acceptCommunityTermsForRenewalWithSig', commitType: 'AcceptCommunityTerms', claimType: 'AcceptCommunityTermsForClaim', + renewalType: 'AcceptCommunityTermsForRenewal', }, ] as const type Kind = (typeof KINDS)[number] @@ -189,6 +195,80 @@ async function offerToBob({ baseRegistrar }: Fixture, id = COMMUNITY_ID) { return baseRegistrar.read.offers([id]) } +const renewAs = ( + controller: any, + account: any, + k: Kind = COMMUNITY, +): WritePromise => + controller.write.renew([k.label, YEAR, zeroHash], { + account, + value: YEAR_PRICE_USD, + }) + +type RenewalAcceptance = Partial<{ + k: Kind + id: bigint + hash: `0x${string}` + duration: bigint + expires: bigint +}> + +const acceptForRenewal = ( + namePolicy: any, + account: any, + { + k = COMMUNITY, + id = k.id, + hash = k.hash, + duration = YEAR, + expires = 0n, + }: RenewalAcceptance = {}, +): WritePromise => + callPolicy( + namePolicy, + k.acceptForRenewal, + [id, expires, duration, hash], + account, + ) + +const acceptRenewal = async ( + { namePolicy, baseRegistrar }: Fixture, + account: any, + acceptance: RenewalAcceptance = {}, +) => + acceptForRenewal(namePolicy, account, { + ...acceptance, + expires: await baseRegistrar.read.nameExpires([ + acceptance.id ?? (acceptance.k ?? COMMUNITY).id, + ]), + }) + +const signRenewalAcceptance = async ( + namePolicy: any, + signer: any, + { k = COMMUNITY, expires = 0n, duration = YEAR }: RenewalAcceptance = {}, +) => + signIntent( + publicClient, + signer, + 'SimplexNamePolicy', + namePolicy.address, + k.renewalType, + { + tokenId: k.id, + expires, + duration, + termsHash: k.hash, + nonce: await namePolicy.read.nonces([signer.account.address]), + deadline: FAR_FUTURE, + }, + ) + +const renewOnCredit = (sponsor: any, holder: `0x${string}`, k: Kind = COMMUNITY) => + sponsor.write.renewWithCredit([k.label, YEAR, zeroHash, holder], { + account: registrar, + }) + describe('community names', () => { describe('listing', () => { it('the guardian lists a free name, and only the owner removes it', async () => { @@ -282,6 +362,24 @@ describe('community names', () => { ), ), ) + expect( + await namePolicy.read.ACCEPT_GENERAL_TERMS_FOR_RENEWAL_TYPEHASH(), + ).toBe( + keccak256( + toHex( + 'AcceptGeneralTermsForRenewal(uint256 tokenId,uint256 expires,uint256 duration,bytes32 termsHash,uint256 nonce,uint256 deadline)', + ), + ), + ) + expect( + await namePolicy.read.ACCEPT_COMMUNITY_TERMS_FOR_RENEWAL_TYPEHASH(), + ).toBe( + keccak256( + toHex( + 'AcceptCommunityTermsForRenewal(uint256 tokenId,uint256 expires,uint256 duration,bytes32 termsHash,uint256 nonce,uint256 deadline)', + ), + ), + ) }) it('both setters revert for a contract bound to another controller or registrar', async () => { @@ -489,13 +587,11 @@ describe('community names', () => { it('the sponsor renews a listed name up to three years out, not further', async () => { const f = await load() await acceptAndRegister(f) - await f.sponsor.write.renewWithCredit(['community', YEAR, zeroHash], { - account: registrar, - }) + await acceptRenewal(f, alice) + await renewOnCredit(f.sponsor, alice.address) + await acceptRenewal(f, alice) await expect( - f.sponsor.write.renewWithCredit(['community', YEAR, zeroHash], { - account: registrar, - }), + renewOnCredit(f.sponsor, alice.address), ).toBeRevertedWithCustomErrorFrom(f.controller, 'DurationTooLong') }) @@ -857,6 +953,253 @@ describe('community names', () => { ).toBeRevertedWithCustomError('NotRegistrar') }) }) + + describe('renewal', () => { + it('a renewal by anyone but the holder reverts', async () => { + const f = await load() + await acceptAndRegister(f) + await expect(renewAs(f.controller, bob)).rejects.toThrow() + await expect(renewAs(f.controller, bob)).toBeRevertedWithCustomErrorFrom( + f.namePolicy, + 'NotHolder', + ) + }) + + it('a renewal by the holder reverts without an acceptance', async () => { + const f = await load() + await acceptAndRegister(f) + await expect( + renewAs(f.controller, alice), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + }) + + it('the holder accepts and renews once, and the event records the name, holder and terms', async () => { + const f = await load() + await acceptAndRegister(f) + const before = await f.baseRegistrar.read.nameExpires([COMMUNITY_ID]) + await acceptRenewal(f, alice) + const hash = await renewAs(f.controller, alice) + expect(await f.baseRegistrar.read.nameExpires([COMMUNITY_ID])).toBe( + before + YEAR, + ) + expect(await acceptanceEvents(f.namePolicy, hash)).toEqual([ + ['community', getAddress(alice.address), TERMS], + ]) + expect( + await f.namePolicy.read.acceptedRenewalTerms([ + COMMUNITY_ID, + alice.address, + before, + YEAR, + ]), + ).toBe(zeroHash) + await expect( + renewAs(f.controller, alice), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + }) + + it("someone else's acceptance neither counts nor blocks the holder's", async () => { + const f = await load() + await acceptAndRegister(f) + await acceptRenewal(f, bob) + await expect(renewAs(f.controller, bob)).toBeRevertedWithCustomErrorFrom( + f.namePolicy, + 'NotHolder', + ) + await expect( + renewAs(f.controller, alice), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await acceptRenewal(f, alice) + await renewAs(f.controller, alice) + }) + + it('an acceptance is valid only for the duration it was made for', async () => { + const f = await load() + await acceptAndRegister(f) + await acceptRenewal(f, alice, { duration: YEAR + DAY }) + await expect( + renewAs(f.controller, alice), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await acceptRenewal(f, alice) + await renewAs(f.controller, alice) + }) + + it('an acceptance is valid only for the term it was made in', async () => { + const f = await load() + await acceptAndRegister(f, GENERAL) + await acceptRenewal(f, alice, { k: GENERAL }) + await acceptRenewal(f, alice, { + k: GENERAL, + duration: 2n * YEAR, + }) + await renewOnCredit(f.sponsor, alice.address, GENERAL) + await expect( + renewOnCredit(f.sponsor, alice.address, GENERAL), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await expect( + f.sponsor.write.renewWithCredit( + [GENERAL.label, 2n * YEAR, zeroHash, alice.address], + { account: registrar }, + ), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + }) + + it('the registrar cannot renew for the holder without the acceptance of that renewal', async () => { + const f = await load() + await acceptAndRegister(f) + await expect( + renewOnCredit(f.sponsor, alice.address), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await acceptRenewal(f, alice, { duration: YEAR + DAY }) + await expect( + renewOnCredit(f.sponsor, alice.address), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await acceptRenewal(f, alice) + await renewOnCredit(f.sponsor, alice.address) + }) + + it('new terms between acceptance and renewal require a new acceptance', async () => { + const f = await load() + await acceptAndRegister(f) + await acceptRenewal(f, alice) + await f.namePolicy.write.setCommunityTerms([NEW_TERMS_LINK], { + account: owner, + }) + await expect( + renewAs(f.controller, alice), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await expect( + acceptForRenewal(f.namePolicy, alice), + ).toBeRevertedWithCustomError('TermsMismatch') + await acceptRenewal(f, alice, { hash: NEW_TERMS }) + await renewAs(f.controller, alice) + }) + + it('the registrar relays a signed acceptance and renews on credit for the holder only', async () => { + const f = await load() + await acceptAndRegister(f) + const expires = await f.baseRegistrar.read.nameExpires([COMMUNITY_ID]) + await f.namePolicy.write.acceptCommunityTermsForRenewalWithSig( + [ + COMMUNITY_ID, + expires, + YEAR, + TERMS, + alice.address, + 0n, + FAR_FUTURE, + await signRenewalAcceptance(f.namePolicy, aliceClient, { expires }), + ], + { account: registrar }, + ) + await expect( + renewOnCredit(f.sponsor, bob.address), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'NotHolder') + const hash = await renewOnCredit(f.sponsor, alice.address) + expect(await acceptanceEvents(f.namePolicy, hash)).toEqual([ + ['community', getAddress(alice.address), TERMS], + ]) + }) + + it('an acceptance names the current expiry, so a signed one relayed after a renewal reverts', async () => { + const f = await load() + await acceptAndRegister(f) + const expires = await f.baseRegistrar.read.nameExpires([COMMUNITY_ID]) + await expect( + acceptForRenewal(f.namePolicy, alice, { expires: expires + YEAR }), + ).toBeRevertedWithCustomError('ExpiryMismatch') + const sig = await signRenewalAcceptance(f.namePolicy, aliceClient, { + expires, + }) + await acceptRenewal(f, alice) + await renewAs(f.controller, alice) + await expect( + f.namePolicy.write.acceptCommunityTermsForRenewalWithSig( + [COMMUNITY_ID, expires, YEAR, TERMS, alice.address, 0n, FAR_FUTURE, sig], + { account: registrar }, + ), + ).toBeRevertedWithCustomError('ExpiryMismatch') + expect(await f.namePolicy.read.nonces([alice.address])).toBe(0n) + await expect( + renewOnCredit(f.sponsor, alice.address), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + }) + + it("a transfer ends the former holder's renewals, and the new holder accepts its own", async () => { + const f = await load() + await acceptAndRegister(f) + await acceptRenewal(f, alice) + const offer = await offerToBob(f) + await f.namePolicy.write.acceptCommunityTermsForClaim( + [COMMUNITY_ID, alice.address, offer, TERMS], + { account: bob }, + ) + await f.baseRegistrar.write.transferFrom( + [alice.address, bob.address, COMMUNITY_ID], + { account: bob }, + ) + await expect( + renewAs(f.controller, alice), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'NotHolder') + await expect( + renewOnCredit(f.sponsor, alice.address), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'NotHolder') + await expect(renewAs(f.controller, bob)).toBeRevertedWithCustomErrorFrom( + f.namePolicy, + 'TermsNotAccepted', + ) + await acceptRenewal(f, bob) + await renewAs(f.controller, bob) + }) + + it('in the grace period the holder renews on either path, and no one else does', async () => { + const f = await load() + await acceptAndRegister(f) + await acceptAndRegister(f, GENERAL) + const bobs = registration('bobsname', bob.address) + await commitUnderGeneralTerms(connection, f.controller, bobs, { + account: bob, + }) + await f.controller.write.register([bobs], { + account: bob, + value: 2n * YEAR_PRICE_USD, + }) + await connection.networkHelpers.time.increase(MIN_REGISTRATION + DAY) + await expect( + f.baseRegistrar.read.ownerOf([COMMUNITY_ID]), + ).rejects.toThrow() + + await acceptRenewal(f, bob) + await expect(renewAs(f.controller, bob)).toBeRevertedWithCustomErrorFrom( + f.namePolicy, + 'NotHolder', + ) + await expect( + renewOnCredit(f.sponsor, bob.address), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'NotHolder') + + await acceptRenewal(f, alice) + await renewOnCredit(f.sponsor, alice.address) + await acceptRenewal(f, alice, { k: GENERAL }) + await renewAs(f.controller, alice, GENERAL) + expect(await f.baseRegistrar.read.ownerOf([COMMUNITY_ID])).toBe( + getAddress(alice.address), + ) + expect(await f.baseRegistrar.read.ownerOf([GENERAL.id])).toBe( + getAddress(alice.address), + ) + }) + + it('only the controller consumes a renewal acceptance', async () => { + const { namePolicy } = await load() + await expect( + namePolicy.write.consumeRenewal( + ['community', labelhash('community'), YEAR, alice.address], + { account: alice }, + ), + ).toBeRevertedWithCustomError('NotController') + }) + }) }) describe('terms of each kind', () => { @@ -970,9 +1313,71 @@ describe('terms of each kind', () => { bob, ), ).toBeRevertedWithCustomError('TermsMismatch') + await expect( + acceptForRenewal(namePolicy, alice, { k, hash }), + ).toBeRevertedWithCustomError('TermsMismatch') } }) + it('renewal refuses an acceptance of the other terms', async () => { + const f = await load() + await acceptAndRegister(f, k) + await acceptRenewal(f, alice, { k: other, id: k.id, hash: other.hash }) + await expect( + renewAs(f.controller, alice, k), + ).toBeRevertedWithCustomErrorFrom(f.namePolicy, 'TermsNotAccepted') + await acceptRenewal(f, alice, { k }) + await renewAs(f.controller, alice, k) + }) + + it('a signed renewal acceptance reverts from the wrong signer, on replay and after its deadline', async () => { + const { namePolicy } = await load() + const relay = (deadline: bigint, sig: `0x${string}`) => + callPolicy( + namePolicy, + k.acceptForRenewalWithSig, + [k.id, 0n, YEAR, k.hash, bob.address, 0n, deadline, sig], + registrar, + ) + await expect( + relay( + FAR_FUTURE, + await signRenewalAcceptance(namePolicy, malloryClient, { k }), + ), + ).toBeRevertedWithCustomError('InvalidSignature') + await expect(relay(1n, '0x')).toBeRevertedWithCustomError( + 'SignatureExpired', + ) + const sig = await signRenewalAcceptance(namePolicy, bobClient, { k }) + await relay(FAR_FUTURE, sig) + expect( + await namePolicy.read.acceptedRenewalTerms([ + k.id, + bob.address, + 0n, + YEAR, + ]), + ).toBe(k.hash) + await expect(relay(FAR_FUTURE, sig)).toBeRevertedWithCustomError( + 'InvalidNonce', + ) + }) + + it('nothing is renewed before the link is set', async () => { + const f = await load() + await acceptAndRegister(f, k) + const unset = await policyWithoutTheseTerms(f) + await expect( + acceptForRenewal(unset, alice, { k }), + ).toBeRevertedWithCustomError('TermsMismatch') + await f.controller.write.setNamePolicy([unset.address], { + account: owner, + }) + await expect( + renewAs(f.controller, alice, k), + ).toBeRevertedWithCustomErrorFrom(unset, 'TermsNotAccepted') + }) + it('registration and claim refuse an acceptance of the other terms', async () => { const f = await load() const commitment = await f.controller.read.makeCommitment([reg]) diff --git a/test/simplex/TestDisputePeriod.test.ts b/test/simplex/TestDisputePeriod.test.ts index 3a8805b9..5be4e9fa 100644 --- a/test/simplex/TestDisputePeriod.test.ts +++ b/test/simplex/TestDisputePeriod.test.ts @@ -11,6 +11,7 @@ import { describe, expect, it } from 'vitest' import { acceptGeneralTermsForClaim, + acceptGeneralTermsForRenewal, AMPLE_ALLOWANCE, commitUnderGeneralTerms, deployNamesV2, @@ -295,6 +296,9 @@ describe('dispute period', () => { { account: bob }, ), ).toBeRevertedWithoutReason() + await acceptGeneralTermsForRenewal(connection, f.controller, 'nomoreclaim', YEAR, { + account: alice, + }) await expect( f.controller.write.renew(['nomoreclaim', YEAR, zeroHash], { account: alice, @@ -378,13 +382,16 @@ describe('dispute period', () => { ...EXACT, }), renewcredit: () => - f.sponsor.write.renewWithCredit(['renewcredit', YEAR, zeroHash], { + f.sponsor.write.renewWithCredit(['renewcredit', YEAR, zeroHash, alice.address], { account: registrar, ...EXACT, }), } for (const [label, renew] of Object.entries(paths)) { const until = await sponsored(f, label) + await acceptGeneralTermsForRenewal(connection, f.controller, label, YEAR, { + account: alice, + }) await exactly(until - 1n) await expect(renew()).toBeRevertedWithCustomErrorFrom( f.baseRegistrar, diff --git a/test/simplex/TestLaunchLifecycle.test.ts b/test/simplex/TestLaunchLifecycle.test.ts index 67fdb3db..72a3617f 100644 --- a/test/simplex/TestLaunchLifecycle.test.ts +++ b/test/simplex/TestLaunchLifecycle.test.ts @@ -14,6 +14,7 @@ import { import { beforeAll, describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, commitUnderGeneralTerms, COMMUNITY_TERMS_LINK, deployBehindProxy, @@ -28,6 +29,7 @@ import { PRICE_EXCEPTIONS, registerWithCredit, signGeneralTermsAcceptance, + signGeneralTermsForRenewal, signIntent, signRegistration, storageLayout, @@ -745,7 +747,17 @@ describe('launch lifecycle', () => { registrar.address, ]) await passDisputePeriod(connection) - await sponsor.write.renewWithCredit([OWNED, YEAR, zeroHash], { + await namePolicy.write.acceptGeneralTermsForRenewalWithSig( + await signGeneralTermsForRenewal( + connection, + controller, + OWNED, + YEAR, + alice.address, + ), + { account: registrar }, + ) + await sponsor.write.renewWithCredit([OWNED, YEAR, zeroHash, alice.address], { account: registrar, }) expect(await baseRegistrar.read.nameExpires([tokenId])).toBe(before + YEAR) @@ -1107,13 +1119,29 @@ describe('launch lifecycle', () => { ).toBeRevertedWithCustomErrorFrom(namePolicy, 'NameReserved') }) - it('anyone may renew anyone name — renewal is unauthenticated by design', async () => { + it('only the holder renews, after accepting the terms again', async () => { const tokenId = BigInt(labelhash('freshname')) const before = await baseRegistrar.read.nameExpires([tokenId]) - await controller.write.renew(['freshname', YEAR, zeroHash], { + const renew = (account: any) => + controller.write.renew(['freshname', YEAR, zeroHash], { + account, + value: 2n * yearPriceUSD(6), + }) + await acceptGeneralTermsForRenewal(connection, controller, 'freshname', YEAR, { account: bob, - value: 2n * yearPriceUSD(6), }) + await expect(renew(bob)).toBeRevertedWithCustomErrorFrom( + namePolicy, + 'NotHolder', + ) + await expect(renew(buyer)).toBeRevertedWithCustomErrorFrom( + namePolicy, + 'TermsNotAccepted', + ) + await acceptGeneralTermsForRenewal(connection, controller, 'freshname', YEAR, { + account: buyer, + }) + await renew(buyer) expect(await baseRegistrar.read.nameExpires([tokenId])).toBe(before + YEAR) }) diff --git a/test/simplex/TestNamePolicy.test.ts b/test/simplex/TestNamePolicy.test.ts index f65c1dfc..bc6c01ce 100644 --- a/test/simplex/TestNamePolicy.test.ts +++ b/test/simplex/TestNamePolicy.test.ts @@ -1,6 +1,8 @@ import hre from 'hardhat' import { + concat, getAddress, + keccak256, labelhash, pad, size, @@ -11,6 +13,7 @@ import { import { describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, commitUnderGeneralTerms, COMMUNITY_TERMS, COMMUNITY_TERMS_LINK, @@ -217,6 +220,27 @@ describe('name policy', () => { expect(await slot(6)).toBe(longString(GENERAL_TERMS_LINK)) expect(await slot(7)).toBe(GENERAL_TERMS) }) + + it('renewal acceptances take slot 8 of the proxy', async () => { + const { namePolicy } = await load() + const id = BigInt(labelhash('heldname')) + await namePolicy.write.acceptGeneralTermsForRenewal( + [id, 0n, YEAR, GENERAL_TERMS], + { account: alice }, + ) + const entry = (key: `0x${string}`, slot: `0x${string}`) => + keccak256(concat([key, slot])) + const slot = entry( + pad(toHex(YEAR)), + entry( + pad(toHex(0)), + entry(pad(alice.address), entry(pad(toHex(id)), pad(toHex(8)))), + ), + ) + expect( + await publicClient.getStorageAt({ address: namePolicy.address, slot }), + ).toBe(GENERAL_TERMS) + }) }) describe('registerReserved', () => { @@ -331,10 +355,17 @@ describe('name policy', () => { }) await expect(renew()).toBeRevertedWithCustomError('NameReserved') - const other = await policyWithNoReservations(f) + const other = await connection.viem.getContractAt( + 'SimplexNamePolicy', + (await policyWithNoReservations(f)).address, + ) + await other.write.setGeneralTerms([GENERAL_TERMS_LINK], { account: owner }) await f.controller.write.setNamePolicy([other.address], { account: owner, }) + await acceptGeneralTermsForRenewal(connection, f.controller, 'heldname', YEAR, { + account: alice, + }) await renew() }) diff --git a/test/simplex/TestPublicSalesSwitch.test.ts b/test/simplex/TestPublicSalesSwitch.test.ts index 73263c29..381b4071 100644 --- a/test/simplex/TestPublicSalesSwitch.test.ts +++ b/test/simplex/TestPublicSalesSwitch.test.ts @@ -3,6 +3,7 @@ import { zeroAddress, zeroHash } from 'viem' import { describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, AMPLE_ALLOWANCE, commitUnderGeneralTerms, deployNamesV2, @@ -96,11 +97,17 @@ describe('public sales switch', () => { await passDisputePeriod(connection) expect(await controller.read.publicSalesOpen()).toBe(false) + const accept = () => + acceptGeneralTermsForRenewal(connection, controller, 'renewany', YEAR, { + account: alice, + }) + await accept() await controller.write.renew(['renewany', YEAR, zeroHash], { account: alice, value: YEAR_PRICE_USD, }) - await sponsor.write.renewWithCredit(['renewany', YEAR, zeroHash], { + await accept() + await sponsor.write.renewWithCredit(['renewany', YEAR, zeroHash, alice.address], { account: registrar, }) }) diff --git a/test/simplex/TestRecipientConsent.test.ts b/test/simplex/TestRecipientConsent.test.ts index d644623a..db053c34 100644 --- a/test/simplex/TestRecipientConsent.test.ts +++ b/test/simplex/TestRecipientConsent.test.ts @@ -237,7 +237,7 @@ describe('recipient consent', () => { ).toBeRevertedWithCustomError('NotSponsor') await expect( controller.write.renewSponsored( - ['alicename', MIN_REGISTRATION, zeroHash], + ['alicename', MIN_REGISTRATION, zeroHash, alice.address], { account: registrar }, ), ).toBeRevertedWithCustomError('NotSponsor') diff --git a/test/simplex/TestRegisterReserved.test.ts b/test/simplex/TestRegisterReserved.test.ts index 79a8a738..f3b90d40 100644 --- a/test/simplex/TestRegisterReserved.test.ts +++ b/test/simplex/TestRegisterReserved.test.ts @@ -3,6 +3,7 @@ import { labelhash, zeroAddress, zeroHash } from 'viem' import { describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, AMPLE_ALLOWANCE, commitUnderGeneralTerms, deployNamesV2, @@ -33,6 +34,11 @@ async function fixture() { } const load = () => connection.networkHelpers.loadFixture(fixture) +const acceptBrandnameRenewal = (controller: any) => + acceptGeneralTermsForRenewal(connection, controller, 'brandname', YEAR, { + account: brand, + }) + const renewBrandname = (controller: any) => controller.write.renew(['brandname', YEAR, zeroHash], { account: brand, @@ -111,6 +117,7 @@ describe('registerReserved', () => { const id = BigInt(labelhash('brandname')) const before = await baseRegistrar.read.nameExpires([id]) + await acceptBrandnameRenewal(controller) await renewBrandname(controller) expect(await baseRegistrar.read.nameExpires([id])).toBe(before + YEAR) }) @@ -237,7 +244,9 @@ describe('a disputed name', () => { account: guardian, }) await expect( - sponsor.write.renewWithCredit(['brandname', YEAR, zeroHash], { account: registrar }), + sponsor.write.renewWithCredit(['brandname', YEAR, zeroHash, brand.address], { + account: registrar, + }), ).toBeRevertedWithCustomErrorFrom(controller, 'NameReserved') }) @@ -246,6 +255,7 @@ describe('a disputed name', () => { await namePolicy.write.removeReservedNames([['brandname']], { account: owner }) const id = BigInt(labelhash('brandname')) const before = await baseRegistrar.read.nameExpires([id]) + await acceptBrandnameRenewal(controller) await renewBrandname(controller) expect(await baseRegistrar.read.nameExpires([id])).toBe(before + YEAR) }) diff --git a/test/simplex/TestRegistrarAllowance.test.ts b/test/simplex/TestRegistrarAllowance.test.ts index f6a0648e..d57ec5c8 100644 --- a/test/simplex/TestRegistrarAllowance.test.ts +++ b/test/simplex/TestRegistrarAllowance.test.ts @@ -3,6 +3,7 @@ import { labelhash, zeroHash } from 'viem' import { describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, AMPLE_ALLOWANCE, commitUnderGeneralTerms, deployNamesV2, @@ -270,7 +271,10 @@ describe('registrar allowance', () => { const tokenId = BigInt(labelhash('renewable')) const before = await baseRegistrar.read.nameExpires([tokenId]) - await sponsor.write.renewWithCredit(['renewable', YEAR, zeroHash], { + await acceptGeneralTermsForRenewal(connection, controller, 'renewable', YEAR, { + account: alice, + }) + await sponsor.write.renewWithCredit(['renewable', YEAR, zeroHash, alice.address], { account: registrar, }) @@ -315,11 +319,17 @@ describe('registrar allowance', () => { await sponsoredRegister(controller, registration('decade', alice.address)) await passDisputePeriod(connection) const longest = 9n * YEAR + MIN_DISPUTE_DAYS * DAY - await sponsor.write.renewWithCredit(['decade', longest, zeroHash], { + await acceptGeneralTermsForRenewal(connection, controller, 'decade', longest, { + account: alice, + }) + await sponsor.write.renewWithCredit(['decade', longest, zeroHash, alice.address], { account: registrar, }) + await acceptGeneralTermsForRenewal(connection, controller, 'decade', YEAR, { + account: alice, + }) await expect( - sponsor.write.renewWithCredit(['decade', YEAR, zeroHash], { + sponsor.write.renewWithCredit(['decade', YEAR, zeroHash, alice.address], { account: registrar, }), ).toBeRevertedWithCustomErrorFrom(controller, 'DurationTooLong') diff --git a/test/simplex/TestSimplexController.test.ts b/test/simplex/TestSimplexController.test.ts index a4c8f6cc..53f1943c 100644 --- a/test/simplex/TestSimplexController.test.ts +++ b/test/simplex/TestSimplexController.test.ts @@ -2,6 +2,7 @@ import hre from 'hardhat' import { labelhash, namehash, zeroAddress, zeroHash } from 'viem' import { + acceptGeneralTermsForRenewal, commitUnderGeneralTerms, deployBehindProxy, deployNamePolicy, @@ -1209,6 +1210,9 @@ describe('SimplexController', () => { const id = BigInt(labelhash('renewme')) const before = await baseRegistrar.read.nameExpires([id]) const price = await controller.read.rentPrice(['renewme', RENEWAL_TIME]) + await acceptGeneralTermsForRenewal(connection, controller, 'renewme', RENEWAL_TIME, { + account: registrantAccount, + }) await controller.write.renew(['renewme', RENEWAL_TIME, zeroHash], { account: registrantAccount, value: price.base, @@ -1236,6 +1240,9 @@ describe('SimplexController', () => { const before = await baseRegistrar.read.nameExpires([id]) const year = 365n * DAY const price = await controller.read.rentPrice(['yearrenew', year]) + await acceptGeneralTermsForRenewal(connection, controller, 'yearrenew', year, { + account: registrantAccount, + }) await controller.write.renew(['yearrenew', year, zeroHash], { account: registrantAccount, value: price.base, @@ -1263,6 +1270,9 @@ describe('SimplexController', () => { await testClient.increaseTime({ seconds: Number(REGISTRATION_TIME) + Number(DAY) }) await testClient.mine({ blocks: 1 }) const price = await controller.read.rentPrice(['graceful', REGISTRATION_TIME]) + await acceptGeneralTermsForRenewal(connection, controller, 'graceful', REGISTRATION_TIME, { + account: registrantAccount, + }) await controller.write.renew(['graceful', REGISTRATION_TIME, zeroHash], { account: registrantAccount, value: price.base, @@ -1275,6 +1285,9 @@ describe('SimplexController', () => { await commitAndRegister(controller, 'renewexcess', registrantAccount) const balBefore = await publicClient.getBalance({ address: controller.address }) const price = await controller.read.rentPrice(['renewexcess', RENEWAL_TIME]) + await acceptGeneralTermsForRenewal(connection, controller, 'renewexcess', RENEWAL_TIME, { + account: registrantAccount, + }) await controller.write.renew(['renewexcess', RENEWAL_TIME, zeroHash], { account: registrantAccount, value: price.base + 12345n, @@ -1347,6 +1360,9 @@ describe('SimplexController', () => { const { controller } = await loadFixture() await commitAndRegister(controller, 'farrenew', registrantAccount) const price = await controller.read.rentPrice(['farrenew', RENEWAL_TIME + DAY]) + await acceptGeneralTermsForRenewal(connection, controller, 'farrenew', RENEWAL_TIME + DAY, { + account: registrantAccount, + }) await expect( controller.write.renew(['farrenew', RENEWAL_TIME + DAY, zeroHash], { account: registrantAccount, @@ -1585,6 +1601,9 @@ describe('SimplexController', () => { value: total, // exact — no refund, so register succeeds }) const price = await controller.read.rentPrice(['renewfail', RENEWAL_TIME]) + await acceptGeneralTermsForRenewal(connection, controller, 'renewfail', RENEWAL_TIME, { + account: rejecter.address, + }) await expect( controller.write.renew(['renewfail', RENEWAL_TIME, zeroHash], { account: rejecter.address, diff --git a/test/simplex/TestSponsoredPathIndependence.test.ts b/test/simplex/TestSponsoredPathIndependence.test.ts index 252fae64..481da324 100644 --- a/test/simplex/TestSponsoredPathIndependence.test.ts +++ b/test/simplex/TestSponsoredPathIndependence.test.ts @@ -3,6 +3,7 @@ import { labelhash, parseEventLogs, zeroHash } from 'viem' import { describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, AMPLE_ALLOWANCE, commitUnderGeneralTerms, deployNamesV2, @@ -65,7 +66,10 @@ describe('the sponsored path does not depend on the price feed', () => { const tokenId = BigInt(labelhash('deadfeed')) const before = await baseRegistrar.read.nameExpires([tokenId]) await passDisputePeriod(connection) - await sponsor.write.renewWithCredit(['deadfeed', YEAR, zeroHash], { + await acceptGeneralTermsForRenewal(connection, controller, 'deadfeed', YEAR, { + account: alice, + }) + await sponsor.write.renewWithCredit(['deadfeed', YEAR, zeroHash, alice.address], { account: registrar, }) expect(await baseRegistrar.read.nameExpires([tokenId])).toBe(before + YEAR) @@ -121,8 +125,11 @@ describe('the sponsored path does not depend on the price feed', () => { const { controller, sponsor } = await load() await sponsoredRegister(controller, 'renewevent') await passDisputePeriod(connection) + await acceptGeneralTermsForRenewal(connection, controller, 'renewevent', YEAR, { + account: alice, + }) const hash = await sponsor.write.renewWithCredit( - ['renewevent', YEAR, zeroHash], + ['renewevent', YEAR, zeroHash, alice.address], { account: registrar }, ) const receipt = await publicClient.getTransactionReceipt({ hash }) diff --git a/test/simplex/TestStaleRecords.test.ts b/test/simplex/TestStaleRecords.test.ts index 22fc9972..acc2d058 100644 --- a/test/simplex/TestStaleRecords.test.ts +++ b/test/simplex/TestStaleRecords.test.ts @@ -3,6 +3,7 @@ import { labelhash, zeroAddress as zeroAddressLocal, zeroHash } from 'viem' import { describe, expect, it } from 'vitest' import { + acceptGeneralTermsForRenewal, AMPLE_ALLOWANCE, commitUnderGeneralTerms, deployBehindProxy, @@ -233,7 +234,10 @@ describe('renewal and availability semantics', () => { await connection.networkHelpers.time.increase(Number(MIN_REGISTRATION + 1n)) const before = await sponsor.read.registrarAllowance([registrar.address]) - await sponsor.write.renewWithCredit(['renewprem', YEAR, zeroHash], { + await acceptGeneralTermsForRenewal(connection, controller, 'renewprem', YEAR, { + account: victim, + }) + await sponsor.write.renewWithCredit(['renewprem', YEAR, zeroHash, victim.address], { account: registrar, }) const spent = diff --git a/test/simplex/__snapshots__/storageLayouts.json b/test/simplex/__snapshots__/storageLayouts.json index 934dcb3b..a6f48a72 100644 --- a/test/simplex/__snapshots__/storageLayouts.json +++ b/test/simplex/__snapshots__/storageLayouts.json @@ -110,7 +110,8 @@ "5:0+32 SimplexNamePolicy.communityTermsLink string", "6:0+32 SimplexNamePolicy.generalTermsLink string", "7:0+32 SimplexNamePolicy.generalTermsHash bytes32", - "8:0+1344 SimplexNamePolicy.__gap uint256[42]" + "8:0+32 SimplexNamePolicy.acceptedRenewalTerms mapping(uint256 => mapping(address => mapping(uint256 => mapping(uint256 => bytes32))))", + "9:0+1312 SimplexNamePolicy.__gap uint256[41]" ], "SimplexSponsor": [ "0:0+32 SimplexSponsor.registrarAllowance mapping(address => uint256)", diff --git a/test/simplex/fixtures/namesV2.ts b/test/simplex/fixtures/namesV2.ts index 4ca6b2f0..6512dcc0 100644 --- a/test/simplex/fixtures/namesV2.ts +++ b/test/simplex/fixtures/namesV2.ts @@ -510,6 +510,22 @@ export const eip712Types = { { name: 'nonce', type: 'uint256' }, { name: 'deadline', type: 'uint256' }, ], + AcceptGeneralTermsForRenewal: [ + { name: 'tokenId', type: 'uint256' }, + { name: 'expires', type: 'uint256' }, + { name: 'duration', type: 'uint256' }, + { name: 'termsHash', type: 'bytes32' }, + { name: 'nonce', type: 'uint256' }, + { name: 'deadline', type: 'uint256' }, + ], + AcceptCommunityTermsForRenewal: [ + { name: 'tokenId', type: 'uint256' }, + { name: 'expires', type: 'uint256' }, + { name: 'duration', type: 'uint256' }, + { name: 'termsHash', type: 'bytes32' }, + { name: 'nonce', type: 'uint256' }, + { name: 'deadline', type: 'uint256' }, + ], } as const export const FAR_FUTURE = 4102444800n @@ -681,6 +697,88 @@ export async function acceptGeneralTermsForClaim( ) } +export async function acceptGeneralTermsForRenewal( + connection: NetworkConnection, + controller: any, + label: string, + duration: bigint, + options: { account: any }, +) { + const namePolicy = await connection.viem.getContractAt( + 'SimplexNamePolicy', + await controller.read.namePolicy(), + ) + const tokenId = BigInt(labelhash(label)) + return namePolicy.write.acceptGeneralTermsForRenewal( + [ + tokenId, + await nameExpires(connection, namePolicy, tokenId), + duration, + GENERAL_TERMS, + ], + options, + ) +} + +async function nameExpires( + connection: NetworkConnection, + namePolicy: any, + tokenId: bigint, +): Promise { + const registrar = await connection.viem.getContractAt( + 'BaseRegistrarImplementation', + await namePolicy.read.registrar(), + ) + return registrar.read.nameExpires([tokenId]) +} + +export async function signGeneralTermsForRenewal( + connection: NetworkConnection, + controller: any, + label: string, + duration: bigint, + holder: `0x${string}`, +): Promise< + [ + bigint, + bigint, + bigint, + `0x${string}`, + `0x${string}`, + bigint, + bigint, + `0x${string}`, + ] +> { + const namePolicy = await connection.viem.getContractAt( + 'SimplexNamePolicy', + await controller.read.namePolicy(), + ) + const client = (await connection.viem.getWalletClients()).find( + (c) => c.account.address.toLowerCase() === holder.toLowerCase(), + ) + if (!client) throw new Error(`no wallet client for ${holder}`) + const tokenId = BigInt(labelhash(label)) + const expires = await nameExpires(connection, namePolicy, tokenId) + const nonce: bigint = await namePolicy.read.nonces([holder]) + const sig = await signIntent( + await connection.viem.getPublicClient(), + client, + 'SimplexNamePolicy', + namePolicy.address, + 'AcceptGeneralTermsForRenewal', + { + tokenId, + expires, + duration, + termsHash: GENERAL_TERMS, + nonce, + deadline: FAR_FUTURE, + }, + ) + return [tokenId, expires, duration, GENERAL_TERMS, holder, nonce, FAR_FUTURE, sig] +} + /** Enough allowance that a test never has to think about it: $1000. */ export const AMPLE_ALLOWANCE = 1000n * 10n ** 18n