From 218ee77acdcf153fb10330cb9c43d921280d5aad Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 18 Aug 2022 18:57:20 +0300 Subject: [PATCH] feat: handle forced logout --- .../components/serial-signer/serial-signer.js | 38 +++++++++++++++---- .../extensions/watchonly/static/js/utils.js | 1 + 2 files changed, 32 insertions(+), 7 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 57ab6ada8..41dc81ec2 100644 --- a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js +++ b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js @@ -426,14 +426,15 @@ async function serialSigner(path) { } }, handleLogoutResponse: function (res = '') { - this.hww.authenticated = !(res.trim() === '1') - if (this.hww.authenticated) { + const authenticated = !(res.trim() === '1') + if (this.hww.authenticated && !authenticated) { this.$q.notify({ - type: 'warning', - message: 'Failed to logout from Hardware Wallet', + type: 'positive', + message: 'Logged Out', timeout: 10000 }) } + this.hww.authenticated = authenticated }, hwwSendPsbt: async function (psbtBase64, tx) { try { @@ -523,13 +524,12 @@ async function serialSigner(path) { }) }, hwwCheckPairing: async function () { - const testString = 'lnbits' const iv = window.crypto.getRandomValues(new Uint8Array(16)) console.log('### this.sharedSecret', this.sharedSecret) const encrypted = await this.encryptMessage( this.sharedSecret, iv, - testString.length + ' ' + testString + PAIRING_CONTROL_TEXT.length + ' ' + PAIRING_CONTROL_TEXT ) const encryptedHex = nobleSecp256k1.utils.bytesToHex(encrypted) @@ -548,7 +548,31 @@ async function serialSigner(path) { } }, handleCheckPairingResponse: async function (res = '') { - console.log('### handleCheckPairingResponse', res) + const [statusCode, encryptedMessage] = res.split(' ') + switch (statusCode) { + case '0': + const controlText = await this.decryptData(encryptedMessage) + if (controlText == PAIRING_CONTROL_TEXT) { + this.$q.notify({ + type: 'positive', + message: 'Re-paired with success!', + timeout: 10000 + }) + } else { + this.$q.notify({ + type: 'warning', + message: 'Re-pairing failed!', + caption: 'Remove (forget) device and try again!', + timeout: 10000 + }) + } + break + default: + // noting to do here yet + break + } + + }, hwwPair: async function () { try { diff --git a/lnbits/extensions/watchonly/static/js/utils.js b/lnbits/extensions/watchonly/static/js/utils.js index 2fc5eeaea..d21534066 100644 --- a/lnbits/extensions/watchonly/static/js/utils.js +++ b/lnbits/extensions/watchonly/static/js/utils.js @@ -17,6 +17,7 @@ const COMMAND_LOG = '/log' const COMMAND_CHECK_PAIRING = '/check-pairing' const DEFAULT_RECEIVE_GAP_LIMIT = 20 +const PAIRING_CONTROL_TEXT = 'lnbits' const blockTimeToDate = blockTime => blockTime ? moment(blockTime * 1000).format('LLL') : ''