Closed Gabi201265 closed 2 months ago
Input needs a value and a witness_type.
With this it works:
for utxo in utxos:
tx.add_input(
prev_txid=utxo['tx_hash'],
output_n=utxo['tx_output_n'],
witness_type='segwit',
value=btc_to_satoshis(balance_84)
)
Not sure why it needs it witness_type though, it should be segwit by default...
Couldn't resist to make a version without the bip_utils library:
seed_phrase = 'screen plunge tiny enrich salmon unfair anxiety embrace offer uniform gym dose'
network = 'testnet'
masterkey = HDKey().from_passphrase(seed_phrase, network=network)
newkey = masterkey.subkey_for_path("m/84'/1'/0'/0/0")
destination_address = "tb1qarfay6n0djw8hvr5272sy3dptnwjutjdqjf4rl"
tx_fee = 0.0000025 #
print(f"Adresse publique (BIP84): {newkey.address()}")
print(f"Clé privée (WIF) (BIP84): {newkey.wif_key()}")
base_url = "https://api.blockcypher.com/v1/btc/test3"
def get_balance(address):
response = requests.get(f"{base_url}/addrs/{address}/balance?token={api_token}")
data = response.json()
return data.get('final_balance', 0) / 100000000 # Satoshis to BTC
def get_utxos(address):
response = requests.get(f"{base_url}/addrs/{address}?token={api_token}&unspentOnly=true")
# return response.json().get('unconfirmed_txrefs', [])
return response.json().get('txrefs', [])
def btc_to_satoshis(btc_amount):
return int(btc_amount * 100000000)
balance_84 = get_balance(newkey.address())
print(f"Solde disponible (BIP84): {balance_84} BTC")
utxos = get_utxos(newkey.address())
if not utxos:
print("Aucun UTXO disponible pour effectuer la transaction.")
exit(1)
# Build transaction
tx = Transaction(network=network)
amount_to_send = balance_84 - tx_fee
amount_to_send_satoshis = btc_to_satoshis(amount_to_send)
for utxo in utxos:
tx.add_input(
prev_txid=utxo['tx_hash'],
output_n=utxo['tx_output_n'],
witness_type='segwit',
value=btc_to_satoshis(balance_84)
)
tx.add_output(amount_to_send_satoshis, destination_address)
for i in range(len(utxos)):
tx.sign(keys=newkey.wif_key())
signed_tx_hex = tx.raw_hex()
print(f"Transaction signée en hex: {signed_tx_hex}")`
The get_balance and get_balance can also be replaces by methods in the Service class.
Hi,
I have an issue. I try to develop a script to automate transaction.
Here is my code :
And here is my result :
Adresse publique (BIP84): Clé privée (WIF) (BIP84): Solde disponible (BIP84): 0.00011554 BTC Transaction signée en hex: 0200000001ddef60775377f2cb8743c4ff4a86bb773d9d89aedc5063d253594860aae211fd140000006b483045022100daf2d94a17c8625bf7e5cd6a831ae3e3cb64c30a46470f9b860937c95673106f02201b38853e1edd750af6dacc0b84e42805d2f0e2b6b8d068e961a5b03326c1aeaa0121034c440c9f262ca3d0e1f9819b884868576759c8e3d415621e73fd5573ca9768e6ffffffff01272c00000000000016001461f16ac29f99560627fcd688be0fbb8bc438f4f000000000 {'error': 'Error validating transaction: witness script detected in tx without witness data.'}
Someone can help me ? Thanks, Best regards, Gabriel