forked from OpenBazaar/OpenBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trust.py
112 lines (77 loc) · 2.79 KB
/
trust.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import obelisk
import logging
import pybitcointools
from threading import Thread
# from twisted.internet import reactor
_log = logging.getLogger('trust')
TESTNET = False
# OBELISK_SERVER_TESTNET = "tcp://obelisk-testnet2.airbitz.co:9091"
# OBELISK_SERVER_MAINNET = "tcp://obelisk.bysh.me:9091"
# def build_output_info_list(unspent_rows):
# unspent_infos = []
# for row in unspent_rows:
# assert len(row) == 4
# outpoint = obelisk.OutPoint()
# outpoint.hash = row[0]
# outpoint.index = row[1]
# value = row[3]
# unspent_infos.append(
# obelisk.OutputInfo(outpoint, value))
# return unspent_infos
def burnaddr_from_guid(guid_hex):
_log.debug("burnaddr_from_guid: %s" % guid_hex)
if TESTNET:
guid_hex = '6f' + guid_hex
else:
guid_hex = '00' + guid_hex
_log.debug("GUID address on bitcoin net: %s" % guid_hex)
guid = guid_hex.decode('hex')
_log.debug("Decoded GUID address on bitcoin net")
# perturbate GUID
# to ensure unspendability through
# near-collision resistance of SHA256
# by flipping the last non-checksum bit of the address
guid = guid[:-1] + chr(ord(guid[-1]) ^ 1)
_log.debug("Perturbated bitcoin proof-of-burn address")
return obelisk.bitcoin.EncodeBase58Check(guid)
def get_global(guid, callback):
get_unspent(burnaddr_from_guid(guid), callback)
def get_unspent(addr, callback):
_log.debug('get_unspent call')
# def history_fetched(ec, history):
# _log.debug('History fetched')
# if ec is not None:
# _log.debug('Error fetching history: ', ec)
# return
# unspent_rows = [row[:4] for row in history if row[4] is None]
# unspent = build_output_info_list(unspent_rows)
# unspent = obelisk.select_outputs(unspent, 10000)
# if unspent is None:
# callback(0)
# return
# points = unspent.points
# if len(points) != 1:
# callback(0)
# return
# point = points[0]
# value = point.value
# callback(value)
# if TESTNET:
# obelisk_addr = OBELISK_SERVER_TESTNET
# else:
# obelisk_addr = OBELISK_SERVER_MAINNET
# _log.debug('unspent query to obelisk server at %s' % obelisk_addr)
# client = obelisk.ObeliskOfLightClient(obelisk_addr)
# _log.debug('Obelisk client instantiated')
# def get_history():
# _log.debug("get_history called from thread")
# client.fetch_history(addr, history_fetched)
# reactor.callFromThread(get_history)
def get_history():
history = pybitcointools.history(addr)
total = 0
for tx in history:
total += tx['value']
callback(total)
t = Thread(target=get_history)
t.start()