From 7e861c879716ba2aeb46a0af5829bf387e83e2de Mon Sep 17 00:00:00 2001 From: meriadec Date: Fri, 11 May 2018 13:54:10 +0200 Subject: [PATCH] Resolve sqlite path for lib ledger core --- src/init-ledger-core.js | 21 +++++++++++++++++++ src/internals/accounts/helpers.js | 4 +++- .../accounts/scanAccountsOnDevice.js | 4 ++-- src/main/bridge.js | 10 ++++++--- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/init-ledger-core.js diff --git a/src/init-ledger-core.js b/src/init-ledger-core.js new file mode 100644 index 00000000..33b9d556 --- /dev/null +++ b/src/init-ledger-core.js @@ -0,0 +1,21 @@ +// Yep. That's a singleton. +// +// Electron needs to tell lib ledger core where to store the sqlite files, when +// instanciating wallet pool, but we don't need to do each everytime we +// require ledger-core, only the first time, so, eh. + +const core = require('ledger-core') + +let instanciated = false + +module.exports = () => { + console.log(`INSTANCIATED = ${instanciated}`) + if (!instanciated) { + core.instanciateWalletPool({ + // sqlite files will be located in the app local data folder + dbPath: process.env.LEDGER_LIVE_SQLITE_PATH, + }) + instanciated = true + } + return core +} diff --git a/src/internals/accounts/helpers.js b/src/internals/accounts/helpers.js index 9cdf67db..88bdff4a 100644 --- a/src/internals/accounts/helpers.js +++ b/src/internals/accounts/helpers.js @@ -31,7 +31,9 @@ export async function getFreshReceiveAddress({ currencyId: string, accountIndex: number, }) { - const core = require('ledger-core') + // TODO: investigate why importing it on file scope causes trouble + const core = require('init-ledger-core')() + const wallet = await core.getWallet(currencyId) const account = await wallet.getAccount(accountIndex) const addresses = await account.getFreshPublicAddresses() diff --git a/src/internals/accounts/scanAccountsOnDevice.js b/src/internals/accounts/scanAccountsOnDevice.js index df7afe86..154adf91 100644 --- a/src/internals/accounts/scanAccountsOnDevice.js +++ b/src/internals/accounts/scanAccountsOnDevice.js @@ -69,7 +69,7 @@ async function scanNextAccount(props) { } = props // TODO: investigate why importing it on file scope causes trouble - const core = require('ledger-core') + const core = require('init-ledger-core')() console.log(`>> Scanning account ${accountIndex}`) @@ -113,7 +113,7 @@ async function scanNextAccount(props) { async function getOrCreateWallet(WALLET_IDENTIFIER, currencyId) { // TODO: investigate why importing it on file scope causes trouble - const core = require('ledger-core') + const core = require('init-ledger-core')() try { const wallet = await core.getWallet(WALLET_IDENTIFIER) return wallet diff --git a/src/main/bridge.js b/src/main/bridge.js index c84da7d7..75f0948f 100644 --- a/src/main/bridge.js +++ b/src/main/bridge.js @@ -2,9 +2,9 @@ import '@babel/polyfill' import { fork } from 'child_process' -import { BrowserWindow, ipcMain } from 'electron' +import { BrowserWindow, ipcMain, app } from 'electron' import objectPath from 'object-path' -import { resolve } from 'path' +import path from 'path' import cpuUsage from 'helpers/cpuUsage' @@ -12,6 +12,9 @@ import setupAutoUpdater, { quitAndInstall } from './autoUpdate' const { DEV_TOOLS } = process.env +// sqlite files will be located in the app local data folder +const LEDGER_LIVE_SQLITE_PATH = path.resolve(app.getPath('userData'), 'sqlite') + const processes = [] function cleanProcesses() { @@ -29,10 +32,11 @@ function onForkChannel(forkType) { return (event: any, payload) => { const { type, data } = payload - let compute = fork(resolve(__dirname, `${__DEV__ ? '../../' : './'}dist/internals`), { + let compute = fork(path.resolve(__dirname, `${__DEV__ ? '../../' : './'}dist/internals`), { env: { DEV_TOOLS, FORK_TYPE: forkType, + LEDGER_LIVE_SQLITE_PATH, }, })