Skip to content
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
5,601 changes: 0 additions & 5,601 deletions packages/restapi/package-lock.json

This file was deleted.

29 changes: 27 additions & 2 deletions packages/restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"simple-peer": "^9.11.1",
"socket.io-client": "^4.7.2",
"video-stream-merger": "^4.0.1",
"viem": "^1.20.3"
"viem": "^2.28.0"
},
"scripts": {
"test": "TS_NODE_PROJECT='./tsconfig.mocha.json' NODE_OPTIONS='--loader ts-node/esm' DOTENV_CONFIG_PATH='./tests/.env' mocha -r ts-node/register -r dotenv/config 'tests/**/*.test.ts' --timeout 1200000 --require tests/root.ts --serial"
Expand All @@ -41,6 +41,31 @@
"mocha": "^10.2.0",
"mocha-typescript": "^1.1.17",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
"typescript": "^5.0.2",
"undici": "^6.21.2",
"web-streams-polyfill": "^4.1.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to dev-dependences

"@ethersproject/providers": "^5.7.2",
"@lit-protocol/auth-browser": "^7.0.5",
"@lit-protocol/auth-helpers": "^7.0.5",
"@lit-protocol/constants": "^7.0.5",
"@lit-protocol/contracts-sdk": "^7.0.5",
"@lit-protocol/lit-auth-client": "^7.0.5",
"@lit-protocol/lit-node-client": "^7.0.5",
"@lit-protocol/pkp-ethers": "^7.0.5",
"@lit-protocol/types": "^7.0.5",
"@lit-protocol/wrapped-keys": "^7.0.3",
"@pushprotocol/restapi": "^1.7.32-exp.1",
"@zerodev/hooks": "^5.3.0",
"@zerodev/intent": "^0.0.7",
"@zerodev/multi-chain-ecdsa-validator": "^5.4.2",
"@zerodev/permissions": "^5.5.0",
"@zerodev/remote-signer": "^5.3.0",
"@zerodev/session-key": "^5.5.0",
"@zerodev/weighted-ecdsa-validator": "^5.4.0",
"dotenv": "^16.3.1",
"ethers": "^5.7.2",
"isomorphic-fetch": "^3.0.0",
"@zerodev/ecdsa-validator": "^5.4.7",
"@zerodev/sdk": "^5.4.29"
}
}
4 changes: 3 additions & 1 deletion packages/restapi/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum ENCRYPTION_TYPE {
PGP_V2 = 'aes256GcmHkdfSha256',
PGP_V3 = 'eip191-aes256-gcm-hkdf-sha256',
NFTPGP_V1 = 'pgpv1:nft',
SCWPGP_V1 = 'pgpv1:scw',
}

