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

Commit bee40a2

Browse files
chore: restore git and eth graph traversal examples (#9)
1 parent 80a3e0c commit bee40a2

18 files changed

+167
-0
lines changed

examples/traverse-ipld-graphs/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
- [retrieve a node from a graph](#retrieve-a-node-from-a-graph)
3535
- [resolve a path in a graph](#resolve-a-path-in-a-graph)
3636
- [resolve through graphs of different kind](#resolve-through-graphs-of-different-kind)
37+
- [traverse through a slice of the ethereum blockchain](#traverse-through-a-slice-of-the-ethereum-blockchain)
38+
- [traverse through a git repo](#traverse-through-a-git-repo)
3739
- [Video of the demos](#video-of-the-demos)
3840
- [Documentation](#documentation)
3941
- [Contributing](#contributing)
@@ -106,6 +108,14 @@ See [ipld/interface-ipld-format](https://github.com/ipld/interface-ipld-format)
106108

107109
### [resolve through graphs of different kind](./get-path-accross-formats.js)
108110

111+
## [traverse through a slice of the ethereum blockchain](./eth.js)
112+
113+
## [traverse through a git repo](./git.js)
114+
115+
The example objects contained in "git-objects" have already been decompressed with zlib. An example of how to do this:
116+
117+
$ cat .git/objects/7d/df25817f57c2090a9568cdb17106a76dad7d04 | zlib-flate -uncompress > 7ddf25817f57c2090a9568cdb17106a76dad7d04
118+
109119
### Video of the demos
110120

111121
Find a video with a walkthrough of this examples on Youtube:
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/traverse-ipld-graphs/eth.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node')
4+
const path = require('path')
5+
const { CID } = require('multiformats/cid')
6+
const MultihashDigest = require('multiformats/hashes/digest')
7+
const fs = require('fs').promises
8+
const uint8ArrayToString = require('uint8arrays/to-string')
9+
const { convert } = require('ipld-format-to-blockcodec')
10+
const sha3 = require('js-sha3')
11+
12+
async function main () {
13+
const ipfs = await createNode({
14+
ipld: {
15+
codecs: [
16+
...Object.values(require('ipld-ethereum')).map(format => convert(format))
17+
],
18+
hashers: [{
19+
name: 'keccak-256',
20+
code: 0x1b,
21+
digest: async (buf) => {
22+
return MultihashDigest.create(
23+
0x1b,
24+
new Uint8Array(sha3.keccak256.arrayBuffer(buf))
25+
)
26+
}
27+
}]
28+
}
29+
})
30+
31+
console.log('\nStart of the example:')
32+
33+
const ethBlocks = [
34+
path.join(__dirname, '/eth-blocks/block_302516'),
35+
path.join(__dirname, '/eth-blocks/block_302517')
36+
]
37+
38+
for (const ethBlockPath of ethBlocks) {
39+
const data = await fs.readFile(ethBlockPath)
40+
41+
const cid = await ipfs.block.put(data, {
42+
format: 'eth-block',
43+
mhtype: 'keccak-256',
44+
version: 1
45+
})
46+
47+
console.log(cid.toString())
48+
}
49+
50+
const block302516 = CID.parse('z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA')
51+
const block302517 = CID.parse('z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj')
52+
let res
53+
54+
res = await ipfs.dag.get(block302516, { path: 'number' })
55+
console.log(uint8ArrayToString(res.value, 'base16'))
56+
57+
res = await ipfs.dag.get(block302517, { path: 'parent/number' })
58+
console.log(uint8ArrayToString(res.value, 'base16'))
59+
60+
await ipfs.stop()
61+
}
62+
63+
main()
64+
.catch(err => {
65+
console.error(err)
66+
process.exit(1)
67+
})
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/traverse-ipld-graphs/git.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node')
4+
const path = require('path')
5+
const { CID } = require('multiformats/cid')
6+
const MultihashDigest = require('multiformats/hashes/digest')
7+
const fs = require('fs').promises
8+
const uint8ArrayToString = require('uint8arrays/to-string')
9+
const { convert } = require('ipld-format-to-blockcodec')
10+
const crypto = require('crypto')
11+
12+
async function main () {
13+
const ipfs = await createNode({
14+
ipld: {
15+
codecs: [
16+
convert(require('ipld-git'))
17+
],
18+
hashers: [{
19+
name: 'sha1',
20+
code: 0x11,
21+
digest: async (buf) => {
22+
return MultihashDigest.create(0x11, crypto.createHash('sha1').update(buf).digest())
23+
}
24+
}]
25+
}
26+
})
27+
28+
console.log('\nStart of the example:')
29+
30+
const gitObjects = [
31+
path.join(__dirname, '/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593'),
32+
path.join(__dirname, '/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe'),
33+
path.join(__dirname, '/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a'),
34+
path.join(__dirname, '/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249'),
35+
path.join(__dirname, '/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d'),
36+
path.join(__dirname, '/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe'),
37+
path.join(__dirname, '/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a'),
38+
path.join(__dirname, '/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f'),
39+
path.join(__dirname, '/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c'),
40+
path.join(__dirname, '/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21')
41+
]
42+
43+
await Promise.all(gitObjects.map(async gitObjectsPath => {
44+
const data = await fs.readFile(gitObjectsPath)
45+
46+
const cid = await ipfs.block.put(data, {
47+
format: 'git-raw',
48+
mhtype: 'sha1',
49+
version: 1
50+
})
51+
52+
console.log(cid.toString())
53+
}))
54+
55+
const v1tag = CID.parse('z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue')
56+
57+
async function logResult (fn, comment) {
58+
const result = await fn()
59+
60+
if (result.value instanceof Uint8Array) { // Blobs (files) are returned as buffer instance
61+
result.value = uint8ArrayToString(result.value)
62+
}
63+
64+
console.log('-'.repeat(80))
65+
console.log(comment)
66+
console.log(result.value)
67+
}
68+
69+
await logResult(() => ipfs.dag.get(v1tag), 'Tag object:')
70+
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/message' }), 'Tagged commit message:')
71+
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/message' }), 'Parent of tagged commit:')
72+
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/tree/src/hash/hello/hash' }), '/src/hello file:')
73+
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/tree/src/hash/hello/hash' }), 'previous version of /src/hello file:')
74+
75+
await ipfs.stop()
76+
}
77+
78+
main()
79+
.catch(err => {
80+
console.error(err)
81+
process.exit(1)
82+
})

examples/traverse-ipld-graphs/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"@ipld/dag-pb": "^2.1.3",
1717
"ipfs": "^0.56.0",
1818
"ipld-ethereum": "^6.0.0",
19+
"ipld-format-to-blockcodec": "0.0.1",
1920
"ipld-git": "^0.6.1",
21+
"js-sha3": "^0.8.0",
2022
"multiformats": "^9.4.1"
2123
},
2224
"devDependencies": {

examples/traverse-ipld-graphs/tests/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ async function runTest () {
1616
console.info('Testing get-path-accross-formats.js')
1717
await node.waitForOutput('capoeira', 'node', [path.resolve(__dirname, '../get-path-accross-formats.js')])
1818

19+
console.info('Testing eth.js')
20+
await node.waitForOutput('302516', 'node', [path.resolve(__dirname, '../eth.js')])
21+
22+
console.info('Testing git.js')
23+
await node.waitForOutput("CID(baf4bcfhoohhpkaa3qsydcrby65wpblgthcrp2ii)", 'node', [path.resolve(__dirname, '../git.js')])
24+
1925
console.info('Done!')
2026
}
2127

0 commit comments

Comments
 (0)