# Problem generating the wallet with RawKey ()

**URL:** <https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965>\
**Category:** dApps\
**Created:** [November 29, 2021, 6:34pm UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965 "2021-11-29T18:34:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dklen](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/dklen/32/1525_2.png) [@dklen](https://classic-agora.terra.money/u/dklen)\
**Post date:** [November 29, 2021, 6:34pm UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/1 "2021-11-29T18:34:51Z")

</div>

Are we able to create wallet from privatekey exported with TerraStation?  
The following code throws an error complaining about the private key:  
Error: Expected private key to be an Uint8Array with length 32

import { LCDClient, MnemonicKey, RawKey } from ‘@terra-money/terra.js’

const terra = new LCDClient({  
URL: ‘[https://lcd.terra.dev](https://lcd.terra.dev)’,  
chainId: ‘columbus-4’  
})

const wallet = terra.wallet(  
new RawKey(  
Buffer.from(‘xxxxprivatekeyxxxx’)  
)  
)

---

<div class="post-metadata">

**Author:** ![larry](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/larry/32/783_2.png) [@larry](https://classic-agora.terra.money/u/larry)\
**Post date:** [November 30, 2021, 12:03am UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/2 "2021-11-30T00:03:40Z")

</div>

`Buffer.from` uses UTF-8 encodings by default, while private keys usually use hex or base64.

Try:

```javascript
Buffer.from("yourPrivateKey", "hex")
// or
Buffer.from("yourPrivateKey", "base64")

```

---

<div class="post-metadata">

**Author:** ![dklen](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/dklen/32/1525_2.png) [@dklen](https://classic-agora.terra.money/u/dklen)\
**Post date:** [November 30, 2021, 2:01am UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/3 "2021-11-30T02:01:49Z")

</div>

It seems that terra.js doesn’t support private key exported from Terra Station where a password is required

---

<div class="post-metadata">

**Author:** ![larry](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/larry/32/783_2.png) [@larry](https://classic-agora.terra.money/u/larry)\
**Post date:** [November 30, 2021, 7:14am UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/4 "2021-11-30T07:14:19Z")

</div>

Alright, I figured it out

```auto
import * as CryptoJS from "crypto-js";
import { LCDClient, MnemonicKey, RawKey } from "@terra-money/terra.js";

const terra = new LCDClient({
  chainID: "columbus-5",
  URL: "https://lcd.terra.dev",
});

```

First, I generate a mnemonic phrase and create a wallet from it using terra.js. I take a note of what the wallet address:

```auto
const wallet1 = terra.wallet(
  new MnemonicKey({
    mnemonic:
      "satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn",
  })
);
console.log(wallet1.key.accAddress);

```

Output:

```auto
terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8

```

Now I put the same mnemonic phrase into Terra Station, export wallet, decrypt the exported private key, and create a wallet using the decrypted key with terra.js. If everything works it should give me the same wallet address as above.

```auto
/**
 * https://github.com/terra-money/station/blob/main/src/utils/terra-keystore.ts#L31
 */
function decrypt(transitmessage: string, pass: string) {
  const salt = CryptoJS.enc.Hex.parse(transitmessage.substr(0, 32));
  const iv = CryptoJS.enc.Hex.parse(transitmessage.substr(32, 32));
  const encrypted = transitmessage.substring(64);

  const keySize = 256;
  const iterations = 100;
  const key = CryptoJS.PBKDF2(pass, salt, {
    keySize: keySize / 32,
    iterations: iterations,
  });

  return CryptoJS.AES.decrypt(encrypted, key, {
    iv: iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC,
  }).toString(CryptoJS.enc.Utf8);
}

```

```auto
type ExportedWallet = {
  name: string;
  address: string;
  encrypted_key: string;
};

const exportedWallet: ExportedWallet = JSON.parse(
  Buffer.from(
    "eyJuYW1lIjoibG9jYWx0ZXJyYTEiLCJhZGRyZXNzIjoidGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDgiLCJlbmNyeXB0ZWRfa2V5IjoiYThjMDg1ZTIyZjYwZmU5ZDljYzU5NGZkMWM5NjEwMjlmZDJhOGY2Y2NkYjA0MjQ5NWY3MmE2MDI1YjA3MmYyZjlJWXgybHhPcTU5Q3lJNDBEa1dXMEtkRzNLZjlnM3NrM3M5eFkwZStVRnZ0Z3VMYnAzd2QyYTFuUWVyS1hOcndveFlMQ29EUU1lTlk5NktHdSszNXh6alA3NUE3TnBTYVhRWUFqK1JNY0lBPSJ9",
    "base64"
  ).toString("utf8")
);
const decryptedKey = decrypt(exportedWallet.encrypted_key, "localterra");
const wallet2 = terra.wallet(new RawKey(Buffer.from(decryptedKey, "hex")));
console.log(wallet2.key.accAddress);

```

Output:

```auto
terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8

```

---

<div class="post-metadata">

**Author:** ![donkeydonkey](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@donkeydonkey](https://classic-agora.terra.money/u/donkeydonkey)\
**Post date:** [January 4, 2022, 3:58pm UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/5 "2022-01-04T15:58:28Z")

</div>

awesome.  
thanks so much for the code. it worked great!

---

<div class="post-metadata">

**Author:** ![Bahadir\_Yagci](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/bahadir_yagci/32/2568_2.png) [@Bahadir\_Yagci](https://classic-agora.terra.money/u/Bahadir_Yagci)\
**Post date:** [March 11, 2022, 10:20pm UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/6 "2022-03-11T22:20:41Z")

</div>

hello guys, I have the same warning when try to transfer or swap, and dont know anythng about the codes, please help me

---

<div class="post-metadata">

**Author:** ![DCI\_riva](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/dci_riva/32/5305_2.png) [@DCI\_riva](https://classic-agora.terra.money/u/DCI_riva)\
**Post date:** [May 14, 2022, 2:19am UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/9 "2022-05-14T02:19:50Z")

</div>

Thank you for your Post.

Is it Okay for “_localterra_” as argument even on mainnet?

My private Key isn’t work for this.

Error with below message:  
Expected private key to be an Uint8Array with length 32

---

<div class="post-metadata">

**Author:** ![larry](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/larry/32/783_2.png) [@larry](https://classic-agora.terra.money/u/larry)\
**Post date:** [May 14, 2022, 11:40am UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/10 "2022-05-14T11:40:54Z")

</div>

in thie example, `localterra` is the password used to encrypt the key in Terra Station

---

<div class="post-metadata">

**Author:** ![joanmechenique](https://sea2.discourse-cdn.com/terra/user_avatar/classic-agora.terra.money/joanmechenique/32/5255_2.png) [@joanmechenique](https://classic-agora.terra.money/u/joanmechenique)\
**Post date:** [May 15, 2022, 8:44pm UTC](https://classic-agora.terra.money/t/problem-generating-the-wallet-with-rawkey/2965/11 "2022-05-15T20:44:57Z")

</div>

Please can you guide me.  
My terra station wallet does not allow me to transfer my funds to any other wallet or exchange.  
Can you help me with this.  
I will thank you.
