@@ -498,3 +498,31 @@ def get_txs_in_block(inp):
498498def get_block_height (txhash ):
499499 j = json .loads (make_request ('https://blockchain.info/rawtx/' + txhash ).decode ("utf-8" ))
500500 return j ['block_height' ]
501+
502+ # fromAddr, toAddr, 12345, changeAddress
503+ def get_tx_composite (inputs , outputs , output_value , change_address = None , network = None ):
504+ """mktx using blockcypher API"""
505+ inputs = [inputs ] if not isinstance (inputs , list ) else inputs
506+ outputs = [outputs ] if not isinstance (outputs , list ) else outputs
507+ network = set_network (change_address or inputs ) if not network else network .lower ()
508+ url = "http://api.blockcypher.com/v1/btc/{network}/txs/new?includeToSignTx=true" .format (
509+ network = ('test3' if network == 'testnet' else 'main' ))
510+ is_address = lambda a : bool (re .match ("^[123mn][a-km-zA-HJ-NP-Z0-9]{26,33}$" , a ))
511+ if any ([is_address (x ) for x in inputs ]):
512+ inputs_type = 'addresses' # also accepts UTXOs, only addresses supported presently
513+ if any ([is_address (x ) for x in outputs ]):
514+ outputs_type = 'addresses' # TODO: add UTXO support
515+ data = {
516+ 'inputs' : [{inputs_type : inputs }],
517+ 'confirmations' : 0 ,
518+ 'preference' : 'high' ,
519+ 'outputs' : [{outputs_type : outputs , "value" : output_value }]
520+ }
521+ if change_address :
522+ data ["change_address" ] = change_address #
523+ jdata = json .loads (make_request (url , data ))
524+ hash , txh = jdata .get ("tosign" )[0 ], jdata .get ("tosign_tx" )[0 ]
525+ assert bin_dbl_sha256 (txh .decode ('hex' )).encode ('hex' ) == hash , "checksum mismatch %s" % hash
526+ return txh .encode ("utf-8" )
527+
528+ blockcypher_mktx = get_tx_composite
0 commit comments