From fcfe7db631d68e933d29e989575bcc9243cea63c Mon Sep 17 00:00:00 2001 From: Isla Koenigsknecht Date: Tue, 31 Dec 2024 16:51:04 -0500 Subject: [PATCH] Reuse noise keys and fix some libp2p settings --- .github/actions/setup-env/action.yml | 3 +++ 3rd-party/js-libp2p-noise | 1 + packages/backend/src/nest/common/utils.ts | 3 +++ .../connections-manager.service.ts | 6 ++++-- .../backend/src/nest/libp2p/libp2p.service.ts | 21 ++++++++++++++----- .../backend/src/nest/libp2p/libp2p.types.ts | 1 + .../components/Channel/Channel.stories.cy.tsx | 1 + .../components/Channel/Channel.stories.tsx | 1 + .../src/rtl-tests/community.create.test.tsx | 4 ++++ .../src/rtl-tests/community.join.test.tsx | 8 ++++++- .../src/sagas/users/users.selectors.test.ts | 2 ++ .../state-manager/src/utils/tests/helpers.ts | 1 + packages/types/src/identity.ts | 3 ++- 13 files changed, 46 insertions(+), 9 deletions(-) create mode 160000 3rd-party/js-libp2p-noise diff --git a/.github/actions/setup-env/action.yml b/.github/actions/setup-env/action.yml index 23bfcc0c13..37a10855de 100644 --- a/.github/actions/setup-env/action.yml +++ b/.github/actions/setup-env/action.yml @@ -63,6 +63,9 @@ runs: - name: "Build submodules" run: | + git submodule update --init --recursive + git submodule update --recursive --remote + npm run build:auth npm run build:noise shell: bash diff --git a/3rd-party/js-libp2p-noise b/3rd-party/js-libp2p-noise new file mode 160000 index 0000000000..ae3800261c --- /dev/null +++ b/3rd-party/js-libp2p-noise @@ -0,0 +1 @@ +Subproject commit ae3800261cb57603b2f61be7b64194da7d26839e diff --git a/packages/backend/src/nest/common/utils.ts b/packages/backend/src/nest/common/utils.ts index 89ad402392..58336ffe8a 100644 --- a/packages/backend/src/nest/common/utils.ts +++ b/packages/backend/src/nest/common/utils.ts @@ -16,6 +16,7 @@ import { createLibp2pAddress, createLibp2pListenAddress, isDefined } from '@quie import { Libp2pService } from '../libp2p/libp2p.service' import { CertFieldsTypes, getReqFieldValue, loadCSR } from '@quiet/identity' import { createLogger } from './logger' +import { pureJsCrypto } from '@chainsafe/libp2p-noise' const logger = createLogger('utils') @@ -257,10 +258,12 @@ export const tmpQuietDirPath = (name: string): string => { export async function createPeerId(): Promise { const privKey = await generateKeyPair('Ed25519', 32) + const noiseKey = pureJsCrypto.generateX25519KeyPair().privateKey const peerId = peerIdFromPrivateKey(privKey) return { peerId, privKey, + noiseKey, } } diff --git a/packages/backend/src/nest/connections-manager/connections-manager.service.ts b/packages/backend/src/nest/connections-manager/connections-manager.service.ts index b4e11aee83..087a9270eb 100644 --- a/packages/backend/src/nest/connections-manager/connections-manager.service.ts +++ b/packages/backend/src/nest/connections-manager/connections-manager.service.ts @@ -402,7 +402,8 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI const peerId = await createPeerId() const peerIdJson: QuietPeerId = { id: peerId.peerId.toString(), - privKey: uint8ArrayToString(peerId.privKey!.raw, 'base64'), + privKey: uint8ArrayToString(peerId.privKey.raw, 'base64'), + noiseKey: uint8ArrayToString(peerId.noiseKey, 'base64'), } this.logger.info(`Created network for peer ${peerId.toString()}. Address: ${hiddenService.onionAddress}`) @@ -783,7 +784,8 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI this.logger.info(JSON.stringify(identity.peerId, null, 2)) const peerIdData: CreatedLibp2pPeerId = { peerId: peerIdFromString(identity.peerId.id), - privKey: privateKeyFromRaw(Buffer.from(identity.peerId.privKey!, 'base64')), + privKey: privateKeyFromRaw(Buffer.from(identity.peerId.privKey, 'base64')), + noiseKey: Buffer.from(identity.peerId.noiseKey, 'base64'), } this.logger.info(peerIdData.peerId.toString()) const peers = filterValidAddresses(community.peerList ? community.peerList : []) diff --git a/packages/backend/src/nest/libp2p/libp2p.service.ts b/packages/backend/src/nest/libp2p/libp2p.service.ts index f3edef437f..3f65637a89 100644 --- a/packages/backend/src/nest/libp2p/libp2p.service.ts +++ b/packages/backend/src/nest/libp2p/libp2p.service.ts @@ -270,10 +270,11 @@ export class Libp2pService extends EventEmitter { connectionProtector: preSharedKey({ psk: params.psk }), streamMuxers: [ yamux({ - keepAliveInterval: 60_000, + maxInboundStreams: 3_000, + maxOutboundStreams: 3_000, }), ], - connectionEncrypters: [noise({ crypto: pureJsCrypto })], + connectionEncrypters: [noise({ crypto: pureJsCrypto, staticNoiseKey: params.peerId.noiseKey })], transports: [ webSockets({ filter: filters.all, @@ -291,12 +292,14 @@ export class Libp2pService extends EventEmitter { allowPublishToZeroTopicPeers: true, fallbackToFloodsub: true, emitSelf: true, + debugName: params.peerId.peerId.toString(), + doPX: true, }), - identify: identify(), - identifyPush: identifyPush(), + identify: identify({ timeout: 30_000 }), + identifyPush: identifyPush({ timeout: 30_000 }), keychain: keychain(), dht: kadDHT({ - allowQueryWithZeroPeers: false, + allowQueryWithZeroPeers: true, clientMode: false, }), }, @@ -328,6 +331,14 @@ export class Libp2pService extends EventEmitter { this.logger.info(`${peerId.peerId.toString()} discovered ${peer.detail.id}`) }) + this.libp2pInstance.addEventListener('connection:close', event => { + this.logger.warn(`Connection closing with ${event.detail.remotePeer}`) + }) + + this.libp2pInstance.addEventListener('transport:close', event => { + this.logger.warn(`Transport closing`) + }) + this.libp2pInstance.addEventListener('peer:connect', async event => { const remotePeerId = event.detail.toString() const localPeerId = peerId.peerId.toString() diff --git a/packages/backend/src/nest/libp2p/libp2p.types.ts b/packages/backend/src/nest/libp2p/libp2p.types.ts index 7e7bdacd12..598e2893d5 100644 --- a/packages/backend/src/nest/libp2p/libp2p.types.ts +++ b/packages/backend/src/nest/libp2p/libp2p.types.ts @@ -34,4 +34,5 @@ export type Libp2pDatastoreOptions = { export interface CreatedLibp2pPeerId { peerId: PeerId privKey: PrivateKey + noiseKey: Uint8Array } diff --git a/packages/desktop/src/renderer/components/Channel/Channel.stories.cy.tsx b/packages/desktop/src/renderer/components/Channel/Channel.stories.cy.tsx index c156fbe7b8..91ae99d1b8 100644 --- a/packages/desktop/src/renderer/components/Channel/Channel.stories.cy.tsx +++ b/packages/desktop/src/renderer/components/Channel/Channel.stories.cy.tsx @@ -64,6 +64,7 @@ const Template: ComponentStory = () => { peerId: { id: 'id', privKey: 'privKey', + noiseKey: 'noiseKey' }, userCsr: { userCsr: 'userCsr', diff --git a/packages/desktop/src/renderer/components/Channel/Channel.stories.tsx b/packages/desktop/src/renderer/components/Channel/Channel.stories.tsx index bbc818a36b..58adefb5c1 100644 --- a/packages/desktop/src/renderer/components/Channel/Channel.stories.tsx +++ b/packages/desktop/src/renderer/components/Channel/Channel.stories.tsx @@ -23,6 +23,7 @@ const args: Partial = { peerId: { id: 'id', privKey: 'privKey', + noiseKey: 'noiseKey', }, userCsr: { userCsr: 'userCsr', diff --git a/packages/desktop/src/rtl-tests/community.create.test.tsx b/packages/desktop/src/rtl-tests/community.create.test.tsx index 440cc14a73..98eb0d2626 100644 --- a/packages/desktop/src/rtl-tests/community.create.test.tsx +++ b/packages/desktop/src/rtl-tests/community.create.test.tsx @@ -85,6 +85,8 @@ describe('User', () => { }, peerId: { id: 'peerId', + privKey: 'mock', + noiseKey: 'mock', }, } as Identity } @@ -99,6 +101,8 @@ describe('User', () => { }, peerId: { id: 'peerId', + privKey: 'mock', + noiseKey: 'mock', } as PeerId, userCsr: { userCsr: 'mock', diff --git a/packages/desktop/src/rtl-tests/community.join.test.tsx b/packages/desktop/src/rtl-tests/community.join.test.tsx index 34d2c5f2a7..5628a0fa45 100644 --- a/packages/desktop/src/rtl-tests/community.join.test.tsx +++ b/packages/desktop/src/rtl-tests/community.join.test.tsx @@ -105,6 +105,8 @@ describe('User', () => { }, peerId: { id: 'peerId', + privKey: 'mock', + noiseKey: 'mock', }, } as Identity case SocketActionTypes.CREATE_USER_CSR: @@ -119,7 +121,7 @@ describe('User', () => { peerId: { id: csrPayload.communityId, privKey: 'mock', - pubKey: 'mock', + noiseKey: 'mock', } as PeerId, nickname: csrPayload.nickname, userCsr: { @@ -266,6 +268,8 @@ describe('User', () => { }, peerId: { id: 'peerId', + privKey: 'mock', + noiseKey: 'mock', }, } } @@ -354,6 +358,8 @@ describe('User', () => { }, peerId: { id: 'peerId', + privKey: 'mock', + noiseKey: 'mock', }, } } diff --git a/packages/state-manager/src/sagas/users/users.selectors.test.ts b/packages/state-manager/src/sagas/users/users.selectors.test.ts index 0d49f6e860..f59fc4bf53 100644 --- a/packages/state-manager/src/sagas/users/users.selectors.test.ts +++ b/packages/state-manager/src/sagas/users/users.selectors.test.ts @@ -47,6 +47,8 @@ describe('users selectors', () => { }, peerId: { id: aliceCertificateData.peerId, + privKey: 'foobar', + noiseKey: 'barbaz', }, }) diff --git a/packages/state-manager/src/utils/tests/helpers.ts b/packages/state-manager/src/utils/tests/helpers.ts index 3b0f303a00..93e636c0a5 100644 --- a/packages/state-manager/src/utils/tests/helpers.ts +++ b/packages/state-manager/src/utils/tests/helpers.ts @@ -14,6 +14,7 @@ export const createPeerIdTestHelper = (): PeerId => { return { id: '12D3KooWRga8g1J1oiH7UYnSQ8YMPRDfVuehuiuAd7PMkcXSxRsp', privKey: 'jAXL3ZK13AWR9WcwbX8nM/qgQqdaApPDqWj6dK9IPwHru99WpGniLouugCv2+t7QN4xnYLMoAFPRP40xTUTrCw', + noiseKey: 'B+zyZ6mQ5f+h0EDkr0woI+pIJc8xm62+f+M24eYVeMY=', } } diff --git a/packages/types/src/identity.ts b/packages/types/src/identity.ts index 7f7ff151e0..a76bb2af32 100644 --- a/packages/types/src/identity.ts +++ b/packages/types/src/identity.ts @@ -17,7 +17,8 @@ export interface HiddenService { export interface PeerId { id: string - privKey?: string + privKey: string + noiseKey: string } export interface Identity {