@@ -43,97 +43,13 @@ class PeerStore extends EventEmitter {
4343 * ProtoBook containing a map of peerIdStr to supported protocols.
4444 */
4545 this . protoBook = new ProtoBook ( this )
46- }
47-
48- // TODO: Temporary adapter for modules using PeerStore
49- // This should be removed under a breaking change
50- /**
51- * Stores the peerInfo of a new peer on each book.
52- * @param {PeerInfo } peerInfo
53- * @param {object } [options]
54- * @param {boolean } [options.replace = true]
55- * @return {PeerInfo }
56- */
57- put ( peerInfo , options ) {
58- const multiaddrs = peerInfo . multiaddrs . toArray ( )
59- const protocols = Array . from ( peerInfo . protocols || new Set ( ) )
60-
61- this . addressBook . set ( peerInfo . id , multiaddrs , options )
62- this . protoBook . set ( peerInfo . id , protocols , options )
63-
64- const peer = this . find ( peerInfo . id )
65- const pInfo = new PeerInfo ( peerInfo . id )
66-
67- if ( ! peer ) {
68- return pInfo
69- }
70-
71- peer . protocols . forEach ( ( p ) => pInfo . protocols . add ( p ) )
72- peer . multiaddrInfos . forEach ( ( mi ) => pInfo . multiaddrs . add ( mi . multiaddr ) )
73-
74- return pInfo
75- }
76-
77- // TODO: Temporary adapter for modules using PeerStore
78- // This should be removed under a breaking change
79- /**
80- * Get the info of the given id.
81- * @param {peerId } peerId
82- * @returns {PeerInfo }
83- */
84- get ( peerId ) {
85- const peer = this . find ( peerId )
86-
87- const pInfo = new PeerInfo ( peerId )
88- peer . protocols . forEach ( ( p ) => pInfo . protocols . add ( p ) )
89- peer . multiaddrInfos . forEach ( ( mi ) => pInfo . multiaddrs . add ( mi . multiaddr ) )
90-
91- return pInfo
92- }
93-
94- // TODO: Temporary adapter for modules using PeerStore
95- // This should be removed under a breaking change
96- /**
97- * Has the info to the given id.
98- * @param {PeerId } peerId
99- * @returns {boolean }
100- */
101- has ( peerId ) {
102- return Boolean ( this . find ( peerId ) )
103- }
104-
105- // TODO: Temporary adapter for modules using PeerStore
106- // This should be removed under a breaking change
107- /**
108- * Removes the peer provided.
109- * @param {PeerId } peerId
110- * @returns {boolean } true if found and removed
111- */
112- remove ( peerId ) {
113- return this . delete ( peerId )
114- }
11546
116- // TODO: Temporary adapter for modules using PeerStore
117- // This should be removed under a breaking change
118- /**
119- * Completely replaces the existing peers metadata with the given `peerInfo`
120- * @param {PeerInfo } peerInfo
121- * @returns {void }
122- */
123- replace ( peerInfo ) {
124- this . put ( peerInfo )
125- }
126-
127- // TODO: Temporary adapter for modules using PeerStore
128- // This should be removed under a breaking change
129- /**
130- * Returns the known multiaddrs for a given `PeerInfo`. All returned multiaddrs
131- * will include the encapsulated `PeerId` of the peer.
132- * @param {PeerInfo } peerInfo
133- * @returns {Array<Multiaddr> }
134- */
135- multiaddrsForPeer ( peerInfo ) {
136- return this . addressBook . getMultiaddrsForPeer ( peerInfo . id )
47+ /**
48+ * TODO: this should only exist until we have the key-book
49+ * Map known peers to their peer-id.
50+ * @type {Map<string, Array<PeerId> }
51+ */
52+ this . peerIds = new Map ( )
13753 }
13854
13955 /**
@@ -195,15 +111,16 @@ class PeerStore extends EventEmitter {
195111 }
196112
197113 /**
198- * Find the stored information of a given peer.
114+ * Get the stored information of a given peer.
199115 * @param {PeerId } peerId
200116 * @returns {peerInfo }
201117 */
202- find ( peerId ) {
118+ get ( peerId ) {
203119 if ( ! PeerId . isPeerId ( peerId ) ) {
204120 throw errcode ( new Error ( 'peerId must be an instance of peer-id' ) , ERR_INVALID_PARAMETERS )
205121 }
206122
123+ const id = this . peerIds . get ( peerId . toB58String ( ) )
207124 const multiaddrInfos = this . addressBook . get ( peerId )
208125 const protocols = this . protoBook . get ( peerId )
209126
@@ -212,6 +129,7 @@ class PeerStore extends EventEmitter {
212129 }
213130
214131 return {
132+ id : id || peerId ,
215133 multiaddrInfos : multiaddrInfos || [ ] ,
216134 protocols : protocols || [ ]
217135 }
0 commit comments