Skip to content

Commit

Permalink
fix: add decryptedPGPPrivateKey in options (#1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 authored Apr 8, 2024
1 parent 7f034ff commit ed9d3a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
57 changes: 34 additions & 23 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,41 @@ export class PushAPI {
try {
let signer: SignerType | undefined;
let options: PushAPIInitializeProps | undefined;
let decryptedPGPPrivateKey: string | undefined;

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

// Check for decryptedPGPPrivateKey in options, regardless of how options was assigned.
if (
options &&
'decryptedPGPPrivateKey' in options &&
typeof options.decryptedPGPPrivateKey === 'string'
) {
decryptedPGPPrivateKey = options.decryptedPGPPrivateKey;
}

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

// Determine readMode based on the presence of signer and decryptedPGPPrivateKey
let readMode = !signer && !decryptedPGPPrivateKey;

// Default options
const defaultOptions: PushAPIInitializeProps = {
env: ENV.STAGING,
Expand All @@ -175,7 +186,6 @@ export class PushAPI {
: ALPHA_FEATURE_CONFIG[PACKAGE_BUILD],
};

let readMode = !signer;
const initializationErrors: {
type: 'WARN' | 'ERROR';
message: string;
Expand All @@ -200,7 +210,6 @@ export class PushAPI {
throw new Error('Account could not be derived.');
}

let decryptedPGPPrivateKey: string | undefined;
let pgpPublicKey: string | undefined;

/**
Expand All @@ -220,14 +229,16 @@ export class PushAPI {
if (!readMode) {
try {
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,
});
if (!decryptedPGPPrivateKey) {
decryptedPGPPrivateKey = await PUSH_CHAT.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: signer,
toUpgrade: settings.autoUpgrade,
additionalMeta: settings.versionMeta,
progressHook: settings.progressHook,
env: settings.env,
});
}
} else {
const newUser = await PUSH_USER.create({
env: settings.env,
Expand Down
1 change: 1 addition & 0 deletions packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface PushAPIInitializeProps {
alpha?: {
feature: string[];
};
decryptedPGPPrivateKey?: string | null;
}

export interface GroupCreationOptions {
Expand Down

0 comments on commit ed9d3a7

Please sign in to comment.