While attempting to encrypt a private key in JavaScript using dotenv, I encountered an issue: TypeError - I was unable to read properties of undefined, specifically 'toHexString'

Welcome to encrypt.js

const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();

async function main() {
  const wallet = new ethers.Wallet(process.env.RRIVATE_KEY);
  const encryptedJsonKey = await wallet.encrypt(
    process.env.PRIVATE_KEY_PASSWORD,
    process.env.PRIVATE_KEY
  );
  console.log(encryptedJsonKey);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

This is the content of .env file

PRIVATE_KEY=0x3ed74d4599fbc880e1f2dd094bf1dde078e808779f4f888d4b59a6d07bee99cb
RPC_URL=http://127.0.0.1:7545
PRIVATE_KEY_PASSWORD=password

An error occurred with the message: "TypeError: Cannot read properties of undefined (reading 'toHexString')"

I tried searching online for a solution and someone suggested that I should keep all files in the same directory, which I have already done. Check here for directory When hovering over "PRIVATE_KEY_PASSWORD" or "PRIVATE_PASSWORD", it shows "string | undefined". Where could the issue be?


Upon further inspection, I discovered the mistake. There was a spelling error in the code: const wallet = new ethers.Wallet(process.env.RRIVATE_KEY); Simply change RRIVATE to PRIVATE. Apologies for the oversight.

Answer №1

It appears that the Hexadecimal number syntax is being used in this code snippet

PRIVATE_KEY=0x3ed74d4599fbc880e1f2dd094bf1dde078e808779f4f888d4b59a6d07bee99cb

To convert it to a string, you can encapsulate it like so

PRIVATE_KEY=`0x3ed74d4599fbc880e1f2dd094bf1dde078e808779f4f888d4b59a6d07bee99cb`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance your application by utilizing additional hooks in the Context API

I'm exploring API and react hooks and have a query related to dispatching API fetch to ContextAPI component. Is there a way to consolidate all useState hooks into a single ContextAPI component? The objective is code refactoring by breaking it down int ...

The controller in ng-controller returns an undefined error when loaded using RequireJs

I have encountered an issue while loading the Angular application using requirejs. The ng-controller written in main.html loads directly but ends up being undefined. app.js define(['sfongApp', 'ng-grid', 'angular-bootstrap'] ...

Identifying all elements with querySelectorAll and monitoring events with

I am currently attempting to modify this demonstration for page transitions triggered by clicking on links with a shared class. Despite following @ourmaninamsterdam's suggestion here, I am facing difficulties in getting the transitions to work. Can yo ...

The passport local strategy functions properly when tested with Postman, but encounters a "missing credentials" error when used with axios

I am currently working on creating a login system using passport and a local strategy. Strangely, when I attempt to send the request through axios it doesn't seem to work, although it functions properly with Postman. When using axios, I receive an er ...

"Can you explain the method by which reveal.js adjusts the size of

I have been exploring the resizing behavior of elements in reveal.js and trying to understand how they dynamically adjust. If you resize the page height, you can observe that the elements shrink up to a certain extent along with the page. However, when in ...

Does a .innerXML exist?

Is anyone knowledgeable in XML able to offer some assistance? I have a function that I use to parse XML data, and you can find the XML file I am working with here. function dialogXML(varName,url){ if (window.XMLHttpRequest){ r[varName]=new XML ...

What is the technique for creating multiple circles using CSS?

Creating multiple circles in CSS and linking them to my HTML file has posed a challenge. The code I have so far is: .circle{ height: 50px; width: 50px; background-color: red; } However, attempting to make another circle using the same 'circle' c ...

Utilizing d3.js to filter a dataset based on dropdown selection

I am working with a data set that contains country names as key attributes. When I select a country from a dropdown menu, I want to subset the dataset to display only values related to the selected country. However, my current code is only outputting [obje ...

React-Router failing to properly unmount components when the location parameter changes

I'm struggling with a frustrating issue - I have a simple app created using create-react-app and I'm utilizing React-Router. JSX: <Router> <Route path="/pages/:pageId" component={Page} /> </Router> Within the Page ...

Revamping a design using Mustache js

I've successfully implemented mustache js to render a template using data from an API, but now I face a challenge in updating the same template at a later time. Within my template, there is a list structured like this: template.html <div id="temp ...

When the HTML and PHP code keeps running, the JavaScript text on the page updates itself

I was experimenting with threading in different languages like Java, PHP, and JavaScript. I am aware that JavaScript is single-threaded and PHP lacks robust threading capabilities. Below is the code snippet I have been working on: <body> <p id= ...

Passing the index in React Native

I am currently developing a music app utilizing the react-native-track-player library. I have created three components named Clusters, Songlist, and Play. Understanding How the Screen Works The flow of components is as follows: Clusters component -> S ...

Using Bootstrap Modal with Jquery: Detected 2 elements on the DOM that share the same id, but thankfully all IDs are distinct

I have integrated Bootstrap 4 into my website, and I am facing an issue with having two different modal types on a single page. I am using Ajax to populate the content of these modals, and while the forms in each modal are distinct, they share some similar ...

Ways to merge several getServerSideProps functions

Within my project, I have two important pages: index.js and other.js. In index.js, there exists a crucial method known as getServerSideProps: export async function getServerSideProps(context) { //code here } The challenge arises when I realize that I ...

My React/Redux App is experiencing difficulties as the state fails to load

Currently, I am working on a project based on the Redux Documentation page. Instead of uploading it directly to Stack Overflow, I decided to share my progress on GitHub: https://github.com/davidreke/reduxDocumentationProject I have completed the instructi ...

Database not receiving input data from AngularJS form submission

Having some trouble saving form data to the database using angularjs. Not sure what I'm doing wrong. Here's my HTML form: <form id="challenge_form" class="row" > <input type="text" placeholder="Challenge Name" ng-model="ch ...

Struggling to receive accurate data from every statement

I have an object (known as allUserInfo) that looks like this: Object { available_client_ids: Array[2] 0: "demo" 1: "4532t78" available_client_names: Array[2] 0: "Demo" 1: "Bobs Bakery" email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Guide to capturing mouse drag coordinates using JavaScript within a JSP page

My task is to record the starting coordinates (x,y) and ending coordinates (x,y) of a mouse event. For example, on an HTML page with an image, users should be able to select a specific area by dragging the mouse. Therefore, I need to capture the coordinat ...

Adding the <br> tag in a Bootstrap modal: a simple guide

Hi everyone! I am currently working with a bootstrap modal to create a login functionality within the modal itself. There is a data validation function in place. However, when an error occurs, the word 'br' displays directly in the HTML instead ...

React Material UI Select component is failing to recognize scrolling event

Having some difficulty understanding how to detect a scroll event with a Select component using Material-UI. The Select has MenuProps={...}, and I want to listen for the scroll event inside it. I've tried putting onScroll within MenuProps={...}, but ...