Skip to content

Commit

Permalink
fix: changed AdditionalMeta structure (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 authored May 4, 2023
1 parent 863f48f commit ee8ca02
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
6 changes: 5 additions & 1 deletion packages/demoreact/src/app/ChatTest/CreateUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ const CreateUserTest = () => {
signer: librarySigner,
account: account,
env,
additionalMeta: { password: password },
additionalMeta: {
NFTPGP_V1: {
password: password,
},
},
});
}
break;
Expand Down
6 changes: 5 additions & 1 deletion packages/demoreact/src/app/ChatTest/UpgradeUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const UpgradeUserTest = () => {
signer: librarySigner,
account: account,
env,
additionalMeta: { password: password },
additionalMeta: {
NFTPGP_V1: {
password: password,
},
},
});
}
break;
Expand Down
20 changes: 14 additions & 6 deletions packages/restapi/src/lib/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ type decryptPgpKeyProps = {
signer?: SignerType | null;
env?: ENV;
toUpgrade?: boolean;
additionalMeta?: { password?: string };
additionalMeta?: {
NFTPGP_V1?: {
password: string;
};
};
progressHook?: (progress: ProgressHookType) => void;
};

Expand Down Expand Up @@ -205,8 +209,8 @@ export const decryptPGPKey = async (options: decryptPgpKeyProps) => {
}
case Constants.ENC_TYPE_V4: {
let password: string | null = null;
if (additionalMeta?.password) {
password = additionalMeta.password;
if (additionalMeta?.NFTPGP_V1) {
password = additionalMeta.NFTPGP_V1.password;
} else {
if (!wallet?.signer) {
throw new Error(
Expand Down Expand Up @@ -464,7 +468,11 @@ export const encryptPGPKey = async (
encryptionType: string,
privateKey: string,
wallet: walletType,
addtionalMeta?: { password?: string }
additionalMeta?: {
NFTPGP_V1?: {
password: string;
};
}
): Promise<encryptedPrivateKeyType> => {
let encryptedPrivateKey: encryptedPrivateKeyType;
switch (encryptionType) {
Expand Down Expand Up @@ -504,14 +512,14 @@ export const encryptPGPKey = async (
break;
}
case Constants.ENC_TYPE_V4: {
if (!addtionalMeta?.password) {
if (!additionalMeta?.NFTPGP_V1?.password) {
throw new Error('Password is required!');
}
const enc = new TextEncoder();
const encodedPrivateKey = enc.encode(privateKey);
encryptedPrivateKey = await encryptV2(
encodedPrivateKey,
hexToBytes(stringToHex(addtionalMeta?.password))
hexToBytes(stringToHex(additionalMeta.NFTPGP_V1.password))
);
encryptedPrivateKey.version = Constants.ENC_TYPE_V4;
encryptedPrivateKey.preKey = '';
Expand Down
27 changes: 15 additions & 12 deletions packages/restapi/src/lib/user/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ export type CreateUserProps = {
account?: string;
signer?: SignerType;
version?: typeof Constants.ENC_TYPE_V1 | typeof Constants.ENC_TYPE_V3;
additionalMeta?: { password?: string };
additionalMeta?: {
NFTPGP_V1?: {
password: string;
};
};
progressHook?: (progress: ProgressHookType) => void;
};

export const create = async (options: CreateUserProps): Promise<IUser> => {
const passPrefix = '$0Pc'; //password prefix to ensure password validation
const {
env = Constants.ENV.PROD,
account = null,
signer = null,
version = Constants.ENC_TYPE_V3,
additionalMeta,
additionalMeta = {
NFTPGP_V1: {
password: passPrefix + generateRandomSecret(10),
},
},
progressHook,
} = options || {};

Expand All @@ -52,8 +61,8 @@ export const create = async (options: CreateUserProps): Promise<IUser> => {
if (!isValidETHAddress(address)) {
throw new Error(`Invalid address!`);
}
if (additionalMeta && additionalMeta.password) {
validatePssword(additionalMeta.password);
if (additionalMeta?.NFTPGP_V1?.password) {
validatePssword(additionalMeta.NFTPGP_V1.password);
}

const caip10: string = walletToPCAIP10(address);
Expand Down Expand Up @@ -100,23 +109,17 @@ export const create = async (options: CreateUserProps): Promise<IUser> => {
level: 'INFO',
});

let password: string;
if (!additionalMeta?.password) {
password = generateRandomSecret(10);
} else {
password = additionalMeta.password;
}
const encryptedPrivateKey: encryptedPrivateKeyType = await encryptPGPKey(
encryptionType,
keyPairs.privateKeyArmored,
wallet,
{ password: password }
additionalMeta
);

if (encryptionType === Constants.ENC_TYPE_V4) {
const encryptedPassword: encryptedPrivateKeyTypeV2 = await encryptPGPKey(
Constants.ENC_TYPE_V3,
password,
additionalMeta.NFTPGP_V1?.password as string,
wallet,
additionalMeta
);
Expand Down
8 changes: 6 additions & 2 deletions packages/restapi/src/lib/user/upgradeUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export type UpgradeUserProps = {
env?: ENV;
account?: string;
signer: SignerType;
additionalMeta?: { password?: string };
additionalMeta?: {
NFTPGP_V1?: {
password: string;
};
};
progressHook?: (progress: ProgressHookType) => void;
};
/**
Expand Down Expand Up @@ -124,7 +128,7 @@ export const upgrade = async (options: UpgradeUserProps): Promise<IUser> => {
if (encryptionType === Constants.ENC_TYPE_V4) {
const encryptedPassword: encryptedPrivateKeyTypeV2 = await encryptPGPKey(
Constants.ENC_TYPE_V3,
additionalMeta?.password as string,
additionalMeta?.NFTPGP_V1?.password as string,
wallet,
additionalMeta
);
Expand Down

0 comments on commit ee8ca02

Please sign in to comment.