Skip to content

Commit

Permalink
Chat keys Encryption V2 (#231)
Browse files Browse the repository at this point in the history
* feat: chat key encryption v2

BREAKING CHANGE: New chat key ecnryption

* feat: chat key encryption v2

New encryption version for pgp keys

BREAKING CHANGE: Signer is required in ui-web

* refactor: add signer in ApproveRequest

* fix: revert dev from localhost

* feat: auto uprade keys

* test: tests for upgrade User

* test: test for send chat message

* test: rename tests

* test: text formating fix
  • Loading branch information
Aman035 authored Mar 27, 2023
1 parent 348cfba commit f35730b
Show file tree
Hide file tree
Showing 32 changed files with 15,362 additions and 13,974 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"expo-linear-gradient": "~11.3.0",
"html-react-parser": "^1.4.13",
"mocha": "^10.2.0",
"mocha-typescript": "^1.1.17",
"moment": "^2.29.4",
"react": "17.0.2",
"react-dom": "17.0.2",
Expand Down Expand Up @@ -69,8 +70,10 @@
"@testing-library/jest-native": "4.0.4",
"@testing-library/react": "12.1.5",
"@testing-library/react-native": "9.1.0",
"@types/chai": "^4.3.4",
"@types/crypto-js": "^4.1.1",
"@types/jest": "27.4.1",
"@types/mocha": "^10.0.1",
"@types/node": "16.11.7",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
Expand All @@ -86,6 +89,7 @@
"@web3-react/types": "^6.0.7",
"babel-jest": "27.5.1",
"babel-plugin-styled-components": "^1.10.7",
"chai-as-promised": "^7.1.1",
"commitizen": "^4.2.5",
"cz-conventional-changelog": "^3.3.0",
"eslint": "~8.12.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/demoreact/src/app/ChatSupportTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type ChatProps = {
};

