diff --git a/packages/peer/package.json b/packages/peer/package.json index 81e7e0a6..43419345 100644 --- a/packages/peer/package.json +++ b/packages/peer/package.json @@ -43,6 +43,7 @@ "node-pre-gyp": "^0.13.0", "p-event": "^5.0.1", "uint8arrays": "^4.0.3", + "unique-names-generator": "^4.7.1", "wrtc": "^0.4.7", "yargs": "^17.0.1" }, diff --git a/packages/peer/src/index.ts b/packages/peer/src/index.ts index db018d84..7e1cc544 100644 --- a/packages/peer/src/index.ts +++ b/packages/peer/src/index.ts @@ -4,3 +4,4 @@ export { Peer, PeerIdObj, createPeerId } from './peer.js'; export { RelayNodeInit, createRelayNode } from './relay.js'; +export { getPseudonymForPeerId } from './utils/index.js'; diff --git a/packages/peer/src/peer.ts b/packages/peer/src/peer.ts index 386ba4cc..a9b6f5c1 100644 --- a/packages/peer/src/peer.ts +++ b/packages/peer/src/peer.ts @@ -270,6 +270,15 @@ export class Peer { return unsubscribe; } + isRelayPeerMultiaddr (multiaddrString: string): boolean { + // Multiaddr not having p2p-circuit id or webrtc-star id is of a relay node + return !(multiaddrString.includes(P2P_CIRCUIT_ID) || multiaddrString.includes(P2P_WEBRTC_STAR_ID)); + } + + isPrimaryRelay (multiaddrString: string): boolean { + return multiaddrString === this._relayNodeMultiaddr.toString(); + } + async _handleChangeProtocols ({ peerId, protocols }: { peerId: PeerId, protocols: string[] }) { assert(this._node); @@ -328,17 +337,12 @@ export class Peer { }); } - _isRelayPeerMultiaddr (multiaddrString: string): boolean { - // Multiaddr not having p2p-circuit id or webrtc-star id is of a relay node - return !(multiaddrString.includes(P2P_CIRCUIT_ID) || multiaddrString.includes(P2P_WEBRTC_STAR_ID)); - } - _handleDiscovery (peer: PeerInfo, maxRelayConnections: number): void { // Check connected peers as they are discovered repeatedly. if (!this._node?.getPeers().some(remotePeerId => remotePeerId.toString() === peer.id.toString())) { let isRelayPeer = false; for (const multiaddr of peer.multiaddrs) { - if (this._isRelayPeerMultiaddr(multiaddr.toString())) { + if (this.isRelayPeerMultiaddr(multiaddr.toString())) { isRelayPeer = true; break; } @@ -364,7 +368,7 @@ export class Peer { // Log connected peer console.log(`Connected to ${remotePeerIdString} using multiaddr ${remoteAddrString}`); - if (this._isRelayPeerMultiaddr(remoteAddrString)) { + if (this.isRelayPeerMultiaddr(remoteAddrString)) { // Check if relay connections limit has already been reached if (this._numRelayConnections >= maxRelayConnections) { console.log(`Closing connection to relay ${remotePeerIdString} as max relay connections limit reached`); @@ -446,7 +450,7 @@ export class Peer { console.log(`Disconnected from ${disconnectedPeerId.toString()} using multiaddr ${remoteAddrString}`); console.log(`Current number of peers connected: ${this._node?.getPeers().length}`); - if (this._isRelayPeerMultiaddr(remoteAddrString)) { + if (this.isRelayPeerMultiaddr(remoteAddrString)) { this._numRelayConnections--; } diff --git a/packages/peer/src/utils/index.ts b/packages/peer/src/utils/index.ts index 9ba6abc6..da7991d3 100644 --- a/packages/peer/src/utils/index.ts +++ b/packages/peer/src/utils/index.ts @@ -4,6 +4,7 @@ import { Libp2p } from '@cerc-io/libp2p'; import { Multiaddr } from '@multiformats/multiaddr'; +import { uniqueNamesGenerator, adjectives, colors, names } from 'unique-names-generator'; interface DialWithRetryOptions { redialDelay: number @@ -47,3 +48,18 @@ export const dialWithRetry = async (node: Libp2p, multiaddr: Multiaddr, options: throw new Error(`Stopping dial retry after ${maxRetry} attempts for multiaddr ${multiaddr.toString()}`); }; + +/** + * Get a deterministic pseudonym of form [adjective-color-name] for a given libp2p peer id + * Eg. 12D3KooWJLXEX2GfHPSZR3z9QKNSN8EY6pXo7FZ9XtFhiKLJATtC -> jolly-green-diann + * @param peerId + */ +export const getPseudonymForPeerId = (peerId: string): string => { + return uniqueNamesGenerator({ + seed: peerId, + dictionaries: [adjectives, colors, names], + length: 3, + style: 'lowerCase', + separator: '-' + }); +}; diff --git a/yarn.lock b/yarn.lock index 5e8b4eb4..cf12cd71 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15952,6 +15952,11 @@ unique-filename@^1.1.1: dependencies: unique-slug "^2.0.0" +unique-names-generator@^4.7.1: + version "4.7.1" + resolved "https://registry.yarnpkg.com/unique-names-generator/-/unique-names-generator-4.7.1.tgz#966407b12ba97f618928f77322cfac8c80df5597" + integrity sha512-lMx9dX+KRmG8sq6gulYYpKWZc9RlGsgBR6aoO8Qsm3qvkSJ+3rAymr+TnV8EDMrIrwuFJ4kruzMWM/OpYzPoow== + unique-slug@^2.0.0: version "2.0.2" resolved "https://registry.npmjs.org/unique-slug/-/unique-slug-2.0.2.tgz"