Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created July 8, 2018 15:05
Show Gist options
  • Save KamilLelonek/46a96d3de7d65579c4d3cea1c2722c5b to your computer and use it in GitHub Desktop.
Save KamilLelonek/46a96d3de7d65579c4d3cea1c2722c5b to your computer and use it in GitHub Desktop.
def derive_key(%Private{chain_code: chain_code} = private_key, index)
when normal?(index) do
chain_code
|> compressed_hmac_sha512(private_key, index)
|> private_derivation(private_key)
end
def derive_key(%Private{key: key, chain_code: chain_code} = private_key, index)
when hardened?(index) do
chain_code
|> Crypto.hmac_sha512(<<0::8, key::binary, index::32>>)
|> private_derivation(private_key)
end
def derive_key(%Public{chain_code: chain_code} = public_key, index)
when normal?(index) do
chain_code
|> compressed_hmac_sha512(public_key, index)
|> elliptic_curve_point_addition(public_key)
end
def derive_key(%Public{}, index)
when hardened?(index),
do: raise("Cannot derive Public Hardened Child!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment