Skip to content

Commit

Permalink
Polish Ledger device derivation switching (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
floating committed Oct 29, 2020
1 parent 5678622 commit f083f57
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions main/signers/ledger/Ledger/index.js
Expand Up @@ -31,24 +31,15 @@ class Ledger extends Signer {
this.handlers = {}
this.lastUse = Date.now()
this.network = store('main.currentNetwork.id')
this.networkObserver = store.observer(() => {
if (this.network !== store('main.currentNetwork.id')) {
this.reset()
this.deviceStatus()
}
})
this.derivation = store('main.ledger.derivation')
this.derivationObserver = store.observer(() => {
if (this.derivation !== store('main.ledger.derivation')) {
this.reset()
this.deviceStatus()
}
})
this.hardwareDerivation = store('main.hardwareDerivation')
this.hardwareDerivationObserver = store.observer(() => {
if (this.hardwareDerivation !== store('main.hardwareDerivation')) {
this.varObserver = store.observer(() => {
if (
this.derivation !== store('main.ledger.derivation') ||
this.hardwareDerivation !== store('main.hardwareDerivation') ||
this.network !== store('main.currentNetwork.id')
) {
this.reset()
this.deviceStatus()
}
})
this.deviceStatus()
Expand Down Expand Up @@ -112,17 +103,17 @@ class Ledger extends Signer {
}

reset () {
this.derivation = store('main.ledger.derivation')
this.pauseLive = true
this.network = store('main.currentNetwork.id')
this.derivation = store('main.ledger.derivation')
this.hardwareDerivation = store('main.hardwareDerivation')
this.status = 'loading'
this.addresses = []
this.update()
this.signers.remove(this.id)
this.scan()
this.deriveAddresses()
}

async getDeviceAddress (i, cb) {
async getDeviceAddress (i, cb = () => {}) {
if (this.pause) return cb(new Error('Device access is paused'))
try {
const { address } = await this.getAddress(this.getPath(i), false, true)
Expand Down Expand Up @@ -189,9 +180,9 @@ class Ledger extends Signer {
if (this._deviceStatus) clearTimeout(this._deviceStatus)
if (this._signMessage) clearTimeout(this._signMessage)
if (this._signTransaction) clearTimeout(this._signTransaction)
if (this._scanTimer) clearTimeout(this._scanTimer)
this.releaseDevice()
this.networkObserver.remove()
this.derivationObserver.remove()
this.varObserver.remove()
store.removeSigner(this.id)
super.close()
}
Expand All @@ -216,6 +207,10 @@ class Ledger extends Signer {
this.deviceStatus()
}
this.status = 'ok'
if (!this.addresses.length) {
this.status = 'loading'
this.deriveAddresses()
}
this.update()
this.deviceStatusActive = false
} catch (err) {
Expand Down Expand Up @@ -362,10 +357,19 @@ class Ledger extends Signer {
}

async _deriveLiveAddresses () {
const addresses = []
let addresses = []
this.status = 'Deriving Live Addresses'
this.liveAddressesFound = 0
for (let i = 0; i < 10; i++) {
if (this.pauseLive) {
this.status = 'loading'
addresses = []
this.liveAddressesFound = 0
this.update()
this.pauseLive = false
this._scanTimer = setTimeout(() => this.scan(), 300)
break
}
const { address } = await this.getAddress(this.getPath(i), false, false)
log.info(`Found Ledger Live address #${i}: ${address}`)
addresses.push(address)
Expand Down

0 comments on commit f083f57

Please sign in to comment.