skip to Main Content

In the documentation of Cosmos SDK and everywhere in the internet what’s described is how to generate a wallet out of a mnemonic phrase:

import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";


const my_mnemonic = "<..........>";
const wallet_1 = await DirectSecp256k1HdWallet.fromMnemonic(my_mnemonic, {prefix: 'abcd'});

What I need, however, is to do it out of a private key instead of a mnemonic phrase.

How?

2

Answers


  1. You would just provide the key. I don’t recommend this though:

    import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
    
    
    const my_mnemonic = "<the private key here>";
    const wallet_1 = await DirectSecp256k1HdWallet.fromMnemonic(my_mnemonic, {prefix: 'abcd'});
    
    Login or Signup to reply.
  2. In order to generate a wallet from a private key in Cosmos SDK, you can use the DirectSecp256k1Wallet instead of DirectSecp256k1HdWallet.

    Here’s an example code snippet:

    const { DirectSecp256k1Wallet } = require('@cosmjs/proto-signing');
    const { Secp256k1, PrivKey } = require('@cosmjs/crypto');
    
    const privateKey = PrivKey.fromRaw('your_private_key_here');
    
    const wallet = await DirectSecp256k1Wallet.fromKey(privateKey, 'abcd');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search