import { Cryptobox } from '@wireapp/cryptobox' ; import { init , keys } from '@wireapp/proteus' ; import * as bazinga64 from 'bazinga64' ; import type { CryptographyService } from '../cryptography' ; const StoreHelper = require ( './StoreHelper' ) ; export async function createEncodedCipherText ( receiver , preKey , text ) { await init ( ) ; const senderEngine = await StoreHelper . createMemoryEngine ( ) ; const sender = new Cryptobox ( senderEngine , 1 ) ; await sender . create ( ) ; const sessionId = `from-${ sender [ 'identity' ] ! . public_key . fingerprint ( ) }-to-${ preKey . key_pair . public_key . fingerprint ( ) }` ; const alicePublicKey = receiver . public_key ; const publicPreKeyBundle = new keys . PreKeyBundle ( alicePublicKey , preKey ) ; const encryptedPreKeyMessage = await sender . encrypt ( sessionId , text , publicPreKeyBundle . serialise ( ) ) ; return bazinga64 . Encoder . toBase64 ( encryptedPreKeyMessage ) . asString ; } export async function getPlainText ( cryptographyService , encodedPreKeyMessage , sessionId = `temp-${ Date . now ( ) }` ) { try { const decryptedMessage = await cryptographyService . decrypt ( sessionId , encodedPreKeyMessage ) ; return Buffer . from ( decryptedMessage ) . toString ( 'utf8' ) ; } catch ( error ) { } }