Skip to content

Commit

Permalink
removed authentication nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
F483 committed Aug 19, 2015
1 parent 6e14ea9 commit 7c9985b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
21 changes: 10 additions & 11 deletions dataserv/Farmer.py
Expand Up @@ -19,7 +19,6 @@ class Farmer(db.Model):
btc_addr = db.Column(db.String(35), unique=True)
last_seen = db.Column(DateTime, default=datetime.utcnow)
height = db.Column(db.Integer, default=0)
authentication_nonce = db.Column(db.Integer, default=0)

def __init__(self, btc_addr, last_seen=None):
"""
Expand All @@ -34,18 +33,19 @@ def __init__(self, btc_addr, last_seen=None):
def __repr__(self):
return '<Farmer BTC Address: %r>' % self.btc_addr

def authenticate(self, btc_addr, signature):
farmer = self.lookup()
def get_server_address(self):
return app.config["ADDRESS"]

def authenticate(self, signature, timestamp):
# FIXME validate timestamp

# verify signature
message = app.config["ADDRESS"] + "-" + farmer.authentication_nonce
data = binascii.hexlify(message)
if not BtcTxStore().verify_signature(address, signature, data):
message = self.get_server_address() + "-" + timestamp
data = binascii.hexlify(message.encode("utf-8"))
if not BtcTxStore().verify_signature(self.btc_addr, signature, data):
raise ValueError("Invalid signature!")
return True

# increment authentication nonce
farmer.authentication_nonce = authentication_nonce + 1
db.session.commit()

def is_btc_address(self):
"""Check if the address is a valid Bitcoin public key."""
Expand Down Expand Up @@ -115,7 +115,6 @@ def to_json(self):
payload = {
"btc_addr": self.btc_addr,
"last_seen": (datetime.utcnow() - self.last_seen).seconds,
"height": self.height,
"authentication_nonce": self.authentication_nonce,
"height": self.height
}
return json.dumps(payload)
17 changes: 16 additions & 1 deletion tests/test_Farmer.py
@@ -1,5 +1,7 @@
import json
import unittest
import binascii
from btctxstore import BtcTxStore
from dataserv.app import db
from dataserv.Farmer import sha256
from dataserv.Farmer import Farmer
Expand Down Expand Up @@ -100,8 +102,21 @@ def test_to_json(self):
farmer.ping()
farmer.set_height(50)

test_payload = u'{"authentication_nonce": 0, "height": 50, "btc_addr": "191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc", "last_seen": 0}'
test_payload = u'{"height": 50, "btc_addr": "191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc", "last_seen": 0}'
test_json = json.loads(test_payload)
call_payload = json.loads(farmer.to_json())
self.assertEqual(test_json, call_payload)

def test_authentication_success(self):
blockchain = BtcTxStore()
wif = blockchain.create_key()
address = blockchain.get_address(wif)
farmer = Farmer(address)

# first authentication
timestamp = "TODO timestamp"
message = farmer.get_server_address() + "-" + timestamp
data = binascii.hexlify(message.encode('utf-8'))
signature = blockchain.sign_data(wif, data)
self.assertTrue(farmer.authenticate(signature, timestamp))

0 comments on commit 7c9985b

Please sign in to comment.