@@ -5,6 +5,9 @@ import type { Route } from './+types/user-solana-wallet'
5
5
import { useWallet } from '@solana/wallet-adapter-react'
6
6
import { solanaAuth } from '~/lib/solana-auth/solana-auth'
7
7
import type { SolanaAuthMessage , SolanaAuthMessageSigned } from '~/lib/solana-auth/solana-auth-message'
8
+ import { getUserBySolanaIdentity } from '~/features/auth/data-access/get-user-by-solana-identity'
9
+ import { getUser } from '~/features/auth/data-access/get-user'
10
+ import { getSolanaVerificationType , SolanaVerificationType } from './get-solana-verification-type'
8
11
9
12
function parsePayload ( payload : string = '' ) : SolanaAuthMessageSigned {
10
13
try {
@@ -23,8 +26,27 @@ export async function action({ request }: Route.LoaderArgs) {
23
26
if ( ! publicKey ) {
24
27
return { success : false , message : `No public key` }
25
28
}
29
+ const actor = await getUser ( request )
30
+ const owner = await getUserBySolanaIdentity ( { providerId : publicKey } )
31
+
32
+ // This determines the type of verification we are performing
33
+ const verification = getSolanaVerificationType ( {
34
+ actorId : actor ?. id ?? undefined ,
35
+ ownerId : owner ?. id ?? undefined ,
36
+ enabledTypes : [
37
+ SolanaVerificationType . Login ,
38
+ SolanaVerificationType . Link ,
39
+ SolanaVerificationType . Register ,
40
+ SolanaVerificationType . Verify ,
41
+ ] ,
42
+ } )
43
+ if ( verification . type === SolanaVerificationType . Error ) {
44
+ return { success : false , message : verification . message }
45
+ }
26
46
27
- console . log ( `user-solana-wallet -> action` , action , 'publicKey' , publicKey )
47
+ console . log (
48
+ `user-solana-wallet [${ action } ] -> publicKey: ${ publicKey } -> owner: ${ owner ? owner . username : 'NONE' } -> type: ${ verification . type } ` ,
49
+ )
28
50
29
51
switch ( formData . get ( 'action' ) ) {
30
52
case 'sign-message-create' :
@@ -34,16 +56,14 @@ export async function action({ request }: Route.LoaderArgs) {
34
56
type : 'solana-auth-message' ,
35
57
}
36
58
case 'sign-message-verify' :
37
- const { message, signature, blockhash, nonce } = parsePayload ( payload )
38
- console . log ( `sign message -> verify` , 'message' , message , 'signature' , signature , 'blockhash' , blockhash )
39
- const result = await solanaAuth . verifyMessage ( {
40
- method : 'solana:signMessage' ,
41
- publicKey,
42
- message,
43
- signature,
44
- blockhash,
45
- nonce,
46
- } )
59
+ const parsed = parsePayload ( payload )
60
+ const result = await solanaAuth . verifyMessage ( parsed )
61
+ if ( ! result ) {
62
+ throw new Error ( 'Invalid signature' )
63
+ }
64
+ console . log ( `sign message -> verify` , 'message' , parsed , 'signature' , parsed . signature , 'result' , result )
65
+ // TODO: Wallet is verified. We can now:
66
+ // - Check if the wallet exists
47
67
return {
48
68
success : true ,
49
69
message : result ,
0 commit comments