Using the Datum SDK
Note: Make sure you did all the steps in Getting Started before you proceed with this page.
Store Data
To store data in the Datum Network use the set method, either with or without specifying a key name. Key names make it easier to retrieve data again because you do not need to save the hash id that is used to reference the data.
//e.g. Developer or other 3rd party
var publicKeyToAdd = ["a1ed299db79a79fbdec883e5324afdf982094f24ea1c195d51650720576fb719"]
//sample data to store
var data = {
"Position": {
"Longitude": 9.93368,
"Latitude": 53.55608
}
}
//init with keystore and array of publicKeys that have access, e.g. Developer
datum.initialize({
identity: JSON.stringify(keystore),
defaultPublicKeys: publicKeyToAdd
});
//set password for keystore during session, otherwise you have to pass it everytime
datum.identity.storePassword("YourPassword");
//set data
datum.set(data).then(hash => {
console.log(hash);
}).catch(error => {
console.log(error);
})
//store data with key name
datum.set(data, "PIN_SEEDS")
.then(hash => {
//returns the id of the stored data
console.log(hash);
})
.catch(error => {
console.log(error);
})
Retrieve Data
To retrieve data from the Datum Network use the get or getWithKey method, get
gets data by id/hash of the data while getWithKey
gets data by the key name of the data.
//sample data hash. replace with your hash from "set"
var id = "0x6e2f6b88a8dd00616072d4fd99bcb4c59d69cd39a6ac707156b393f5cd9a61ba";
//retrieve data with given ide
datum.get(id)
.then(result => {
//returns the original data
console.log(result);
})
.catch(error => {
console.log(error);
});
Get data from storage node using key name
//retrieve data with given ide
datum.getWithKey("PIN_SEEDS")
.then(result => {
//returns the original data
console.log(result);
})
.catch(error => {
console.log(error);
});
Get all data hashes/ids stored under this keyname
//retrieve data with given ide
datum.getIdsForKey("PIN_SEEDS")
.then(result => {
//returns the original data
console.log(result);
})
.catch(error => {
console.log(error);
});
Delete Data
Use the remove and removebykey methods to delete data from Storage Nodes, the hash of the data will remain on the blockchain layer which cannot be deleted (immutable).
Remove single data hash/id
//store data and remove it again
datum.set(data)
.on('transaction', function (txHash) {
console.log('set tx broadcasted: ' + txHash);
})
.then(id => {
console.log('data stored.');
return datum.remove(id)
.on('transaction', function (txHash) {
console.log('remove tx broadcasted: ' + txHash);
});
})
.then(result => {
console.log('data deleted');
})
.catch(error => {
console.log(error);
});
Remove all items data under given keyname
//store data and remove it again
datum.set(data, "locations").then(id => {
console.log('data stored: ' + id);
return datum.set(data, "locations");
}).then(id => {
console.log('data stored: ' + id);
return datum.getIdsForKey("locations")
}).then(ids => {
console.log('ids in keyspace');
console.log(ids);
return datum.removeByKey("locations");
}).then(deleted => {
console.log('deleted');
return datum.getIdsForKey("locations")
}).then(ids => {
console.log('ids in keyspace');
console.log(ids);
}).catch(error => {
console.log(error);
});
Events
All blockchain related methods return a promise with additional events fired to monitor progress.
Get events of the progress of storage transactions
datum.deposit(1)
.once('transactionRaw', function(tx){
console.log('the raw transaction before signing');
console.log(tx);
})
.once('transactionHash', function(hash){
console.log('the transaction hash after broadcasted to network');
console.log(hash);
})
.then(mined => {
console.log('transaction was mined in last block')
; console.log(mined);
})
.catch(error => {
console.log('error');
console.log(error);
})
Storage Cost
Use the method getStorageCosts to obtain the cost associated with uploading data on the Datum Blockchain based on size of data in bytes and duration of storage in days. The example is for 1MB of storage for 365 days.
Datum.getStorageCosts(1024 * 1024, 365)
.then(costs => {
console.log(costs);
})
.catch(error => {
console.log('error');
console.log(error);
})
Storage Traffic Costs
Use the method getTrafficCostsGB to obtain the cost based on excepted traffic volume in GB. The example is for 10GB of traffic volume.
Datum.getTrafficCostsGB(10)
.then(costs => {
console.log(costs);
})
.catch(error => {
console.log('error');
console.log(error);
})