forked from gdassori/BitcoinAVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.py
223 lines (174 loc) · 7.25 KB
/
account.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
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2013 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from bitcoin import *
from transaction import Transaction
class Account(object):
def __init__(self, v):
self.addresses = v.get('0', [])
self.change = v.get('1', [])
def dump(self):
return {'0':self.addresses, '1':self.change}
def get_addresses(self, for_change):
return self.change[:] if for_change else self.addresses[:]
def create_new_address(self, for_change):
addresses = self.change if for_change else self.addresses
n = len(addresses)
address = self.get_address( for_change, n)
addresses.append(address)
print address
return address
def get_address(self, for_change, n):
pass
def get_pubkeys(self, sequence):
return [ self.get_pubkey( *sequence )]
class OldAccount(Account):
""" Privatekey(type,n) = Master_private_key + H(n|S|type) """
def __init__(self, v):
self.addresses = v.get(0, [])
self.change = v.get(1, [])
self.mpk = v['mpk'].decode('hex')
def dump(self):
return {0:self.addresses, 1:self.change}
@classmethod
def mpk_from_seed(klass, seed):
curve = SECP256k1
secexp = klass.stretch_key(seed)
master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
return master_public_key
@classmethod
def stretch_key(self,seed):
oldseed = seed
for i in range(100000):
seed = hashlib.sha256(seed + oldseed).digest()
return string_to_number( seed )
def get_sequence(self, for_change, n):
return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.mpk ) )
def get_address(self, for_change, n):
pubkey = self.get_pubkey(for_change, n)
address = public_key_to_bc_address( pubkey.decode('hex') )
return address
def get_pubkey(self, for_change, n):
curve = SECP256k1
mpk = self.mpk
z = self.get_sequence(for_change, n)
master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
pubkey_point = master_public_key.pubkey.point + z*curve.generator
public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
return '04' + public_key2.to_string().encode('hex')
def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
order = generator_secp256k1.order()
secexp = ( secexp + self.get_sequence(for_change, n) ) % order
pk = number_to_string( secexp, generator_secp256k1.order() )
compressed = False
return SecretToASecret( pk, compressed )
def get_private_key(self, seed, sequence):
for_change, n = sequence
secexp = self.stretch_key(seed)
return self.get_private_key_from_stretched_exponent(for_change, n, secexp)
def check_seed(self, seed):
curve = SECP256k1
secexp = self.stretch_key(seed)
master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
master_public_key = master_private_key.get_verifying_key().to_string()
if master_public_key != self.mpk:
print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
raise Exception('Invalid password')
return True
def redeem_script(self, sequence):
return None
class BIP32_Account(Account):
def __init__(self, v):
Account.__init__(self, v)
self.c = v['c'].decode('hex')
self.K = v['K'].decode('hex')
self.cK = v['cK'].decode('hex')
def dump(self):
d = Account.dump(self)
d['c'] = self.c.encode('hex')
d['K'] = self.K.encode('hex')
d['cK'] = self.cK.encode('hex')
return d
def get_address(self, for_change, n):
pubkey = self.get_pubkey(for_change, n)
address = public_key_to_bc_address( pubkey.decode('hex') )
return address
def first_address(self):
return self.get_address(0,0)
def get_pubkey(self, for_change, n):
K = self.K
chain = self.c
for i in [for_change, n]:
K, K_compressed, chain = CKD_prime(K, chain, i)
return K_compressed.encode('hex')
def redeem_script(self, sequence):
return None
class BIP32_Account_2of2(BIP32_Account):
def __init__(self, v):
BIP32_Account.__init__(self, v)
self.c2 = v['c2'].decode('hex')
self.K2 = v['K2'].decode('hex')
self.cK2 = v['cK2'].decode('hex')
def dump(self):
d = BIP32_Account.dump(self)
d['c2'] = self.c2.encode('hex')
d['K2'] = self.K2.encode('hex')
d['cK2'] = self.cK2.encode('hex')
return d
def get_pubkey2(self, for_change, n):
K = self.K2
chain = self.c2
for i in [for_change, n]:
K, K_compressed, chain = CKD_prime(K, chain, i)
return K_compressed.encode('hex')
def redeem_script(self, sequence):
chain, i = sequence
pubkey1 = self.get_pubkey(chain, i)
pubkey2 = self.get_pubkey2(chain, i)
return Transaction.multisig_script([pubkey1, pubkey2], 2)
def get_address(self, for_change, n):
address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
return address
def get_pubkeys(self, sequence):
return [ self.get_pubkey( *sequence ), self.get_pubkey2( *sequence )]
class BIP32_Account_2of3(BIP32_Account_2of2):
def __init__(self, v):
BIP32_Account_2of2.__init__(self, v)
self.c3 = v['c3'].decode('hex')
self.K3 = v['K3'].decode('hex')
self.cK3 = v['cK3'].decode('hex')
def dump(self):
d = BIP32_Account_2of2.dump(self)
d['c3'] = self.c3.encode('hex')
d['K3'] = self.K3.encode('hex')
d['cK3'] = self.cK3.encode('hex')
return d
def get_pubkey3(self, for_change, n):
K = self.K3
chain = self.c3
for i in [for_change, n]:
K, K_compressed, chain = CKD_prime(K, chain, i)
return K_compressed.encode('hex')
def get_redeem_script(self, sequence):
chain, i = sequence
pubkey1 = self.get_pubkey(chain, i)
pubkey2 = self.get_pubkey2(chain, i)
pubkey3 = self.get_pubkey3(chain, i)
return Transaction.multisig_script([pubkey1, pubkey2, pubkey3], 3)
def get_pubkeys(self, sequence):
return [ self.get_pubkey( *sequence ), self.get_pubkey2( *sequence ), self.get_pubkey3( *sequence )]