Skip to content

Commit

Permalink
merge branch to main
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshRajat committed May 10, 2024
2 parents f039900 + 17febb3 commit 92f84f5
Show file tree
Hide file tree
Showing 27 changed files with 2,125 additions and 398 deletions.
31 changes: 21 additions & 10 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getCAIPWithChainId,
validateCAIP,
getFallbackETHCAIPAddress,
pCAIP10ToWallet,
} from '../helpers';

import { PushNotificationBaseClass } from './pushNotificationBase';
Expand Down Expand Up @@ -50,11 +51,16 @@ export class Notification extends PushNotificationBaseClass {
raw = false,
} = options || {};
try {
const account = options?.account
? options.account
: this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null;
let account: string | null;
if (options?.account) {
if (this.isValidPCaip(options.account)) {
account = pCAIP10ToWallet(options.account);
} else {
account = options.account;
}
} else if(this.account){
account = getFallbackETHCAIPAddress(this.env!, this.account!)
}
// guest mode and valid address check
this.checkUserAddressExists(account!);
const nonCaipAccount = this.getAddressFromCaip(account!);
Expand Down Expand Up @@ -99,11 +105,16 @@ export class Notification extends PushNotificationBaseClass {
channel = null,
raw,
} = options || {};
const account = options?.account
? options.account
: this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null;
let account: string | null;
if (options?.account) {
if (this.isValidPCaip(options.account)) {
account = pCAIP10ToWallet(options.account);
} else {
account = options.account;
}
} else if(this.account){
account = getFallbackETHCAIPAddress(this.env!, this.account!)
}
this.checkUserAddressExists(account!);
return await PUSH_USER.getSubscriptions({
user: account!,
Expand Down
18 changes: 15 additions & 3 deletions packages/restapi/src/lib/pushNotification/pushNotificationBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
import { axiosGet, axiosPost } from '../utils/axiosUtil';
import { PushAPI } from '../pushapi/PushAPI';
import { channel } from 'diagnostics_channel';
import * as viem from 'viem';


// ERROR CONSTANTS
const ERROR_ACCOUNT_NEEDED = 'Account is required';
Expand Down Expand Up @@ -843,7 +845,7 @@ export class PushNotificationBaseClass {
throw new Error('Signer is not provided');
}
const pushSigner = new Signer(this.signer);
let addAliasRes
let addAliasRes;
if (!pushSigner.isViemSigner(this.signer)) {
if (!this.signer.provider) {
throw new Error('ethers provider is not provided');
Expand Down Expand Up @@ -875,12 +877,13 @@ export class PushNotificationBaseClass {
throw new Error('Signer is not provided');
}
const pushSigner = new Signer(this.signer);
let verifyAliasRes
let verifyAliasRes;
if (!pushSigner.isViemSigner(this.signer)) {
if (!this.signer.provider) {
throw new Error('ethers provider is not provided');
}
const addAliasTrxPromise = contract!['verifyChannelAlias'](channelAddress);
const addAliasTrxPromise =
contract!['verifyChannelAlias'](channelAddress);
const addAliasTrx = await addAliasTrxPromise;
await this.signer?.provider?.waitForTransaction(addAliasTrx.hash);
verifyAliasRes = addAliasTrx.hash;
Expand Down Expand Up @@ -926,4 +929,13 @@ export class PushNotificationBaseClass {
protected getAddressFromCaip(caipAddress: string): string {
return caipAddress?.split(':')[caipAddress?.split(':').length - 1];
}

protected isValidPCaip(address: string): boolean {
const addressComponents = address.split(':');
return (
addressComponents.length == 2 &&
addressComponents[0] == 'eip155' &&
viem.isAddress(addressComponents[1])
);
}
}
19 changes: 17 additions & 2 deletions packages/restapi/tests/lib/notification/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ describe('PushAPI.notification functionality', () => {
expect(response).not.null;
});


it('Should return feeds when signer with provider is used', async () => {
const response = await userKate.notification.list('SPAM', {
account: 'eip155:0xD8634C39BBFd4033c0d3289C4515275102423681',
});
// console.log(response)
expect(response).not.null;
});

it('Should return feeds when viem is used', async () => {
const response = await userViem.notification.list('SPAM');
// console.log(response);
Expand Down Expand Up @@ -267,11 +276,18 @@ describe('PushAPI.notification functionality', () => {
expect(response.length).not.equal(0);
});

it('Signer with account: Should return response', async () => {
const response = await userKate.notification.subscriptions({
account: 'eip155:0xD8634C39BBFd4033c0d3289C4515275102423681',
});
expect(response).not.null;
expect(response.length).not.equal(0);
});

it('Signer with account: Should return response', async () => {
const response = await userKate.notification.subscriptions({
account: '0xD8634C39BBFd4033c0d3289C4515275102423681',
});
// console.log(JSON.stringify(response));
expect(response).not.null;
expect(response.length).not.equal(0);
});
Expand All @@ -282,7 +298,6 @@ describe('PushAPI.notification functionality', () => {
raw: false,
channel: '0xD8634C39BBFd4033c0d3289C4515275102423681',
});
console.log(JSON.stringify(response));
expect(response).not.null;
});
});
Expand Down
Loading

0 comments on commit 92f84f5

Please sign in to comment.