View Source BitcoinLib.Key.PrivateKey (BitcoinLib v0.2.0-pre1)
Bitcoin extended private key management module
Link to this section Summary
Functions
Simply calls from_derivation_path and directly returns the private key whatever the outcome.any() Will crash if the index is negative or greater than 0x7FFFFFFF
Derives the nth child of a HD private key
Deserialization of a private key from its xpriv version
Simply calls from_derivation_path and directly returns the private key whatever the outcome.any() Will crash if used with an invalid derivation path
Derives a child private key, following a derivation path
Converts a mnemonic phrase into an extended private key
Converts a seed into a master private key hash containing the key itself and the chain code
Create a private key from a WIF
Creates a Bitcoin private key using efficient randomness
Serialization of a master private key into its xpriv version
Signs a message using a private key
Converts a private key to the WIF format
Link to this section Functions
@spec derive_child!( %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }, integer(), boolean() ) :: %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }
Simply calls from_derivation_path and directly returns the private key whatever the outcome.any() Will crash if the index is negative or greater than 0x7FFFFFFF
examples
Examples
iex> private_key = %BitcoinLib.Key.PrivateKey{ ...> key: <<0xf79bb0d317b310b261a55a8ab393b4c8a1aba6fa4d08aef379caba502d5d67f9::256>>, ...> chain_code: <<0x463223aac10fb13f291a1bc76bc26003d98da661cb76df61e750c139826dea8b::256>> ...> } ...> index = 0 ...> BitcoinLib.Key.PrivateKey.derive_child!(private_key, index) %BitcoinLib.Key.PrivateKey{
key: <<0x39f329fedba2a68e2a804fcd9aeea4104ace9080212a52ce8b52c1fb89850c72::256>>,
chain_code: <<0x05aae71d7c080474efaab01fa79e96f4c6cfe243237780b0df4bc36106228e31::256>>,
depth: 1,
index: 0,
parent_fingerprint: <<0x18C1259::32>>
}
@spec derive_child( %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }, integer(), boolean() ) :: {:ok, %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }}
Derives the nth child of a HD private key
Takes a private key, its chain code and the child's index Returns the child's private key and it's associated chain code
Inspired by https://learnmeabitcoin.com/technical/extended-keys#child-extended-key-derivation
examples
Examples
iex> private_key = %BitcoinLib.Key.PrivateKey{ ...> key: <<0xf79bb0d317b310b261a55a8ab393b4c8a1aba6fa4d08aef379caba502d5d67f9::256>>, ...> chain_code: <<0x463223aac10fb13f291a1bc76bc26003d98da661cb76df61e750c139826dea8b::256>> ...> } ...> index = 0 ...> BitcoinLib.Key.PrivateKey.derive_child(private_key, index) {
:ok,
%BitcoinLib.Key.PrivateKey{
key: <<0x39f329fedba2a68e2a804fcd9aeea4104ace9080212a52ce8b52c1fb89850c72::256>>,
chain_code: <<0x05aae71d7c080474efaab01fa79e96f4c6cfe243237780b0df4bc36106228e31::256>>,
depth: 1,
index: 0,
parent_fingerprint: <<0x18C1259::32>>
}
}
@spec deserialize(binary()) :: %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }
Deserialization of a private key from its xpriv version
examples
Examples
values from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
iex> "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi" ...> |> BitcoinLib.Key.PrivateKey.deserialize()
%BitcoinLib.Key.PrivateKey {
key: <<0xE8F32E723DECF4051AEFAC8E2C93C9C5B214313817CDB01A1494B917C8436B35::256>>,
chain_code: <<0x873DFF81C02F525623FD1FE5167EAC3A55A049DE3D314BB42EE227FFED37D508::256>>,
depth: 0,
index: 0
}
@spec from_derivation_path!( %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }, %BitcoinLib.Key.HD.DerivationPath{ account: term(), address_index: term(), change: term(), coin_type: term(), purpose: term(), type: term() } ) :: %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }
@spec from_derivation_path!( %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }, binary() ) :: %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }
Simply calls from_derivation_path and directly returns the private key whatever the outcome.any() Will crash if used with an invalid derivation path
examples
Examples
iex> private_key = %BitcoinLib.Key.PrivateKey{ ...> key: <<0xF79BB0D317B310B261A55A8AB393B4C8A1ABA6FA4D08AEF379CABA502D5D67F9::256>>, ...> chain_code: <<0x463223AAC10FB13F291A1BC76BC26003D98DA661CB76DF61E750C139826DEA8B::256>> ...> } ...> {:ok, derivation_path} = BitcoinLib.Key.HD.DerivationPath.parse("m/44'") ...> BitcoinLib.Key.PrivateKey.from_derivation_path!(private_key, derivation_path) %BitcoinLib.Key.PrivateKey{
key: <<0x4E59086C1DEC6D081986CB079F536E38B3D2B6DA7A8EDCFFB1942AE8B9FDF156::256>>,
chain_code: <<0xF42DE823EE78F6227822D79BC6F6101D084D7F0F876B7828BF027D681294E538::256>>,
depth: 1,
index: 0x8000002C,
parent_fingerprint: <<0x18C1259::32>>
}
@spec from_derivation_path( %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }, %BitcoinLib.Key.HD.DerivationPath{ account: term(), address_index: term(), change: term(), coin_type: term(), purpose: term(), type: term() } ) :: {:ok, %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }}
@spec from_derivation_path( %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }, binary() ) :: {:ok, %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }}
Derives a child private key, following a derivation path
examples
Examples
iex> private_key = %BitcoinLib.Key.PrivateKey{ ...> key: <<0xF79BB0D317B310B261A55A8AB393B4C8A1ABA6FA4D08AEF379CABA502D5D67F9::256>>, ...> chain_code: <<0x463223AAC10FB13F291A1BC76BC26003D98DA661CB76DF61E750C139826DEA8B::256>> ...> } ...> {:ok, derivation_path} = BitcoinLib.Key.HD.DerivationPath.parse("m/44'") ...> BitcoinLib.Key.PrivateKey.from_derivation_path(private_key, derivation_path) {
:ok,
%BitcoinLib.Key.PrivateKey{
key: <<0x4E59086C1DEC6D081986CB079F536E38B3D2B6DA7A8EDCFFB1942AE8B9FDF156::256>>,
chain_code: <<0xF42DE823EE78F6227822D79BC6F6101D084D7F0F876B7828BF027D681294E538::256>>,
depth: 1,
index: 0x8000002C,
parent_fingerprint: <<0x18C1259::32>>
}
}
Converts a mnemonic phrase into an extended private key
examples
Examples
iex> "rally celery split order almost twenty ignore record legend learn chaos decade" ...> |> BitcoinLib.Key.PrivateKey.from_mnemonic_phrase() %BitcoinLib.Key.PrivateKey{
fingerprint: <<0x2E92A74C::32>>,
key: <<0xD6EAD233E06C068585976B5C8373861D77E7F030EC452E65EE81C85FA6906970::256>>,
chain_code: <<0xA17100DD000D9D4A37034F7CEE0D46F5AE97B570BE065E57A00546FAE014A8A2::256>>,
depth: 0x0,
index: 0x0
}
@spec from_seed(binary()) :: %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }
Converts a seed into a master private key hash containing the key itself and the chain code
examples
Examples
iex> "7e4803bd0278e223532f5833d81605bedc5e16f39c49bdfff322ca83d444892ddb091969761ea406bee99d6ab613fad6a99a6d4beba66897b252f00c9dd7b364" ...> |> BitcoinLib.Key.PrivateKey.from_seed() %BitcoinLib.Key.PrivateKey{
chain_code: <<0x5A7AEBB0FBE37BB89E690A6E350FAFED353B624741269E71001E608732FD8125::256>>,
key: <<0x41DF6FA7F014A60FD79EC50B201FECF9CEDD8328921DDF670ACFCEF227242688::256>>,
fingerprint: <<0xB317B417::32>>,
parent_fingerprint: <<0::32>>
}
Create a private key from a WIF
Based on https://learnmeabitcoin.com/technical/wif
examples
Examples
iex> "KzractrYn5gnDNVwBDy7sYhAkyMvX4WeYQpwyhCAUKDogJTzYsUc" ...> |> BitcoinLib.Key.PrivateKey.from_wif() %BitcoinLib.Key.PrivateKey{key: <<108, 122, 178, 249, 97, 161, 188, 63, 19, 205, 192, 141, 196, 28, 63, 67, 154, 222, 189, 84, 154, 142, 241, 192, 137, 232, 26, 89, 7, 55, 97, 7>>}
Creates a Bitcoin private key using efficient randomness
Inspired by https://learnmeabitcoin.com/technical/private-key
examples
Examples
iex> %{raw: raw1} = BitcoinLib.Key.PrivateKey.generate ...> %{raw: raw2} = BitcoinLib.Key.PrivateKey.generate ...> raw1 == raw2 false
@spec serialize(%BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }) :: binary()
Serialization of a master private key into its xpriv version
examples
Examples
values from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
iex> %BitcoinLib.Key.PrivateKey { ...> key: <<0xE8F32E723DECF4051AEFAC8E2C93C9C5B214313817CDB01A1494B917C8436B35::256>>, ...> chain_code: <<0x873DFF81C02F525623FD1FE5167EAC3A55A049DE3D314BB42EE227FFED37D508::256>> ...> } ...> |> BitcoinLib.Key.PrivateKey.serialize() "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
@spec sign_message(binary(), %BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }) :: binary()
Signs a message using a private key
Below is an example of a signature... this doctest doesn't end with a value, because the signature is different on every call, and even can have different lengths.
examples
Examples
iex> message = "76a914c825a1ecf2a6830c4401620c3a16f1995057c2ab88ac" ...> private_key = %BitcoinLib.Key.PrivateKey{key: <<0xd6ead233e06c068585976b5c8373861d77e7f030ec452e65ee81c85fa6906970::256>>} ...> BitcoinLib.Crypto.Secp256k1.sign(message, private_key)
Converts a private key to the WIF format
Based on https://learnmeabitcoin.com/technical/wif
examples
Examples
iex> %BitcoinLib.Key.PrivateKey{key: <<108, 122, 178, 249, 97, 161, 188, 63, 19, 205, 192, 141, ...> 196, 28, 63, 67, 154, 222, 189, 84, 154, 142, 241, 192, 137, 232, 26, 89, ...> 7, 55, 97, 7>>} ...> |> BitcoinLib.Key.PrivateKey.to_wif() "KzractrYn5gnDNVwBDy7sYhAkyMvX4WeYQpwyhCAUKDogJTzYsUc"