<commit_before>/**
 * This module executes inside of electron's main process. You can start
 * electron renderer process from here and communicate with the other processes
 * through IPC.
 *
 * When running `npm run build` or `npm run build-main`, this file is compiled to
 * `./app/dist/main.prod.js` using webpack. This gives us some performance wins.
 */
import { app, BrowserWindow, session } from 'electron'
import installExtension, {
  REACT_DEVELOPER_TOOLS,
  REDUX_DEVTOOLS
} from 'electron-devtools-installer'
import get from 'lodash.get'
import path from 'path'
import url from 'url'
import os from 'os'
import fs from 'fs'
import querystring from 'querystring'
import { mainLog } from './lib/utils/log'
import ZapMenuBuilder from './lib/zap/menuBuilder'
import ZapController from './lib/zap/controller'
import ZapUpdater from './lib/zap/updater'
import themes from './themes'
import { getDbName } from './store/db'

// Set the Electron userDir to a temporary directory if the ELECTRON_USER_DIR_TEMP env var is set.
// This provides an easy way to run the app with a completely fresh environment, useful for e2e tests.
if (process.env.ELECTRON_USER_DIR_TEMP) {
  const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'zap-'))
  mainLog.info('Using temporary directory %s for userData', folder)
  app.setPath('userData', folder)
}

// By default, run the app in development mode.
if (!process.env.NODE_ENV) {
  process.env.NODE_ENV = 'development'
}

// Set up references to application helpers and controllers.
let zap
let updater
let menuBuilder
let mainWindow
let protocolUrl

// Set up a couple of timers to track the app startup progress.
mainLog.time('Time until app is ready')

/**
 * Handler for open-link events.
 */
const handleOpenUrl = (urlToOpen = '') => {
  // If we already have the mainWindow, handle the link right away.
  if (mainWindow) {
    mainLog.debug('handleOpenUrl: %s', urlToOpen)
    const type = urlToOpen.split(':')[0]

    switch (type) {
      case 'lightning':
        handleLightningLink(urlToOpen)
        break

      case 'lndconnect':
        handleLndconnectLink(urlToOpen)
    }
  }

  // Otherwise, defer until the winow content has fully loaded.
  // See mainWindow.webContents.on('did-finish-load') below.
  else {
    protocolUrl = urlToOpen
  }
}

/**
 * Handler for lightning: links
 */
const handleLightningLink = input => {
  const payReq = input.split(':')[1]
  zap.sendMessage('lightningPaymentUri', { payReq })
  mainWindow.show()
}

/**
 * Handler for lndconnect: links
 */
const handleLndconnectLink = input => {
  const parsedUrl = url.parse(input)
  const { host, cert, macaroon } = querystring.parse(parsedUrl.query)
  zap.sendMessage('lndconnectUri', { host, cert, macaroon })
  mainWindow.show()
}

/**
 * Fetch user settings from indexedDb.
 * We do this by starting up a new browser window and accessing indexedDb from within it.
 */
const fetchSettings = () => {
  const win = new BrowserWindow({
    show: false,
    focusable: false,
    webPreferences: {<commit_msg>fix(wallet): support updated lndconnect link format<commit_after>/**
 * This module executes inside of electron's main process. You can start
 * electron renderer process from here and communicate with the other processes
 * through IPC.
 *
 * When running `npm run build` or `npm run build-main`, this file is compiled to
 * `./app/dist/main.prod.js` using webpack. This gives us some performance wins.
 */
import { app, BrowserWindow, session } from 'electron'
import installExtension, {
  REACT_DEVELOPER_TOOLS,
  REDUX_DEVTOOLS
} from 'electron-devtools-installer'
import get from 'lodash.get'
import path from 'path'
import url from 'url'
import os from 'os'
import fs from 'fs'
import querystring from 'querystring'
import { mainLog } from './lib/utils/log'
import ZapMenuBuilder from './lib/zap/menuBuilder'
import ZapController from './lib/zap/controller'
import ZapUpdater from './lib/zap/updater'
import themes from './themes'
import { getDbName } from './store/db'

// Set the Electron userDir to a temporary directory if the ELECTRON_USER_DIR_TEMP env var is set.
// This provides an easy way to run the app with a completely fresh environment, useful for e2e tests.
if (process.env.ELECTRON_USER_DIR_TEMP) {
  const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'zap-'))
  mainLog.info('Using temporary directory %s for userData', folder)
  app.setPath('userData', folder)
}

// By default, run the app in development mode.
if (!process.env.NODE_ENV) {
  process.env.NODE_ENV = 'development'
}

// Set up references to application helpers and controllers.
let zap
let updater
let menuBuilder
let mainWindow
let protocolUrl

// Set up a couple of timers to track the app startup progress.
mainLog.time('Time until app is ready')

/**
 * Handler for open-link events.
 */
const handleOpenUrl = (urlToOpen = '') => {
  // If we already have the mainWindow, handle the link right away.
  if (mainWindow) {
    mainLog.debug('handleOpenUrl: %s', urlToOpen)
    const type = urlToOpen.split(':')[0]

    switch (type) {
      case 'lightning':
        handleLightningLink(urlToOpen)
        break

      case 'lndconnect':
        handleLndconnectLink(urlToOpen)
    }
  }

  // Otherwise, defer until the winow content has fully loaded.
  // See mainWindow.webContents.on('did-finish-load') below.
  else {
    protocolUrl = urlToOpen
  }
}

/**
 * Handler for lightning: links
 */
const handleLightningLink = input => {
  const payReq = input.split(':')[1]
  zap.sendMessage('lightningPaymentUri', { payReq })
  mainWindow.show()
}

/**
 * Handler for lndconnect: links
 */
const handleLndconnectLink = input => {
  const parsedUrl = url.parse(input)
  const { host, cert, macaroon } = querystring.parse(parsedUrl.query)
  zap.sendMessage('lndconnectUri', { host: host || parsedUrl.host, cert, macaroon })
  mainWindow.show()
}

/**
 * Fetch user settings from indexedDb.
 * We do this by starting up a new browser window and accessing indexedDb from within it.
 */
const fetchSettings = () => {
  const win = new BrowserWindow({
    show: false,
    focusable: false,
    webPreferences: {
{'fraction_non_alphanumeric': 0.10964483545128706, 'fraction_numerical': 0.0009775171065493646, 'mean_word_length': 3.811128526645768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4278300', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1872, 'n_words': 643}