@@ -3,14 +3,14 @@ import { sha256 } from 'multiformats/hashes/sha2'
3
3
import { base58btc } from 'multiformats/bases/base58'
4
4
// @ts -ignore
5
5
import vd from 'varint-decoder'
6
- import { isMapEqual } from '../../utils/index.js'
6
+ import ve from '../utils/varint-encoder.js'
7
+ import { isMapEqual } from '../utils/index.js'
7
8
import { Message } from './message.js'
8
9
import { BitswapMessageEntry as Entry } from './entry.js'
9
- import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
10
10
import errcode from 'err-code'
11
11
12
12
/**
13
- * @typedef {import('multiformats/hashes/interface ').MultihashHasher } MultihashHasher
13
+ * @typedef {import('../types ').MultihashHasherLoader } MultihashHasherLoader
14
14
*/
15
15
16
16
export class BitswapMessage {
@@ -174,11 +174,13 @@ export class BitswapMessage {
174
174
175
175
for ( const [ cidStr , data ] of this . blocks . entries ( ) ) {
176
176
const cid = CID . parse ( cidStr )
177
- const codec = Uint8Array . from ( [ cid . code ] )
178
- const multihash = cid . multihash . bytes . subarray ( 0 , 2 )
179
- const prefix = uint8ArrayConcat ( [
180
- [ cid . version ] , codec , multihash
181
- ] , 1 + codec . byteLength + multihash . byteLength )
177
+ const version = cid . version
178
+ const codec = cid . code
179
+ const multihash = cid . multihash . code
180
+ const digestLength = cid . multihash . digest . length
181
+ const prefix = ve ( [
182
+ version , codec , multihash , digestLength
183
+ ] )
182
184
183
185
msg . payload . push (
184
186
new Message . Block ( {
@@ -230,9 +232,9 @@ export class BitswapMessage {
230
232
231
233
/**
232
234
* @param {Uint8Array } raw
233
- * @param {Record<number, MultihashHasher> } [hashers ]
235
+ * @param {MultihashHasherLoader } [hashLoader ]
234
236
*/
235
- BitswapMessage . deserialize = async ( raw , hashers = { } ) => {
237
+ BitswapMessage . deserialize = async ( raw , hashLoader ) => {
236
238
const decoded = Message . decode ( raw )
237
239
238
240
const isFull = ( decoded . wantlist && decoded . wantlist . full ) || false
@@ -286,7 +288,7 @@ BitswapMessage.deserialize = async (raw, hashers = {}) => {
286
288
const cidVersion = values [ 0 ]
287
289
const multicodec = values [ 1 ]
288
290
const hashAlg = values [ 2 ]
289
- const hasher = hashAlg === sha256 . code ? sha256 : hashers [ hashAlg ]
291
+ const hasher = hashAlg === sha256 . code ? sha256 : hashLoader && await hashLoader . getHasher ( hashAlg )
290
292
291
293
if ( ! hasher ) {
292
294
throw errcode ( new Error ( 'Unknown hash algorithm' ) , 'ERR_UNKNOWN_HASH_ALG' )
0 commit comments