Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

853 improvement proposal add read only low functions to high level functions #873

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading