

import * as randombytes from 'randombytes'

import { curve } from '../variables'
import { CRYPTOGRAPHY_KEY_ALGORITHM } from '../constants/asymmetric-cryptography'
import { GenerateKeyPair, AsymmetricKeyPair } from '../../../@utility/data-structures/asymmetric-cryptography'

const generateKeyPair: GenerateKeyPair = (): Promise<AsymmetricKeyPair[]> => {
  const bytes = randombytes(16)
  const privateKey = bytes.toString('hex')
  const publicKey = curve.keyFromPrivate(bytes).getPublic('hex')

  const keyPair = new AsymmetricKeyPair()
  keyPair.privateKey = `${CRYPTOGRAPHY_KEY_ALGORITHM}:${privateKey}`
  keyPair.publicKey = `${CRYPTOGRAPHY_KEY_ALGORITHM}:${publicKey}`

  return Promise.resolve([keyPair])
}

export {
  generateKeyPair
}