diff --git a/lib/cli/commands/listpeers.ts b/lib/cli/commands/listpeers.ts index c44cb7cf8..01d1b953b 100644 --- a/lib/cli/commands/listpeers.ts +++ b/lib/cli/commands/listpeers.ts @@ -3,7 +3,7 @@ import colors from 'colors/safe'; import { Arguments } from 'yargs'; import { ListPeersRequest, ListPeersResponse, Peer } from '../../proto/xudrpc_pb'; import { callback, loadXudClient } from '../command'; -import { generateHeaders } from '../utils'; +import { generateHeaders, shorten } from '../utils'; const HEADERS = [ 'Peer', @@ -18,13 +18,6 @@ const createTable = () => { return table; }; -const trimPubKey = (key: string) => { - if (key.length <= 0) { - return ''; - } - return `${key.slice(0, 10)}...${key.slice(key.length - 10)}`; -}; - const formatPairList = (pairs: string[]) => { let pairString = ''; pairs.forEach((pair) => { @@ -38,7 +31,7 @@ const formatLndPubKeys = (lndKeys: string[][]) => { let str = ''; lndKeys.forEach((client) => { /* eslint disable-next-line */ - str = `${str}${str ? '\n' : ''}${client[0]} lnd key: ${trimPubKey(client[1])}`; + str = `${str}${str ? '\n' : ''}${client[0]} lnd key: ${shorten(client[1])}`; }); return str; }; @@ -60,7 +53,7 @@ ${address}`, \nversion: ${peer.xudVersion}\ \ntime connected: ${peer.secondsConnected.toString()} seconds\ \n${formatLndPubKeys(peer.lndPubKeysMap)}\ -${peer.raidenAddress ? `\nraiden address: ${trimPubKey(peer.raidenAddress)}` : ''}`, +${peer.raidenAddress ? `\nraiden address: ${shorten(peer.raidenAddress)}` : ''}`, ]; formattedPeers.push(details); }); diff --git a/lib/cli/utils.ts b/lib/cli/utils.ts index 0535e9aee..b79675f57 100644 --- a/lib/cli/utils.ts +++ b/lib/cli/utils.ts @@ -22,6 +22,20 @@ export function getDefaultCertPath() { } } +export function shorten(key: string, length = 10) { + if (key.length <= (length * 2) + 3) { + return key; + } + return `${key.slice(0, length)}...${key.slice(key.length - length)}`; +} + +export function trim(key: string, length = 10) { + if (key.length <= length + 3) { + return ''; + } + return `${key.slice(0, length)}...`; +} + export const generateHeaders = (headers: string[]) => { return headers.map((header) => { return colors.blue(header);