forked from antIggl/xrpl-unl-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodeUNL.py
executable file
·126 lines (93 loc) · 4.21 KB
/
encodeUNL.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
import sys
import utils
import json
import os
def parseValidatorTokenFile(vtokenfname):
"""Parses the validator-token.txt file and return a json object
Arguments:
vtokenfname {[type]} -- [description]
Returns:
[type] -- [description]
"""
with open(vtokenfname,'r') as f:
vkcont=f.read()
vtokenstr=vkcont.split('[validator_token]')[1].strip()
vtokenstr=vtokenstr.replace('\n','')
# print(vtokenstr)
return (utils.decodeValidatorToken(vtokenstr))
def parseListFile(listfname:str):
"""Parses the list file
The list file could be either line list of validators' names OR
a file with a json array. starts with '['
Arguments:
listfname {str} -- [description]
Returns:
list of validator names
"""
with open(listfname,'r') as f:
fcont=f.read()
fcont=fcont.strip()
if fcont.startswith('['):
# it's json array
return json.loads(fcont)
else:
# it should be a line list
flines=fcont.split('\n')
for l in flines:
if l.strip().startswith('#'):
# it's commented out
flines.remove(l)
return flines
if __name__=='__main__':
import argparse
import time
DEFAULT_TIMEDIFF=31528800.0 # One year
argparser=argparse.ArgumentParser(description="Encodes a XRP Ledger UNL from a file containing either a JSON list or line-separated validator names")
argparser.add_argument("-f","--list-file", default='./unl-list.json', type=str, help="Defines the UNL file to be parsed. It needs the validators-keys-path")
argparser.add_argument("-blf","--bloblist-file", type=str, help="Defines the UNL blob list file to be parsed. \n - Expiration date and sequence can be set separately")
argparser.add_argument("-bf","--blob-file", type=str, help="Defines the UNL blob file to be parsed \n - Expiration date and sequence cannot be set")
# cmdgroup.set_defaults()
argparser.add_argument("-v","--version", default=1,type=int,
help="Defines the version/sequence of the UNL.")
argparser.add_argument("-xd","--expire-date", type=str,
help="Sets the expiration date of the generated UNL. (format: YYYYMMDDhhmmss). Defaults to 1 year since now.")
argparser.add_argument("-kf","--keys-file", default='./unl-generator-token.txt', type=str, help="Defines the keys-pair file used to sign the UNL")
argparser.add_argument("-kp","--validators-keys-path", default='./configfiles/', type=str, help="Defines the root path for the validators")
argparser.add_argument("-o","--output-file", type=str,default='./encoded-list.json',help="Defines the output file.")
aa=argparser.parse_args()
if not aa.keys_file:
print("--keys-file argument is required to sign the generated UNL.")
sys.exit(1)
# print (aa,aa.keys_file)
vtoken=parseValidatorTokenFile(aa.keys_file)
exp_date= time.time()+DEFAULT_TIMEDIFF
if aa.expire_date:
exp_date=time.mktime(time.strptime(aa.expire_date, "%Y%m%d%H%M%S"))
vkpath=None
mvallist=[]
munl={}
if not (aa.bloblist_file or aa.blob_file):
print("Parsing the list file {} and looking for validators keys in {}".format(aa.list_file,aa.validators_keys_path))
mvallist=parseListFile(aa.list_file)
vkpath=os.path.abspath(aa.validators_keys_path)
munl=utils.createUNL(mvallist,vtoken,aa.version,vkpath,exp_date)
if aa.blob_file:
mblobvallist={}
with open(aa.blob_file,'r')as f:
mblobvallist= json.load(f)
# print (mblobvallist)
munl=utils.createUNL_from_blob(mblobvallist,vtoken)#,vkpath)
# print(munl, type(munl))
# print (json.dumps(munl))
if aa.bloblist_file:
mblobvallist={}
with open(aa.bloblist_file,'r')as f:
mblobvallist= json.load(f)
# print (mblobvallist)
munl=utils.createUNL_from_bloblist(mblobvallist,vtoken,aa.version,exp_date)
# print(munl, type(munl))
# print (json.dumps(munl))
with open(aa.output_file,'w') as f:
json.dump(munl,f)
print ('Finished!!!')