# Keys

In order to engage as an active participant in the blockchain, you will need to manage a private key that can sign transactions.

Key is an abstract interface to the signing capabilities of a private key, and Wallet is a wrapper around it to provide convenient functions for generating and signing transactions.

# Mnemonic Key

jigu.key.mnemonic.MnemonicKey(mnemonic, account=0, index=0, coin_type=330)

# Parameters

str
mnemonic
optional

English mnemonic seed phrase.


DEFAULT 0
int
account
optional

Account number for the BIP44 path.


DEFAULT 0
int
index
optional

Index for the BIP44 path.


DEFAULT 0
int
coin_type
optional

BIP44 coin type.


DEFAULT LUNA_COIN_TYPE (330)

A standard mnemonic for Terra is a 24-word English BIP39 Seed Phrase that stores all the information needed to reproduce a private key. Although any BIP39 mnemonic can be used to provide the seed entropy for a private key, all the other tools in the Terra ecosystem such as terracli or Terra Station use this mnemonic scheme.

# Import an existing mnemonic

You can create a MnemonicKey if you have access to your mnemonic phrase. Make sure your mnemonic is 24 words separated by spaces (not commas), with no whitespace on either end as these issues may result in a different private key.

from jigu.key.mnemonic import MnemonicKey
key = MnemonicKey("<your mnemonic here>")

WARNING

It is strongly advised to NOT keep mnemonic phrases hard-coded in your program, as it is vulnerable to being stolen by anyone viewing your code.

A safer method is to reference environment variables in your code, and then load them manually through your shell prior to runtime.

import os
key = MnemonicKey(os.env.get("MNEMONIC"))

For more secure applications, consider using a Ledger Key or custom HSM Key.

# Generate a random mnemonic

MnemonicKey.generate(account=0, index=0, coin_type=330)

If you don't yet have a mnemonic, you can generate one using MnemonicKey.

key = MnemonicKey.generate()
key.mnemonic
# ...

Be sure to write down your 24-word seed phrase and keep it in a place safe from prying eyes, for anybody who knows your sequence of words potentially has access to your funds!

# Parameters

int
account
optional

Account number for the BIP44 path.


DEFAULT 0
int
index
optional

Index for the BIP44 path.


DEFAULT 0
int
coin_type
optional

BIP44 coin type.


DEFAULT LUNA_COIN_TYPE (330)

# Get account information

MnemonicKey is an instance of an abstract Key, and exposes the following:

AccAddress
acc_address

terra- prefixed Bech32 address derived from the public key. Your account address is used for receiving funds and serves as chain identity outside of validator functions.

ValAddress
val_address

terravaloper- prefixed Bech32 address derived from the public key. It is used for identifying yourself as a validator for functions in staking, rewards distribution, submitting exchange rate votes for the Oracle, etc.

Every account has an associated validator address that can be determined from the account address, even if not yet registered as a validator.

AccPubKey
acc_pubkey

terrapub- prefixed Bech32-encoded public key.

ValPubKey
val_pubkey

terravalpub- prefixed Bech32 address derived from the public key.

# Writing a Custom Key

jigu.key.Key

Jigu defines Key as an abstract interface, whose instances should implement public_key property and sign() method.

Though MnemonicKey will work perfectly fine for most wallet automation tasks, many forms of key management such as hardware wallets or HSM won't allow the seed phrase or private key to be accessed directly. See this example to see how to extend Key for use with more secure solutions.

To make Jigu work with other forms of key management, you can write your own implementation of Key, overriding the public_key property and the sign() method. Address / public key derivation and transaction signing capabilities are automatically provided.

The structure of BasicKey, is shown below as an example to illustrate how this is can be done.

from jigu.key import Key

class BasicKey(Key):

    @property
    def public_key(self) -> bytes:
        """The public key in bytes."""

    def sign(self, payload: bytes) -> bytes:
        """Signs the bytes payload using ECDSA Secp256k1"""

Updated on: 3/11/2020, 12:25:19 PM