Skip to content

Commit

Permalink
853 improvement proposal add read only low functions to high level fu…
Browse files Browse the repository at this point in the history
…nctions (#873)

* fix: PUSH API read only mode

* fix: fix error message
  • Loading branch information
mohammeds1992 authored Nov 23, 2023
1 parent c9b7f9e commit 5914c23
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
validateCAIP,
} from '../helpers';
import * as PUSH_ALIAS from '../alias';
import { PushAPI } from '../pushapi/PushAPI';

// ERROR CONSTANTS
const ERROR_ACCOUNT_NEEDED = 'Account is required';
Expand Down Expand Up @@ -113,7 +114,7 @@ export class PushNotificationBaseClass {

// checks if the signer object is supplied
protected checkSignerObjectExists() {
if (!this.signer) throw new Error(ERROR_SIGNER_NEEDED);
if (!this.signer) throw new Error(PushAPI.ensureSignerMessage());
return true;
}

Expand Down
141 changes: 96 additions & 45 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
} from '../pushstream/pushStreamTypes';

export class PushAPI {
private signer: SignerType;
private signer?: SignerType;
private readMode: boolean;
private account: string;
private decryptedPgpPvtKey: string;
private pgpPublicKey: string;
private decryptedPgpPvtKey?: string;
private pgpPublicKey?: string;
private env: ENV;
private progressHook?: (progress: ProgressHookType) => void;

Expand All @@ -34,14 +35,16 @@ export class PushAPI {
public notification!: Notification;

private constructor(
signer: SignerType,
env: ENV,
account: string,
decryptedPgpPvtKey: string,
pgpPublicKey: string,
readMode: boolean,
decryptedPgpPvtKey?: string,
pgpPublicKey?: string,
signer?: SignerType,
progressHook?: (progress: ProgressHookType) => void
) {
this.signer = signer;
this.readMode = readMode;
this.env = env;
this.account = account;
this.decryptedPgpPvtKey = decryptedPgpPvtKey;
Expand All @@ -53,33 +56,61 @@ export class PushAPI {
// Initialize the instances of the four classes
this.chat = new Chat(
this.account,
this.decryptedPgpPvtKey,
this.env,
this.decryptedPgpPvtKey,
this.signer,
this.progressHook
);
this.profile = new Profile(
this.account,
this.decryptedPgpPvtKey,
this.env,
this.decryptedPgpPvtKey,
this.progressHook
);
this.encryption = new Encryption(
this.account,
this.env,
this.decryptedPgpPvtKey,
this.pgpPublicKey,
this.env,
this.signer,
this.progressHook
);
this.user = new User(this.account, this.env);
}

// Overloaded initialize method signatures
static async initialize(
signer: SignerType,
signer?: SignerType,
options?: PushAPIInitializeProps
): Promise<PushAPI> {
): Promise<PushAPI>;
static async initialize(options?: PushAPIInitializeProps): Promise<PushAPI>;

static async initialize(...args: any[]): Promise<PushAPI> {
try {
let signer: SignerType | undefined;
let options: PushAPIInitializeProps | undefined;

if (
args.length === 1 &&
typeof args[0] === 'object' &&
'account' in args[0]
) {
// Single options object provided
options = args[0];
} else if (args.length === 1) {
// Only signer provided
[signer] = args;
} else if (args.length === 2) {
// Separate signer and options arguments provided
[signer, options] = args;
} else {
// Handle other cases or throw an error
throw new Error('Invalid arguments provided to initialize method.');
}

if (!signer && !options?.account) {
throw new Error("Either 'signer' or 'account' must be provided.");
}

// Default options
const defaultOptions: PushAPIInitializeProps = {
env: ENV.STAGING,
Expand All @@ -101,17 +132,29 @@ export class PushAPI {
: defaultOptions.autoUpgrade,
};

const readMode = !signer;

// Get account
// Derives account from signer if not provided
const derivedAccount = await getAccountAddress(
getWallet({
account: settings.account as string | null,
signer: signer,
})
);

let decryptedPGPPrivateKey: string;
let pgpPublicKey: string;
let derivedAccount;
if (signer) {
derivedAccount = await getAccountAddress(
getWallet({
account: settings.account as string | null,
signer: signer,
})
);
} else {
derivedAccount = options?.account;
}

if (!derivedAccount) {
throw new Error('Account could not be derived.');
}

let decryptedPGPPrivateKey;
let pgpPublicKey;

/**
* Decrypt PGP private key
Expand All @@ -122,37 +165,41 @@ export class PushAPI {
account: derivedAccount,
env: settings.env,
});
if (user && user.encryptedPrivateKey) {
decryptedPGPPrivateKey = await PUSH_CHAT.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: signer,
toUpgrade: settings.autoUpgrade,
additionalMeta: settings.versionMeta,
progressHook: settings.progressHook,
env: settings.env,
});
pgpPublicKey = user.publicKey;
} else {
const newUser = await PUSH_USER.create({
env: settings.env,
account: derivedAccount,
signer,
version: settings.version,
additionalMeta: settings.versionMeta,
origin: settings.origin,
progressHook: settings.progressHook,
});
decryptedPGPPrivateKey = newUser.decryptedPrivateKey as string;
pgpPublicKey = newUser.publicKey;

if (!readMode) {
if (user && user.encryptedPrivateKey) {
decryptedPGPPrivateKey = await PUSH_CHAT.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: signer,
toUpgrade: settings.autoUpgrade,
additionalMeta: settings.versionMeta,
progressHook: settings.progressHook,
env: settings.env,
});
pgpPublicKey = user.publicKey;
} else {
const newUser = await PUSH_USER.create({
env: settings.env,
account: derivedAccount,
signer,
version: settings.version,
additionalMeta: settings.versionMeta,
origin: settings.origin,
progressHook: settings.progressHook,
});
decryptedPGPPrivateKey = newUser.decryptedPrivateKey as string;
pgpPublicKey = newUser.publicKey;
}
}

// Initialize PushAPI instance
const api = new PushAPI(
signer,
settings.env as ENV,
derivedAccount,
readMode,
decryptedPGPPrivateKey,
pgpPublicKey,
signer,
settings.progressHook
);

Expand All @@ -173,11 +220,11 @@ export class PushAPI {

this.stream = await PushStream.initialize(
this.account,
this.decryptedPgpPvtKey,
this.signer,
listen,
this.env,
this.decryptedPgpPvtKey,
this.progressHook,
this.signer,
options
);

Expand All @@ -190,4 +237,8 @@ export class PushAPI {
env: this.env,
});
}

static ensureSignerMessage(): string {
return 'Operation not allowed in read-only mode. Signer is required.';
}
}
Loading

0 comments on commit 5914c23

Please sign in to comment.