@dashevo/dapi-sdk
v1.2.0
Published
Client SDK for DASH DAPI.
Downloads
6
Keywords
Readme
Note on software compatibility:
This package requires the use of Node >= 8.0.0 and is broadly compatible with modern browsers implementing the ECMAScript 2015 standard or better.
Table of Content
Features :
- Deliver a bitcore-wallet-service compatible D-API (see BWS doc here).
- Provide accounts registration/authentication (using OP_RETURN for now).
- Basic discovery mode and request balancing.
- Explorer API (connector with Insight-API)
- Blockchain/SPV validation (blockheaders)
- TX/Blocks events (whenever a new block/tx is found emit the data).
Getting Started
Install DAPI-SDK
- npm :
npm i -S dapi-sdk
- github :
npm i -S github:dashevo/dapi-sdk
Uses :
You can check our test folder to see some usage exemples.
Import the package :
//import package
const DAPISDK = require('dapi-sdk');
//Quiter version of possibles options.
const options = {
debug:false,
verbose:false,
errors:false,
warnings:false
};
let SDK = DAPISDK(options);
Where theses options can be (in parenthesis,the default value) :
- debug(false) : Bool - When activated, returns utils logs (methods called, uris...)
- verbose(false) : Bool - Will talk. A lot. Emitted events, received stuff...
- warnings(true) : Bool - When activated, log errors received/handled.
- errors(true) : Bool - When activated, log errors received/handled.
- DISCOVER: Object -
- INSIGHT_SEEDS : Array of insight-api uri metadata endpoints (temporary step - see below an exemple)
{
"INSIGHT_SEEDS": [
{
"protocol": "http",
"path": "insight-api-dash",
"base": "51.15.5.18",
"port": 3001
}
]
}
Most of the SDK methods will returns you a Promise.
Therefore depending on your specific needs you can init or call methods of the SDK in a async way :
let API = SDK.Explorer.API;
API
.getLastBlockHeight()
.then(function (height) {
console.log(`last height is ${height}`);
})
API
.getLastBlockHeight(API.getHashFromHeight)
.then(function(hash){
console.log(`Last hash is ${hash}`);
})
});
or using async/await features provided in last NodeJS version :
let height = await SDK.Explorer.API.getLastBlockHeight();
console.log(`last height is ${height}`);
On the API documentation below, and for readability reasons, await will mostly be used.
Add specific insight-api node
During developement phase, you might need to have access to the website you want to call your data from. Therefore you have the possibility to add your seed yourself (we might bring default stable server later).
Exemple :
const options = {
STUFF:stuff,
DISCOVER:{
INSIGHT_SEEDS:[{
protocol:"https",
path:'api',
base:"insight.dash.siampm.com",
port: 443
}]
}
};
let SDK = await DAPISDK(options);
API Documentation :
Components :
After having initiate DAPI-SDK, you will then have access to differents components (à-la framework).
Explorer
will allow you to perform some command on the Insight-API of a masternode (chosen randomnly on a validated list). As it will use someDiscover
methods in order to get the Insight Candidate, calling an Explorer call will first perform an init of Discover (and therefore will fetch and validate the list) before returning the value. Make a note that this will be performed only once.Blockchain
will allow you to have access to some components such as : getting an object stored in your in-mem db, or validate multiple blocks, calculate the next difficulty. The initialization must be done by yourself using :await SDK.Blockchain.init()
in order to beneficiate theses function. Make note that by default it will connect to a randomnly selected insight-api by websocket and will listen to all block. When a block is emitter by the API, it will add it in the blockchain. This comportement can be disable by passing to init the corresponding options (see below). UsingSDK.Blockchain.chain
enable you to use the blockchain-spv-dash methodstools
will allow to access to some of the dependencies of the SDK. Most notably, you have access to :SDK.tools.util
which correspond to a library of handy stuff such as toHash(hex), compressTarget, expandTarget. API here : dash-utilSDK.tools.bitcore
which correspond to a library used in insight-api, see API here : bitcore-dash-library. Contains elements that allow to generate address, sign/verify a message...