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
44 changes: 44 additions & 0 deletions src/background/handlers/account-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,50 @@
}
},

// Validate private key without importing
async validatePrivateKey(data: {
privateKey: string;
chain: ChainType;
}): Promise<{ isValid: boolean; address: string }> {
try {
if (!data.privateKey || typeof data.privateKey !== 'string') {
return { isValid: false, address: '' };
}

let walletKeys: any;

Check warning on line 49 in src/background/handlers/account-handler.ts

View workflow job for this annotation

GitHub Actions / code-quality

Unexpected any. Specify a different type
let isValid = false;

// Validate private key based on chain type
if (data.chain === 'eip155') {
isValid = evmWalletKeyUtils.isValidPrivateKey(data.privateKey);
if (isValid) {
walletKeys = evmWalletKeyUtils.fromPrivateKey(data.privateKey);
}
} else if (data.chain === 'solana') {
isValid = solanaWalletKeyUtils.isValidPrivateKey(data.privateKey);
if (isValid) {
walletKeys = solanaWalletKeyUtils.fromPrivateKey(data.privateKey);
}
} else if (data.chain === 'sui') {
isValid = suiWalletKeyUtils.isValidPrivateKey(data.privateKey);
if (isValid) {
walletKeys = suiWalletKeyUtils.fromPrivateKey(data.privateKey);
}
} else {
return { isValid: false, address: '' };
}

if (isValid && walletKeys) {
return { isValid: true, address: walletKeys.address };
} else {
return { isValid: false, address: '' };
}
} catch (error) {
console.error('Private key validation error:', error);
return { isValid: false, address: '' };
}
},

async isPrivateKeyAlreadyImported(privateKey: string): Promise<boolean> {
try {
// Validate input
Expand Down
28 changes: 21 additions & 7 deletions src/background/handlers/evm-rpc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ const createSuccessResponse = (data: any): MessageResponse => {
};
};

// Helper function to parse chainId from string to number
const parseChainId = (chainId: string): number => {
// Handle both hex (0x...) and decimal string formats
return chainId.startsWith('0x')
? parseInt(chainId, 16)
: parseInt(chainId, 10);
};

// Helper function to create error response
const createErrorResponse = (error: string, code?: number): MessageResponse => {
return {
Expand All @@ -104,7 +112,7 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
targetChainId = parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down Expand Up @@ -144,7 +152,7 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
targetChainId = parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down Expand Up @@ -177,7 +185,7 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
targetChainId = parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down Expand Up @@ -219,7 +227,10 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
// Handle both hex (0x...) and decimal string formats
targetChainId = chainId.startsWith('0x')
? parseInt(chainId, 16)
: parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down Expand Up @@ -252,7 +263,10 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
// Handle both hex (0x...) and decimal string formats
targetChainId = chainId.startsWith('0x')
? parseInt(chainId, 16)
: parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down Expand Up @@ -287,7 +301,7 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
targetChainId = parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down Expand Up @@ -326,7 +340,7 @@ export const evmRpcHandler = {
// Convert chainId to number if it's a string
let targetChainId: number;
if (typeof chainId === 'string') {
targetChainId = parseInt(chainId, 10);
targetChainId = parseChainId(chainId);
if (isNaN(targetChainId)) {
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
}
Expand Down
3 changes: 3 additions & 0 deletions src/background/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export class MessageHandler {
data.accountName
);
break;
case 'VALIDATE_PRIVATE_KEY':
result = await accountHandler.validatePrivateKey(data);
break;
case 'IS_WATCH_ONLY_ADDRESS_EXISTS':
result = await accountHandler.isWalletAddressExists(data.address);
break;
Expand Down
Loading
Loading