Keys & Wallets

A Key is an object that provides an abstraction for the agency of signing transactions.

Key (abstract)

Implementers of Keys meant for signing should override Key.sign() or Key.create_signature() methods. More details are available in guides/custom_key.

Some properties such as acc_address and val_address are provided.

class terra_sdk.key.key.Key(public_key=None)[source]

Abstract Key interface, representing an agent with transaction-signing capabilities.

Parameters

public_key (Optional[bytes]) – compressed public key bytes,

property acc_address

Terra Bech32 account address. Default derivation via public_key is provided.

Raises

ValueError – if Key was not initialized with proper public key

Returns

account address

Return type

AccAddress

property acc_pubkey

Terra Bech32 account pubkey. Default derivation via public_key is provided.

Raises

ValueError – if Key was not initialized with proper public key

Returns

account pubkey

Return type

AccPubKey

create_signature(tx)[source]

Signs the transaction with the signing algorithm provided by this Key implementation, and outputs the signature. The signature is only returned, and must be manually added to the signatures field of an StdTx.

Parameters

tx (StdSignMsg) – unsigned transaction

Raises

ValueError – if missing public_key

Returns

signature object

Return type

StdSignature

public_key: Optional[bytes]

Compressed public key bytes, used to derive raw_address and raw_pubkey.

raw_address: Optional[bytes]

Raw Bech32 words of address, used to derive associated account and validator operator addresses.

raw_pubkey: Optional[bytes]

Raw Bech32 words of pubkey, used to derive associated account and validator pubkeys.

abstract sign(payload)[source]

Signs the data payload. An implementation of Key is expected to override this method.

Parameters

payload (bytes) – arbitrary data payload

Raises

NotImplementedError – if not implemented

Returns

signed payload

Return type

bytes

sign_tx(tx)[source]

Signs the transaction with the signing algorithm provided by this Key implementation, and creates a ready-to-broadcast StdTx object with the signature applied.

Parameters

tx (StdSignMsg) – unsigned transaction

Returns

ready-to-broadcast transaction object

Return type

StdTx

property val_address

Terra Bech32 validator operator address. Default derivation via public_key is provided.

Raises

ValueError – if Key was not initialized with proper public key

Returns

validator operator address

Return type

ValAddress

property val_pubkey

Terra Bech32 validator pubkey. Default derivation via public_key is provided.

Raises

ValueError – if Key was not initialized with proper public key

Returns

validator pubkey

Return type

ValPubKey

RawKey

class terra_sdk.key.raw.RawKey(private_key)[source]

RawKey directly uses a raw (plaintext) private key in memory, and provides the implementation for signing with ECDSA on curve Secp256k1.

Parameters

private_key (bytes) – private key in bytes

classmethod from_hex(private_key_hex)[source]

Create a new RawKey from a hex-encoded private key string.

Parameters

private_key_hex (str) – hex-encoded private key

Return type

RawKey

private_key: bytes

Private key, in bytes.

sign(payload)[source]

Signs the data payload using ECDSA and curve Secp256k1 with the private key as the signing key.

Parameters

payload (bytes) – data to sign

Return type

bytes

MnemonicKey

class terra_sdk.key.mnemonic.MnemonicKey(mnemonic=None, account=0, index=0, coin_type=330)[source]

A MnemonicKey derives a private key using a BIP39 mnemonic seed phrase, and provides key-derivation options based on the BIP44 HD path standard.

Note

You can change coin_type to 118 to derive the key for a legacy Terra wallet (shares coin_type with ATOM).

Parameters
  • mnemonic (str, optional) – space-separated mnemonic seed phrase. If not provided, a 24-word mnemonic will be generated.

  • account (int, optional) – HD path parameter - account number.

  • index (int, optional) – HD path parameter - account index.

  • coin_type (int, optional) – HD path parameter - coin type.

account: int

account number.

Type

HD path parameter

coin_type: int

coin type

Type

HD path parameter

property hd_path

Returns the BIP32 HD path for key-derivation:

m/44'/COIN_TYPE'/ACCOUNT'/0/INDEX'

Returns

full BIP32 HD path

Return type

str

index: int

account index.

Type

HD path parameter

mnemonic: str

Mnemonic key phrase associated with the account (space-separated).

Wallet

class terra_sdk.client.lcd.wallet.Wallet(lcd, key)[source]

Wraps around a Key implementation and provides transaction building and signing functionality. It is recommended to create this object through LCDClient.wallet().

account_number()[source]

Fetches account number for the account associated with the Key.

Return type

int

account_number_and_sequence()[source]

Fetches both account and sequence number associated with the Key.

Return type

dict

create_and_sign_tx(msgs, fee=None, memo='', gas_prices=None, gas_adjustment=None, fee_denoms=None, account_number=None, sequence=None)[source]

Creates and signs a StdTx object in a single step. This is the recommended method for preparing transaction for immediate signing and broadcastring. The transaction is generated exactly as create_tx().

Parameters
  • msgs (List[Msg]) – list of messages to include

  • fee (Optional[StdFee], optional) – transaction fee. If None, will be estimated. See more on `fee estimation`_.

  • memo (str, optional) – optional short string to include with transaction.

  • gas_prices (Optional[Coins.Input], optional) – gas prices for fee estimation.

  • gas_adjustment (Optional[Numeric.Input], optional) – gas adjustment for fee estimation.

  • fee_denoms (Optional[List[str]], optional) – list of denoms to use for fee after estimation.

  • account_number (Optional[int], optional) – account number (overrides blockchain query if provided)

  • sequence (Optional[int], optional) – sequence (overrides blockchain qu ery if provided)

Returns

signed transaction

Return type

StdTx

create_tx(msgs, fee=None, memo='', gas_prices=None, gas_adjustment=None, fee_denoms=None, account_number=None, sequence=None)[source]

Builds an unsigned transaction object. The Wallet will first query the blockchain to fetch the latest account and sequence values for the account corresponding to its Key, unless the they are both provided. If no fee parameter is set, automatic fee estimation will be used (see fee_estimation).

Parameters
  • msgs (List[Msg]) – list of messages to include

  • fee (Optional[StdFee], optional) – transaction fee. If None, will be estimated. See more on `fee estimation`_.

  • memo (str, optional) – optional short string to include with transaction.

  • gas_prices (Optional[Coins.Input], optional) – gas prices for fee estimation.

  • gas_adjustment (Optional[Numeric.Input], optional) – gas adjustment for fee estimation.

  • fee_denoms (Optional[List[str]], optional) – list of denoms to use for fee after estimation.

  • account_number (Optional[int], optional) – account number (overrides blockchain query if provided)

  • sequence (Optional[int], optional) – sequence (overrides blockchain qu ery if provided)

Returns

unsigned transaction

Return type

StdSignMsg

sequence()[source]

Fetches the sequence number for the account associated with the Key.

Return type

int