-
Notifications
You must be signed in to change notification settings - Fork 23
/
chain.py
67 lines (53 loc) · 2 KB
/
chain.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
# Copyright 2020 by Coinkite Inc. This file is covered by license found in COPYING-CC.
#
# chain.py --- API to blockstream stuff
#
import sys, os, asyncio, logging, requests
from objstruct import ObjectStruct
from persist import settings
from status import STATUS
from binascii import b2a_hex, a2b_hex
from utils import json_loads
logging.getLogger(__name__).addHandler(logging.NullHandler())
def broadcast_txn(txn):
# take bytes and get them shared over P2P to the world
# - raise w/ text about what happened if it fails
# - limited docs: <https://github.com/Blockstream/esplora/blob/master/API.md>
ses = requests.session()
ses.proxies = dict(http=settings.TOR_SOCKS)
ses.headers.clear() # hide user-agent
url = settings.EXPLORA
url += '/api/tx' if not STATUS.is_testnet else '/testnet/api/tx'
assert '.onion/' in url, 'dude, your privacy'
logging.warning(f"Sending txn via: {url}")
resp = ses.post(url, data=b2a_hex(txn).decode('ascii'))
msg = resp.text
if not resp.ok:
# content is like:
# sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed"}
# which is a text thing, including some JSON from bitcoind?
if '"message":' in msg:
try:
prefix, rest = msg.split(': ', 1)
j = json_loads(rest)
if prefix == 'sendrawtransaction RPC error':
msg = j.message
else:
msg = prefix + ': ' + j.message
except:
pass
msg = f"Transaction broadcast FAILED: {msg}"
logging.error(msg)
return msg
# untested
msg = f"Transaction broadcast success: {msg}"
logging.info(msg)
return msg
def link_to_txn(txn_hash):
path = '/tx/' if not STATUS.is_testnet else '/testnet/tx/'
assert len(txn_hash) == 64
return settings.EXPLORA + path + txn_hash
if __name__ == '__main__':
# test code
r = broadcast_txn(b'sdhffkhjkdfshdfshjdfshdfkshdfs')
# EOF