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

Ethers V6 Support #968

Merged
merged 3 commits into from
Jan 3, 2024
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
8 changes: 4 additions & 4 deletions packages/restapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ This package gives access to Push Protocol (Push Nodes) APIs. Visit [Developer D
## Installation

```bash
yarn add @pushprotocol/restapi@latest ethers@^5.6
yarn add @pushprotocol/restapi@latest ethers
```

or

```bash
npm install @pushprotocol/restapi@latest ethers@^5.6
npm install @pushprotocol/restapi@latest ethers
```

## Import SDK
Expand Down Expand Up @@ -170,7 +170,7 @@ const userAlice = await PushAPI.initialize(signer, {

| Param | Type | Default | Remarks |
| --------------------------------------- | ------------------------------------------------- | ------------- | -------------------------------------------------------------------------------------- |
| `signer` | `SignerType` | - | EthersV5 or Viem Signer. |
| `signer` | `SignerType` | - | Ethers or Viem Signer. |
| `options` \* | `PushAPIInitializeProps` | - | Optional configuration properties for initializing the PushAPI. |
| `options.env` \* | `ENV` | `staging` | API env - 'prod', 'staging', 'dev'. |
| `options.progressHook`\* | `(progress: ProgressHookType) => void` | - | A callback function to receive progress updates during initialization. |
Expand Down Expand Up @@ -707,7 +707,7 @@ const userAlice = await PushAPI.initialize(signer, {

| Param | Type | Default | Remarks |
| --------------------------------------- | ------------------------------------------------- | ------------- | -------------------------------------------------------------------------------------- |
| `signer` | `SignerType` | - | EthersV5 or Viem Signer. |
| `signer` | `SignerType` | - | Ethers or Viem Signer. |
| `options` \* | `PushAPIInitializeProps` | - | Optional configuration properties for initializing the PushAPI. |
| `options.env` \* | `ENV` | `staging` | API env - 'prod', 'staging', 'dev'. |
| `options.progressHook`\* | `(progress: ProgressHookType) => void` | - | A callback function to receive progress updates during initialization. |
Expand Down
5 changes: 3 additions & 2 deletions packages/restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"registry": "https://registry.npmjs.org/"
},
"peerDependencies": {
"ethers": "^5.6.8"
"ethers": "^5.0.0 || ^6.0.0"
},
"dependencies": {
"@ambire/signature-validator": "^1.3.1",
Expand All @@ -19,7 +19,8 @@
"openpgp": "^5.5.0",
"simple-peer": "^9.11.1",
"socket.io-client": "^4.7.2",
"video-stream-merger": "^4.0.1"
"video-stream-merger": "^4.0.1",
"viem": "^1.20.3"
},
"scripts": {
"test": "TS_NODE_PROJECT='./tsconfig.mocha.json' NODE_OPTIONS='--loader ts-node/esm' mocha -r ts-node/register 'tests/**/*.test.ts' --timeout 1200000 --require tests/root.ts --serial"
Expand Down
61 changes: 35 additions & 26 deletions packages/restapi/src/lib/channels/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import axios from "axios";
import {
getCAIPAddress,
getConfig,
getCAIPDetails,
signTypedData
} from '../helpers';
import axios from 'axios';
import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers';
import {
getTypeInformation,
getDomainInformation,
getSubscriptionMessage
getSubscriptionMessage,
} from './signature.helpers';
import Constants, {ENV} from '../constants';
import { SignerType } from "../types";
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';
export type SubscribeOptionsType = {
signer: SignerType;
channelAddress: string;
userAddress: string;
verifyingContractAddress?: string;
env?: ENV;
onSuccess?: () => void
onError?: (err: Error) => void,
}
onSuccess?: () => void;
onError?: (err: Error) => void;
};

export const subscribe = async (
options: SubscribeOptionsType
) => {
export const subscribe = async (options: SubscribeOptionsType) => {
const {
signer,
channelAddress,
Expand All @@ -36,19 +29,26 @@ export const subscribe = async (
} = options || {};

try {
const _channelAddress = await getCAIPAddress(env, channelAddress, 'Channel');
const _channelAddress = await getCAIPAddress(
env,
channelAddress,
'Channel'
);

const channelCAIPDetails = getCAIPDetails(_channelAddress);
if (!channelCAIPDetails) throw Error('Invalid Channel CAIP!');

const chainId = parseInt(channelCAIPDetails.networkId, 10);

const _userAddress = await getCAIPAddress(env, userAddress, 'User');

const userCAIPDetails = getCAIPDetails(_userAddress);
if (!userCAIPDetails) throw Error('Invalid User CAIP!');

const { API_BASE_URL,EPNS_COMMUNICATOR_CONTRACT } = getConfig(env, channelCAIPDetails);
const { API_BASE_URL, EPNS_COMMUNICATOR_CONTRACT } = getConfig(
env,
channelCAIPDetails
);

const requestUrl = `${API_BASE_URL}/v1/channels/${_channelAddress}/subscribe`;

Expand All @@ -59,17 +59,23 @@ export const subscribe = async (
);

// get type information
const typeInformation = getTypeInformation("Subscribe");
const typeInformation = getTypeInformation('Subscribe');

// get message
const messageInformation = getSubscriptionMessage(
channelCAIPDetails.address,
userCAIPDetails.address,
"Subscribe"
'Subscribe'
);

// sign a message using EIP712
const signature = await signTypedData(signer, domainInformation, typeInformation, messageInformation, "Subscribe");
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation as any,
messageInformation,
'Subscribe'
);

const verificationProof = signature; // might change

Expand All @@ -78,18 +84,21 @@ export const subscribe = async (
message: {
...messageInformation,
channel: _channelAddress,
subscriber: _userAddress
subscriber: _userAddress,
},
};

await axios.post(requestUrl, body);

if (typeof onSuccess === 'function') onSuccess();

return { status: "success", message: "successfully opted into channel" };
return { status: 'success', message: 'successfully opted into channel' };
} catch (err) {
if (typeof onError === 'function') onError(err as Error);

return { status: "error", message: err instanceof Error ? err.message : JSON.stringify(err) };
return {
status: 'error',
message: err instanceof Error ? err.message : JSON.stringify(err),
};
}
}
};
18 changes: 5 additions & 13 deletions packages/restapi/src/lib/channels/subscribeV2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import axios from 'axios';
import {
getCAIPAddress,
getConfig,
getCAIPDetails,
signTypedData,
} from '../helpers';
import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers';
import {
getDomainInformation,
getTypeInformationV2,
Expand Down Expand Up @@ -77,8 +72,8 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
),
};
// sign a message using EIP712
const signature = await signTypedData(
signer,
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation,
messageInformation,
Expand All @@ -89,9 +84,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {

const body = {
verificationProof: `eip712v2:${verificationProof}`,
message:
messageInformation.data,

message: messageInformation.data,
};

const res = await axios.post(requestUrl, body);
Expand All @@ -100,11 +93,10 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {

return { status: res.status, message: 'successfully opted into channel' };
} catch (err: any) {

if (typeof onError === 'function') onError(err as Error);

return {
status: err?.response?.status?? '' ,
status: err?.response?.status ?? '',
message: err instanceof Error ? err.message : JSON.stringify(err),
};
}
Expand Down
63 changes: 36 additions & 27 deletions packages/restapi/src/lib/channels/unsubscribe.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import axios from "axios";
import {
getCAIPAddress,
getConfig,
getCAIPDetails,
signTypedData
} from '../helpers';
import axios from 'axios';
import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers';
import {
getTypeInformation,
getDomainInformation,
getSubscriptionMessage
getSubscriptionMessage,
} from './signature.helpers';
import Constants, {ENV} from '../constants';
import { SignerType } from "../types";
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';

export type UnSubscribeOptionsType = {
signer: SignerType;
channelAddress: string;
userAddress: string;
verifyingContractAddress?: string;
env?: ENV;
onSuccess?: () => void
onError?: (err: Error) => void,
}
onSuccess?: () => void;
onError?: (err: Error) => void;
};

export const unsubscribe = async (
options: UnSubscribeOptionsType
) => {
export const unsubscribe = async (options: UnSubscribeOptionsType) => {
const {
signer,
channelAddress,
Expand All @@ -37,19 +30,26 @@ export const unsubscribe = async (
} = options || {};

try {
const _channelAddress = await getCAIPAddress(env, channelAddress, 'Channel');
const _channelAddress = await getCAIPAddress(
env,
channelAddress,
'Channel'
);

const channelCAIPDetails = getCAIPDetails(_channelAddress);
if (!channelCAIPDetails) throw Error('Invalid Channel CAIP!');

const chainId = parseInt(channelCAIPDetails.networkId, 10);

const _userAddress = await getCAIPAddress(env, userAddress, 'User');

const userCAIPDetails = getCAIPDetails(_userAddress);
if (!userCAIPDetails) throw Error('Invalid User CAIP!');

const { API_BASE_URL,EPNS_COMMUNICATOR_CONTRACT } = getConfig(env, channelCAIPDetails);
const { API_BASE_URL, EPNS_COMMUNICATOR_CONTRACT } = getConfig(
env,
channelCAIPDetails
);

const requestUrl = `${API_BASE_URL}/v1/channels/${_channelAddress}/unsubscribe`;

Expand All @@ -60,17 +60,23 @@ export const unsubscribe = async (
);

// get type information
const typeInformation = getTypeInformation("Unsubscribe");
const typeInformation = getTypeInformation('Unsubscribe');

// get message
const messageInformation = getSubscriptionMessage(
channelCAIPDetails.address,
userCAIPDetails.address,
"Unsubscribe"
'Unsubscribe'
);

// sign a message using EIP712
const signature = await signTypedData(signer, domainInformation, typeInformation, messageInformation, "Unsubscribe");
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation as any,
messageInformation,
'Unsubscribe'
);

const verificationProof = signature; // might change

Expand All @@ -79,18 +85,21 @@ export const unsubscribe = async (
message: {
...messageInformation,
channel: _channelAddress,
unsubscriber: _userAddress
unsubscriber: _userAddress,
},
};

await axios.post(requestUrl, body);

if (typeof onSuccess === 'function') onSuccess();

return { status: "success", message: "successfully opted out channel" };
return { status: 'success', message: 'successfully opted out channel' };
} catch (err) {
if (typeof onError === 'function') onError(err as Error);

return { status: "error", message: err instanceof Error ? err.message : JSON.stringify(err) };
return {
status: 'error',
message: err instanceof Error ? err.message : JSON.stringify(err),
};
}
}
};
15 changes: 4 additions & 11 deletions packages/restapi/src/lib/channels/unsubscribeV2.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import axios from 'axios';
import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers';
import {
getCAIPAddress,
getConfig,
getCAIPDetails,
signTypedData,
} from '../helpers';
import {
getTypeInformation,
getDomainInformation,
getSubscriptionMessage,
getTypeInformationV2,
getSubscriptionMessageV2,
} from './signature.helpers';
Expand Down Expand Up @@ -79,8 +72,8 @@ export const unsubscribeV2 = async (options: UnSubscribeOptionsV2Type) => {
};

// sign a message using EIP712
const signature = await signTypedData(
signer,
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation,
messageInformation,
Expand All @@ -103,7 +96,7 @@ export const unsubscribeV2 = async (options: UnSubscribeOptionsV2Type) => {
if (typeof onError === 'function') onError(err as Error);

return {
status: err?.response?.status?? '' ,
status: err?.response?.status ?? '',
message: err instanceof Error ? err.message : JSON.stringify(err),
};
}
Expand Down
Loading
Loading