From 424c7c79d7569c7d3ae096986557327f0cb69337 Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 16 Mar 2023 17:10:17 +0000 Subject: [PATCH] chore: avoid SubtleCrypto --- packages/app/src/Feed/EventPublisher.ts | 2 +- packages/nostr/package.json | 1 + packages/nostr/src/legacy/Event.ts | 11 +++++------ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/app/src/Feed/EventPublisher.ts b/packages/app/src/Feed/EventPublisher.ts index 1d5eaf2..16af28b 100644 --- a/packages/app/src/Feed/EventPublisher.ts +++ b/packages/app/src/Feed/EventPublisher.ts @@ -35,7 +35,7 @@ export default function useEventPublisher() { async function signEvent(ev: NEvent): Promise { if (hasNip07 && !privKey) { - ev.Id = await ev.CreateId(); + ev.Id = ev.CreateId(); const tmpEv = (await barrierNip07(() => window.nostr.signEvent(ev.ToObject()))) as RawEvent; return new NEvent(tmpEv as TaggedRawEvent); } else if (privKey) { diff --git a/packages/nostr/package.json b/packages/nostr/package.json index 46bbf09..cbc9a23 100644 --- a/packages/nostr/package.json +++ b/packages/nostr/package.json @@ -24,6 +24,7 @@ "semi": false }, "dependencies": { + "@noble/hashes": "^1.2.0", "@noble/secp256k1": "^1.7.1", "bech32": "^2.0.0", "events": "^3.3.0", diff --git a/packages/nostr/src/legacy/Event.ts b/packages/nostr/src/legacy/Event.ts index e7493fb..1b3017a 100644 --- a/packages/nostr/src/legacy/Event.ts +++ b/packages/nostr/src/legacy/Event.ts @@ -1,4 +1,5 @@ import * as secp from "@noble/secp256k1"; +import { sha256 } from "@noble/hashes/sha256"; import * as base64 from "@protobufjs/base64"; import { HexKey, RawEvent, TaggedRawEvent } from "./index"; import EventKind from "./EventKind"; @@ -78,7 +79,7 @@ export default class Event { * Sign this message with a private key */ async Sign(key: HexKey) { - this.Id = await this.CreateId(); + this.Id = this.CreateId(); const sig = await secp.schnorr.sign(this.Id, key); this.Signature = secp.utils.bytesToHex(sig); @@ -92,12 +93,12 @@ export default class Event { * @returns True if valid signature */ async Verify() { - const id = await this.CreateId(); + const id = this.CreateId(); const result = await secp.schnorr.verify(this.Signature, id, this.PubKey); return result; } - async CreateId() { + CreateId() { const payload = [ 0, this.PubKey, @@ -107,9 +108,7 @@ export default class Event { this.Content, ]; - const payloadData = new TextEncoder().encode(JSON.stringify(payload)); - const data = await secp.utils.sha256(payloadData); - const hash = secp.utils.bytesToHex(data); + const hash = secp.utils.bytesToHex(sha256(JSON.stringify(payload))); if (this.Id !== "" && hash !== this.Id) { console.debug(payload); throw "ID doesnt match!";