Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Generate an IPFS address outside of IPFS (JavaScript) #208

jcalfee opened this issue Dec 6, 2016 · 4 comments

Generate an IPFS address outside of IPFS (JavaScript) #208

jcalfee opened this issue Dec 6, 2016 · 4 comments


Copy link

jcalfee commented Dec 6, 2016

I would like to create IPFS links but not actually insert the data into IPFS now. Can you provide a minimal example to do this?

Apparently some of the action happens here:

Here are my notes, I'm not sure how to put these together:

echo -n multihash > multihash.txt

ipfs add multihash.txt
added QmZLXzjiZU39eN8QirMZ2CGXjMLiuEkQriRu7a7FeSB4fg multihash.txt

# The desired output: QmZLXzjiZU39eN8QirMZ2CGXjMLiuEkQriRu7a7FeSB4fg

sha256sum multihash.txt
9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47  multihash.txt


> var bs58=require('bs58')
bs58.encode(new Buffer('9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47','hex'))

> var mh=require('multihashes')
mh.toB58String(mh.encode(new Buffer('9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47','hex'), 'sha2-256'))

var Unixfs = require('ipfs-unixfs')
var data = new UnixFS('file', 'multihash')

> data
Data {
  type: 'file',
  data: 'multihash',
  blockSizes: [],
  addBlockSize: [Function],
  removeBlockSize: [Function],
  fileSize: [Function],
  marshal: [Function] }

> data.marshal()
<Buffer 08 02 12 09 6d 75 6c 74 69 68 61 73 68 18 09>

const ipfsMDAG = require('ipfs-merkle-dag')
var link = new ipfsMDAG.DAGLink('multihash.txt', 9, new Buffer('9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47', 'hex'))
> link
DAGLink {
  name: 'multihash.txt',
  size: 9,
  hash: <Buffer 9c bc 07 c3 f9 91 72 58 36 a3 aa 2a 58 1c a2 02 91 98 aa 42 0b 9d 99 bc 0e 13 1d 9f 3e 2c be 47> }

// what should I sha256 hash here???

var mh=require('multihashes')
var sha=crypto.createHash('sha256').update(data.marshal()).digest()
mh.toB58String(mh.encode(sha, 'sha2-256'))
Copy link

jcalfee commented Dec 6, 2016

I guess I need to extract my example from createAndStoreFile here:

Copy link

jcalfee commented Dec 6, 2016

I see that this is probably going to be a small project. I did have some success with small files, but this does not handle chunking:

const UnixFS = require('ipfs-unixfs')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode

const l = new UnixFS('file', new Buffer('multihash'))
DAGNode.create(l.marshal(), (err, node) => {console.log(node)})

An alternative would be nice: generate the IPFS hash without running the IPFS daemon. The createAndStoreFile method implies that it will not give me the hash without trying to store it.

@jcalfee jcalfee closed this as completed Dec 7, 2016
Copy link

@jcalfee This is awesome. can you tell me the JavaScript code for the same so that i can embed it to webpage.

Copy link

@jcalfee Thanks for the suggestion. actually i have referred that but i didn't find any function for only-hash.i have also submitted a query there. if the same nodejs code written by you can be converted to javascript code can solve my problem.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

No branches or pull requests

2 participants