-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathuseVerifyAccount.ts
67 lines (55 loc) · 1.91 KB
/
useVerifyAccount.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { isEthereumAddress } from '@polkadot/util-crypto'
import { useSignMessage } from '@wagmi/vue'
export type SignaturePair = { signature: string, message: string }
const signMessagePolkadot = async (address: string, message: string) => {
const injector = await getAddress(toDefaultAddress(address))
const signedMessage = await injector.signer.signRaw({
address: address,
data: message,
type: 'bytes',
})
console.log('Signed polkadot message:', { message, address, signedMessage })
return signedMessage.signature
}
export const SIGNATURE_MESSAGE = 'Verify ownership of this account on Koda'
export default function useVerifyAccount() {
const walletStore = useWalletStore()
const { accountId } = useAuth()
const signedMessage = computed(() => walletStore.getSignedMessage)
const { signMessageAsync } = useSignMessage()
const signMessageEthereum = async (address: string, message: string) => {
const signedMessage = await signMessageAsync({
account: address as `0x${string}`,
message: message,
})
console.log('Signed evm message:', { message, address, signedMessage })
return signedMessage
}
const getSignedMessage = async () => {
if (!accountId.value) {
throw new Error('Please connect your wallet first')
}
if (signedMessage.value) {
return signedMessage.value
}
const signMessageFn = isEthereumAddress(accountId.value)
? signMessageEthereum
: signMessagePolkadot
const signature = await signMessageFn(accountId.value, SIGNATURE_MESSAGE)
if (signature) {
walletStore.setSignedMessage(signature)
return signature
}
throw new Error('You have not completed address verification')
}
const getSignaturePair = async (): Promise<SignaturePair> => {
const signature = await getSignedMessage()
return {
signature,
message: SIGNATURE_MESSAGE,
}
}
return {
getSignaturePair,
}
}