Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 87 additions & 6 deletions clk-gateway/src/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -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" }],
},
});
});
});
11 changes: 8 additions & 3 deletions clk-gateway/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down Expand Up @@ -367,7 +372,7 @@ const defaultTypes = {
],
};
export function buildTypedData(
value: Record<string, string>,
message: Record<string, string>,
types: Record<string, { name: string; type: string }[]> = defaultTypes
) {
/* Representative domain for the Name Service, this is a placeholder */
Expand All @@ -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 };
}

/**
Expand Down
12 changes: 3 additions & 9 deletions clk-gateway/src/routes/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
import {
buildTypedData,
validateSignature,
getDecodedToken,
checkUserByEmail,
asyncHandler,
fetchZyfiSponsored,
isParsableError,
Expand Down Expand Up @@ -220,7 +218,7 @@ router.post(

const typedData = buildTypedData(
{
name,
name: data.name,
key: data.key,
value: data.value,
},
Expand Down Expand Up @@ -366,9 +364,7 @@ router.post(
},
],
});
res.status(200).send({
typedData,
});
res.status(200).send(typedData);
})
);

Expand Down Expand Up @@ -431,9 +427,7 @@ router.post(
email: data.email || "example@not-valid.com",
});

res.status(200).send({
typedData,
});
res.status(200).send(typedData);
})
);

Expand Down
3 changes: 2 additions & 1 deletion src/nameservice/NameService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down