Skip to content

Commit

Permalink
Adding Message content for the signature. (#13)
Browse files Browse the repository at this point in the history
* Adding Message content for the signature.

* Removing POH check.
  • Loading branch information
mario-christopher authored Mar 12, 2024
1 parent bc2460e commit 97be9cb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 36 deletions.
48 changes: 20 additions & 28 deletions packages/functions/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const headers = {
'Content-Type': 'application/json',
};

export type Payload = {
address: Address;
signedOn: number;
subject: string;
};

/**
* This function is called on every network call.
* @param event - The event object.
Expand Down Expand Up @@ -109,22 +115,23 @@ async function checkSignature(body: string) {
};
}

const { signature, address }: { signature: Hex; address: Address } =
const { signature, payload }: { signature: Hex; payload: Payload } =
JSON.parse(body);

if (!signature || !address) {
if (!signature || !payload) {
return {
statusCode: 400,
headers,
body: JSON.stringify({
message: 'Signature or address not provided',
message: 'Signature or payload not provided',
}),
};
}

const message = JSON.stringify(payload);
const valid = await verifyMessage({
address,
message: 'Example message.',
address: payload.address,
message,
signature,
});

Expand All @@ -138,28 +145,6 @@ async function checkSignature(body: string) {
};
}

/* const response = await fetch(
`https://linea-xp-poh-api.linea.build/poh/${address}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
},
);
const result = await response.json();
if (!result.poh) {
return {
statusCode: 400,
headers,
body: JSON.stringify({
message: "Doesn't have a valid POH",
}),
};
}*/

try {
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];

Expand All @@ -183,7 +168,14 @@ async function checkSignature(body: string) {
};
}

await sheet.addRow([Date.now(), address as string]);
// Google Sheet Columns : timestamp, address, signedOn, subject, signature
await sheet.addRow([
Date.now(),
payload.address as string,
payload.signedOn,
payload.subject,
signature,
]);

return {
statusCode: 201,
Expand Down
8 changes: 7 additions & 1 deletion packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ const Index = () => {
const handlePersonalSign = async () => {
setClaimMessage('Pending...');
try {
const message = `0x${stringToHex('Example message.')}`;
const payload = {
address: connectedAccount,
signedOn: Date.now(),
subject: 'LXP Snap Activation',
};
const message = `0x${stringToHex(JSON.stringify(payload))}`;

const signature = await window.ethereum.request({
method: 'personal_sign',
Expand All @@ -168,6 +173,7 @@ const Index = () => {
method: 'personalSign',
params: {
signature,
payload,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/Consensys/lxp-snap"
},
"source": {
"shasum": "HjlzN/jmHtir7dVCYXU7DanPbsH9UirSbSnXsU6Cc+A=",
"shasum": "gzwnxtUZZDMLFZ1N3d/bjfjv07te6Wn+CCro4me+jJY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
6 changes: 4 additions & 2 deletions packages/snap/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Payload } from './types';

const getData = async (url: string) => {
const response = await fetch(url, {
method: 'GET',
Expand Down Expand Up @@ -38,13 +40,13 @@ export const fetchLxpActivations = async () => {

export const postAddressRegistration = async (
signature: string,
address: string,
payload: Payload,
) => {
const response = await postData(
'https://lxp-snap-api.netlify.app/.netlify/functions/api',
{
signature,
address,
payload,
},
);

Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
}

case 'personalSign': {
const { signature } = params;
return registerAddress(signature);
const { signature, payload } = params;
return registerAddress(signature, payload);
}

default:
Expand Down
6 changes: 4 additions & 2 deletions packages/snap/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
fetchPohStatus,
postAddressRegistration,
} from './api';
import type { Payload } from './types';
import { convertBalanceToDisplay, getState } from './utils';

/**
Expand Down Expand Up @@ -79,14 +80,15 @@ export async function getCurrentActivations() {
/**
* Registers the address as using the snap.
* @param signature - The signature to check before registering the address.
* @param payload - The content of the message that was signed.
* @returns The status of the registration.
*/
export async function registerAddress(signature: string) {
export async function registerAddress(signature: string, payload: Payload) {
const { lxpAddress } = await getState();

if (!lxpAddress) {
throw new Error('No LXP address found.');
}

return await postAddressRegistration(signature, lxpAddress);
return await postAddressRegistration(signature, payload);
}
8 changes: 8 additions & 0 deletions packages/snap/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Address } from '@metamask/snaps-sdk';

export type Captions = {
locale: string;
poh: {
Expand Down Expand Up @@ -45,3 +47,9 @@ export type SnapState = {
myPohStatus?: boolean;
activations?: Activation[];
};

export type Payload = {
address: Address;
signedOn: number;
subject: string;
};

0 comments on commit 97be9cb

Please sign in to comment.