Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated wallet.ts with error handling and improved clarity #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 18 additions & 13 deletions helpers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import { mnemonicToSeedSync } from 'bip39';
import { derivePath } from 'ed25519-hd-key';

export function getWallet(wallet: string): Keypair {
// most likely someone pasted the private key in binary format
if (wallet.startsWith('[')) {
const raw = new Uint8Array(JSON.parse(wallet))
return Keypair.fromSecretKey(raw);
}
try {
// Check if the wallet is in JSON format (most likely a private key in binary format)
if (wallet.startsWith('[')) {
const raw = new Uint8Array(JSON.parse(wallet));
return Keypair.fromSecretKey(raw);
}

// most likely someone pasted mnemonic
if (wallet.split(' ').length > 1) {
const seed = mnemonicToSeedSync(wallet, '');
const path = `m/44'/501'/0'/0'`; // we assume it's first path
return Keypair.fromSeed(derivePath(path, seed.toString('hex')).key);
}
// Check if the wallet is a mnemonic
if (wallet.split(' ').length > 1) {
const seed = mnemonicToSeedSync(wallet, '');
const path = `m/44'/501'/0'/0'`; // Using the first path for derivation
return Keypair.fromSeed(derivePath(path, seed.toString('hex')).key);
}

// most likely someone pasted base58 encoded private key
return Keypair.fromSecretKey(bs58.decode(wallet));
// Assume the wallet is a base58 encoded private key
return Keypair.fromSecretKey(bs58.decode(wallet));
} catch (error) {
console.error('Failed to create wallet:', error);
throw new Error('Invalid wallet input format');
}
}