Permalink
Browse files

accounts, tests

  • Loading branch information...
jmunsch
jmunsch committed Sep 24, 2017
1 parent 31aea47 commit 34e96c8e285365321ea8a2c5948feac6606eb95e
Showing with 137 additions and 4 deletions.
  1. +26 −0 Makefile
  2. +19 −4 saffron/accounts.py
  3. +92 −0 tests/test_accounts.py
View
@@ -0,0 +1,26 @@
.PHONY: clean-pyc clean-build docs
help:
@echo "clean-build - remove build artifacts"
@echo "clean-pyc - remove Python file artifacts"
@echo "release - package and upload a release"
@echo "sdist - package"
clean: clean-build clean-pyc
clean-build:
rm -fr build/
rm -fr dist/
rm -fr *.egg-info
clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
release: clean
python setup.py sdist bdist bdist_wheel upload
sdist: clean
python setup.py sdist bdist bdist_wheel
ls -l dist
View
@@ -46,10 +46,23 @@ class Account:
_address (str): chain address.
_name (str): name of token/chain.
Ethereum methods :
self.p.importRawKey
self.p.newAccount
self.p.listAccounts
self.p.getListAccounts
self.p.sendTransaction
self.p.signAndSendTransaction
self.p.lockAccount
self.p.unlockAccount
self.p.sign
self.p.ecRecover
'''
def __init__(self, name=None, address=None, password=None, chain=None):
'''initialize the class
TODO : document chain ()
TODO : salt passwords with bcrypt or better
Args:
_address (str): chain address.
_name (str): name of token/chain.
@@ -66,13 +79,15 @@ def __init__(self, name=None, address=None, password=None, chain=None):
self.name = _name
self.address = _address
self._new_account = False
node_info = json.loads(open(os.environ['NODE_INFO_JSON']).read())
self.web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:{port}".format(port=node_info.get('rpcport'))))
self.p = Personal(self.web3)
@classmethod
def _from_db(self, name=None, address=None):
return
#TODO
#fancy shit making interacting with the blockchain easy (get balance, transact, etc)
def balance(self):
return Eth.get_balance(self.address)
return Eth.get_balance(self.address)
View
@@ -1,4 +1,10 @@
import pytest, os
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import re
import socket
from threading import Thread
from saffron.accounts import Account
from saffron.genesis import Chain
from saffron.database import insert_account
@@ -26,6 +32,39 @@ def read(self, *args, **kwargs):
# monkeypatch.setattr( 'some.Example', Example)
return Chain()
class MockServerRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
return
def do_POST(self, *args, **kwargs):
content_len = int(self.headers.get('content-length'))
post_body = self.rfile.read(content_len)
if post_body == b'{"jsonrpc": "2.0", "method": "personal_newAccount", "params": ["doesnt_matter"], "id": 0}':
self.send_response(200)
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
response_content = json.dumps({"result": ''})
self.wfile.write(response_content.encode('utf-8'))
return
def get_free_port():
s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
s.bind(('127.0.0.1', 8545))
address, port = s.getsockname()
s.close()
return port
def start_mock_server(port):
mock_server = HTTPServer(('127.0.0.1', port), MockServerRequestHandler)
mock_server_thread = Thread(target=mock_server.serve_forever)
mock_server_thread.setDaemon(True)
mock_server_thread.start()
start_mock_server(8545)
def test_accounts(chain):
new_account = str(uuid.uuid1())
a = Account(name=new_account, password='testing123', chain=chain)
@@ -43,3 +82,56 @@ def test_account_stored_in_db_when_created():
assert a.name == b.name
assert a._new_account
# assert b._new_account is False
# def test_importRawKey(chain):
# new_account = str(uuid.uuid1())
# p = 'doesnt_matter'
# a = Account(name=new_account, password=p, chain=Chain())
# private_key = 'asdf'
# a.p.importRawKey(private_key, p)
def test_newAccount(chain):
new_account = str(uuid.uuid1())
p = 'doesnt_matter'
a = Account(name=new_account, password=p, chain=Chain())
a.p.newAccount(password=p)
def test_listAccounts(chain):
new_account = str(uuid.uuid1())
a = Account(name=new_account, password='doesnt_matter', chain=Chain())
a.p.listAccounts()
# def test_getListAccounts(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.getListAccounts(*args, **kwargs)
# def test_sendTransaction(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.sendTransaction(transaction, passphrase)
# def test_signAndSendTransaction(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.signAndSendTransaction()
# def test_lockAccount(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.unlockAccount(account, passphrase, duration=None)
# def test_unlockAccount(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.unlockAccount()
# def test_sign(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.sign(message, signer, passphrase)
# def test_ecRecover(chain):
# new_account = str(uuid.uuid1())
# a = Account(name=new_account, password='doesnt_matter', chain=Chain())
# a.p.ecRecover(self, message, signature)

0 comments on commit 34e96c8

Please sign in to comment.