Skip to content

Instantly share code, notes, and snippets.

@mikeal
Created October 20, 2020 20:24
Show Gist options
  • Save mikeal/879a6d5b3e32360ffe0c0c41fa8e2c9a to your computer and use it in GitHub Desktop.
Save mikeal/879a6d5b3e32360ffe0c0c41fa8e2c9a to your computer and use it in GitHub Desktop.
Filecoin Liftoff
import * as Block from 'multiformats/block'
import { sha256 as hasher } from 'multiformats/hashes/sha2'
import * as codec from '@ipld/dag-cbor'
import { CID } from 'multiformats'
let value = { hello: 'world' }
let hello = await Block.encode({ value, codec, hasher })
let bbbb = await Block.encode({ value: { foo: hello.cid }, codec, hasher })
let cccc = await Block.encode({ value: { bar: hello.cid, prop: 31337 }, codec, hasher })
let dddd = await Block.encode({ value: { one: bbbb.cid, two: cccc.cid }, codec, hasher })
const putBlock = async (ipfs, block) => {
const opts = { cid: block.cid.toString() }
await ipfs.block.put(block.bytes, opts)
return true
}
const getBlock = async (ipfs, cid) => {
const { data } = await ipfs.block.get(cid.toString())
return Block.create({ bytes: data, cid, codec, hasher })
}
import * as Block from 'multiformats/block'
import { sha256 as hasher } from 'multiformats/hashes/sha2'
import * as codec from '@ipld/dag-cbor'
import { CID } from 'multiformats'
let value = { hello: 'world' }
let hello = await Block.encode({ value, codec, hasher })
// hello.bytes - encoded binary data
// hello.cid - CID address
let bbbb = await Block.encode({ value: { foo: hello.cid }, codec, hasher })
let cccc = await Block.encode({ value: { bar: hello.cid, prop: 31337 }, codec, hasher })
let dddd = await Block.encode({ value: { one: bbbb.cid, two: cccc.cid }, codec, hasher })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment