-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagicWallet.py
204 lines (163 loc) · 6.4 KB
/
MagicWallet.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
# note hashin scp and paramiko to both requirements.txt
import paramiko
from scp import SCPClient
# Make sure PKTWallet.py have these integrated, via hashin
import os.path
from os import path
import time
import subprocess
import sys
# Add all of these
CONNECTED = False
LOCAL_WALLET_PATH = "." # This is set by get_wallet_db on integration
REMOTE_WALLET_PATH = "Wallets" # This is static
WALLET_NAME = "wallet.db"
WALLET_NAME_E = "wallet.db.gpg"
MAGIC_WALLET = True
class connection:
host = "raspberrypi.local"
port = 22
username = "magic"
password = "redman3" #redman2
def connect_magic_wllt(conn):
global CONNECTED, passphrase
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(conn.host, conn.port, conn.username, conn.password)
print("Connected successfully!", ssh)
passphrase = conn.password
CONNECTED = True
except Exception as e:
print("Couldn\'t connect, connection error:", e)
CONNECTED = False
#print("Failed to connect to device")
passphrase = input("Enter your password:")
passphrase = passphrase.strip()
conn.password = passphrase
print('CONN PWD:', conn.password)
ssh = connect_magic_wllt(conn)
return ssh
def request_frm_bck_up():
retrieve = input("Do you wish to retrieve your wallet from your magic stick? (y/n)")
if retrieve == 'y':
return True
else:
return False
def get_magic_wllt(ssh, passphrase):
if request_frm_bck_up():
command = "gpg --batch --passphrase passphrase --decrypt "+ WALLET_NAME_E +" | cat " + WALLET_NAME
try:
#stdin, stdout, stderr = ssh.exec_command(command)
#lines = stdout.readlines()
#if lines:
# print('lines:',lines)
scp = SCPClient(ssh.get_transport())
scp.get(REMOTE_WALLET_PATH + "/" + WALLET_NAME, LOCAL_WALLET_PATH)
scp.close()
result = "Successfully retrieved wallet from device."
except Exception as e:
print("Couldn\'t connect, connection error:", e)
result = "Failed to retrieve wallet from device."
sys.exit() # Quit
ssh.close()
print(result)
return result
def progress(filename, size, sent):
sys.stdout.write("%s's progress: %.2f%% \r" % (filename, float(sent)/float(size)*100) )
def progress4(filename, size, sent, peername):
sys.stdout.write("(%s:%s) %s's progress: %.2f%% \r" % (peername[0], peername[1], filename, float(sent)/float(size)*100) )
def put_magic_wllt(ssh, passphrase):
print("LOCAL_WALLET_PATH", LOCAL_WALLET_PATH)
# Check if local wallet exists
if path.exists(LOCAL_WALLET_PATH): # replace with get_wallet_db()
command = "gpg -c --batch --no-symkey-cache --compress-algo none --passphrase "+ passphrase + " " + REMOTE_WALLET_PATH + "/" + WALLET_NAME
try:
scp = SCPClient(ssh.get_transport(), progress4=progress4)
scp.put(f"{get_wallet_db()}", REMOTE_WALLET_PATH)
# Encrypt it
#stdin, stdout, stderr = ssh.exec_command(command)
#lines = stdout.readlines()
#if lines:
# print('Lines:',lines)
result = "Copied local wallet to device\n"
except Exception as e:
print("Couldn\'t connect, connection error:", e)
result = "Failed to copy wallet to device"
else:
result = "Couldn\'t find local wallet.db file, exiting..."
scp.close()
ssh.close()
print(result)
return result
def first_connect(ssh):
cmd = "ls -al "+ REMOTE_WALLET_PATH +"/"+ WALLET_NAME +">> /dev/null 2>&1 && echo yes || echo no | tr -d '\n'"
print('CMD', cmd)
stdin, stdout, stderr = ssh.exec_command(cmd)
lines = stdout.readlines()
print('LS result:', lines)
if len(lines) > 1:
for line in lines:
if "Too many logins" in line:
print('Too many logins on magic stick. If you are not logged into your stick through ssh then your wallet has been compromised.')
sys.exit()
else:
line = lines[0]
print('LIne', line)
if line == 'yes':
print('Wallet found on magic stick.')
print('Lines:',lines)
return False
elif line == 'no':
print('No wallet found on magic stick.')
return True
def get_wallet_db():
wallet_db = ''
get_db_cmd = "bin/getwalletdb"
get_db_result = (subprocess.Popen(get_db_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]).decode("utf-8")
print('get_db_result:', get_db_result)
if get_db_result.strip() != "Path not found":
wallet_db = get_db_result.strip('\n')+'/wallet.db'
print('Wallet location:', wallet_db)
else:
wallet_db = ''
print('wallet_db', wallet_db)
return wallet_db
def get_passphrase():
passphrase = input('Enter new wallet passphrase:')
passphrase = passphrase.strip()
return passphrase
def set_new_passphrase(passphrase, ssh):
command = "echo \'magic:"+passphrase+"\' | sudo chpasswd"
stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
if lines:
print('Lines:',lines)
# ----- MAIN -----
if __name__ == "__main__":
global passphrase
LOCAL_WALLET_PATH = get_wallet_db()
# On Startup
conn = connection()
ssh = connect_magic_wllt(conn)
print('MW', MAGIC_WALLET, 'CONNECTED', CONNECTED, 'ssh', ssh)
if MAGIC_WALLET and CONNECTED and ssh:
# On first connection
if first_connect(ssh):
# Copy local wallet to remote
print("First time connecting. Setting new PWD and Backing up local wallet to magic stick...")
#set new passphrase
passphrase = get_passphrase()
set_new_passphrase(passphrase, ssh)
put_magic_wllt(ssh, passphrase )
else:
# Copy remote wallet to local, Assumes remote wallet is more up to date.
print("Retrieving wallet from magic stick...")
get_magic_wllt(ssh, passphrase)
print("sleeping before shutdown...")
time.sleep(10)
# On Shutdown cycle
ssh = connect_magic_wllt(conn)
if MAGIC_WALLET and CONNECTED and ssh:
# Copy local wallet to remote
put_magic_wllt(ssh, passphrase)