-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportScholarsFromSeeds.py
177 lines (145 loc) · 5.54 KB
/
ImportScholarsFromSeeds.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import sys
import DB
from Common import client, getNameFromDiscordID
import binascii
import getpass
import os
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Util import Counter
from eth_account import Account
from loguru import logger
import SeedStorage
fName = "import.txt"
# Globals
mnemonicList = SeedStorage.SeedList
accounts = {}
currentCount = 0
Account.enable_unaudited_hdwallet_features()
if os.path.exists("./.botpass"):
with open("./.botpass", "r") as f:
logger.info("Using password saved in .botpass file")
decryptionPass = f.read().strip()
decryptionKey = PBKDF2(decryptionPass, "axiesalt", 32)
else:
print("Note, the password field is hidden so it will not display what you type.")
decryptionPass = getpass.getpass().strip()
decryptionKey = PBKDF2(decryptionPass, "axiesalt", 32)
with open("iv.dat", "rb") as f:
try:
iv = f.read()
except:
logger.error("There was an error reading your IV data file.")
exit()
if len(sys.argv) > 1:
fName = str(sys.argv[1])
if not os.path.exists(fName):
print(f"File {fName} not found, please provide the import file")
exit()
@client.event
async def on_ready():
await importScholars(fName)
async def importScholars(fName):
try:
await DB.createMainTables()
except:
logger.error("Failed to create tables")
exit()
try:
currentCount = await getFromMnemonic()
except:
logger.error("Something went wrong")
exit()
count = 0
with open(fName) as f:
for line in f:
# skip comment lines
if line.startswith('#'):
continue
args = [x.strip() for x in line.split(',')]
if len(args) > 4:
logger.error("Too many args, are you trying to run the normal import scholars file?")
exit()
roninAddr = args[0].replace("ronin:", "0x").strip()
discordID = args[1]
scholarShare = round(float(args[2]), 3)
if len(args) > 3:
payoutAddr = args[3].replace("ronin:", "0x").strip()
else:
payoutAddr = None
seedNum, accountNum, currentCount = await getAccountNum(currentCount, roninAddr)
name = await getNameFromDiscordID(discordID)
res = await DB.addScholar(discordID, name, seedNum+1, accountNum+1, roninAddr, scholarShare)
if payoutAddr is not None and payoutAddr != "":
await DB.updateScholarAddress(discordID, payoutAddr)
if not res["success"]:
logger.error(f"failed to import scholar {discordID}")
logger.error(res)
else:
count += 1
logger.info(res['msg'])
res = await DB.getAllScholars()
if not res["success"]:
logger.error("failed to get all scholars from database")
exit()
for row in res["rows"]:
logger.info(f"{row['discord_id']}: seed/acc {row['seed_num']}/{row['account_num']} and share {row['share']}")
logger.info(f"Imported {count} scholars")
sys.exit("Done")
# 32 bit key, IV binary string, and ciphertext to decrypt
def decrypt(key, iv, ciphertext):
assert len(key) == 32
# convert IV to integer and create counter using the IV
iv_int = int(binascii.hexlify(iv), 16)
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# create cipher object
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# decrypt ciphertext and return the decrypted binary string
plaintext = aes.decrypt(ciphertext)
return plaintext
async def getFromMnemonic():
try:
for a in range(len(mnemonicList)):
accounts[a] = {}
mnemonic = decrypt(decryptionKey, iv, mnemonicList[int(a)]).decode("utf8")
for b in range(500):
scholarAccount = Account.from_mnemonic(mnemonic, "", "m/44'/60'/0'/0/" + str(int(b)))
accounts[a][scholarAccount.address.lower()] = b
currentCount = 500
return currentCount
except Exception:
logger.error("Exception in getFromMnemonic, not logging trace since key or passwords may be involved")
return None
async def getMoreAddresses(currentCount):
try:
for a in range(len(mnemonicList)):
mnemonic = decrypt(decryptionKey, iv, mnemonicList[int(a)]).decode("utf8")
for b in range(250):
scholarAccount = Account.from_mnemonic(mnemonic, "", "m/44'/60'/0'/0/" + str(int(b + currentCount)))
accounts[a][scholarAccount.address.lower()] = b+currentCount
currentCount += 250
return currentCount
except Exception:
logger.error("Exception in getMoreAddresses, not logging trace since key or passwords may be involved")
return None
async def getAccountNum(currentCount, address):
seedNum = None
accountNum = None
for a in accounts:
if address in accounts[a]:
seedNum = a
accountNum = accounts[a][address]
break
if currentCount >= 5000:
logger.error("Could not get scholars address " + address + " from seeds. Something is wrong. Are you missing a seed phrase?")
exit()
if seedNum is None:
currentCount = await getMoreAddresses(currentCount)
return await getAccountNum(currentCount, address)
return seedNum, accountNum, currentCount
try:
x = decrypt(decryptionKey, iv, mnemonicList[0]).decode("utf8")
except:
print(f"Password failed.")
exit()
client.run(SeedStorage.DiscordBotToken)