Getting Started
Add the Datum SDK
npm install datum-sdk --save
Before you begin, you'll need a JavaScript (web or Node.js) app to add the Datum SDK to. If you don't have an app already, you can start with a boilerplate from the truffle framework project, e.g. a React Truffle Box.
To use the Datum SDK in your app you will need to:
- Add the Datum SDK npm package to your project
- Create a Datum Identity
- A Datum Identity has at least one blockchain account attached to it, at the moment this is an ethereum account on the Datum Blockchain
- Fund your Datum Identity with DAT Tokens to pay for storage
- Storing data on the Datum Network requires DAT Tokens, you can get DAT from the testnet faucet for free for testing purposes
The first step is to install the datum-sdk
package from npm
Create a Datum Identity
const Datum = require('datum-sdk');
Datum.createIdentity("YourPassword").then(identity => {
console.log(identity);
})
Once the datum-sdk
package has been installed, you can create a Datum Identity using the createIdentity function. You only have to perform this step once, you will re-use the same Datum Identity in the following examples.
When the code example has run, a 12 word seed and a keystore pair will be provided on your console.
Example Result
{
seed: 'dad angry today genuine prevent discover always capable spatial program jump robust',
keystore:{
"encSeed":{
"encStr":"PpjItQW7n4HinSm2SRvzmdtbM21UpuWWlkiWIsYvt5JCJCrT/OuSaujZmQyHg7kTVOw+jq5vhGtPxtb3blTsOlez24L9twwtr4s9PX1qallSnZR7varPpvAesnsQocaHD2bDlsK29B8TMPwSLr82MUqTWu3Q2ng3Y7i16FwB4BfkUekB93u8dg==",
"nonce":"pwGja3JlwgYKvt1j6+UZxMRDurEugQ7M"
},
"encHdRootPriv":{
"encStr":"MJhSu4D/oEDSh5+2BZzJZMYjoThtO7D5bCcJTbiPkphXTthn6Grq2PDVYonD2iIME0j5QRpcM5niFGtPyXaNAP27azl6lenIbY3MdMkJwuiWigdW/JYwkr4dbxIrSwD1erv0v8yapP3Rvea65cof3gpbExQF82crdXBWvBpEUA==",
"nonce":"7gFt74epS3zhTqj8i7+Ph4PLFhw9/ghe"
},
"addresses":[
"cca23c404fea6a1d4b7fe93d8738f7b12fd31c72"
],
"encPrivKeys":{
"cca23c404fea6a1d4b7fe93d8738f7b12fd31c72":{
"key":"uT98j90SJb4kh7wWIFRDpVcETcogQQ+cL3+5QQWuPjQtMb6H5J/CsDd98kq1esDQ",
"nonce":"DDBWzhnrD901DompAJhtLDj3an5tjV3i"
}
},
"hdPathString":"m/44'/60'/0'/0",
"salt":"XsOLgpITdrDSA3gLzgWSK0RSOc85Ltc28UbHnXINGK4=",
"hdIndex":1,
"version":3
}
}
Initialize SDK
Initialize the Datum SDK instance with the keystore of the users Datum Identity and the public key of your Datum Identity which will be used to identify you as developer of data that is saved through the Datum SDK and grants you access rights and other functionality. You can use the createIdentity function to create a Datum Identity for your user if he does not already have one.
const Datum = require('datum-sdk');
var datum = new Datum();
datum.initialize({
storage: "https://node-eu-west.datum.org/storage",
network: "https://node-us-west.datum.org/api",
useFuelingServer : false,
identity : [keystore],
defaultPublicKeys : [publicKeys]
});
storage
default storage endpoint to send/receive the data
network
default networkendpoint to send blockchain transactions
useFuelingServer
active fueling server for blockchain transactions
identity
datum identity keystore
defaultPublicKeys
array of public keys that will be added by default (e.g. Developer)
The default DatumEndpoint is the testnet
test network. Optionally you can specify other endpoints, see the API reference section for details.
Fund wallet with DAT
In order to store data on the Datum Network, a Datum Identity (that also creates a keystore) must have been generated. To act in the network sufficient balance of DAT tokens is needed. Use the DAT Faucet service in the testnet to get free DAT for testing, see the example below. After requesting free DATs, you should be able to see your updated account balance in the block explorer
Get free test DAT from the faucet in Datum Blockchain (100 DAT's every 24 hours):
https://faucet.megatron.datum.org/v1/faucet/dat/[developer wallet address - datum.identity.address]
Transfer Storage Deposit
Deposit Tokens to initialize your storage space. To store data you need to transfer DAT into the storage contract which will be used as deposit to pay for storage and transaction costs. Use the code example to the right to deposit 10 DAT to the storage contract.
//set password for key storage otherwise you have to pass it every time to access keystore
datum.identity.storePassword("YourPassword");
//static method call for reads!
Datum.getBalance(datum.identity.address).then(balance => {
if (balance == 0) {
throw new Error("No Balance in network. Use the faucet service for free DAT");
}
//deposit
datum.deposit(10).on('transaction', function (txHash) {
console.log('transaction broadcasted. Wait for mining. : ' + txHash);
}).then(result => {
console.log('Deposit done.');
}).catch(error => {
console.log(error);
})
}).catch(error => {
console.log(error);
})
Congratulations, you are now ready to save your first data on Datum. See the next section to learn how to store your first data.