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

fix CID utils for lock location #111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 1 addition & 36 deletions dmap.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const ebnf = require('ebnf')
const ethers = require('ethers')
const multiformats = require('multiformats')

const pack = require('./pack/premap.dpack.json')
const pack = require('./pack/dmap_full_ethereum.dpack.json')
const artifact = require('./pack/Dmap.json')

const abi = artifact.abi
Expand All @@ -14,7 +13,6 @@ const need =(b,s)=> b || fail(s)

const coder = ethers.utils.defaultAbiCoder
const keccak256 = ethers.utils.keccak256
const prefLenIndex = 2

module.exports = lib = {}

Expand Down Expand Up @@ -98,36 +96,3 @@ lib.walk = async (dmap, path) => {
}
return {meta, data}
}

lib.prepareCID = (cidStr, lock) => {
const cid = multiformats.CID.parse(cidStr)
need(cid.multihash.size <= 32, `Hash exceeds 256 bits`)
const prefixLen = cid.byteLength - cid.multihash.size
const meta = new Uint8Array(32).fill(0)
const data = new Uint8Array(32).fill(0)

data.set(cid.bytes.slice(-cid.multihash.size), 32 - cid.multihash.size)
meta.set(cid.bytes.slice(0, prefixLen), 32 - prefixLen)
if (lock) meta[0] |= lib.FLAG_LOCK
meta[prefLenIndex] = prefixLen
return [meta, data]
}

lib.unpackCID = (metaStr, 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
const cidBytes = new Uint8Array(prefixLen + hashLen)

cidBytes.set(meta.slice(-prefixLen), 0)
cidBytes.set(data.slice(32 - hashLen), prefixLen)
const cid = multiformats.CID.decode(cidBytes)
return cid.toString()
}

lib.readCID = async (dmap, path) => {
const packed = await lib.walk(dmap, path)
return lib.unpackCID(packed.meta, packed.data)
}
Loading