from pycardano import Address, Network, PaymentSigningKey, PaymentVerificationKey from pycardano import PaymentSigningKey, StakeSigningKey, PaymentVerificationKey, StakeVerificationKey from pycardano import PaymentKeyPair, StakeKeyPair from pycardano import (TransactionBody, TransactionInput, TransactionId, TransactionOutput, TransactionBuilder,VerificationKeyWitness,Transaction,TransactionWitnessSet,StakeRegistration,StakeCredential,StakeDelegation) from blockfrost import ApiUrls from pycardano import BlockFrostChainContext # BlockFrost Config network = Network.TESTNET context = BlockFrostChainContext("redacted", base_url=ApiUrls.preprod.value) # Load keys payment_signing_key = PaymentSigningKey.load("payment.skey") payment_verification_key = PaymentVerificationKey.load("payment.vkey") # Get stake keys stake_key_pair = StakeKeyPair.generate() stake_signing_key = stake_key_pair.signing_key stake_verification_key = stake_key_pair.verification_key # Build pay address pay_address = Address(payment_part=payment_verification_key.hash(), network=Network.TESTNET) # Creating a transaction from utxo(obtained from blockfrost) included staticaly for ease: tx_id = "40e4dfa915931b4811d69175b56d7dc479e45a25896ef12d1dce54f83b35da03" tx_in = TransactionInput.from_primitive([tx_id, 0]) # Converting address to string newaddress = str(pay_address) # Define two transaction outputs, the first one is the amount we want to send, the second one is the change. output1 = TransactionOutput.from_primitive([newaddress, 4000000000]) output2 = TransactionOutput.from_primitive([newaddress, 1000000000-2165897]) # Build Staking Registration and Delegation Certificates stakecred = StakeCredential(credential = [stake_verification_key.hash()]) mystakeregistation = StakeRegistration(stake_credential=stakecred) delegation = StakeDelegation(stakecred, "bfc661ac04379ab66f11be7762e4aabfd6470755c2d41a1cc488dddca11f0354") # Build Transaction Body tx_body = TransactionBody(inputs=[tx_in], certificates=[mystakeregistation] ,outputs=[output1, output2], fee=2165897) # Sign the transaction body hash signature = payment_signing_key.sign(tx_body.hash()) # Add verification key and the signature to the witness set vk_witnesses = [VerificationKeyWitness(payment_verification_key, signature)] # Create final signed transaction signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses)) # Submit transaction to blockfrost result = context.submit_tx(signed_tx.to_cbor()) print(result)