From 85eb1752453da0dd16608f37dbd7a071fc9ef967 Mon Sep 17 00:00:00 2001 From: "pl.despaigne" Date: Wed, 23 Jan 2019 11:18:03 +0100 Subject: [PATCH] adding varint encoding & remove hardcoded varint values -> #11 --- dist/index.js | 27 ++++++++++++++++++++------- index.js | 25 +++++++++++++++++++------ package.json | 3 ++- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/dist/index.js b/dist/index.js index bd26965..ac95752 100644 --- a/dist/index.js +++ b/dist/index.js @@ -18,6 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +const varint = require('varint') const multiC = require('multicodec') const multiH = require('multihashes') @@ -61,9 +62,11 @@ module.exports = { let buffer = hexString(hash) + const codec = Buffer.from(varint.decode(buffer).toString(16), 'hex') // get decoded varint codec + const value = buffer.slice(varint.decode.bytes + 1 + 1) // remove the codec bytes plus de cid version byte plus merkle dag codec // TODO use js-cid instead of +1 +1 - const codec = buffer.slice(0, 1) - const value = buffer.slice(4) + // const codec = buffer.slice(0, 1) + // const value = buffer.slice(4) let res = value.toString('hex') if (codec.compare(this.Types.swarm) === 0){// TODO change that for "multiC.getCodec(res) === 'swarm-ns'" when multicodec will be updated @@ -87,8 +90,13 @@ module.exports = { let res = multiC.addPrefix('dag-pb', multihash) // adding MerkleDAG codec : 0x70 res = hexString('01' + res.toString('hex')) // adding CID v1 : 0x01 - res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) - res = Buffer.concat([this.Types.ipfs, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('ipfs-ns', res)' when multicodec will be updated + // res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) + // res = Buffer.concat([this.Types.ipfs, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('ipfs-ns', res)' when multicodec will be updated + + let codec = Buffer.from(varint.encode(parseInt(this.Types.ipfs.toString('hex'), 16))) // TODO remove that when multicodec will be updated + // let codec = Buffer.from(varint.encode(parseInt(multiC.getCodeVarint('ipfs-ns'), 16))) // TODO use that when multicodec will be updated + res = Buffer.concat([codec, res]) + return res.toString('hex') }, @@ -103,8 +111,13 @@ module.exports = { let res = multiC.addPrefix('dag-pb', multihash) // adding MerkleDAG codec : 0x70 res = hexString('01' + res.toString('hex')) // adding CID v1 : 0x01 - res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) - res = Buffer.concat([this.Types.swarm, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('swarm-ns', res)' when multicodec will be updated + // res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) + // res = Buffer.concat([this.Types.swarm, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('swarm-ns', res)' when multicodec will be updated + + let codec = Buffer.from(varint.encode(parseInt(this.Types.swarm.toString('hex'), 16))) // TODO remove that when multicodec will be updated + // let codec = Buffer.from(varint.encode(parseInt(multiC.getCodeVarint('ipfs-ns'), 16))) // TODO use that when multicodec will be updated + res = Buffer.concat([codec, res]) + return res.toString('hex') }, @@ -155,7 +168,7 @@ module.exports = { }, } }).call(this,require("buffer").Buffer) -},{"buffer":5,"multicodec":8,"multihashes":13}],2:[function(require,module,exports){ +},{"buffer":5,"multicodec":8,"multihashes":13,"varint":17}],2:[function(require,module,exports){ // base-x encoding / decoding // Copyright (c) 2018 base-x contributors // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp) diff --git a/index.js b/index.js index 1f4dc96..d20f5b5 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +const varint = require('varint') const multiC = require('multicodec') const multiH = require('multihashes') @@ -59,9 +60,11 @@ module.exports = { let buffer = hexString(hash) + const codec = Buffer.from(varint.decode(buffer).toString(16), 'hex') // get decoded varint codec + const value = buffer.slice(varint.decode.bytes + 1 + 1) // remove the codec bytes plus de cid version byte plus merkle dag codec // TODO use js-cid instead of +1 +1 - const codec = buffer.slice(0, 1) - const value = buffer.slice(4) + // const codec = buffer.slice(0, 1) + // const value = buffer.slice(4) let res = value.toString('hex') if (codec.compare(this.Types.swarm) === 0){// TODO change that for "multiC.getCodec(res) === 'swarm-ns'" when multicodec will be updated @@ -85,8 +88,13 @@ module.exports = { let res = multiC.addPrefix('dag-pb', multihash) // adding MerkleDAG codec : 0x70 res = hexString('01' + res.toString('hex')) // adding CID v1 : 0x01 - res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) - res = Buffer.concat([this.Types.ipfs, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('ipfs-ns', res)' when multicodec will be updated + // res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) + // res = Buffer.concat([this.Types.ipfs, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('ipfs-ns', res)' when multicodec will be updated + + let codec = Buffer.from(varint.encode(parseInt(this.Types.ipfs.toString('hex'), 16))) // TODO remove that when multicodec will be updated + // let codec = Buffer.from(varint.encode(parseInt(multiC.getCodeVarint('ipfs-ns'), 16))) // TODO use that when multicodec will be updated + res = Buffer.concat([codec, res]) + return res.toString('hex') }, @@ -101,8 +109,13 @@ module.exports = { let res = multiC.addPrefix('dag-pb', multihash) // adding MerkleDAG codec : 0x70 res = hexString('01' + res.toString('hex')) // adding CID v1 : 0x01 - res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) - res = Buffer.concat([this.Types.swarm, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('swarm-ns', res)' when multicodec will be updated + // res = hexString('01' + res.toString('hex')) // adding uvarint // ! USE A LIB, DO NOT HARDCODE ! (see https://github.com/multiformats/unsigned-varint) + // res = Buffer.concat([this.Types.swarm, res]) // adding IPFS code : 0xef // TODO change that for 'multiC.addPrefix('swarm-ns', res)' when multicodec will be updated + + let codec = Buffer.from(varint.encode(parseInt(this.Types.swarm.toString('hex'), 16))) // TODO remove that when multicodec will be updated + // let codec = Buffer.from(varint.encode(parseInt(multiC.getCodeVarint('ipfs-ns'), 16))) // TODO use that when multicodec will be updated + res = Buffer.concat([codec, res]) + return res.toString('hex') }, diff --git a/package.json b/package.json index b84b6d0..7ba447a 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "license": "ISC", "dependencies": { "multicodec": "^0.4.0", - "multihashes": "^0.4.14" + "multihashes": "^0.4.14", + "varint": "^5.0.0" }, "devDependencies": { "browserify": "^16.2.3",