Typescript encountered an error while attempting to generate an AptosAccount due to an authentication key issue (INVALID

Currently, my goal is to establish a token collection through the use of the Aptos Typescript SDK.

const account = new AptosAccount(Uint8Array.from(Buffer.from(PRIVATE_KEY)), ACCOUNT_ADDR);

await tokenClient.createCollection(
        account,
        "A test collection 1",
        "A test collection",
        "https://google.com",
    );

However, I am encountering the following error:

ApiError2: {"message":"Invalid transaction: Type: Validation Code: INVALID_AUTH_KEY","error_code":"vm_error","vm_error_code":2}

I am unsure of what mistake I may be making. Any insights on this issue?

I attempted to follow the official Aptos example but with a twist - rather than creating a new account, I intend to utilize an existing account with funds already allocated.

Answer №1

If you possess a private key in the form of a hexadecimal string, the following code snippet demonstrates how you can handle it:

import { AptosAccount, HexString } from "aptos";

const privateKeyHex = "0x57f6046ff85febd0da6d4e5ef4e2f1b7f9c78f1619f8b40ecf738fad65afcd0";
const privateKeyBytes = HexString.ensure(privateKeyHex).toUint8Array();
const account = new AptosAccount(privateKeyBytes);

Answer №2

After encountering the same issue, I discovered that the root cause was a mismatch between the address and private key. It turns out that my private_key was not in the correct ed25519 format.

To resolve this problem, make sure your keys are generated using the ed25519 curve. You can create your address from the key by using a library like bip_utils. In my case, I used bip_utils to generate a bip_private_key (compatible with Near protocol, which also uses ed25519) and then converted it as follows:

private_key = ed25519.PrivateKey.from_hex(bip_private_key)

Answer №3

In addition to Daniel's answer, I wanted to provide more information on how to retrieve the private key after creating a wallet and then using it:

import { AptosAccount } from "aptos";

const wallet = new AptosAccount();
const privateKeyHex = wallet.toPrivateKeyObject().privateKeyHex;

// ...

const privateKeyBytes = HexString.ensure(privateKeyHex).toUint8Array();
const account = new AptosAccount(privateKeyBytes);

Answer №4

Learn how to implement this using TS SDK v2:

// To start, you'll need to generate a private key instance for either the Ed25519 or Secp256k1 scheme.
const privateKeyEd = new Ed25519PrivateKey("0xdcaf65ead38f7cf0eb4f81961f8fc7f9b7f1e2f45e2d4a6da0dbef85f46f6057");
// Alternatively, use the Secp256k1 scheme
const privateKeySec = new Secp256k1PrivateKey("0xdcaf65ead38f7cf0eb4f81961f8fc7f9b7f1e2f45e2d4a6da0dbef85f46f6057");

// Next, create an account. Keep in mind that this method is only applicable to accounts with unchanged authentication keys.
const account = await Account.fromPrivateKey({ privateKey });

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

Integration of NextAuth with Typescript in nextjs is a powerful tool for authentication

I am diving into NextAuth for the first time, especially with all the new changes in Nextjs 13. Setting up nextauth on my project seems to be a daunting task. I have gone through the documentation here I am struggling to configure it for nextjs 13. How do ...

Fixed scrollable element positioned at the bottom of the webpage

I’m facing a challenge where I need a scrollable element positioned at the bottom of my webpage. Initially, I tried setting position: fixed along with bottom:0. However, this approach didn’t work for me since I require a horizontal list that can be scr ...

Confirming the Checkbox Field - ASP.NET with the Power of jQuery

I am currently working on a straightforward validation process for checking agreements using an asp:checkbox and an asp:button click. When the button is clicked, I have this function being called: OnClientClick="ValidateConditions();" The function itsel ...

What is the best way to showcase the output of a Perl script on a webpage

I recently came across a resource discussing the process of executing a perl script from a webpage. What is the best way to run a perl script from a webpage? However, I am facing a situation where the script I have takes more than 30 seconds to run and d ...

In VB.Net, utilize regex to eliminate whitespace except for the content within <script> tags

I'm seeking a solution to minimize the size of my HTML output stream by eliminating empty lines and whitespace. I've attempted using regex, but my current pattern is inadvertently removing entire script blocks. How can I refine my approach to ens ...

Error TS2322 occurs during compilation in Typescript when using ng.IPromise

Having some issues while using Angular 1.x with Typescript. Here is the code causing trouble: get(id): ng.IPromise<Server.MyItem> { return this.$http.get(`${this.baseAddress}/${id}`).then(d => d.data); } After compiling with tsc, I am encoun ...

Can a jQuery object be generated from any random HTML string? For example, is it possible to create a new link variable like this: var $newlink = $('<a>new link</a>')?

I'm looking to create an object without it being attached to the dom. By passing a HTML string, I want to insert the element into the dom and still have a reference to it. ...

Problem with jQuery's UI autocomplete functionality

Implementing jQ UI Autocomplete with multiple values Below is how my function is structured: mailto_input.bind( "keydown", function( event ) { if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) { ...

What is the best way to test a React component that includes a Router, Redux, and two Higher Order Components using Jest and Enzyme?

I'm currently facing a challenge with this issue. I have a React Component that is linked to React Router 4 and Redux store, and it's wrapped by two HOCs. It may sound complicated, but that's how it was implemented. Here's the export st ...

Limiting the fields returned from a $lookup operation in MongoDB when creating a new view

Within my MongoDB/Node backend configuration, I have implemented a series of pipeline operators using Studio 3T to generate a specific view. One crucial step in this process involves utilizing the $lookup operator to retrieve associated data from a seconda ...

Combining properties of JavaScript objects into one

Just starting my Vue journey and encountering a slight challenge. Displayed below is a table with various items: Whenever an item is selected and its quantity increased, I need my addOptional method (optional) to update a variable with the item's qu ...

Submitting a form in a Chrome packaged app

Exploring Chrome packaged apps for the first time and facing a common issue that involves two questions. Attempting to create a basic form to submit data to a server using Ajax and POST. However, running into the restriction of not being able to use inlin ...

Explanation of returning multiple values

Can you help me understand how to specify the type when a function returns two values? Consider this function: const exampleFunction = (input: number) => { const firstValue = input; const innerFunction = (funcInput: number) => { funcInput * ...

Delay the next loop iteration until receiving an HTTP request

I have a task where I am collecting YouTube video IDs and storing them in an array. Then, I am looping through each video ID to determine the size of that ID's thumbnail image. My challenge now is making sure my loop waits for the HTTP request to fini ...

Identify the activation of the "Inspect Element" feature

On Samy Kamkar's personal website at , there is a clever feature that detects when the console is opened and automatically clears the source code/console. It's a fascinating display of coding magic! https://i.stack.imgur.com/kag6U.jpg Curious t ...

Cyber Platform

I recently encountered a challenge while working on my web project. What are some areas that can be improved? import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import {map} from 'rxjs/op ...

What is the best way to incorporate my light/dark mode theme into index.js and have my header serve as the switch location?

Could someone assist me with setting up a theme around my index.js components and using a switch state in my header.js to toggle between light and dark themes? I'm looking for a way to simplify the process to minimize the amount of code needed. As a ...

Unable to retrieve nested element from its parent

I am facing an issue with accessing a child element's method from the parent element using ref='menu'. When I try to call $refs.menu.show in a button element within the Vue app, it works fine. However, when I try to do the same in a photo el ...

Using ES6 Generator in conjunction with the $http service

I'm exploring the ES6 generator in combination with Angular's $http service on the client side. My goal is to utilize the $http service without relying on callbacks, if achievable. For example: var gen = function* () { var test = yield $http ...

What measures can be taken to secure an API from unauthorized access by clients?

As I work on building a website using react and nextjs, I find myself creating API endpoints within the API folder. However, I am unsure of how to protect these endpoints from being accessed by clients directly. My goal is to utilize these APIs exclusive ...