The mnemonic phrase is created following the BIP-0039
indexes = Aewallet.Indexes.generate_indexes()
Aewallet.Mnemonic.generate_phrase(indexes)
The mnemonic phrase is encrypted using the AES algorithm with the CBC cipher mode. In the following diagram you can see how the Cypher Block Chaining (CBC) mode is working. More info here
In code goes like follows
Aewallet.Cypher.encrypt(data_to_encrypt, "password")
Aewallet.Cypher.decrypt(encrypted_data, "password")
To create a seed from an already generated mnemonic phrase use the following function.
seed = Aewallet.KeyPair.generate_seed(mnemonic)
If a passphrase is not present, an empty string "" is used instead.
But an user may decide to protect their mnemonic with a passphrase, to do so add the passphrase as the next parameter
seed = Aewallet.KeyPair.generate_seed(mnemonic, pass_phrase)
Following the BIP-0032
HD wallets are created from a single root seed, which is a 128bit random number. Most commonly, this seed is generated from a mnemonic as detailed in the previous section.
Every key in the HD wallet is deterministically derived from this root seed, which makes it possible to re-create the entire HD wallet from that seed in any compatible HD wallet. This makes it easy to back up, restore, export, and import HD wallets containing thousands or even millions of keys by simply transferring only the mnemonic that the root seed is derived from.
After we have generated our seed we can use to generate the Master Private key use the following function
master_priv_key = Aewallet.KeyPair.generate_master_key(seed, network, opts)
For the network you could use the following atoms:
For creating keys on Mainnet - :mainnet
For creating keys on Testnet - :testnet
For opts you could use only the option :type
and as for values:
For Aeternity keys use - :ae
For Bitcoin keys use - :btc
If you don't state options Aeternity keys will be created by default!
After we have generated the extended private key we can convert it to public key using the following function:
extended_pub_key = Aewallet.KeyPair.to_public_key(extended_private_key)
If the private key has an Aeternity prefix, an Aeternity public key shall be created, otherwise a Bitcoin public key.
Having already the Public key generated, we can derive the address as follows
address = Aewallet.KeyPair.generate_wallet_address(pub_key, network, opts)
For the network you could use the following atoms:
For creating address on Mainnet - :mainnet
For creating address on Testnet - :testnet
For opts you could use only the option :type
and as for values:
For Aeternity address use - :ae
For Bitcoin address use - :btc
Once we have the master extended keys we can use them to derive children using the following functions:
If we want to derive child private key we will use lowercase m
in the path
Aewallet.KeyPair.derive(extended_priv_key, "m/0'", network)
If we want to derive child public key we will use uppercase M
in the path
Aewallet.KeyPair.derive(extended_priv_key, "M/0'", network)
For network use either :mainnet
or :testnet
To derive hardned key use an apostrophe '
sign after the number and a slash /
to go deeper in the hierarchy
For deriving a Private key the following prefix values should be used:
mainnet = 0x9E850AC9
testnet = 0x9E850AC9
For deriving a Public key the following prefix values should be used:
mainnet = 0x9E86B78E
testnet = 0xF350DAF8
For the address use the following Network bytes:
mainnet = 0x18
testnet = 0x42
Make sure you have installed the following packages to make sure that the dependency libsecp256k1
will work properly:
sudo apt-get install autoconf autogen
sudo apt-get install libtool
sudo apt-get install libgmp3-dev
If available in Hex, the package can be installed
by adding aewallet
to your list of dependencies in mix.exs
:
def deps do
[
{:aewallet, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/elixir_wallet.