This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dial to PeerId and/or Multiaddr in addition to PeerInfo (#222)
* chore: update deps * feat: support dial to peerId and/or multiaddr in adition to peerInfo * chore: update CI
- Loading branch information
Showing
7 changed files
with
69 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict' | ||
|
||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const multiaddr = require('multiaddr') | ||
|
||
/* | ||
* Helper method to check the data type of peer and convert it to PeerInfo | ||
*/ | ||
function getPeerInfo (peer, peerBook) { | ||
let p | ||
|
||
// PeerInfo | ||
if (PeerInfo.isPeerInfo(peer)) { | ||
p = peer | ||
// Multiaddr instance (not string) | ||
} else if (multiaddr.isMultiaddr(peer)) { | ||
const peerIdB58Str = peer.getPeerId() | ||
try { | ||
p = peerBook.get(peerIdB58Str) | ||
} catch (err) { | ||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) | ||
} | ||
p.multiaddrs.add(peer) | ||
|
||
// PeerId | ||
} else if (PeerId.isPeerId(peer)) { | ||
const peerIdB58Str = peer.toB58String() | ||
try { | ||
p = peerBook.get(peerIdB58Str) | ||
} catch (err) { | ||
throw new Error('Couldnt get PeerInfo') | ||
} | ||
} else { | ||
throw new Error('peer type not recognized') | ||
} | ||
|
||
return p | ||
} | ||
|
||
module.exports = getPeerInfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters