diff --git a/packages/restapi/src/lib/pushapi/PushAPI.ts b/packages/restapi/src/lib/pushapi/PushAPI.ts index e5e849f02..0112368c3 100644 --- a/packages/restapi/src/lib/pushapi/PushAPI.ts +++ b/packages/restapi/src/lib/pushapi/PushAPI.ts @@ -191,8 +191,8 @@ export class PushAPI { throw new Error('Account could not be derived.'); } - let decryptedPGPPrivateKey; - let pgpPublicKey; + let decryptedPGPPrivateKey: string | undefined; + let pgpPublicKey: string | undefined; /** * Decrypt PGP private key @@ -204,6 +204,10 @@ export class PushAPI { env: settings.env, }); + if (user && user.publicKey) { + pgpPublicKey = user.publicKey; + } + if (!readMode) { if (user && user.encryptedPrivateKey) { try { @@ -235,7 +239,6 @@ export class PushAPI { } readMode = true; } - pgpPublicKey = user.publicKey; } else { const newUser = await PUSH_USER.create({ env: settings.env, diff --git a/packages/restapi/tests/lib/chat/chat.test.ts b/packages/restapi/tests/lib/chat/chat.test.ts index b78b1fc0a..c71b03f70 100644 --- a/packages/restapi/tests/lib/chat/chat.test.ts +++ b/packages/restapi/tests/lib/chat/chat.test.ts @@ -106,7 +106,6 @@ describe('PushAPI.chat functionality', () => { 'Operation not allowed in read-only mode. Signer is required.' ); }); - it('Should decrypt message ', async () => { await userAlice.chat.send(account2, { content: 'Hello', diff --git a/packages/restapi/tests/lib/encryption/encryption.test.ts b/packages/restapi/tests/lib/encryption/encryption.test.ts new file mode 100644 index 000000000..deacdc536 --- /dev/null +++ b/packages/restapi/tests/lib/encryption/encryption.test.ts @@ -0,0 +1,52 @@ +import * as path from 'path'; +import * as dotenv from 'dotenv'; +dotenv.config({ path: path.resolve(__dirname, '../../.env') }); + +import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; // Ensure correct import path +import { expect } from 'chai'; +import { ethers } from 'ethers'; +import CONSTANTS from '../../../src/lib/constantsV2'; + +const env = CONSTANTS.ENV.DEV; +describe('PushAPI.encryption functionality', () => { + let signer1: any; + let account1: string; + + before(async () => { + const WALLET1 = ethers.Wallet.createRandom(); + signer1 = new ethers.Wallet(WALLET1.privateKey); + account1 = WALLET1.address; + + // To make sure user exists + await PushAPI.initialize(signer1, { + env, + }); + }); + + it('Encryption.info', async () => { + const userAlice = await PushAPI.initialize(signer1, { + env, + }); + const response = await userAlice.encryption.info(); + expect(response.decryptedPgpPrivateKey).to.include( + '-----BEGIN PGP PRIVATE KEY BLOCK-----\n' + ); + expect(response.pgpPublicKey).to.be.include( + '-----BEGIN PGP PUBLIC KEY BLOCK-----\n' + ); + }); + + it('Encryption.info in Read Mode', async () => { + // Read only mode + const _signer = undefined; + const userAlice = await PushAPI.initialize(_signer, { + env, + account: account1, + }); + const response = await userAlice.encryption.info(); + expect(response.decryptedPgpPrivateKey).to.be.undefined; + expect(response.pgpPublicKey).to.be.include( + '-----BEGIN PGP PUBLIC KEY BLOCK-----\n' + ); + }); +});