Skip to content

Commit

Permalink
fix: fix public key in read mode (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 authored Jan 31, 2024
1 parent 5dc0dee commit 32e6cea
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -204,6 +204,10 @@ export class PushAPI {
env: settings.env,
});

if (user && user.publicKey) {
pgpPublicKey = user.publicKey;
}

if (!readMode) {
if (user && user.encryptedPrivateKey) {
try {
Expand Down Expand Up @@ -235,7 +239,6 @@ export class PushAPI {
}
readMode = true;
}
pgpPublicKey = user.publicKey;
} else {
const newUser = await PUSH_USER.create({
env: settings.env,
Expand Down
1 change: 0 additions & 1 deletion packages/restapi/tests/lib/chat/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
52 changes: 52 additions & 0 deletions packages/restapi/tests/lib/encryption/encryption.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});

0 comments on commit 32e6cea

Please sign in to comment.