Skip to content
This repository has been archived by the owner on Aug 8, 2018. It is now read-only.

Commit

Permalink
Merge cd6e700 into fe206b4
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Oct 20, 2017
2 parents fe206b4 + cd6e700 commit 4f7e81c
Show file tree
Hide file tree
Showing 35 changed files with 659 additions and 368 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ omit =
*/examples/*
*/tests/*

[report]
exclude_lines =
pragma: no cover
def __repr__
Expand Down
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ sudo: required
dist: trusty
python:
- '2.7'
- '3.6'
before_install:
- sudo add-apt-repository -y ppa:ethereum/ethereum
- sudo apt-get update
- sudo apt-get install -y solc
install:
- pip install tox-travis
- USE_PYETHEREUM_DEVELOP=1 python setup.py install
- pip install coveralls readme_renderer
script:
- coverage run --source pyethapp setup.py test
- python setup.py check --restructuredtext --strict
- tox
after_success:
- coveralls
env:
Expand Down
5 changes: 2 additions & 3 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pytest==2.9.1
pytest
mock==2.0.0
pytest-mock==1.6.0
ethereum-serpent>=1.8.1
pytest==2.9.1
coverage==4.0.3
ethereum-serpent
48 changes: 32 additions & 16 deletions pyethapp/accounts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from builtins import hex
from builtins import object
import json
import os
from random import SystemRandom
Expand All @@ -7,10 +9,14 @@
from ethereum.tools import keys
from ethereum.slogging import get_logger
from ethereum.utils import privtopub # this is different than the one used in devp2p.crypto
from ethereum.utils import sha3, is_string, decode_hex, remove_0x_head
from ethereum.utils import sha3, is_string, encode_hex, remove_0x_head, to_string
from rlp.utils import decode_hex

from pyethapp.utils import MinType

log = get_logger('accounts')

DEFAULT_COINBASE = 'de0b295669a9fd93d5f28d9ec85e40f4cb697bae'.decode('hex')
DEFAULT_COINBASE = decode_hex('de0b295669a9fd93d5f28d9ec85e40f4cb697bae')

random = SystemRandom()

Expand All @@ -22,7 +28,7 @@ def mk_privkey(seed):
def mk_random_privkey():
k = hex(random.getrandbits(256))[2:-1].zfill(64)
assert len(k) == 64
return k.decode('hex')
return decode_hex(k)


class Account(object):
Expand All @@ -38,7 +44,7 @@ class Account(object):
def __init__(self, keystore, password=None, path=None):
self.keystore = keystore
try:
self._address = self.keystore['address'].decode('hex')
self._address = decode_hex(self.keystore['address'])
except KeyError:
self._address = None
self.locked = True
Expand All @@ -61,6 +67,13 @@ def new(cls, password, key=None, uuid=None, path=None):
"""
if key is None:
key = mk_random_privkey()

# [NOTE]: key and password should be bytes
if not is_string(key):
key = to_string(key)
if not is_string(password):
password = to_string(password)

keystore = keys.make_keystore_json(key, password)
keystore['id'] = uuid
return Account(keystore, password, path)
Expand Down Expand Up @@ -94,9 +107,9 @@ def dump(self, include_address=True, include_id=True):
d['crypto'] = self.keystore['crypto']
d['version'] = self.keystore['version']
if include_address and self.address is not None:
d['address'] = self.address.encode('hex')
d['address'] = encode_hex(self.address)
if include_id and self.uuid is not None:
d['id'] = self.uuid
d['id'] = str(self.uuid)
return json.dumps(d)

def unlock(self, password):
Expand Down Expand Up @@ -146,7 +159,7 @@ def address(self):
if self._address:
pass
elif 'address' in self.keystore:
self._address = self.keystore['address'].decode('hex')
self._address = decode_hex(self.keystore['address'])
elif not self.locked:
self._address = keys.privtoaddr(self.privkey)
else:
Expand Down Expand Up @@ -187,7 +200,7 @@ def sign_tx(self, tx):

def __repr__(self):
if self.address is not None:
address = self.address.encode('hex')
address = encode_hex(self.address)
else:
address = '?'
return '<Account(address={address}, id={id})>'.format(address=address, id=self.uuid)
Expand Down Expand Up @@ -257,7 +270,9 @@ def coinbase(self):
return DEFAULT_COINBASE
cb = self.accounts_with_address[0].address
else:
if not is_string(cb_hex):
# [NOTE]: check it!
# if not is_string(cb_hex):
if not isinstance(cb_hex, str):
raise ValueError('coinbase must be string')
try:
cb = decode_hex(remove_0x_head(cb_hex))
Expand Down Expand Up @@ -305,8 +320,9 @@ def add_account(self, account, store=True, include_address=True, include_id=True
errno=e.errno)
raise
self.accounts.append(account)
self.accounts.sort(key=lambda account: account.path)

min_value = MinType()
self.accounts.sort(key=lambda account: min_value if account.path is None else account.path)

def update_account(self, account, new_password, include_address=True, include_id=True):
"""Replace the password of an account.
Expand Down Expand Up @@ -424,7 +440,7 @@ def find(self, identifier):
except ValueError:
pass
else:
return self.get_by_id(str(uuid))
return self.get_by_id(uuid.hex)

try:
index = int(identifier, 10)
Expand All @@ -441,7 +457,7 @@ def find(self, identifier):
if identifier[:2] == '0x':
identifier = identifier[2:]
try:
address = identifier.decode('hex')
address = decode_hex(identifier)
except TypeError:
success = False
else:
Expand Down Expand Up @@ -480,16 +496,16 @@ def get_by_address(self, address):
assert len(address) == 20
accounts = [account for account in self.accounts if account.address == address]
if len(accounts) == 0:
raise KeyError('account with address {} not found'.format(address.encode('hex')))
raise KeyError('account with address {} not found'.format(encode_hex(address)))
elif len(accounts) > 1:
log.warning('multiple accounts with same address found', address=address.encode('hex'))
log.warning('multiple accounts with same address found', address=encode_hex(address))
return accounts[0]

def sign_tx(self, address, tx):
self.get_by_address(address).sign_tx(tx)

def propose_path(self, address):
return os.path.join(self.keystore_dir, address.encode('hex'))
return os.path.join(self.keystore_dir, encode_hex(address))

def __contains__(self, address):
assert len(address) == 20
Expand Down

0 comments on commit 4f7e81c

Please sign in to comment.