mirror of
https://github.com/lnbits/lnbits.git
synced 2025-07-05 04:54:39 +02:00
fix: wait for connection to init
This commit is contained in:
@ -15,7 +15,7 @@ async function serialSigner(path) {
|
|||||||
receivedData: '',
|
receivedData: '',
|
||||||
config: {},
|
config: {},
|
||||||
decryptionKey: null,
|
decryptionKey: null,
|
||||||
sharedSecret: null, // todo: store in secure local storage
|
sharedSecret: null,
|
||||||
|
|
||||||
hww: {
|
hww: {
|
||||||
password: null,
|
password: null,
|
||||||
@ -57,13 +57,16 @@ async function serialSigner(path) {
|
|||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
pairedDevices: {
|
pairedDevices: {
|
||||||
|
cache: false,
|
||||||
get: function () {
|
get: function () {
|
||||||
|
console.log('### get pairedDevices')
|
||||||
return (
|
return (
|
||||||
JSON.parse(window.localStorage.getItem('lnbits-paired-devices')) ||
|
JSON.parse(window.localStorage.getItem('lnbits-paired-devices')) ||
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
set: function (devices) {
|
set: function (devices) {
|
||||||
|
console.log('### set pairedDevices', devices)
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
'lnbits-paired-devices',
|
'lnbits-paired-devices',
|
||||||
JSON.stringify(devices)
|
JSON.stringify(devices)
|
||||||
@ -81,6 +84,7 @@ async function serialSigner(path) {
|
|||||||
await this.openSerialPort(this.config)
|
await this.openSerialPort(this.config)
|
||||||
},
|
},
|
||||||
openSerialPort: async function (config = {baudRate: 9600}) {
|
openSerialPort: async function (config = {baudRate: 9600}) {
|
||||||
|
console.log('### openSerialPort', config)
|
||||||
if (!this.checkSerialPortSupported()) return false
|
if (!this.checkSerialPortSupported()) return false
|
||||||
if (this.selectedPort) {
|
if (this.selectedPort) {
|
||||||
this.$q.notify({
|
this.$q.notify({
|
||||||
@ -109,7 +113,10 @@ async function serialSigner(path) {
|
|||||||
|
|
||||||
// Wait for the serial port to open.
|
// Wait for the serial port to open.
|
||||||
await this.selectedPort.open(config)
|
await this.selectedPort.open(config)
|
||||||
|
// do not await
|
||||||
this.startSerialPortReading()
|
this.startSerialPortReading()
|
||||||
|
// wait to init
|
||||||
|
sleep(1000)
|
||||||
|
|
||||||
const textEncoder = new TextEncoderStream()
|
const textEncoder = new TextEncoderStream()
|
||||||
this.writableStreamClosed = textEncoder.readable.pipeTo(
|
this.writableStreamClosed = textEncoder.readable.pipeTo(
|
||||||
@ -307,7 +314,11 @@ async function serialSigner(path) {
|
|||||||
},
|
},
|
||||||
hwwPing: async function () {
|
hwwPing: async function () {
|
||||||
try {
|
try {
|
||||||
|
console.log('### hwwPing 1', window.location.host)
|
||||||
|
// Send an empty ping. The serial port buffer might have some jubk data. Flush it.
|
||||||
|
await this.sendCommandClearText(COMMAND_PING)
|
||||||
await this.sendCommandClearText(COMMAND_PING, [window.location.host])
|
await this.sendCommandClearText(COMMAND_PING, [window.location.host])
|
||||||
|
console.log('### hwwPing 2')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.$q.notify({
|
this.$q.notify({
|
||||||
type: 'warning',
|
type: 'warning',
|
||||||
@ -582,7 +593,7 @@ async function serialSigner(path) {
|
|||||||
hwwCheckPairing: async function () {
|
hwwCheckPairing: async function () {
|
||||||
const iv = window.crypto.getRandomValues(new Uint8Array(16))
|
const iv = window.crypto.getRandomValues(new Uint8Array(16))
|
||||||
const encrypted = await this.encryptMessage(
|
const encrypted = await this.encryptMessage(
|
||||||
this.sharedSecret,
|
this.sharedSecret, // todo: revisit
|
||||||
iv,
|
iv,
|
||||||
PAIRING_CONTROL_TEXT.length + ' ' + PAIRING_CONTROL_TEXT
|
PAIRING_CONTROL_TEXT.length + ' ' + PAIRING_CONTROL_TEXT
|
||||||
)
|
)
|
||||||
@ -724,6 +735,7 @@ async function serialSigner(path) {
|
|||||||
},
|
},
|
||||||
hwwHelp: async function () {
|
hwwHelp: async function () {
|
||||||
try {
|
try {
|
||||||
|
console.log('### cmd help')
|
||||||
await this.sendCommandSecure(COMMAND_HELP)
|
await this.sendCommandSecure(COMMAND_HELP)
|
||||||
this.$q.notify({
|
this.$q.notify({
|
||||||
type: 'positive',
|
type: 'positive',
|
||||||
@ -746,7 +758,7 @@ async function serialSigner(path) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.$q.notify({
|
this.$q.notify({
|
||||||
type: 'warning',
|
type: 'warning',
|
||||||
message: 'Failed to ask for help!',
|
message: 'Failed to wipe!',
|
||||||
caption: `${error}`,
|
caption: `${error}`,
|
||||||
timeout: 10000
|
timeout: 10000
|
||||||
})
|
})
|
||||||
@ -861,7 +873,13 @@ async function serialSigner(path) {
|
|||||||
|
|
||||||
sendCommandSecure: async function (command, attrs = []) {
|
sendCommandSecure: async function (command, attrs = []) {
|
||||||
const message = [command].concat(attrs).join(' ')
|
const message = [command].concat(attrs).join(' ')
|
||||||
|
console.log('### sendCommandSecure', message)
|
||||||
const iv = window.crypto.getRandomValues(new Uint8Array(16))
|
const iv = window.crypto.getRandomValues(new Uint8Array(16))
|
||||||
|
if (!this.sharedSecret || !this.sharedSecret.length) {
|
||||||
|
throw new Error(
|
||||||
|
`Secure connection not estabileshed. Tried to run command: ${command}`
|
||||||
|
)
|
||||||
|
}
|
||||||
const encrypted = await this.encryptMessage(
|
const encrypted = await this.encryptMessage(
|
||||||
this.sharedSecret,
|
this.sharedSecret,
|
||||||
iv,
|
iv,
|
||||||
@ -874,6 +892,7 @@ async function serialSigner(path) {
|
|||||||
},
|
},
|
||||||
sendCommandClearText: async function (command, attrs = []) {
|
sendCommandClearText: async function (command, attrs = []) {
|
||||||
const message = [command].concat(attrs).join(' ')
|
const message = [command].concat(attrs).join(' ')
|
||||||
|
console.log('### sendCommandClearText', message)
|
||||||
await this.writer.write(message + '\n')
|
await this.writer.write(message + '\n')
|
||||||
},
|
},
|
||||||
extractCommand: async function (value) {
|
extractCommand: async function (value) {
|
||||||
|
Reference in New Issue
Block a user