From b18d08b70e7540fe71d4a6afe54482cecb202726 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Fri, 2 Nov 2018 00:04:26 +0100 Subject: [PATCH] feat(crypto): add utility methods for lnd data Adds a few basic utility methods for dealing with lnd related data. --- app/lib/utils/crypto.js | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/app/lib/utils/crypto.js b/app/lib/utils/crypto.js index ba7c35e4..39b3a74d 100644 --- a/app/lib/utils/crypto.js +++ b/app/lib/utils/crypto.js @@ -59,6 +59,10 @@ export const parseNumber = (_value, precision) => { * @return {Boolean} boolean indicating wether the address is a valid on-chain address. */ export const isOnchain = (input, chain = 'bitcoin', network = 'mainnet') => { + if (typeof input !== 'string') { + return false + } + if (chain !== 'bitcoin') { // TODO: Implement address checking for litecoin. return true @@ -82,6 +86,10 @@ export const isOnchain = (input, chain = 'bitcoin', network = 'mainnet') => { * @return {Boolean} boolean indicating wether the address is a lightning address. */ export const isLn = (input, chain = 'bitcoin', network = 'mainnet') => { + if (typeof input !== 'string') { + return false + } + let prefix = 'ln' // Prefixes come from SLIP-0173 // See https://github.com/satoshilabs/slips/blob/master/slip-0173.md @@ -122,3 +130,53 @@ export const isLn = (input, chain = 'bitcoin', network = 'mainnet') => { return false } } + +/** + * Get a nodes alias. + * @param {String} pubkey pubKey of node to fetch alias for. + * @param {Array} Node list to search. + * @return {String} Node alias, if found + */ +export const getNodeAlias = (pubkey, nodes = []) => { + const node = nodes.find(n => n.pub_key === pubkey) + + if (node && node.alias.length) { + return node.alias + } + + return null +} + +/** + * Given a list of routest, find the minimum fee. + * @param {QueryRoutesResponse} routes + * @return {Number} minimum fee. + */ +export const getMinFee = (routes = []) => { + if (!routes || !routes.length) { + return null + } + return routes.reduce((min, b) => Math.min(min, b.total_fees_msat), routes[0].total_fees_msat) +} + +/** + * Given a list of routest, find the maximum fee. + * @param {QueryRoutesResponse} routes + * @return {Number} maximum fee. + */ +export const getMaxFee = routes => { + if (!routes || !routes.length) { + return null + } + return routes.reduce((max, b) => Math.max(max, b.total_fees_msat), routes[0].total_fees_msat) +} + +/** + * Given a list of routest, find the maximum and maximum fee. + * @param {QueryRoutesResponse} routes + * @return {Object} object with kets `min` and `max` + */ +export const getFeeRange = (routes = []) => ({ + min: getMinFee(routes), + max: getMaxFee(routes) +})