forked from LampAndMaxAxie/LampAndMaxAxieBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.py
252 lines (203 loc) · 7.48 KB
/
Common.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import binascii
import configparser
import getpass
import json
import os
import sys
import discord
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Util import Counter
from discord.ext import commands
from dislash import slash_commands
from eth_account import Account
from loguru import logger
from web3 import Web3
import SeedStorage
# Setup Config Parser
config = configparser.ConfigParser()
try:
config.read(r'./config.cfg')
except:
logger.error("Please fill out a config.cfg file according to specifications.")
sys.exit()
try:
programName = config.get('Manager', 'programName')
ownerID = config.get('Manager', 'ownerDiscordID')
ownerRonin = config.get('Manager', 'ownerRoninAddr').replace("ronin:", "0x")
if ownerID is None or ownerID == "" or ownerRonin is None or ownerRonin == "":
raise Exception("Missing owner information")
except:
logger.error("Please fill out a [Manager] section for programName and ownerId.")
sys.exit()
try:
alertChannelId = int(config.get('Server', 'alertsChannelId'))
leaderboardChannelId = int(config.get('Server', 'leaderboardsChannelId'))
leaderboardPeriod = int(config.get('Server', 'leaderboardsPeriodHours'))
serverIds = json.loads(config.get('Server', 'serverIds'))
except:
logger.error("Please fill out a [Server] section with alertsChannelId and serverIds.")
sys.exit()
try:
qrBlacklist = json.loads(config.get('Bot', 'qrBlacklistIds'))
payBlacklist = json.loads(config.get('Bot', 'payBlacklistIds'))
prefix = config.get('Bot', 'prefix')
dmPayoutsToScholars = config.get('Bot', 'dmPayoutsToScholars')
if dmPayoutsToScholars == "True":
dmPayoutsToScholars = True
else:
dmPayoutsToScholars = False
hideScholarRonins = config.get('Bot', 'hideScholarRonins')
if hideScholarRonins == "True":
hideScholarRonins = True
else:
hideScholarRonins = False
dmErrorsToManagers = config.get('Bot', 'dmErrorsToManagers')
if dmErrorsToManagers == "False":
dmErrorsToManagers = False
else:
dmErrorsToManagers = True
except:
logger.error("Please fill out a [Bot] section with qrBlacklistIds, prefix, dmErrorsToManagers, dmPayoutsToScholars, and hideScholarRonins.")
sys.exit()
try:
requireNaming = config.get('Bot', 'requireNaming')
if requireNaming == "True":
requireNaming = True
requiredName = config.get('Bot', 'requiredName')
else:
requireNaming = False
requiredName = ""
except:
requireNaming = False
requiredName = ""
logger.error("Error in processing requireNaming and requiredName config options. Continuing without the requirement.")
try:
axieSalt = config.get('Encryption', 'salt')
if axieSalt == "" or axieSalt == "mysaltpleasechangeme" or len(axieSalt) > 1024:
raise Exception("Invalid salt")
except:
logger.error("Please fill out an [Encryption] section with a salt property up to 1024 characters.")
sys.exit()
# Setup Discord Bot
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=prefix, intents=intents)
slash = slash_commands.SlashClient(client)
# Globals
decryptionPass = ""
decryptionKey = ""
mnemonicList = SeedStorage.SeedList
Account.enable_unaudited_hdwallet_features()
# 32 bit keys => AES256 encryption
key_bytes = 32
if not os.path.exists("./iv.dat"):
print("IV data file not found. Please restore the file or re-run your seed encryption.")
sys.exit()
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, key_bytes)
else:
print("Note, the password field is hidden so it will not display what you type.")
decryptionPass = getpass.getpass().strip()
decryptionKey = PBKDF2(decryptionPass, axieSalt, key_bytes)
iv = None
with open("iv.dat", "rb") as f:
try:
iv = f.read()
except:
logger.error("There was an error reading your IV data file.")
sys.exit()
# Functions
# Encryption methodology adopted from https://stackoverflow.com/a/44662262
# 32 bit key, binary plaintext string to encrypt, and IV binary string
def encrypt(key, plaintext, iv=None):
assert len(key) == key_bytes
# create random IV if one not provided
if iv is None:
iv = Random.new().read(AES.block_size)
# convert IV to integer
iv_int = int(binascii.hexlify(iv), 16)
# create counter using the IV
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# create cipher object
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# encrypt the string and return the IV/ciphertext
ciphertext = aes.encrypt(plaintext)
return iv, ciphertext
# 32 bit key, IV binary string, and ciphertext to decrypt
def decrypt(key, iv, ciphertext):
assert len(key) == key_bytes
# 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 messageManagers(msg, managerIds):
global client
global dmErrorsToManagers
if not dmErrorsToManagers:
return
for managerId in managerIds:
logger.warning("Message error to " + str(managerId))
user = await client.fetch_user(int(managerId))
if user is not None:
await user.send(msg)
else:
logger.error("Failed to DM manager: " + str(managerId))
async def getNameFromDiscordID(UID):
logger.info(f"Fetching name for {UID}")
try:
usr = await client.fetch_user(int(UID))
logger.info(usr)
except:
usr = None
if usr is None:
return None
return usr.name + "#" + usr.discriminator
async def handleResponse(message, content, isSlash):
if isSlash:
await message.edit(content=content)
else:
await message.reply(content=content)
def isFloat(val):
try:
float(val)
except ValueError:
return False
return True
# Setup Filesystem
if not os.path.exists("./qr/"):
os.mkdir("qr")
if not os.path.exists("./images/"):
os.mkdir("images")
async def getFromMnemonic(seedNumber, accountNumber, scholarAddress):
try:
mnemonic = decrypt(decryptionKey, iv, mnemonicList[int(seedNumber) - 1]).decode("utf8")
scholarAccount = Account.from_mnemonic(mnemonic, "", "m/44'/60'/0'/0/" + str(int(accountNumber) - 1))
if scholarAddress.lower() == scholarAccount.address.lower():
# logger.info("Got the key for " + scholarAddress + " correctly")
return {
"key": Web3.toHex(scholarAccount.key),
"address": scholarAccount.address.lower()
}
else:
logger.error("Account Address did not match derived address")
logger.error(f"{scholarAddress} != {scholarAccount.address}")
return None
except Exception:
logger.error("Exception in getFromMnemonic, not logging trace since key or passwords may be involved")
# logger.error(traceback.format_exc())
return None
# check password
try:
x = decrypt(decryptionKey, iv, mnemonicList[0]).decode("utf8")
except:
print(f"Password failed.")
sys.exit()