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

fix: fix viem support, add ethers support #929

Merged
merged 1 commit into from
Dec 18, 2023
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
4 changes: 1 addition & 3 deletions packages/restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"peerDependencies": {
"ethers": "^5.6.8"
},
"dependencies": {
"ethers": "^5.6.8",
"@ambire/signature-validator": "^1.3.1",
"@metamask/eth-sig-util": "^5.0.2",
"@pushprotocol/socket": "^0.5.2",
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 = 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
13 changes: 6 additions & 7 deletions packages/restapi/src/lib/chat/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ import {
} from '../../types';
import { get } from '../../user';
import {
Signer,
decryptPGPKey,
decryptWithWalletRPCMethod,
isValidETHAddress,
walletToPCAIP10,
signTypedData,
signMessage,
} from '../../helpers';
import { get as getUser } from '../../user';
import { createUserService } from './service';
Expand Down Expand Up @@ -363,7 +361,8 @@ export const getEip191Signature = async (
const _signer = wallet?.signer;
// EIP191 signature

const signature = await signMessage(_signer, message);
const pushSigner = new Signer(_signer);
const signature = await pushSigner.signMessage(message);
const sigType = version === 'v1' ? 'eip191' : 'eip191v2';
return { verificationProof: `${sigType}:${signature}` };
};
Expand All @@ -381,17 +380,17 @@ export const getEip712Signature = async (

const typeInformation = getTypeInformation();
const _signer = wallet?.signer;
const pushSigner = new Signer(_signer);
let chainId: number;
try {
chainId = await _signer.getChainId();
chainId = await pushSigner.getChainId();
} catch (err) {
chainId = 1;
}
const domain = getDomainInformation(chainId);

// sign a message using EIP712
const signedMessage = await signTypedData(
_signer,
const signedMessage = await pushSigner.signTypedData(
isDomainEmpty ? {} : domain,
typeInformation,
{ data: hash },
Expand Down
Loading