1
+ from builtins import hex
2
+ from builtins import object
1
3
import json
2
4
import os
3
5
from random import SystemRandom
7
9
from ethereum .tools import keys
8
10
from ethereum .slogging import get_logger
9
11
from ethereum .utils import privtopub # this is different than the one used in devp2p.crypto
10
- from ethereum .utils import sha3 , is_string , decode_hex , remove_0x_head
12
+ from ethereum .utils import sha3 , is_string , encode_hex , remove_0x_head , to_string
13
+ from rlp .utils import decode_hex
14
+
15
+ from pyethapp .utils import MinType
16
+
11
17
log = get_logger ('accounts' )
12
18
13
- DEFAULT_COINBASE = 'de0b295669a9fd93d5f28d9ec85e40f4cb697bae' . decode ( 'hex ' )
19
+ DEFAULT_COINBASE = decode_hex ( 'de0b295669a9fd93d5f28d9ec85e40f4cb697bae ' )
14
20
15
21
random = SystemRandom ()
16
22
@@ -22,7 +28,7 @@ def mk_privkey(seed):
22
28
def mk_random_privkey ():
23
29
k = hex (random .getrandbits (256 ))[2 :- 1 ].zfill (64 )
24
30
assert len (k ) == 64
25
- return k . decode ( 'hex' )
31
+ return decode_hex ( k )
26
32
27
33
28
34
class Account (object ):
@@ -38,7 +44,7 @@ class Account(object):
38
44
def __init__ (self , keystore , password = None , path = None ):
39
45
self .keystore = keystore
40
46
try :
41
- self ._address = self .keystore ['address' ]. decode ( 'hex' )
47
+ self ._address = decode_hex ( self .keystore ['address' ])
42
48
except KeyError :
43
49
self ._address = None
44
50
self .locked = True
@@ -61,6 +67,13 @@ def new(cls, password, key=None, uuid=None, path=None):
61
67
"""
62
68
if key is None :
63
69
key = mk_random_privkey ()
70
+
71
+ # [NOTE]: key and password should be bytes
72
+ if not is_string (key ):
73
+ key = to_string (key )
74
+ if not is_string (password ):
75
+ password = to_string (password )
76
+
64
77
keystore = keys .make_keystore_json (key , password )
65
78
keystore ['id' ] = uuid
66
79
return Account (keystore , password , path )
@@ -94,9 +107,9 @@ def dump(self, include_address=True, include_id=True):
94
107
d ['crypto' ] = self .keystore ['crypto' ]
95
108
d ['version' ] = self .keystore ['version' ]
96
109
if include_address and self .address is not None :
97
- d ['address' ] = self .address . encode ( 'hex' )
110
+ d ['address' ] = encode_hex ( self .address )
98
111
if include_id and self .uuid is not None :
99
- d ['id' ] = self .uuid
112
+ d ['id' ] = str ( self .uuid )
100
113
return json .dumps (d )
101
114
102
115
def unlock (self , password ):
@@ -146,7 +159,7 @@ def address(self):
146
159
if self ._address :
147
160
pass
148
161
elif 'address' in self .keystore :
149
- self ._address = self .keystore ['address' ]. decode ( 'hex' )
162
+ self ._address = decode_hex ( self .keystore ['address' ])
150
163
elif not self .locked :
151
164
self ._address = keys .privtoaddr (self .privkey )
152
165
else :
@@ -187,7 +200,7 @@ def sign_tx(self, tx):
187
200
188
201
def __repr__ (self ):
189
202
if self .address is not None :
190
- address = self .address . encode ( 'hex' )
203
+ address = encode_hex ( self .address )
191
204
else :
192
205
address = '?'
193
206
return '<Account(address={address}, id={id})>' .format (address = address , id = self .uuid )
@@ -257,7 +270,9 @@ def coinbase(self):
257
270
return DEFAULT_COINBASE
258
271
cb = self .accounts_with_address [0 ].address
259
272
else :
260
- if not is_string (cb_hex ):
273
+ # [NOTE]: check it!
274
+ # if not is_string(cb_hex):
275
+ if not isinstance (cb_hex , str ):
261
276
raise ValueError ('coinbase must be string' )
262
277
try :
263
278
cb = decode_hex (remove_0x_head (cb_hex ))
@@ -305,8 +320,9 @@ def add_account(self, account, store=True, include_address=True, include_id=True
305
320
errno = e .errno )
306
321
raise
307
322
self .accounts .append (account )
308
- self .accounts .sort (key = lambda account : account .path )
309
-
323
+ min_value = MinType ()
324
+ self .accounts .sort (key = lambda account : min_value if account .path is None else account .path )
325
+
310
326
def update_account (self , account , new_password , include_address = True , include_id = True ):
311
327
"""Replace the password of an account.
312
328
@@ -424,7 +440,7 @@ def find(self, identifier):
424
440
except ValueError :
425
441
pass
426
442
else :
427
- return self .get_by_id (str ( uuid ) )
443
+ return self .get_by_id (uuid . hex )
428
444
429
445
try :
430
446
index = int (identifier , 10 )
@@ -441,7 +457,7 @@ def find(self, identifier):
441
457
if identifier [:2 ] == '0x' :
442
458
identifier = identifier [2 :]
443
459
try :
444
- address = identifier . decode ( 'hex' )
460
+ address = decode_hex ( identifier )
445
461
except TypeError :
446
462
success = False
447
463
else :
@@ -480,16 +496,16 @@ def get_by_address(self, address):
480
496
assert len (address ) == 20
481
497
accounts = [account for account in self .accounts if account .address == address ]
482
498
if len (accounts ) == 0 :
483
- raise KeyError ('account with address {} not found' .format (address . encode ( 'hex' )))
499
+ raise KeyError ('account with address {} not found' .format (encode_hex ( address )))
484
500
elif len (accounts ) > 1 :
485
- log .warning ('multiple accounts with same address found' , address = address . encode ( 'hex' ))
501
+ log .warning ('multiple accounts with same address found' , address = encode_hex ( address ))
486
502
return accounts [0 ]
487
503
488
504
def sign_tx (self , address , tx ):
489
505
self .get_by_address (address ).sign_tx (tx )
490
506
491
507
def propose_path (self , address ):
492
- return os .path .join (self .keystore_dir , address . encode ( 'hex' ))
508
+ return os .path .join (self .keystore_dir , encode_hex ( address ))
493
509
494
510
def __contains__ (self , address ):
495
511
assert len (address ) == 20
0 commit comments