Signing and sending a raw transaction using Bitcoinj
In this article, we will go through the process of creating a raw transaction of the Ethereum blockchain using the Bitcoinj library. We will cover how to sign and send a raw transaction from scratch.
Prerequisites
Before you begin, make sure you have:
- Bitcoinj (the official Java performance of the Bitcoin Protocol)
- Bitcoin client (optional but shot to fix errors)
Create a raw transaction
The harsh transaction is a simple text string that represents the content of the block. To create one, we must follow the steps of the thesis:
- Create a new transaction : We will use the Bitcoinj Transaction class to create a new transaction.
- Initialize the list of unspents : We will store all the coins spent on this list.
Code
`Java
Transaction Transaction = New Transaction (Params);
// 遍历未花费列表 组装合适的 组装合适的 item
Double sum = 0;
String address = null;
List
// …
Params.Geunspent ().
If (unspen.ispunded ()) {
Double quantity = unspen.getamount ();
amount += amount;
Sredcoins.put (new coin (unspen.gethash (), 1), indivisible);
}
});
Double Totalamount = Sum – Scredcoins.Values ().
.Maptodouble (unpens :: tamount) .sum ();
Transaction.Setfromaddress (address);
Transaction.Settoaddress (“0x …”); // Replace with your recipient’s address
Transaction.Adtransctionondails (Totalamount, “Transaction Datesit”);
`
key points
- We repeat the list of immediate and add any coins that are based on our
Scredcoins' folder.
- We spent the total amount in the raw transaction, removing the sums of all coins from the amount.
- We set the sender's address to a fixed value ("0x ...") and add details of the raw transaction, including its total amount.
Signing of raw transaction
To sign the raw transaction using Bitcoinj, we will create a new copySigner:
Java
Signing signature = new signature (transaction);
`
We can be this signing to generate a raw transaction signature:
`Java
byte [] Signature = Signer.signrawransction;
`
Sending the raw transaction
To send the raw transaction, we will create a copy of “Transactionender”:
`Java
TransactionDender Sender = New Transactionender (Transaction, Null); // not a recipient’s address given
`
We can be this sender to broadcast the raw transaction in Bitcoin Network:
`Java
Sender.broadcast ();
`
Note that in practice you could probably set the recipient address for the raw transaction when creating it. The example above shows how to do this.
Example use
To send a raw transaction from scratch using Bitcoinj, follow the steps of the thesis:
- Create a new Transaction Copy and Initialize the List of Unspens.
- Iteration above the list of immediate and add all funded coins to a card.
- Calculate the total amount spent in the harsh transaction.
- Set the sender’s address and add details about the raw transaction.
- Sign the raw transaction using a signature.
- Send the signed raw transaction.
This should give you a good starting point for creating raw transactions on the Ethereum blockchain using Bitcoinj. Be sure to replace the replacement addresses with information about your own recipient!