How to get Bitcoin transaction Details from TransactionId in Java

Posted By : Krishna Verma | 28-Jul-2018

Hello, everyone, this is Krishna Verma, in this blog I am going to tell you about how you can get the details of bitcoin transaction from the transaction hash in a java program. if you have ever worked on a crypto trading platform or ever want to work then you must have the knowledge of how to get the transaction details if someone gives you the transaction hash using java program.

first, you have to make a java program and add bitcoin dependency in your project so that you can connect to bitcoin server and can get the details.

1. Make a controller which accepts transaction hash from the user.

@RestController
public class BitcoinController {

    private static Logger LOGGER = LoggerFactory.getLogger(UtilityController.class);

    @Autowired
    MessageService messageService;

    @Autowired
	private BitcoinService bitcoinService;
    
    @RequestMapping(value=URLMapping.SAVE_BITCOIN_RECEIVE_TRANSACTION, method=RequestMethod.POST)
	public void saveBitcoinReceiveTransaction(@RequestBody String data){
    	LOGGER.info("FROM BITCOIN RECEIVER : {}",data);
		bitcoinService.bitcoinListener(data);
	}
}

2. make a service to implement the logic for the controller

@Service
public class BitcoinService {

	private static Logger logger = LoggerFactory.getLogger(BitcoinService.class);

	@Autowired
	private BitcoinJSONRPCClient bitcoinJSONRPCClient;


	public void bitcoinListener(String data) {
		String transactionHash = data.replace("txid=", "");
		logger.info("transactionHash is : {}", transactionHash);
			TransactionDTO transactionDTO = getReceivedTransactionInfo(transactionHash);
		}

	}

	public TransactionDTO getReceivedTransactionInfo(String transactionHash) {
		TransactionDTO transactionDTO = null;
		try {
			Object object = bitcoinJSONRPCClient.query("gettransaction", transactionHash);
			ObjectMapper mapper = new ObjectMapper();
			transactionDTO = mapper.convertValue(object, TransactionDTO.class);
			logger.info("transaction Data successfully received from blockchain");
		} catch (Exception ex) {
			logger.error(ex.getMessage());
		}
		return transactionDTO;
	}
}

here is the domain for transactionDTO

 
public class TransactionDTO {
	private Double amount;
	private String sendTo ="";
	
	public String getSendTo() {
		return sendTo;
	}

	public void setSendTo(String sendTo) {
		this.sendTo = sendTo;	
	}

	private Long confirmations;
	private String txid;
	private List details;

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public List getDetails() {
		return details;
	}

	public void setDetails(List details) {
		this.details = details;
	}

	public Long getConfirmations() {
		return confirmations;
	}

	public void setConfirmations(Long confirmations) {
		this.confirmations = confirmations;
	}

	public String getTxid() {
		return txid;
	}

	public void setTxid(String txid) {
		this.txid = txid;
	}
}

domain for DetailsDTO is 

 
public class DetailsDTO {
	private String account;
	private String address;
	private String category;
	private Double amount;

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}
}

the transaction data that you will receive from bitcoin server is an object and you have to cast that object into a DTO so that you can use is in your code. when the transaction object is converted in transactionDTO you can use that transactionDTO object and get all the details from transactionDTO which will be offered by the bitcoin like receiver address, amount, account name, time etc.

you can make a domain for saving this transaction in the database the domain structure you can follow with basic fields is :

 
/**
 * @author krishna
 *
 * @Dat29-Jun-2018
 */
@Entity
public class BitcoinTransaction {

	public BitcoinTransaction(Long userId, String transactionHash, String receivedToAddress, BigDecimal amount,
			TransactionType transactionType, TransactionStatus transactionStatus, Date transactionDate) {
		super();
		this.userId = userId;
		this.transactionHash = transactionHash;
		this.receivedToAddress = receivedToAddress;
		this.amount = amount;
		this.transactionType = transactionType;
		this.transactionStatus = transactionStatus;
		this.transactionDate = transactionDate;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private Long userId;

	private String sendToAddress;

	private String sendFromAddress;

	private String transactionHash;

	private String receivedFromAddress;

	private String receivedToAddress;

	private BigDecimal amount;

	private TransactionType transactionType;

	private TransactionStatus transactionStatus;

	@Temporal(TemporalType.TIMESTAMP)
	private Date transactionDate = new Date();;

	private Double transactionFee;

	public BitcoinTransaction() {
		super();
	}

	public BitcoinTransaction(Long userId, BigDecimal amount, String sendToAddress, TransactionStatus transactionStatus,
			String sendFromAddress, TransactionType transactionType) {
		// TODO Auto-generated constructor stub
		this.userId = userId;
		this.amount = amount;
		this.sendToAddress = sendToAddress;
		this.transactionStatus = transactionStatus;
		this.sendFromAddress = sendFromAddress;
		this.transactionType = transactionType;
	}

	public BitcoinTransaction(Long userId, String receivedFromAddress, String receivedToAddress, BigDecimal amount,
			TransactionType transactionType, TransactionStatus transactionStatus) {
		super();
		this.userId = userId;
		this.receivedFromAddress = receivedFromAddress;
		this.receivedToAddress = receivedToAddress;
		this.amount = amount;
		this.transactionType = transactionType;
		this.transactionStatus = transactionStatus;
	}

	public String getReceivedFromAddress() {
		return receivedFromAddress;
	}

	public void setReceivedFromAddress(String receivedFromAddress) {
		this.receivedFromAddress = receivedFromAddress;
	}

	public String getReceivedToAddress() {
		return receivedToAddress;
	}

	public void setReceivedToAddress(String receivedToAddress) {
		this.receivedToAddress = receivedToAddress;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getSendToAddress() {
		return sendToAddress;
	}

	public void setSendToAddress(String sendToAddress) {
		this.sendToAddress = sendToAddress;
	}

	public String getSendFromAddress() {
		return sendFromAddress;
	}

	public void setSendFromAddress(String sendFromAddress) {
		this.sendFromAddress = sendFromAddress;
	}

	public String getTransactionHash() {
		return transactionHash;
	}

	public void setTransactionHash(String transactionHash) {
		this.transactionHash = transactionHash;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public TransactionType getTransactionType() {
		return transactionType;
	}

	public void setTransactionType(TransactionType transactionType) {
		this.transactionType = transactionType;
	}

	public TransactionStatus getTransactionStatus() {
		return transactionStatus;
	}

	public void setTransactionStatus(TransactionStatus transactionStatus) {
		this.transactionStatus = transactionStatus;
	}

	public Date getTransactionDate() {
		return transactionDate;
	}

	public void setTransactionDate(Date transactionDate) {
		this.transactionDate = transactionDate;
	}

	public Double getTransactionFee() {
		return transactionFee;
	}

	public void setTransactionFee(Double transactionFee) {
		this.transactionFee = transactionFee;
	}

	
	public Long getUserId() {
		return userId;
	}

	
	public void setUserId(Long userId) {
		this.userId = userId;
	}
}

this domain contains all the fields which is offered by bitcoin in a transaction, and which are generally used in a crypto trading platform.
thanks 

About Author

Author Image
Krishna Verma

Krishna is a Software Developer having key skills in java , his hobbies are learning new technologies

Request for Proposal

Name is required

Comment is required

Sending message..