Skip to content

Commit

Permalink
Merge pull request #1187 from interlay/rui/fix-use-sign-message
Browse files Browse the repository at this point in the history
fix: sign message hook
  • Loading branch information
tomjeatt authored May 12, 2023
2 parents e7674fe + 25c918d commit 4ebc2b7
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

2 comments on commit 4ebc2b7

@vercel
Copy link

@vercel vercel bot commented on 4ebc2b7 May 12, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 4ebc2b7 May 12, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.