/**
Expand Down Expand Up @@ -67,13 +68,14 @@ const Constants = {
DEV_CHAIN_ID: 99999,
NON_ETH_CHAINS: [
137, 80002, 56, 97, 10, 11155420, 2442, 1101, 421614, 42161, 122, 123,
80085,59141, 59144, 111557560, 7560, 84532, 8453,
80085, 59141, 59144, 111557560, 7560, 84532, 8453,
],
ETH_CHAINS: [1, 11155111],
ENC_TYPE_V1: 'x25519-xsalsa20-poly1305',
ENC_TYPE_V2: 'aes256GcmHkdfSha256',
ENC_TYPE_V3: 'eip191-aes256-gcm-hkdf-sha256',
ENC_TYPE_V4: 'pgpv1:nft',
ENC_TYPE_V5: 'pgpv1:scw',
ALPHA_FEATURES,
};

Expand Down
10 changes: 7 additions & 3 deletions packages/restapi/src/lib/helpers/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function getFallbackETHCAIPAddress(env: ENV, address: string) {
* throw error!
*/
export async function getCAIPAddress(env: ENV, address: string, msg?: string) {
if (isValidNFTCAIP(address)) {
if (isValidNFTCAIP(address) || isValidSCWCAIP(address)) {
return await convertToValidDID(address, env);
}
if (validateCAIP(address)) {
Expand Down Expand Up @@ -245,14 +245,18 @@ export const getCAIPWithChainId = (

// P = Partial CAIP
export const walletToPCAIP10 = (account: string): string => {
if (isValidNFTCAIP(account) || account.includes('eip155:')) {
if (
isValidNFTCAIP(account) ||
isValidSCWCAIP(account) ||
account.includes('eip155:')
) {
return account;
}
return 'eip155:' + account;
};

export const pCAIP10ToWallet = (wallet: string): string => {
if (isValidNFTCAIP(wallet)) return wallet;
if (isValidNFTCAIP(wallet) || isValidSCWCAIP(wallet)) return wallet;
wallet = wallet.replace('eip155:', '');
return wallet;
};
38 changes: 30 additions & 8 deletions packages/restapi/src/lib/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type decryptPgpKeyProps = {
NFTPGP_V1?: {
password: string;
};
SCWPGP_V1?: {
password: string;
};
};
progressHook?: (progress: ProgressHookType) => void;
};
Expand Down Expand Up @@ -213,10 +216,13 @@ export const decryptPGPKey = async (options: decryptPgpKeyProps) => {
privateKey = dec.decode(encodedPrivateKey);
break;
}
case Constants.ENC_TYPE_V4: {
case Constants.ENC_TYPE_V4:
case Constants.ENC_TYPE_V5: {
let password: string | null = null;
if (additionalMeta?.NFTPGP_V1) {
if (additionalMeta?.NFTPGP_V1?.password) {
password = additionalMeta.NFTPGP_V1.password;
} else if (additionalMeta?.SCWPGP_V1?.password) {
password = additionalMeta.SCWPGP_V1.password;
} else {
if (!wallet?.signer) {
throw new Error(
Expand All @@ -243,7 +249,12 @@ export const decryptPGPKey = async (options: decryptPgpKeyProps) => {
}

// try key upgradation
if (signer && toUpgrade && encryptionType !== Constants.ENC_TYPE_V4) {
if (
signer &&
toUpgrade &&
encryptionType !== Constants.ENC_TYPE_V4 &&
encryptionType !== Constants.ENC_TYPE_V5
) {
try {
await upgrade({ env, account: address, signer, progressHook });
} catch (err) {
Expand Down Expand Up @@ -380,6 +391,9 @@ export const encryptPGPKey = async (
NFTPGP_V1?: {
password: string;
};
SCWPGP_V1?: {
password: string;
};
}
): Promise<encryptedPrivateKeyType> => {
let encryptedPrivateKey: encryptedPrivateKeyType;
Expand Down Expand Up @@ -437,17 +451,24 @@ export const encryptPGPKey = async (
encryptedPrivateKey.preKey = input;
break;
}
case Constants.ENC_TYPE_V4: {
if (!additionalMeta?.NFTPGP_V1?.password) {
case Constants.ENC_TYPE_V4:
case Constants.ENC_TYPE_V5: {
if (
!additionalMeta?.NFTPGP_V1?.password &&
!additionalMeta?.SCWPGP_V1?.password
) {
throw new Error('Password is required!');
}
const enc = new TextEncoder();
const encodedPrivateKey = enc.encode(privateKey);
const password =
additionalMeta?.NFTPGP_V1?.password ||
additionalMeta?.SCWPGP_V1?.password;
encryptedPrivateKey = await encryptV2(
encodedPrivateKey,
hexToBytes(stringToHex(additionalMeta.NFTPGP_V1.password))
hexToBytes(stringToHex(password as string))
);
encryptedPrivateKey.version = Constants.ENC_TYPE_V4;
encryptedPrivateKey.version = encryptionType;
encryptedPrivateKey.preKey = '';
break;
}
Expand All @@ -470,7 +491,8 @@ export const preparePGPPublicKey = async (
}
case Constants.ENC_TYPE_V2:
case Constants.ENC_TYPE_V3:
case Constants.ENC_TYPE_V4: {
case Constants.ENC_TYPE_V4:
case Constants.ENC_TYPE_V5: {
const verificationProof = 'DEPRECATED';
// TODO - Change JSON Structure to string ie equivalent to ENC_TYPE_V1 ( would be done after PUSH Node changes )
chatPublicKey = JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ export class PushNotificationBaseClass {
coreContract = getContract({
abi: config.ABIS.CORE,
address: config.CORE_CONFIG[env].EPNS_CORE_CONTRACT as `0x${string}`,
publicClient: client,
walletClient: signer as unknown as WalletClient,
client: { public: client },
});
} else {
coreContract = new ethers.Contract(
Expand Down Expand Up @@ -323,8 +322,7 @@ export class PushNotificationBaseClass {
contract = getContract({
abi: contractABI,
address: contractAddress as `0x${string}`,
publicClient: client,
walletClient: this.signer as unknown as WalletClient,
client: { public: client },
});
} else {
contract = new ethers.Contract(
Expand Down
18 changes: 15 additions & 3 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { ALPHA_FEATURE_CONFIG } from '../config';
import { Space } from './space';
import { Video } from './video';
import { isValidNFTCAIP, walletToPCAIP10 } from '../helpers';
import { isValidNFTCAIP, isValidSCWCAIP, walletToPCAIP10 } from '../helpers';
import { LRUCache } from 'lru-cache';
import { cache } from '../helpers/cache';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -267,7 +267,8 @@ export class PushAPI {
}
} catch (error) {
const decryptionError =
'Error decrypting PGP private key ...swiching to Guest mode';
'Error decrypting PGP private key ...swiching to Guest mode ' +
('\n ' + error);
initializationErrors.push({
type: 'ERROR',
message: decryptionError,
Expand All @@ -282,6 +283,14 @@ export class PushAPI {
message: nftDecryptionError,
});
console.warn(nftDecryptionError);
} else if (isValidSCWCAIP(derivedAccount)) {
const nftDecryptionError =
'Smart Contract Wallet Account Detected. If this SCW was recently transferred to you, please ensure you have received the correct password from the previous owner. Alternatively, you can reinitialize for a fresh start. Please be aware that reinitialization will result in the loss of all previous account data.';
initializationErrors.push({
type: 'WARN',
message: nftDecryptionError,
});
console.warn(nftDecryptionError);
}
readMode = true;
}
Expand Down Expand Up @@ -312,7 +321,10 @@ export class PushAPI {
* @notice - All data will be lost after reinitialization
*/
async reinitialize(options: {
versionMeta: { NFTPGP_V1: { password: string } };
versionMeta: {
NFTPGP_V1?: { password: string };
SCWPGP_V1?: { password: string };
};
}): Promise<void> {
const newUser = await PUSH_USER.create({
env: this.env,
Expand Down
26 changes: 19 additions & 7 deletions packages/restapi/src/lib/pushapi/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@ export class Encryption {
const userInfo = await this.userInstance.info();
let decryptedPassword;
if (this.signer) {
const encryptedPassword = JSON.stringify(
JSON.parse(userInfo.encryptedPrivateKey).encryptedPassword
);

// Check if account starts with "scw" to determine encryption type
const additionalMeta = this.account.toLowerCase().startsWith('scw')
? {
SCWPGP_V1: {
encryptedPassword: encryptedPassword,
},
}
: {
NFTPGP_V1: {
encryptedPassword: encryptedPassword,
},
};

decryptedPassword = await PUSH_USER.decryptAuth({
account: this.account,
env: this.env,
signer: this.signer,
progressHook: this.progressHook,
additionalMeta: {
NFTPGP_V1: {
encryptedPassword: JSON.stringify(
JSON.parse(userInfo.encryptedPrivateKey).encryptedPassword
),
},
},
additionalMeta,
});
}

Expand All @@ -51,6 +62,7 @@ export class Encryption {
options?: {
versionMeta?: {
NFTPGP_V1?: { password: string };
SCWPGP_V1?: { password: string };
};
}
) {
Expand Down
5 changes: 4 additions & 1 deletion packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export interface PushAPIInitializeProps {
progressHook?: (progress: ProgressHookType) => void;
account?: string | null;
version?: typeof Constants.ENC_TYPE_V1 | typeof Constants.ENC_TYPE_V3;
versionMeta?: { NFTPGP_V1?: { password: string } };
versionMeta?: {
NFTPGP_V1?: { password: string };
SCWPGP_V1?: { password: string };
};
autoUpgrade?: boolean;
origin?: string;
alpha?: {
Expand Down
9 changes: 6 additions & 3 deletions packages/restapi/src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// for namespace TYPES
/* eslint-disable @typescript-eslint/no-namespace */
import { ResolvedConfig } from 'viem';
import { Account } from 'viem';
import { Signer } from 'ethers';
// import { ResolvedConfig } from 'viem'; // ResolvedConfig is not exported in viem v2+
import { Chain } from 'viem/chains';
import {
ADDITIONAL_META_TYPE,
IDENTITY_TYPE,
Expand All @@ -21,7 +24,7 @@ import {
export * from './messageTypes';
export * from './videoTypes';

export type Env = typeof ENV[keyof typeof ENV];
export type Env = (typeof ENV)[keyof typeof ENV];

// the type for the the response of the input data to be parsed
export type ApiNotificationType = {
Expand Down Expand Up @@ -729,7 +732,7 @@ export type TypedDataField = {
export type TypedDataDomain = {
chainId?: number | undefined;
name?: string | undefined;
salt?: ResolvedConfig['BytesType']['outputs'] | undefined;
salt?: `0x${string}` | undefined;
verifyingContract?: string | undefined;
version?: string | undefined;
};
Expand Down
15 changes: 14 additions & 1 deletion packages/restapi/src/lib/user/auth.updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum ENCRYPTION_TYPE_VERSION {
'aes256GcmHkdfSha256' = 'PGP_V2',
'eip191-aes256-gcm-hkdf-sha256' = 'PGP_V3',
'pgpv1:nft' = 'NFTPGP_V1',
'pgpv1:scw' = 'SCWPGP_V1',
}

export type AuthUpdateProps = {
Expand All @@ -40,6 +41,9 @@ export type AuthUpdateProps = {
NFTPGP_V1?: {
password: string; //new nft profile password
};
SCWPGP_V1?: {
password: string; //new scw profile password
};
};
progressHook?: (progress: ProgressHookType) => void;
};
Expand All @@ -64,7 +68,8 @@ export const authUpdate = async (options: AuthUpdateProps): Promise<IUser> => {
const address = await getAccountAddress(wallet);

const updatingCreds =
pgpEncryptionVersion === Constants.ENCRYPTION_TYPE.NFTPGP_V1
pgpEncryptionVersion === Constants.ENCRYPTION_TYPE.NFTPGP_V1 ||
pgpEncryptionVersion === Constants.ENCRYPTION_TYPE.SCWPGP_V1
? true
: false;

Expand Down Expand Up @@ -115,6 +120,14 @@ export const authUpdate = async (options: AuthUpdateProps): Promise<IUser> => {
additionalMeta
);
encryptedPgpPrivateKey.encryptedPassword = encryptedPassword;
} else if (pgpEncryptionVersion === ENCRYPTION_TYPE.SCWPGP_V1) {
const encryptedPassword: encryptedPrivateKeyTypeV2 = await encryptPGPKey(
ENCRYPTION_TYPE.PGP_V3,
additionalMeta?.SCWPGP_V1?.password as string,
wallet,
additionalMeta
);
encryptedPgpPrivateKey.encryptedPassword = encryptedPassword;
}

// Report Progress
Expand Down
Loading
Loading