🏋️♂️
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prepend_index(private_key), do: <<0::8, private_key::binary>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@version_numbers %{ | |
private: %{ | |
main: <<4, 136, 173, 228>>, | |
test: <<4, 53, 131, 148>> | |
}, | |
public: %{ | |
main: <<4, 136, 178, 30>>, | |
test: <<4, 53, 135, 207>> | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
decoded_seed = Base.decode16!(seed, case: :lower) | |
hashed_seed = :crypto.hmac(:sha512, "Bitcoin seed", decoded_seed) | |
<<private_key::binary-32, chain_code::binary-32>> = hashed_seed | |
{private_key, chain_code} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp iterate(_mnemonic, round_number, _previous_block, result) | |
when round_number > @rounds_count, | |
do: result | |
defp iterate(mnemonic, round_number, previous_block, result) do | |
with next_block = :crypto.hmac(:sha512, mnemonic, previous_block), | |
do: iterate(mnemonic, round_number + 1, next_block, :crypto.exor(next_block, result)) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp pbkdf2(initial_block, mnemonic), | |
do: iterate(mnemonic, @initial_round_number + 1, initial_block, initial_block) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate(mnemonic, passphrase \\ "") do | |
passphrase | |
|> salt() | |
|> initial_round(mnemonic) | |
|> pbkdf2(mnemonic) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp hmac_sha512(key, data), do: :crypto.hmac(:sha512, key, data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp salt(passphrase \\ ""), do: "mnemonic" <> passphrase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~r/.{1,11}/ | |
|> Regex.scan(entropy) | |
|> List.flatten() | |
|> Enum.map(fn part -> | |
index = String.to_integer(part, 2) | |
Enum.at(@words, index) | |
end) | |
|> Enum.join(" ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.slice(binary_string, 0..(checksum_length - 1)) |