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

Commit d4090e0

Browse files
authored
chore: restore git and eth graph traversal examples (#3770)
These were removed during the multiformats upgrade but they are quite useful as a demo of how to configure extra multiformat blockcodecs/hashers
1 parent 4b79dcb commit d4090e0

File tree

5 files changed

+165
-3
lines changed

5 files changed

+165
-3
lines changed

examples/traverse-ipld-graphs/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ See [ipld/interface-ipld-format](https://github.com/ipld/interface-ipld-format)
5050

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

53+
## [traverse through a slice of the ethereum blockchain](./eth.js)
54+
55+
## [traverse through a git repo](./git.js)
56+
The example objects contained in "git-objects" have already been decompressed with zlib. An example of how to do this:
57+
58+
$ cat .git/objects/7d/df25817f57c2090a9568cdb17106a76dad7d04 | zlib-flate -uncompress > 7ddf25817f57c2090a9568cdb17106a76dad7d04
59+
5360
## Video of the demos
5461

5562
Find a video with a walkthrough of this examples on Youtube:

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+
})

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"test-ipfs-example": "^3.0.0"
1414
},
1515
"dependencies": {
16-
"@ipld/dag-pb": "^2.1.3",
1716
"ipfs": "^0.56.0",
18-
"ipld-git": "^0.6.1",
1917
"ipld-ethereum": "^6.0.0",
20-
"multiformats": "^9.4.1"
18+
"ipld-format-to-blockcodec": "0.0.1",
19+
"ipld-git": "^0.6.1",
20+
"js-sha3": "^0.8.0"
2121
}
2222
}

examples/traverse-ipld-graphs/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ async function runTest () {
1818
console.info('Testing get-path-accross-formats.js')
1919
await waitForOutput('capoeira', path.resolve(__dirname, 'get-path-accross-formats.js'))
2020

21+
console.info('Testing eth.js')
22+
await waitForOutput('302516', path.resolve(__dirname, 'eth.js'))
23+
24+
console.info('Testing git.js')
25+
await waitForOutput("CID(baf4bcfhoohhpkaa3qsydcrby65wpblgthcrp2ii)", path.resolve(__dirname, 'git.js'))
26+
2127
console.info('Done!')
2228
}
2329

0 commit comments

Comments
 (0)