Skip to content

Commit ecbe0b0

Browse files
committed
refactor(decodeMsg): rewrite the code to ES6+
1 parent 02487cb commit ecbe0b0

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/groups/decodeMsg.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ const keys = require('../helpers/keys');
44

55
module.exports = (msg, senderPublicKey, passPhrase, nonce) => {
66
const keypair = keys.createKeypairFromPassPhrase(passPhrase);
7-
let privateKey = keypair.privateKey;
7+
8+
let {privateKey} = keypair;
9+
810
if (typeof msg === 'string') {
911
msg = hexToBytes(msg);
1012
}
13+
1114
if (typeof nonce === 'string') {
1215
nonce = hexToBytes(nonce);
1316
}
@@ -19,17 +22,19 @@ module.exports = (msg, senderPublicKey, passPhrase, nonce) => {
1922
if (typeof privateKey === 'string') {
2023
privateKey = hexToBytes(privateKey);
2124
}
25+
2226
const DHPublicKey = ed2curve.convertPublicKey(senderPublicKey);
2327
const DHSecretKey = ed2curve.convertSecretKey(privateKey);
2428
const decrypted = nacl.box.open(msg, nonce, DHPublicKey, DHSecretKey);
29+
2530
return decrypted ? utf8ArrayToStr(decrypted) : '';
2631
};
2732

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

3136
for (let c = 0; c < hexString.length; c += 2) {
32-
bytes.push(parseInt(hexString.substr(c, 2), 16));
37+
bytes.push(parseInt(hexString.substring(c, c + 2), 16));
3338
}
3439

3540
return Uint8Array.from(bytes);

0 commit comments

Comments
 (0)