-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the publishPrivateMessage API
- Loading branch information
1 parent
c2bc603
commit 3c788b9
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var pb = require('private-box') | ||
var sodium = require('sodium-universal') | ||
|
||
module.exports = { | ||
box: box, | ||
unbox: unbox | ||
} | ||
|
||
// Data buffer, array of hypercore public key buffers | ||
// Returns ciphertext buffer | ||
function box (data, recipients) { | ||
if (!Array.isArray(recipients)) recipients = [recipients] | ||
recipients = recipients.map(function (key) { | ||
var pkbuf = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) | ||
sodium.crypto_sign_ed25519_pk_to_curve25519(pkbuf, key) | ||
return pkbuf | ||
}) | ||
return pb.encrypt(data, recipients) | ||
} | ||
|
||
// Encrypted data buffer, hypercore secret key | ||
// Returns decrypted buffer, or undefined if not addressed to the key | ||
function unbox (cdata, key) { | ||
var skbuf = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) | ||
sodium.crypto_sign_ed25519_sk_to_curve25519(skbuf, key) | ||
return pb.decrypt(cdata, skbuf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Tests for private messages. | ||
|
||
const Cabal = require('..') | ||
const test = require('tape') | ||
const ram = require('random-access-memory') | ||
const crypto = require('hypercore-crypto') | ||
const {unbox} = require('../lib/crypto') | ||
|
||
test('write a private message & check it\'s not plaintext', function (t) { | ||
t.plan(4) | ||
|
||
const keypair = crypto.keyPair() | ||
|
||
const cabal = Cabal(ram) | ||
cabal.ready(function () { | ||
cabal.publishPrivateMessage('greetings', keypair.publicKey, function (err, cipherMsg) { | ||
t.error(err) | ||
t.same(cipherMsg.type, 'encrypted', 'type is "encrypted"') | ||
t.ok(Buffer.isBuffer(cipherMsg.content), 'content is a buffer') | ||
t.notSame(cipherMsg.content.toString(), 'greetings') | ||
}) | ||
}) | ||
}) | ||
|
||
test('write a private message & manually decrypt', function (t) { | ||
t.plan(5) | ||
|
||
const keypair = crypto.keyPair() | ||
|
||
const cabal = Cabal(ram) | ||
cabal.ready(function () { | ||
cabal.publishPrivateMessage('hello', keypair.publicKey, function (err, cipherMsg) { | ||
t.error(err) | ||
t.same(cipherMsg.type, 'encrypted', 'type is "encrypted"') | ||
|
||
const plaintext = unbox(cipherMsg.content, keypair.secretKey).toString() | ||
try { | ||
const message = JSON.parse(plaintext) | ||
t.same(message.type, 'private/text', 'type is ok') | ||
t.same(typeof message.content, 'object', 'content is set') | ||
t.same(message.content.text, 'hello', 'text is ok') | ||
} catch (err) { | ||
t.error(err) | ||
} | ||
}) | ||
}) | ||
}) |