Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bci.py #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions bitcoin/bci.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,21 +499,35 @@ def get_block_height(txhash):
j = json.loads(make_request('https://blockchain.info/rawtx/'+txhash).decode("utf-8"))
return j['block_height']

# fromAddr, toAddr, 12345, changeAddress

# fromAddr OR txid:index, toAddr, 12345, changeAddress
def get_tx_composite(inputs, outputs, output_value, change_address=None, network=None):
"""mktx using blockcypher API"""
"""use blockcypher API to composite a Tx"""
inputs = [inputs] if not isinstance(inputs, list) else inputs
outputs = [outputs] if not isinstance(outputs, list) else outputs
network = set_network(change_address or inputs) if not network else network.lower()
url = "http://api.blockcypher.com/v1/btc/{network}/txs/new?includeToSignTx=true".format(
network=('test3' if network=='testnet' else 'main'))
url = "http://api.blockcypher.com/v1/btc/{network}/txs/new?includeToSignTx=true".format(\
network=('test3' if network=='testnet' else 'main'))
is_address = lambda a: bool(re.match("^[123mn][a-km-zA-HJ-NP-Z0-9]{26,33}$", a))
if any([is_address(x) for x in inputs]):
inputs_type = 'addresses' # also accepts UTXOs, only addresses supported presently
if any([is_address(x) for x in inputs]): # inputs as addresses
inputs_type = 'addresses'
elif any(filter(lambda s: ":" in s, inputs)): # inputs as utxo:idx
inputs_type = 'utxos'
ins = []
for i in inputs:
ins.append({
"prev_hash": i[:i.find(":")],
"output_index": int(i[i.find(":")+1:])
})
inputs = ins[:]
elif any([is_address(x) for x in inputs]) and any(filter(lambda s: ":" in s, inputs)):
raise Exception("Inputs takes EITHER address or 'utxo:index'")
if any([is_address(x) for x in outputs]):
outputs_type = 'addresses' # TODO: add UTXO support
outputs_type = 'addresses'
else:
raise Exception("Output must be an address")
data = {
'inputs': [{inputs_type: inputs}],
'inputs': [{"addresses": inputs}] if inputs_type == "addresses" else inputs,
'confirmations': 0,
'preference': 'high',
'outputs': [{outputs_type: outputs, "value": output_value}]
Expand All @@ -523,6 +537,6 @@ def get_tx_composite(inputs, outputs, output_value, change_address=None, network
jdata = json.loads(make_request(url, data))
hash, txh = jdata.get("tosign")[0], jdata.get("tosign_tx")[0]
assert bin_dbl_sha256(txh.decode('hex')).encode('hex') == hash, "checksum mismatch %s" % hash
return txh.encode("utf-8")
return txh[:-8].encode()

blockcypher_mktx = get_tx_composite