Getting Started
Getting Started
After installing datum SDK, in order to start using datum SDK you will need to do the following :
- Create Datum ID.
- init Datum object with the ID created from step 1.
- Get Some DAT (on test net this is done through our Faucet).
- Deposit DAT to storage contract.
- Setting & Retrieving data.
Installation
npm i --save datum-sdk
Creating Datum ID
const Datum = require('datum-sdk');
let password = "password";
Datum.createIdentity('Password')
.then((id)=>{
console.log(id);
});
Output
{
"seed": "around evidence renew filter winter race differ hammer country error arrive man",
"keystore":"{
"encSeed":
{"encStr":"iJeDYSA3RYFjlz1Alini1LblZcea…cjkI3z4SM3mgbYnfhu/Qx8=",
"hdIndex":1,
"version":3
}"
}
}
- Password is used to encrypt the created keystore.
- Seed must be stored since it is used for recovery, it is up to the SDK user to choose how he will do that, or how he will instruct the DAPP user to do that.
- Notice that value of keystore is stringified.
initilize Datum Object
async function createID(password){
let id = await Datum.createIdentity(password);
let tmpDatumObj = new Datum();
tmpDatumObj.initialize({
identity:id.keystore
});
tmpDatObj.identity.storePassword(password);
return tmpDatumObj;
}
- We initialize datum obj with the keystore, which only contains the encrypted keystore
tmpDatObj.identity.storePassword
allows us to do future calls without providing the password on every call
Getting DAT
You can get DAT on the test network through our Faucet.
GET https://faucet.megatron.datum.org/v1/faucet/dat/[Wallet_Address]
- Currently faucet returns 10 DAT Example
fetch(`https://faucet.megatron.datum.org/v1/faucet/dat/${datum.identity.address}`)
.then(response=>{
return Datum.getBalance(datum.identity.address);
}).then(balance=>console.log(`Your balance: ${balance}`));
To check your Balance
Datum.getBalance(datum.identity.address).then(balance=>{console.log(balance)});
- Note it will take sometime before the balance is reflected due to mining time.
- Balances are shown in WEI, so you need to convert them to ETH
Depositing DAT to storage contract
datum.deposit(3).then(hash=>{console.log(`TX Hash: ${hash}`)});
To check your current deposited balance
datum.getDepositBalance().then(dbalance=>{console.log(dbalance)});
//Alternatively you can also use the following
Datum.getDepositBalance(datum.identity.address).then(dbalance=>{console.log(dbalance)});
To be able to perform operations on the data (SET/GET/DELETE...) you need to first prepay or top up your balance in the storage contract first.
After depositing/Prepaying DAT to storage contract, your balance will be deducted based on what operation you are performing on the data.
You have the option to withdraw the unused amount of DAT that you deposited. To withdraw 1 DAT from deposited balance
datum.withdrawal(1)
.then(response=>{
console.log(response);
return Datum.getBalance(datum.identity.address;
}))
.then(balance=>console.log(`Your current balance: ${balance}`));
Setting & Retrieving data
After we perform the previous operations, now we can use datum storage nodes to persist data
datum.set({msg:'hello world :)'})
.then(itemHash=>{
//itemHash= 0xcd06fc30e91efdeef6adf4e8925f2d753521975cac268c426316570c15df53be
return datum.get(itemHash);
})
.then(data=>{
console.log(data); //output: "{"msg":"hello world :)"}"
})