Skip to content

Instantly share code, notes, and snippets.

@pcardune
Last active February 15, 2018 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcardune/411c93eb4dd7d15a642b3750a4bd8cfa to your computer and use it in GitHub Desktop.
Save pcardune/411c93eb4dd7d15a642b3750a4bd8cfa to your computer and use it in GitHub Desktop.
Simple example of storing data in your local ipfs node using ipld. Note that this only works when your local ipfs node is not running.
import dagCBOR from 'ipld-dag-cbor';
import IPLDResolver from 'ipld-resolver';
import BlockService from 'ipfs-block-service';
import IPFSRepo from 'ipfs-repo';
const repo = new IPFSRepo(process.env.HOME + '/.ipfs');
const ipfsBlockService = new BlockService(repo);
const resolver = new IPLDResolver(ipfsBlockService);
const node1 = {
some: 'data',
that: {
is: 'structured',
},
};
repo.open(() => {
dagCBOR.util.cid(node1, (err, cid1) => {
console.log('cid for', node1, 'is', cid1.toBaseEncodedString());
resolver.put(node1, { cid: cid1 }, (err, cid) => {
console.log('put', cid1.toBaseEncodedString(), 'in ipld');
console.log('got error', err);
console.log('got cid', cid.toBaseEncodedString());
resolver.get(cid1, value => {
console.log('got value back...', value);
repo.close(() => {
console.log('all done!');
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment