1
1
'use strict'
2
2
3
3
const PeerId = require ( 'peer-id' )
4
- const { CID } = require ( 'multiformats/cid' )
5
4
const errCode = require ( 'err-code' )
6
5
const { NotEnabledError } = require ( '../errors' )
7
6
const get = require ( 'dlv' )
@@ -19,15 +18,15 @@ module.exports = ({ network, repo }) => {
19
18
*/
20
19
async get ( key , options = { } ) {
21
20
const { libp2p } = await use ( network , options )
22
- return libp2p . _dht . get ( normalizeCID ( key ) , options )
21
+ return libp2p . _dht . get ( key , options )
23
22
} ,
24
23
25
24
/**
26
25
* @type {import('ipfs-core-types/src/dht').API["put"] }
27
26
*/
28
27
async * put ( key , value , options ) {
29
28
const { libp2p } = await use ( network , options )
30
- yield * libp2p . _dht . put ( normalizeCID ( key ) , value )
29
+ yield * libp2p . _dht . put ( key , value )
31
30
} ,
32
31
33
32
/**
@@ -36,7 +35,7 @@ module.exports = ({ network, repo }) => {
36
35
async * findProvs ( cid , options = { numProviders : 20 } ) {
37
36
const { libp2p } = await use ( network , options )
38
37
39
- for await ( const peer of libp2p . _dht . findProviders ( normalizeCID ( cid ) , {
38
+ for await ( const peer of libp2p . _dht . findProviders ( cid , {
40
39
maxNumProviders : options . numProviders ,
41
40
signal : options . signal
42
41
} ) ) {
@@ -52,7 +51,7 @@ module.exports = ({ network, repo }) => {
52
51
*/
53
52
async findPeer ( peerId , options ) {
54
53
const { libp2p } = await use ( network , options )
55
- const peer = await libp2p . _dht . findPeer ( PeerId . createFromB58String ( peerId ) )
54
+ const peer = await libp2p . _dht . findPeer ( PeerId . parse ( peerId ) )
56
55
57
56
return {
58
57
id : peer . id . toB58String ( ) ,
@@ -91,7 +90,7 @@ module.exports = ({ network, repo }) => {
91
90
async * query ( peerId , options ) {
92
91
const { libp2p } = await use ( network , options )
93
92
94
- for await ( const closerPeerId of libp2p . _dht . getClosestPeers ( PeerId . createFromB58String ( peerId ) . toBytes ( ) ) ) {
93
+ for await ( const closerPeerId of libp2p . _dht . getClosestPeers ( PeerId . parse ( peerId ) . toBytes ( ) ) ) {
95
94
yield {
96
95
id : closerPeerId . toB58String ( ) ,
97
96
addrs : [ ] // TODO: get addrs?
@@ -110,32 +109,6 @@ module.exports = ({ network, repo }) => {
110
109
}
111
110
}
112
111
113
- /**
114
- * Turns given cid in some stringifyable representation, to Uint8Array
115
- * representation. Throws an error if given value isn't a valid CID.
116
- *
117
- * @param {any } cid
118
- * @returns {Uint8Array }
119
- */
120
- const parseCID = cid => {
121
- try {
122
- const cidStr = cid . toString ( ) . split ( '/' )
123
- . filter ( ( /** @type {string } */ part ) => part && part !== 'ipfs' && part !== 'ipns' ) [ 0 ]
124
-
125
- return CID . parse ( cidStr ) . bytes
126
- } catch ( error ) {
127
- throw errCode ( error , 'ERR_INVALID_CID' )
128
- }
129
- }
130
-
131
- /**
132
- * Turns given cid in some representation to Uint8Array representation
133
- *
134
- * @param {any } cid
135
- */
136
- const normalizeCID = cid =>
137
- cid instanceof Uint8Array ? cid : parseCID ( cid )
138
-
139
112
/**
140
113
* @param {import('../types').NetworkService } network
141
114
* @param {import('ipfs-core-types/src/utils').AbortOptions } [options]
0 commit comments