-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export blockstore key encode/decode utils (#206)
* feat: export blockstore key encode/decode utils These have already been duplicated twice in js-ipfs. This exposes utility functions to encode a CID as a blockstore Key and decode a blockstore Key to a CID. refs https://github.com/ipfs/js-ipfs/pull/2022/files#r303389863 License: MIT Signed-off-by: Alan Shaw <[email protected]> * chore: appease linter License: MIT Signed-off-by: Alan Shaw <[email protected]>
- Loading branch information
Showing
7 changed files
with
64 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
const base32 = require('base32.js') | ||
const { Key } = require('interface-datastore') | ||
const CID = require('cids') | ||
|
||
/** | ||
* Transform a cid to the appropriate datastore key. | ||
* | ||
* @param {CID} cid | ||
* @returns {Key} | ||
*/ | ||
exports.cidToKey = cid => { | ||
const enc = new base32.Encoder() | ||
return new Key('/' + enc.write(cid.buffer).finalize(), false) | ||
} | ||
|
||
/** | ||
* Transform a datastore Key instance to a CID | ||
* | ||
* @param {Key} key | ||
* @returns {CID} | ||
*/ | ||
exports.keyToCid = key => { | ||
// Block key is of the form /<base32 encoded string> | ||
const decoder = new base32.Decoder() | ||
const buff = decoder.write(key.toString().slice(1)).finalize() | ||
return new CID(Buffer.from(buff)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
const { expect } = chai | ||
const { Key } = require('interface-datastore') | ||
const CID = require('cids') | ||
const Repo = require('../src') | ||
|
||
module.exports = () => { | ||
describe('blockstore utils', () => { | ||
it('converts a CID to a datastore Key and back', () => { | ||
const originalCid = new CID('Qme6KJdKcp85TYbLxuLV7oQzMiLremD7HMoXLZEmgo6Rnh') | ||
const key = Repo.utils.blockstore.cidToKey(originalCid) | ||
expect(key instanceof Key).to.be.true() | ||
const cid = Repo.utils.blockstore.keyToCid(key) | ||
expect(cid instanceof CID).to.be.true() | ||
expect(originalCid.toString()).to.equal(cid.toString()) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters