From 594653146699c8030f14c7565d6672eaa9c3823d Mon Sep 17 00:00:00 2001 From: fsteff Date: Fri, 1 Oct 2021 18:30:50 +0200 Subject: [PATCH] bugfix --- index.js | 21 ++++++++++++--------- package.json | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 87089de..429fc6d 100644 --- a/index.js +++ b/index.js @@ -171,8 +171,12 @@ class PubSub extends EventEmitter { this.sub(topic, onData, announce) function onData(data, app, peer) { - const msg = privateMessageOpen(data, publicKey, secretKey) - if(msg) handler(msg, app, peer) + try { + const msg = privateMessageOpen(data, publicKey, secretKey) + if(msg) handler(msg, app, peer) + } catch (err) { + this.emit('err', err) + } } } @@ -190,8 +194,8 @@ function hash(txt) { } function privateMessageSeal(message, recipientPubKey) { - if(!Buffer.isBuffer(message)) this.emit('error', new Error('private message has to be a Buffer')) - if(!Buffer.isBuffer(recipientPubKey) || recipientPubKey.length !== sodium.crypto_box_PUBLICKEYBYTES) this.emit('invalid public key') + if(!Buffer.isBuffer(message)) throw new Error('private message has to be a Buffer') + if(!Buffer.isBuffer(recipientPubKey) || recipientPubKey.length !== sodium.crypto_box_PUBLICKEYBYTES) throw new Error('invalid public key') const ciphertext = Buffer.alloc(message.length + sodium.crypto_box_SEALBYTES) sodium.crypto_box_seal(ciphertext, message, recipientPubKey) @@ -199,14 +203,13 @@ function privateMessageSeal(message, recipientPubKey) { } function privateMessageOpen(ciphertext, publicKey, secretKey) { - if(!Buffer.isBuffer(ciphertext) || ciphertext.length <= sodium.crypto_box_SEALBYTES) this.emit('error', new Error('invalid ciphertext')) - if(!Buffer.isBuffer(publicKey) || publicKey.length !== sodium.crypto_box_PUBLICKEYBYTES) this.emit('invalid public key') - if(!Buffer.isBuffer(secretKey) || secretKey.length !== sodium.crypto_box_SECRETKEYBYTES) this.emit('invalid secret key') + if(!Buffer.isBuffer(ciphertext) || ciphertext.length <= sodium.crypto_box_SEALBYTES) throw new Error('invalid ciphertext') + if(!Buffer.isBuffer(publicKey) || publicKey.length !== sodium.crypto_box_PUBLICKEYBYTES) throw new Error('invalid public key') + if(!Buffer.isBuffer(secretKey) || secretKey.length !== sodium.crypto_box_SECRETKEYBYTES) throw new Error('invalid secret key') const message = Buffer.alloc(ciphertext.length - sodium.crypto_box_SEALBYTES) if(!sodium.crypto_box_seal_open(message, ciphertext, publicKey, secretKey)) { - this.emit('error', 'failed to open sealed box - corrupted or not intended for this receipient') - return null + throw new Error('failed to open sealed box - corrupted or not intended for this receipient') } return message } diff --git a/package.json b/package.json index 3164241..222ecc1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hyperpubsub", - "version": "1.2.2", + "version": "1.2.3", "description": "Hypercore Protocol Extension for simple PubSub", "main": "index.js", "scripts": {