How To Get Bitcoin Transaction Details Using Transaction Hash

Posted By : Dharmendra Kumar | 26-Jun-2018

Whenever you perform a bitcoin transaction you would get a transaction hash. This hash can be later used to validate your transaction. Sometimes it's our projects need to get transaction details of bitcoin by using transaction hash. There are some third-party API's that can be used to get these details. To do this in your project you need to call these API's using rest template and use the returned response as per the requirement.

 

 Step 1: First of all you have to create a controller. Pass the transaction hash in the request parameter as a string.

 @RequestMapping(value = "/api/v1/transaction-hash", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResponseEntity<Object> bitcoinTransactionDetails(@RequestParam String bitcoinTransactionHash) {
		
		Map<String, Object> response;
		try {
			response = bitcoinService.getDetails(bitcoinTransactionHash);
		} catch (Exception e) {
			return ResponseUtil.errorResponse(MessageUtility.getErrorMessage("notfound"), HttpStatus.BAD_REQUEST);
		}
		
		return ResponseUtil.response(response, MessageUtility.getSuccessMessage("completed"));
	}

 

Step 2: Now create a service for the above controller.

public Map<String, Object> bitcoinTransactionDetails(String bitcoinTransactionHash) {

		Map<String, Object> response = new HashMap<>();

		ResponseEntity<BlockExplorerData> res = null;
		String url = "https://testnet.blockexplorer.com/api/tx/"+ bitcoinTransactionHash;

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
		headers.add("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
		HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

		res = restTemplate.exchange(url, HttpMethod.GET, entity, BlockExplorerData.class);

                if(res!=null)                   
		response.put("res", res.getBody());

		return response;
	}     

 

In the above service, we are using the APIs of blockexplore.com.We are appending the transaction hash in the URL. Now create an object of rest template and call exchange() method of RestTemplate class. This method returns an object of ResponseEntity class of BlockExploreData type. 

 

Step 3: Create a domain as BlockExploreData.

package com.bitcoin.domain;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class BlockExplorerData {

	private String txid;
	private int confirmations;
	private List<BTCTransactionOutput> vout;

	public BlockExplorerData() {
	}

	public String getTxid() {
		return txid;
	}

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

	public int getConfirmations() {
		return confirmations;
	}

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

	public List<BTCTransactionOutput> getVout() {
		return vout;
	}

	public void setVout(List<BTCTransactionOutput> vout) {
		this.vout = vout;
	}
}

 

Step 4: Create a Domain as BTCTransactionOutput.

 

package com.bitcoin.domain;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class BTCTransactionOutput {

	private String value;
	private ScriptPubKey scriptPubKey;

	public BTCTransactionOutput() {
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public ScriptPubKey getScriptPubKey() {
		return scriptPubKey;
	}

	public void setScriptPubKey(ScriptPubKey scriptPubKey) {
		this.scriptPubKey = scriptPubKey;
	}

	public static class ScriptPubKey {
		private List<String> addresses;

		public List<String> getAddresses() {
			return addresses;
		}

		public void setAddresses(List<String> addresses) {
			this.addresses = addresses;
		}
	}
}

 

On the return object of RestTemplate, we call getBody() method and it returns the object of BlockExploreData class. This object contains information about the total number of confirmation on the transaction and to get confirmations we have to call getConfirmations()  method.
If you want to get the information of from address and amount call getVout() method on BlockExploreData class object.it returns a list of BTCTransactionOutput class. Iterate this list and fetch the details of address and amount
 

About Author

Author Image
Dharmendra Kumar

Dharmendra is skilled in Java, Spring MVC, JPA, MySQL, and C.He has Bachelor's degree focussed in Computer Science and Engineering.

Request for Proposal

Name is required

Comment is required

Sending message..