dkg.js 6.0.0-beta.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,237 @@
1
+ ![](./header.png)
2
+
3
+ # DKG.js
4
+
5
+ **Javascript library for interaction with the OriginTrail Decentralized Knowledge Graph**
6
+
7
+ **Note**: This library is currently in beta, so you can expect issues to arise. We'd appreciate that if you do run into trouble, you [open up an issue on this repository](https://github.com/OriginTrail/dkg.js/issues) and let us know.
8
+ Also, there are two actively maintained versions, v5 and v6, make sure you are using the appropriate one.
9
+ The official OriginTrail documentation for v6 can be found [here](https://docs.origintrail.io/dkg-v6-upcoming-version/introduction-to-dkg-v6-start-here).
10
+
11
+
12
+ ## Intro - What is a Decentralized Knowledge Graph (DKG)
13
+
14
+
15
+ There are many avaialable definitions of a knowlege graph, therefore we will present a simplified one focused on usability, rather than completeness. The purpose of this introduction is not to be a comprehensive guide for knowledge graphs, however it aims to get you started with the basics.
16
+
17
+ A **knowledge graph (KG)** is a network of entities — physical & digital objects, events or concepts — illustrating the relationship between them (aka a semantic network). KGs are used by major companies such as [Amazon](http://lunadong.com/talks/PG.pdf), [Google](https://en.wikipedia.org/wiki/Google_Knowledge_Graph), [Uber](https://www.youtube.com/watch?v=r3yMSl5NB_Q), [IBM](https://www.ibm.com/cloud/learn/knowledge-graph) etc for various applications: search, data integration, knowledge reasoning, recommendation engines, analytics, machine learning and AI etc.
18
+
19
+ Key characteristics of knowledge graphs:
20
+ * focus on data connections as "first class citizens" (linked data)
21
+ * designed to ingest data from multiple sources, usually in different formats
22
+ * flexible data model, easily extendable
23
+
24
+ Common knowledge graphs however are deployed within the domain of one organization and are designed to capture knowledge from various sources both from within and outside of the organization.
25
+
26
+ We define **decentralized knowledge graph (DKG)** as a global shared knowledge graph that is designed to benefit organizations and individuals by providing a common infrastructure for data exchange. The DKG:
27
+
28
+ * Enables Dapps with search, integration, analytics, AI and ML capabilities for any data source: blockchains, IPFS, enterprise systems, web services, personal devices
29
+ * Removes central authorities (decentralized infrastructure)
30
+ * Enables permissionless PUBLISH and QUERY (public network)
31
+ * Decentralized identity & Verifiable Credentials based access control (references private data)
32
+
33
+ ## The OriginTrail DKG Architecture
34
+
35
+ The OriginTrail Decentralized Network implements the DKG according the the OriginTrail protocol.
36
+
37
+ It is:
38
+
39
+ * **a permissionless network** - anyone can run OriginTrail nodes
40
+ * **a multi-chain data exchange network** - connects to several blockchains (currently Ethereum and xDai with more integrations upcoming such as with Polkadot)
41
+ * **designed for off-chain data exchange using standardized data models** (GS1 & W3C standards and recommendations)
42
+ * **public open source software**
43
+ * **infrastructure for knowledge marketplaces & tenders** - more info [here](https://www.youtube.com/watch?v=4uCxYGRh5fk)
44
+
45
+ More information is available on the OriginTrail [website](https://origintrail.io), [official documentation](https://docs.origintrail.io) and [blog](https://medium.com/origintrail).
46
+
47
+
48
+ ![](https://i.imgur.com/yTNtZE1.png)
49
+
50
+
51
+
52
+ ## DKG Client library
53
+
54
+ This library provides an interface into the OriginTrail Decentralized Knowledge Graph, enabling:
55
+
56
+ * provisioning & updating assets on the public DKG
57
+ * network and local querying of information based on topics and identifiers
58
+ * verifying the integrity of queried data
59
+
60
+ ### Instalation
61
+
62
+ Run:
63
+ ```sh
64
+ npm install dkg.js
65
+ ```
66
+
67
+ Include:
68
+ ```javascript
69
+ <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
70
+ <script src="./node_modules/dist/index.bundle.js"></script>
71
+ ```
72
+
73
+ ### Setting up your development environment
74
+
75
+ The easiest way to jumpstart development in a local environment is to [set up OT-node v6 in local environment or connect it to public beta DKG](https://docs.origintrail.io/dkg-v6-upcoming-version/setup-instructions-dockerless).
76
+
77
+ ### Getting started
78
+
79
+ ```html
80
+ <!DOCTYPE html>
81
+ <html>
82
+ <head>
83
+ <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
84
+ <script src="./dist/index.bundle.js"></script>
85
+ <script>
86
+ window.addEventListener('load', async function () {
87
+
88
+ const OT_NODE_HOSTNAME = 'alpha-stella-node-06.origin-trail.network';
89
+ const OT_NODE_PORT = '8900';
90
+ let options = { endpoint: OT_NODE_HOSTNAME, port: OT_NODE_PORT, useSSL: true, loglevel: 'trace' };
91
+
92
+ this.dkg = new SemanticWeb3(options);
93
+
94
+ this.dkg.nodeInfo().then(result => {
95
+ console.log(JSON.stringify(result, null, 2));
96
+ });
97
+
98
+ // Create and update an assertion
99
+ options = {
100
+ keywords: ['ETHDenver'],
101
+ visibility: 'public'
102
+ };
103
+
104
+ const content = {
105
+ "@context": "https://www.schema.org/",
106
+ "@type": "ERC721",
107
+ "asset_data": {
108
+ "properties": {
109
+ "prop1": "value1",
110
+ "prop2": {
111
+ "abc":"ads",
112
+ "abs":"adsb"
113
+ },
114
+ },
115
+
116
+ "urls": "https://opensea.io/assets/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/8520"
117
+
118
+ },
119
+ "native_blockchain": "polygon",
120
+ "onchain_data": {
121
+ "contract_address": "0x123",
122
+ "tokenID": "32",
123
+ "tokenURI": "....",
124
+ "owner": "0x12345431",
125
+ "name": "Tracie 101 Updated",
126
+ "symbol": "TRACIE",
127
+ "eventHistory": [{
128
+ "timestamp": "2020-10-12",
129
+ "from": "0x123",
130
+ "to": "0x343",
131
+ "event": "Transfer",
132
+ },
133
+ {
134
+ "timestamp": "2020-09-11",
135
+ "from": "0x123",
136
+ "to": "0x343",
137
+ "event": "Sale",
138
+ "price": "1.23",
139
+ "currency": "ETH",
140
+ }
141
+
142
+ ]
143
+ },
144
+ "erc721_metadata": {
145
+ "title": "Asset Metadata",
146
+ "type": "object",
147
+ "properties": {
148
+ "name": {
149
+ "type": "string",
150
+ "description": "Identifies the asset to which this NFT represents"
151
+ },
152
+ "description": {
153
+ "type": "string",
154
+ "description": "Describes the asset to which this NFT represents"
155
+ },
156
+ "image": {
157
+ "type": "string",
158
+ "description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive."
159
+ }
160
+ }
161
+ },
162
+ "linkedTo": [
163
+ "ual1",
164
+ "ual2",
165
+ ]
166
+ }
167
+
168
+ let result = await dkg.assets.create(content,options)
169
+ let ual = result.data.metadata.UALs[0];
170
+ console.log(`Created UAL is ${ual}`)
171
+
172
+ // Get an asset
173
+ let asset = await dkg.assets.get(ual);
174
+
175
+ const proof = await asset.data.proof.valueOf;
176
+ console.log(`Proof is ${JSON.stringify(proof)}}`)
177
+
178
+ // Search for assertions by using a keyword
179
+ options = { query: 'ETHDenver', resultType: 'assertions' }; //or entities
180
+ await dkg.search(options).then((result) => {
181
+ console.log(JSON.stringify(result, null, 2));
182
+ });
183
+
184
+ // Run sparql queries
185
+
186
+ // Retrieve all connected UALs for a given UAL
187
+ ual = 'a44c97e1a27eab0db298f01f1a5d7c9d84caed8c7ce91480acaf92989888fc37';
188
+ options = {
189
+ query: `PREFIX schema: <http://schema.org/>
190
+ construct { ?b2 schema:linkedTo ?linkedAssets }
191
+ WHERE {
192
+ GRAPH ?g {
193
+ ?b1 schema:linkedTo ?linkedAssets .
194
+ }
195
+ ?b2 schema:hasUALs '${ual}'
196
+ FILTER (?g = ?b2)
197
+ }`
198
+ };
199
+
200
+ dkg.query(options).then((result) => {
201
+ console.log(JSON.stringify(result, null, 2));
202
+ });
203
+
204
+ // Get all states for an asset
205
+ ual = 'a44c97e1a27eab0db298f01f1a5d7c9d84caed8c7ce91480acaf92989888fc37';
206
+ options = {
207
+ query: `PREFIX schema: <http://schema.org/>
208
+ construct { ?assertionId schema:hasUALs ?assertionId }
209
+ WHERE {
210
+ ?assertionId schema:hasUALs '${ual}' .
211
+ }`
212
+ };
213
+
214
+ dkg.query(options).then((result) => {
215
+ console.log(JSON.stringify(result, null, 2));
216
+ });
217
+ });
218
+ </script>
219
+ </head>
220
+ <body>
221
+
222
+ <h1>Traceon!</h1>
223
+
224
+
225
+ </body>
226
+ </html>
227
+ ```
228
+
229
+ ## Learn more
230
+
231
+ More information can be found on the [official DKGv6 documentation](https://docs.origintrail.io/dkg-v6-upcoming-version/introduction-to-dkg-v6-start-here), [website](https://origintrail.io) and [Github](https://github.com/OriginTrail).
232
+
233
+ ## Get in touch
234
+
235
+ Get in touch with the OriginTrail tech community through [Discord](https://discordapp.com/invite/FCgYk2S).
236
+
237
+ [`#traceon`]()
package/assets-demo.js ADDED
@@ -0,0 +1,194 @@
1
+ const DkgClient = require('./index');
2
+
3
+ const OT_NODE_HOSTNAME = '0.0.0.0';
4
+ const OT_NODE_PORT = '8900';
5
+
6
+ // initialize connection to your DKG Node
7
+ let options = { endpoint: OT_NODE_HOSTNAME, port: OT_NODE_PORT, useSSL: false, loglevel: 'trace' };
8
+ const dkg = new DkgClient(options);
9
+
10
+ async function main() {
11
+ let result, ual, name, asset;
12
+
13
+ console.log('============ DKG-Client v6 Demo ===================')
14
+
15
+ await dkg.nodeInfo().then(result => {
16
+ console.log('============ Node info results ===================');
17
+ console.log(JSON.stringify(result, null, 2));
18
+ console.log('===============================');
19
+ });
20
+
21
+ options = {
22
+ keywords: ['tracie', 'nft', 'origintrail'],
23
+ visibility: 'public'
24
+ };
25
+
26
+ result = await dkg.assets.create({
27
+ "@context": "https://www.schema.org/",
28
+ "@type": "ERC721",
29
+ "asset_data": {
30
+ "properties": {
31
+ "prop1": "value1",
32
+ "prop2": {
33
+ "abc":"ads",
34
+ "abs":"adsb"
35
+ },
36
+ },
37
+
38
+ "urls": "https://opensea.io/assets/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/8520"
39
+
40
+ },
41
+ "keywords": [
42
+ "tracie",
43
+ "origintrail",
44
+ "denver",
45
+ ],
46
+ "native_blockchain": "polygon",
47
+ "onchain_data": {
48
+ "contract_address": "0x123",
49
+ "tokenID": "32",
50
+ "tokenURI": "....",
51
+ "owner": "0x12345431",
52
+ "name": "Tracie 101 Updated",
53
+ "symbol": "TRACIE",
54
+ "eventHistory": [{
55
+ "timestamp": "2020-10-12",
56
+ "from": "0x123",
57
+ "to": "0x343",
58
+ "event": "Transfer",
59
+ },
60
+ {
61
+ "timestamp": "2020-09-11",
62
+ "from": "0x123",
63
+ "to": "0x343",
64
+ "event": "Sale",
65
+ "price": "1.23",
66
+ "currency": "ETH",
67
+ }
68
+
69
+ ]
70
+ },
71
+ "erc721_metadata": {
72
+ "title": "Asset Metadata",
73
+ "type": "object",
74
+ "properties": {
75
+ "name": {
76
+ "type": "string",
77
+ "description": "Identifies the asset to which this NFT represents"
78
+ },
79
+ "description": {
80
+ "type": "string",
81
+ "description": "Describes the asset to which this NFT represents"
82
+ },
83
+ "image": {
84
+ "type": "string",
85
+ "description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive."
86
+ }
87
+ }
88
+ },
89
+ "linkedTo": [
90
+ "ual1",
91
+ "ual2",
92
+ ],
93
+ "proof": {
94
+ "signature": "sa9disa9das9d",
95
+ "hash": "sa9disa9das9d",
96
+ "public_key": "0x14356745324523",
97
+ }
98
+ },options)
99
+ ual = result.data.metadata.UALs[0];
100
+ console.log(`Created UAL is ${ual}`)
101
+
102
+ const states = await dkg.assets.getStateCommitHashes(ual);
103
+ asset = await dkg.assets.get(ual);
104
+
105
+ name = await asset.data.name;
106
+ console.log(`Owner of UAL ${ual} is ${name}`)
107
+
108
+ options = {
109
+ keywords: ['tracie', 'nft', 'origintrail'],
110
+ visibility: 'public',
111
+ };
112
+
113
+ await dkg.assets.update({
114
+ "@context": "https://www.schema.org/",
115
+ "@type": "ERC721",
116
+ "asset_data": {
117
+ "properties": {
118
+ "prop1": "value1",
119
+ "prop2": {
120
+ "abc":"ads",
121
+ "abs":"adsb"
122
+ },
123
+ },
124
+
125
+ "urls": "https://opensea.io/assets/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/8520"
126
+
127
+ },
128
+ "keywords": [
129
+ "tracie",
130
+ "origintrail",
131
+ "denver",
132
+ ],
133
+ "native_blockchain": "polygon",
134
+ "onchain_data": {
135
+ "contract_address": "0x123",
136
+ "tokenID": "32",
137
+ "tokenURI": "....",
138
+ "owner": "0x12345431",
139
+ "name": "Tracie 101 Updated",
140
+ "symbol": "TRACIE",
141
+ "eventHistory": [{
142
+ "timestamp": "2020-10-12",
143
+ "from": "0x123",
144
+ "to": "0x343",
145
+ "event": "Transfer",
146
+ },
147
+ {
148
+ "timestamp": "2020-09-11",
149
+ "from": "0x123",
150
+ "to": "0x343",
151
+ "event": "Sale",
152
+ "price": "1.23",
153
+ "currency": "ETH",
154
+ }
155
+
156
+ ]
157
+ },
158
+ "erc721_metadata": {
159
+ "title": "Asset Metadata",
160
+ "type": "object",
161
+ "properties": {
162
+ "name": {
163
+ "type": "string",
164
+ "description": "Identifies the asset to which this NFT represents"
165
+ },
166
+ "description": {
167
+ "type": "string",
168
+ "description": "Describes the asset to which this NFT represents"
169
+ },
170
+ "image": {
171
+ "type": "string",
172
+ "description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive."
173
+ }
174
+ }
175
+ },
176
+ "linkedTo": [
177
+ "ual1",
178
+ "ual2",
179
+ ],
180
+ "proof": {
181
+ "signature": "sa9disa9das9d",
182
+ "hash": "sa9disa9das9d",
183
+ "public_key": "0x14356745324523",
184
+ }
185
+ }, ual, options)
186
+
187
+ name = await asset.data.name.valueOf;
188
+ console.log(`New owner of UAL ${ual} is ${name.toString()}`)
189
+
190
+ console.log("That's all folks");
191
+ process.exit(0);
192
+ }
193
+
194
+ main();