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 Apr 19, 2023
2 parents d0e7b13 + 069783e commit 7f35e12
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 179 deletions.
217 changes: 110 additions & 107 deletions packages/restapi/src/lib/chat/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import {
walletType,
} from '../../types';
import { get } from '../../user';
import { decryptPGPKey, decryptWithWalletRPCMethod, isValidETHAddress, walletToPCAIP10, decryptAndVerifySignature } from '../../helpers';
import {
decryptPGPKey,
decryptWithWalletRPCMethod,
isValidETHAddress,
walletToPCAIP10,
decryptAndVerifySignature,
} from '../../helpers';
import { get as getUser } from '../../user';
import { createUserService } from './service';
import Constants, {ENV} from '../../constants';
import Constants, { ENV } from '../../constants';
import { getDomainInformation, getTypeInformation } from './signature';

const SIG_TYPE_V2 = "eip712v2";
const SIG_TYPE_V2 = 'eip712v2';

interface IEncryptedRequest {
message: string;
Expand All @@ -31,7 +37,7 @@ export const encryptAndSign = async ({
privateKeyArmored,
}: {
plainText: string;
keys: Array<string>
keys: Array<string>;
privateKeyArmored: string;
}): Promise<{
cipherText: string;
Expand Down Expand Up @@ -59,7 +65,6 @@ export const encryptAndSign = async ({
};
};


export const signMessageWithPGP = async ({
message,
privateKeyArmored,
Expand All @@ -77,7 +82,7 @@ export const signMessageWithPGP = async ({

return {
signature,
sigType: 'pgp'
sigType: 'pgp',
};
};

Expand All @@ -89,36 +94,36 @@ export const decryptFeeds = async ({
}: {
feeds: IFeeds[];
connectedUser: IUser;
pgpPrivateKey?:string;
pgpPrivateKey?: string;
env: ENV;
}): Promise<IFeeds[]> => {
let otherPeer: IUser;
let signatureValidationPubliKey: string; // To do signature verification it depends on who has sent the message
for (const feed of feeds) {
let gotOtherPeer = false;
if (feed.msg.encType !== 'PlainText') {
if (!pgpPrivateKey) {
throw Error('Decrypted private key is necessary');
}
if (feed.msg.fromCAIP10 !== connectedUser.wallets.split(',')[0]) {
if (!gotOtherPeer) {
otherPeer = await getUser({ account: feed.msg.fromCAIP10, env });
gotOtherPeer = true;
}
signatureValidationPubliKey = otherPeer!.publicKey!;
} else {
signatureValidationPubliKey = connectedUser.publicKey!;
let otherPeer: IUser;
let signatureValidationPubliKey: string; // To do signature verification it depends on who has sent the message
for (const feed of feeds) {
let gotOtherPeer = false;
if (feed.msg.encType !== 'PlainText') {
if (!pgpPrivateKey) {
throw Error('Decrypted private key is necessary');
}
if (feed.msg.fromCAIP10 !== connectedUser.wallets.split(',')[0]) {
if (!gotOtherPeer) {
otherPeer = await getUser({ account: feed.msg.fromCAIP10, env });
gotOtherPeer = true;
}
feed.msg.messageContent = await decryptAndVerifySignature({
cipherText: feed.msg.messageContent,
encryptedSecretKey: feed.msg.encryptedSecret,
publicKeyArmored: signatureValidationPubliKey,
signatureArmored: feed.msg.signature,
privateKeyArmored: pgpPrivateKey,
message: feed.msg
});
signatureValidationPubliKey = otherPeer!.publicKey!;
} else {
signatureValidationPubliKey = connectedUser.publicKey!;
}
feed.msg.messageContent = await decryptAndVerifySignature({
cipherText: feed.msg.messageContent,
encryptedSecretKey: feed.msg.encryptedSecret,
publicKeyArmored: signatureValidationPubliKey,
signatureArmored: feed.msg.signature,
privateKeyArmored: pgpPrivateKey,
message: feed.msg,
});
}
}
return feeds;
};

Expand Down Expand Up @@ -162,7 +167,7 @@ export const decryptMessages = async ({
privateKeyArmored: connectedUser.privateKey,
publicKeyArmored: signatureValidationPubliKey,
signatureArmored: savedMsg.signature,
message: savedMsg
message: savedMsg,
});
}
}
Expand All @@ -175,9 +180,8 @@ export const getEncryptedRequest = async (
message: string,
isGroup: boolean,
env: ENV,
group: GroupDTO | null,
group: GroupDTO | null
): Promise<IEncryptedRequest | void> => {

if (!isGroup) {
const receiverCreatedUser: IUser = await get({
account: receiverAddress,
Expand All @@ -198,13 +202,10 @@ export const getEncryptedRequest = async (
});
// If the user is being created here, that means that user don't have a PGP keys. So this intent will be in plaintext

const {
signature
} = await signMessageWithPGP({
message: message,
privateKeyArmored: senderCreatedUser.privateKey!,
});

const { signature } = await signMessageWithPGP({
message: message,
privateKeyArmored: senderCreatedUser.privateKey!,
});

return {
message: message,
Expand All @@ -220,10 +221,7 @@ export const getEncryptedRequest = async (
'-----BEGIN PGP PUBLIC KEY BLOCK-----'
)
) {

const {
signature
} = await signMessageWithPGP({
const { signature } = await signMessageWithPGP({
message: message,
privateKeyArmored: senderCreatedUser.privateKey!,
});
Expand All @@ -235,111 +233,116 @@ export const getEncryptedRequest = async (
signature: signature,
};
} else {
const {
cipherText,
encryptedSecret,
signature,
} = await encryptAndSign({
plainText: message,
keys: [receiverCreatedUser.publicKey, senderCreatedUser.publicKey],
privateKeyArmored: senderCreatedUser.privateKey!,
});
return {
message: cipherText,
encryptionType: 'pgp',
aesEncryptedSecret: encryptedSecret,
signature: signature,
};
}
}
} else if(group) {
if(group.isPublic) {
const {
signature
} = await signMessageWithPGP({
message: message,
privateKeyArmored: senderCreatedUser.privateKey!,
});
return {
message: message,
encryptionType: 'PlainText',
aesEncryptedSecret: '',
signature: signature,
const { cipherText, encryptedSecret, signature } = await encryptAndSign(
{
plainText: message,
keys: [receiverCreatedUser.publicKey, senderCreatedUser.publicKey],
privateKeyArmored: senderCreatedUser.privateKey!,
}
}
else {
const publicKeys: string[] = group.members.map(member => member.publicKey);
const {
cipherText,
encryptedSecret,
signature,
} = await encryptAndSign({
plainText: message,
keys: publicKeys,
privateKeyArmored: senderCreatedUser.privateKey!,
});
);
return {
message: cipherText,
encryptionType: 'pgp',
aesEncryptedSecret: encryptedSecret,
signature: signature,
};
}
}
} else if (group) {
if (group.isPublic) {
const { signature } = await signMessageWithPGP({
message: message,
privateKeyArmored: senderCreatedUser.privateKey!,
});
return {
message: message,
encryptionType: 'PlainText',
aesEncryptedSecret: '',
signature: signature,
};
} else {
const publicKeys: string[] = group.members.map(
(member) => member.publicKey
);
const { cipherText, encryptedSecret, signature } = await encryptAndSign({
plainText: message,
keys: publicKeys,
privateKeyArmored: senderCreatedUser.privateKey!,
});
return {
message: cipherText,
encryptionType: 'pgp',
aesEncryptedSecret: encryptedSecret,
signature: signature,
};
}
}
};

export const getEip191Signature = async (wallet: walletType, message: string) => {
if(!wallet?.signer) {
console.warn("This method is deprecated. Provide signer in the function");
export const getEip191Signature = async (
wallet: walletType,
message: string
) => {
if (!wallet?.signer) {
console.warn('This method is deprecated. Provide signer in the function');
// sending random signature for making it backward compatible
return { signature: "xyz", sigType: "a" };
return { signature: 'xyz', sigType: 'a' };
}
const _signer = wallet?.signer;
// sign a message using EIP191
const signedMessage = await _signer?.signMessage(message);
return {verificationProof: `eip191:${signedMessage}`};
}
return { verificationProof: `eip191:${signedMessage}` };
};

export const getEip712Signature = async (wallet: walletType, hash: string, isDomainEmpty: boolean) => {
if(!wallet?.signer) {
console.warn("This method is deprecated. Provide signer in the function");
export const getEip712Signature = async (
wallet: walletType,
hash: string,
isDomainEmpty: boolean
) => {
if (!wallet?.signer) {
console.warn('This method is deprecated. Provide signer in the function');
// sending random signature for making it backward compatible
return { signature: "xyz", sigType: "a" };
return { signature: 'xyz', sigType: 'a' };
}

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

// sign a message using EIP712
const signedMessage = await _signer?._signTypedData(
const signedMessage = await _signer?._signTypedData!(
isDomainEmpty ? {} : domain,
typeInformation,
{ data: hash },
{ data: hash }
);
const verificationProof = isDomainEmpty ? `${SIG_TYPE_V2}:${signedMessage}` : `${SIG_TYPE_V2}:${chainId}:${signedMessage}`
const verificationProof = isDomainEmpty
? `${SIG_TYPE_V2}:${signedMessage}`
: `${SIG_TYPE_V2}:${chainId}:${signedMessage}`;
return { verificationProof };
}
};

export async function getDecryptedPrivateKey(wallet: walletType, user: any, address: string): Promise<string> {
export async function getDecryptedPrivateKey(
wallet: walletType,
user: any,
address: string
): Promise<string> {
let decryptedPrivateKey;
if (wallet.signer) {
decryptedPrivateKey = await decryptPGPKey({
signer: wallet.signer,
encryptedPGPPrivateKey: user.encryptedPrivateKey
})
encryptedPGPPrivateKey: user.encryptedPrivateKey,
});
} else {
decryptedPrivateKey = await decryptWithWalletRPCMethod(
user.encryptedPrivateKey,
address
);
}
return decryptedPrivateKey;
}
}
34 changes: 17 additions & 17 deletions packages/restapi/src/lib/payloads/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export interface ChainIdToSourceType {
[key: number]: string
[key: number]: string;
}

export const CHAIN_ID_TO_SOURCE: ChainIdToSourceType = {
1: "ETH_MAINNET",
5: "ETH_TEST_GOERLI",
137: "POLYGON_MAINNET",
80001: "POLYGON_TEST_MUMBAI",
56: "BSC_MAINNET",
97: "BSC_TESTNET",
10: "OPTIMISM_MAINNET",
420: "OPTIMISM_TESTNET"
1: 'ETH_MAINNET',
5: 'ETH_TEST_GOERLI',
137: 'POLYGON_MAINNET',
80001: 'POLYGON_TEST_MUMBAI',
56: 'BSC_MAINNET',
97: 'BSC_TESTNET',
10: 'OPTIMISM_MAINNET',
420: 'OPTIMISM_TESTNET',
};

export const SOURCE_TYPES = {
Expand All @@ -22,19 +22,19 @@ export const SOURCE_TYPES = {
BSC_TESTNET: 'BSC_TESTNET',
OPTIMISM_MAINNET: 'OPTIMISM_MAINNET',
OPTIMISM_TESTNET: 'OPTIMISM_TESTNET',
THE_GRAPH: 'THE_GRAPH'
THE_GRAPH: 'THE_GRAPH',
PUSH_VIDEO: 'PUSH_VIDEO',
};

export enum IDENTITY_TYPE {
export enum IDENTITY_TYPE {
MINIMAL = 0,
IPFS = 1,
DIRECT_PAYLOAD = 2,
SUBGRAPH = 3
};
SUBGRAPH = 3,
}

export enum NOTIFICATION_TYPE {
export enum NOTIFICATION_TYPE {
BROADCAST = 1,
TARGETTED = 3,
SUBSET = 4
};

SUBSET = 4,
}
Loading

0 comments on commit 7f35e12

Please sign in to comment.