diff --git a/errors/index.js b/errors/index.js index 7288d64..2fd06f8 100644 --- a/errors/index.js +++ b/errors/index.js @@ -26,7 +26,7 @@ const parseAxiosError = error => { message || dataError.message || 'no message provided in the response, check the server logs' const {method, url} = basic const humanReadableMessage = - `${status} <-- ${method} ${url} || ${finalMessage}` + `${status} ${method} ${url} :: ${finalMessage}` // console.log({humanReadableMessage}) return { status, diff --git a/lnd/index.js b/lnd/index.js index 8c11666..734f032 100644 --- a/lnd/index.js +++ b/lnd/index.js @@ -2,4 +2,5 @@ module.exports = { lndClients: require('./lnd-clients'), lndStreams: require('./lnd-streams'), lndPolarConfig: require('./lnd-polar-config'), + lndEnvConfig: require('./lnd-env-config'), } diff --git a/lnd/lnd-env-config.js b/lnd/lnd-env-config.js new file mode 100644 index 0000000..7fb2b8d --- /dev/null +++ b/lnd/lnd-env-config.js @@ -0,0 +1,15 @@ +const {curry} = require('../fusto') + +module.exports = curry((future, { + host = '127.0.0.1', + port = 8080, + // username, + macaroonType = 'readonly', +}) => { + // const macaroonPath = `${basePath}/${username}/data/chain/bitcoin/regtest/admin.macaroon` + const baseUrl = `${host}:${port}` + // const macaroon = process.env[`LND_${macaroonType.toUpperCase()}_MACAROON`] + const macaroon = process.env.LND_INVOICE_MACAROON + const cert = process.env.LND_TLS_CERT + return {macaroon, cert, baseUrl} +}) diff --git a/lnd/lnd-polar-config.js b/lnd/lnd-polar-config.js index 291b6fd..2a1aa7b 100644 --- a/lnd/lnd-polar-config.js +++ b/lnd/lnd-polar-config.js @@ -10,9 +10,11 @@ module.exports = curry((polarNetwork, { host = '127.0.0.1', port = 8080, username, + macaroonType = 'readonly', }) => { const basePath = polarBasePath(polarNetwork) - const macaroonPath = `${basePath}/${username}/data/chain/bitcoin/regtest/admin.macaroon` + // const macaroonPath = `${basePath}/${username}/data/chain/bitcoin/regtest/admin.macaroon` + const macaroonPath = `${basePath}/${username}/data/chain/bitcoin/regtest/${macaroonType}.macaroon` const tlsPath = `${basePath}/${username}/tls.cert` const baseUrl = `${host}:${port}` const macaroon = readFileSync(macaroonPath).toString('hex') diff --git a/tests/lnd/lnd-env-config.test.js b/tests/lnd/lnd-env-config.test.js new file mode 100644 index 0000000..b2be977 --- /dev/null +++ b/tests/lnd/lnd-env-config.test.js @@ -0,0 +1,20 @@ +// const assert = require('assert') +// const {test} = require('node:test') + +const {lndEnvConfig} = require('../../lnd') + +test('calling network by id', () => { + const network = lndEnvConfig() + expect(network).toBeDefined() + // const alice = network({username: 'alice'}) +}) + +test('load config', () => { + process.env.LND_INVOICE_MACAROON = 'a fake macaroon' + process.env.LND_TLS_CERT = 'a fake cert' + const network = lndEnvConfig(1) + const alice = network({username: 'alice', macaroonType: 'invoice'}) + expect(alice).toHaveProperty('baseUrl', '127.0.0.1:8080') + expect(alice).toHaveProperty('cert', 'a fake cert') + expect(alice).toHaveProperty('macaroon', 'a fake macaroon') +}) diff --git a/tests/lnd/lnd-polar-config.test.js b/tests/lnd/lnd-polar-config.test.js index fedec21..9eb7ebc 100644 --- a/tests/lnd/lnd-polar-config.test.js +++ b/tests/lnd/lnd-polar-config.test.js @@ -3,10 +3,23 @@ const lndPolarConfig = require('../../lnd/lnd-polar-config') +const isHexString = string => { + const hexPattern = /^[0-9a-fA-F]+$/ + return hexPattern.test(string) +} + +const isValidUTF8 = str => { + try { + Buffer.from(str, 'utf8') + return true + } catch (error) { + return false + } +} + test('calling network by id', () => { - const network = lndPolarConfig(2) + const network = lndPolarConfig(3) expect(network).toBeDefined() - // const alice = network({username: 'alice'}) }) test('load config', () => { @@ -16,3 +29,22 @@ test('load config', () => { expect(alice).toHaveProperty('cert') expect(alice).toHaveProperty('baseUrl') }) + +test('check content', () => { + const network = lndPolarConfig(3) + const {cert, macaroon} = network({username: 'alice'}) + + expect(isHexString(macaroon)).toBe(true) + expect(isHexString(cert)).toBe(false) + + const certUtf8 = Buffer.from(cert).toString('utf8') + // console.log(certUtf8) + expect(isValidUTF8(certUtf8)).toBe(true) + expect(certUtf8.length).toBe(806) + expect(certUtf8.startsWith('-----BEGIN CERTIFICATE-----')).toBe(true) + expect(certUtf8.endsWith('-----END CERTIFICATE-----\n')).toBe(true) + expect(certUtf8.includes('MIICJzCCAc2gAwIBA')).toBe(true) + // expect(cert).toHaveProperty('data') + // expect(cert).toHaveProperty('type') + // expect(cert).toMatchObject({type: 'Buffer'}) +})