Saturday, June 3, 2023

basic Java program that utilizes the BitcoinJ library to inspect Bitcoin transactions

import org.bitcoinj.core.*;

import org.bitcoinj.params.MainNetParams;

import org.bitcoinj.script.Script;

import org.bitcoinj.script.ScriptChunk;

import org.bitcoinj.script.ScriptOpCodes;

public class BitcoinTransactionInspector {

    public static void main(String[] args) {

        // Transaction hash to inspect

        String transactionHash = "a65e6c2de0e20ce08e086f56cc2decd921aae6e73db1e4d86b8c5a583f192235";


        // Initialize BitcoinJ and load the transaction

        NetworkParameters networkParameters = MainNetParams.get();

        Context context = new Context(networkParameters);

        BlockChain blockChain = new BlockChain(context, new MemoryBlockStore(networkParameters));

        Wallet wallet = new Wallet(networkParameters);

        PeerGroup peerGroup = new PeerGroup(networkParameters, blockChain);




        // Retrieve the transaction

        Sha256Hash txHash = Sha256Hash.wrap(transactionHash);

        StoredBlock storedBlock = blockChain.getBlockStore().getChainHead();

        Transaction transaction = blockChain.getTransaction(txHash);


        // Print transaction details

        System.out.println("Transaction: " + transaction.getHashAsString());

        System.out.println("Inputs: ");

        for (TransactionInput input : transaction.getInputs()) {

            System.out.println("  - Previous Output: " + input.getOutpoint().toString());

            System.out.println("  - Script: " + input.getScriptSig().toString());


        System.out.println("Outputs: ");

        for (TransactionOutput output : transaction.getOutputs()) {

            System.out.println("  - Value: " + output.getValue().toFriendlyString());

            System.out.println("  - Script: " + output.getScriptPubKey().toString());



        // Inspect the input script for P2PKH transactions

        TransactionInput input = transaction.getInput(0);

        Script inputScript = input.getScriptSig();

        if (inputScript.isSentToP2PKH()) {

            System.out.println("This is a Pay-to-Public-Key-Hash (P2PKH) transaction.");

            ScriptChunk[] scriptChunks = inputScript.getChunks();

            byte[] signature = scriptChunks[0].data;

            byte[] publicKey = scriptChunks[1].data;

            System.out.println("Signature: " + Utils.HEX.encode(signature));

            System.out.println("Public Key: " + Utils.HEX.encode(publicKey));







To run this program, you will need to have the BitcoinJ library added to your project's dependencies. You can include it in your `pom.xml` file if you're using Maven or download the JAR file manually and add it to your project.

This program initializes the BitcoinJ library, connects to the Bitcoin network, and retrieves the specified transaction. It then prints details about the transaction, including inputs and outputs. Additionally, it inspects the input script for Pay-to-Public-Key-Hash (P2PKH) transactions and prints the signature and public key if applicable.

Please note that this is a basic example and does not cover all possible transaction types or error handling scenarios. It's recommended to refer to the BitcoinJ documentation for more advanced usage or to handle specific use cases.

No comments:

Post a Comment