-
Notifications
You must be signed in to change notification settings - Fork 2
/
barn.py
executable file
·147 lines (104 loc) · 4.95 KB
/
barn.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import asyncio
import sys
import argparse
import json
import urllib.request
import subprocess
'''
Available APIs to pull CAT data from:
natsabari in Keybase:
https://api2.spacescan.io/1/xch/tokens/summary?page=1&count=25
https://xchtoken.org/token_api.php
'''
CAT_API_URL='https://xchtoken.org/token_api.php'
async def update_with_new_cats(chia_path, chia_wallet_fingerprint):
print('----------UPDATE CATS ---------')
# get a list of current cats
all_cats = await get_available_cats()
# print(json.dumps(all_cats, indent=4, sort_keys=True))
print('Looking for stray CATs...\n')
# get CATs from chia light wallet which don't have names in chia wallet
chia_local_wallet_cat_list=chia_path + " wallet show -f " + chia_wallet_fingerprint
process = subprocess.Popen(chia_local_wallet_cat_list, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output,stderr = process.communicate()
status = process.poll()
# print(output)
untamed_cats_list = []
for line in output.decode('ascii').splitlines():
# print(line)
if "type COLOURED_COIN CAT WALLET" in line:
line_as_list = line.split()
asset_id_location = line_as_list.index('ID:')
if (asset_id_location != -1):
unnamed_asset_id = line_as_list[asset_id_location+1]
unnamed_asset_id = unnamed_asset_id.replace(')', '')
wallet_id = line_as_list[2]
print("FOUND unnamed CAT: walletID: " + wallet_id + " assetID: " + unnamed_asset_id)
untamed_cats_list.append(unnamed_asset_id)
if (len(untamed_cats_list) == 0):
print ("All cats are tamed...err...named [^._.^]ノ彡")
sys.exit(1)
# print(json.dumps(all_cats, indent=4, sort_keys=True))
# rename cats without names
for untamed_cat in untamed_cats_list:
await rename_cat(chia_path, chia_wallet_fingerprint, untamed_cat, all_cats)
print("All cats are tamed...err...named [^._.^]ノ彡")
#chia wallet add_token -id <asset_id> -n <asset_name>
async def rename_cat(chia_path, chia_wallet_fingerprint, asset_id, known_cats_list):
known_cats_list = known_cats_list['data']
for known_cat in known_cats_list:
# print(known_cat)
# print('-------------------------------------------')
if not 'ASSET_ID' in known_cat:
continue
if (known_cat['ASSET_ID'] == asset_id):
if not 'Symbol' in known_cat and not 'Name' in known_cat:
print('ERROR: Found ASSET_ID but neither Symbol nor Name...skipping...')
continue
new_wallet_name = ''
symbol = ''
name = ''
if 'Symbol' in known_cat:
symbol = known_cat['Symbol']
new_wallet_name = symbol
if 'Name' in known_cat:
name = known_cat['Name']
if len(new_wallet_name) > 0:
new_wallet_name += " (" + name + ")"
else:
new_wallet_name = name
# print("Adopting CAT: " + new_wallet_name)
chia_local_wallet_cat_list=chia_path + " wallet add_token -f " + chia_wallet_fingerprint + " -id " + asset_id + ' -n \"' + new_wallet_name + '\"'
process = subprocess.Popen(chia_local_wallet_cat_list, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output,stderr = process.communicate()
status = process.poll()
for line in output.decode('ascii').splitlines():
print(line)
print('----------------------------------------')
async def get_available_cats():
print('Retrieving lastes CAT names...')
cat_api_url=CAT_API_URL
req = urllib.request.Request(cat_api_url)
with urllib.request.urlopen(req) as url:
all_cats_json = json.loads(url.read().decode())
return all_cats_json
def get_args():
# Add default=bootup(), for calling a method
parser = argparse.ArgumentParser(description='Quickly add Chia Asset Token names to your light wallet.')
parser.add_argument('-u', '--herd-cats', metavar=('CHIA_PATH', 'fingerprint'), nargs=2, required=False, help='Update Chia light wallet with new CAT names. Provide Chia executable path and wallet fingerprint.')
#sys.argv includes a list of elements starting with the program
if len(sys.argv) < 2:
# parser.print_usage()
parser.print_help()
sys.exit(1)
return parser.parse_args()
ARGS = get_args()
async def main():
if ARGS.herd_cats:
chia_path=ARGS.herd_cats[0]
wallet_fingerprint=ARGS.herd_cats[1]
await update_with_new_cats(chia_path, wallet_fingerprint)
asyncio.run(main())