Skip to content

Commit

Permalink
Update lib
Browse files Browse the repository at this point in the history
  • Loading branch information
luistorres committed Apr 4, 2024
1 parent aabcd2b commit 8b60492
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 180 deletions.
122 changes: 106 additions & 16 deletions packages/web-app/app/_lib/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { getPublicInfo } from '../_server/idos';
import { insertGrantBySignature } from '../_server/grants';
import { useFetchGrantMessage } from './contract-queries';
import { useSignMessage } from 'wagmi';
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import { useKyc } from '../_providers/kyc/context';
import { useFetchNewDataId } from './queries';

export const useAcquireAccessGrantMutation = () => {
const { sdk } = useIdOS();
Expand Down Expand Up @@ -35,21 +37,23 @@ export const useAcquireAccessGrantMutation = () => {
};

export const useInsertGrantBySignatureMutation = () => {
const { address: owner } = useIdOS();
const queryClient = useQueryClient();

return useMutation({
mutationFn: async ({
owner,
grantee,
dataId,
signature,
expiration,
}: {
owner: string;
grantee: string;
dataId: string;
signature: string;
expiration: number;
}) => {
if (!owner) return null;

const hash = await insertGrantBySignature({
owner,
grantee,
Expand All @@ -60,48 +64,134 @@ export const useInsertGrantBySignatureMutation = () => {

return hash;
},
onSuccess: () => {
onSuccess: (_data, { grantee }) => {
// queryClient.invalidateQueries({ queryKey: ['credentials'] });
// queryClient.invalidateQueries({ queryKey: ['credential-content'] });
// invalidate queries after 10 seconds
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ['grants'] });
// update once we know the id
queryClient.invalidateQueries({ queryKey: ['grants', owner, grantee] });
}, 10000);
},
});
};

export const useSignDelegatedAccessGrant = () => {
type TNewDataIdMutation = {
id: string;
encryptionPublicKey: string;
};

// export const useGenerateNewDataIdMutation = () => {
// const { sdk, hasSigner } = useIdOS();

// return useMutation({
// mutationFn: async ({ id, encryptionPublicKey }: TNewDataIdMutation) => {
// if (!sdk || !hasSigner) return null;

// console.log(
// '%c==>',
// 'color: green; background: yellow; font-size: 20px',
// id,
// encryptionPublicKey,
// );

// try {
// const { id: dataId } = await sdk.data.share(
// 'credentials',
// id,
// encryptionPublicKey,
// );

// return dataId;
// } catch (error) {
// console.log(
// '%c==>',
// 'color: green; background: purple; font-size: 20px',
// error,
// );

// return null;
// }
// },
// });
// };

export const useSignDelegatedAccessGrant = (
grantee: string,
encryptionPublicKey: string,
lockTimeSpanSeconds: number,
) => {
const expiration = useMemo(
() => Math.floor(Date.now() / 1000) + lockTimeSpanSeconds,
[lockTimeSpanSeconds],
);
const { id, status: kycStatus } = useKyc();

const {
mutate,
mutate: insertGrant,
isPending: isServerPending,
isSuccess,
isSuccess: isGrantInsertSuccess,
data: transactionHash,
} = useInsertGrantBySignatureMutation();
const { owner, grantee, dataId, expiration, message } =
useFetchGrantMessage();
const {
data: dataId,
// isPending: isDataIdPending,
// isSuccess: isDataIdSuccess,
} = useFetchNewDataId(grantee, encryptionPublicKey);
const { data: message, isSuccess: isMessageRequestSuccess } =
useFetchGrantMessage(grantee, expiration, dataId);
const {
data: signature,
signMessage,
isPending: isSignPending,
} = useSignMessage();

useEffect(() => {
if (owner && grantee && dataId && expiration && signature && !isSuccess) {
mutate({
owner,
if (
grantee &&
dataId &&
expiration &&
signature &&
!isGrantInsertSuccess &&
!isServerPending
) {
console.log(
'%c==>',
'color: green; background: yellow; font-size: 20px',
'INSERT GRANT',
);

insertGrant({
grantee,
dataId,
signature,
expiration,
});
}
}, [signature, mutate, isSuccess, owner, grantee, dataId, expiration]);
}, [
signature,
insertGrant,
isGrantInsertSuccess,
grantee,
dataId,
expiration,
isServerPending,
]);

useEffect(() => {
if (message && !signature && !isSignPending && isMessageRequestSuccess) {
try {
signMessage({ message: message as string });
} catch (error) {
console.log(error);
}
}
}, [message, signature, isSignPending, isMessageRequestSuccess, signMessage]);

const sign = useCallback(async () => {
try {
if (!message) return;
signMessage({ message });
signMessage({ message: message as string });
} catch (error) {
console.log(error);
}
Expand All @@ -112,7 +202,7 @@ export const useSignDelegatedAccessGrant = () => {
dataId,
isServerPending,
isSignPending,
isSuccess,
isGrantInsertSuccess,
transactionHash,
};
};
78 changes: 39 additions & 39 deletions packages/web-app/app/_lib/contract-queries.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useFetchNewDataId } from './queries';
import { useReadContract } from 'wagmi';
import { grantsAbi } from '../_server/grants/abi';
import { useMemo } from 'react';
import { useIdOS } from '../_providers/idos';

type TFetchGrantMessage = {
type TGrantMessage = {
owner: string | undefined;
grantee: string | undefined;
dataId: string | undefined;
Expand All @@ -16,48 +16,48 @@ type TFetchGrantMessage = {

/** generate a grant message to be signed by the user and later inserted by our server
*/
export const useFetchGrantMessage = (): TFetchGrantMessage => {
const { data, isSuccess: isSuccessDataId } = useFetchNewDataId();
const {
data: message,
isSuccess: isSuccessMessage,
isError,
isLoading,
} = useReadContract({
export const useFetchGrantMessage = (
grantee: string,
expiration: number,
dataId?: string | null, // doesn't run while we don't have a dataId
) => {
const { address: owner } = useIdOS();

// const {
// data: message,
// isSuccess,
// isError,
// isLoading,
// } = useReadContract({
return useReadContract({
abi: grantsAbi,
address: process.env.NEXT_PUBLIC_IDOS_CONTRACT_ADDRESS,
functionName: 'insertGrantBySignatureMessage',
args: [data?.owner, data?.grantee, data?.dataId, data?.expiration],
args: [owner, grantee, dataId, expiration],
query: {
enabled: !!(
isSuccessDataId &&
data?.owner &&
data?.grantee &&
data?.dataId &&
data?.expiration
),
enabled: !!(owner && grantee && dataId && expiration),
},
});

return useMemo(() => {
return {
owner: data?.owner,
grantee: data?.grantee,
dataId: data?.dataId,
expiration: data?.expiration,
message: message as string | undefined, //review later
isSuccess: isSuccessMessage,
isError,
isLoading,
};
}, [
message,
isSuccessMessage,
isError,
isLoading,
data?.owner,
data?.grantee,
data?.dataId,
data?.expiration,
]);
// return useMemo(() => {
// return {
// owner: owner,
// grantee: grantee,
// dataId: dataId,
// expiration: expiration,
// message: message as string | undefined, //review later
// isSuccess,
// isError,
// isLoading,
// };
// }, [
// message,
// isSuccess,
// isError,
// isLoading,
// owner,
// grantee,
// dataId,
// expiration,
// ]);
};
Loading

0 comments on commit 8b60492

Please sign in to comment.