Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit eebd9b5

Browse files
authored
More dag.api goodness (#783)
* feat: dag.get now supports CID as string and CID+Path as String * feat: .dag.tree * docs: add dag-api examples
1 parent e769719 commit eebd9b5

File tree

15 files changed

+406
-19
lines changed

15 files changed

+406
-19
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Let us know if you find any issue or if you want to contribute and add a new tut
1111
- [How to bundle js-ipfs with WebPack](./bundle-webpack)
1212
- [How to use js-ipfs with a script tag](./browser-script-tag)
1313
- [Use IPFS to explore the Ethereum BlockChain](./explore-ethereum)
14-
- [How to use WebRTC star](./how-to-use-webrtc-star)
14+
- [Create and resolve through graphs with the dag API](./dag)
1515

1616
## Tutorials

examples/dag/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Create and resolve through graphs with the dag API
2+
==================================================
3+
4+
In this set of examples, you can find snippets of code to:
5+
6+
- [create nodes of a graph](./put.js)
7+
- [retrieve a graph node](./get.js)
8+
- [resolve a path in a graph](./get-path.js)
9+
- [resolve through graphs of different kind](./get-path-accross-formats.js)
10+
- [explore a graph with the .tree](./tree.js)
11+
- [traverse through a slice of the ethereum blockchain](./eth.js)
12+
13+
### Video of the demos
14+
15+
You can find a video with a walkthrough of this examples on Youtube:
16+
17+
- [IPFS DAG API Preview](https://youtu.be/drULwJ_ZDRQ?t=1m29s)

examples/dag/create-node.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
const series = require('async/series')
4+
5+
const IPFS = require('../../src/core')
6+
// In your project, replace by the following line and install IPFS as a dep
7+
// const IPFS = require('ipfs')
8+
9+
function createNode (options, callback) {
10+
if (typeof options === 'function') {
11+
callback = options
12+
options = {}
13+
}
14+
15+
options.path = options.path || '/tmp/ipfs' + Math.random()
16+
17+
const node = new IPFS({
18+
repo: options.path
19+
})
20+
21+
series([
22+
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
23+
(cb) => node.load(cb),
24+
(cb) => node.goOnline(cb)
25+
], (err) => callback(err, node))
26+
}
27+
28+
module.exports = createNode
530 Bytes
Binary file not shown.
530 Bytes
Binary file not shown.
527 Bytes
Binary file not shown.

examples/dag/eth.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node.js')
4+
const asyncEach = require('async/each')
5+
const path = require('path')
6+
const multihashing = require('multihashing-async')
7+
const Block = require('ipfs-block')
8+
const CID = require('cids')
9+
const fs = require('fs')
10+
11+
createNode((err, ipfs) => {
12+
if (err) {
13+
throw err
14+
}
15+
16+
console.log('\nStart of the example:')
17+
18+
const ethBlocks = [
19+
path.join(__dirname, '/eth-blocks/block_302516'),
20+
path.join(__dirname, '/eth-blocks/block_302517')
21+
]
22+
23+
asyncEach(ethBlocks, (ethBlockPath, cb) => {
24+
const data = fs.readFileSync(ethBlockPath)
25+
26+
multihashing(data, 'keccak-256', (err, multihash) => {
27+
if (err) {
28+
cb(err)
29+
}
30+
const cid = new CID(1, 'eth-block', multihash)
31+
// console.log(cid.toBaseEncodedString())
32+
33+
ipfs.block.put(new Block(data), cid, cb)
34+
})
35+
}, (err) => {
36+
if (err) {
37+
throw err
38+
}
39+
40+
const block302516 = 'z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA'
41+
const block302517 = 'z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj'
42+
43+
function errOrLog (err, result) {
44+
if (err) {
45+
throw err
46+
}
47+
console.log(result.value.toString('hex'))
48+
}
49+
50+
ipfs.dag.get(block302516 + '/number', errOrLog)
51+
ipfs.dag.get(block302517 + '/parent/number', errOrLog)
52+
})
53+
})
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node.js')
4+
const series = require('async/series')
5+
const dagPB = require('ipld-dag-pb')
6+
7+
createNode((err, ipfs) => {
8+
if (err) {
9+
throw err
10+
}
11+
12+
console.log('\nStart of the example:')
13+
14+
let cidPBNode
15+
let cidCBORNode
16+
17+
series([
18+
(cb) => {
19+
const someData = new Buffer('capoeira')
20+
21+
dagPB.DAGNode.create(someData, (err, node) => {
22+
if (err) {
23+
cb(err)
24+
}
25+
26+
ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
27+
if (err) {
28+
cb(err)
29+
}
30+
cidPBNode = cid
31+
cb()
32+
})
33+
})
34+
},
35+
(cb) => {
36+
const myData = {
37+
name: 'David',
38+
likes: ['js-ipfs', 'icecream', 'steak'],
39+
hobbies: [{ '/': cidPBNode.toBaseEncodedString() }]
40+
}
41+
42+
ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => {
43+
if (err) {
44+
throw err
45+
}
46+
47+
cidCBORNode = cid
48+
cb()
49+
})
50+
},
51+
(cb) => {
52+
ipfs.dag.get(cidCBORNode, 'hobbies/0/Data', (err, result) => {
53+
if (err) {
54+
throw err
55+
}
56+
57+
console.log(result.value.toString())
58+
})
59+
}
60+
])
61+
})

examples/dag/get-path.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node.js')
4+
5+
createNode((err, ipfs) => {
6+
if (err) {
7+
throw err
8+
}
9+
10+
console.log('\nStart of the example:')
11+
12+
const myData = {
13+
name: 'David',
14+
likes: ['js-ipfs', 'icecream', 'steak']
15+
}
16+
17+
ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
18+
if (err) {
19+
throw err
20+
}
21+
22+
ipfs.dag.get(cid, 'name', (err, result) => {
23+
if (err) {
24+
throw err
25+
}
26+
27+
console.log(result.value, result.remainderPath)
28+
})
29+
30+
ipfs.dag.get(cid, 'likes', (err, result) => {
31+
if (err) {
32+
throw err
33+
}
34+
35+
console.log(result.value)
36+
})
37+
38+
const cidStr = cid.toBaseEncodedString()
39+
40+
ipfs.dag.get(cidStr + '/likes/0', (err, result) => {
41+
if (err) {
42+
throw err
43+
}
44+
45+
console.log(result.value)
46+
})
47+
})
48+
})

examples/dag/get.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node.js')
4+
5+
createNode((err, ipfs) => {
6+
if (err) {
7+
throw err
8+
}
9+
10+
console.log('\nStart of the example:')
11+
12+
const myData = {
13+
name: 'David',
14+
likes: ['js-ipfs', 'icecream', 'steak']
15+
}
16+
17+
ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
18+
if (err) {
19+
throw err
20+
}
21+
22+
ipfs.dag.get(cid, (err, result) => {
23+
if (err) {
24+
throw err
25+
}
26+
27+
console.log(JSON.stringify(result.value))
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)