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 DKG (the node is a dependency). Therefore, to use the SDK, you either need to run a gateway node on your local environment or a hosted OT-node.
Prerequisites
node ≥ 16.0.0
npm ≥ 8.0.0
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 the 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:
npm install dkg.js@latest
Then, include dkg.js in your project files. This will expose the DKG object:
const DKG = require('dkg.js');
To use the DKG library, you need to connect to a running local or remote OT-node.
const dkg = new DKG({
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
},
});
const nodeInfo = await dkg.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 (stay 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 a Knowledge Asset on the DKG. epochsNum specifies how many epochs the asset should be kept for (an epoch is equal to one month).
const result = await DkgClient.asset.create(content, {
epochsNum: 6
});
console.log(result);
The complete response of the method will look like:
If you want to create multiple different assets, you can increase your allowance. Then, each time you initiate a publish, the step of calling the blockchain to increase your allowance will be skipped, resulting in a faster publishing 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. 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.
Non-state-changing interactions with smart contracts are free and can be described as contract-getters. They don’t require transactions on the blockchain. This means they do not incur transaction fees.
Smart contract transactions are state-changing operations. This means they change the state of the smart contract memory, which costs some amount of 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: