|
| 1 | +import { GraphQLClient, gql } from 'graphql-request'; |
| 2 | +import { PUBLIC_CONTROL_PANEL_URL, PUBLIC_REGISTRY_URL } from '$env/static/public'; |
| 3 | +import type { BindingDocument, SocialConnection } from '@metastate-foundation/types'; |
| 4 | + |
| 5 | +const BINDING_DOCUMENTS_QUERY = gql` |
| 6 | + query GetBindingDocuments($first: Int!) { |
| 7 | + bindingDocuments(first: $first) { |
| 8 | + edges { |
| 9 | + node { |
| 10 | + id |
| 11 | + parsed |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | + } |
| 16 | +`; |
| 17 | + |
| 18 | +const USER_PROFILE_QUERY = gql` |
| 19 | + query GetUserProfile($ontologyId: ID!, $first: Int!) { |
| 20 | + metaEnvelopes(filter: { ontologyId: $ontologyId }, first: $first) { |
| 21 | + edges { |
| 22 | + node { |
| 23 | + parsed |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | +`; |
| 29 | + |
| 30 | +const USER_PROFILE_ONTOLOGY = '550e8400-e29b-41d4-a716-446655440000'; |
| 31 | + |
| 32 | +interface RegistryResolveResponse { |
| 33 | + evaultUrl?: string; |
| 34 | + uri?: string; |
| 35 | +} |
| 36 | + |
| 37 | +interface PlatformCertificationResponse { |
| 38 | + token: string; |
| 39 | +} |
| 40 | + |
| 41 | +interface BindingDocumentsResponse { |
| 42 | + bindingDocuments: { |
| 43 | + edges: Array<{ |
| 44 | + node: { |
| 45 | + id: string; |
| 46 | + parsed: Record<string, unknown> | null; |
| 47 | + }; |
| 48 | + }>; |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +class EvaultService { |
| 53 | + private platformToken: string | null = null; |
| 54 | + private profileNameCache = new Map<string, string>(); |
| 55 | + |
| 56 | + private getRegistryUrl(): string { |
| 57 | + const registryUrl = PUBLIC_REGISTRY_URL || 'https://registry.w3ds.metastate.foundation'; |
| 58 | + return registryUrl; |
| 59 | + } |
| 60 | + |
| 61 | + private getGraphqlUrl(evaultBaseUrl: string): string { |
| 62 | + return new URL('/graphql', evaultBaseUrl).toString(); |
| 63 | + } |
| 64 | + |
| 65 | + normalizeEName(value: string): string { |
| 66 | + return value.startsWith('@') ? value : `@${value}`; |
| 67 | + } |
| 68 | + |
| 69 | + private async getPlatformToken(): Promise<string> { |
| 70 | + if (this.platformToken) return this.platformToken; |
| 71 | + const platform = PUBLIC_CONTROL_PANEL_URL || 'control-panel'; |
| 72 | + const endpoint = new URL('/platforms/certification', this.getRegistryUrl()).toString(); |
| 73 | + const response = await fetch(endpoint, { |
| 74 | + method: 'POST', |
| 75 | + headers: { 'Content-Type': 'application/json' }, |
| 76 | + body: JSON.stringify({ platform }), |
| 77 | + signal: AbortSignal.timeout(10_000) |
| 78 | + }); |
| 79 | + |
| 80 | + if (!response.ok) { |
| 81 | + throw new Error(`Failed to get platform token: HTTP ${response.status}`); |
| 82 | + } |
| 83 | + |
| 84 | + const data = (await response.json()) as PlatformCertificationResponse; |
| 85 | + if (!data.token) { |
| 86 | + throw new Error('Failed to get platform token: missing token in response'); |
| 87 | + } |
| 88 | + |
| 89 | + this.platformToken = data.token; |
| 90 | + return this.platformToken; |
| 91 | + } |
| 92 | + |
| 93 | + async resolveEVaultUrl(eName: string): Promise<string> { |
| 94 | + const normalized = this.normalizeEName(eName); |
| 95 | + const endpoint = new URL( |
| 96 | + `/resolve?w3id=${encodeURIComponent(normalized)}`, |
| 97 | + this.getRegistryUrl() |
| 98 | + ).toString(); |
| 99 | + |
| 100 | + const response = await fetch(endpoint, { |
| 101 | + signal: AbortSignal.timeout(10_000) |
| 102 | + }); |
| 103 | + |
| 104 | + if (!response.ok) { |
| 105 | + throw new Error(`Registry resolve failed: HTTP ${response.status}`); |
| 106 | + } |
| 107 | + |
| 108 | + const data = (await response.json()) as RegistryResolveResponse; |
| 109 | + const resolved = data.evaultUrl ?? data.uri; |
| 110 | + |
| 111 | + if (!resolved) { |
| 112 | + throw new Error('Registry did not return an eVault URL'); |
| 113 | + } |
| 114 | + |
| 115 | + return resolved; |
| 116 | + } |
| 117 | + |
| 118 | + private async resolveDisplayNameForEName(eName: string): Promise<string> { |
| 119 | + const normalized = this.normalizeEName(eName); |
| 120 | + const cached = this.profileNameCache.get(normalized); |
| 121 | + if (cached) return cached; |
| 122 | + |
| 123 | + const [evaultBaseUrl, token] = await Promise.all([ |
| 124 | + this.resolveEVaultUrl(normalized), |
| 125 | + this.getPlatformToken() |
| 126 | + ]); |
| 127 | + |
| 128 | + const client = new GraphQLClient(this.getGraphqlUrl(evaultBaseUrl), { |
| 129 | + headers: { |
| 130 | + Authorization: `Bearer ${token}`, |
| 131 | + 'X-ENAME': normalized |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + const response = await client.request<{ |
| 136 | + metaEnvelopes?: { |
| 137 | + edges?: Array<{ node?: { parsed?: Record<string, unknown> | null } }>; |
| 138 | + }; |
| 139 | + }>(USER_PROFILE_QUERY, { |
| 140 | + ontologyId: USER_PROFILE_ONTOLOGY, |
| 141 | + first: 1 |
| 142 | + }); |
| 143 | + |
| 144 | + const profile = response.metaEnvelopes?.edges?.[0]?.node?.parsed; |
| 145 | + const displayName = |
| 146 | + (typeof profile?.displayName === 'string' && profile.displayName) || |
| 147 | + (typeof profile?.name === 'string' && profile.name) || |
| 148 | + normalized; |
| 149 | + |
| 150 | + this.profileNameCache.set(normalized, displayName); |
| 151 | + return displayName; |
| 152 | + } |
| 153 | + |
| 154 | + async fetchBindingDocuments(eName: string): Promise<{ |
| 155 | + eName: string; |
| 156 | + documents: BindingDocument[]; |
| 157 | + socialConnections: SocialConnection[]; |
| 158 | + }> { |
| 159 | + const normalized = this.normalizeEName(eName); |
| 160 | + const [evaultBaseUrl, token] = await Promise.all([ |
| 161 | + this.resolveEVaultUrl(normalized), |
| 162 | + this.getPlatformToken() |
| 163 | + ]); |
| 164 | + |
| 165 | + const client = new GraphQLClient(this.getGraphqlUrl(evaultBaseUrl), { |
| 166 | + headers: { |
| 167 | + Authorization: `Bearer ${token}`, |
| 168 | + 'X-ENAME': normalized |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + const response = await client.request<BindingDocumentsResponse>( |
| 173 | + BINDING_DOCUMENTS_QUERY, |
| 174 | + { first: 100 } |
| 175 | + ); |
| 176 | + |
| 177 | + const documents: BindingDocument[] = response.bindingDocuments.edges |
| 178 | + .map((edge) => { |
| 179 | + const parsed = edge.node.parsed; |
| 180 | + if (!parsed || typeof parsed !== 'object') return null; |
| 181 | + const { subject, type, data, signatures } = parsed; |
| 182 | + if ( |
| 183 | + typeof subject !== 'string' || |
| 184 | + typeof type !== 'string' || |
| 185 | + typeof data !== 'object' || |
| 186 | + data === null || |
| 187 | + !Array.isArray(signatures) |
| 188 | + ) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + return { |
| 192 | + id: edge.node.id, |
| 193 | + subject, |
| 194 | + type: type as BindingDocument['type'], |
| 195 | + data: data as Record<string, unknown>, |
| 196 | + signatures: signatures as BindingDocument['signatures'] |
| 197 | + }; |
| 198 | + }) |
| 199 | + .filter((doc): doc is BindingDocument => doc !== null); |
| 200 | + |
| 201 | + const socialCandidates = documents.filter( |
| 202 | + (doc) => doc.type === 'social_connection' && doc.signatures.length === 2 |
| 203 | + ); |
| 204 | + |
| 205 | + const socialConnections = ( |
| 206 | + await Promise.all( |
| 207 | + socialCandidates.map(async (doc) => { |
| 208 | + const otherPartyEName = doc.signatures.find( |
| 209 | + (signature) => signature.signer !== normalized |
| 210 | + )?.signer; |
| 211 | + |
| 212 | + if (!otherPartyEName) return null; |
| 213 | + |
| 214 | + const name = await this.resolveDisplayNameForEName(otherPartyEName); |
| 215 | + |
| 216 | + return { |
| 217 | + id: doc.id, |
| 218 | + name, |
| 219 | + witnessEName: otherPartyEName, |
| 220 | + signatures: doc.signatures |
| 221 | + }; |
| 222 | + }) |
| 223 | + ) |
| 224 | + ).filter((entry): entry is NonNullable<typeof entry> => entry !== null); |
| 225 | + |
| 226 | + return { eName: normalized, documents, socialConnections }; |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +export const evaultService = new EvaultService(); |
0 commit comments