Skip to content

Commit d5f080b

Browse files
committed
updated python-obelisk for changed to libbitcoin-server wire format.
1 parent 2e7a67d commit d5f080b

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

examples/chain-test.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ def txhashes_fetched(ec, hashes):
2121
def height_fetched(ec, height):
2222
print "Height:", height
2323

24+
def history_fetched(ec, history):
25+
print "History:"
26+
for id, hash, index, height, value in history:
27+
print id, hash.encode("hex") + ":" + str(index), height, value
2428

2529
def stealth_fetched(ec, result):
2630
print "Stealth:"
2731
for ephemkey, address, tx_hash in result:
28-
print " ephemkey:", ephemkey.encode("hex"), "address:", address, \
32+
print " ephemkey:", ephemkey.encode("hex"), \
33+
"address:", address.encode("hex"), \
2934
"tx_hash:", tx_hash.encode("hex")
3035

3136

3237
if __name__ == '__main__':
33-
client = obelisk.ObeliskOfLightClient("tcp://85.25.198.97:9091")
38+
client = obelisk.ObeliskOfLightClient("tcp://localhost:9091")
3439
tx_hash = "e9a66845e05d5abc0ad04ec80f774a7e" \
3540
"585c6e8db975962d069a522137b80c1d".decode("hex")
3641
client.fetch_transaction(tx_hash, tx_fetched)
@@ -40,10 +45,14 @@ def stealth_fetched(ec, result):
4045
outpoint.index = 0
4146
client.fetch_spend(outpoint, spend_fetched)
4247
client.fetch_transaction_index(outpoint.hash, txidx_fetched)
43-
client.fetch_block_transaction_hashes(100000, txhashes_fetched)
48+
client.fetch_block_transaction_hashes(tx_hash, txhashes_fetched)
4449
blk_hash = "000000000003ba27aa200b1cecaad478" \
4550
"d2b00432346c3f1f3986da1afd33e506".decode("hex")
4651
client.fetch_block_height(blk_hash, height_fetched)
4752
client.fetch_last_height(height_fetched)
48-
client.fetch_stealth((2, 1763505291), stealth_fetched)
53+
client.fetch_history("13cbkpKW2DdhEUmeGC2h9HyxbBdtc7Wcth", history_fetched)
54+
# You can compare with blockchain db using libbitcoin-blockchain/tools/
55+
# $ stealth_db scan stealth_index stealth_rows 101010101 0
56+
client.fetch_stealth("101010101", stealth_fetched)
4957
reactor.run()
58+

examples/fetch_block_header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from twisted.internet import reactor
33

44
if __name__ == '__main__':
5-
c = obelisk.ObeliskOfLightClient("tcp://obelisk.unsystem.net:9091")
5+
c = obelisk.ObeliskOfLightClient("tcp://localhost:9091")
66
h = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
77

88
def cb(*args):

examples/query-blockchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def header_fetched(ec, header):
7070

7171

7272
if __name__ == '__main__':
73-
c = obelisk.ObeliskOfLightClient('tcp://85.25.198.97:8081')
73+
c = obelisk.ObeliskOfLightClient('tcp://localhost:9091')
7474
#c.fetch_last_height(print_height)
7575
#blk_hash = "000000000000000471988cc24941335b" \
7676
# "91d35d646971b7de682b4236dc691919".decode("hex")

obelisk/client.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ def pack_block_index(index):
2121
else:
2222
raise ValueError("Unknown index type")
2323

24+
def binary_str_to_bytes(str):
25+
split = lambda str: [str[x:x + 8] for x in range(0, len(str), 8)]
26+
add_padding = lambda str: str + ((8 - len(str)) * "0")
27+
result = []
28+
for bin_byte in split(str):
29+
bin_byte = add_padding(bin_byte)
30+
value = int(bin_byte, 2)
31+
assert value < 256
32+
result.append(value)
33+
return tuple(result)
2434

2535
class ObeliskOfLightClient(ClientBase):
2636
valid_messages = [
@@ -135,9 +145,9 @@ def fetch_transaction_index(self, tx_hash, cb):
135145
'blockchain.fetch_transaction_index', data, cb
136146
)
137147

138-
def fetch_block_transaction_hashes(self, index, cb):
148+
def fetch_block_transaction_hashes(self, tx_hash, cb):
139149
"""Fetches list of transaction hashes in a block by block hash."""
140-
data = pack_block_index(index)
150+
data = serialize.ser_hash(tx_hash)
141151
self.send_command(
142152
'blockchain.fetch_block_transaction_hashes', data, cb
143153
)
@@ -159,9 +169,12 @@ def fetch_stealth(self, prefix, cb, from_height=0):
159169
height, and may also include results from earlier blocks.
160170
It is provided as an optimisation. All results at and after
161171
from_height are guaranteed to be returned however."""
162-
number_bits, bitfield = prefix
163-
data = struct.pack('<BII', number_bits, bitfield, from_height)
164-
assert len(data) == 9
172+
values = binary_str_to_bytes(prefix)
173+
number_bits = len(prefix)
174+
data = struct.pack('<B', number_bits)
175+
for value in values:
176+
data += struct.pack('<B', value)
177+
data += struct.pack('<I', from_height)
165178
self.send_command('blockchain.fetch_stealth', data, cb)
166179

167180
# receive handlers
@@ -174,18 +187,15 @@ def _on_fetch_block_header(self, data):
174187
def _on_fetch_history(self, data):
175188
error = unpack_error(data)
176189
# parse results
177-
rows = self.unpack_table("<32sIIQ32sII", data, 4)
190+
rows = self.unpack_table("<B32sIIQ", data, 4)
178191
history = []
179-
for row in rows:
180-
o_hash, o_index, o_height, value, s_hash, s_index, s_height = row
181-
o_hash = o_hash[::-1]
182-
s_hash = s_hash[::-1]
183-
if s_index == 4294967295:
184-
s_hash = None
185-
s_index = None
186-
s_height = None
187-
history.append(
188-
(o_hash, o_index, o_height, value, s_hash, s_index, s_height))
192+
for id, hash, index, height, value in rows:
193+
if id == 0:
194+
id = False
195+
else:
196+
id = True
197+
hash = hash[::-1]
198+
history.append((id, hash, index, height, value))
189199
return (error, history)
190200

191201
def _on_fetch_last_height(self, data):
@@ -221,11 +231,11 @@ def _on_fetch_block_height(self, data):
221231

222232
def _on_fetch_stealth(self, data):
223233
error = unpack_error(data)
224-
raw_rows = self.unpack_table("<33sB20s32s", data, 4)
234+
raw_rows = self.unpack_table("<32s20s32s", data, 4)
225235
rows = []
226-
for ephemkey, address_version, address_hash, tx_hash in raw_rows:
227-
address = bitcoin.hash_160_to_bc_address(
228-
address_hash[::-1], address_version)
236+
for ephemkey, address, tx_hash in raw_rows:
237+
ephemkey = ephemkey[::-1]
238+
address = address[::-1]
229239
tx_hash = tx_hash[::-1]
230240
rows.append((ephemkey, address, tx_hash))
231241
return (error, rows)

0 commit comments

Comments
 (0)