Browse Source

refactor(lnd): refactor lnd/config

Refactor lnd/config for better readability.
renovate/lint-staged-8.x
Tom Kirkpatrick 7 years ago
parent
commit
02d0a5564d
No known key found for this signature in database GPG Key ID: 72203A8EC5967EA8
  1. 69
      app/lnd/config/index.js

69
app/lnd/config/index.js

@ -1,8 +1,3 @@
// Cert will be tlsCertPathated depending on your machine:
//
// Mac OS X: /Users/.../Library/Application Support/Lnd/tls.cert
// Linux: ~/.lnd/tls.cert
// Windows: C:\Users\...\AppData\Local\Lnd
import { homedir, platform } from 'os'
import { dirname, join, normalize } from 'path'
import Store from 'electron-store'
@ -54,36 +49,44 @@ if (isDev) {
lndPath = join(appRootPath, 'bin', lndBin)
}
export default {
lnd: () => {
// Get an electromn store named 'connection' in which the saved connection detailes are stored.
const store = new Store({ name: 'connection' })
// Determine what hostname to use.
let host = store.get('host')
if (typeof host === 'undefined') {
host = 'localhost:10009'
}
// Determine what cert to use.
let cert = store.get('cert')
if (typeof cert === 'undefined') {
cert = join(lndDataDir, 'tls.cert')
}
/**
* Get current lnd configuration.
*
* Cert and Macaroon will be at one of the following destinations depending on your machine:
* Mac OS X: ~/Library/Application Support/Lnd/tls.cert
* Linux: ~/.lnd/tls.cert
* Windows: C:\Users\...\AppData\Local\Lnd
*
* @return {object} current lnd configuration options.
*/
const lnd = () => {
// Get an electron store named 'connection' in which the saved connection detailes are stored.
const store = new Store({ name: 'connection' })
// Determine what macaroon to use.
let macaroon = store.get('macaroon')
if (typeof macaroon === 'undefined') {
macaroon = join(lndDataDir, 'admin.macaroon')
/**
* Fetch a config option from the connection store.
* if undefined fallback to a path relative to the lnd data dir.
*
* @param {string} name name of property to fetch from the store.
* @param {string} path path relative to the lnd data dir.
* @return {string} config param or filepath relative to the lnd data dir.
*/
const getFromStoreOrDataDir = (name, file) => {
let path = store.get(name)
if (typeof path === 'undefined') {
path = join(lndDataDir, file)
}
return untildify(path)
}
return {
lndPath,
configPath: join(appRootPath, 'resources', 'lnd.conf'),
rpcProtoPath: join(appRootPath, 'resources', 'rpc.proto'),
host,
cert: untildify(cert),
macaroon: untildify(macaroon)
}
return {
lndPath,
configPath: join(appRootPath, 'resources', 'lnd.conf'),
rpcProtoPath: join(appRootPath, 'resources', 'rpc.proto'),
host: store.get('host', 'localhost:10009'),
cert: getFromStoreOrDataDir('cert', 'tls.cert'),
macaroon: getFromStoreOrDataDir('macaroon', 'admin.macaroon')
}
}
export default { lnd }

Loading…
Cancel
Save