Skip to content

Commit

Permalink
Reuse noise keys and fix some libp2p settings
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed Dec 31, 2024
1 parent 779156e commit fcfe7db
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/actions/setup-env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions 3rd-party/js-libp2p-noise
Submodule js-libp2p-noise added at ae3800
3 changes: 3 additions & 0 deletions packages/backend/src/nest/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -257,10 +258,12 @@ export const tmpQuietDirPath = (name: string): string => {

export async function createPeerId(): Promise<CreatedLibp2pPeerId> {
const privKey = await generateKeyPair('Ed25519', 32)
const noiseKey = pureJsCrypto.generateX25519KeyPair().privateKey
const peerId = peerIdFromPrivateKey(privKey)
return {
peerId,
privKey,
noiseKey,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)

Expand Down Expand Up @@ -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 : [])
Expand Down
21 changes: 16 additions & 5 deletions packages/backend/src/nest/libp2p/libp2p.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand 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,
}),
},
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/nest/libp2p/libp2p.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export type Libp2pDatastoreOptions = {
export interface CreatedLibp2pPeerId {
peerId: PeerId
privKey: PrivateKey
noiseKey: Uint8Array
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const Template: ComponentStory<typeof ChannelComponent> = () => {
peerId: {
id: 'id',
privKey: 'privKey',
noiseKey: 'noiseKey'
},
userCsr: {
userCsr: 'userCsr',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const args: Partial<ChannelComponentProps & UploadFilesPreviewsProps> = {
peerId: {
id: 'id',
privKey: 'privKey',
noiseKey: 'noiseKey',
},
userCsr: {
userCsr: 'userCsr',
Expand Down
4 changes: 4 additions & 0 deletions packages/desktop/src/rtl-tests/community.create.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ describe('User', () => {
},
peerId: {
id: 'peerId',
privKey: 'mock',
noiseKey: 'mock',
},
} as Identity
}
Expand All @@ -99,6 +101,8 @@ describe('User', () => {
},
peerId: {
id: 'peerId',
privKey: 'mock',
noiseKey: 'mock',
} as PeerId,
userCsr: {
userCsr: 'mock',
Expand Down
8 changes: 7 additions & 1 deletion packages/desktop/src/rtl-tests/community.join.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ describe('User', () => {
},
peerId: {
id: 'peerId',
privKey: 'mock',
noiseKey: 'mock',
},
} as Identity
case SocketActionTypes.CREATE_USER_CSR:
Expand All @@ -119,7 +121,7 @@ describe('User', () => {
peerId: {
id: csrPayload.communityId,
privKey: 'mock',
pubKey: 'mock',
noiseKey: 'mock',
} as PeerId,
nickname: csrPayload.nickname,
userCsr: {
Expand Down Expand Up @@ -266,6 +268,8 @@ describe('User', () => {
},
peerId: {
id: 'peerId',
privKey: 'mock',
noiseKey: 'mock',
},
}
}
Expand Down Expand Up @@ -354,6 +358,8 @@ describe('User', () => {
},
peerId: {
id: 'peerId',
privKey: 'mock',
noiseKey: 'mock',
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ describe('users selectors', () => {
},
peerId: {
id: aliceCertificateData.peerId,
privKey: 'foobar',
noiseKey: 'barbaz',
},
})

Expand Down
1 change: 1 addition & 0 deletions packages/state-manager/src/utils/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const createPeerIdTestHelper = (): PeerId => {
return {
id: '12D3KooWRga8g1J1oiH7UYnSQ8YMPRDfVuehuiuAd7PMkcXSxRsp',
privKey: 'jAXL3ZK13AWR9WcwbX8nM/qgQqdaApPDqWj6dK9IPwHru99WpGniLouugCv2+t7QN4xnYLMoAFPRP40xTUTrCw',
noiseKey: 'B+zyZ6mQ5f+h0EDkr0woI+pIJc8xm62+f+M24eYVeMY=',
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface HiddenService {

export interface PeerId {
id: string
privKey?: string
privKey: string
noiseKey: string
}

export interface Identity {
Expand Down

0 comments on commit fcfe7db

Please sign in to comment.