Please note: This driver is compatible with Android API 23 and later.
BigchainDB Server | BigchainDB Java Driver |
---|---|
2.x |
1.x |
The build system supports both Maven and Gradle.
In your pom.xml
file, add bigchaindb-driver
as a dependency:
<dependency>
<groupId>com.bigchaindb</groupId>
<artifactId>bigchaindb-driver</artifactId>
<version>1.0</version>
</dependency>
then
mvn clean install
In your build.gradle
file, add bigchaindb-driver
as a dependency:
dependencies {
implementation 'com.bigchaindb.bigchaindb-driver:1.2'
}
then
./gradlew install
A sample of an end-to-end CREATE and TRANSFER operation is available in the gist https://gist.github.com/innoprenuer/d4c6798fe5c0581c05a7e676e175e515
BigchainDbConfigBuilder
.baseUrl("https://node1.example.com/")
.addToken("header1", <header1_value>)
.addToken("header2", <header2_value>).setup();
Note - multi-node setup is only available in version 1.2 and later
Assumption - The following setup assumes that all nodes are all connected within same BigchainDB network.
//define connections
Map<String, Object> conn1Config = new HashMap<String, Object>(),
conn2Config = new HashMap<String, Object>();
//define headers for connections
Map<String, String> headers1 = new HashMap<String, String>();
Map<String, String> headers2 = new HashMap<String, String>();
//config header for connection 1
headers1.put("app_id", "<your-app-id>");
headers1.put("app_key", "<your-app-key>");
//config header for connection 2
headers2.put("app_id", "<your-app-id>");
headers2.put("app_key", "<your-app-key>");
//config connection 1
conn1Config.put("baseUrl", "https://node1.mysite.com/");
conn1Config.put("headers", headers1);
Connection conn1 = new Connection(conn1Config);
//config connection 2
conn2Config.put("baseUrl", "https://node2.mysite.com/");
conn2Config.put("headers", headers2);
Connection conn2 = new Connection(conn2Config);
//add connections
List<Connection> connections = new ArrayList<Connection>();
connections.add(conn1);
connections.add(conn2);
//...You can add as many nodes as you want
BigchainDbConfigBuilder
.addConnections(connections)
.setTimeout(60000) //override default timeout of 20000 milliseconds
.setup();
}
// prepare your keys
net.i2p.crypto.eddsa.KeyPairGenerator edDsaKpg = new net.i2p.crypto.eddsa.KeyPairGenerator();
KeyPair keyPair = edDsaKpg.generateKeyPair();
// New asset
Map<String, String> assetData = new TreeMap<String, String>() {{
put("city", "Berlin, DE");
put("temperature", "22");
put("datetime", new Date().toString());
}};
// New metadata
MetaData metaData = new MetaData();
metaData.setMetaData("what", "My first BigchainDB transaction");
Performing a CREATE transaction in BigchainDB allocates or issues a digital asset.
// Set up, sign, and send your transaction
Transaction createTransaction = BigchainDbTransactionBuilder
.init()
.addAssets(assetData, TreeMap.class)
.addMetaData(metaData)
.operation(Operations.CREATE)
.buildAndSign((EdDSAPublicKey) keyPair.getPublic(), (EdDSAPrivateKey) keyPair.getPrivate())
.sendTransaction();
Performing a TRANSFER transaction in BigchainDB changes an asset's ownership (or, more accurately, authorized signers):
// Generate a new keypair to TRANSFER the asset to
KeyPair targetKeypair = edDsaKpg.generateKeyPair();
// Describe the output you are fulfilling on the previous transaction
final FulFill spendFrom = new FulFill();
spendFrom.setTransactionId(createTransaction.getId());
spendFrom.setOutputIndex(0);
// Change the metadata if you want
MetaData transferMetadata = new MetaData();
metaData.setMetaData("what2", "My first BigchainDB transaction");
// the asset's ID is equal to the ID of the transaction that created it
String assetId = createTransaction.getId();
// By default, the 'amount' of a created digital asset == "1". So we spend "1" in our TRANSFER.
String amount = "1";
// Use the previous transaction's asset and TRANSFER it
Transaction transferTransaction = BigchainDbTransactionBuilder
.init()
.addMetaData(metaData)
// source keypair is used in the input, because the current owner is "spending" the output to transfer it
.addInput(null, spendFrom, (EdDSAPublicKey) keyPair.getPublic())
// after this transaction, the target 'owns' the asset, so, the new output includes the target's public key
.addOutput(output, (EdDSAPublicKey) targetKeypair.getPublic())
// reference the asset by ID when doing a transfer
.addAssets(assetId, String.class)
.operation(Operations.TRANSFER)
// the source key signs the transaction to authorize the transfer
.buildAndSign((EdDSAPublicKey) keyPair.getPublic(), (EdDSAPrivateKey) keyPair.getPrivate())
.sendTransaction();
public class MyCustomMonitor implements MessageHandler {
@Override
public void handleMessage(String message) {
ValidTransaction validTransaction = JsonUtils.fromJson(message, ValidTransaction.class);
}
}
// config
BigchainDbConfigBuilder
.baseUrl("https://api.example.net")
.addToken("app_id", "2bbaf3ff")
.addToken("app_key", "c929b708177dcc8b9d58180082029b8d")
.webSocketMonitor(new MyCustomMonitor())
.setup();
// Set up your transaction but only build it
Transaction transaction = BigchainDbTransactionBuilder
.init()
.addAssets(assetData, TreeMap.class)
.addMetaData(metaData)
.operation(Operations.CREATE)
.buildOnly((EdDSAPublicKey) keyPair.getPublic());
// Set up your transaction
Transaction transaction = BigchainDbTransactionBuilder
.init()
.addAssets(assetData, TreeMap.class)
.addMetaData(metaData)
.operation(Operations.CREATE)
.buildAndSignOnly((EdDSAPublicKey) keyPair.getPublic(), (EdDSAPrivateKey) keyPair.getPrivate());
TransactionsApi.sendTransaction(Transaction transaction) throws IOException
TransactionsApi.sendTransaction(Transaction transaction, final GenericCallback callback)
Transaction TransactionsApi.getTransactionById(String id) throws IOException
Transactions TransactionsApi.getTransactionsByAssetId(String assetId, Operations operation)
Outputs getOutputs(String publicKey) throws IOException
Outputs getSpentOutputs(String publicKey) throws IOException
Outputs getUnspentOutputs(String publicKey) throws IOException
Assets getAssets(String searchKey) throws IOException
Assets getAssetsWithLimit(String searchKey, String limit) throws IOException
Block getBlock(String blockId) throws IOException
List<String> getBlocksByTransactionId(String transactionId) throws IOException
MetaDatas getMetaData(String searchKey) throws IOException
MetaDatas getMetaDataWithLimit(String searchKey, String limit) throws IOException
Validators getValidators() throws IOException
- HTTP API Reference
- The Transaction Model
- Inputs and Outputs
- Asset Transfer
- All BigchainDB Documentation
Inspired by http://github.com/authenteq/java-bigchaindb-driver.
The BigchainDB team and others including:
- @bodia
- @alvin-reyes
- @agwego
- @nf-PostQuantum
- @Rokko11
- @tzclucian
- @kremalicious
- @avanaur
- @GerardoGa
- @bakaoh
- @innoprenuer
To execute a release build with Maven, define performRelease
to enable GPG signing:
mvn clean package install -DperformRelease
See LICENSE and LICENSE-docs.
Exception: src/main/java/com/bigchaindb/util/Base58.java
has a different license; see the comments at the top of that file for more information.