Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ VITE_GA_MEASUREMENT_ID=
VITE_POSTHOG_HOST=
VITE_POSTHOG_KEY=
VITE_TEMPO_ENV= # testnet|devnet|localnet
VITE_WEBAUTHN_AUTH_URL= # e.g. https://wallet.tempo.xyz/api/webauthn
1 change: 0 additions & 1 deletion env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ interface EnvironmentVariables {
readonly VITE_POSTHOG_KEY: string
readonly VITE_POSTHOG_HOST: string
readonly VITE_TEMPO_ENV: 'localnet' | 'devnet' | 'moderato'
readonly VITE_WEBAUTHN_AUTH_URL?: string

readonly INDEXSUPPLY_API_KEY: string
readonly SLACK_FEEDBACK_WEBHOOK: string
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"vocs": "https://pkg.pr.new/wevm/vocs@2fb25c2",
"wagmi": "0.0.0-canary-20260421205751",
"waku": "1.0.0-alpha.4",
"webauthx": "~0.1.1",
"zod": "^4.3.6"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions src/lib/webAuthnCeremony.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { WebAuthnCeremony } from 'accounts'
import { Bytes } from 'ox'
import { Authentication, Registration } from 'webauthx/server'

/**
* Custom {@link WebAuthnCeremony.WebAuthnCeremony} that wraps the
* [`keys.tempo.xyz`](https://github.com/tempoxyz/tempo-apps/tree/main/apps/key-manager)
* key-manager service.
*
* The service is a thin challenge issuer + credential public-key store
* (3 endpoints). Unlike `WebAuthnCeremony.server({ url })`, this ceremony
* builds the `PublicKeyCredentialCreationOptions` /
* `PublicKeyCredentialRequestOptions` client-side using `webauthx/server`,
* sources challenges from `GET /challenge`, persists the registered
* credential's public key via `POST /:id`, and retrieves it later via
* `GET /:id`.
*/
export function keys(options: keys.Options = {}): WebAuthnCeremony.WebAuthnCeremony {
const { url = 'https://keys.tempo.xyz', rpId: rpIdOption } = options

async function getChallenge() {
const response = await fetch(`${url}/challenge`)
if (!response.ok) throw new Error('Failed to get challenge')
return (await response.json()) as {
challenge: `0x${string}`
rp?: { id: string; name: string }
}
}

function resolveRpId(rp: { id: string; name: string } | undefined) {
return (
rpIdOption ?? rp?.id ?? (typeof location !== 'undefined' ? location.hostname : 'localhost')
)
}

return WebAuthnCeremony.from({
async getRegistrationOptions(parameters) {
const { excludeCredentialIds, name, userId } = parameters
const { challenge, rp: rpFromServer } = await getChallenge()
const rpId = resolveRpId(rpFromServer)
const { options: opts } = Registration.getOptions({
challenge,
excludeCredentialIds,
name,
rp: { id: rpId, name: rpFromServer?.name ?? rpId },
user: userId ? { id: Bytes.fromString(userId), name } : undefined,
})
return { options: opts }
},
async verifyRegistration(credential) {
const publicKey = credential.publicKey
const response = await fetch(`${url}/${credential.id}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ credential, publicKey }),
})
if (!response.ok) {
const { error } = (await response.json().catch(() => ({}))) as { error?: string }
throw new Error(error ?? 'Failed to register credential')
}
return { credentialId: credential.id, publicKey }
},
async getAuthenticationOptions(parameters = {}) {
const { allowCredentialIds, challenge, credentialId } = parameters
const resolvedChallenge = challenge ?? (await getChallenge()).challenge
const { options: opts } = Authentication.getOptions({
challenge: resolvedChallenge,
credentialId: allowCredentialIds ? [...allowCredentialIds] : credentialId,
rpId: resolveRpId(undefined),
})
return { options: opts }
},
async verifyAuthentication(response) {
const res = await fetch(`${url}/${response.id}`)
if (!res.ok) throw new Error(`Unknown credential: ${response.id}`)
const { publicKey } = (await res.json()) as { publicKey: `0x${string}` }
return { credentialId: response.id, publicKey }
},
})
}

export namespace keys {
export type Options = {
/** Base URL of the key-manager service. @default `"https://keys.tempo.xyz"` */
url?: string | undefined
/** Override Relying Party ID. @default rp returned by `GET /challenge`, falling back to `location.hostname`. */
rpId?: string | undefined
}
}
7 changes: 2 additions & 5 deletions src/wagmi.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'wagmi'
import { tempoWallet, webAuthn } from 'wagmi/tempo'
import { alphaUsd, betaUsd, pathUsd, thetaUsd } from './components/guides/tokens'
import * as WebAuthnCeremony from './lib/webAuthnCeremony.ts'
import { feeToken, moderatoZones } from './lib/private-zones.ts'

const chain =
Expand Down Expand Up @@ -67,11 +68,7 @@ export function getConfig(options: getConfig.Options = {}) {
url: 'https://sponsor.moderato.tempo.xyz',
},
}),
webAuthn(
import.meta.env.VITE_WEBAUTHN_AUTH_URL
? { authUrl: import.meta.env.VITE_WEBAUTHN_AUTH_URL }
: undefined,
),
webAuthn({ ceremony: WebAuthnCeremony.keys() }),
]),
],
multiInjectedProviderDiscovery,
Expand Down
Loading