Skip to content

feat: initial implementation of solana auth #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .react-router/types/+register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Params = {
"/onboarding/done": {};
"/dashboard": {};
"/profile": {};
"/solana": {};
"/login": {};
"/logout": {};
"/about": {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// React Router generated types for route:
// ./features/solana/user-solana-layout.tsx

import type * as T from "react-router/route-module"

import type { Info as Parent0 } from "../../../+types/root.js"
import type { Info as Parent1 } from "../../app/+types/layout-app.js"

type Module = typeof import("../user-solana-layout.js")

export type Info = {
parents: [Parent0, Parent1],
id: "features/solana/user-solana-layout"
file: "./features/solana/user-solana-layout.tsx"
path: "undefined"
params: {} & { [key: string]: string | undefined }
module: Module
loaderData: T.CreateLoaderData<Module>
actionData: T.CreateActionData<Module>
}

export namespace Route {
export type LinkDescriptors = T.LinkDescriptors
export type LinksFunction = () => LinkDescriptors

export type MetaArgs = T.CreateMetaArgs<Info>
export type MetaDescriptors = T.MetaDescriptors
export type MetaFunction = (args: MetaArgs) => MetaDescriptors

export type HeadersArgs = T.HeadersArgs
export type HeadersFunction = (args: HeadersArgs) => Headers | HeadersInit

export type unstable_MiddlewareFunction = T.CreateServerMiddlewareFunction<Info>
export type unstable_ClientMiddlewareFunction = T.CreateClientMiddlewareFunction<Info>
export type LoaderArgs = T.CreateServerLoaderArgs<Info>
export type ClientLoaderArgs = T.CreateClientLoaderArgs<Info>
export type ActionArgs = T.CreateServerActionArgs<Info>
export type ClientActionArgs = T.CreateClientActionArgs<Info>

export type HydrateFallbackProps = T.CreateHydrateFallbackProps<Info>
export type ComponentProps = T.CreateComponentProps<Info>
export type ErrorBoundaryProps = T.CreateErrorBoundaryProps<Info>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// React Router generated types for route:
// ./features/solana/user-solana-wallet.tsx

import type * as T from "react-router/route-module"

import type { Info as Parent0 } from "../../../+types/root.js"
import type { Info as Parent1 } from "../../app/+types/layout-app.js"
import type { Info as Parent2 } from "./user-solana-layout.js"

type Module = typeof import("../user-solana-wallet.js")

export type Info = {
parents: [Parent0, Parent1, Parent2],
id: "features/solana/user-solana-wallet"
file: "./features/solana/user-solana-wallet.tsx"
path: "solana"
params: {} & { [key: string]: string | undefined }
module: Module
loaderData: T.CreateLoaderData<Module>
actionData: T.CreateActionData<Module>
}

export namespace Route {
export type LinkDescriptors = T.LinkDescriptors
export type LinksFunction = () => LinkDescriptors

export type MetaArgs = T.CreateMetaArgs<Info>
export type MetaDescriptors = T.MetaDescriptors
export type MetaFunction = (args: MetaArgs) => MetaDescriptors

export type HeadersArgs = T.HeadersArgs
export type HeadersFunction = (args: HeadersArgs) => Headers | HeadersInit

export type unstable_MiddlewareFunction = T.CreateServerMiddlewareFunction<Info>
export type unstable_ClientMiddlewareFunction = T.CreateClientMiddlewareFunction<Info>
export type LoaderArgs = T.CreateServerLoaderArgs<Info>
export type ClientLoaderArgs = T.CreateClientLoaderArgs<Info>
export type ActionArgs = T.CreateServerActionArgs<Info>
export type ClientActionArgs = T.CreateClientActionArgs<Info>

export type HydrateFallbackProps = T.CreateHydrateFallbackProps<Info>
export type ComponentProps = T.CreateComponentProps<Info>
export type ErrorBoundaryProps = T.CreateErrorBoundaryProps<Info>
}
1 change: 1 addition & 0 deletions app/features/app/layout-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function LayoutApp({ loaderData: { user } }: Route.ComponentProps
const links: UiHeaderLink[] = [
{ label: 'Dashboard', to: '/dashboard' },
{ label: 'Profile', to: '/profile' },
{ label: 'Solana', to: '/solana' },
]
if (user.admin) {
links.push({ label: 'Admin', to: '/admin' })
Expand Down
9 changes: 9 additions & 0 deletions app/features/auth/data-access/get-user-by-identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { prisma } from '~/lib/db.server'
import { IdentityProvider } from '@prisma/client'

export async function getUserByIdentity({ provider, providerId }: { provider: IdentityProvider; providerId: string }) {
return await prisma.user.findFirst({
include: { identities: true },
where: { identities: { some: { provider, providerId } } },
})
}
10 changes: 10 additions & 0 deletions app/features/auth/data-access/get-user-by-solana-identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isValidSolanaPubKey } from '~/lib/solana/is-valid-solana-pubkey'
import { IdentityProvider } from '@prisma/client'
import { getUserByIdentity } from '~/features/auth/data-access/get-user-by-identity'

export async function getUserBySolanaIdentity({ providerId }: { providerId: string }) {
if (!isValidSolanaPubKey(providerId)) {
throw new Error('Invalid Solana providerId')
}
return getUserByIdentity({ provider: IdentityProvider.Solana, providerId })
}
11 changes: 2 additions & 9 deletions app/features/pubkey/pubkey-feature-profile-create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { UiCard } from '~/ui/ui-card'
import { PubkeyUiProfileCreateForm } from './ui/pubkey-ui-profile-create-form'
import { getPubkeySdkCommunity } from '~/lib/pubkey/get-pubkey-sdk-community'
import { useConnection, useWallet } from '@solana/wallet-adapter-react'
import { PublicKey, VersionedTransaction } from '@solana/web3.js'
import { VersionedTransaction } from '@solana/web3.js'
import { base58 } from '@metaplex-foundation/umi'
import { isValidSolanaPubKey } from '~/lib/solana/is-valid-solana-pubkey'

export function meta() {
return appMeta('Profiles')
Expand Down Expand Up @@ -114,14 +115,6 @@ export default function PubkeyFeatureCommunityCreate({ loaderData: { config } }:
)
}

function isValidSolanaPubKey(address: string) {
try {
return !!new PublicKey(address)
} catch {
return false
}
}

function txToBase58(tx: VersionedTransaction): string {
const [res] = base58.deserialize(tx.serialize())
return res
Expand Down
54 changes: 54 additions & 0 deletions app/features/solana/get-solana-verification-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export enum SolanaVerificationType {
Error = 'Error',
Login = 'Login',
Register = 'Register',
Link = 'Link',
Verify = 'Verify',
}

export function getSolanaVerificationType({
actorId,
ownerId,
enabledTypes = [
SolanaVerificationType.Login,
SolanaVerificationType.Link,
SolanaVerificationType.Register,
SolanaVerificationType.Verify,
],
}: {
actorId?: string
ownerId?: string
enabledTypes?: SolanaVerificationType[]
}): {
message?: string
type: SolanaVerificationType
} {
function ensureEnabledType(type: SolanaVerificationType) {
if (!enabledTypes.includes(type)) {
throw new Error(`Solana verification type ${type} is not enabled`)
}
return type
}

// If the wallet isn't owned by any user
if (!ownerId) {
return actorId
? // If actor is set, we want to link the wallet
{ type: ensureEnabledType(SolanaVerificationType.Link) }
: // Otherwise, we want to register a new user
{ type: ensureEnabledType(SolanaVerificationType.Register) }
}

// We are not logged in, so this is a login
if (!actorId) {
return { type: ensureEnabledType(SolanaVerificationType.Login) }
}

// We are logged in, make sure that the user owns the wallet
if (ownerId !== actorId) {
return { type: SolanaVerificationType.Error, message: 'User does not own public key' }
}

// Actor owns the wallet, we can now verify it
return { type: ensureEnabledType(SolanaVerificationType.Verify) }
}
8 changes: 8 additions & 0 deletions app/features/solana/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { index, layout, prefix } from '@react-router/dev/routes'

export const userSolanaRoutes = layout('./features/solana/user-solana-layout.tsx',
prefix('solana', [
index('./features/solana/user-solana-wallet.tsx'),
])
)

11 changes: 11 additions & 0 deletions app/features/solana/user-solana-layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { Suspense } from 'react'
import { Loader } from '@mantine/core'
import { Outlet } from 'react-router'

export default function LayoutApp() {
return (
<Suspense fallback={<Loader />}>
<Outlet />
</Suspense>
)
}
Loading