From e6c8865ecac54d3dd387b7a209118ff8ee195357 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 12 Aug 2022 14:12:40 +0300 Subject: [PATCH] feat: decrypt data from HWW --- .../components/serial-signer/serial-signer.js | 147 +++++++----------- 1 file changed, 58 insertions(+), 89 deletions(-) diff --git a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js index 7966eea3a..8dadb1945 100644 --- a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js +++ b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js @@ -15,7 +15,7 @@ async function serialSigner(path) { receivedData: '', config: {}, decryptionKey: null, - dheKey: null, // todo: store in secure local storage + sharedSecret: null, // todo: store in secure local storage hww: { password: null, @@ -202,55 +202,8 @@ async function serialSigner(path) { } }, handleSerialPortResponse: async function (value) { - const command = value.split(' ')[0] - const commandData = value.substring(command.length).trim() - - if (command === COMMAND_DH_EXCHANGE) { - this.handleDhExchangeResponse(commandData) - return - } - switch (command) { - case COMMAND_DH_EXCHANGE: - this.handleDhExchangeResponse(commandData) - return - case COMMAND_LOG: - - console.log(` %c${commandData}`, 'background: #222; color: #bada55') - - return - } - if (this.dheKey) { - try { - console.log('### value', value) - const messageBin = nobleSecp256k1.utils.hexToBytes(value) - try { - const xx = await decryptMessage(this.dheKey, messageBin) - console.log('### xx: ', xx) - } catch (error) { - console.log('### error 1', error) - } - - try { - const decrypted1 = await decryptMessage2( - this.sharedSecret, - messageBin - ) - - console.log( - '### decrypted hex 1: ', - nobleSecp256k1.utils.bytesToHex(decrypted1) - ) - console.log('### decrypted text 1', new TextDecoder().decode(decrypted1)) - - var decryptedText = aesjs.utils.utf8.fromBytes(decrypted1); - console.log('### decrypted text utf8 1',decryptedText); - } catch (error) { - console.log('### error 2', error) - } - } catch (error) { - console.log(error) - } - } + console.log('### extract value', value) + const {command, commandData} = await this.extractCommand(value) switch (command) { case COMMAND_SIGN_PSBT: @@ -274,8 +227,17 @@ async function serialSigner(path) { case COMMAND_SEED: this.handleShowSeedResponse(commandData) break + case COMMAND_DH_EXCHANGE: + this.handleDhExchangeResponse(commandData) + break + case COMMAND_LOG: + console.log( + ` %c${commandData}`, + 'background: #222; color: #bada55' + ) + break default: - console.log('### hww console', value) + console.log(` %c${value}`, 'background: #222; color: red') } }, updateSerialPortConsole: function (value) { @@ -724,48 +686,55 @@ async function serialSigner(path) { const message = [command].concat(attrs).join(' ') const encodedMessage = asciiToUint8Array(message.length + ' ' + message) - // const encodedMessageHex = - // '6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710' - // const encodedMessage = nobleSecp256k1.utils.hexToBytes( - // encodedMessageHex - // ) const encrypted = await encryptMessage(this.dheKey, encodedMessage) const encryptedHex = nobleSecp256k1.utils.bytesToHex(encrypted) - console.log('### encrypted hex: ', encryptedHex) - - /* - - const decrypted = await decryptMessage(this.dheKey, encrypted) - console.log( - '### decrypted hex 0: ', - nobleSecp256k1.utils.bytesToHex(new Uint8Array(decrypted)) - ) - const encrypted1 = - '8f13a7763f021d7701f4100631f6c3d80576fcd0e3718b2594ceb7b910ceed29a334d1019dd6f0ffdba5b6be8c11637d6124d7adbd29c88af13800cb1f980f7d' - const encrypted1Bin = nobleSecp256k1.utils.hexToBytes(encrypted1) - - const decrypted1 = await decryptMessage2( - this.sharedSecret, - encrypted1Bin - ) - console.log( - '### decrypted hex 1: ', - nobleSecp256k1.utils.bytesToHex(decrypted1) - ) - - const encrypted2 = - '1e785d2d8cdf930aae61e8f3dbc5637917c1a621f4cdc6857cabbcdf696108d6b831cddce4c9f759e8b8eee2419f4a8f2133bb8893ae8bd251597439a6f4f7b172bd2c9eead0877cbf4cb996ad343330' - const encrypted2Bin = nobleSecp256k1.utils.hexToBytes(encrypted2) - - const decrypted2 = await decryptMessage(this.dheKey, encrypted2Bin) - console.log( - '### decrypted hex 2: ', - nobleSecp256k1.utils.bytesToHex(new Uint8Array(decrypted2)) - ) - */ - await this.writer.write(encryptedHex + '\n') + }, + extractCommand: async function (value) { + const command = value.split(' ')[0] + const commandData = value.substring(command.length).trim() + + if (command === COMMAND_DH_EXCHANGE || command === COMMAND_LOG) + return {command, commandData} + + const decryptedValue = await this.decryptData(value) + const decryptedCommand = decryptedValue.split(' ')[0] + const decryptedCommandData = decryptedValue.substring(decryptedCommand.length).trim() + return { + command: decryptedCommand, + commandData: decryptedCommandData + } + }, + decryptData: async function (value) { + if (!this.sharedSecret) { + this.$q.notify({ + type: 'warning', + message: 'Secure session not established!', + timeout: 10000 + }) + return '/error Secure session not established!' + } + try { + const messageBytes = nobleSecp256k1.utils.hexToBytes(value) + const decrypted1 = await decryptMessage2( + this.sharedSecret, + messageBytes + ) + const data = new TextDecoder().decode(decrypted1) + const [len] = data.split(' ') + const command = data.substring(len.length +1, +len + len.length + 1).trim() + console.log('### decryptData ', data, command) + return command + } catch (error) { + this.$q.notify({ + type: 'warning', + message: 'Failed to decrypt message from device!', + caption: `${error}`, + timeout: 10000 + }) + return '/error Failed to decrypt message from device!' + } } }, created: async function () {