Skip to content

Commit b52c68c

Browse files
pcarranzavnikgraf
andauthored
fix: use the app identity keys everywhere (#276)
Co-authored-by: Nik Graf <[email protected]>
1 parent 8182fe0 commit b52c68c

File tree

24 files changed

+448
-366
lines changed

24 files changed

+448
-366
lines changed

apps/connect/src/routes/authenticate.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ function AuthenticateComponent() {
261261
id: keyData.id,
262262
ciphertext: Utils.bytesToHex(keyBox.keyBoxCiphertext),
263263
nonce: Utils.bytesToHex(keyBox.keyBoxNonce),
264-
authorPublicKey: appIdentity.encryptionPublicKey,
264+
authorPublicKey: keys.encryptionPublicKey,
265265
accountAddress: accountAddress,
266266
};
267267
});
@@ -388,19 +388,25 @@ function AuthenticateComponent() {
388388
rpcUrl: import.meta.env.VITE_HYPERGRAPH_RPC_URL,
389389
});
390390

391+
const appIdentityKeys = {
392+
encryptionPrivateKey: newAppIdentity.encryptionPrivateKey,
393+
encryptionPublicKey: newAppIdentity.encryptionPublicKey,
394+
signaturePrivateKey: newAppIdentity.signaturePrivateKey,
395+
signaturePublicKey: newAppIdentity.signaturePublicKey,
396+
};
391397
console.log('encrypting app identity');
392398
const { ciphertext, nonce } = await Connect.encryptAppIdentity(
393399
signer,
394400
newAppIdentity.address,
395401
newAppIdentity.addressPrivateKey,
396402
permissionId,
397-
keys,
403+
appIdentityKeys,
398404
);
399405
console.log('proving ownership');
400406
const { accountProof, keyProof } = await Identity.proveIdentityOwnership(
401407
smartAccountClient,
402408
accountAddress,
403-
keys,
409+
appIdentityKeys,
404410
);
405411

