Skip to content

Commit

Permalink
working on auth & fixing all-category playbooks 🍂
Browse files Browse the repository at this point in the history
  • Loading branch information
dysbulic committed Jul 19, 2024
1 parent 83e942c commit 1204ce2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
20 changes: 9 additions & 11 deletions packages/utils/src/did/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
import { signMessage, verifyMessage } from '@wagmi/core';
import { ethers } from 'ethers';
import { Base64 } from 'js-base64';
import { v4 as uuidv4 } from 'uuid';
import { Client, PublicClient, WalletClient } from 'viem';
import { PublicClient, WalletClient } from 'viem';

import { wagmiMainnetConfig } from '../constants.js';
import { verifySignature } from '../ethereumHelper.js';
import { Maybe } from '../extendedProfileTypes.js';

const tokenDuration = 1000 * 60 * 60 * 24 * 7; // 7 days

const WELCOME_MESSAGE = `Welcome to MetaGame Anon 🐙\n\nPlease sign this message so we know it is you.\n\nWe care about privacy and assure you, we don't harvest your data. Unless you create a Player account, we simply store a token in your browser's local storage. This can be removed by using the disconnect button.\n\n`;

type Claim = {
iat: Date;
exp: Date;
iat: number;
exp: number;
iss: string;
aud: string;
tid: string;
};

export async function createToken(walletClient: WalletClient) {
export async function createToken(
{ client, account }: { client: WalletClient, account: `0x${string}` }
) {
const iat = new Date().getTime();

const { account } = walletClient
if(!account) {
throw new Error('No account found in Viem signing client.')
}

const claim = {
const claim: Claim = {
iat,
exp: iat + tokenDuration,
iss: account.address,
iss: account,
aud: 'the-game',
tid: uuidv4(),
};

const serializedClaim = JSON.stringify(claim);
const message = `${WELCOME_MESSAGE}${serializedClaim}`;
const proof = await walletClient.signMessage({ account, message });
const proof = await client.signMessage({ account, message });

return Base64.encode(JSON.stringify([proof, serializedClaim]));
}
Expand Down
53 changes: 34 additions & 19 deletions packages/web/contexts/Web3Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ export async function getExistingAuth(
}
}

export async function authenticateWallet(client: WalletClient) {
const token = await did.createToken(client);
export async function authenticateWallet(
{ client, signer }: { client: WalletClient, signer: `0x${string}` }
) {
const token = await did.createToken({ client, account: signer });
setTokenInStore(token);
return token;
}
Expand Down Expand Up @@ -137,24 +139,37 @@ export const Web3ContextProvider: React.FC<Web3ContextProviderOptions> = ({

const updateWeb3State = useCallback(
async () => {
const network = chain?.id;
if (!viemClients || !userAddress || !network) return;
let token = await getExistingAuth(viemClients.public, userAddress);

if (!token) {
token = await authenticateWallet(viemClients.wallet);
try {
const network = chain?.id;

if(!userAddress) {
throw new Error('No user address set in authentication.')
}
if(!network) {
throw new Error('No network configured in authentication.')
}

let token = await getExistingAuth(viemClients.public, userAddress);

if (!token) {
token = await authenticateWallet(
{ client: viemClients.wallet, signer: userAddress }
);
}

const networkId = `0x${network.toString(16)}`;

setWeb3State({
provider: wagmiProvider ?? null,
viemClients: viemClients ?? null,
chainId: networkId,
address: userAddress,
authToken: token,
w3storage,
});
} catch (err) {
console.warn(`Error Authenticating: ${(err as Error).message}`);
}

const networkId = `0x${network.toString(16)}`;

setWeb3State({
provider: wagmiProvider ?? null,
viemClients: viemClients ?? null,
chainId: networkId,
address: userAddress,
authToken: token,
w3storage,
});
},
[chain?.id, userAddress, viemClients, w3storage, wagmiProvider],
);
Expand Down
9 changes: 5 additions & 4 deletions packages/web/pages/academy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import React, { lazy } from 'react';
import {
QuestChainPathsAndPlaybooksDetails,
QuestChainsCategories,
QuestChainsCategoriesDetails,
} from 'utils/questChains';

Expand Down Expand Up @@ -93,8 +94,8 @@ const AcademyPage: React.FC = () => {

const allItems = Object.entries(
QuestChainPathsAndPlaybooksDetails,
).filter(([, { category: cat }]) => cat === 'all');
categoryItems.concat(allItems);
).filter(([, { category: cat }]) => cat === QuestChainsCategories.ALL);
categoryItems.push(...allItems);

return (
<VStack
Expand Down Expand Up @@ -188,8 +189,8 @@ const AcademyPage: React.FC = () => {
Why not{' '}
<MetaLink href="https://chat.metagame.wtf/" isExternal>
join the Discord
</MetaLink>{' '}
and find out how to create one and get it added!
</MetaLink>,{' '}
find out how to create one, and get it added!
</Text>
</Box>
)}
Expand Down

0 comments on commit 1204ce2

Please sign in to comment.