diff --git a/src/services/nostr/handler.ts b/src/services/nostr/handler.ts index 7360d869..4b8c063f 100644 --- a/src/services/nostr/handler.ts +++ b/src/services/nostr/handler.ts @@ -1,6 +1,6 @@ //import { SimplePool, Sub, Event, UnsignedEvent, getEventHash, signEvent } from 'nostr-tools' import { SimplePool, Sub, Event, UnsignedEvent, getEventHash, finishEvent, relayInit } from './tools/index.js' -import { encryptData, decryptData, getSharedSecret, decodePayload, encodePayload } from './nip44.js' +import { encryptData, decryptData, getSharedSecret, decodePayload, encodePayload, EncryptedData } from './nip44.js' import { ERROR, getLogger } from '../helpers/logger.js' import { encodeNprofile } from '../../custom-nip19.js' const handledEvents: string[] = [] // TODO: - big memory leak here, add TTL @@ -182,7 +182,13 @@ export default class Handler { const keys = this.GetSendKeys(initiator) let toSign: UnsignedEvent if (data.type === 'content') { - const decoded = await encryptData(data.content, getSharedSecret(keys.privateKey, data.pub)) + let decoded: EncryptedData + try { + decoded = await encryptData(data.content, getSharedSecret(keys.privateKey, data.pub)) + } catch (e: any) { + this.log(ERROR, "failed to encrypt content", e.message) + return + } const content = encodePayload(decoded) toSign = { content, @@ -194,8 +200,13 @@ export default class Handler { } else { toSign = data.event if (data.encrypt) { - const content = await encryptData(data.event.content, getSharedSecret(keys.privateKey, data.encrypt.toPub)) - toSign.content = encodePayload(content) + try { + const content = await encryptData(data.event.content, getSharedSecret(keys.privateKey, data.encrypt.toPub)) + toSign.content = encodePayload(content) + } catch (e: any) { + this.log(ERROR, "failed to encrypt content", e.message) + return + } } if (!toSign.pubkey) { toSign.pubkey = keys.publicKey diff --git a/src/services/nostr/nip44.ts b/src/services/nostr/nip44.ts index 7741f3a7..4bf1d596 100644 --- a/src/services/nostr/nip44.ts +++ b/src/services/nostr/nip44.ts @@ -3,7 +3,7 @@ import { randomBytes } from "@noble/hashes/utils"; import { streamXOR as xchacha20 } from "@stablelib/xchacha20"; import { secp256k1 } from "@noble/curves/secp256k1"; import { sha256 } from "@noble/hashes/sha256"; -type EncryptedData = { +export type EncryptedData = { ciphertext: Uint8Array; nonce: Uint8Array; }