Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipfs connection to local or embedded node #86

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ <h1>dmap</h1>

<div id="result"></div>

<!--<script defer src="dependencies.js" crossorigin="no-cors"></script>-->
<!--<script defer src="main.js"></script>-->

<script defer src="dependencies.js"></script>
<script defer src="main.js"></script>

Expand Down
25 changes: 5 additions & 20 deletions dmap.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const ebnf = require('ebnf')
const multiformats = require('multiformats')

const ethers = require('ethers')
const { b32 } = require('minihat');

const multiformats = require('multiformats')
const abi = require('./artifacts/core/dmap.sol/Dmap.json').abi
const dmap_i = new ethers.utils.Interface(abi)

Expand Down Expand Up @@ -91,11 +88,11 @@ lib.walk = async (dmap, path) => {
if (zone === '0x' + '00'.repeat(20)) {
fail(`zero register`)
}
const fullname = '0x' + lib._strToHex(step.name) + '00'.repeat(32-step.name.length);
const fullname = '0x' + Buffer.from(step.name).toString('hex') + '00'.repeat(32-step.name.length);
[meta, data] = await lib.get(dmap, zone, fullname)
if (step.locked) {
need(ctx.locked, `Encountered ':' in unlocked subpath`)
need((lib._hexToArrayBuffer(meta)[0] & lib.FLAG_LOCK) !== 0, `Entry is not locked`)
need((Buffer.from(meta.slice(2), 'hex')[0] & lib.FLAG_LOCK) !== 0, `Entry is not locked`)
ctx.locked = true
}
ctx.locked = step.locked
Expand All @@ -118,8 +115,8 @@ lib.prepareCID = (cidStr, lock) => {
}

lib.unpackCID = (metaStr, dataStr) => {
const meta = lib._hexToArrayBuffer(metaStr)
const data = lib._hexToArrayBuffer(dataStr)
const meta = Buffer.from(metaStr.slice(2), 'hex')
const data = Buffer.from(dataStr.slice(2), 'hex')
const prefixLen = meta[prefLenIndex]
const specs = multiformats.CID.inspectBytes(meta.slice(-prefixLen))
const hashLen = specs.digestSize
Expand All @@ -135,15 +132,3 @@ lib.readCID = async (dmap, path) => {
const packed = await lib.walk(dmap, path)
return lib.unpackCID(packed.meta, packed.data)
}

lib._hexToArrayBuffer = hex => {
const bytes = []
for (let c = 2; c < hex.length; c += 2)
bytes.push(parseInt(hex.slice(c, c + 2), 16))
return new Uint8Array(bytes)
}

lib._strToHex = str => {
let codes = str.split('').map(c => c.charCodeAt(0))
return codes.map(c => c.toString(16)).join('')
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
"@nomiclabs/hardhat-ethers": "^2.0.5",
"@types/mocha": "^9.0.0",
"hardhat": "^2.8.3",
"ipfs-core": "^0.14.2",
"ipfs-http-client": "^56.0.2",
"ipfs-provider": "^2.1.0",
"minihat": "^0.0.5",
"v8": "^0.1.0",
"web3-utils": "^1.7.1",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
},
"dependencies": {
"ebnf": "^1.9.0"
"ebnf": "^1.9.0",
"node-polyfill-webpack-plugin": "^1.1.4"
}
}
34 changes: 25 additions & 9 deletions view/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { ethers } from 'ethers'
const dpack = require('@etherpacks/dpack')
const { getIpfs, providers } = require('ipfs-provider')
const { httpClient, jsIpfs } = providers
const dmap = require('../dmap.js')
const dmapAddress = '0x7fA88e1014B0640833a03ACfEC71F242b5fBDC85'
const dmapArtifact = require('../artifacts/core/dmap.sol/Dmap.json')

window.onload =()=> {
window.onload = async() => {
const $ = document.querySelector.bind(document);
const result = $('#result')

const line =s=> { $('#result').textContent += s + '\n' }
const node = await getIpfs({
providers: [
// attempt to use local node, if unsuccessful fallback to running embedded core js-ipfs in-page
httpClient({
loadHttpClientModule: () => require('ipfs-http-client'),
apiAddress: '/ip4/127.0.0.1/tcp/5001'
}),
jsIpfs({
loadJsIpfsModule: () => require('ipfs-core'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably going to be overkill
Instead we probably want to just give the user a box to put their own rpc endpoint to use if they can't use one of a small set of default gateways
The important thing is to make sure the client actually verifies the CIDs when it grabs files from somewhere

options: { }
})
]
})

$('#btnGet').addEventListener('click', async () => {
const dpath = $('#dpath').value;
Expand All @@ -32,16 +44,20 @@ window.onload =()=> {
}

try {
// display json content from a CID if we can
// display ipfs content from a CID if we can, otherwise display as text
const cidResult = dmap.unpackCID(walkResult.meta, walkResult.data)
line(`ipfs: ${cidResult}`)
const ipfsResult = await dpack.getIpfsJson(cidResult)
line(JSON.stringify(ipfsResult, null, 4))
const ipfsResult = await node.ipfs.cat(cidResult)
let s = ''
let utf8decoder = new TextDecoder()
for await (const chunk of ipfsResult) {
s += utf8decoder.decode(chunk)
}
line(s)
}
catch(e){
// otherwise show text
let utf8decoder = new TextDecoder()
const bytes = dmap._hexToArrayBuffer(walkResult.data)
const bytes = Buffer.from(walkResult.data.slice(2), 'hex')
let i
for (i = 0; i < bytes.length; i++) {
if (bytes[bytes.length -1 - i] !== 0) {
Expand Down
7 changes: 7 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const webpack = require('webpack');

module.exports = {
mode: 'production',
Expand All @@ -9,6 +10,12 @@ module.exports = {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process/browser'
})
],
optimization: {
minimize: false,
splitChunks: {
Expand Down