Friday, November 22, 2024

segregated witness – Error Validating Segwit Transactions utilizing Python Blockcypher API: Error Working Enter Script

Because the title states, I am attempting to ship a Litecoin transaction utilizing the Blockcypher API Python SDK, and utilizing Bitcoinlib for key administration. I’ve extensively reviewed the documentation and supply code for the instruments I am working with, and I’ve tried researching this particular error however nonetheless have but to get previous this roadblock.

I’ve gotten to some extent the place the transaction is constructed and and signatures are created, however after I strive broadcasting it, the API returns this error to my console:

{'errors': [{'error': 'Error validating generated transaction: 
Error running script for input 0 referencing 79d841bf41ead0e4ecbb58197c289ec9cc087e85cf1a98196e2e415b8990357a at 0:
Script was NOT verified successfully.'}], 

'tx': {'block_height': -1, 'block_index': -1, 
'hash': 'f2ae95498b3b209ee077ad8a32292a0461ab2d37b807a647520aee31e9c62d29', 
'addresses': ['ltc1q7nyvlc6jkznpemw3g0n8f3z5z4eatfhevy07au', 'ltc1qxdvtqt9tm7rwplk8upezujtjsh6k3l85tw39ue'], 
'whole': 98100, 
'charges': 1900, 
'dimension': 221, 
'vsize': 140, 
'choice': 'low', 
'relayed_by': '35.226.89.167', 
'obtained': '2023-07-05T08:53:48.287727164Z', 
'ver': 1, 
'double_spend': False,
'vin_sz': 1, 
'vout_sz': 2, 
'confirmations': 0, 
'inputs': [
    {'prev_hash': '79d841bf41ead0e4ecbb58197c289ec9cc087e85cf1a98196e2e415b8990357a',
     'output_index': 0, 
     'output_value': 100000,
     'sequence': 4294967295,
     'addresses': ['ltc1q7nyvlc6jkznpemw3g0n8f3z5z4eatfhevy07au'], 
     'script_type': 'pay-to-witness-pubkey-hash', 
     'age': 2502561,
     'witness': 
['3044022077515b45f26a56b0dfff42eb94b69d98dbf8fb896fda079ea7e99a17a768fd6f022008b3c435ee0816801e1eea22871cb935a7eaaa8df6ad472eab19b096fb62cd82'
, '034b3110d0be2b52a14c4bbccef285396c67c29586463f278a8d1efacb3c33f439']}], 
'outputs': [{
     'value': 10000, 
     'script': '00143358b02cabdf86e0fec7e0722e497285f568fcf4',
     'addresses': ['ltc1qxdvtqt9tm7rwplk8upezujtjsh6k3l85tw39ue'], 
     'script_type': 'pay-to-witness-pubkey-hash'}, 
     {'worth': 88100, 
     'script': '0014f4c8cfe352b0a61cedd143e674c4541573d5a6f9', 
     'addresses': ['ltc1q7nyvlc6jkznpemw3g0n8f3z5z4eatfhevy07au'], 
     'script_type': 'pay-to-witness-pubkey-hash'}]},
'tosign': ['']}

First, I attempted utilizing the simple_spend methodology as proven under…

from blockcypher import simple_spend
simple_tx = simple_spend(api_key = API_KEY, from_privkey=priv_key_hex,
to_address="ltc1qxdvtqt9tm7rwplk8upezujtjsh6k3l85tw39ue", to_satoshis=10000, coin_symbol="ltc")
print('tx:', simple_tx)

… and this error was produced:

TX Error(s): Tx NOT Signed or Broadcast
Unable to discover a transaction to spend for handle LhYFhgm6iai5da8ADeoUjs2BnAPoPm74cm.
Not sufficient funds in 0 inputs to pay for 1 outputs, lacking -10000.
Not sufficient funds after charges in 0 inputs to pay for 1 outputs, lacking -11400.
Error validating generated transaction: Transaction lacking enter or output.
Traceback (most up-to-date name final):
  File "major.py", line 33, in <module>
    simple_tx = simple_spend(api_key = API_KEY, from_privkey=priv_key_hex, to_address="ltc1qxdvtqt9tm7rwplk8upezujtjsh6k3l85tw39ue", to_satoshis=10000, coin_symbol="ltc")
  File "/house/runner/sendltc/venv/lib/python3.10/site-packages/blockcypher/api.py", line 1684, in simple_spend
    elevate Exception('Construct Unsigned TX Error')
Exception: Construct Unsigned TX Error

This means that the personal key I offered is routinely being formatted as a legacy handle, the place no funds for the enter transaction exist as a result of I funded the segwit spinoff of the personal key. I’ve since verified that this code will correctly execute transactions after I fund the legacy spinoff of the personal key. But it surely doesn’t work after I try to contain a segwit addresses because the sender or receiver.

Since I’m not sure of how to make sure that the simple_spend methodology makes an attempt a segwit transaction, I attempted establishing it piece-by-piece.

This is the code for that:

from bitcoinlib.keys import Key
from blockcypher import create_unsigned_tx, make_tx_signatures, broadcast_signed_transaction

API_KEY = '<api_key>'

wif="<compressed_wif>"


priv_key_hex = Key(wif, community = 'litecoin').private_hex
pub_key_hex = Key(wif, community = 'litecoin').public_hex

sender = Key(priv_key_hex, community = 'litecoin').handle(prefix='ltc', script_type="p2wpkh", encoding='bech32')
inputs = [{'address': f"{sender}"}]
outputs = [{'address': 'ltc1qxdvtqt9tm7rwplk8upezujtjsh6k3l85tw39ue', 'value': 10000}]

unsigned_tx = create_unsigned_tx(inputs=inputs, outputs=outputs, include_tosigntx=True, coin_symbol="ltc", api_key = API_KEY)
print(unsigned_tx)

privkey_list = [str(priv_key_hex)]
pubkey_list = [str(pub_key_hex)]

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

tx = broadcast_signed_transaction(unsigned_tx=unsigned_tx, signatures=tx_signatures, pubkeys=pubkey_list, coin_symbol="ltc", api_key = API_KEY)
print('')
print(tx)

So when I attempt to run this, the aforementioned error is returned, stating that the script couldn’t be verified. My hunch is that it has one thing to do with making an attempt to broadcast a legacy transaction whereas I offered a segwit handle. Upon viewing comparable threads on right here, it might appear that the signature is being malformed both as a result of there’s a downside with how I am deriving my keys, or someway I have to set a flag indicating a segwit transaction. This might result in the foundation of the issue, however I may be overlooking different points.

Any recommendation, pointers, or solutions could be enormously appreciated. Thanks on your time.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles