Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ithacaxyz/oddworld
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Nov 13, 2024
2 parents b70126b + e054127 commit 3944610
Show file tree
Hide file tree
Showing 4 changed files with 655 additions and 145 deletions.
27 changes: 21 additions & 6 deletions examples/blog/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ import * as React from 'react'
export default function Page() {
const { isLoading, user, error } = db.useAuth()

const { data: postsData } = db.useQuery({ posts: { author: {} } })
const { data: postsData } = db.useQuery({
posts: {
author: {},
$: {
order: {
serverCreatedAt: 'desc',
},
},
},
})

if (error) return <div>Uh oh! {error.message}</div>
if (isLoading) return <div />
Expand All @@ -30,12 +39,13 @@ export default function Page() {
{user ? (
<form
className="flex flex-col max-w-[200px]"
onSubmit={(event) => {
onSubmit={async (event) => {
event.preventDefault()
const form = event.currentTarget
const formData = new FormData(event.currentTarget)
const title = formData.get('title') as string
const text = formData.get('text')
db.transact(
await db.transact(
tx.posts[id()]
.update({
createdAt: Date.now(),
Expand All @@ -48,6 +58,7 @@ export default function Page() {
})
.link({ author: user.id }),
)
form.reset()
}}
>
<input type="text" name="title" placeholder="Title" required />
Expand All @@ -74,9 +85,12 @@ export default function Page() {
className="flex flex-col gap-1 text-sm"
>
<h2 className="font-bold m-0 text-base">{post.title}</h2>
<time>{new Date(post.createdAt).toLocaleString()}</time>
<p className="m-0">{post.text}</p>
<button type="button" className="text-xs leading-none w-fit">
<div className="text-xs">
<time>{new Date(post.createdAt).toLocaleString()}</time>
</div>

<p className="m-0 whitespace-pre-line">{post.text}</p>
<button type="button" className="text-xs leading-none mt-1 w-fit">
Tip 1 $EXP
</button>
</div>
Expand Down Expand Up @@ -144,6 +158,7 @@ function Auth() {
>
<input
data-1p-ignore
autoComplete="off"
type="email"
placeholder="name@example.com"
name="email"
Expand Down
44 changes: 16 additions & 28 deletions src/Oddworld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as Provider from 'ox/Provider'
import * as PublicKey from 'ox/PublicKey'
import * as RpcResponse from 'ox/RpcResponse'
import type * as RpcSchema from 'ox/RpcSchema'
import * as WebCryptoP256 from 'ox/WebCryptoP256'
import { http, type Chain, type Transport, createClient } from 'viem'
import { odysseyTestnet } from 'viem/chains'

Expand Down Expand Up @@ -84,15 +83,12 @@ export function create(
'eth_sendTransaction'
>

require(to, 'Missing required parameter: to')
require(from, 'Missing required parameter: from')
requireParameter(to, 'to')
requireParameter(from, 'from')

const account = accounts.find((account) => account.address === from)
if (!account) throw new Provider.UnauthorizedError()

const key = account.keys[0]
if (!key) throw new Provider.UnauthorizedError()

return await AccountDelegation.execute(client, {
account,
calls: [
Expand All @@ -102,7 +98,6 @@ export function create(
value: Hex.toBigInt(value),
},
],
key,
})
}

Expand Down Expand Up @@ -146,38 +141,31 @@ export function create(
)
if (!account) throw new Provider.UnauthorizedError()

const key = account.keys[0]
if (!key) throw new Provider.UnauthorizedError()

const keyPair = await WebCryptoP256.createKeyPair()
const authorizeKey = {
...keyPair,
const key = await AccountDelegation.createWebCryptoKey({
expiry: BigInt(expiry),
type: 'webcrypto',
} satisfies AccountDelegation.WebCryptoKey
})

// TODO: wait for tx to be included?
await AccountDelegation.authorize(client, {
account,
authorizeKey,
key,
})

accounts
.find((account) => account.address === address)!
.keys.push(authorizeKey)
.keys.push(key)

return {
address,
chainId: Hex.fromNumber(0),
context: PublicKey.toHex(authorizeKey.publicKey),
context: PublicKey.toHex(key.publicKey),
expiry,
permissions: [],
signer: {
type: 'key',
data: {
type: 'secp256r1',
publicKey: PublicKey.toHex(authorizeKey.publicKey),
publicKey: PublicKey.toHex(key.publicKey),
},
},
} satisfies RpcSchema.ExtractReturnType<
Expand All @@ -196,24 +184,24 @@ export function create(
'wallet_sendCalls'
>

require(from, 'Missing required parameter: from')
requireParameter(from, 'from')

const account = accounts.find((account) => account.address === from)
if (!account) throw new Provider.UnauthorizedError()

const { context: publicKey } = capabilities?.permissions ?? {}

const key = publicKey
? account.keys.find(
const keyIndex = publicKey
? account.keys.findIndex(
(key) => PublicKey.toHex(key.publicKey) === publicKey,
)
: account.keys[0]
if (!key) throw new Provider.UnauthorizedError()
: 0
if (keyIndex === -1) throw new Provider.UnauthorizedError()

return await AccountDelegation.execute(client, {
account,
calls: calls as AccountDelegation.Calls,
key,
keyIndex,
})
}

Expand Down Expand Up @@ -310,13 +298,13 @@ export type Client = {
}
}

function require(
function requireParameter(
param: unknown,
message: string,
details: string,
): asserts param is NonNullable<typeof param> {
if (typeof param === 'undefined')
throw new RpcResponse.InvalidParamsError({
...RpcResponse.InvalidParamsError,
message,
message: `Missing required parameter: ${details}`,
})
}
Loading

0 comments on commit 3944610

Please sign in to comment.