Skip to content

Seed #18545

Closed
Closed
Seed#18545
@Luch2

Description

@Luch2

const bip39 = require('bip39');
const hdkey = require('ethereumjs-wallet/hdkey');
const ethUtil = require('ethereumjs-util');
const axios = require('axios');

/**

  • Generates and prints random 12-word seed phrases for Trust Wallet with a balance of more than $1000.

  • It also checks the balance of the generated wallet address.
    */
    async function generateSeedPhraseWithBalance() {
    try {
    // Generate a random 12-word seed phrase
    const mnemonic = bip39.generateMnemonic();

     // Derive the wallet address from the seed phrase
     const seed = await bip39.mnemonicToSeed(mnemonic);
     const hdWallet = hdkey.fromMasterSeed(seed);
     const wallet = hdWallet.derivePath("m/44'/60'/0'/0/0").getWallet();
     const address = ethUtil.toChecksumAddress(wallet.getAddressString());
    
     // Check the balance of the wallet address
     const balance = await getBalance(address);
    
     // Print the seed phrase and balance
     console.log("Seed Phrase:", mnemonic);
     console.log("Wallet Address:", address);
     console.log("Balance:", balance);
    

    } catch (error) {
    console.error("Error:", error.message);
    }
    }

/**

  • Retrieves the balance of a given Ethereum wallet address.
  • @param {string} address - The Ethereum wallet address.
  • @returns {number} The balance of the wallet address in Ether.
    */
    async function getBalance(address) {
    try {
    const response = await axios.get(https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest);
    const balance = response.data.result / 1e18; // Convert balance from Wei to Ether
    return balance;
    } catch (error) {
    console.error("Error:", error.message);
    return 0;
    }
    }

/**

  • Unit Tests for generateSeedPhraseWithBalance Function
    */

/**

  • Positive Case

  • This test verifies that the generateSeedPhraseWithBalance function

  • generates a seed phrase and retrieves the balance for a wallet address

  • with a balance of more than $1000.
    */
    describe('Positive Case', () => {

    it('Should generate a seed phrase and retrieve balance', async () => {
    // Mocking the getBalance function to return a balance of $2000
    const mockGetBalance = jest.fn().mockResolvedValue(2000);
    jest.mock('./path/to/ethereumjs-util', () => ({
    ...jest.requireActual('./path/to/ethereumjs-util'),
    getBalance: mockGetBalance
    }));

     // Here we're calling the generateSeedPhraseWithBalance function
     await generateSeedPhraseWithBalance();
    
     // We expect the mockGetBalance function to be called once
     expect(mockGetBalance).toHaveBeenCalledTimes(1);
    

    });

});

/**

  • Negative Case

  • This test verifies that the generateSeedPhraseWithBalance function

  • handles errors properly when retrieving the balance.
    */
    describe('Negative Case', () => {

    it('Should handle error when retrieving balance', async () => {
    // Mocking the getBalance function to throw an error
    const mockGetBalance = jest.fn().mockRejectedValue(new Error("Failed to retrieve balance"));
    jest.mock('./path/to/ethereumjs-util', () => ({
    ...jest.requireActual('./path/to/ethereumjs-util'),
    getBalance: mockGetBalance
    }));

     // Here we're calling the generateSeedPhraseWithBalance function
     await generateSeedPhraseWithBalance();
    
     // We expect the mockGetBalance function to be called once
     expect(mockGetBalance).toHaveBeenCalledTimes(1);
    

    });

});

// Usage Example for generateSeedPhraseWithBalance Function

generateSeedPhraseWithBalance();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions