Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proof): connect to spirekey sdk #2373

Merged
merged 29 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1ac2ccc
Scaffold sdk implement connect
EnoF Jul 9, 2024
ee32a73
Implement minting attendance token using spirekey sdk sign
EnoF Jul 9, 2024
25e496c
Wait for account to be minted
EnoF Jul 10, 2024
a0b2fb4
Merge branch 'main' into feat/proof-of-us-spirekey-sdk
sstraatemans Jul 23, 2024
a092998
stay logged in
sstraatemans Jul 24, 2024
08e285b
working as expected
sstraatemans Jul 24, 2024
adf07c1
fix build
sstraatemans Jul 24, 2024
9867545
fix attendance token
sstraatemans Jul 24, 2024
8910fce
add a trycatch for the login call
sstraatemans Jul 24, 2024
162bdb4
adding try/catches to all spirekey calls
sstraatemans Jul 24, 2024
b95f491
fix build
sstraatemans Jul 24, 2024
d35623d
Merge branch 'main' into feat/proof-of-us-spirekey-sdk
sstraatemans Jul 26, 2024
7cdc0a7
update spirekey sdk version
sstraatemans Jul 26, 2024
2880360
latest
sstraatemans Jul 26, 2024
3227090
trying to test
sstraatemans Jul 26, 2024
929e74f
last try
sstraatemans Jul 26, 2024
7061524
fix for the tests
sstraatemans Jul 29, 2024
ff534d6
fix the login of pou
sstraatemans Jul 29, 2024
17bab3f
add extra test
sstraatemans Jul 29, 2024
2aa2bb2
fix the new spirekey account names with the '.'
sstraatemans Aug 30, 2024
5480492
Merge branch 'main' into feat/proof-of-us-spirekey-sdk
sstraatemans Aug 30, 2024
bd04bfa
Merge branch 'main' into feat/proof-of-us-spirekey-sdk
sstraatemans Sep 2, 2024
52488e7
fix e2e tests
sstraatemans Sep 2, 2024
f1813a3
Merge branch 'main' into feat/proof-of-us-spirekey-sdk
sstraatemans Sep 2, 2024
2735ca6
cleanup
sstraatemans Sep 2, 2024
3a57305
fix theme
sstraatemans Sep 4, 2024
39e5446
Merge branch 'main' into feat/proof-of-us-spirekey-sdk
sstraatemans Sep 4, 2024
6326d6e
fixing the loader
sstraatemans Sep 5, 2024
a54914c
fix build
sstraatemans Sep 6, 2024
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
1 change: 1 addition & 0 deletions packages/apps/proof-of-us/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/strict-boolean-expressions': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/consistent-type-imports': 'off',
},
};
4 changes: 3 additions & 1 deletion packages/apps/proof-of-us/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ certificates

# generated files
src/__generated__
!src/__generated__/.gitkeep
!src/__generated__/.gitkeep
.vercel
.env*.local
1 change: 1 addition & 0 deletions packages/apps/proof-of-us/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@kadena/kode-icons": "workspace:*",
"@kadena/kode-ui": "workspace:*",
"@kadena/pactjs": "workspace:*",
"@kadena/spirekey-sdk": "0.0.1-dev-12",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use 13?

"@pinata/sdk": "^2.1.0",
"@vanilla-extract/css": "1.14.2",
"@vanilla-extract/recipes": "0.5.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';
import { env } from '@/utils/env';
import { getAccountCookieName } from '@/utils/getAccountCookieName';
import { getReturnUrl } from '@/utils/getReturnUrl';
import { store } from '@/utils/socket/store';
import { useRouter, useSearchParams } from 'next/navigation';
import { connect, initSpireKey } from '@kadena/spirekey-sdk';
import { useRouter } from 'next/navigation';
import type { FC, PropsWithChildren } from 'react';
import { createContext, useCallback, useEffect, useState } from 'react';

Expand All @@ -30,27 +30,17 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
const [account, setAccount] = useState<IAccount>();
const [isMounted, setIsMounted] = useState(false);
const router = useRouter();
const searchParams = useSearchParams();

