Mnemonics and Keys
This section explains how to generate mnemonics, derive keys and get a key pair
Check crypto module reference for more info:
Mnemonic generation
To generate a random mnemonic, use mnemonic_from_random function. Specify the dictionary, and a number of words (12 or 24).
const SEED_PHRASE_WORD_COUNT = 12; //Mnemonic word count
const SEED_PHRASE_DICTIONARY_ENGLISH = 1; //Dictionary identifier
const { phrase } = await client.crypto.mnemonic_from_random({
dictionary: SEED_PHRASE_DICTIONARY_ENGLISH,
word_count: SEED_PHRASE_WORD_COUNT,
});
console.log(`Generated seed phrase "${phrase}"`);Result:
Key pair generation from mnemonic
Here is the fast way to generate a key pair for a signature from a specified mnemonic and path with mnemonic_derive_sign_keys method. The specified path, dictionary and word count is compatible with Surf and Everdev CLI:
Result:
See the Keys derivation section for more information about the algorithm that is used in mnemonic_derive_sign_keys.
Key pair generation without mnemonic
Sometimes there is no need for mnemonic.
For example, if you generate a key pair for a server that does not need a readable form. Then you can easily generate such a pair with generate_random_sign_keys function:
Result:
Great! Now you can use this key pair in methods of contracts module, such as abi.encode_message, abi.encode_message_body, etc.
Keys derivation
Master (root) key
To derive a key within a specified path, first, you need to generate a seed from the given mnemonic and then get the extended master private key that will be the root for all the derived keys.
Both these operations can be performed with hdkey_xprv_from_mnemonic method:
Result:
Derived key
Now you can derive the key within the specified path with hdkey_derive_from_xprv_path method. The result will be an extended derived private key.
Result:
Now lets extract the private key itself from the extended key with function:
Result:
Generate keys for signature
After we've got the derived private key we can generate a ed25519 key pair for signature:
Result:
Great! Now you can use this key pair in methods of contracts module, such as abi.encode_message, abi.encode_message_body, etc.
Last updated
Was this helpful?