Skip to content

Commit

Permalink
refactor(decodeMsg): rewrite the code to ES6+
Browse files Browse the repository at this point in the history
  • Loading branch information
martiliones committed Mar 31, 2022
1 parent 02487cb commit ecbe0b0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/groups/decodeMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ const keys = require('../helpers/keys');

module.exports = (msg, senderPublicKey, passPhrase, nonce) => {
const keypair = keys.createKeypairFromPassPhrase(passPhrase);
let privateKey = keypair.privateKey;

let {privateKey} = keypair;

if (typeof msg === 'string') {
msg = hexToBytes(msg);
}

if (typeof nonce === 'string') {
nonce = hexToBytes(nonce);
}
Expand All @@ -19,17 +22,19 @@ module.exports = (msg, senderPublicKey, passPhrase, nonce) => {
if (typeof privateKey === 'string') {
privateKey = hexToBytes(privateKey);
}

const DHPublicKey = ed2curve.convertPublicKey(senderPublicKey);
const DHSecretKey = ed2curve.convertSecretKey(privateKey);
const decrypted = nacl.box.open(msg, nonce, DHPublicKey, DHSecretKey);

return decrypted ? utf8ArrayToStr(decrypted) : '';
};

function hexToBytes(hexString = '') {
const bytes = [];

for (let c = 0; c < hexString.length; c += 2) {
bytes.push(parseInt(hexString.substr(c, 2), 16));
bytes.push(parseInt(hexString.substring(c, c + 2), 16));
}

return Uint8Array.from(bytes);
Expand Down

0 comments on commit ecbe0b0

Please sign in to comment.