Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

feat: support identity hash in block.get + dag.get #3616

Merged
merged 8 commits into from
Apr 28, 2021
12 changes: 10 additions & 2 deletions packages/interface-ipfs-core/src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict'

const uint8ArrayFromString = require('uint8arrays/from-string')
const multihash = require('multihashing-async').multihash
const multihashing = require('multihashing-async')
const CID = require('cids')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const testTimeout = require('../utils/test-timeout')
Expand Down Expand Up @@ -43,7 +43,7 @@ module.exports = (common, options) => {
})

it('should get by CID in string', async () => {
const block = await ipfs.block.get(multihash.toB58String(hash))
const block = await ipfs.block.get(multihashing.multihash.toB58String(hash))

expect(block.data).to.eql(uint8ArrayFromString('blorb'))
expect(block.cid.multihash).to.eql(hash)
Expand Down Expand Up @@ -89,6 +89,14 @@ module.exports = (common, options) => {
expect(block.data).to.eql(input)
})

it('should get a block with an identity CID, without putting first', async () => {
const identityData = uint8ArrayFromString('A16461736466190144', 'base16upper')
const identityHash = await multihashing(identityData, 'identity')
const identityCID = new CID(1, 'dag-cbor', identityHash)
const block = await ipfs.block.get(identityCID)
expect(block.data).to.eql(identityData)
})

it('should return an error for an invalid CID', () => {
return expect(ipfs.block.get('Non-base58 character')).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
Expand Down
9 changes: 9 additions & 0 deletions packages/interface-ipfs-core/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const all = require('it-all')
const CID = require('cids')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const testTimeout = require('../utils/test-timeout')
const multihashing = require('multihashing-async')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -240,6 +241,14 @@ module.exports = (common, options) => {
expect(result.value).to.deep.equal(buf)
})

it('should be able to get a dag-cbor node with the identity hash', async () => {
const identityData = uint8ArrayFromString('A16461736466190144', 'base16upper')
const identityHash = await multihashing(identityData, 'identity')
const identityCID = new CID(1, 'dag-cbor', identityHash)
const result = await ipfs.dag.get(identityCID)
expect(result.value).to.deep.equal({ asdf: 324 })
})

it('should throw error for invalid string CID input', () => {
return expect(ipfs.dag.get('INVALID CID'))
.to.eventually.be.rejected()
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-core/src/components/repo/gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const BLOCK_RM_CONCURRENCY = 256
* @typedef {import('ipfs-core-types/src/refs').API} RefsAPI
* @typedef {import('ipfs-repo')} IPFSRepo
* @typedef {import('interface-datastore').Key} Key
* @typedef {import('ipld-block')} Block
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-server/src/api/resources/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exports.get = {
throw Boom.notFound('Block was unwanted before it could be remotely retrieved')
}

return h.response(block.data).header('X-Stream-Output', '1')
return h.response(Buffer.from(block.data.buffer, block.data.byteOffset, block.data.byteLength)).header('X-Stream-Output', '1')
}
}
exports.put = {
Expand Down