Skip to content

Commit

Permalink
added farmer authentication method
Browse files Browse the repository at this point in the history
  • Loading branch information
F483 committed Aug 19, 2015
1 parent 1576922 commit 6e14ea9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
22 changes: 19 additions & 3 deletions dataserv/Farmer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import hashlib
from dataserv.run import db
import binascii
from dataserv.run import db, app
from datetime import datetime
from sqlalchemy import DateTime
from btctxstore import BtcTxStore
from dataserv.Validator import is_btc_address


Expand All @@ -17,7 +19,7 @@ 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)
last_message_hash = db.Column(db.String(64), unique=True) # sha256sum
authentication_nonce = db.Column(db.Integer, default=0)

def __init__(self, btc_addr, last_seen=None):
"""
Expand All @@ -32,6 +34,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()

# verify signature
message = app.config["ADDRESS"] + "-" + farmer.authentication_nonce
data = binascii.hexlify(message)
if not BtcTxStore().verify_signature(address, signature, data):
raise ValueError("Invalid signature!")

# 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."""
return is_btc_address(self.btc_addr)
Expand Down Expand Up @@ -100,6 +115,7 @@ def to_json(self):
payload = {
"btc_addr": self.btc_addr,
"last_seen": (datetime.utcnow() - self.last_seen).seconds,
"height": self.height
"height": self.height,
"authentication_nonce": self.authentication_nonce,
}
return json.dumps(payload)
2 changes: 2 additions & 0 deletions dataserv/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

DATA_DIR = 'data/'
BYTE_SIZE = 1024*1024*128 # 128 MB

ADDRESS = "16ZcxFDdkVJR1P8GMNmWFyhS4EKrRMsWNG" # unique per server address
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Flask == 0.10.1
Flask-SQLAlchemy == 2.0
RandomIO == 0.2.1
partialhash == 1.1.0
btctxstore == 4.1.2
2 changes: 1 addition & 1 deletion tests/test_Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_to_json(self):
farmer.ping()
farmer.set_height(50)

test_payload = u'{"height": 50, "btc_addr": "191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc", "last_seen": 0}'
test_payload = u'{"authentication_nonce": 0, "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)
Expand Down

0 comments on commit 6e14ea9

Please sign in to comment.