extended_key v0.3.0 ExtendedKey View Source

BIP32 implementation

Link to this section Summary

Functions

Derives a child extended key with a given index

Derives child extended key with given key path, hardened can be represented with H, h or ', e.g. m/0H/1/2H/2/1000000000

Generates ExtendedKey instance from given key string

Checks if the key is hardened key

Generates private master extended key with given seed and network

Returns the network of the key

Generates a new extended public key from extended private key. If the input key is already an extended public key, the key will be returned unaltered

Checks if the key is normal key

Checks if the key is xprv

Checks if the key is xpub

Generates HD wallet seed with specified byte size

Encodes an extended key to string

Link to this section Types

Link to this type

key() View Source
key() :: %ExtendedKey{
  chain_code: binary(),
  child_num: integer(),
  depth: integer(),
  key: binary(),
  parent_fingerprint: binary(),
  version: binary()
}

Link to this section Functions

Link to this function

derive_child(parent, child_index) View Source
derive_child(parent :: key(), child_index :: integer()) ::
  key() | {:error, term()}

Derives a child extended key with a given index

Examples

iex> seed = Base.decode16!("81D0E7581BF0C55B2941B2295EB4FD1F9C52D080F8D58A3DB634DE80200BA238")
iex> master = ExtendedKey.master(seed)
iex> ExtendedKey.derive_child(master, 0)
%ExtendedKey{
  chain_code: <<81, 134, 58, 230, 254, 116, 95, 133, 120,
    201, 148, 202, 21, 162, 172, 251, 172, 207, 11, 123, 23,
    163, 17, 104, 229, 164, 4, 203, 82, 235, 182, 98>>,
  child_num: 0,
  depth: 1,
  key: <<221, 183, 68, 24, 208, 227, 114, 203, 222, 172, 41,
    97, 113, 120, 208, 123, 220, 241, 248, 18, 174, 129, 46,
    20, 141, 240, 111, 137, 78, 163, 176, 38>>,
  parent_fingerprint: <<197, 217, 101, 7>>,
  version: <<4, 136, 173, 228>>
}
Link to this function

derive_path(master, path) View Source
derive_path(master :: key(), path :: String.t()) :: key() | {:error, term()}

Derives child extended key with given key path, hardened can be represented with H, h or ', e.g. m/0H/1/2H/2/1000000000

Examples

Derive private extended key

iex> seed = Base.decode16!("81D0E7581BF0C55B2941B2295EB4FD1F9C52D080F8D58A3DB634DE80200BA238")
iex> master = ExtendedKey.master(seed)
iex> ExtendedKey.derive_path(master, "m/0")
%ExtendedKey{
  chain_code: <<81, 134, 58, 230, 254, 116, 95, 133, 120,
    201, 148, 202, 21, 162, 172, 251, 172, 207, 11, 123, 23,
    163, 17, 104, 229, 164, 4, 203, 82, 235, 182, 98>>,
  child_num: 0,
  depth: 1,
  key: <<221, 183, 68, 24, 208, 227, 114, 203, 222, 172, 41,
    97, 113, 120, 208, 123, 220, 241, 248, 18, 174, 129, 46,
    20, 141, 240, 111, 137, 78, 163, 176, 38>>,
  parent_fingerprint: <<197, 217, 101, 7>>,
  version: <<4, 136, 173, 228>>
}

Derives public extended key

iex> seed = Base.decode16!("81D0E7581BF0C55B2941B2295EB4FD1F9C52D080F8D58A3DB634DE80200BA238")
iex> master = ExtendedKey.master(seed)
iex> ExtendedKey.derive_path(master, "M/0")
%ExtendedKey{
  chain_code: <<81, 134, 58, 230, 254, 116, 95, 133, 120,
    201, 148, 202, 21, 162, 172, 251, 172, 207, 11, 123, 23,
    163, 17, 104, 229, 164, 4, 203, 82, 235, 182, 98>>,
  child_num: 0,
  depth: 1,
  key: <<3, 105, 136, 100, 17, 127, 136, 122, 182, 104, 212,
    103, 97, 101, 215, 37, 57, 133, 58, 22, 120, 242, 32,
    192, 179, 205, 202, 61, 143, 32, 166, 96, 204>>,
  parent_fingerprint: <<197, 217, 101, 7>>,
  version: <<4, 136, 178, 30>>
}
Link to this function

from_string(key_string) View Source
from_string(key_string :: String.t()) :: key()

Generates ExtendedKey instance from given key string

Examples

