Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/hypergraph/src/space-events/accept-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ export const acceptInvitation = ({
previousEventHash,
};
const encodedTransaction = stringToUint8Array(canonicalize(transaction));
const signature = secp256k1
.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), { prehash: true })
.toCompactHex();
const signatureResult = secp256k1.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), {
prehash: true,
});

return Effect.succeed({
transaction,
author: {
accountId: author.accountId,
publicKey: author.signaturePublicKey,
signature,
signature: {
hex: signatureResult.toCompactHex(),
recovery: signatureResult.recovery,
},
},
});
};
21 changes: 10 additions & 11 deletions packages/hypergraph/src/space-events/apply-event.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { secp256k1 } from '@noble/curves/secp256k1';
import { sha256 } from '@noble/hashes/sha256';
import { Effect, Schema } from 'effect';
import type { ParseError } from 'effect/ParseResult';

import { canonicalize, hexToBytes, stringToUint8Array } from '../utils/index.js';
import { canonicalize, stringToUint8Array } from '../utils/index.js';
import { hashEvent } from './hash-event.js';
import {
InvalidEventError,
Expand Down Expand Up @@ -41,16 +41,15 @@ export const applyEvent = ({

const encodedTransaction = stringToUint8Array(canonicalize(event.transaction));

const isValidSignature = secp256k1.verify(
event.author.signature,
encodedTransaction,
hexToBytes(event.author.publicKey),
{
prehash: true,
},
);
let signatureInstance = secp256k1.Signature.fromCompact(event.author.signature.hex);
signatureInstance = signatureInstance.addRecoveryBit(event.author.signature.recovery);
// @ts-expect-error
const authorPublicKey = signatureInstance.recoverPublicKey(sha256(encodedTransaction));
// TODO compare it to the public key from the author accountId (this already verifies the signature)
// in case of a failure we return Effect.fail(new VerifySignatureError());

if (!isValidSignature) {
// biome-ignore lint/correctness/noConstantCondition: wip
if (false) {
return Effect.fail(new VerifySignatureError());
}

Expand Down
12 changes: 7 additions & 5 deletions packages/hypergraph/src/space-events/create-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export const createInvitation = ({
previousEventHash,
};
const encodedTransaction = stringToUint8Array(canonicalize(transaction));
const signature = secp256k1
.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), { prehash: true })
.toCompactHex();
const signatureResult = secp256k1.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), {
prehash: true,
});

return Effect.succeed({
transaction,
author: {
accountId: author.accountId,
publicKey: author.signaturePublicKey,
signature,
signature: {
hex: signatureResult.toCompactHex(),
recovery: signatureResult.recovery,
},
},
});
};
12 changes: 7 additions & 5 deletions packages/hypergraph/src/space-events/create-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ export const createSpace = ({ author }: Params): Effect.Effect<CreateSpaceEvent,
creatorAccountId: author.accountId,
};
const encodedTransaction = stringToUint8Array(canonicalize(transaction));
const signature = secp256k1
.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), { prehash: true })
.toCompactHex();
const signatureResult = secp256k1.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), {
prehash: true,
});

const event: CreateSpaceEvent = {
transaction,
author: {
accountId: author.accountId,
publicKey: author.signaturePublicKey,
signature,
signature: {
hex: signatureResult.toCompactHex(),
recovery: signatureResult.recovery,
},
},
};

Expand Down
12 changes: 7 additions & 5 deletions packages/hypergraph/src/space-events/delete-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ export const deleteSpace = ({ author, id, previousEventHash }: Params): Effect.E
previousEventHash,
};
const encodedTransaction = stringToUint8Array(canonicalize(transaction));
const signature = secp256k1
.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), { prehash: true })
.toCompactHex();
const signatureResult = secp256k1.sign(encodedTransaction, hexToBytes(author.signaturePrivateKey), {
prehash: true,
});

const event: DeleteSpaceEvent = {
transaction,
author: {
accountId: author.accountId,
publicKey: author.signaturePublicKey,
signature,
signature: {
hex: signatureResult.toCompactHex(),
recovery: signatureResult.recovery,
},
},
};
return Effect.succeed(event);
Expand Down
6 changes: 2 additions & 4 deletions packages/hypergraph/src/space-events/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as Schema from 'effect/Schema';
import { SignatureWithRecovery } from '../types.js';

export const EventAuthor = Schema.Struct({
accountId: Schema.String,
// must be validated if it belongs to the accountId before being used
// Note: could be removed, but also might be useful to keep around in case accounts rotate their keys
publicKey: Schema.String,
signature: Schema.String,
signature: SignatureWithRecovery,
});

export type EventAuthor = Schema.Schema.Type<typeof Author>;
Expand Down
8 changes: 8 additions & 0 deletions packages/hypergraph/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Schema from 'effect/Schema';

export const SignatureWithRecovery = Schema.Struct({
hex: Schema.String,
recovery: Schema.Number,
});

export type SignatureWithRecovery = Schema.Schema.Type<typeof SignatureWithRecovery>;
Loading