-
Notifications
You must be signed in to change notification settings - Fork 7
/
keychain.py
94 lines (81 loc) · 2.58 KB
/
keychain.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
import database
import ec
from database import txn_required
import jserialize as js
import bserialize as bs
from utils import *
# 1huR1oV8uEE77HqQHCAuCzCzoq9HzXDSh
# L5TG3BkBgABJ1EXqznUSgRNXbdXwUpXEAEd6MDSFPsEnS5v2yX1i
keychain = database.open_db("keychain.dat")
keychain.set_get_returns_none(0)
class KeyEntry(bs.Entity, js.Entity):
bfields = [
("privatkey", bs.VarBytes),
("publickey", bs.VarBytes),
("txs", bs.VarList(bs.Hash))
]
fields = {
"privatkey": js.Bytes,
"publickey": js.Bytes,
"txs": js.List(js.Hash)
}
@property
def hash(self):
return hash160(self.publickey)
@staticmethod
def iter_keys(txn=None):
try:
cur = keychain.cursor(txn=txn)
while True:
try:
h, data = cur.next()
except KeyError:
break
yield KeyEntry.frombinary(data)[0]
finally:
cur.close()
def __init_key(self):
self._key = ec.Key.from_privkey(self.privatkey)
@property
def bitcoinaddress(self):
return hash2addr(self.hash)
@staticmethod
def get_by_hash(h, txn=None):
ret = KeyEntry.frombinary(keychain.get(h, txn=txn))[0]
ret.__init_key()
return ret
@staticmethod
def get_by_publickey(key, txn=None):
return KeyEntry.get_by_hash(hash160(key), txn=txn)
def tosecret(self):
secret = "\x80" + self._key.get_secret()
if self._key.get_compressed():
secret += "\x01"
secret = secret+doublesha(secret)[:4]
return b58encode(secret)
@classmethod
def fromsecret(cls, secret):
self = cls()
secret = b58decode(secret, None)
secret, cksum = secret[:-4], secret[-4:]
if doublesha(secret)[:4] != cksum:
return None
valid, secret, compressed = secret[0]=="\x80", secret[1:33], secret[33:] == "\x01"
if not valid:
return None
self._key = ec.Key.generate(secret, compressed)
self.privatkey = self._key.get_privkey()
self.publickey = self._key.get_pubkey()
self.txs = []
return self
@classmethod
def generate(cls):
self = cls()
self._key = ec.Key.generate()
self.privatkey = self._key.get_privkey()
self.publickey = self._key.get_pubkey()
self.txs = []
return self
@txn_required
def put(self, txn=None):
keychain.put(self.hash, self.tobinary(), txn=txn)