mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 12:26:19 +00:00
Add a method to get a deterministic pseudonym for given peer id (#326)
This commit is contained in:
parent
d50d78ed17
commit
83ad5d80a7
@ -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"
|
||||
},
|
||||
|
@ -4,3 +4,4 @@
|
||||
|
||||
export { Peer, PeerIdObj, createPeerId } from './peer.js';
|
||||
export { RelayNodeInit, createRelayNode } from './relay.js';
|
||||
export { getPseudonymForPeerId } from './utils/index.js';
|
||||
|
@ -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--;
|
||||
}
|
||||
|
||||
|
@ -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: '-'
|
||||
});
|
||||
};
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user