Skip to content

Commit

Permalink
update auth path
Browse files Browse the repository at this point in the history
  • Loading branch information
daoauth committed Sep 10, 2024
1 parent e9b956a commit 8756f7d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 49 additions & 11 deletions packages/holder/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import queryString from 'query-string';
import {
Box,
Button,
Expand All @@ -15,16 +17,22 @@ import { useRecoilState } from 'recoil';
import { useProviderFS } from '../provider/file';
import { IAccount, walrusDidState } from '../recoil';
import { getGoogleloginInfo } from '../utils/getGoogleloginInfo';
import { FILE_NAME_ACCOUNT, FILE_NAME_DID_DOCS } from '../utils/config';
import { FILE_NAME_ACCOUNT, FILE_NAME_DID_DOCS, SALT_TEMP } from '../utils/config';
import { Account } from '../component/Account';
import { DidDocs } from '../component/DidDocs';
import { jwtToAddress } from '@mysten/zklogin';
import { getZkProof } from '../utils/getZkProof';

export const Home = () => {
const initialized = useRef<boolean>(false);

const fs = useProviderFS();

const navigate = useNavigate();
const location = useLocation();

const [walrusState, setWalrusState] = useRecoilState(walrusDidState);
const [showLoginBtn, setShowLoginBtn] = useState<boolean>(true);

const handleGooglelogin = async () => {
if (fs) {
Expand Down Expand Up @@ -53,6 +61,7 @@ export const Home = () => {
// await fs.rmFile(FILE_NAME_DID_DOCS);
}
setWalrusState(undefined);
navigate('/');
}
};

Expand All @@ -66,15 +75,44 @@ export const Home = () => {
const account = JSON.parse(
Buffer.from(accountJson).toString('utf8'),
) as IAccount;
let didDocs: string[] = [];
const hasDocs = await fs.isExist(FILE_NAME_DID_DOCS);
if (hasDocs) {
const docs = await fs.readFile(FILE_NAME_DID_DOCS);
didDocs = JSON.parse(
Buffer.from(docs).toString('utf8'),
) as string[];
if (account.zkAddress) {
let didDocs: string[] = [];
const hasDocs = await fs.isExist(FILE_NAME_DID_DOCS);
if (hasDocs) {
const docs = await fs.readFile(FILE_NAME_DID_DOCS);
didDocs = JSON.parse(
Buffer.from(docs).toString('utf8'),
) as string[];
}
setWalrusState({ account, didDocs });
} else {
// temp
setShowLoginBtn(false)
const { id_token: jwt } = queryString.parse(location.hash) as {
id_token: string;
};
const address = jwtToAddress(jwt, BigInt(SALT_TEMP));
const proof = await getZkProof({
randomness: account.nonce.randomness,
maxEpoch: account.nonce.maxEpoch,
jwt,
ephemeralPublicKey: account.nonce.publicKey,
salt: SALT_TEMP,
});
account.zkAddress = {
address,
salt: SALT_TEMP,
proof,
jwt,
};
setWalrusState({ account, didDocs: [] });
await fs.writeFile(
FILE_NAME_ACCOUNT,
Buffer.from(JSON.stringify(account), 'utf8'),
);
navigate('/');
// temp
}
setWalrusState({ account, didDocs });
} else {
setWalrusState(undefined);
}
Expand Down Expand Up @@ -108,7 +146,7 @@ export const Home = () => {
</Box>
</>
)}
{initialized && !walrusState && (
{initialized && !walrusState && showLoginBtn && (
<Button
size="large"
variant="outlined"
Expand Down
2 changes: 1 addition & 1 deletion packages/holder/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const FILE_NAME_ACCOUNT = 'account.json';
export const FILE_NAME_DID_DOCS = 'did-docs.json';
export const REDIRECT_URL = 'https://4yju9x2wv34m3zpqg5dt1eaxvor0d0wpvett3rmebzy6xxrdvh.walrus.site/auth';
export const REDIRECT_URL = 'https://4yju9x2wv34m3zpqg5dt1eaxvor0d0wpvett3rmebzy6xxrdvh.walrus.site';
export const SALT_TEMP = '1553493906625353164421699608680';

0 comments on commit 8756f7d

Please sign in to comment.