encryppt trycatch

This commit is contained in:
boufni95 2024-09-09 15:48:03 +00:00
parent 75d2750a8f
commit 776c486d1c
2 changed files with 16 additions and 5 deletions

View file

@ -1,6 +1,6 @@
//import { SimplePool, Sub, Event, UnsignedEvent, getEventHash, signEvent } from 'nostr-tools' //import { SimplePool, Sub, Event, UnsignedEvent, getEventHash, signEvent } from 'nostr-tools'
import { SimplePool, Sub, Event, UnsignedEvent, getEventHash, finishEvent, relayInit } from './tools/index.js' 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 { ERROR, getLogger } from '../helpers/logger.js'
import { encodeNprofile } from '../../custom-nip19.js' import { encodeNprofile } from '../../custom-nip19.js'
const handledEvents: string[] = [] // TODO: - big memory leak here, add TTL const handledEvents: string[] = [] // TODO: - big memory leak here, add TTL
@ -182,7 +182,13 @@ export default class Handler {
const keys = this.GetSendKeys(initiator) const keys = this.GetSendKeys(initiator)
let toSign: UnsignedEvent let toSign: UnsignedEvent
if (data.type === 'content') { 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) const content = encodePayload(decoded)
toSign = { toSign = {
content, content,
@ -194,8 +200,13 @@ export default class Handler {
} else { } else {
toSign = data.event toSign = data.event
if (data.encrypt) { if (data.encrypt) {
try {
const content = await encryptData(data.event.content, getSharedSecret(keys.privateKey, data.encrypt.toPub)) const content = await encryptData(data.event.content, getSharedSecret(keys.privateKey, data.encrypt.toPub))
toSign.content = encodePayload(content) toSign.content = encodePayload(content)
} catch (e: any) {
this.log(ERROR, "failed to encrypt content", e.message)
return
}
} }
if (!toSign.pubkey) { if (!toSign.pubkey) {
toSign.pubkey = keys.publicKey toSign.pubkey = keys.publicKey

View file

@ -3,7 +3,7 @@ import { randomBytes } from "@noble/hashes/utils";
import { streamXOR as xchacha20 } from "@stablelib/xchacha20"; import { streamXOR as xchacha20 } from "@stablelib/xchacha20";
import { secp256k1 } from "@noble/curves/secp256k1"; import { secp256k1 } from "@noble/curves/secp256k1";
import { sha256 } from "@noble/hashes/sha256"; import { sha256 } from "@noble/hashes/sha256";
type EncryptedData = { export type EncryptedData = {
ciphertext: Uint8Array; ciphertext: Uint8Array;
nonce: Uint8Array; nonce: Uint8Array;
} }