Skip to content

Commit 5618129

Browse files
committed
move getVerifiedIdentity to core
1 parent 81e998f commit 5618129

File tree

5 files changed

+95
-26
lines changed

5 files changed

+95
-26
lines changed

apps/server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { parse } from 'node:url';
21
import { Identity, Messages, SpaceEvents, Utils } from '@graphprotocol/hypergraph';
32
import cors from 'cors';
43
import { Effect, Exit, Schema } from 'effect';
54
import express, { type Request, type Response } from 'express';
5+
import { parse } from 'node:url';
66
import { SiweMessage } from 'siwe';
77
import type { Hex } from 'viem';
88
import WebSocket, { WebSocketServer } from 'ws';

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type HypergraphAppCtx = {
4343
acceptInvitation(params: Readonly<{ invitation: Messages.Invitation }>): Promise<unknown>;
4444
subscribeToSpace(params: Readonly<{ spaceId: string }>): void;
4545
inviteToSpace(params: Readonly<{ space: SpaceStorageEntry; invitee: { accountId: Address } }>): Promise<unknown>;
46-
getUserIdentity(accountId: string): Promise<{
46+
getVerifiedIdentity(accountId: string): Promise<{
4747
accountId: string;
4848
encryptionPublicKey: string;
4949
signaturePublicKey: string;
@@ -52,28 +52,36 @@ export type HypergraphAppCtx = {
5252
};
5353

5454
export const HypergraphAppContext = createContext<HypergraphAppCtx>({
55-
async login() {},
56-
logout() {},
57-
setIdentityAndSessionToken() {},
55+
async login() {
56+
throw new Error('login is missing');
57+
},
58+
logout() {
59+
throw new Error('logout is missing');
60+
},
61+
setIdentityAndSessionToken() {
62+
throw new Error('setIdentityAndSessionToken is missing');
63+
},
5864
invitations: [],
5965
async createSpace() {
60-
return {};
66+
throw new Error('createSpace is missing');
67+
},
68+
listSpaces() {
69+
throw new Error('listSpaces is missing');
70+
},
71+
listInvitations() {
72+
throw new Error('listInvitations is missing');
6173
},
62-
listSpaces() {},
63-
listInvitations() {},
6474
async acceptInvitation() {
65-
return {};
75+
throw new Error('acceptInvitation is missing');
76+
},
77+
subscribeToSpace() {
78+
throw new Error('subscribeToSpace is missing');
6679
},
67-
subscribeToSpace() {},
6880
async inviteToSpace() {
69-
return {};
81+
throw new Error('inviteToSpace is missing');
7082
},
71-
async getUserIdentity() {
72-
return {
73-
accountId: '',
74-
encryptionPublicKey: '',
75-
signaturePublicKey: '',
76-
};
83+
async getVerifiedIdentity() {
84+
throw new Error('getVerifiedIdentity is missing');
7785
},
7886
loading: true,
7987
});
@@ -699,7 +707,7 @@ export function HypergraphAppProvider({
699707
signaturePublicKey: string;
700708
}> => {
701709
const storeState = store.getSnapshot();
702-
const identity = storeState.context.userIdentities[accountId];
710+
const identity = storeState.context.identities[accountId];
703711
if (identity) {
704712
return {
705713
accountId,
@@ -725,7 +733,7 @@ export function HypergraphAppProvider({
725733
}
726734

727735
store.send({
728-
type: 'addUserIdentity',
736+
type: 'addVerifiedIdentity',
729737
accountId: resDecoded.accountId,
730738
encryptionPublicKey: resDecoded.encryptionPublicKey,
731739
signaturePublicKey: resDecoded.signaturePublicKey,
@@ -807,6 +815,13 @@ export function HypergraphAppProvider({
807815
websocketConnection?.send(Messages.serialize(message));
808816
};
809817

818+
const getVerifiedIdentity = useCallback(
819+
(accountId: string) => {
820+
return Identity.getVerifiedIdentity(accountId, syncServerUri);
821+
},
822+
[syncServerUri],
823+
);
824+
810825
return (
811826
<HypergraphAppContext.Provider
812827
value={{
@@ -819,7 +834,7 @@ export function HypergraphAppProvider({
819834
listInvitations,
820835
acceptInvitation: acceptInvitationForContext,
821836
subscribeToSpace,
822-
getUserIdentity,
837+
getVerifiedIdentity,
823838
inviteToSpace,
824839
loading,
825840
}}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as Schema from 'effect/Schema';
2+
import * as Messages from '../messages/index.js';
3+
import { store } from '../store.js';
4+
import { verifyIdentityOwnership } from './prove-ownership.js';
5+
6+
export const getVerifiedIdentity = async (
7+
accountId: string,
8+
syncServerUri: string,
9+
): Promise<{
10+
accountId: string;
11+
encryptionPublicKey: string;
12+
signaturePublicKey: string;
13+
}> => {
14+
const storeState = store.getSnapshot();
15+
const identity = storeState.context.identities[accountId];
16+
if (identity) {
17+
return {
18+
accountId,
19+
encryptionPublicKey: identity.encryptionPublicKey,
20+
signaturePublicKey: identity.signaturePublicKey,
21+
};
22+
}
23+
const res = await fetch(`${syncServerUri}/identity?accountId=${accountId}`);
24+
if (res.status !== 200) {
25+
throw new Error('Failed to fetch identity');
26+
}
27+
const resDecoded = Schema.decodeUnknownSync(Messages.ResponseIdentity)(await res.json());
28+
29+
if (
30+
!(await verifyIdentityOwnership(
31+
resDecoded.accountId,
32+
resDecoded.signaturePublicKey,
33+
resDecoded.accountProof,
34+
resDecoded.keyProof,
35+
))
36+
) {
37+
throw new Error('Invalid identity');
38+
}
39+
40+
store.send({
41+
type: 'addVerifiedIdentity',
42+
accountId: resDecoded.accountId,
43+
encryptionPublicKey: resDecoded.encryptionPublicKey,
44+
signaturePublicKey: resDecoded.signaturePublicKey,
45+
accountProof: resDecoded.accountProof,
46+
keyProof: resDecoded.keyProof,
47+
});
48+
return {
49+
accountId: resDecoded.accountId,
50+
encryptionPublicKey: resDecoded.encryptionPublicKey,
51+
signaturePublicKey: resDecoded.signaturePublicKey,
52+
};
53+
};

packages/hypergraph/src/identity/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './auth-storage.js';
22
export * from './create-identity-keys.js';
3+
export * from './get-verified-identity.js';
34
export * from './identity-encryption.js';
45
export * from './login.js';
56
export * from './prove-ownership.js';

packages/hypergraph/src/store.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface StoreContext {
2121
updatesInFlight: string[];
2222
invitations: Invitation[];
2323
repo: Repo;
24-
userIdentities: {
24+
identities: {
2525
[accountId: string]: {
2626
encryptionPublicKey: string;
2727
signaturePublicKey: string;
@@ -40,7 +40,7 @@ const initialStoreContext: StoreContext = {
4040
updatesInFlight: [],
4141
invitations: [],
4242
repo: new Repo({}),
43-
userIdentities: {},
43+
identities: {},
4444
authenticated: false,
4545
accountId: null,
4646
sessionToken: null,
@@ -57,7 +57,7 @@ type StoreEvent =
5757
| { type: 'updateConfirmed'; spaceId: string; clock: number }
5858
| { type: 'applyUpdate'; spaceId: string; firstUpdateClock: number; lastUpdateClock: number }
5959
| {
60-
type: 'addUserIdentity';
60+
type: 'addVerifiedIdentity';
6161
accountId: string;
6262
encryptionPublicKey: string;
6363
signaturePublicKey: string;
@@ -193,7 +193,7 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
193193
}),
194194
};
195195
},
196-
addUserIdentity: (
196+
addVerifiedIdentity: (
197197
context,
198198
event: {
199199
accountId: string;
@@ -205,8 +205,8 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
205205
) => {
206206
return {
207207
...context,
208-
userIdentities: {
209-
...context.userIdentities,
208+
identities: {
209+
...context.identities,
210210
[event.accountId]: {
211211
encryptionPublicKey: event.encryptionPublicKey,
212212
signaturePublicKey: event.signaturePublicKey,

0 commit comments

Comments
 (0)