import { curve } from '../variables' import { CRYPTOGRAPHY_KEY_ALGORITHM } from '../constants/asymmetric-cryptography' import { SignMessage } from '../../../@utility/data-structures/asymmetric-cryptography' import { verifyKeyFormat } from './verify-key-format' import { newError } from '../../../../@utility/algorithms/new-error' const signMessage: SignMessage = method async function method (message: Buffer, privateKey: string): Promise { if (!(await verifyKeyFormat(privateKey))) { throw newError(ERROR_INVALID_KEY_FORMAT, arguments) } if (privateKey.indexOf(CRYPTOGRAPHY_KEY_ALGORITHM) >= 0) { privateKey = privateKey.split(`${CRYPTOGRAPHY_KEY_ALGORITHM}:`)[1] } const keyPair = curve.keyFromPrivate(privateKey) const signature = curve.sign(message, keyPair) return Promise.resolve([signature.toDER()]) } const ERROR_INVALID_KEY_FORMAT = `Error: Invalid key format. Expected value '${CRYPTOGRAPHY_KEY_ALGORITHM}'` export { signMessage }