1+ from builtins import hex
2+ from builtins import object
13import json
24import os
35from random import SystemRandom
79from ethereum .tools import keys
810from ethereum .slogging import get_logger
911from 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+
1117log = get_logger ('accounts' )
1218
13- DEFAULT_COINBASE = 'de0b295669a9fd93d5f28d9ec85e40f4cb697bae' . decode ( 'hex ' )
19+ DEFAULT_COINBASE = decode_hex ( 'de0b295669a9fd93d5f28d9ec85e40f4cb697bae ' )
1420
1521random = SystemRandom ()
1622
@@ -22,7 +28,7 @@ def mk_privkey(seed):
2228def mk_random_privkey ():
2329 k = hex (random .getrandbits (256 ))[2 :- 1 ].zfill (64 )
2430 assert len (k ) == 64
25- return k . decode ( 'hex' )
31+ return decode_hex ( k )
2632
2733
2834class Account (object ):
@@ -38,7 +44,7 @@ class Account(object):
3844 def __init__ (self , keystore , password = None , path = None ):
3945 self .keystore = keystore
4046 try :
41- self ._address = self .keystore ['address' ]. decode ( 'hex' )
47+ self ._address = decode_hex ( self .keystore ['address' ])
4248 except KeyError :
4349 self ._address = None
4450 self .locked = True
@@ -61,6 +67,13 @@ def new(cls, password, key=None, uuid=None, path=None):
6167 """
6268 if key is None :
6369 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+
6477 keystore = keys .make_keystore_json (key , password )
6578 keystore ['id' ] = uuid
6679 return Account (keystore , password , path )
@@ -94,9 +107,9 @@ def dump(self, include_address=True, include_id=True):
94107 d ['crypto' ] = self .keystore ['crypto' ]
95108 d ['version' ] = self .keystore ['version' ]
96109 if include_address and self .address is not None :
97- d ['address' ] = self .address . encode ( 'hex' )
110+ d ['address' ] = encode_hex ( self .address )
98111 if include_id and self .uuid is not None :
99- d ['id' ] = self .uuid
112+ d ['id' ] = str ( self .uuid )
100113 return json .dumps (d )
101114
102115 def unlock (self , password ):
@@ -146,7 +159,7 @@ def address(self):
146159 if self ._address :
147160 pass
148161 elif 'address' in self .keystore :
149- self ._address = self .keystore ['address' ]. decode ( 'hex' )
162+ self ._address = decode_hex ( self .keystore ['address' ])
150163 elif not self .locked :
151164 self ._address = keys .privtoaddr (self .privkey )
152165 else :
@@ -187,7 +200,7 @@ def sign_tx(self, tx):
187200
188201 def __repr__ (self ):
189202 if self .address is not None :
190- address = self .address . encode ( 'hex' )
203+ address = encode_hex ( self .address )
191204 else :
192205 address = '?'
193206 return '<Account(address={address}, id={id})>' .format (address = address , id = self .uuid )
@@ -257,7 +270,9 @@ def coinbase(self):
257270 return DEFAULT_COINBASE
258271 cb = self .accounts_with_address [0 ].address
259272 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 ):
261276 raise ValueError ('coinbase must be string' )
262277 try :
263278 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
305320 errno = e .errno )
306321 raise
307322 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+
310326 def update_account (self , account , new_password , include_address = True , include_id = True ):
311327 """Replace the password of an account.
312328
@@ -424,7 +440,7 @@ def find(self, identifier):
424440 except ValueError :
425441 pass
426442 else :
427- return self .get_by_id (str ( uuid ) )
443+ return self .get_by_id (uuid . hex )
428444
429445 try :
430446 index = int (identifier , 10 )
@@ -441,7 +457,7 @@ def find(self, identifier):
441457 if identifier [:2 ] == '0x' :
442458 identifier = identifier [2 :]
443459 try :
444- address = identifier . decode ( 'hex' )
460+ address = decode_hex ( identifier )
445461 except TypeError :
446462 success = False
447463 else :
@@ -480,16 +496,16 @@ def get_by_address(self, address):
480496 assert len (address ) == 20
481497 accounts = [account for account in self .accounts if account .address == address ]
482498 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 )))
484500 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 ))
486502 return accounts [0 ]
487503
488504 def sign_tx (self , address , tx ):
489505 self .get_by_address (address ).sign_tx (tx )
490506
491507 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 ))
493509
494510 def __contains__ (self , address ):
495511 assert len (address ) == 20
0 commit comments