1 import * as jsig from 'jsonld-signatures'
6 } from '../initializers'
16 import { logger } from './logger'
17 import { AccountInstance } from '../models/account/account-interface'
19 async function createPrivateAndPublicKeys () {
20 logger.info('Generating a RSA key...')
22 const { key } = await createPrivateKey(PRIVATE_RSA_KEY_SIZE)
23 const { publicKey } = await getPublicKey(key)
25 return { privateKey: key, publicKey }
28 function isSignatureVerified (fromAccount: AccountInstance, signedDocument: object) {
29 const publicKeyObject = {
30 '@context': jsig.SECURITY_CONTEXT_URL,
31 '@id': fromAccount.url,
32 '@type': 'CryptographicKey',
33 owner: fromAccount.url,
34 publicKeyPem: fromAccount.publicKey
37 const publicKeyOwnerObject = {
38 '@context': jsig.SECURITY_CONTEXT_URL,
39 '@id': fromAccount.url,
40 publicKey: [ publicKeyObject ]
44 publicKey: publicKeyObject,
45 publicKeyOwner: publicKeyOwnerObject
48 return jsonldVerifyPromise(signedDocument, options)
50 logger.error('Cannot check signature.', err)
55 function signObject (byAccount: AccountInstance, data: any) {
57 privateKeyPem: byAccount.privateKey,
58 creator: byAccount.url
61 return jsonldSignPromise(data, options)
64 function comparePassword (plainPassword: string, hashPassword: string) {
65 return bcryptComparePromise(plainPassword, hashPassword)
68 async function cryptPassword (password: string) {
69 const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE)
71 return bcryptHashPromise(password, salt)
74 // ---------------------------------------------------------------------------
79 createPrivateAndPublicKeys,