diff --git a/packages/hypergraph/src/space-events/accept-invitation.ts b/packages/hypergraph/src/space-events/accept-invitation.ts index 4a71d4d6..8ce075ab 100644 --- a/packages/hypergraph/src/space-events/accept-invitation.ts +++ b/packages/hypergraph/src/space-events/accept-invitation.ts @@ -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, + }, }, }); }; diff --git a/packages/hypergraph/src/space-events/apply-event.ts b/packages/hypergraph/src/space-events/apply-event.ts index 5214c404..b2085c1a 100644 --- a/packages/hypergraph/src/space-events/apply-event.ts +++ b/packages/hypergraph/src/space-events/apply-event.ts @@ -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, @@ -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()); } diff --git a/packages/hypergraph/src/space-events/create-invitation.ts b/packages/hypergraph/src/space-events/create-invitation.ts index 288f65c4..9fa7586b 100644 --- a/packages/hypergraph/src/space-events/create-invitation.ts +++ b/packages/hypergraph/src/space-events/create-invitation.ts @@ -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, + }, }, }); }; diff --git a/packages/hypergraph/src/space-events/create-space.ts b/packages/hypergraph/src/space-events/create-space.ts index f735b127..a071da7f 100644 --- a/packages/hypergraph/src/space-events/create-space.ts +++ b/packages/hypergraph/src/space-events/create-space.ts @@ -15,16 +15,18 @@ export const createSpace = ({ author }: Params): Effect.Effect; diff --git a/packages/hypergraph/src/types.ts b/packages/hypergraph/src/types.ts new file mode 100644 index 00000000..288362a5 --- /dev/null +++ b/packages/hypergraph/src/types.ts @@ -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;