From 6cc3645712b7b9fc1fb2dc52887933373a3bcd5f Mon Sep 17 00:00:00 2001 From: Oliverio Sousa <47525443+oliveriosousa@users.noreply.github.com> Date: Wed, 28 Jul 2021 13:45:30 +0100 Subject: [PATCH] chore: restore git and eth graph traversal examples --- examples/traverse-ipld-graphs/README.md | 10 +++ .../eth-blocks/block_302515 | Bin 0 -> 530 bytes .../eth-blocks/block_302516 | Bin 0 -> 530 bytes .../eth-blocks/block_302517 | Bin 0 -> 527 bytes examples/traverse-ipld-graphs/eth.js | 67 ++++++++++++++ .../0f328c91df28c5c01b9e9f9f7e663191fa156593 | Bin 0 -> 186 bytes .../177bf18bc707d82b21cdefd0b43b38fc8c5c13fe | Bin 0 -> 22 bytes .../23cc25f631cb076d5de5036c87678ea713cbaa6a | Bin 0 -> 29 bytes .../4e425dba7745a781f0712c9a01455899e8c0c249 | Bin 0 -> 72 bytes .../6850c7be7136e6be00976ddbae80671b945c3e9d | Bin 0 -> 150 bytes .../a5095353cd62a178663dd26efc2d61f4f61bccbe | Bin 0 -> 41 bytes .../dc9bd15e8b81b6565d3736f9c308bd1bba60f33a | Bin 0 -> 72 bytes .../e68e6f6e31857877a79fd6b3956898436bb5a76f | Bin 0 -> 233 bytes .../ee62b3d206cb23f939208898f32d8708c0e3fa3c | Bin 0 -> 41 bytes .../ee71cef5001b84b0314438f76cf0acd338a2fd21 | Bin 0 -> 28 bytes examples/traverse-ipld-graphs/git.js | 82 ++++++++++++++++++ examples/traverse-ipld-graphs/package.json | 2 + examples/traverse-ipld-graphs/tests/test.js | 6 ++ 18 files changed, 167 insertions(+) create mode 100644 examples/traverse-ipld-graphs/eth-blocks/block_302515 create mode 100644 examples/traverse-ipld-graphs/eth-blocks/block_302516 create mode 100644 examples/traverse-ipld-graphs/eth-blocks/block_302517 create mode 100644 examples/traverse-ipld-graphs/eth.js create mode 100644 examples/traverse-ipld-graphs/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593 create mode 100644 examples/traverse-ipld-graphs/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe create mode 100644 examples/traverse-ipld-graphs/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a create mode 100644 examples/traverse-ipld-graphs/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249 create mode 100644 examples/traverse-ipld-graphs/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d create mode 100644 examples/traverse-ipld-graphs/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe create mode 100644 examples/traverse-ipld-graphs/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a create mode 100644 examples/traverse-ipld-graphs/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f create mode 100644 examples/traverse-ipld-graphs/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c create mode 100644 examples/traverse-ipld-graphs/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21 create mode 100644 examples/traverse-ipld-graphs/git.js diff --git a/examples/traverse-ipld-graphs/README.md b/examples/traverse-ipld-graphs/README.md index f24302c6..6d4e624c 100644 --- a/examples/traverse-ipld-graphs/README.md +++ b/examples/traverse-ipld-graphs/README.md @@ -34,6 +34,8 @@ - [retrieve a node from a graph](#retrieve-a-node-from-a-graph) - [resolve a path in a graph](#resolve-a-path-in-a-graph) - [resolve through graphs of different kind](#resolve-through-graphs-of-different-kind) +- [traverse through a slice of the ethereum blockchain](#traverse-through-a-slice-of-the-ethereum-blockchain) +- [traverse through a git repo](#traverse-through-a-git-repo) - [Video of the demos](#video-of-the-demos) - [Documentation](#documentation) - [Contributing](#contributing) @@ -106,6 +108,14 @@ See [ipld/interface-ipld-format](https://github.com/ipld/interface-ipld-format) ### [resolve through graphs of different kind](./get-path-accross-formats.js) +## [traverse through a slice of the ethereum blockchain](./eth.js) + +## [traverse through a git repo](./git.js) + +The example objects contained in "git-objects" have already been decompressed with zlib. An example of how to do this: + + $ cat .git/objects/7d/df25817f57c2090a9568cdb17106a76dad7d04 | zlib-flate -uncompress > 7ddf25817f57c2090a9568cdb17106a76dad7d04 + ### Video of the demos Find a video with a walkthrough of this examples on Youtube: diff --git a/examples/traverse-ipld-graphs/eth-blocks/block_302515 b/examples/traverse-ipld-graphs/eth-blocks/block_302515 new file mode 100644 index 0000000000000000000000000000000000000000..1e6b8fdc5179ffc55027bd82152b9eb5b657faab GIT binary patch literal 530 zcmey##J|Ay4NLWj2X~YtT;^%9w4W;28NW4Ov3H7YlQ+}+f=+9{1+r&+U)(z$TeZ4% zYx=e`SEMcrxk^vzDiQv$(CM$kmC5c?UZwlreJ=8M`rOBFKYYzvS(x3~cVdD6xt}3- zwSygZ*EuMveXz^a;JN*lYwp6&3ws!reOvRUYeCoxd2#78q09a^yFQzA;74Ax#{=n^ zIcpCvCh2c|oV-PGL9+3Z{mdSEeR2Bf#|6qZ=Wx%ep0&&;^J^}TV93=??t(iR8Sn!I zWN8*;4zX-PHV|tL$?Al_Iur=$Ybz2yP-Atp*+5ma*_KnWnc2RfC1S4T?f%q~jMSps z%)C?uU4_tK!v&dNC9|cU#H|rN5LNHYsDESzQ*mH)r+HOrY@0^O=MM9Z=PB$mbBpu- F007XRfQtYC literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/eth-blocks/block_302516 b/examples/traverse-ipld-graphs/eth-blocks/block_302516 new file mode 100644 index 0000000000000000000000000000000000000000..9c4a667658040dd45491fa8890b5708b6f25236c GIT binary patch literal 530 zcmey##J}J+e|*K~4QG@pW}V`Y`|2-Xc<5Z#^2fKu8z+7I`ix)X#RAzgzAx?_kF8qW zx;1^w z^*cMsbmFCLzSB?Hy414Wee5q=wlb-3>4`V{BNm+AWb=mY80YeRWjlk5GIH-!>!q$# z{yx>Bz38C6Z_cqr3j~gHPjfN5zs>Q%^6d>zJfams(jIXp={QcE7Cy@~C`f82BLfz| zfe=#wG8S|a9Z-syMDDM&3!EzMPM4Gr=Sa9uFnN^|1|nJrS`rki75=k+{~=MCNv aCz^5a!S)QX#YIS~LwB85W$ literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/eth-blocks/block_302517 b/examples/traverse-ipld-graphs/eth-blocks/block_302517 new file mode 100644 index 0000000000000000000000000000000000000000..3df292fd2d8fedbed277049fac4886e2c583ff0c GIT binary patch literal 527 zcmey##IqpVF2wQuKbIhx$3NuDcTbw}zW&zRpNf)_r~UuhW@O}?T_AhL_r<;Au~n;E zx2A79b4BX1kgN2Rt`gx73!VNtT$$`XCGiBu4K1sqMuw%lYY*C#S4SFh-&~+G;kPoI z-4j2%Io?zBwHABF?{7Bh)V!ROR^$`>@cb;^1z|7b#ih@LF8kl?`fSpHA9>Lp52R=2 ztUbV(q`&oX@)kvc3U)FwjC9aeWYBK0;%c*js(7<4k4!^L#9YnC{ZDo@Ki#|X`O convert(format)) + ], + hashers: [{ + name: 'keccak-256', + code: 0x1b, + digest: async (buf) => { + return MultihashDigest.create( + 0x1b, + new Uint8Array(sha3.keccak256.arrayBuffer(buf)) + ) + } + }] + } + }) + + console.log('\nStart of the example:') + + const ethBlocks = [ + path.join(__dirname, '/eth-blocks/block_302516'), + path.join(__dirname, '/eth-blocks/block_302517') + ] + + for (const ethBlockPath of ethBlocks) { + const data = await fs.readFile(ethBlockPath) + + const cid = await ipfs.block.put(data, { + format: 'eth-block', + mhtype: 'keccak-256', + version: 1 + }) + + console.log(cid.toString()) + } + + const block302516 = CID.parse('z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA') + const block302517 = CID.parse('z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj') + let res + + res = await ipfs.dag.get(block302516, { path: 'number' }) + console.log(uint8ArrayToString(res.value, 'base16')) + + res = await ipfs.dag.get(block302517, { path: 'parent/number' }) + console.log(uint8ArrayToString(res.value, 'base16')) + + await ipfs.stop() +} + +main() + .catch(err => { + console.error(err) + process.exit(1) + }) diff --git a/examples/traverse-ipld-graphs/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593 b/examples/traverse-ipld-graphs/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593 new file mode 100644 index 0000000000000000000000000000000000000000..4fc8373bcd9be6ee605436668ca20a3b839826a7 GIT binary patch literal 186 zcmb7-O$x#=5QSam6tfp((wJ!iMO?ViTjXa-BTYw21dngP6S#Z4kN3SKwU!2Yw1>7c zLry+sZP`VqBU!T9jD}{P47v_t41}{WL4|i)WQT_=8{C9pdiIosulx!1wel%Ry@0lO bChciJIO2e)dfWdR|G!lCt+WzW_$kyE+mJZY literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe b/examples/traverse-ipld-graphs/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe new file mode 100644 index 0000000000000000000000000000000000000000..b762482e170f3501ec3d4b2ffbf8542543a42941 GIT binary patch literal 22 dcmYew$xl)+G+}VhEK%?b@NrQ{NzKjY0su;N24MgI literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a b/examples/traverse-ipld-graphs/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a new file mode 100644 index 0000000000000000000000000000000000000000..d50e6d186b399089c91d990b04d795bb99f898e7 GIT binary patch literal 29 kcmYew$xl)+GGs_i&d66#_ejmj$=6W`&o9bJQB>yw0EN8>qW}N^ literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249 b/examples/traverse-ipld-graphs/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249 new file mode 100644 index 0000000000000000000000000000000000000000..f6de31c9fc49d2d041ff559f0f37166e31c652fb GIT binary patch literal 72 zcmXRZN=;QTGhr|^FfcPQQ3!H%bn$g%5U>8&eVqMj^KE>Tl$StL}B_YPa$w|;*35M z#6w1b(cU`mTyX6;NHnb#-}=T#j5atKRsL+CKCSC5t6Uy0U2(-fPI%BcUGH>Cpl&W2 f5{`I>bz4xC8&eVqMw2+xBI`Q|Ci=o)~$XNCY|SM Ee(tzLtN;K2 literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c b/examples/traverse-ipld-graphs/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c new file mode 100644 index 0000000000000000000000000000000000000000..78a038b49be60bc37986ac28bea9c7fdd8182d6a GIT binary patch literal 41 xcmXRZN=;QTHfAt1FfcbYRme!q$;oF>KBM~0@HBgF>{I5P_Vm8x!lzed0RRmp4&49% literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21 b/examples/traverse-ipld-graphs/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21 new file mode 100644 index 0000000000000000000000000000000000000000..27296bc7a78359868dda94a9423c666ba5376af2 GIT binary patch literal 28 jcmYew$xl)+GGIte&d66#&q&S5$yX@PFUm<#RObQ!fU*d- literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git.js b/examples/traverse-ipld-graphs/git.js new file mode 100644 index 00000000..f14b6be1 --- /dev/null +++ b/examples/traverse-ipld-graphs/git.js @@ -0,0 +1,82 @@ +'use strict' + +const createNode = require('./create-node') +const path = require('path') +const { CID } = require('multiformats/cid') +const MultihashDigest = require('multiformats/hashes/digest') +const fs = require('fs').promises +const uint8ArrayToString = require('uint8arrays/to-string') +const { convert } = require('ipld-format-to-blockcodec') +const crypto = require('crypto') + +async function main () { + const ipfs = await createNode({ + ipld: { + codecs: [ + convert(require('ipld-git')) + ], + hashers: [{ + name: 'sha1', + code: 0x11, + digest: async (buf) => { + return MultihashDigest.create(0x11, crypto.createHash('sha1').update(buf).digest()) + } + }] + } + }) + + console.log('\nStart of the example:') + + const gitObjects = [ + path.join(__dirname, '/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593'), + path.join(__dirname, '/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe'), + path.join(__dirname, '/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a'), + path.join(__dirname, '/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249'), + path.join(__dirname, '/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d'), + path.join(__dirname, '/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe'), + path.join(__dirname, '/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a'), + path.join(__dirname, '/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f'), + path.join(__dirname, '/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c'), + path.join(__dirname, '/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21') + ] + + await Promise.all(gitObjects.map(async gitObjectsPath => { + const data = await fs.readFile(gitObjectsPath) + + const cid = await ipfs.block.put(data, { + format: 'git-raw', + mhtype: 'sha1', + version: 1 + }) + + console.log(cid.toString()) + })) + + const v1tag = CID.parse('z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue') + + async function logResult (fn, comment) { + const result = await fn() + + if (result.value instanceof Uint8Array) { // Blobs (files) are returned as buffer instance + result.value = uint8ArrayToString(result.value) + } + + console.log('-'.repeat(80)) + console.log(comment) + console.log(result.value) + } + + await logResult(() => ipfs.dag.get(v1tag), 'Tag object:') + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/message' }), 'Tagged commit message:') + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/message' }), 'Parent of tagged commit:') + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/tree/src/hash/hello/hash' }), '/src/hello file:') + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/tree/src/hash/hello/hash' }), 'previous version of /src/hello file:') + + await ipfs.stop() +} + +main() + .catch(err => { + console.error(err) + process.exit(1) + }) diff --git a/examples/traverse-ipld-graphs/package.json b/examples/traverse-ipld-graphs/package.json index 0d8cbc95..799b864f 100644 --- a/examples/traverse-ipld-graphs/package.json +++ b/examples/traverse-ipld-graphs/package.json @@ -16,7 +16,9 @@ "@ipld/dag-pb": "^2.1.3", "ipfs": "^0.56.0", "ipld-ethereum": "^6.0.0", + "ipld-format-to-blockcodec": "0.0.1", "ipld-git": "^0.6.1", + "js-sha3": "^0.8.0", "multiformats": "^9.4.1" }, "devDependencies": { diff --git a/examples/traverse-ipld-graphs/tests/test.js b/examples/traverse-ipld-graphs/tests/test.js index c0bb0f3b..045cd31a 100644 --- a/examples/traverse-ipld-graphs/tests/test.js +++ b/examples/traverse-ipld-graphs/tests/test.js @@ -16,6 +16,12 @@ async function runTest () { console.info('Testing get-path-accross-formats.js') await node.waitForOutput('capoeira', 'node', [path.resolve(__dirname, '../get-path-accross-formats.js')]) + console.info('Testing eth.js') + await node.waitForOutput('302516', 'node', [path.resolve(__dirname, '../eth.js')]) + + console.info('Testing git.js') + await node.waitForOutput("CID(baf4bcfhoohhpkaa3qsydcrby65wpblgthcrp2ii)", 'node', [path.resolve(__dirname, '../git.js')]) + console.info('Done!') }