forked from lapitskiy/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.py
More file actions
90 lines (74 loc) · 2.8 KB
/
wallet.py
File metadata and controls
90 lines (74 loc) · 2.8 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
import ecdsa
import binascii
import hashlib
import base58
import redis
from decimal import Decimal,getcontext
getcontext().prec = 7
import os
from uuid import uuid4
from argon2 import PasswordHasher
import urllib3
class Wallet:
# SECP256k1 elliptic curve
# make cookie wallet
def __init__(self):
self.private_key = []
self.public_key = []
def b58_key(self,key):
key = binascii.hexlify(key.to_string()).decode('ascii')
return key
def b58_key_encode(self,key):
key = base58.b58encode(bytes.fromhex(key))
return key
def public_key_to_adress(self,key):
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(key.encode()).digest())
key = base58.b58encode(ripemd160.digest())
return key
def b58_key_decode(self,key):
key = base58.b58decode(key).hex()
return key
@staticmethod
def write_new_wallet(wfc):
with open('wallet.pem', 'w') as out:
for key, val in wfc.items():
out.write('{}:{}\n'.format(key, val))
def read_wallet(self,wallet_data):
item = eval(wallet_data)
# item = dict(item.split(':') for item in wallet_data.split())
return item
def pass_hash(self,pwd):
# Победитель Password Hashing Competition Argon2 https://habrahabr.ru/post/281569/
ph = PasswordHasher()
pwd_hash = ph.hash(pwd)
return pwd_hash
def pass_verify(self,pwd,hash):
ph = PasswordHasher()
something = 'pass wrong'
try:
val = ph.verify(hash, pwd)
except:
return something
return bool(val)
def generate_key(self):
generate_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
public_key = generate_key.get_verifying_key() # clear key: <ecdsa.keys.SigningKey object at 0x06521A30>
public_key = public_key.to_string().hex()
private_key = binascii.hexlify(generate_key.to_string()).decode() #privat key: 374bc766d11a59a826249fc42f370cee0518e70925c96e73c1848716216d2f64
return (private_key, public_key)
def generate_sig(self, prkey, msg):
unhex = binascii.unhexlify(prkey) # unhex privat key clear: b'7K\xc7f\xd1\x1aY\xa8&$\x9f\xc4/7\x0c\xee\x05\x18\xe7\t%\xc9ns\xc1\x84\x87\x16!m/d'
sign_msg = ecdsa.SigningKey.from_string(unhex, curve=ecdsa.SECP256k1)
msg = str(msg).encode()
sig = sign_msg.sign(msg)
sig = sig.hex()
return sig
def verify_sig(self, pbkey, sign, msg):
verify_key = ecdsa.VerifyingKey.from_string(bytes.fromhex(pbkey), curve=ecdsa.SECP256k1)
try:
msg = str(msg).encode()
verify = verify_key.verify(bytes.fromhex(sign), msg)
except ecdsa.BadSignatureError:
verify = False
return verify