Skip to content

Commit 314c1eb

Browse files
author
Simone Sanfratello
authored
fix/cid prefix (#16)
* fix: protocol block prefix * build: v0.5.1
1 parent d666794 commit 314c1eb

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "e-ipfs-core-lib",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "E-IPFS core library",
55
"license": "(Apache-2.0 AND MIT)",
66
"homepage": "https://github.com/elastic-ipfs/core-lib",
@@ -26,6 +26,7 @@
2626
"protobufjs": "^7.1.2",
2727
"sodium-native": "^3.4.1",
2828
"undici": "^5.13.0",
29+
"varint": "^6.0.0",
2930
"xml-js": "^1.6.11"
3031
},
3132
"devDependencies": {

src/protocol.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { default as protobuf } from 'protobufjs'
44
import path from 'path'
55

6-
import { dirname } from '../src/util.js'
6+
import { dirname, varintEncoder } from '../src/util.js'
77
import { CID } from 'multiformats/cid'
88

99
const definitions = protobuf.loadSync(path.join(dirname(import.meta.url), './bitswap.proto'))
@@ -144,15 +144,17 @@ class WantList {
144144
class Block {
145145
constructor (prefixOrCid, data) {
146146
if (prefixOrCid instanceof CID) {
147-
prefixOrCid = Buffer.from([
148-
prefixOrCid.version,
149-
prefixOrCid.code,
150-
prefixOrCid.multihash.bytes[0],
151-
prefixOrCid.multihash.bytes[1]
147+
const version = prefixOrCid.version
148+
const codec = prefixOrCid.code
149+
const multihash = prefixOrCid.multihash.code
150+
const digestLength = prefixOrCid.multihash.digest.length
151+
this.prefix = varintEncoder([
152+
version, codec, multihash, digestLength
152153
])
154+
} else {
155+
this.prefix = prefixOrCid
153156
}
154157

155-
this.prefix = prefixOrCid
156158
this.data = data
157159
}
158160

src/util.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import fs from 'fs'
33
import path from 'path'
44
import url from 'url'
5+
import * as varint from 'varint'
56
import { base58btc as base58 } from 'multiformats/bases/base58'
67

78
function dirname (importMetaUrl) {
@@ -21,4 +22,26 @@ function cidToKey (cid) {
2122
}
2223
}
2324

24-
export { dirname, version, cidToKey }
25+
/**
26+
* from https://github.com/ipfs/js-ipfs-bitswap
27+
* @param {Array<number>} buf
28+
* @returns {Uint8Array}
29+
*/
30+
function varintEncoder (buf) {
31+
let out = new Uint8Array(buf.reduce((acc, curr) => {
32+
return acc + varint.default.encodingLength(curr)
33+
}, 0))
34+
35+
let offset = 0
36+
37+
for (const num of buf) {
38+
out = varint.encode(num, out, offset)
39+
40+
// @ts-expect-error types are wrong
41+
offset += varint.default.encodingLength(num)
42+
}
43+
44+
return out
45+
}
46+
47+
export { dirname, version, cidToKey, varintEncoder }

0 commit comments

Comments
 (0)