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: AccAddress
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
- property acc_pubkey: AccPubKey
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
- create_signature(sign_doc)[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 anTx
.- Parameters
sign_doc (SignDoc) – unsigned transaction
- Raises
ValueError – if missing
public_key
- Returns
signature object
- Return type
SignatureV2
- public_key: Optional[terra_sdk.core.public_key.PublicKey]
Compressed public key bytes, used to derive
raw_address
andraw_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, options)[source]
Signs the transaction with the signing algorithm provided by this Key implementation, and creates a ready-to-broadcast
Tx
object with the signature applied.
- property val_address: ValAddress
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
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
- private_key: bytes
Private key, in 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 (sharescoin_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: str
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 throughLCDClient.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(options)[source]
Creates and signs a
Tx
object in a single step. This is the recommended method for preparing transaction for immediate signing and broadcastring. The transaction is generated exactly ascreate_tx()
.- Parameters
options (CreateTxOptions) – Options to create a tx
- Returns
signed transaction
- Return type
- create_tx(options)[source]
Builds an unsigned transaction object. The
Wallet
will first query the blockchain to fetch the latestaccount
andsequence
values for the account corresponding to its Key, unless the they are both provided. If nofee
parameter is set, automatic fee estimation will be used (see fee_estimation).- Parameters
options (CreateTxOptions) – Options to create a tx
- Returns
unsigned transaction
- Return type