0

Does anyone know how the Terra Station Wallet generates the 364 characters Private Key? I am looking for a way to generate this 364 characters Private Key using terra-sdk, but the length of the mk = MnemonicKey()'s mk.private_key is not 364 characters.

Appreciate any help

2 Answers 2

1

You sparked my curiosity. I went through the Terra Station code (I chose mobile) to see how they do it. I first searched for where in the UI was the “Export Private Key”; it looks to be the encrypted Key string in AuthDataValueType.

Here’s where they read it out of keystore. https://github.com/terra-money/station-mobile/blob/f74c4224986fd9ed32b4380b537e9ae13ca05c3e/src/utils/authData.ts#L15

Here’s where they create it for a newly recovered wallet. https://github.com/terra-money/station-mobile/blob/3ec15b9a620432dee47378f5b6e621d93780748a/src/utils/wallet.ts#L66

And, lastly here are the encrypt util functions. https://github.com/terra-money/station-mobile/blob/3ec15b9a620432dee47378f5b6e621d93780748a/src/utils/crypto.ts

This is all NodeJS/ReactNative code so you would need to create the same encrypt/decrypt, password and storage flow in Python, if necessary.

5
  • 1
    Thank you Pete for the guidance. Appreciate it. So, as I suspected, this is not the private key, but an encrypted version of that. I encountered the challenge when I wanted to import my terra-sdk's mk.private_key private keys, to the terra station wallet. There, it won't accept my generated private key, but accepts the 364 characters encrypted private key of its own (that was produced in the export wallet process), which is weird!! Apr 4, 2022 at 9:45
  • Yes, I think you’re on the right track. I think you would have to implement the same encrypt/decrypt process—and I think it is based on the password—to allow a user to move a private key from Terra Station wallet to your own wallet implementation. This seems to be why most just focus on using the Mnemonic for recovering wallet—IMHO.
    – petegordon
    Apr 4, 2022 at 15:49
  • 1
    Yeah, you're right. I was more comfortable working with the mnemonic phrase, too. But, I couldn't see an import option with the mnemonic phrase in the terra station wallet. Anyways, thanks again. Apr 5, 2022 at 3:59
  • On the mobile Terra Station Software you can use the mnemonic by going to “recover wallet” and then “use seed phrase”
    – petegordon
    Apr 6, 2022 at 4:05
  • 1
    Oh, I see. I am seeing the same option in Chrome extension. Thanks again Pete. Apr 7, 2022 at 7:00
0

The import 'key' for Terra Station is actually a base64 encoded JSON object containing the wallet name, Terra address and the private key (which is further AES encrypted and base64 encoded). This is some C# to create it (you will need to get the private key using something like the Mnemonic Code Converter webpage) - fill in the string variables at the top:

        string privatekey = @"";  
        string walletName = @"";
        string address = @"";
        string password = @"changeme";

        byte[] salt = Encoding.UTF8.GetBytes("kopwemdmondawfwa");
        byte[] iv = Encoding.UTF8.GetBytes("dgfdkfsokwedopmf");
        int iterations = 100;
        int keySize = 256;

        var myRijndael = new RijndaelManaged();

        myRijndael.KeySize = keySize; 
        myRijndael.IV = iv;

        var rfc2898 = new Rfc2898DeriveBytes(System.Text.Encoding.UTF8.GetBytes(password), salt, iterations);
        byte[] key = rfc2898.GetBytes(keySize / 8);

        myRijndael.Key = key;
        myRijndael.Padding = PaddingMode.PKCS7;
        myRijndael.Mode = CipherMode.CBC;
        ICryptoTransform transform = myRijndael.CreateEncryptor();

        byte[] bak = new System.Text.UTF8Encoding().GetBytes(privatekey);
        byte[] encrypted = transform.TransformFinalBlock(bak, 0, bak.Length);

        string saltStr = BitConverter.ToString(salt).Replace("-", ""); 
        string ivStr = BitConverter.ToString(iv).Replace("-", "");  
        string cipherStr = System.Convert.ToBase64String(encrypted);

        string keyString = saltStr + ivStr + cipherStr;

        string res = "{ \"name\":\"" + walletName + "\",\"address\":\"" + address + "\",\"encrypted_key\":\"" + keyString + "\"}";
        byte[] resBytes = new System.Text.UTF8Encoding().GetBytes(res);
        string base64 = System.Convert.ToBase64String(resBytes);

        File.WriteAllText(@"c:\temp\ts_exported_key.txt", base64);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.