-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminus-cli
executable file
·113 lines (87 loc) · 3.17 KB
/
terminus-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# Copyright (c) 2021 Moneysocket Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php
import os
import sys
import json
import argparse
from configparser import ConfigParser
from twisted.internet import reactor
from txjsonrpc.web.jsonrpc import Proxy
from terminus.cmd_parse import TerminusCmdParse
from moneysocket.wad.wad import Wad
CL_CONFIG_FILE = os.path.join(os.path.expanduser("~"),
".lightning/bitcoin/moneysocket-terminus.conf")
LND_CONFIG_FILE = os.path.join(os.path.expanduser("~"),
".lnd/moneysocket-terminus.conf")
cl_exists = os.path.exists(CL_CONFIG_FILE)
lnd_exists = os.path.exists(LND_CONFIG_FILE)
CONFIG_FILE_HELP = """ Configuration settings to app run instance with. """
parser = TerminusCmdParse.get_parser()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(-1)
parser.add_argument('-c', '--config', type=str,
default=None, help=CONFIG_FILE_HELP)
parsed = parser.parse_args()
if parsed.config and not os.path.exists(parsed.config):
sys.exit("*** can't use config: %s" % parsed.config)
elif not (cl_exists or lnd_exists):
sys.exit("*** could not find %s or %s" % (CL_CONFIG_FILE,
LND_CONFIG_FILE))
CONFIG_FILE = (parsed.config if
(parsed.config and os.path.exists(parsed.config)) else
(CL_CONFIG_FILE if cl_exists else LND_CONFIG_FILE))
config = ConfigParser()
config.read(CONFIG_FILE)
rpc_interface = config['Rpc']['ExternalHost']
rpc_port = int(config['Rpc']['ExternalPort'])
URL = "http://%s:%d" % (rpc_interface, rpc_port)
def fmt_getinfo(json_resp):
#print(json_resp)
s = []
r = json.loads(json_resp)
s += ["ACCOUNTS:"]
for a in r['accounts']:
w = Wad.from_dict(a['wad'])
c = Wad.from_dict(a['cap'])
s += ["\t%8s: wad: %20s cap: %20s" % (a['name'], w, c)]
s += ["\t\tincoming beacons:"]
cas = a['connection_attempts']
for i in a['incoming_beacons']:
s += ["\t\t\t%s" % i]
s += ["\t\toutgoing beacons:"]
for o in a['outgoing_beacons']:
s += ["\t\t\t%s" % o]
if o in cas:
s += ["\t\t\t\t%s" % cas[o]]
s += [""]
return "\n".join(s)
def fmt_create(json_resp):
s = []
r = json.loads(json_resp)
w = Wad.from_dict(r['wad'])
c = Wad.from_dict(r['cap'])
s += ["success: %s" % r['success']]
s += ["account name: %s" % r['name']]
s += ["wad: %s" % w]
s += ["cap: %s" % c]
return "\n".join(s)
def printValue(value, cmd):
if cmd == "getinfo":
print(fmt_getinfo(value))
elif cmd == "create":
print(fmt_create(value))
else:
print(value)
def printError(error):
print("*** - %s" % error.getErrorMessage())
def shutDown(data):
reactor.stop()
proxy = Proxy(URL)
cmd = sys.argv[3] if parsed.config else sys.argv[1]
remainder = sys.argv[4:] if parsed.config else sys.argv[2:]
d = proxy.callRemote(cmd, remainder)
d.addCallback(printValue, cmd).addErrback(printError).addBoth(shutDown)
reactor.run()