Skip to content

Commit

Permalink
Create walletBackup.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent 66c6e8d commit 6386f82
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/wallet/walletBackup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// walletBackup.js

import { ethers } from 'ethers';
import fs from 'fs';
import path from 'path';

class WalletBackup {
constructor() {
this.walletFilePath = path.join(__dirname, 'walletBackup.json');
}

// Create a new wallet and generate a mnemonic phrase
createWallet() {
const wallet = ethers.Wallet.createRandom();
const mnemonic = wallet.mnemonic.phrase;
const privateKey = wallet.privateKey;

// Save the wallet backup
this.saveBackup(mnemonic, privateKey);
console.log('Wallet created and backed up successfully.');
return { mnemonic, privateKey };
}

// Save wallet backup to a file
saveBackup(mnemonic, privateKey) {
const backupData = {
mnemonic,
privateKey,
timestamp: new Date().toISOString()
};
fs.writeFileSync(this.walletFilePath, JSON.stringify(backupData, null, 2));
}

// Recover wallet from mnemonic phrase
recoverWallet(mnemonic) {
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
console.log('Wallet recovered successfully.');
return wallet;
}

// Load wallet backup from file
loadBackup() {
if (fs.existsSync(this.walletFilePath)) {
const backupData = JSON.parse(fs.readFileSync(this.walletFilePath));
return backupData;
} else {
throw new Error('No wallet backup found.');
}
}

// Example usage
exampleUsage() {
// Create a new wallet
const { mnemonic, privateKey } = this.createWallet();
console.log('Mnemonic:', mnemonic);
console.log('Private Key:', privateKey);

// Load backup
const backup = this.loadBackup();
console.log('Loaded Backup:', backup);

// Recover wallet
const recoveredWallet = this.recoverWallet(backup.mnemonic);
console.log('Recovered Wallet Address:', recoveredWallet.address);
}
}

// Example usage
const walletBackup = new WalletBackup();
walletBackup.exampleUsage();

export default WalletBackup;

0 comments on commit 6386f82

Please sign in to comment.