create DHT-KeyPair with optional configured Seed

This commit is contained in:
Claus-Peter Hübner 2022-11-25 01:01:14 +01:00
parent 28fa11bcd5
commit 5863736658

View File

@ -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 })