Created
January 2, 2017 19:37
-
-
Save kumrzz/5ef7f620b24d2c68fa9310a2d289f881 to your computer and use it in GitHub Desktop.
various(4) BTC testnet transmit / propagate methods, all failed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
various(4) BTC testnet transmit / pushtx/ propagate methods, all failed :( | |
--------------------------------------- | |
sending using helloblock: | |
HTTPSConnectionPool(host='testnet.helloblock.io', port=443): Max retries exceeded with url: /v1/transactions (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x000000000475E860>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)) | |
--------------------------------------- | |
sending using blockcypher: | |
{'error': "Couldn't deserialize request: invalid character 'x' in literal true (expecting 'r')"} | |
--------------------------------------- | |
sending using blockcypher @ sessions: | |
{u'error': u"Couldn't deserialize request: invalid character 'x' in literal true (expecting 'r')"} | |
--------------------------------------- | |
sending using webbtc: | |
{u'error': u'Connection refused - connect(2) for "node" port 9999'} | |
--------------------------------------- | |
sending using pybitcointools: | |
{'status': 'fail', 'message': '', 'code': 500, 'data': 'Could not decode your transaction!'} | |
--------------------------------------- | |
""" | |
from pycoin.key.validate import is_address_valid | |
from pycoin.tx.tx_utils import create_signed_tx | |
from pycoin.convention import btc_to_satoshi | |
from pycoin.tx import Spendable | |
from urllib2 import urlopen | |
import json, pybitcointools | |
from pybitcointools import make_request | |
from pycoin.serialize import h2b, h2b_rev | |
from requests import session | |
import requests, ast | |
def helloblocksend(tx): | |
print 'sending using helloblock:' | |
helloblockurl = 'https://testnet.helloblock.io/v1/transactions' | |
payload = {'rawTxHex': tx.as_hex()} | |
try: | |
return requests.post(helloblockurl, data=payload).json() | |
except Exception, e: | |
errahmessage1 = e[0] | |
return errahmessage1 | |
def blockcyphersend(tx): | |
print 'sending using blockcypher:' | |
blockcypherurl = 'http://api.blockcypher.com/v1/btc/test3/txs/push' | |
try: | |
return json.loads(make_request(blockcypherurl, 'tx:%s' % tx.as_hex())) | |
except Exception, e: | |
errahmessage1 = ast.literal_eval(e[0]) | |
return errahmessage1 | |
def blockcypherREQsend(tx): | |
print 'sending using blockcypher @ sessions:' | |
blockcypherurl = 'http://api.blockcypher.com/v1/btc/test3/txs/push' | |
payload = {'tx': tx.as_hex(),} | |
return requests.post(blockcypherurl, data=payload).json() | |
def webbtcsend(tx): | |
print 'sending using webbtc:' | |
webbtc_url = 'http://test.webbtc.com/relay_tx.json' | |
payload = {'tx': tx.as_hex(),} | |
return requests.post(webbtc_url, data=payload).json() | |
def pybitcointoolssend(tx): | |
print 'sending using pybitcointools:' | |
try: | |
return pybitcointools.blockr_pushtx(tx.as_hex(), network=pbtctNet) | |
except Exception, e: | |
errahmessage1 = ast.literal_eval(e[0]) | |
return errahmessage1 | |
wif = 'cMuqTgLkdQtsCD25HXDaStwjuBjb4TmdkV26FPjubjUGvRe8VRmy' #cQp5i1edstsXqyd1vKNU2HJyyhYrD5TxaiNh3NncnwjV9Y9r8qNV | |
address = 'mtyC8FT3vossVqhXQfrzWQ2WEqjm1bwi2u' #mt8kNQiejsjuZWAn5hse3nESfWb4hoadRU | |
netcode = 'XTN' | |
dst_address = 'mt8kNQiejsjuZWAn5hse3nESfWb4hoadRU' | |
if is_address_valid(address): | |
print("address ok! :)") | |
else: | |
print("invalid address, please try again") | |
if netcode == "XTN": | |
URL = "https://testnet.blockexplorer.com/api/addrs/%s/utxo" % address | |
pbtctNet = 'testnet' | |
if netcode == "BTC": | |
URL = "https://blockexplorer.com/api/addrs/%s/utxo" % address | |
spendables = [] | |
r = json.loads(urlopen(URL).read().decode("utf8")) | |
for u in r: | |
coin_value = btc_to_satoshi(u.get("amount")) | |
script = h2b(u.get("scriptPubKey")) | |
previous_hash = h2b_rev(u.get("txid")) | |
previous_index = u.get("vout") | |
spendables.append(Spendable(coin_value, script, previous_hash, previous_index)) | |
print spendables | |
if spendables != []: | |
tx = create_signed_tx(spendables, payables=[dst_address], wifs=[wif], netcode=netcode) | |
print 'here is the signed output transaction (' + netcode + '): ' | |
print(tx.as_hex()) | |
print '---------------------------------------' | |
print helloblocksend(tx) | |
print '---------------------------------------' | |
print blockcyphersend(tx) | |
print '---------------------------------------' | |
print blockcypherREQsend(tx) | |
print '---------------------------------------' | |
print webbtcsend(tx) | |
print '---------------------------------------' | |
print pybitcointoolssend(tx) | |
print '---------------------------------------' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment