Skip to content

Commit

Permalink
fix: sign message hook
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao committed May 11, 2023
1 parent e7674fe commit 25c918d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/utils/hooks/use-sign-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ const useSignMessage = (): UseSignMessageResult => {
);

const getSignature = useCallback(
async (account: KeyringPair) => {
async (account: KeyringPair): Promise<boolean> => {
const storedSignature = signatures?.[account.address];

if (storedSignature !== undefined) {
return { account, hasSignature: storedSignature };
return storedSignature;
}

const res = await fetch(`${SIGNER_API_URL}/${account.address}`, {
Expand All @@ -78,9 +78,11 @@ const useSignMessage = (): UseSignMessageResult => {

const response: GetSignatureData = await res.json();

return { account, hasSignature: response.exists };
setSignature(account.address, response.exists);

return response.exists;
},
[signatures]
[setSignature, signatures]
);

const queryKey = ['hasSignature', selectedAccount?.address];
Expand All @@ -91,12 +93,7 @@ const useSignMessage = (): UseSignMessageResult => {
refetchOnMount: false,
refetchOnReconnect: false,
enabled: !!selectedAccount,
queryFn: () => selectedAccount && getSignature(selectedAccount),
onSuccess: (data) => {
if (!data) return;
console.log(data);
setSignature(data.account.address, data.hasSignature);
}
queryFn: () => selectedAccount && getSignature(selectedAccount)
});

const signMessageMutation = useMutation((account: KeyringPair) => postSignature(account), {
Expand Down Expand Up @@ -138,7 +135,7 @@ const useSignMessage = (): UseSignMessageResult => {
const result = await refetchSignatureData({ queryKey: ['hasSignature', account.address] });

// Exit if there is a signature
if (result.data?.hasSignature) return;
if (result.data) return;

// Open signing modal if there is not a signature
dispatch(showSignTermsModalAction(true));
Expand Down

0 comments on commit 25c918d

Please sign in to comment.