Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteff committed Oct 1, 2021
1 parent b975c98 commit 5946531
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -190,23 +194,22 @@ 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)
return ciphertext
}

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
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 5946531

Please sign in to comment.