Skip to content

Commit 79007fa

Browse files
authored
Merge pull request #36 from terrancrypt/fix/send
Fix/send
2 parents e3b401b + 59358ae commit 79007fa

12 files changed

Lines changed: 967 additions & 612 deletions

File tree

src/background/handlers/account-handler.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,50 @@ export const accountHandler = {
3636
}
3737
},
3838

39+
// Validate private key without importing
40+
async validatePrivateKey(data: {
41+
privateKey: string;
42+
chain: ChainType;
43+
}): Promise<{ isValid: boolean; address: string }> {
44+
try {
45+
if (!data.privateKey || typeof data.privateKey !== 'string') {
46+
return { isValid: false, address: '' };
47+
}
48+
49+
let walletKeys: any;
50+
let isValid = false;
51+
52+
// Validate private key based on chain type
53+
if (data.chain === 'eip155') {
54+
isValid = evmWalletKeyUtils.isValidPrivateKey(data.privateKey);
55+
if (isValid) {
56+
walletKeys = evmWalletKeyUtils.fromPrivateKey(data.privateKey);
57+
}
58+
} else if (data.chain === 'solana') {
59+
isValid = solanaWalletKeyUtils.isValidPrivateKey(data.privateKey);
60+
if (isValid) {
61+
walletKeys = solanaWalletKeyUtils.fromPrivateKey(data.privateKey);
62+
}
63+
} else if (data.chain === 'sui') {
64+
isValid = suiWalletKeyUtils.isValidPrivateKey(data.privateKey);
65+
if (isValid) {
66+
walletKeys = suiWalletKeyUtils.fromPrivateKey(data.privateKey);
67+
}
68+
} else {
69+
return { isValid: false, address: '' };
70+
}
71+
72+
if (isValid && walletKeys) {
73+
return { isValid: true, address: walletKeys.address };
74+
} else {
75+
return { isValid: false, address: '' };
76+
}
77+
} catch (error) {
78+
console.error('Private key validation error:', error);
79+
return { isValid: false, address: '' };
80+
}
81+
},
82+
3983
async isPrivateKeyAlreadyImported(privateKey: string): Promise<boolean> {
4084
try {
4185
// Validate input

src/background/handlers/evm-rpc-handler.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ const createSuccessResponse = (data: any): MessageResponse => {
7878
};
7979
};
8080

81+
// Helper function to parse chainId from string to number
82+
const parseChainId = (chainId: string): number => {
83+
// Handle both hex (0x...) and decimal string formats
84+
return chainId.startsWith('0x')
85+
? parseInt(chainId, 16)
86+
: parseInt(chainId, 10);
87+
};
88+
8189
// Helper function to create error response
8290
const createErrorResponse = (error: string, code?: number): MessageResponse => {
8391
return {
@@ -104,7 +112,7 @@ export const evmRpcHandler = {
104112
// Convert chainId to number if it's a string
105113
let targetChainId: number;
106114
if (typeof chainId === 'string') {
107-
targetChainId = parseInt(chainId, 10);
115+
targetChainId = parseChainId(chainId);
108116
if (isNaN(targetChainId)) {
109117
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
110118
}
@@ -144,7 +152,7 @@ export const evmRpcHandler = {
144152
// Convert chainId to number if it's a string
145153
let targetChainId: number;
146154
if (typeof chainId === 'string') {
147-
targetChainId = parseInt(chainId, 10);
155+
targetChainId = parseChainId(chainId);
148156
if (isNaN(targetChainId)) {
149157
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
150158
}
@@ -177,7 +185,7 @@ export const evmRpcHandler = {
177185
// Convert chainId to number if it's a string
178186
let targetChainId: number;
179187
if (typeof chainId === 'string') {
180-
targetChainId = parseInt(chainId, 10);
188+
targetChainId = parseChainId(chainId);
181189
if (isNaN(targetChainId)) {
182190
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
183191
}
@@ -219,7 +227,10 @@ export const evmRpcHandler = {
219227
// Convert chainId to number if it's a string
220228
let targetChainId: number;
221229
if (typeof chainId === 'string') {
222-
targetChainId = parseInt(chainId, 10);
230+
// Handle both hex (0x...) and decimal string formats
231+
targetChainId = chainId.startsWith('0x')
232+
? parseInt(chainId, 16)
233+
: parseChainId(chainId);
223234
if (isNaN(targetChainId)) {
224235
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
225236
}
@@ -252,7 +263,10 @@ export const evmRpcHandler = {
252263
// Convert chainId to number if it's a string
253264
let targetChainId: number;
254265
if (typeof chainId === 'string') {
255-
targetChainId = parseInt(chainId, 10);
266+
// Handle both hex (0x...) and decimal string formats
267+
targetChainId = chainId.startsWith('0x')
268+
? parseInt(chainId, 16)
269+
: parseChainId(chainId);
256270
if (isNaN(targetChainId)) {
257271
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
258272
}
@@ -287,7 +301,7 @@ export const evmRpcHandler = {
287301
// Convert chainId to number if it's a string
288302
let targetChainId: number;
289303
if (typeof chainId === 'string') {
290-
targetChainId = parseInt(chainId, 10);
304+
targetChainId = parseChainId(chainId);
291305
if (isNaN(targetChainId)) {
292306
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
293307
}
@@ -326,7 +340,7 @@ export const evmRpcHandler = {
326340
// Convert chainId to number if it's a string
327341
let targetChainId: number;
328342
if (typeof chainId === 'string') {
329-
targetChainId = parseInt(chainId, 10);
343+
targetChainId = parseChainId(chainId);
330344
if (isNaN(targetChainId)) {
331345
return createErrorResponse(`Invalid chainId: ${chainId}`, 4001);
332346
}

src/background/message-handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export class MessageHandler {
125125
data.accountName
126126
);
127127
break;
128+
case 'VALIDATE_PRIVATE_KEY':
129+
result = await accountHandler.validatePrivateKey(data);
130+
break;
128131
case 'IS_WATCH_ONLY_ADDRESS_EXISTS':
129132
result = await accountHandler.isWalletAddressExists(data.address);
130133
break;

0 commit comments

Comments
 (0)