I’m trying to make a bitcoin transaction using the BlockCypher API and I’m getting an error when I try to sign the transaction.

This function creates an unsigned transaction:

def buildTransaction(fromAddress, toAddress, value = -1, feePreference = "low"): # 'feePreference' Can be high, medium or low. # 'value' in this transaction output, in satoshis. # 'value' will be set to -1 to sweep all value from input address data = { 'inputs': [{ "addresses": [fromAddress], }], 'outputs': [{ "addresses": [toAddress], "value": value }], "preference": feePreference } data = json.dumps(data) print(data) return blockcypherAPI.request('/txs/new', 'POST', data).json()

The function return object:

 { "tx": { "addresses": [ "CEztKBAYNoUEEaPYbkyFeXC5v8Jz9RoZH9", "C1rGdt7QEPGiwPMFhNKNhHmyoWpa5X92pn" ], "block_height": -1, "confidence": 0, "confirmations": 0, "double_spend": False, "fees": 142700, "hash": "afceb017a0d280a746ad5ed5c18228e302ebc2aa71ec768454b9a768f73c24c1", "inputs": [ { "addresses": [ "CEztKBAYNoUEEaPYbkyFeXC5v8Jz9RoZH9" ], "age": 163376, "output_index": 0, "output_value": 23455, "prev_hash": "457a538479fb50f3e060702594ef92962c2570d592b8e7c089d7b13fc9b5b3dc", "script": "", "script_type": "", "sequence": 4294967295 }, ..., ], "lock_time": 0, "outputs": [ { "addresses": [ "C1rGdt7QEPGiwPMFhNKNhHmyoWpa5X92pn" ], "script": "76a9145fb1af31edd2aa5a2bbaa24f6043d6ec31f7e63288ac", "script_type": "pay-to-pubkey-hash", "value": 1000000 }, ..., ], "preference": "high", "received": "2015-10-21T19:11:35.774722671Z", "relayed_by": ",", "size": 775, "vsize": 775, "total": 3773210, "ver": 1, "vin_sz": 17, "vout_sz": 2, "tosign": ['798097d46807966f214079ae99a4989b6b320befc0212f88779437962b2f2585'] } }

The problem starts in this function:

I need to use my private key to sign the data provided in the tosign array from what the previous function returned.

I am currently using the BlockCypher library to sign the

def signature(unsigned_tx, privKey): privkey_list = [privKey] pubkey_list = [unsigned_tx['tx']['inputs'][0]['addresses'][0]] #print(pubkey_list) return make_tx_signatures(txs_to_sign=unsigned_tx['tosign'], privkey_list=privkey_list, pubkey_list=pubkey_list)

The function returns the following error:

Traceback (most recent call last): File "/Users/user/PycharmProjects/blockcypherAPI/transaction.py", line 44, in <module> print(transaction(fromAddress= "CEztKBAYNoUEEaPYbkyFeXC5v8Jz9RoZH9", fromPriv= "{PK}", toAddress= "C1rGdt7QEPGiwPMFhNKNhHmyoWpa5X92pn", value= -1)) File "/Users/user/PycharmProjects/blockcypherAPI/transaction.py", line 41, in transaction sign = signature(unsignedTX, fromPriv) File "/Users/user/PycharmProjects/blockcypherAPI/transaction.py", line 35, in signature return make_tx_signatures(txs_to_sign=unsigned_tx['tosign'], privkey_list=privkey_list, pubkey_list=pubkey_list) File "/Users/user/PycharmProjects/blockcypherAPI/venv/lib/python3.10/site-packages/blockcypher/api.py", line 1596, in make_tx_signatures assert ecdsa_raw_verify(tx_to_sign, der_decode_sig(sig), pubkey_list[cnt]), err_msg File "/Users/user/PycharmProjects/blockcypherAPI/venv/lib/python3.10/site-packages/bitcoin/main.py", line 507, in ecdsa_raw_verify x, y = fast_add(fast_multiply(G, u1), fast_multiply(decode_pubkey(pub), u2)) File "/Users/user/PycharmProjects/blockcypherAPI/venv/lib/python3.10/site-packages/bitcoin/main.py", line 193, in decode_pubkey if not formt: formt = get_pubkey_format(pub) File "/Users/user/PycharmProjects/blockcypherAPI/venv/lib/python3.10/site-packages/bitcoin/main.py", line 174, in get_pubkey_format else: raise Exception("Pubkey not in recognized format")
Exception: Pubkey not in recognized format

In addition, I would like to understand how to perform the signature independently without the blockCypher library

Thanks for the help!

Leave a Reply

Your email address will not be published. Required fields are marked *