Skip to content

Commit

Permalink
fix: Merge branch 'main' into deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed May 30, 2023
2 parents 7953495 + 4c0164d commit 1d52d6b
Show file tree
Hide file tree
Showing 20 changed files with 1,099 additions and 1,510 deletions.
2 changes: 1 addition & 1 deletion packages/demoreact/src/app/ChatTest/GetUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const GetUserTest = () => {
) : null}
</div>
</SectionItem>
{connectedUser && (connectedUser as IUser).nftOwner !== null && (
{connectedUser && (connectedUser as IUser).did.split(':')[0] === 'nft' && (
<SectionItem style={{ marginTop: 20 }}>
<div>
{connectedUser ? <CodeFormatter>{password}</CodeFormatter> : null}
Expand Down
775 changes: 265 additions & 510 deletions packages/restapi/CHANGELOG.md

Large diffs are not rendered by default.

1,439 changes: 650 additions & 789 deletions packages/restapi/README.md

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions packages/restapi/src/lib/chat/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ export const getEncryptedRequest = async (
user: receiverAddress,
publicKey: '',
encryptedPrivateKey: '',
encryptionType: '',
signature: 'pgp',
sigType: 'pgp',
env,
});
// If the user is being created here, that means that user don't have a PGP keys. So this intent will be in plaintext
Expand Down
77 changes: 12 additions & 65 deletions packages/restapi/src/lib/chat/helpers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ import {
walletType,
} from '../../types';
import { getEip191Signature } from './crypto';
import { populateDeprecatedUser } from '../../utils/populateIUser';

type CreateUserOptionsType = {
user: string;
wallet?: walletType;
name?: string;
nftOwner?: string | null;
encryptedPassword?: string | null;
publicKey?: string;
encryptedPrivateKey?: string;
encryptionType?: string;
signature?: string;
sigType?: string;
env?: ENV;
};

Expand All @@ -34,16 +29,13 @@ export const createUserService = async (options: CreateUserOptionsType) => {
wallet,
publicKey = '',
encryptedPrivateKey = '',
encryptionType = '',
env = Constants.ENV.PROD,
encryptedPassword = null,
nftOwner = null,
} = options || {};
let { user } = options || {};

const API_BASE_URL = getAPIBaseUrls(env);

const requestUrl = `${API_BASE_URL}/v1/users/`;
const requestUrl = `${API_BASE_URL}/v2/users/`;

if (isValidCAIP10NFTAddress(user)) {
const epoch = Math.floor(Date.now() / 1000);
Expand All @@ -56,33 +48,14 @@ export const createUserService = async (options: CreateUserOptionsType) => {
did: walletToPCAIP10(user),
publicKey,
encryptedPrivateKey,

// DEPRECATED in eip191v2

// encryptionType,
// name: '',
// encryptedPassword: encryptedPassword,
// nftOwner: nftOwner ? nftOwner.toLowerCase() : nftOwner,
};

const hash = generateHash(data);

const signatureObj = await getEip191Signature(wallet!, hash, 'v2');

// NOTE - To be removed after backend route changes
const updatedData = {
caip10: walletToPCAIP10(user),
did: walletToPCAIP10(user),
publicKey,
encryptedPrivateKey,
encryptionType,
name: '',
encryptedPassword: encryptedPassword,
nftOwner: nftOwner ? nftOwner.toLowerCase() : nftOwner,
};

const body = {
...updatedData,
...data,
...signatureObj,
};

Expand All @@ -91,12 +64,11 @@ export const createUserService = async (options: CreateUserOptionsType) => {
.then((response) => {
if (response.data)
response.data.publicKey = verifyPGPPublicKey(
response.data.encryptionType,
response.data.encryptedPrivateKey,
response.data.publicKey,
response.data.did,
response.data.nftOwner
response.data.did
);
return response.data;
return populateDeprecatedUser(response.data);
})
.catch((err) => {
console.error(`[Push SDK] - API ${requestUrl}: `, err);
Expand All @@ -110,62 +82,37 @@ export const authUpdateUserService = async (options: CreateUserOptionsType) => {
wallet,
publicKey = '',
encryptedPrivateKey = '',
encryptionType = '',
name = '',
encryptedPassword = null,
nftOwner = null,
env = Constants.ENV.PROD,
} = options || {};

const API_BASE_URL = getAPIBaseUrls(env);

const requestUrl = `${API_BASE_URL}/v1/users/${walletToPCAIP10(user)}/auth`;
const requestUrl = `${API_BASE_URL}/v2/users/${walletToPCAIP10(user)}/auth`;

const data = {
caip10: walletToPCAIP10(user),
did: walletToPCAIP10(user),
publicKey,
encryptedPrivateKey,

// DEPRECATED in eip191v2

// encryptionType,
// name,
// encryptedPassword,
// nftOwner: nftOwner ? nftOwner.toLowerCase() : nftOwner,
};

const hash = generateHash(data);

const signatureObj = await getEip191Signature(wallet!, hash, 'v2');

// NOTE - To be removed after backend route changes
const updatedData = {
caip10: walletToPCAIP10(user),
publicKey,
encryptedPrivateKey,
encryptionType,
name,
encryptedPassword,
nftOwner: nftOwner ? nftOwner.toLowerCase() : nftOwner,
};

const body = {
...updatedData,
...signatureObj,
};
// Exclude the "did" property from the "body" object
const { did, ...body } = { ...data, ...signatureObj };

return axios
.put(requestUrl, body)
.then((response) => {
if (response.data)
response.data.publicKey = verifyPGPPublicKey(
response.data.encryptionType,
response.data.encryptedPrivateKey,
response.data.publicKey,
response.data.did,
response.data.nftOwner
response.data.did
);
return response.data;
return populateDeprecatedUser(response.data);
})
.catch((err) => {
console.error(`[Push SDK] - API ${requestUrl}: `, err);
Expand Down
8 changes: 2 additions & 6 deletions packages/restapi/src/lib/chat/helpers/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export const getTypedData = (
export const verifyProfileSignature = (
verificationProof: string,
signedData: string,
address: string,
nftOwner: string
address: string
): boolean => {
const SIG_TYPE_V2 = 'eip712v2';
const SIG_TYPE_V3 = 'eip191';
Expand Down Expand Up @@ -138,10 +137,7 @@ export const verifyProfileSignature = (
hashMessage(signedData),
signature
);
if (
recoveredAddress.toLowerCase() === address.toLowerCase() ||
recoveredAddress.toLowerCase() === nftOwner.toLowerCase()
) {
if (recoveredAddress.toLowerCase() === address.toLowerCase()) {
return true;
} else return false;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/restapi/src/lib/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ export * from './chats';
export * from './chat';
export * from './requests';
export * from './send';
export * from "./conversationHash";
export * from './conversationHash';
export * from './approveRequest';
export * from './updateUser';
export * from './historicalMessages';
export * from './latestMessage';
export { decryptWithWalletRPCMethod } from "../helpers/crypto";
export { decryptPGPKey } from "../helpers/crypto";
export { decryptConversation } from "./helpers";
export { decryptWithWalletRPCMethod } from '../helpers/crypto';
export { decryptPGPKey } from '../helpers/crypto';
export { decryptConversation } from './helpers';
export * from './helpers/payloadHelper';
export * from './createGroup';
export * from './updateGroup';
Expand Down
40 changes: 0 additions & 40 deletions packages/restapi/src/lib/chat/updateUser.ts

This file was deleted.

51 changes: 30 additions & 21 deletions packages/restapi/src/lib/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
IMessageIPFS,
ProgressHookType,
} from '../types';
import { isValidETHAddress, pCAIP10ToWallet } from './address';
import {
isValidCAIP10NFTAddress,
isValidETHAddress,
pCAIP10ToWallet,
} from './address';
import { verifyProfileSignature } from '../chat/helpers/signature';
import { upgrade } from '../user/upgradeUser';

Expand Down Expand Up @@ -575,30 +579,35 @@ export const preparePGPPublicKey = async (
};

export const verifyPGPPublicKey = (
encryptionType: string,
encryptedPrivateKey: string,
publicKey: string,
did: string,
nftOwner: string
did: string
): string => {
if (encryptionType && encryptionType !== Constants.ENC_TYPE_V1) {
const { key, signature: verificationProof } = JSON.parse(publicKey);
publicKey = key;
let signedData: string;
if (encryptionType === Constants.ENC_TYPE_V2)
signedData = 'Create Push Chat Profile \n' + generateHash(key);
else signedData = 'Create Push Profile \n' + generateHash(key);
if (
verifyProfileSignature(
verificationProof,
signedData,
pCAIP10ToWallet(did),
nftOwner ? pCAIP10ToWallet(nftOwner) : nftOwner
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 (
verifyProfileSignature(
verificationProof,
signedData,
isValidCAIP10NFTAddress(did)
? pCAIP10ToWallet(JSON.parse(encryptedPrivateKey).owner)
: pCAIP10ToWallet(did)
)
)
)
return publicKey;
else throw new Error('Cannot verify Encryption Keys for this user');
return publicKey;
else throw new Error('Cannot Verify this publicKey Owner!!!');
}
return publicKey;
} catch (err) {
console.warn('Cannot Verify this publicKey Owner!!!');
return publicKey;
}
return publicKey;
};

export const validatePssword = (password: string) => {
Expand Down
Loading

0 comments on commit 1d52d6b

Please sign in to comment.