Skip to content

Commit

Permalink
chore(merge): main
Browse files Browse the repository at this point in the history
  • Loading branch information
madhur-push committed Nov 27, 2023
2 parents 95d6b02 + d1f0c50 commit 7e9e443
Show file tree
Hide file tree
Showing 18 changed files with 757 additions and 102 deletions.
7 changes: 4 additions & 3 deletions packages/restapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ const searchResult = await userAlice.channel.search("push")
### **Get Subscribers Of A Channel**

```tsx
// fetches subscribers of a channel
const subscribersResult = await userAlice.channel.subscribers()
// fetches subscribers of a channel in a paginated manner
const subscribersResult = await userAlice.channel.subscribers({page: 1, limit: 10})

```

Expand All @@ -335,7 +335,8 @@ const subscribersResult = await userAlice.channel.subscribers()
| --- | --- | --- | --- |
| options* | ChannelInfoOptions | - | Configuration options for retrieving subscribers. |
| options.channel* | string | - | Channel address in CAIP |

| options.page* | number | - | The page number for pagination |
| options.limit* | number | - | The maximum number of items to retrieve per page |
\* - Optional

---
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/channels/_getSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const deprecationWarning = `

export const _getSubscribers = async (
options: GetSubscribersOptionsType
) => {
) : Promise<string[]> => {

console.warn(deprecationWarning);

Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/chat/createGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const createGroupCore = async (
groupName: groupName,
groupDescription: groupDescription == undefined ? null : groupDescription,
members: convertedMembers,
groupImage: groupImage,
groupImage: groupImage == undefined ? null : groupImage,
admins: convertedAdmins,
isPublic: isPublic,
contractAddressNFT:
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/chat/updateGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export const updateGroupCore = async (
const convertedAdmins = await Promise.all(convertedAdminsPromise);
const bodyToBeHashed = {
groupName: groupName,
groupDescription: groupDescription,
groupImage: groupImage,
groupDescription: groupDescription == undefined ? null : groupDescription,
groupImage: groupImage == undefined ? null : groupImage,
members: convertedMembers,
admins: convertedAdmins,
chatId: chatId,
Expand Down
8 changes: 7 additions & 1 deletion packages/restapi/src/lib/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,13 @@ export const verifyProfileKeys = async (
}

try {
if (publicKey && publicKey.length > 0 && verificationProof) {
if (
publicKey &&
publicKey.length > 0 &&
verificationProof &&
// Allow pgp sig validation after eip191v2 only
verificationProof.split(':')[0] === 'eip191v2'
) {
const data = {
caip10,
did,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type SubscriptionOptions = {
};
export type ChannelInfoOptions = {
channel?: string;
page?: number;
limit?: number;
};

export type SubscribeUnsubscribeOptions = {
Expand Down
19 changes: 15 additions & 4 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,21 @@ export class Channel extends PushNotificationBaseClass {
if (!validateCAIP(channel!)) {
throw new Error('Invalid CAIP');
}
return await PUSH_CHANNEL._getSubscribers({
channel: channel!,
env: this.env,
});
if (options && options.page) {
return await PUSH_CHANNEL.getSubscribers({
channel: channel!,
env: this.env,
page: options.page,
limit: options.limit ?? 10,
});
} else {
/** @dev - Fallback to deprecated method when page is not provided ( to ensure backward compatibility ) */
/** @notice - This will be removed in V2 Publish */
return await PUSH_CHANNEL._getSubscribers({
channel: channel!,
env: this.env,
});
}
} catch (error) {
throw new Error(`Push SDK Error: API : channel::subscribers : ${error}`);
}
Expand Down
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
148 changes: 100 additions & 48 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Constants, { ENV } from '../constants';
import { SignerType, ProgressHookType } from '../types';
import { PushAPIInitializeProps } from './pushAPITypes';
import { InfoOptions, PushAPIInitializeProps } from './pushAPITypes';
import * as PUSH_USER from '../user';
import * as PUSH_CHAT from '../chat';
import { getAccountAddress, getWallet } from '../chat/helpers';
Expand All @@ -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,21 +220,26 @@ export class PushAPI {

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

return this.stream;
}

async info() {
async info(options?: InfoOptions) {
const accountToUse = options?.overrideAccount || this.account;
return await PUSH_USER.get({
account: this.account,
account: accountToUse,
env: this.env,
});
}

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

0 comments on commit 7e9e443

Please sign in to comment.