From 5faa5236900808018e2a107a315df68abe38bbf4 Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Fri, 14 Apr 2023 20:06:50 +0200 Subject: [PATCH] feat: add did resolver Signed-off-by: Timo Glastra --- packages/siera-ui/src/layout/Layout.tsx | 1 + .../src/pages/agent/dids/DidsScreen.tsx | 86 +++++++++++++++++++ packages/siera-ui/src/routes.tsx | 10 +++ 3 files changed, 97 insertions(+) create mode 100644 packages/siera-ui/src/pages/agent/dids/DidsScreen.tsx diff --git a/packages/siera-ui/src/layout/Layout.tsx b/packages/siera-ui/src/layout/Layout.tsx index 4c8545a..f2aa0bb 100644 --- a/packages/siera-ui/src/layout/Layout.tsx +++ b/packages/siera-ui/src/layout/Layout.tsx @@ -17,6 +17,7 @@ const navigationItems: NavigationItem[] = [ { name: 'Connections', href: '/agent/connections' }, { name: 'Credentials', href: '/agent/credentials' }, { name: 'Proofs', href: '/agent/proofs' }, + { name: 'Dids', href: '/agent/dids' }, ] export const Layout = ({ children }: LayoutProps) => { diff --git a/packages/siera-ui/src/pages/agent/dids/DidsScreen.tsx b/packages/siera-ui/src/pages/agent/dids/DidsScreen.tsx new file mode 100644 index 0000000..b435edc --- /dev/null +++ b/packages/siera-ui/src/pages/agent/dids/DidsScreen.tsx @@ -0,0 +1,86 @@ +import type { DidDocument } from '@aries-framework/core' + +import { useAgent } from '@aries-framework/react-hooks' +import { Box, Group, Space, TextInput } from '@mantine/core' +import { useForm } from '@mantine/form' +import { showNotification } from '@mantine/notifications' +import React, { useState } from 'react' + +import { Card } from '../../../components/Card' +import { Header } from '../../../components/Header' +import { Loading } from '../../../components/Loading' +import { SecondaryButton } from '../../../components/generic' +import { RecordCodeBlock } from '../../../components/generic/information/RecordCodeBlock' + +interface DidResolveValues { + did: string +} + +export const DidsScreen = () => { + const form = useForm({ initialValues: { did: '' } }) + const { agent } = useAgent() + const [isLoading, setIsLoading] = useState(false) + const [didResult, setDidResult] = useState() + + const getErrorMessage = (did: string, error?: string) => { + const errorMessage: Record string> = { + invalidDid: (did: string) => `did '${did}' is not a valid did.`, + notFound: (did: string) => `did '${did}' could not be found. Make sure the did exists.`, + unsupportedDidMethod: (did: string) => { + const [, method] = did.split(':') + + // TODO: extract supportedMethods from agent.dids.config.resolvers when updated to 0.4.0 + return `did method '${method}' is not supported at the moment.` + }, + } + + if (!error || !errorMessage[error]) return '' + return errorMessage[error](did) + } + + const resolveDid = async ({ did }: DidResolveValues) => { + if (isLoading) return + + setIsLoading(true) + const result = await agent?.dids.resolve(did) + setIsLoading(false) + + if (!result?.didDocument) { + showNotification({ + title: 'Error', + message: `Could not resolve did document. ${getErrorMessage(did, result?.didResolutionMetadata.error)}`, + color: 'error', + }) + setDidResult(undefined) + return + } + + setDidResult(result.didDocument) + } + + return ( + <> +
+ +
+ + + + + + Resolve + + +
+ + {isLoading ? ( + + ) : didResult ? ( + + + + ) : null} +
+ + ) +} diff --git a/packages/siera-ui/src/routes.tsx b/packages/siera-ui/src/routes.tsx index 3f913aa..9c37c04 100644 --- a/packages/siera-ui/src/routes.tsx +++ b/packages/siera-ui/src/routes.tsx @@ -9,6 +9,7 @@ import { ConnectionDetailsScreen } from './pages/agent/connections/ConnectionDet import { ConnectionsScreen } from './pages/agent/connections/ConnectionsScreen' import { CredentialsDetailsScreen } from './pages/agent/credentials/CredentialsDetailsScreen' import { CredentialsScreen } from './pages/agent/credentials/CredentialsScreen' +import { DidsScreen } from './pages/agent/dids/DidsScreen' import { ProofDetailsScreen } from './pages/agent/proofs/ProofDetailsScreen' import { ProofsScreen } from './pages/agent/proofs/ProofsScreen' @@ -21,6 +22,7 @@ export const routeUrls = [ '/agent/credentials/{credentialId}', '/agent/proofs', '/agent/proofs/{proofId}', + '/agent/dids', ] as const export const routes: RouteObject[] = [ @@ -87,6 +89,14 @@ export const routes: RouteObject[] = [ ), }, + { + path: 'dids', + element: ( + + + + ), + }, ], }, ]