-
Notifications
You must be signed in to change notification settings - Fork 78
/
api.py
56 lines (46 loc) · 1.71 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import webapp2, jinja2, os, hashlib, logging, urllib
import json
import datetime
from model import Document
from blockchain import get_txs_for_addr, publish_data,\
publish_data_old, new_address, callback_secret_valid
from base import JsonAPIHandler
from doc import DigestStoreHandler
from config import MIN_SATOSHIS_PAYMENT
class ExternalRegisterHandler(DigestStoreHandler):
def handle(self):
digest = self.request.get("d") # expects client-side hashing
if not digest or len(digest) != 64:
return {"success" : False, "reason" : "format"}
try:
digest.decode("hex")
except TypeError:
return {"success" : False, "reason" : "format"}
ret = self.store_digest(digest)
if not ret["success"]:
del ret["args"]
return ret
doc_dict = self.doc.to_dict()
pay_address = doc_dict.get('payment_address')
if not pay_address:
return {"success" : False, "reason" : "cant generate pay address"}
ret["pay_address"] = pay_address
ret["price"] = MIN_SATOSHIS_PAYMENT
return ret
class ExternalStatusHandler(JsonAPIHandler):
def handle(self):
digest = self.request.get("d")
doc = Document.get_doc(digest)
if not digest:
return {"success": False, "reason": "format"}
if not doc:
return {"success": False, "reason": "nonexistent"}
if doc.tx:
return {"success": True, "status": "confirmed", "transaction": doc.tx, "txstamp": doc.txstamp}
if doc.is_actionable():
return {"success": True, "status": "pending"}
return {"success": True, \
"status": "registered", \
"pay_address": doc.payment_address, \
"price": MIN_SATOSHIS_PAYMENT \
}