406412
const message: Messages.RequestConnectCreateAppIdentity = {
@@ -432,10 +438,10 @@ function AuthenticateComponent() {
432438
address: newAppIdentity.address,
433439
addressPrivateKey: newAppIdentity.addressPrivateKey,
434440
accountAddress,
435-
encryptionPrivateKey: keys.encryptionPrivateKey,
436-
signaturePrivateKey: keys.signaturePrivateKey,
437-
encryptionPublicKey: keys.encryptionPublicKey,
438-
signaturePublicKey: keys.signaturePublicKey,
441+
encryptionPrivateKey: newAppIdentity.encryptionPrivateKey,
442+
signaturePrivateKey: newAppIdentity.signaturePrivateKey,
443+
encryptionPublicKey: newAppIdentity.encryptionPublicKey,
444+
signaturePublicKey: newAppIdentity.signaturePublicKey,
439445
sessionToken: appIdentityResponse.appIdentity.sessionToken,
440446
sessionTokenExpires: new Date(appIdentityResponse.appIdentity.sessionTokenExpires),
441447
permissionId,

apps/events/src/Boot.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ declare module '@tanstack/react-router' {
1515

1616
export function Boot() {
1717
return (
18-
<HypergraphAppProvider syncServerUri="http://localhost:3030" mapping={mapping}>
18+
<HypergraphAppProvider
19+
syncServerUri="http://localhost:3030"
20+
mapping={mapping}
21+
appId="93bb8907-085a-4a0e-83dd-62b0dc98e793"
22+
>
1923
<RouterProvider router={router} />
2024
</HypergraphAppProvider>
2125
);

apps/events/src/routes/login.lazy.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function Login() {
1616
storage: localStorage,
1717
connectUrl: 'http://localhost:5180',
1818
successUrl: `${window.location.origin}/authenticate-success`,
19-
appId: '93bb8907-085a-4a0e-83dd-62b0dc98e793',
2019
redirectFn: (url: URL) => {
2120
window.location.href = url.toString();
2221
},

apps/next-example/src/components/providers.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export default function Providers({ children }: { children: React.ReactNode }) {
77
const storage = typeof window !== 'undefined' ? window.localStorage : (undefined as unknown as Storage);
88

99
return (
10-
<HypergraphAppProvider syncServerUri="http://localhost:3030" mapping={{}}>
10+
<HypergraphAppProvider
11+
syncServerUri="http://localhost:3030"
12+
mapping={{}}
13+
appId="83aa8907-085b-430f-1296-ab87dc98e793"
14+
>
1115
{children}
1216
</HypergraphAppProvider>
1317
);

apps/server/src/handlers/applySpaceEvent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Messages } from '@graphprotocol/hypergraph';
44
import { Identity, SpaceEvents } from '@graphprotocol/hypergraph';
55

66
import { prisma } from '../prisma.js';
7-
import { getConnectIdentity } from './getConnectIdentity.js';
7+
import { getAppOrConnectIdentity } from './getAppOrConnectIdentity.js';
88

99
type Params = {
1010
accountAddress: string;
@@ -40,7 +40,7 @@ export async function applySpaceEvent({ accountAddress, spaceId, event, keyBoxes
4040
orderBy: { counter: 'desc' },
4141
});
4242

43-
const getVerifiedIdentity = (accountAddressToFetch: string) => {
43+
const getVerifiedIdentity = (accountAddressToFetch: string, publicKey: string) => {
4444
console.log('getVerifiedIdentity', accountAddressToFetch, accountAddress);
4545
// applySpaceEvent is only allowed to be called by the account that is applying the event
4646
if (accountAddressToFetch !== accountAddress) {
@@ -49,7 +49,8 @@ export async function applySpaceEvent({ accountAddress, spaceId, event, keyBoxes
4949

5050
return Effect.gen(function* () {
5151
const identity = yield* Effect.tryPromise({
52-
try: () => getConnectIdentity({ accountAddress: accountAddressToFetch }),
52+
try: () =>
53+
getAppOrConnectIdentity({ accountAddress: accountAddressToFetch, signaturePublicKey: publicKey, spaceId }),
5354
catch: () => new Identity.InvalidIdentityError(),
5455
});
5556
return identity;

apps/server/src/handlers/create-space.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Messages } from '@graphprotocol/hypergraph';
55
import { Identity, SpaceEvents } from '@graphprotocol/hypergraph';
66

77
import { prisma } from '../prisma.js';
8-
import { getConnectIdentity } from './getConnectIdentity.js';
8+
import { getAppOrConnectIdentity } from './getAppOrConnectIdentity.js';
99

1010
type Params = {
1111
accountAddress: string;
@@ -26,15 +26,15 @@ export const createSpace = async ({
2626
infoSignatureRecovery,
2727
name,
2828
}: Params) => {
29-
const getVerifiedIdentity = (accountAddressToFetch: string) => {
29+
const getVerifiedIdentity = (accountAddressToFetch: string, publicKey: string) => {
3030
// applySpaceEvent is only allowed to be called by the account that is applying the event
3131
if (accountAddressToFetch !== accountAddress) {
3232
return Effect.fail(new Identity.InvalidIdentityError());
3333
}
3434

3535
return Effect.gen(function* () {
3636
const identity = yield* Effect.tryPromise({
37-
try: () => getConnectIdentity({ accountAddress: accountAddressToFetch }),
37+
try: () => getAppOrConnectIdentity({ accountAddress: accountAddressToFetch, signaturePublicKey: publicKey }),
3838
catch: () => new Identity.InvalidIdentityError(),
3939
});
4040
return identity;

apps/server/src/handlers/getAccountInbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function getAccountInbox({ accountAddress, inboxId }: { accountAddr
88
id: true,
99
account: {
1010
select: {
11-
id: true,
11+
address: true,
1212
},
1313
},
1414
isPublic: true,
@@ -24,7 +24,7 @@ export async function getAccountInbox({ accountAddress, inboxId }: { accountAddr
2424

2525
return {
2626
inboxId: inbox.id,
27-
accountAddress: inbox.account.id,
27+
accountAddress: inbox.account.address,
2828
isPublic: inbox.isPublic,
2929
authPolicy: inbox.authPolicy as Inboxes.InboxSenderAuthPolicy,
3030
encryptionPublicKey: inbox.encryptionPublicKey,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { prisma } from '../prisma.js';
2+
3+
type Params =
4+
| {
5+
accountAddress: string;
6+
signaturePublicKey: string;
7+
spaceId?: string;
8+
}
9+
| {
10+
accountAddress: string;
11+
appId: string;
12+
spaceId?: string;
13+
};
14+
15+
export type GetIdentityResult = {
16+
accountAddress: string;
17+
ciphertext: string;
18+
nonce: string;
19+
signaturePublicKey: string;
20+
encryptionPublicKey: string;
21+
accountProof: string;
22+
keyProof: string;
23+
appId: string | null;
24+
};
25+
26+
export const getAppOrConnectIdentity = async (params: Params): Promise<GetIdentityResult> => {
27+
if (!('appId' in params)) {
28+
const where: { address: string; connectSignaturePublicKey?: string } = { address: params.accountAddress };
29+
if ('signaturePublicKey' in params) {
30+
where.connectSignaturePublicKey = params.signaturePublicKey;
31+
}
32+
const account = await prisma.account.findFirst({
33+
where,
34+
});
35+
if (account) {
36+
return {
37+
accountAddress: account.address,
38+
ciphertext: account.connectCiphertext,
39+
nonce: account.connectNonce,
40+
signaturePublicKey: account.connectSignaturePublicKey,
41+
encryptionPublicKey: account.connectEncryptionPublicKey,
42+
accountProof: account.connectAccountProof,
43+
keyProof: account.connectKeyProof,
44+
appId: null,
45+
};
46+
}
47+
}
48+
const appWhere: {
49+
accountAddress: string;
50+
appId?: string;
51+
signaturePublicKey?: string;
52+
spaces?: { some: { id: string } };
53+
} = {
54+
accountAddress: params.accountAddress,
55+
};
56+
if ('signaturePublicKey' in params) {
57+
appWhere.signaturePublicKey = params.signaturePublicKey;
58+
}
59+
if ('appId' in params) {
60+
appWhere.appId = params.appId;
61+
}
62+
if (params.spaceId) {
63+
appWhere.spaces = { some: { id: params.spaceId } };
64+
}
65+
66+
const appIdentity = await prisma.appIdentity.findFirst({
67+
where: appWhere,
68+
});
69+
if (appIdentity) {
70+
return {
71+
accountAddress: appIdentity.accountAddress,
72+
ciphertext: appIdentity.ciphertext,
73+
nonce: appIdentity.nonce,
74+
signaturePublicKey: appIdentity.signaturePublicKey,
75+
encryptionPublicKey: appIdentity.encryptionPublicKey,
76+
accountProof: appIdentity.accountProof,
77+
keyProof: appIdentity.keyProof,
78+
appId: appIdentity.appId,
79+
};
80+
}
81+
throw new Error('Identity not found');
82+
};

apps/server/src/handlers/getSpace.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { prisma } from '../prisma.js';
44
type Params = {
55
spaceId: string;
66
accountAddress: string;
7+
appIdentityAddress: string;
78
};
89

9-
export const getSpace = async ({ spaceId, accountAddress }: Params) => {
10+
export const getSpace = async ({ spaceId, accountAddress, appIdentityAddress }: Params) => {
1011
const space = await prisma.space.findUniqueOrThrow({
1112
where: {
1213
id: spaceId,
@@ -27,6 +28,7 @@ export const getSpace = async ({ spaceId, accountAddress }: Params) => {
2728
keyBoxes: {
2829
where: {
2930
accountAddress,
31+
appIdentityAddress,
3032
},
3133
select: {
3234
nonce: true,

0 commit comments

Comments
 (0)