Keys
To perform actions using an account and private-public key pairs with feather.js, you need an implementation of the Key class, which provides an abstraction around the signing functions of an account. There are multiple implementations available:
You can also create a custom signing solution by extending the base Key
class.
RawKey
The most basic implementation of a Key
is a RawKey
, which is created using a plain private key. RawKey
wraps the 32 bytes of a private key and supplies a corresponding public key:
MnemonicKey
A MnemonicKey
derives itself from a 24-word BIP-39 mnemonic as opposed to the bytes of a private key.
A MnemonicKey
has various levels of definition:
- Supply no arguments for the mnemonic to be randomly generated (effectively generating a random key).
- Supply only a 24-word BIP-39 mnemonic to generate a corresponding key.
- Supply a full HD path (using either a random or specified mnenomic).
Specifying an HD path
The MnemonicKey
can be used to recover a wallet with a particular BIP44 HD path: m/44'/${coinType}'/${account}'/0/${index}
.
As per the Cosmos HD Key Derivation spec:
Cosmos blockchains support hierarchical deterministic key generation (HD keys) for deriving multiple cryptographic keypairs from a single secret value. This allows the user to use different keypairs for different accounts on one blockchain and create accounts on multiple blockchains without having to manage multiple secrets.
For example, to recover a mnemonic with the old Terra wallet HD path using coin type for ATOM (118):
-
Coin Type Numbers
330
and118
above refer to 'coin-types' for theTerra
andCosmos
blockchains respectively. These numbers are defined according to the BIP044 standard. You can find more information here.
Custom key implementation
If you need to write your own key management solution, you will need to subclass the abstract Key
class and provide your own signing function. Instead of exposing details pertaining to your private key, you can specify a sign()
function that forwards the signing request to a server or to a hardware wallet. The remaining functions related to signing (createSignature()
and signTx()
) are automatically provided and use sign()
underneath.
The following code listing is close to the implementation of RawKey
, which illustrates how to write a custom Key
:
Note that you must call super()
with the public key to generate the relevant account and validator public keys associated with your key.