var img = document.createElement('img'); img.src = "https://terradocs.matomo.cloud//piwik.php?idsite=1&rec=1&url=https://docs.terra.money" + location.pathname; img.style = "border:0"; img.alt = "tracker"; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(img,s);
Skip to main content

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:


_9
import { RawKey } from '@terra-money/feather.js';
_9
import { randomBytes } from 'crypto';
_9
let see = console.log;
_9
_9
const raw_key = new RawKey(randomBytes(32));
_9
_9
see(raw_key.publicKey); // SimplePublicKey { key: 'AlNdglClnFJMwdeFGlfFTAwbx7rxKxWbgN/lwj5mQ3vw' }
_9
see(raw_key.privateKey); // <Buffer 53 e2 53 0b 29 c4 9d 54 b3 66 a4 61 7b d3 e2 6e 4c e5 41 cd 12 a6 e9 27 8a 97 61 1c 55 6e cd 4c>
_9
see(raw_key.accAddress('terra')); // terra1ptj88nsljjr9agx07hahu6etv43acksy2q44sd

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).

_29
import { MnemonicKey } from '@terra-money/feather.js';
_29
const see = console.log;
_29
_29
const MNE_KEY_RANDOM = new MnemonicKey();
_29
_29
see(MNE_KEY_RANDOM.mnemonic); // famous { ... } myth world size
_29
see(MNE_KEY_RANDOM.privateKey); // <Buffer 4f e5 { ... } 51 a4 41 9c>
_29
see(MNE_KEY_RANDOM.publicKey); // SimplePublicKey { key: 'A8TNSJhn6gGHgY2ohJnkOaZz7Y0FaW/QeytGBaqCLIJU' }
_29
see(MNE_KEY_RANDOM.accAddress('terra')); // terra1l63e8q7yjyd77qanwfgvl43ulagf34a2xzcuv4
_29
_29
const MNE_KEY_EXACT = new MnemonicKey({
_29
mnemonic:
_29
'squirrel future level fan world organ daring thing color orange sausage cross fault interest blast wink audit unfair satoshi solution track indoor sun edit',
_29
});
_29
_29
const MNE_KEY_RANDOM_WITH_HD_PATH = new MnemonicKey({
_29
// mnemonic: '', // optional, will be random if not provided
_29
coinType: 330, // optional, default
_29
account: 0, // optional, default
_29
index: 0, // optional, default
_29
});
_29
_29
const MNE_KEY_FULLY_RESOLVED = new MnemonicKey({
_29
mnemonic:
_29
'squirrel future level fan world organ daring thing color orange sausage cross fault interest blast wink audit unfair satoshi solution track indoor sun edit',
_29
coinType: 330,
_29
account: 0,
_29
index: 0,
_29
});

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}.

💡HD keys

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):


_4
const mne_key = new MnemonicKey({
_4
mnemonic: '[ Your BIP39 mnemonic ]',
_4
coinType: 118, // <--------- Cosmos' coin type ( Terra had inherited initially )
_4
});

  • BIP-39 Mnemonics

  • Coin Type Numbers 330 and 118 above refer to 'coin-types' for the Terra and Cosmos 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:


_33
import SHA256 from 'crypto-js/sha256';
_33
import * as secp256k1 from 'secp256k1';
_33
import { Key } from '@terra-money/feather.js';
_33
_33
/**
_33
* An implementation of the Key interfaces that uses a raw private key.
_33
*/
_33
export class NaiveCustomImplementation extends Key {
_33
/**
_33
* Raw private key, in bytes.
_33
*/
_33
public privateKey: Buffer;
_33
_33
constructor(privateKey: Buffer) {
_33
const publicKey = secp256k1.publicKeyCreate(
_33
new Uint8Array(privateKey),
_33
true,
_33
);
_33
super(new SimplePublicKey(Buffer.from(publicKey).toString('base64')));
_33
this.privateKey = privateKey;
_33
}
_33
_33
public sign(payload: Buffer): Promise<Buffer> {
_33
const hash = Buffer.from(SHA256(payload.toString()).toString(), 'hex');
_33
const { signature } = secp256k1.ecdsaSign(
_33
Uint8Array.from(hash),
_33
Uint8Array.from(this.privateKey),
_33
);
_33
return new Promise(() => {
_33
Buffer.from(signature);
_33
});
_33
}
_33
}

Note that you must call super() with the public key to generate the relevant account and validator public keys associated with your key.