Skip to content
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
25 changes: 25 additions & 0 deletions cid-vs-multihash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CID } from 'multiformats/cid'
import * as multihash from 'multiformats/hashes/digest'

/*
HUMAN READABLE CID
base32 - cidv1 - raw - (sha2-256 : 256 : FAE8D07F50DBBCD3FDD65610ADCD6C3DF2171B31AE97041B1E222506E78DFD94)
MULTIBASE - VERSION - MULTICODEC - MULTIHASH (NAME : SIZE : DIGEST IN HEX)
*/
const MY_CID = 'bafkreih25dih6ug3xtj73vswccw423b56ilrwmnos4cbwhrceudopdp5sq'
console.log('CID string', MY_CID)

// Found via https://cid.contact/cid/bafkreih25dih6ug3xtj73vswccw423b56ilrwmnos4cbwhrceudopdp5sq
const MULTIHASH = 'EiD66NB/UNu80/3WVhCtzWw98hcbMa6XBBseIiUG5439lA=='
console.log('IPNI Multihash (base64)', MULTIHASH)
console.log('IPNI multihash (hex)', Buffer.from(MULTIHASH, 'base64').toString('hex'))

const cid = CID.parse(MY_CID)
console.log('CID multihash (hex) ', Buffer.from(cid.multihash.bytes).toString('hex'))
console.log('CID codec', '0x' + cid.code.toString(16))

const cid2 = CID.create(cid.version, 0x70 /* dag-pb */, cid.multihash)
console.log('dag-pb CID', cid2)

console.log('CID FROM MULTIHASH', CID.create(1, 0x55 /* raw */, multihash.decode(Buffer.from(MULTIHASH, 'base64'))))
console.log('ORIGINAL CID ', cid)
295 changes: 292 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
"bugs": {
"url": "https://github.com/filecoin-station/piece-indexer/issues"
},
"homepage": "https://github.com/filecoin-station/piece-indexer#readme"
"homepage": "https://github.com/filecoin-station/piece-indexer#readme",
"dependencies": {
"@ipld/dag-cbor": "^9.2.1",
"@multiformats/multiaddr-to-uri": "^10.1.0",
"multiformats": "^13.1.3"
}
}
31 changes: 31 additions & 0 deletions parse-metada.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { varint } from 'multiformats'
import * as cbor from '@ipld/dag-cbor'

// console.log(parseMetadata(
// 'kBKjaFBpZWNlQ0lE2CpYKAABgeIDkiAgieDL6/pbxDBAJtdZ19ZsvGh1NE77nBxbFwesoG1S/jJsVmVyaWZpZWREZWFs9W1GYXN0UmV0cmlldmFs9Q'
// ))

export function parseMetadata (meta) {
const bytes = Buffer.from(meta, 'base64')
const [protocolCode, nextOffset] = varint.decode(bytes)

const protocol = {
0x900: 'bitswap',
0x910: 'graphsync',
0x0920: 'http'
}[protocolCode] ?? '0x' + protocolCode.toString(16)

if (protocol === 'graphsync') {
// console.log(bytes.subarray(nextOffset).toString('hex'))

/** @type {{
PieceCID: import('multiformats/cid').CID,
VerifiedDeal: boolean,
FastRetrieval: boolean
}} */
const deal = cbor.decode(bytes.subarray(nextOffset))
return { protocol, deal }
} else {
return { protocol }
}
}
Loading