Introduction to BighchainDB and Setup Part 1

Posted By : Himanshu Bhatt | 26-Jul-2019

Today in this blog I will show you how to set up the BigchainDB in your local machine through docker and send your data to the BigchainDB using java.
 

Bigchaindb is software it has blockchain properties it is a decentralized database there are two types of data that go to BigchainDB one is an asset and the other one is metadata. Asset data is immutable we cannot change it is the fixed data and metadata can be updated but if we have the private and public keys then only it can be updated. It follows the concept of the blockchain it is a type of a blockchain but we use it to store the data usually keys and URLs. There is no concept of any cryptocurrency. It has two things one is tender mint which checks the blocks and validates the blocks and the other one is MongoDB on which all the information related to a blockchain is saved.
 

Setup of BigchainDB on your local machine using docker

  • Install docker on your machine.

  • Clone the project from git clone [email protected]:bigchaindb/bigchaindb.git

  • cd BigchainDB

  • make run

This will start BigchainDB in your local machine and to check if it is running go to http://localhost:9984

Sending your data to BigchainDB  using spring boot

  • We need to add maven dependency for BigchainDB in our pom.xml:

<dependency>

  <groupId>com.bigchaindb</groupId>

  <artifactId>bigchaindb-driver</artifactId>

  <version>1.2</version>

</dependency>

 
  • Let's send the data to the BigchainDB. We will make a bigchainDbDetails domain class to save the user’s credentials in the domain. We will save the private and public key of the user it’s asset id and transaction id in the database.
@Entity
@Getter
@Setter
public class BigChainDbDetails{
    private String assetId;
    private String publicKey;
    private String privateKey;
    private String transactionId;

    public BigChainDbDetails() {

    }

    public BigChainDbDetails(String assetId, String publicKey, String privateKey, String transactionId) {
        this.assetId = assetId;
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        this.transactionId = transactionId;
    }
}
  • We will make a controller class to post the data to BigchainDB:
@RestController
public class BigChainController {
    @Autowired
    private BigChainDbService bigChainDbService;
    
    @PostMapping("setBigChainData")
    public ResponseEntity<Object>postBigChainData(@RequestBody MetaDataDto metaDataDto){
        return ResponseHandler.response(bigChainDbService.saveBigchainDataToDb(metaDataDto),"data saved");
    }

}
  • Let's write the service class for the BigchainDB where all the business logic will be written. Here BigchainDB URL is the URL of the BigchainDB in which port it is running. We will use the KeyPair class for signing the public and private key.

@Service
public class BigChainDbService {
    @Value("${bigchaindb.url}")
    private String bigChainDbUrl;
    @Autowired
    private BigChainDbRepo bigChainDbRepo;

public boolean saveBigchainDataToDb(MetaDataDto metaDataDto) {
        KeyPair keyPair = new KeyPairGenerator().generateKeyPair();
            Optional<String> trasnactionID = saveNewCertificatesBlockchain(keyPair, metaDataDto, user.getEmail());
            if (trasnactionID.isPresent()) {
                BigChainDbDetails newBigChainDbDetails = new BigChainDbDetails(trasnactionID.get(), getBigchainPublicKey(keyPair), getBigchainPrivateKey(keyPair), trasnactionID.get(), user);
                bigChainDbRepo.save(newBigChainDbDetails);
            } 

        }

public Optional<String> saveNewCertificatesBlockchain(KeyPair keyPair, MetaDataDto metaDataDto, String assetData) {
        //Save new claim with asset info and meta data
        Map<String, Object> map = new HashMap<>();
        map.put("email", assetData);
        BigchainDbConfigBuilder.baseUrl(bigChainDbUrl).setup();
        BigChainDbGenericCallback callback = new BigChainDbGenericCallback();
        Transaction createTransaction = null;
        try {
            createTransaction = BigchainDbTransactionBuilder.init()
                    .addAssets(map, HashMap.class)
                    .addMetaData(metaDataDto)
                    .operation(Operations.CREATE)
                    .buildAndSign((EdDSAPublicKey) keyPair.getPublic(), (EdDSAPrivateKey) keyPair.getPrivate())
                    .sendTransaction(callback.getCallBack());
            logger.info("transactionId : {} , done : {} ", createTransaction.getId(), callback.isDone());
            while (!callback.isDone()) {
                logger.info("waiting ....");
            }
            logger.info("task done : {} succes : {} ", callback.isDone(), callback.isSuccess());
            if (!callback.isSuccess()) {
                return Optional.empty();
            }
        } catch (Exception e) {
            logger.error("exception from createTransaction : {}", e.getMessage());
            return Optional.empty();
        }
        logger.info("transcaction id is :::::{}", createTransaction.getId());
        return Optional.of(createTransaction.getId());
        //end new claim with asset info and metaData
    }
}
  • Let’s make the dto for metadata in my case I only need two fields in metadata a url and name.
@Getter
@Setter
public class MetaDataDto {
    private String url;
    private String name;
}

That’s all in this part of the blog you can test the API by sending the metadata and you can assign the asset data. In the next part we will update the data and view the data.

Thanks.

About Author

Author Image
Himanshu Bhatt

Himanshu is a bright java developer. He is keen to learn new technologies. His hobbies are to travel and listen music.

Request for Proposal

Name is required

Comment is required

Sending message..