From 58637366582c95ad1355f1655e89abfeb2a744e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Fri, 25 Nov 2022 01:01:14 +0100 Subject: [PATCH] create DHT-KeyPair with optional configured Seed --- backend/src/federation/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/src/federation/index.ts b/backend/src/federation/index.ts index 2ca58b432..a7a06e42a 100644 --- a/backend/src/federation/index.ts +++ b/backend/src/federation/index.ts @@ -4,11 +4,24 @@ import DHT from '@hyperswarm/dht' // import { Connection } from '@dbTools/typeorm' import { backendLogger as logger } from '@/server/logger' +import CONFIG from '@/config' +// eslint-disable-next-line @typescript-eslint/no-var-requires +const sodium = require('sodium-native') function between(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1) + min) } +const getSeed = (): Buffer | null => { + if (CONFIG.FEDERATE_DHT_SEED) { + const secret = CONFIG.FEDERATE_DHT_SEED + const seed = sodium.sodium_malloc(sodium.crypto_box_SEEDBYTES) + seed.write(secret, 'hex') + return seed + } + return null +} + const POLLTIME = 20000 const SUCCESSTIME = 120000 const ERRORTIME = 240000 @@ -28,7 +41,9 @@ export const startDHT = async ( try { const TOPIC = DHT.hash(Buffer.from(topic)) - const keyPair = DHT.keyPair() + const keyPair = DHT.keyPair(getSeed()) + logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) + logger.info(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) const node = new DHT({ keyPair })