export const ChatSupportTest = () => {
const { account } = useContext<any>(Web3Context);
const { account, library } = useContext<any>(Web3Context);
const librarySigner = library.getSigner();
const { env } = useContext<any>(EnvContext);
const theme: ITheme = {
bgColorPrimary: 'gray',
Expand All @@ -32,6 +33,7 @@ export const ChatSupportTest = () => {
return (
<Chat
account={account}
signer={librarySigner}
supportAddress="0xD2A371cAFBE09333F6668229E4BD33196583c325"
apiKey="tAWEnggQ9Z.UaDBNjrvlJZx3giBTIQDcT8bKQo1O1518uF1Tea7rPwfzXv2ouV5rX9ViwgJUrXm"
env={env}
Expand Down
14 changes: 9 additions & 5 deletions packages/demoreact/src/app/ChatTest/ApproveRequestTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { walletToPCAIP10 } from '../helpers';
import ChatTest from './ChatTest';

const ApproveRequestTest = () => {
const { account } = useContext<any>(Web3Context);
const { account, library } = useContext<any>(Web3Context);
const { env, isCAIP } = useContext<any>(EnvContext);
const [isLoading, setLoading] = useState(false);
const [senderAddress, setSenderAddress] = useState<string>('');
Expand All @@ -27,11 +27,14 @@ const ApproveRequestTest = () => {
setLoading(true);
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
const librarySigner = await library.getSigner();
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}

const response = await PushAPI.chat.approve({
Expand All @@ -40,6 +43,7 @@ const ApproveRequestTest = () => {
senderAddress,
env,
pgpPrivateKey: pvtkey,
signer: librarySigner
});

setApproveResponse(response);
Expand Down
10 changes: 6 additions & 4 deletions packages/demoreact/src/app/ChatTest/CreateGroupTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ const CreateGroupTest = () => {
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}

// Remove empty string elements
Expand Down
13 changes: 8 additions & 5 deletions packages/demoreact/src/app/ChatTest/GetChats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { walletToPCAIP10 } from '../helpers';
import ChatTest from './ChatTest';

const GetChatsTest = () => {
const { account } = useContext<any>(Web3Context);
const { account, library } = useContext<any>(Web3Context);
const { env, isCAIP } = useContext<any>(EnvContext);
const [isLoading, setLoading] = useState(false);
const [getChatsResponse, setGetChatsResponse] = useState<any>('');
Expand All @@ -24,13 +24,16 @@ const GetChatsTest = () => {
const testGetChats = async () => {
try {
setLoading(true);
const librarySigner = await library.getSigner();
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}
const response = await PushAPI.chat.chats({
account: isCAIP ? walletToPCAIP10(account) : account,
Expand Down
13 changes: 8 additions & 5 deletions packages/demoreact/src/app/ChatTest/GetRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { walletToPCAIP10 } from '../helpers';
import ChatTest from './ChatTest';

const GetRequestsTest = () => {
const { account } = useContext<any>(Web3Context);
const { account, library } = useContext<any>(Web3Context);
const { env, isCAIP } = useContext<any>(EnvContext);
const [isLoading, setLoading] = useState(false);
const [getRequestsResponse, setGetRequestsResponse] = useState<any>('');
Expand All @@ -26,11 +26,14 @@ const GetRequestsTest = () => {
setLoading(true);
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
const librarySigner = library.getSigner();
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}
const response = await PushAPI.chat.requests({
account: isCAIP ? walletToPCAIP10(account) : account,
Expand Down
13 changes: 8 additions & 5 deletions packages/demoreact/src/app/ChatTest/GetUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { walletToPCAIP10 } from '../helpers';
import ChatTest from './ChatTest';

const GetUserTest = () => {
const { account } = useContext<any>(Web3Context);
const { account, library } = useContext<any>(Web3Context);
const { env, isCAIP } = useContext<any>(EnvContext);
const [isLoading, setLoading] = useState(false);
const [connectedUser, setConnectedUser] = useState<any>({});
Expand Down Expand Up @@ -43,10 +43,13 @@ const GetUserTest = () => {
try {
setLoading(true);
if (Object.keys(connectedUser).length > 0) {
const response = await PushAPI.chat.decryptWithWalletRPCMethod(
(connectedUser as IUser).encryptedPrivateKey,
isCAIP ? walletToPCAIP10(account) : account
);
const librarySigner = await library.getSigner();
const response = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: (connectedUser as IUser).encryptedPrivateKey,
account: isCAIP ? walletToPCAIP10(account) : account,
signer: librarySigner,
env
});

setDecryptedPrivateKey(response);
} else return;
Expand Down
24 changes: 15 additions & 9 deletions packages/demoreact/src/app/ChatTest/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ChatTest from './ChatTest';

const HistoryTest = () => {
const { env } = useContext<any>(EnvContext);
const { account } = useContext<any>(Web3Context);
const { account, library } = useContext<any>(Web3Context);
const [isLoading, setLoading] = useState(false);
const [response, setResponse] = useState<any>({});
const [threadhash, setThreadhash] = useState<string>('');
Expand All @@ -39,10 +39,13 @@ const HistoryTest = () => {
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
const librarySigner = library.getSigner();
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}
const response = await PushAPI.chat.history({
threadhash,
Expand All @@ -69,10 +72,13 @@ const HistoryTest = () => {
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
const librarySigner = library.getSigner();
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}
const decryptedChat = await PushAPI.chat.decryptConversation({
messages:response,
Expand Down
9 changes: 6 additions & 3 deletions packages/demoreact/src/app/ChatTest/SendMessageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const SendMessageTest = () => {
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: librarySigner
signer: librarySigner,
env
});
}
response = await PushAPI.chat.send({
Expand All @@ -90,7 +91,8 @@ const SendMessageTest = () => {
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: librarySigner
signer: librarySigner,
env
});
}
response = await PushAPI.chat.send({
Expand All @@ -112,7 +114,8 @@ const SendMessageTest = () => {
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: pvtKeySigner
signer: pvtKeySigner,
env
});
}
response = await PushAPI.chat.send({
Expand Down
12 changes: 7 additions & 5 deletions packages/demoreact/src/app/ChatTest/UpdateGroupTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ const UpdateGroupTest = () => {
setLoading(true);
const user = await PushAPI.user.get({ account: account, env });
let pvtkey = null;
const librarySigner = await library.getSigner();
if (user?.encryptedPrivateKey) {
pvtkey = await PushAPI.chat.decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
account
);
pvtkey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
account,
signer: librarySigner,
env
});
}
const librarySigner = await library.getSigner();
const response = await PushAPI.chat.updateGroup({
chatId,
groupName,
Expand Down
41 changes: 41 additions & 0 deletions packages/restapi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
"typescript": "^5.0.2"
},
"scripts": {
"test": "TS_NODE_PROJECT='./tsconfig.mocha.json' NODE_OPTIONS='--loader ts-node/esm' mocha -r ts-node/register 'tests/**/*.test.ts' --timeout 12000 --require tests/root.ts --serial"
"test": "TS_NODE_PROJECT='./tsconfig.mocha.json' NODE_OPTIONS='--loader ts-node/esm' mocha -r ts-node/register 'tests/**/*.test.ts' --timeout 120000 --require tests/root.ts --serial"
},
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/crypto-js": "^4.1.1",
"@types/mocha": "^10.0.1",
"chai-as-promised": "^7.1.1",
"mocha": "^10.2.0"
}
}
Loading

0 comments on commit f35730b

Please sign in to comment.