aboutsummaryrefslogtreecommitdiff
path: root/lib/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/app.js')
-rw-r--r--lib/app.js25
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/app.js b/lib/app.js
index 6bfa00a..2ded278 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -7,6 +7,7 @@ var pull = require('pull-stream')
var multicb = require('multicb')
var paramap = require('pull-paramap')
var Contacts = require('ssb-contact')
+var PrivateBox = require('private-box')
var About = require('./about')
var Follows = require('./follows')
var Serve = require('./serve')
@@ -104,11 +105,27 @@ var logPrefix = '[' + pkg.name + ']'
App.prototype.log = console.log.bind(console, logPrefix)
App.prototype.error = console.error.bind(console, logPrefix)
-App.prototype.unboxMsg = function (msg, cb) {
+App.prototype.unboxContentWithKey = function (content, key, cb) {
+ if (!key) return this.unboxContent(content, cb)
+ var data
+ try {
+ var contentBuf = new Buffer(content.replace(/\.box.*$/, ''), 'base64')
+ var keyBuf = new Buffer(key, 'base64')
+ console.error(key, keyBuf.length)
+ data = PrivateBox.multibox_open_body(contentBuf, keyBuf)
+ if (!data) return cb(new Error('failed to decrypt'))
+ data = JSON.parse(data.toString('utf8'))
+ } catch(e) {
+ return cb(new Error(e.stack || e))
+ }
+ cb(null, data)
+}
+
+App.prototype.unboxMsgWithKey = function (msg, key, cb) {
var self = this
var c = msg && msg.value && msg.value.content
if (typeof c !== 'string') cb(null, msg)
- else self.unboxContent(c, function (err, content) {
+ else self.unboxContentWithKey(c, key, function (err, content) {
if (err) {
self.error('unbox:', err)
return cb(null, msg)
@@ -125,6 +142,10 @@ App.prototype.unboxMsg = function (msg, cb) {
})
}
+App.prototype.unboxMsg = function (msg, cb) {
+ return this.unboxMsgWithKey(msg, null, cb)
+}
+
App.prototype.search = function (opts) {
var fsearch = this.sbot.fulltext && this.sbot.fulltext.search
if (fsearch) return fsearch(opts)