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

More dag.api goodness #783

Merged
merged 9 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Let us know if you find any issue or if you want to contribute and add a new tut
- [How to bundle js-ipfs with WebPack](./bundle-webpack)
- [How to use js-ipfs with a script tag](./browser-script-tag)
- [Use IPFS to explore the Ethereum BlockChain](./explore-ethereum)
- [How to use WebRTC star](./how-to-use-webrtc-star)
- [Create and resolve through graphs with the dag API](./dag)

## Tutorials
17 changes: 17 additions & 0 deletions examples/dag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Create and resolve through graphs with the dag API
==================================================

In this set of examples, you can find snippets of code to:

- [create nodes of a graph](./put.js)
- [retrieve a graph node](./get.js)
- [resolve a path in a graph](./get-path.js)
- [resolve through graphs of different kind](./get-path-accross-formats.js)
- [explore a graph with the .tree](./tree.js)
- [traverse through a slice of the ethereum blockchain](./eth.js)

### Video of the demos

You can find a video with a walkthrough of this examples on Youtube:

- [IPFS DAG API Preview](https://youtu.be/drULwJ_ZDRQ?t=1m29s)
28 changes: 28 additions & 0 deletions examples/dag/create-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const series = require('async/series')

const IPFS = require('../../src/core')
// In your project, replace by the following line and install IPFS as a dep
// const IPFS = require('ipfs')

function createNode (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

options.path = options.path || '/tmp/ipfs' + Math.random()

const node = new IPFS({
repo: options.path
})

series([
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
(cb) => node.load(cb),
(cb) => node.goOnline(cb)
], (err) => callback(err, node))
}

module.exports = createNode
Binary file added examples/dag/eth-blocks/block_302515
Binary file not shown.
Binary file added examples/dag/eth-blocks/block_302516
Binary file not shown.
Binary file added examples/dag/eth-blocks/block_302517
Binary file not shown.
53 changes: 53 additions & 0 deletions examples/dag/eth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const createNode = require('./create-node.js')
const asyncEach = require('async/each')
const path = require('path')
const multihashing = require('multihashing-async')
const Block = require('ipfs-block')
const CID = require('cids')
const fs = require('fs')

createNode((err, ipfs) => {
if (err) {
throw err
}

console.log('\nStart of the example:')

const ethBlocks = [
path.join(__dirname, '/eth-blocks/block_302516'),
path.join(__dirname, '/eth-blocks/block_302517')
]

asyncEach(ethBlocks, (ethBlockPath, cb) => {
const data = fs.readFileSync(ethBlockPath)

multihashing(data, 'keccak-256', (err, multihash) => {
if (err) {
cb(err)
}
const cid = new CID(1, 'eth-block', multihash)
// console.log(cid.toBaseEncodedString())

ipfs.block.put(new Block(data), cid, cb)
})
}, (err) => {
if (err) {
throw err
}

const block302516 = 'z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA'
const block302517 = 'z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj'

function errOrLog (err, result) {
if (err) {
throw err
}
console.log(result.value.toString('hex'))
}

ipfs.dag.get(block302516 + '/number', errOrLog)
ipfs.dag.get(block302517 + '/parent/number', errOrLog)
})
})
61 changes: 61 additions & 0 deletions examples/dag/get-path-accross-formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

const createNode = require('./create-node.js')
const series = require('async/series')
const dagPB = require('ipld-dag-pb')

createNode((err, ipfs) => {
if (err) {
throw err
}

console.log('\nStart of the example:')

let cidPBNode
let cidCBORNode

series([
(cb) => {
const someData = new Buffer('capoeira')

dagPB.DAGNode.create(someData, (err, node) => {
if (err) {
cb(err)
}

ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
if (err) {
cb(err)
}
cidPBNode = cid
cb()
})
})
},
(cb) => {
const myData = {
name: 'David',
likes: ['js-ipfs', 'icecream', 'steak'],
hobbies: [{ '/': cidPBNode.toBaseEncodedString() }]
}

ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => {
if (err) {
throw err
}

cidCBORNode = cid
cb()
})
},
(cb) => {
ipfs.dag.get(cidCBORNode, 'hobbies/0/Data', (err, result) => {
if (err) {
throw err
}

console.log(result.value.toString())
})
}
])
})
48 changes: 48 additions & 0 deletions examples/dag/get-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const createNode = require('./create-node.js')

createNode((err, ipfs) => {
if (err) {
throw err
}

console.log('\nStart of the example:')

const myData = {
name: 'David',
likes: ['js-ipfs', 'icecream', 'steak']
}

ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
if (err) {
throw err
}

ipfs.dag.get(cid, 'name', (err, result) => {
if (err) {
throw err
}

console.log(result.value, result.remainderPath)
})

ipfs.dag.get(cid, 'likes', (err, result) => {
if (err) {
throw err
}

console.log(result.value)
})

const cidStr = cid.toBaseEncodedString()

ipfs.dag.get(cidStr + '/likes/0', (err, result) => {
if (err) {
throw err
}

console.log(result.value)
})
})
})
30 changes: 30 additions & 0 deletions examples/dag/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const createNode = require('./create-node.js')

createNode((err, ipfs) => {
if (err) {
throw err
}

console.log('\nStart of the example:')

const myData = {
name: 'David',
likes: ['js-ipfs', 'icecream', 'steak']
}

ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
if (err) {
throw err
}

ipfs.dag.get(cid, (err, result) => {
if (err) {
throw err
}

console.log(JSON.stringify(result.value))
})
})
})
25 changes: 25 additions & 0 deletions examples/dag/put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const createNode = require('./create-node.js')

createNode((err, ipfs) => {
if (err) {
throw err
}

console.log('\nStart of the example:')

const myData = {
name: 'David',
likes: ['js-ipfs', 'icecream', 'steak']
}

ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
if (err) {
throw err
}
console.log(cid.toBaseEncodedString())
// should print:
// zdpuAzZSktMhXjJu5zneSFrg9ue5rLXKAMC9KLigqhQ7Q7vRm
})
})
61 changes: 61 additions & 0 deletions examples/dag/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

const createNode = require('./create-node.js')
const series = require('async/series')
const dagPB = require('ipld-dag-pb')

createNode((err, ipfs) => {
if (err) {
throw err
}

console.log('\nStart of the example:')

let cidPBNode
let cidCBORNode

series([
(cb) => {
const someData = new Buffer('capoeira')

dagPB.DAGNode.create(someData, (err, node) => {
if (err) {
cb(err)
}

ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
if (err) {
cb(err)
}
cidPBNode = cid
cb()
})
})
},
(cb) => {
const myData = {
name: 'David',
likes: ['js-ipfs', 'icecream', 'steak'],
hobbies: [{ '/': cidPBNode.toBaseEncodedString() }]
}

ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => {
if (err) {
throw err
}

cidCBORNode = cid
cb()
})
},
(cb) => {
ipfs.dag.tree(cidCBORNode, { recursive: true }, (err, paths) => {
if (err) {
throw err
}

console.log(paths)
})
}
])
})
Loading