blockcypher / blockcypher-python

Python library for the BlockCypher web services
https://www.blockcypher.com/dev/bitcoin
Apache License 2.0
215 stars 123 forks source link

Question about Error "Script was NOT verified successfully." #100

Closed Jack-Valentine closed 3 years ago

Jack-Valentine commented 3 years ago

Hi blockcypher,

I'm a new developer on Blockchain env. I try to follow the python sdk document, but something is not working well. Can you check my code?

I try to find a problem myself, but I don't know well what is a problem. This code finally return "'error': 'Error validating generated transaction: Error running script for input 0 referencing 6f2c45ff6ddfffab158b5c50991d991c36f0f3eb3861132fd46f6fbcb4937808 at 1: Script was NOT verified successfully.'" message.

import bitcoin
from blockcypher import create_unsigned_tx
from blockcypher import make_tx_signatures
from blockcypher import broadcast_signed_transaction
from blockcypher import decodetx
from blockcypher import verify_unsigned_tx

my_api_key = '{my blockcypher api key}'

sender_wallet_address = '{sender's wallet address}'
receiver_wallet_address = '{receiver's wallet address}'
#it is a wallet public value when I make a new wallet with blockcypher API.
sender_wallet_public_key = '{sender's wallet public key}'
#it is a wallet private value when I make a new wallet with blockcypher API.
sender_wallet_private_key = '{sender's wallet private key}'
#satoshi
want_send_amount = 57022

currency_type = 'btc'

inputs = [{'address': sender_wallet_address}, ]
outputs = [{'address': receiver_wallet_address, 'value': want_send_amount}]

unsigned_tx = create_unsigned_tx(inputs=inputs, outputs=outputs, coin_symbol=currency_type, api_key=my_api_key, preference='low')
print('UnSigned')
print(unsigned_tx)

pubkey_list = [sender_wallet_public_key]
privkey_list = [sender_wallet_private_key]

tx_signatures = make_tx_signatures(txs_to_sign=unsigned_tx['tosign'], privkey_list=privkey_list, pubkey_list=pubkey_list)

print('Signatures')
print(tx_signatures)

decodetx = decodetx(tx_hex=tx_signatures[0], coin_symbol='btc', api_key=my_api_key)
print(decodetx)

broadcast = broadcast_signed_transaction(unsigned_tx=unsigned_tx, signatures=tx_signatures, pubkeys=pubkey_list, coin_symbol='btc', api_key=my_api_key)
print('Broadcast')
print(broadcast)

Thanks Jack

Jack-Valentine commented 3 years ago

I found a problem. And I want share that what is a problem. If your wallet contains bech32 (bc1) addresses, you''ll need to add 01 at the end of the signature.

tx_signatures[0] = tx_signatures[0] + '01'