const decodeAccount = useCallback((userResponse: string) => {
if (!userResponse) return;
const login = useCallback(async () => {
try {
const account: IAccount = JSON.parse(
Buffer.from(userResponse, 'base64').toString(),
);
return account;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
return;
setIsMounted(true);
const acc = await connect(env.NETWORKID, env.CHAINID);
setAccount(acc);
localStorage.setItem(getAccountCookieName(), JSON.stringify(acc));
store.saveAlias(account);
} catch (e) {
localStorage.removeItem(getAccountCookieName());
}
}, []);

const login = useCallback(() => {
router.push(
`${env.WALLET_URL}/connect?returnUrl=${getReturnUrl([
'user',
])}&networkId=${env.NETWORKID}&chainId=${env.CHAINID}&optimistic=true`,
);
}, [router]);

const logout = useCallback(() => {
Expand All @@ -59,38 +49,22 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
router.replace('/');
}, []);

const loginResponse = useCallback(async () => {
const innerSearchParams = new URLSearchParams(window.location.search);
const userResponse = innerSearchParams.has('user')
? innerSearchParams.get('user')
: localStorage.getItem(getAccountCookieName());

if (!userResponse) {
setIsMounted(true);
return;
useEffect(() => {
const storage = localStorage.getItem(getAccountCookieName());
if (storage) {
try {
setAccount(JSON.parse(storage));
} catch (e) {
localStorage.removeItem(getAccountCookieName());
}
}

if (innerSearchParams.has('user')) {
localStorage.setItem(getAccountCookieName(), userResponse);
}
const account = decodeAccount(userResponse);
store.saveAlias(account);
setAccount(account);
initSpireKey({
hostUrl: 'https://spirekey.kadena.io',
});
setIsMounted(true);

if (searchParams.has('user')) {
setTimeout(() => {
router.replace(getReturnUrl(['user']));
}, 100);
}
}, [setAccount, setIsMounted, searchParams, decodeAccount, router]);

useEffect(() => {
loginResponse();
}, []);

console.log({ account }, account?.accountName);

return (
<AccountContext.Provider value={{ account, login, logout, isMounted }}>
{children}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { useAccount } from '@/hooks/account';
import { Stack } from '@kadena/kode-ui';
import type { FC, PropsWithChildren } from 'react';
import { useEffect } from 'react';
import { Button } from '../Button/Button';

export const LoginBoundry: FC<PropsWithChildren> = ({ children }) => {
const { account, isMounted, login } = useAccount();

useEffect(() => {
if (!account && isMounted) {
login();
}
}, [account, isMounted]);
const handleLogin = () => {
login();
};

if (!isMounted) return;

if (isMounted && !account) {
return (
<Stack
width="100%"
height="100%"
justifyContent="center"
alignItems="center"
paddingInline="xxxl"
>
<Button onPress={handleLogin}>Login</Button>
</Stack>
);
}

return <>{children}</>;
};
76 changes: 0 additions & 76 deletions packages/apps/proof-of-us/src/components/MintView/MintView.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions packages/apps/proof-of-us/src/components/MintView/style.css.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface IProofOfUsContext {
hasSigned: () => Promise<boolean>;
isSignee: () => Promise<boolean>;
getSignee: () => Promise<IProofOfUsSignee | undefined>;
getSignees: () => Promise<IProofOfUsSignee[]>;
updateSigneePing: (signee: IProofOfUsSignee) => Promise<void>;
updateProofOfUs: (value: any) => Promise<void>;
getSignature: (tx: IUnsignedCommand) => Promise<string | undefined>;
Expand All @@ -69,6 +70,7 @@ export const ProofOfUsContext = createContext<IProofOfUsContext>({
hasSigned: async () => false,
isSignee: async () => false,
getSignee: async () => undefined,
getSignees: async () => [],
updateSigneePing: async () => {},
updateProofOfUs: async () => {},
getSignature: async () => undefined,
Expand Down Expand Up @@ -299,6 +301,12 @@ export const ProofOfUsProvider: FC<IProps> = ({ children, proofOfUsId }) => {
return signee;
};

const getSignees = async (): Promise<IProofOfUsSignee[]> => {
if (!proofOfUs) return [];

return store.getProofOfUsSignees(proofOfUs.proofOfUsId);
};

const updateSigneePing = async (signee: IProofOfUsSignee): Promise<void> => {
if (!proofOfUs) return;
return store.updateSigneePing(proofOfUs, signee);
Expand Down Expand Up @@ -359,6 +367,7 @@ export const ProofOfUsProvider: FC<IProps> = ({ children, proofOfUsId }) => {
hasSigned,
isSignee,
getSignee,
getSignees,
updateSigneePing,
getSignature,
resetSignatures,
Expand Down
12 changes: 10 additions & 2 deletions packages/apps/proof-of-us/src/components/Providers/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { localStorageProvider } from '@/utils/localStorageProvider';
import type { NormalizedCacheObject } from '@apollo/client';
import { ApolloClient, ApolloProvider, InMemoryCache } from '@apollo/client';

import { darkThemeClass } from '@kadena/kode-ui/styles';
import { ThemeProvider } from 'next-themes';
import type { FC, PropsWithChildren } from 'react';
import { SWRConfig } from 'swr';
Expand All @@ -26,7 +26,15 @@ export const Providers: FC<PropsWithChildren> = ({ children }) => {
<ApolloProvider client={client}>
<AccountProvider>
<TokenProvider>
<ThemeProvider>{children}</ThemeProvider>
<ThemeProvider
attribute="class"
value={{
light: 'light',
dark: darkThemeClass,
}}
>
{children}
</ThemeProvider>
</TokenProvider>
</AccountProvider>
</ApolloProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,9 @@ export const ShareView: FC<IProps> = ({ prev, status }) => {
const transactionData = await createTx();

if (!transactionData) return;
const transaction = Buffer.from(
JSON.stringify(transactionData.transaction),
).toString('base64');

updateProofOfUs({
tx: transaction,
tx: JSON.stringify(transactionData.transaction),
requestKey: transactionData.transaction?.hash,
tokenId: transactionData.tokenId,
manifestData: JSON.stringify(transactionData.manifest),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AvatarEditor } from '@/components/AvatarEditor/AvatarEditor';
import { DetailView } from '@/components/DetailView/DetailView';
import { MintView } from '@/components/MintView/MintView';
import { ShareView } from '@/components/ShareView/ShareView';
import { useProofOfUs } from '@/hooks/proofOfUs';
import { createProofOfUsID } from '@/utils/createProofOfUsID';
Expand Down Expand Up @@ -56,7 +55,6 @@ export const CreateProofOfUs: FC<IProps> = ({ params }) => {
{status === 1 && <AvatarEditor next={next} status={status} />}
{status === 2 && <DetailView next={next} prev={prev} />}
{status === 3 && <ShareView next={next} prev={prev} status={status} />}
{status >= 4 && <MintView next={next} prev={prev} status={status} />}
</div>
);
};
Loading