Skip to content

Commit a31d2ba

Browse files
authored
fix verifyIdentityOwnership (#263)
1 parent 2519dbc commit a31d2ba

File tree

5 files changed

+179
-37
lines changed

5 files changed

+179
-37
lines changed

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const decodeResponseMessage = Schema.decodeUnknownEither(Messages.ResponseMessag
4141

4242
const queryClient = new QueryClient();
4343

44+
const CHAIN = Connect.GEO_TESTNET;
45+
const RPC_URL = Connect.TESTNET_RPC_URL;
46+
4447
export type HypergraphAppCtx = {
4548
// auth related
4649
logout(): void;
@@ -359,7 +362,12 @@ export function HypergraphAppProvider({
359362
signature: update.signature,
360363
accountAddress: update.accountAddress,
361364
});
362-
const authorIdentity = await Identity.getVerifiedIdentity(update.accountAddress, syncServerUri);
365+
const authorIdentity = await Identity.getVerifiedIdentity(
366+
update.accountAddress,
367+
syncServerUri,
368+
CHAIN,
369+
RPC_URL,
370+
);
363371
if (authorIdentity.signaturePublicKey !== signer) {
364372
// console.error(
365373
// `Received invalid signature, recovered signer is ${signer},
@@ -394,7 +402,7 @@ export function HypergraphAppProvider({
394402
const getVerifiedIdentity = (accountAddress: string) => {
395403
return Effect.gen(function* () {
396404
const identity = yield* Effect.tryPromise({
397-
try: () => Identity.getVerifiedIdentity(accountAddress, syncServerUri),
405+
try: () => Identity.getVerifiedIdentity(accountAddress, syncServerUri, CHAIN, RPC_URL),
398406
catch: () => new Identity.InvalidIdentityError(),
399407
});
400408
return identity;
@@ -638,6 +646,8 @@ export function HypergraphAppProvider({
638646
inbox,
639647
response.spaceId,
640648
syncServerUri,
649+
CHAIN,
650+
RPC_URL,
641651
);
642652
if (!isValid) {
643653
console.error('Invalid message', response.message, inbox.inboxId);
@@ -684,6 +694,8 @@ export function HypergraphAppProvider({
684694
inbox,
685695
identity.address,
686696
syncServerUri,
697+
CHAIN,
698+
RPC_URL,
687699
);
688700
if (!isValid) {
689701
console.error('Invalid message', response.message, inbox.inboxId);
@@ -747,7 +759,14 @@ export function HypergraphAppProvider({
747759
response.messages.map(
748760
// If the message has a signature, check that the signature is valid for the authorAccountAddress
749761
async (message) => {
750-
return Inboxes.validateAccountInboxMessage(message, inbox, identity.address, syncServerUri);
762+
return Inboxes.validateAccountInboxMessage(
763+
message,
764+
inbox,
765+
identity.address,
766+
syncServerUri,
767+
CHAIN,
768+
RPC_URL,
769+
);
751770
},
752771
),
753772
);
@@ -807,7 +826,7 @@ export function HypergraphAppProvider({
807826
response.messages.map(
808827
// If the message has a signature, check that the signature is valid for the authorAccountAddress
809828
async (message) => {
810-
return Inboxes.validateSpaceInboxMessage(message, inbox, space.id, syncServerUri);
829+
return Inboxes.validateSpaceInboxMessage(message, inbox, space.id, syncServerUri, CHAIN, RPC_URL);
811830
},
812831
),
813832
);
@@ -1259,7 +1278,7 @@ export function HypergraphAppProvider({
12591278
console.error('No state found for space');
12601279
return;
12611280
}
1262-
const inviteeWithKeys = await Identity.getVerifiedIdentity(invitee.accountAddress, syncServerUri);
1281+
const inviteeWithKeys = await Identity.getVerifiedIdentity(invitee.accountAddress, syncServerUri, CHAIN, RPC_URL);
12631282
const spaceEvent = await Effect.runPromiseExit(
12641283
SpaceEvents.createInvitation({
12651284
author: {
@@ -1305,7 +1324,7 @@ export function HypergraphAppProvider({
13051324

13061325
const getVerifiedIdentity = useCallback(
13071326
(accountAddress: string) => {
1308-
return Identity.getVerifiedIdentity(accountAddress, syncServerUri);
1327+
return Identity.getVerifiedIdentity(accountAddress, syncServerUri, CHAIN, RPC_URL);
13091328
},
13101329
[syncServerUri],
13111330
);

packages/hypergraph/src/identity/get-verified-identity.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as Schema from 'effect/Schema';
2+
import type { Chain } from 'viem';
23
import * as Messages from '../messages/index.js';
34
import { store } from '../store.js';
45
import { verifyIdentityOwnership } from './prove-ownership.js';
56

67
export const getVerifiedIdentity = async (
78
accountAddress: string,
89
syncServerUri: string,
10+
chain: Chain,
11+
rpcUrl: string,
912
): Promise<{
1013
accountAddress: string;
1114
encryptionPublicKey: string;
@@ -32,6 +35,8 @@ export const getVerifiedIdentity = async (
3235
resDecoded.signaturePublicKey,
3336
resDecoded.accountProof,
3437
resDecoded.keyProof,
38+
chain,
39+
rpcUrl,
3540
))
3641
) {
3742
throw new Error('Invalid identity in getVerifiedIdentity');

packages/hypergraph/src/identity/prove-ownership.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { http, type Chain, type Hex, type WalletClient, createPublicClient, veri
22
import { privateKeyToAccount } from 'viem/accounts';
33

44
import type { SmartAccountClient } from 'permissionless';
5-
import { DEFAULT_RPC_URL, GEOGENESIS } from '../connect/smart-account.js';
65
import { publicKeyToAddress } from '../utils/index.js';
76
import type { IdentityKeys } from './types.js';
87

@@ -55,10 +54,9 @@ export const verifyIdentityOwnership = async (
5554
publicKey: string,
5655
accountProof: string,
5756
keyProof: string,
58-
chain: Chain = GEOGENESIS,
59-
rpcUrl: string = DEFAULT_RPC_URL,
57+
chain: Chain,
58+
rpcUrl: string,
6059
): Promise<boolean> => {
61-
console.log('verifyIdentityOwnership', accountAddress, publicKey, accountProof, keyProof, chain, rpcUrl);
6260
const keyProofMessage = getKeyProofMessage(accountAddress, publicKey);
6361
const publicClient = createPublicClient({
6462
chain,

packages/hypergraph/src/inboxes/message-validation.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Chain } from 'viem';
12
import * as Identity from '../identity/index.js';
23
import type * as Messages from '../messages/index.js';
34
import type { AccountInboxStorageEntry, SpaceInboxStorageEntry } from '../store.js';
@@ -8,6 +9,8 @@ export const validateSpaceInboxMessage = async (
89
inbox: SpaceInboxStorageEntry,
910
spaceId: string,
1011
syncServerUri: string,
12+
chain: Chain,
13+
rpcUrl: string,
1114
) => {
1215
if (message.signature) {
1316
if (inbox.authPolicy === 'anonymous') {
@@ -19,7 +22,12 @@ export const validateSpaceInboxMessage = async (
1922
return false;
2023
}
2124
const signer = recoverSpaceInboxMessageSigner(message, spaceId, inbox.inboxId);
22-
const verifiedIdentity = await Identity.getVerifiedIdentity(message.authorAccountAddress, syncServerUri);
25+
const verifiedIdentity = await Identity.getVerifiedIdentity(
26+
message.authorAccountAddress,
27+
syncServerUri,
28+
chain,
29+
rpcUrl,
30+
);
2331
const isValid = signer === verifiedIdentity.signaturePublicKey;
2432
if (!isValid) {
2533
console.error('Invalid signature', signer, verifiedIdentity.signaturePublicKey);
@@ -39,6 +47,8 @@ export const validateAccountInboxMessage = async (
3947
inbox: AccountInboxStorageEntry,
4048
accountAddress: string,
4149
syncServerUri: string,
50+
chain: Chain,
51+
rpcUrl: string,
4252
) => {
4353
if (message.signature) {
4454
if (inbox.authPolicy === 'anonymous') {
@@ -50,7 +60,12 @@ export const validateAccountInboxMessage = async (
5060
return false;
5161
}
5262
const signer = recoverAccountInboxMessageSigner(message, accountAddress, inbox.inboxId);
53-
const verifiedIdentity = await Identity.getVerifiedIdentity(message.authorAccountAddress, syncServerUri);
63+
const verifiedIdentity = await Identity.getVerifiedIdentity(
64+
message.authorAccountAddress,
65+
syncServerUri,
66+
chain,
67+
rpcUrl,
68+
);
5469
const isValid = signer === verifiedIdentity.signaturePublicKey;
5570
if (!isValid) {
5671
console.error('Invalid signature', signer, verifiedIdentity.signaturePublicKey);

0 commit comments

Comments
 (0)