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

Reduce profile creation signature to 2 #639

Merged
merged 2 commits into from
Aug 16, 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
15 changes: 9 additions & 6 deletions packages/examples/sdk-backend-node/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const runChatUseCases = async (): Promise<void> => {
await PushAPI_chat_video_call_notification(TargetChatId);

console.log('PushAPI.chat.createGroup');
const chatId = await PushAPI_chat_createGroup();
const { chatId, name } = await PushAPI_chat_createGroup();

console.log('PushAPI.chat.conversationHash');
await PushAPI_chat_conversationHash();
Expand All @@ -112,7 +112,7 @@ export const runChatUseCases = async (): Promise<void> => {
await PushAPI_chat_updateGroup(chatId);

console.log('PushAPI.chat.getGroupByName');
await PushAPI_chat_getGroupByName();
await PushAPI_chat_getGroupByName(name);

console.log('PushAPI.chat.getGroup');
await PushAPI_chat_getGroup(chatId);
Expand Down Expand Up @@ -400,7 +400,7 @@ async function PushAPI_chat_approve(silent = !showAPIResponse) {
// Push Chat - PushAPI.chat.createGroup
async function PushAPI_chat_createGroup(
silent = !showAPIResponse
): Promise<string> {
): Promise<{ chatId: string; name: string }> {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signerAddress}`,
Expand Down Expand Up @@ -433,7 +433,7 @@ async function PushAPI_chat_createGroup(
if (!silent) {
console.log(response);
}
return response.chatId;
return { chatId: response.chatId, name: response.groupName };
}

// Push Chat - PushAPI.chat.updateGroup
Expand Down Expand Up @@ -483,9 +483,12 @@ async function PushAPI_chat_updateGroup(
}

// Push Chat - PushAPI.chat.getGroupByName
async function PushAPI_chat_getGroupByName(silent = !showAPIResponse) {
async function PushAPI_chat_getGroupByName(
name: string,
silent = !showAPIResponse
) {
const response = await PushAPI.chat.getGroupByName({
groupName: 'Push Group Chat 3',
groupName: name,
env: env as ENV,
});

Expand Down
9 changes: 6 additions & 3 deletions packages/examples/sdk-backend-node/src/chat/nftChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const runNFTChatUseCases = async (): Promise<void> => {
await PushAPI_nft_chat_updateGroup(chatId);

console.log('PushAPI.chat.getGroupByName');
await PushAPI_nft_chat_getGroupByName();
await PushAPI_nft_chat_getGroupByName(updatedNftGroupName);

console.log('PushAPI.chat.getGroup');
await PushAPI_nft_chat_getGroup(chatId);
Expand Down Expand Up @@ -509,9 +509,12 @@ async function PushAPI_nft_chat_updateGroup(
}

// Push Chat - PushAPI.chat.getGroupByName
async function PushAPI_nft_chat_getGroupByName(silent = !showAPIResponse) {
async function PushAPI_nft_chat_getGroupByName(
name: string,
silent = !showAPIResponse
) {
const response = await PushAPI.chat.getGroupByName({
groupName: 'Push Group Chat 3',
groupName: name,
env: env as ENV,
});

Expand Down
14 changes: 9 additions & 5 deletions packages/restapi/src/lib/chat/helpers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getAPIBaseUrls,
getQueryParams,
isValidCAIP10NFTAddress,
verifyPGPPublicKey,
verifyProfileKeys,
walletToPCAIP10,
} from '../../helpers';
import {
Expand Down Expand Up @@ -63,10 +63,12 @@ export const createUserService = async (options: CreateUserOptionsType) => {
.post(requestUrl, body)
.then(async (response) => {
if (response.data)
response.data.publicKey = await verifyPGPPublicKey(
response.data.publicKey = await verifyProfileKeys(
response.data.encryptedPrivateKey,
response.data.publicKey,
response.data.did
response.data.did,
response.data.wallets,
response.data.verificationProof
);
return populateDeprecatedUser(response.data);
})
Expand Down Expand Up @@ -106,10 +108,12 @@ export const authUpdateUserService = async (options: CreateUserOptionsType) => {
.put(requestUrl, body)
.then(async (response) => {
if (response.data)
response.data.publicKey = verifyPGPPublicKey(
response.data.publicKey = await verifyProfileKeys(
response.data.encryptedPrivateKey,
response.data.publicKey,
response.data.did
response.data.did,
response.data.wallets,
response.data.verificationProof
);
return populateDeprecatedUser(response.data);
})
Expand Down
6 changes: 4 additions & 2 deletions packages/restapi/src/lib/chat/helpers/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ export const verifyProfileSignature = async (
): Promise<boolean> => {
const SIG_TYPE_V2 = 'eip712v2';
const SIG_TYPE_V3 = 'eip191';
const SIG_TYPE_V4 = 'eip191v2';
let chainId: number | null = null;
let signature: string;
const sigType = verificationProof.split(':')[0];

if (
(sigType !== SIG_TYPE_V2 && sigType !== SIG_TYPE_V3) ||
(sigType !== SIG_TYPE_V2 &&
sigType !== SIG_TYPE_V3 &&
sigType !== SIG_TYPE_V4) ||
verificationProof.split(':').length > 3
) {
return false;
Expand Down
116 changes: 74 additions & 42 deletions packages/restapi/src/lib/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,26 +448,24 @@ export const preparePGPPublicKey = async (
chatPublicKey = publicKey;
break;
}
case Constants.ENC_TYPE_V3: {
const createProfileMessage =
'Create Push Profile \n' + generateHash(publicKey);
const { verificationProof } = await getEip191Signature(
wallet,
createProfileMessage
);
chatPublicKey = JSON.stringify({
key: publicKey,
signature: verificationProof,
});
break;
}
case Constants.ENC_TYPE_V3:
case Constants.ENC_TYPE_V4: {
const createProfileMessage =
'Create Push Profile \n' + generateHash(publicKey);
const { verificationProof } = await getEip191Signature(
wallet,
createProfileMessage
);
const verificationProof = 'DEPRECATED';

/**
* @deprecated
* PUSH CHAT PROFILE CREATION DOES NOT SIGN PGP PUBLIC KEY
* VERIFICATION PROOF SIGNATURE SHOULD BE USED FOR VERIFICATION OF PUSH PROFILE KEYS
*/

// const createProfileMessage =
// 'Create Push Profile \n' + generateHash(publicKey);
// const { verificationProof } = await getEip191Signature(
// wallet,
// createProfileMessage
// );

// TODO - Change JSON Structure to string ie equivalent to ENC_TYPE_V1 ( would be done after PUSH Node changes )
chatPublicKey = JSON.stringify({
key: publicKey,
signature: verificationProof,
Expand All @@ -480,35 +478,69 @@ export const preparePGPPublicKey = async (
return chatPublicKey;
};

export const verifyPGPPublicKey = async (
/**
* Checks the Push Profile keys using verificationProof
* @param encryptedPrivateKey
* @param publicKey
* @param did
* @param caip10
* @param verificationProof
* @returns PGP Public Key
*/
export const verifyProfileKeys = async (
encryptedPrivateKey: string,
publicKey: string,
did: string
did: string,
caip10: string,
verificationProof: string
): Promise<string> => {
let parsedPublicKey: string;
try {
if (publicKey !== '' && publicKey.includes('signature')) {
const { key, signature: verificationProof } = JSON.parse(publicKey);
publicKey = key;
let signedData: string;
if (verificationProof.includes('eip712'))
signedData = 'Create Push Chat Profile \n' + generateHash(key);
else signedData = 'Create Push Profile \n' + generateHash(key);
if (
await verifyProfileSignature(
verificationProof,
signedData,
isValidCAIP10NFTAddress(did)
? pCAIP10ToWallet(JSON.parse(encryptedPrivateKey).owner)
: pCAIP10ToWallet(did)
)
)
return publicKey;
else throw new Error('Cannot Verify this publicKey Owner!!!');
parsedPublicKey = JSON.parse(publicKey).key;
if (parsedPublicKey === undefined) {
throw new Error('Invalid Public Key');
}
} catch (err) {
parsedPublicKey = publicKey;
}

try {
if (publicKey && publicKey.length > 0 && verificationProof) {
const data = {
caip10,
did,
publicKey,
encryptedPrivateKey,
};

if (isValidCAIP10NFTAddress(did)) {
const keyToRemove = 'owner';
const parsedEncryptedPrivateKey = JSON.parse(encryptedPrivateKey);
if (keyToRemove in parsedEncryptedPrivateKey) {
delete parsedEncryptedPrivateKey[keyToRemove];
}
data.encryptedPrivateKey = JSON.stringify(parsedEncryptedPrivateKey);
}

const signedData = generateHash(data);

const isValidSig: boolean = await verifyProfileSignature(
verificationProof,
signedData,
isValidCAIP10NFTAddress(did)
? pCAIP10ToWallet(JSON.parse(encryptedPrivateKey).owner)
: pCAIP10ToWallet(did)
);
if (isValidSig) {
return parsedPublicKey;
} else {
throw new Error('Invalid Signature');
}
}
return publicKey;
return parsedPublicKey;
} catch (err) {
console.warn('Cannot Verify this publicKey Owner!!!');
return publicKey;
console.warn(`Cannot Verify keys for DID : ${did} !!!`);
return parsedPublicKey;
}
};

Expand Down
8 changes: 5 additions & 3 deletions packages/restapi/src/lib/user/getUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { AccountEnvOptionsType, IUser } from '../types';
import { isValidETHAddress, walletToPCAIP10 } from '../helpers/address';
import { getAPIBaseUrls, verifyPGPPublicKey } from '../helpers';
import { getAPIBaseUrls, verifyProfileKeys } from '../helpers';
import Constants from '../constants';
import { populateDeprecatedUser } from '../utils/populateIUser';

Expand All @@ -17,10 +17,12 @@ export const get = async (options: AccountEnvOptionsType): Promise<IUser> => {
.get(requestUrl)
.then(async (response) => {
if (response.data) {
response.data.publicKey = await verifyPGPPublicKey(
response.data.publicKey = await verifyProfileKeys(
response.data.encryptedPrivateKey,
response.data.publicKey,
response.data.did
response.data.did,
response.data.wallets,
response.data.verificationProof
);
}
return populateDeprecatedUser(response.data);
Expand Down
8 changes: 5 additions & 3 deletions packages/restapi/src/lib/user/getUsersBatch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { IUser } from '../types';
import { isValidETHAddress, walletToPCAIP10 } from '../helpers/address';
import { getAPIBaseUrls, verifyPGPPublicKey } from '../helpers';
import { getAPIBaseUrls, verifyProfileKeys } from '../helpers';
import Constants, { ENV } from '../constants';
import { populateDeprecatedUser } from '../utils/populateIUser';

Expand Down Expand Up @@ -36,10 +36,12 @@ export const getBatch = async (options: GetBatchType): Promise<IUser> => {
.post(requestUrl, requestBody)
.then((response) => {
response.data.users.forEach(async (user: any, index: number) => {
response.data.users[index].publicKey = await verifyPGPPublicKey(
response.data.users[index].publicKey = await verifyProfileKeys(
user.encryptedPrivateKey,
user.publicKey,
user.did
user.did,
user.caip10,
user.verificationProof
);

response.data.users[index] = populateDeprecatedUser(
Expand Down
8 changes: 5 additions & 3 deletions packages/restapi/src/lib/user/profile.updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Constants, { ENV } from '../constants';
import {
getAPIBaseUrls,
isValidETHAddress,
verifyPGPPublicKey,
verifyProfileKeys,
} from '../helpers';
import { IUser, ProgressHookType, ProgressHookTypeFunction } from '../types';
import { get } from './getUser';
Expand Down Expand Up @@ -99,10 +99,12 @@ export const profileUpdate = async (
progressHook?.(PROGRESSHOOK['PUSH-PROFILE-UPDATE-01'] as ProgressHookType);
const response = await axios.put(apiEndpoint, body);
if (response.data)
response.data.publicKey = await verifyPGPPublicKey(
response.data.publicKey = await verifyProfileKeys(
response.data.encryptedPrivateKey,
response.data.publicKey,
response.data.did
response.data.did,
response.data.wallets,
response.data.verificationProof
);

// Report Progress
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/tests/lib/chat/updateGroup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('Update Group', () => {
const admins: string[] = [];
const members = [
account2,
// 'eip155:0xDB0Bb1C25e36a5Ec9d199688bB01eADa4e70225E',
'eip155:0xDB0Bb1C25e36a5Ec9d199688bB01eADa4e70225E',
];
// creator - account
const group = await createGroup({
Expand Down
Loading