Skip to content
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

feat: get namespace from /world instead of env variable #18

Merged
merged 1 commit into from
Mar 22, 2024
Merged
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: 0 additions & 1 deletion .env.example

This file was deleted.

18 changes: 10 additions & 8 deletions src/components/sidebar/messages.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQueryClient } from '@tanstack/react-query'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { BookDashed, MessageSquareCode } from 'lucide-react'
import { useForm } from 'react-hook-form'

Expand Down Expand Up @@ -28,7 +28,7 @@ import {
import { accountFromPersona } from '@/lib/account'
import { useCardinal } from '@/lib/cardinal-provider'
import { useConfig } from '@/lib/config-provider'
import { lastMessageQueryOptions } from '@/lib/query-options'
import { lastMessageQueryOptions, worldQueryOptions } from '@/lib/query-options'
import { WorldField } from '@/lib/types'

import { formatName } from './utils'
Expand Down Expand Up @@ -74,21 +74,23 @@ interface MessageProp {

function Message({ message }: MessageProp) {
const { config, setConfig } = useConfig()
const { cardinalUrl, isCardinalConnected, cardinalNamespace } = useCardinal()
const { cardinalUrl, isCardinalConnected } = useCardinal()
const { data } = useQuery(worldQueryOptions({ cardinalUrl, isCardinalConnected }))
const queryClient = useQueryClient()
const form = useForm()

// @ts-ignore
const handleSubmit = async (values) => {
const { persona: personaTag, ...fields } = values as { [k: string]: string }
const persona = config.personas.filter((p) => p.personaTag === personaTag)[0]
const account = accountFromPersona(persona)
const msg = `${personaTag}${cardinalNamespace}${persona.nonce}${JSON.stringify(fields)}`
const signature = account.sign(msg)
const { sign } = accountFromPersona(persona)
const { namespace } = data!
const msg = `${personaTag}${namespace}${persona.nonce}${JSON.stringify(fields)}`
const signature = sign(msg)
const body = {
personaTag: personaTag,
personaTag,
signature,
namespace: cardinalNamespace,
namespace,
nonce: persona.nonce,
body: fields,
}
Expand Down
17 changes: 9 additions & 8 deletions src/components/sidebar/persona.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { useQueryClient } from '@tanstack/react-query'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useForm } from 'react-hook-form'
import { z } from 'zod'

Expand All @@ -16,7 +16,7 @@ import { Input } from '@/components/ui/input'
import { createPersonaAccount } from '@/lib/account'
import { useCardinal } from '@/lib/cardinal-provider'
import { useConfig } from '@/lib/config-provider'
import { personaQueryOptions } from '@/lib/query-options'
import { personaQueryOptions, worldQueryOptions } from '@/lib/query-options'

const formSchema = z.object({
personaTag: z
Expand All @@ -35,7 +35,8 @@ const formSchema = z.object({

export function CreatePersona() {
const { config, setConfig } = useConfig()
const { cardinalUrl, isCardinalConnected, cardinalNamespace } = useCardinal()
const { cardinalUrl, isCardinalConnected } = useCardinal()
const { data } = useQuery(worldQueryOptions({ cardinalUrl, isCardinalConnected }))
const queryClient = useQueryClient()
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
Expand All @@ -45,16 +46,16 @@ export function CreatePersona() {
})

const handleSubmit = async ({ personaTag }: z.infer<typeof formSchema>) => {
const account = createPersonaAccount(personaTag)
const { privateKey, address } = account
const { privateKey, address, sign } = createPersonaAccount(personaTag)
const { namespace } = data!
const nonce = 0 // new accounts will always start with 0 as the nonce
const message = `${personaTag}${cardinalNamespace}${nonce}{"personaTag":"${personaTag}","signerAddress":"${address}"}`
const signature = account.sign(message)
const message = `${personaTag}${namespace}${nonce}{"personaTag":"${personaTag}","signerAddress":"${address}"}`
const signature = sign(message)
const body = {
personaTag,
nonce,
signature,
namespace: cardinalNamespace,
namespace,
body: { personaTag, signerAddress: address },
}
// TODO: query error handling
Expand Down
4 changes: 0 additions & 4 deletions src/lib/cardinal-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ interface CardinalProviderState {
cardinalUrl: string
setCardinalUrl: (url: string) => void
isCardinalConnected: boolean
cardinalNamespace: string
}

interface CardinalProviderProps {
Expand All @@ -14,12 +13,10 @@ interface CardinalProviderProps {
// TODO: consider whether to put cardinal url/port in .env too
const storageKey = 'cardinal-url'
const defaultCardinalUrl = 'http://localhost:4040'
const defaultCardinalNamespace = (import.meta.env.CARDINAL_NAMESPACE as string) || 'world-1'
const initialState: CardinalProviderState = {
cardinalUrl: defaultCardinalUrl,
setCardinalUrl: () => null,
isCardinalConnected: false,
cardinalNamespace: defaultCardinalNamespace,
}

const CardinalProviderContext = createContext(initialState)
Expand Down Expand Up @@ -55,7 +52,6 @@ export function CardinalProvider({ children, ...props }: CardinalProviderProps)
setCardinalUrl(url)
},
isCardinalConnected,
cardinalNamespace: defaultCardinalNamespace,
}

return (
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface WorldResponse {
components: WorldField[]
messages: WorldField[]
queries: WorldField[]
namespace: string
}

export interface Persona {
Expand Down
Loading