-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.py
More file actions
134 lines (109 loc) · 3.94 KB
/
Copy pathwallet.py
File metadata and controls
134 lines (109 loc) · 3.94 KB
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
import requests
import json
from datetime import datetime, timezone
from ellipticcurve.ecdsa import Ecdsa
from ellipticcurve.privateKey import PrivateKey
from ellipticcurve.publicKey import PublicKey
import socket
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.connect((UDP_IP_ADDRESS, UDP_PORT_NO))
def get(command):
api = requests.get(f'http://127.0.0.1:9999/{command}')
data = api.text
if data != "-1":
data = json.loads(data)
return data
class Wallet():
def __init__(self, public_Addr, private_Key):
self.public_Addr = public_Addr
self.private_Key = private_Key
def get_Balance(self):
data = requests.get(f'http://127.0.0.1:9999/get-balance', json = {"address":self.public_Addr}).text
balance = json.loads(data)
balance = balance["balance"]
return balance
def send(self, value, receiver):
timestamp = str(datetime.now(timezone.utc))
trans_str = f"{value}|{self.public_Addr}|{receiver.replace('-----BEGIN PUBLIC KEY-----', '').replace('-----END PUBLIC KEY-----', '').strip()}|{timestamp}"
signature = Ecdsa.sign(trans_str,PrivateKey.fromString(self.private_Key))
print(signature)
signature = signature._toString()
#test = Signature._fromString(signature)
#print(test)
#print(signature)
Message = f"{value}|{self.public_Addr}|{receiver.replace('-----BEGIN PUBLIC KEY-----', '').replace('-----END PUBLIC KEY-----', '').strip()}|{timestamp}|{signature}"
trans_dict = {"trans_str":Message}
try:
#clientSock.sendto(Message.encode(), (UDP_IP_ADDRESS, UDP_PORT_NO))
transaction = requests.get(f'http://127.0.0.1:9999/transact', json = trans_dict)
#print(f"####{x}####")
print("Transaction Successful!")
except:
print("Connection Failed. Funds were not processed.")
def get_All_transactions(self):
data = requests.get(f'http://127.0.0.1:9999/get-all-transactions', json = {"address":self.public_Addr}).text
transactions = json.load(data)
return transactions
def in_Wallet(wallet):
print("Logged in to your wallet successfully!")
while True:
ans = input("Send monies or check balance?(send/balance)").lower()
if ans == "send":
try:
value = float(input("how many coins?"))
try:
receiver = input("Address of the receiver:\n")
PublicKey.fromString(receiver)
wallet.send(value, receiver)
except Exception as e:
print(e)
print("Address invalid!")
except:
print("Please enter a number.")
elif ans == "balance":
balance = wallet.get_Balance()
if balance == "None":
balance = 0
print(f"Your current balance: {balance}")
def main():
while True:
ans = input("Do you have an account already?(Y/n)").lower()
if ans == 'y':
try:
public_Key = input("Pls enter your public key:\n")
private_Key = input("Pls enter your private key:\n")
msg = "Signing in"
signature = Ecdsa.sign(msg, PrivateKey.fromString(private_Key))
except:
print("Keys are invalid. Try again!")
if Ecdsa.verify(msg, signature, PublicKey.fromString(public_Key)):
wallet = Wallet(public_Key, private_Key)
print("Keys match! Logging you in....")
in_Wallet(wallet)
break
else:
print("Keys do not match. Try again!")
elif ans == 'n' :
ans = input("Do you wanna generate an account?(Y/n)")
if ans == "y":
private_Key = PrivateKey()
public_Key = private_Key.publicKey()
wallet = Wallet(public_Key.toString(), private_Key.toString())
print(f"Your Public Key / Address:\n{public_Key.toString()}")
print(f"Your Private Key (Keep this safe af):\n{private_Key.toString()}")
print("Logging you in....")
print("Dude note the keys down in a safe place.")
while True:
reply = input("Log in to your wallet?(Y/n) (Please note the keys down in a safe place)").lower()
if reply == "y":
in_Wallet(wallet)
else:
print("Ok, waiting..")
elif ans == 'n':
print("Ok Byee")
else:
print("Invalid input!")
if __name__ == "__main__":
main()