iex> ExtendedKey.from_string("xpub6934X9tFysrrNCTyWyFPkXPJRRY6r32gBYxAdaXCqqMhoPTEiwU9dxx4Hyc3PURqGE2sZBVq5m6gAYdr9cJoqZfB4vxZ4iFAtDNmacdccDn")
%ExtendedKey{
  chain_code: <<198, 248, 135, 0, 62, 141, 53, 185, 57, 202,
    175, 125, 253, 73, 139, 246, 205, 111, 67, 194, 153,
    100, 132, 144, 106, 181, 125, 11, 98, 78, 126, 33>>,
  child_num: 0,
  depth: 1,
  key: <<3, 56, 201, 189, 255, 96, 60, 207, 74, 104, 151,
    220, 159, 3, 155, 27, 1, 50, 33, 253, 125, 240, 201, 9,
    55, 77, 5, 200, 44, 30, 112, 6, 104>>,
  parent_fingerprint: <<156, 35, 137, 101>>,
  version: <<4, 136, 178, 30>>
}
Link to this function

hardened?(extended_key) View Source
hardened?(key :: key()) :: boolean()

Checks if the key is hardened key

Link to this function

master(seed, network \\ :mainnet) View Source
master(seed :: binary(), network :: :mainnet | :testnet) ::
  key() | {:error, term()}

Generates private master extended key with given seed and network

Examples

iex> seed = Base.decode16!("81D0E7581BF0C55B2941B2295EB4FD1F9C52D080F8D58A3DB634DE80200BA238")
iex> ExtendedKey.master(seed)
%ExtendedKey{
  chain_code: <<219, 39, 154, 114, 218, 155, 234, 37, 227,
    62, 178, 45, 188, 99, 205, 47, 231, 116, 197, 153, 65,
    210, 122, 59, 183, 217, 163, 153, 181, 126, 212, 49>>,
  child_num: 0,
  depth: 0,
  key: <<186, 255, 178, 91, 225, 143, 38, 20, 157, 136, 154,
    47, 67, 94, 33, 236, 13, 194, 187, 72, 112, 195, 171, 7,
    88, 186, 159, 98, 88, 53, 41, 136>>,
  parent_fingerprint: <<0, 0, 0, 0>>,
  version: <<4, 136, 173, 228>>
}
Link to this function

network(extended_key) View Source
network(key :: key()) :: :mainnet | :testnet

Returns the network of the key

Link to this function

neuter(extended_key) View Source
neuter(key :: key()) :: key()

Generates a new extended public key from extended private key. If the input key is already an extended public key, the key will be returned unaltered

Examples

iex> seed = Base.decode16!("81D0E7581BF0C55B2941B2295EB4FD1F9C52D080F8D58A3DB634DE80200BA238")
iex> master = ExtendedKey.master(seed)
iex> ExtendedKey.neuter(master)
%ExtendedKey{
  chain_code: <<219, 39, 154, 114, 218, 155, 234, 37, 227,
    62, 178, 45, 188, 99, 205, 47, 231, 116, 197, 153, 65,
    210, 122, 59, 183, 217, 163, 153, 181, 126, 212, 49>>,
  child_num: 0,
  depth: 0,
  key: <<2, 50, 31, 41, 0, 12, 128, 135, 180, 205, 101, 152,
    78, 96, 157, 22, 33, 235, 18, 207, 180, 81, 232, 138,
    182, 66, 20, 211, 165, 7, 176, 30, 79>>,
  parent_fingerprint: <<0, 0, 0, 0>>,
  version: <<4, 136, 178, 30>>
}
Link to this function

normal?(extended_key) View Source
normal?(key :: key()) :: boolean()

Checks if the key is normal key

Link to this function

private?(extended_key) View Source
private?(key :: key()) :: boolean()

Checks if the key is xprv

Link to this function

public?(extended_key) View Source
public?(key :: key()) :: boolean()

Checks if the key is xpub

Link to this function

seed(byte_size \\ 32) View Source
seed(byte_size :: integer()) :: binary()

Generates HD wallet seed with specified byte size

Link to this function

to_string(extended_key) View Source
to_string(key :: key()) :: String.t()

Encodes an extended key to string

Examples

iex> seed = Base.decode16!("81D0E7581BF0C55B2941B2295EB4FD1F9C52D080F8D58A3DB634DE80200BA238")
iex> master = ExtendedKey.master(seed)
iex> ExtendedKey.to_string(master)
"xprv9s21ZrQH143K4EucrSWAiD6LAFAv7W3DaVp5Zv8LohRYqTM7hPLCQrxsvvv6DoD8Awb64daXUmYLufQhZv9BjckFgqLP6He9HfSMFHQzmM6"