diff --git a/clk-gateway/src/helpers.test.ts b/clk-gateway/src/helpers.test.ts index 46f2acd..0413267 100644 --- a/clk-gateway/src/helpers.test.ts +++ b/clk-gateway/src/helpers.test.ts @@ -1,13 +1,94 @@ -import { toLengthPrefixedBytes } from "./helpers"; +import { buildTypedData, toLengthPrefixedBytes } from "./helpers"; +import { NAME_SERVICE_INTERFACE } from "./interfaces"; +import { + buildZyfiRegisterRequest, + buildZyfiSetTextRecordRequest, + nameServiceAddresses, + zyfiRequestTemplate, +} from "./setup"; +const testOwner = "0x1234567890123456789012345678901234567890"; +const testSubdomain = "clk"; +const testTLD = "eth"; +const testName = "example"; describe("toLengthPrefixedBytes", () => { - test("example.click.eth", () => { - const result = toLengthPrefixedBytes("example", "click", "eth"); + test("example.clk.eth", () => { + const result = toLengthPrefixedBytes(testOwner, testSubdomain, testTLD); + console.log(result); expect(result).toEqual( Uint8Array.from([ - 7, 101, 120, 97, 109, 112, 108, 101, 5, 99, 108, 105, 99, 107, 3, 101, - 116, 104, - ]), + 42, 48, 120, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, + 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 48, 3, 99, 108, 107, 3, 101, 116, 104, + ]) + ); + }); +}); + +describe("buildZyfiRegisterRequest", () => { + test("example.clk.eth", () => { + const encodedRegister = NAME_SERVICE_INTERFACE.encodeFunctionData( + "register", + [testOwner, testName] + ); + const result = buildZyfiRegisterRequest(testOwner, testName, testSubdomain); + expect(result).toEqual({ + ...zyfiRequestTemplate, + txData: { + ...zyfiRequestTemplate.txData, + data: encodedRegister, + to: nameServiceAddresses[testSubdomain], + }, + }); + }); +}); + +describe("buildZyfiSetTextRecordRequest", () => { + test("example.click.eth", () => { + const encodedSetTextRecord = NAME_SERVICE_INTERFACE.encodeFunctionData( + "setTextRecord", + [testName, "test", "test"] + ); + const result = buildZyfiSetTextRecordRequest( + testName, + testSubdomain, + "test", + "test" + ); + expect(result).toEqual({ + ...zyfiRequestTemplate, + txData: { + ...zyfiRequestTemplate.txData, + data: encodedSetTextRecord, + to: nameServiceAddresses[testSubdomain], + }, + }); + }); +}); + +describe("buildTypedData", () => { + test("example.clk.eth", () => { + const result = buildTypedData( + { + test: "test", + }, + { + Test: [{ name: "test", type: "string" }], + } ); + + expect(result).toEqual({ + domain: { chainId: 300, name: "Nodle Name Service", version: "1" }, + message: { test: "test" }, + primaryType: "Test", + types: { + EIP712Domain: [ + { name: "name", type: "string" }, + { name: "version", type: "string" }, + { name: "chainId", type: "uint256" }, + ], + Test: [{ name: "test", type: "string" }], + }, + }); }); }); diff --git a/clk-gateway/src/helpers.ts b/clk-gateway/src/helpers.ts index 104ea1d..394679f 100644 --- a/clk-gateway/src/helpers.ts +++ b/clk-gateway/src/helpers.ts @@ -266,10 +266,15 @@ export function validateSignature({ expectedSigner: string; }) { try { + if (typedData.types.EIP712Domain) { + // @ts-ignore + delete typedData.types.EIP712Domain; + } + const signerAddress = verifyTypedData( typedData.domain, typedData.types, - typedData.value, + typedData.message, signature ); @@ -367,7 +372,7 @@ const defaultTypes = { ], }; export function buildTypedData( - value: Record, + message: Record, types: Record = defaultTypes ) { /* Representative domain for the Name Service, this is a placeholder */ @@ -387,7 +392,7 @@ export function buildTypedData( const primaryType = Object.keys(types)?.[0] || "Transaction"; - return { types: { ...domainTypes, ...types }, domain, value, primaryType }; + return { types: { ...domainTypes, ...types }, domain, message, primaryType }; } /** diff --git a/clk-gateway/src/routes/names.ts b/clk-gateway/src/routes/names.ts index c7b3e73..11236b8 100644 --- a/clk-gateway/src/routes/names.ts +++ b/clk-gateway/src/routes/names.ts @@ -17,8 +17,6 @@ import { import { buildTypedData, validateSignature, - getDecodedToken, - checkUserByEmail, asyncHandler, fetchZyfiSponsored, isParsableError, @@ -220,7 +218,7 @@ router.post( const typedData = buildTypedData( { - name, + name: data.name, key: data.key, value: data.value, }, @@ -366,9 +364,7 @@ router.post( }, ], }); - res.status(200).send({ - typedData, - }); + res.status(200).send(typedData); }) ); @@ -431,9 +427,7 @@ router.post( email: data.email || "example@not-valid.com", }); - res.status(200).send({ - typedData, - }); + res.status(200).send(typedData); }) ); diff --git a/src/nameservice/NameService.sol b/src/nameservice/NameService.sol index b584986..4b88e49 100644 --- a/src/nameservice/NameService.sol +++ b/src/nameservice/NameService.sol @@ -228,8 +228,9 @@ contract NameService is INameService, ERC721Burnable, AccessControl { bool isOwner = _ownerOf(tokenId) == msg.sender; bool isAdmin = hasRole(DEFAULT_ADMIN_ROLE, msg.sender); + bool isRegistrar = hasRole(REGISTERER_ROLE, msg.sender); - if (!isOwner && !isAdmin) { + if (!isOwner && !isAdmin && !isRegistrar) { revert NotAuthorized(); } if (expires[tokenId] <= block.timestamp) {