Skip to content

Commit

Permalink
extend signing
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre committed Oct 3, 2024
1 parent b3a0522 commit 93c5390
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/app/delegate/api/register/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from 'fs';
import path from 'path';
import * as utils from 'src/features/delegation/utils';
import { getValidRequest } from 'src/test/delegatee-registration-utils';
import { vi } from 'vitest';
import { expect, it, vi } from 'vitest';
import { POST } from './route';

const getRequest = (formData: FormData): Request => {
Expand Down Expand Up @@ -67,7 +67,7 @@ it('successfuly calls PR creation', async () => {
},
"interests": "blockchain, NFTs",
"name": "Delegatee name",
"signature": "0xe37dfb7abd8d7668057189d980fed5ccc08716ab45cabd70638305b91366c8693fb307fc39b5d8c46cebafee318bffa4a4df9d80f7a40b121613cdcb6cc582051c",
"signature": "0x52a3c23ef6c6817691872b77615ef30927453d641acd8c607de458d39e581bcd5411f723102640897af151644086abf4f3a9baf216d684d784194aef2c6730be1c",
"twitterUrl": "https://example.com/x",
"verificationUrl": "https://example.com/verification",
"websiteUrl": "https://example.com",
Expand Down
21 changes: 17 additions & 4 deletions src/features/delegation/DelegateRegistrationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RegisterDelegateResponseStatus,
} from 'src/features/delegation/types';
import { validateRegistrationRequest } from 'src/features/delegation/validateRegistrationRequest';
import { logger } from 'src/utils/logger';
import { useAccount } from 'wagmi';

const initialValues: RegisterDelegateFormValues = {
Expand Down Expand Up @@ -81,13 +82,25 @@ export function DelegateRegistrationForm({
...initialValues,
}}
onSubmit={async (values) => {
let signature: HexString;

setIsSubmitting(true);
setIsSigning(true);

const signature = await signForm({
...values,
address: address!
});
try {
signature = await signForm({
...values,
address: address!,
image: imageFile
});
} catch (err) {
setIsSubmitting(false);

toast.error('Error while signing message');
logger.error(err);

return;
}

setIsSigning(false);

Expand Down
19 changes: 11 additions & 8 deletions src/features/delegation/hooks/useSIgnedData.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { EIP712Delegatee, RegisterDelegateFormValues } from 'src/features/delegation/types';
import { sha256 } from 'viem';
import { useSignTypedData } from 'wagmi';

export function useSignedData() {
const { signTypedDataAsync } = useSignTypedData();
return ({
name,
address,
verificationUrl,
}: Pick<RegisterDelegateFormValues, 'address' | 'name' | 'verificationUrl'>) => {

return async (values: RegisterDelegateFormValues) => {
if (!values.image) {
throw new Error('Image required');
}

return signTypedDataAsync({
...EIP712Delegatee,
message: {
name,
address,
verificationUrl,
...values,
imageSha: sha256(new Uint8Array(await values.image.arrayBuffer())),
websiteUrl: values.websiteUrl || '',
twitterUrl: values.twitterUrl || '',
},
});
};
Expand Down
7 changes: 6 additions & 1 deletion src/features/delegation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ export type Delegatee = DelegateeMetadata & {
export const EIP712Delegatee = {
types: {
Delegatee: [
{ name: 'name', type: 'string' },
{ name: 'address', type: 'address' },
{ name: 'name', type: 'string' },
{ name: 'verificationUrl', type: 'string' },
{ name: 'websiteUrl', type: 'string' },
{ name: 'twitterUrl', type: 'string' },
{ name: 'interests', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'imageSha', type: 'string' },
],
},
primaryType: 'Delegatee',
Expand Down
4 changes: 3 additions & 1 deletion src/features/delegation/validateRegistrationRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RegisterDelegateRequest } from 'src/features/delegation/types';
import * as utils from 'src/features/delegation/utils';
import { validateRegistrationRequest } from 'src/features/delegation/validateRegistrationRequest';
import { getValidRequest } from 'src/test/delegatee-registration-utils';
import { vi } from 'vitest';
import { expect, it, vi } from 'vitest';

it('validates successfully', async () => {
const request = await getValidRequest();
Expand Down Expand Up @@ -88,6 +88,7 @@ it('validates wrong URLs', async () => {

expect(await validateRegistrationRequest(request, true)).toMatchInlineSnapshot(`
{
"signature": "Invalid signature",
"twitterUrl": "Invalid url",
"websiteUrl": "Invalid url",
}
Expand All @@ -106,6 +107,7 @@ it('validates invalid image', async () => {
expect(await validateRegistrationRequest(request, true)).toMatchInlineSnapshot(`
{
"image": "Invalid image",
"signature": "Invalid signature",
}
`);
});
16 changes: 12 additions & 4 deletions src/features/delegation/validateRegistrationRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
RegisterDelegateRequest,
} from 'src/features/delegation/types';
import { isAddressAnAccount } from 'src/features/delegation/utils';
import { isAddress, verifyTypedData } from 'viem';
import { isAddress, sha256, verifyTypedData } from 'viem';

export const validateRegistrationRequest = async (
request: RegisterDelegateRequest,
Expand Down Expand Up @@ -92,20 +92,28 @@ function toSchemaConformantValues(request: RegisterDelegateRequest) {
return values;
}

function verifySigner(request: RegisterDelegateRequest) {
const verifySigner = async (request: RegisterDelegateRequest) => {
if (!request.signature) {
throw new Error('Signature required');
}

if (!request.image) {
throw new Error('Image required');
}

return verifyTypedData({
...EIP712Delegatee,
address: request.address,
signature: request.signature,
message: {
// TODO add remaining fields
address: request.address,
name: request.name,
verificationUrl: request.verificationUrl,
websiteUrl: request.websiteUrl || '',
twitterUrl: request.twitterUrl || '',
interests: request.interests,
description: request.description,
imageSha: sha256(new Uint8Array(await request.image.arrayBuffer())),
},
});
}
};
20 changes: 16 additions & 4 deletions src/test/delegatee-registration-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import { EIP712Delegatee, RegisterDelegateRequest } from 'src/features/delegation/types';
import { createWalletClient, http } from 'viem';
import { createWalletClient, http, sha256 } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { celo } from 'viem/chains';

Expand All @@ -27,12 +27,24 @@ export const getValidRequest = async (): Promise<RegisterDelegateRequest> => {
transport: http(),
});

const requestWithoutSignature = {
address: TEST_ACCOUNT_ADDRESS as Address,
description,
image: new File([imageFile], 'clabs.jpg', { type: 'image/jpeg' }),
interests,
name,
twitterUrl,
verificationUrl,
websiteUrl: websiteUrl as string,
} as RegisterDelegateRequest;

const signature = await walletClient.signTypedData({
...EIP712Delegatee,
message: {
address: TEST_ACCOUNT_ADDRESS,
name,
verificationUrl,
...requestWithoutSignature,
imageSha: sha256(new Uint8Array(imageFile)),
websiteUrl: requestWithoutSignature.websiteUrl || '',
twitterUrl: requestWithoutSignature.twitterUrl || '',
},
});

Expand Down

0 comments on commit 93c5390

Please sign in to comment.