DEV Community

Cover image for How to Generate and Store Ethereum Wallets in AWS Secrets Manager
Rodrigo Burgos
Rodrigo Burgos

Posted on

How to Generate and Store Ethereum Wallets in AWS Secrets Manager

Before we begin, ensure you have the following:

  • Node.js (v16 or later recommended)
  • AWS Account with Secrets Manager permissions
  • IAM User or Role with access to AWS Secrets Manager
  • AWS SDK for JavaScript (@aws-sdk/client-secrets-manager)
  • ethers.js for Ethereum wallet creation

Step 1: Setting Up the Project

First, create a new Node.js project and install the necessary dependencies:

mkdir eth-wallet-secrets && cd eth-wallet-secrets
npm init -y
npm install ethers dotenv @aws-sdk/client-secrets-manager
Enter fullscreen mode Exit fullscreen mode

Then, create a .env file to store your AWS configuration:

Step 2: Writing the Wallet Generation Script

import { Wallet } from "ethers";
import { SecretsManagerClient, CreateSecretCommand } from "@aws-sdk/client-secrets-manager";
import dotenv from "dotenv";

dotenv.config();

const REGION = process.env.AWS_REGION || "us-east-1";
const SECRET_NAME = process.env.AWS_SECRET_NAME || "BlockchainWallet";
const client = new SecretsManagerClient({ region: REGION });

async function generateAndStoreWallet() {
    try {
        // Generate a new Ethereum wallet
        const wallet = Wallet.createRandom();

        const walletData = {
            PUBLIC_ADDRESS: wallet.address,
            PRIVATE_KEY: wallet.privateKey,
            MNEMONIC: wallet.mnemonic?.phrase,
        };

        const command = new CreateSecretCommand({
            Name: SECRET_NAME,
            SecretString: JSON.stringify(walletData),
        });

        const response = await client.send(command);
        console.log("Wallet generated and stored successfully:", response);
    } catch (error) {
        console.error("Error generating or storing the wallet:", error);
    }
}

generateAndStoreWallet();
Enter fullscreen mode Exit fullscreen mode

The result can be see like this in the AWS console:

Image description

Top comments (0)