Javascript library for the Decentralized Knowledge Graph.
If you are looking to build applications leveraging knowledge assets on the OriginTrail Decentralized Knowledge Graph (DKG), the dkg.js SDK library is the best place to start!
The DKG SDK is used together with an OriginTrail gateway node to build applications that interface with the OriginTrail Decentralized Network (the node is a dependency). Therefore you either need to run a gateway node on your local environment or a hosted OT-Node, in order to use the SDK.
Installation
The library can be used both in the browser or in a NodeJS application.
Using dkg.js in the Browser
Use the prebuilt dist/dkg.min.js, or build the file on your own using the dkg.js repository:
npm run build
Then include dist/dkg.min.js in your html file. This will expose DKG on the window object:
<scriptsrc='https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js'></script><scriptsrc='./dist/dkg.min.js'></script><script> window.addEventListener('load', async function () {// DKG object is available here });</script>
Make sure to also include web3.js library as it is a dependency for dkg.js.
Using dkg.js in NodeJS apps
Run the command to install dependency from the NPM repository:
npminstalldkg.js@latest
Then include dkg.js in your project files. This will expose the DKG object:
constDKG=require('dkg.js');
To use the DKG library, you need to connect to a running local or remote OT-Node.
constdkg=newDKG({ environment:ENVIRONMENTS.DEVELOPMENT,// or devnet, testnet, mainnet endpoint:'http://localhost',// gateway node URI port:8900, blockchain: { name:BLOCKCHAIN_IDS.HARDHAT_1,// or any other blockchain id publicKey:PUBLIC_KEY,// not required in browser, metamask used instead privateKey:PRIVATE_KEY,// not required in browser, metamask used instead },});constnodeInfo=awaitdkg.node.info(); // if successfully connected, the will return an object indicating the node version// { 'version': '8.X.X' }
The system supports multiple blockchain networks, which can be configured using the BLOCKCHAIN_IDS constants. You can select the desired blockchain by specifying the corresponding constant. The available options are:
DKG mainnet options:
Base: base:8453
Gnosis: gnosis:100
Neuroweb: otp:2043
DKG testnet options:
Base: base:84532
Gnosis: gnosis:10200
Neuroweb: otp:20430
DKG devnet options:
Base: base:84532
Gnosis: gnosis:10200
Neuroweb: otp:2160
Local options:
Hardhat1: hardhat1:31337
Hardhat2: hardhat2:31337
Create a Knowledge Asset
In this example, let’s create an example Knowledge Asset representing a city. The content contains both public and private assertions. Public assertions will be exposed publicly (replicated to other nodes) while private ones won't (stays on the node you published to only). If you have access to the particular node that has the data when you search for it using get or query you will see both public and private assertions.
When you create the knowledge asset, the above JSON-LD object will be converted into an assertion. When an assertion with public data is prepared, we can create an knowledge asset on DKG. epochsNum specifies for how many epochs the asset should be kept (an epoch is equal to one month).
In case you want to create multiple different assets you can increase your allowance and then each time you initiate a publish, the step where a call to the blockchain is made to increase your allowance will be skipped, resulting in a faster publish time.
After you've finished publishing data to the blockchain, you can decrease your allowance to revoke the authorization given to the contract to spend your tokens - so if you want to revoke all remaining authorization, it's a good practice to pass the same value that you used for increasing your allowance.
Querying the DKG is done by using the SPARQL query language, which is very similar to SQL, applied to graph data (if you have SQL experience, SPARQL should be relatively easy to get started with - more information can be found here).
Let’s write simple query to select all subjects and objects in graph that have the State property of Schema.org context:
Non state-changing interactions with smart contracts are free and can be described as contract getters and don’t require transactions on the blockchain, meaning they do not incur transaction fees. Smart contract transactions are state-changing operations, meaning that they’re changing the state of the smart contract memory and it costs some amount in blockchain native gas tokens (such as ETH, NEURO, etc.).
In order to perform state-changing operations, you need to use a wallet funded with gas tokens.
You can use default keys from the example below for hardhat blockchain: