Skip to content

Commit f4a87f5

Browse files
committed
chore: sign update messages before sending
1 parent efb17b7 commit f4a87f5

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as automerge from '@automerge/automerge';
44
import { uuid } from '@automerge/automerge';
55
import { RepoContext } from '@automerge/automerge-repo-react-hooks';
66
import { Identity, Key, Messages, SpaceEvents, type SpaceStorageEntry, Utils, store } from '@graphprotocol/hypergraph';
7+
import { canonicalize } from '@graphprotocol/hypergraph/utils/jsc';
78
import { useSelector as useSelectorStore } from '@xstate/store/react';
89
import { Effect, Exit } from 'effect';
910
import * as Schema from 'effect/Schema';
@@ -469,6 +470,11 @@ export function HypergraphAppProvider({
469470
console.error('No encryption private key found');
470471
return;
471472
}
473+
const signaturePrivateKey = keys?.signaturePrivateKey;
474+
if (!signaturePrivateKey) {
475+
console.error('No signature private key found.');
476+
return;
477+
}
472478

473479
const onMessage = async (event: MessageEvent) => {
474480
const data = Messages.deserialize(event.data);
@@ -560,17 +566,13 @@ export function HypergraphAppProvider({
560566

561567
const ephemeralId = uuid();
562568

563-
const nonceAndCiphertext = Messages.encryptMessage({
564-
message: lastLocalChange,
565-
secretKey: Utils.hexToBytes(space.keys[0].key),
566-
});
567-
568-
const messageToSend = {
569-
type: 'create-update',
569+
const messageToSend = Messages.signedUpdateMessage({
570570
ephemeralId,
571-
update: nonceAndCiphertext,
572571
spaceId: space.id,
573-
} as const satisfies Messages.RequestCreateUpdate;
572+
message: lastLocalChange,
573+
secretKey: space.keys[0].key,
574+
signaturePrivateKey,
575+
});
574576
websocketConnection.send(Messages.serialize(messageToSend));
575577
} catch (error) {
576578
console.error('Error sending message', error);
@@ -664,7 +666,7 @@ export function HypergraphAppProvider({
664666
return () => {
665667
websocketConnection.removeEventListener('message', onMessage);
666668
};
667-
}, [websocketConnection, spaces, keys?.encryptionPrivateKey]);
669+
}, [websocketConnection, spaces, keys?.encryptionPrivateKey, keys?.signaturePrivateKey]);
668670

669671
const createSpaceForContext = async () => {
670672
if (!accountId) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './decrypt-message.js';
22
export * from './encrypt-message.js';
33
export * from './serialize.js';
4+
export * from './signed-update-message.js';
45
export * from './types.js';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { secp256k1 } from '@noble/curves/secp256k1';
2+
import { canonicalize, hexToBytes, stringToUint8Array } from '../utils/index.js';
3+
import { encryptMessage } from './encrypt-message.js';
4+
import type { RequestCreateUpdate } from './types.js';
5+
6+
interface Params {
7+
ephemeralId: string;
8+
spaceId: string;
9+
message: Uint8Array;
10+
secretKey: string;
11+
signaturePrivateKey: string;
12+
}
13+
14+
export const signedUpdateMessage = ({
15+
ephemeralId,
16+
spaceId,
17+
message,
18+
secretKey,
19+
signaturePrivateKey,
20+
}: Params): RequestCreateUpdate => {
21+
const update = encryptMessage({
22+
message,
23+
secretKey: hexToBytes(secretKey),
24+
});
25+
26+
const messageToSign = stringToUint8Array(
27+
canonicalize({
28+
ephemeralId,
29+
update,
30+
spaceId,
31+
}),
32+
);
33+
34+
const signature = secp256k1.sign(messageToSign, hexToBytes(signaturePrivateKey), { prehash: true }).toCompactHex();
35+
36+
return {
37+
type: 'create-update',
38+
ephemeralId,
39+
update,
40+
spaceId,
41+
signature,
42+
};
43+
};

packages/hypergraph/src/messages/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const RequestCreateUpdate = Schema.Struct({
8686
update: Schema.Uint8Array,
8787
spaceId: Schema.String,
8888
ephemeralId: Schema.String, // used to identify the confirmation message
89+
signature: Schema.String,
8990
});
9091

9192
export const RequestMessage = Schema.Union(

0 commit comments

Comments
 (0)