-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rosenet-node): announce public ip
- Loading branch information
1 parent
8c2ce80
commit 1a6fb51
Showing
6 changed files
with
81 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { isIP } from 'node:net'; | ||
|
||
import { fromNodeAddress } from '@multiformats/multiaddr'; | ||
import { publicIp } from 'public-ip'; | ||
|
||
/** | ||
* identify public ip (v4 or v6) of current node | ||
*/ | ||
const identifyPublicIP = () => publicIp(); | ||
|
||
/** | ||
* get multiaddr containing public ip of current node, to be used as announce | ||
* address | ||
*/ | ||
const getAnnounceMultiaddr = async (port: number) => { | ||
const ip = await identifyPublicIP(); | ||
const ipVersion = isIP(ip); | ||
|
||
const multiaddr = fromNodeAddress( | ||
{ address: ip, family: ipVersion as 4 | 6, port }, | ||
'tcp', | ||
); | ||
|
||
return multiaddr.toString(); | ||
}; | ||
|
||
export default { | ||
getAnnounceMultiaddr, | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const RELAYS_COUNT_TO_CONNECT = 3; | ||
export const ROSENET_DIRECT_PROTOCOL_V1 = '/rosenet/direct/1'; | ||
export const DEFAULT_NODE_PORT = 55123; |
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
29 changes: 29 additions & 0 deletions
29
packages/rosenet-node/tests/address/address-service.spec.ts
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,29 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import addressService from '../../lib/address/address-service'; | ||
|
||
const fakePublicIP = vi.hoisted(() => '172.20.20.20'); | ||
const fakePort = 12345; | ||
|
||
vi.mock('public-ip', () => ({ | ||
publicIp: vi.fn().mockResolvedValue(fakePublicIP), | ||
})); | ||
|
||
describe('getAnnounceMultiaddr', () => { | ||
/** | ||
* @target | ||
* getAnnounceMultiaddr should get announce multiaddr | ||
* @dependencies | ||
* - `public-ip` package | ||
* @scenario | ||
* - call the function | ||
* @expected | ||
* - announceMultiaddr should be the correct one | ||
*/ | ||
it('should get announce multiaddr', async () => { | ||
const announceMultiaddr = | ||
await addressService.getAnnounceMultiaddr(fakePort); | ||
|
||
expect(announceMultiaddr).toEqual(`/ip4/${fakePublicIP}/tcp/${fakePort}`); | ||
}); | ||
}); |