View Source BitcoinLib.Key.PublicKey (BitcoinLib v0.3.2)
Bitcoin extended public key management module
Link to this section Summary
Functions
Simply calls from_derivation_path and directly returns the public key whatever the outcome.any() Will crash if the index is negative or greater than 0x7FFFFFFF
Derives the nth child of a HD public key
Deserialization of a public key from its xpub version
Deserialization of a public key from its xpub version
Derives a child public key, following a derivation path
Derives an extended public key from an extended private key. Happens to be the same process as for regular keys.
Creates a public key hash
Serialization of a master public key into its xpub version
Serialization of a master public key into its xpub version
Converts the public key to an address of the type specified as the second parameter
Checks a signature against a public key
Link to this section Functions
@spec derive_child!( %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }, integer() ) :: %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }
Simply calls from_derivation_path and directly returns the public key whatever the outcome.any() Will crash if the index is negative or greater than 0x7FFFFFFF
examples
Examples
iex> public_key = %BitcoinLib.Key.PublicKey{
...> key: <<0x252C616D91A2488C1FD1F0F172E98F7D1F6E51F8F389B2F8D632A8B490D5F6DA9::264>>,
...> chain_code: <<0x463223AAC10FB13F291A1BC76BC26003D98DA661CB76DF61E750C139826DEA8B::256>>
...> }
...> index = 0
...> BitcoinLib.Key.PublicKey.derive_child!(public_key, index)
%BitcoinLib.Key.PublicKey{
key: <<0x30204D3503024160E8303C0042930EA92A9D671DE9AA139C1867353F6B6664E59::264>>,
chain_code: <<0x05AAE71D7C080474EFAAB01FA79E96F4C6CFE243237780B0DF4BC36106228E31::256>>,
depth: 1,
index: 0,
parent_fingerprint: <<0x18C1259::32>>,
fingerprint: <<0x9680603F::32>>
}
@spec derive_child( %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }, integer() ) :: {:ok, %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }} | {:error, binary()}
Derives the nth child of a HD public key
Takes a public key, its chain code and the child's index Returns the child's public key and it's associated chain code
Inspired by https://learnmeabitcoin.com/technical/extended-keys#child-extended-key-derivation
examples
Examples
iex> public_key = %BitcoinLib.Key.PublicKey{
...> key: <<0x252C616D91A2488C1FD1F0F172E98F7D1F6E51F8F389B2F8D632A8B490D5F6DA9::264>>,
...> chain_code: <<0x463223AAC10FB13F291A1BC76BC26003D98DA661CB76DF61E750C139826DEA8B::256>>
...> }
...> index = 0
...> BitcoinLib.Key.PublicKey.derive_child(public_key, index)
{
:ok,
%BitcoinLib.Key.PublicKey{
key: <<0x30204D3503024160E8303C0042930EA92A9D671DE9AA139C1867353F6B6664E59::264>>,
chain_code: <<0x05AAE71D7C080474EFAAB01FA79E96F4C6CFE243237780B0DF4BC36106228E31::256>>,
depth: 1,
index: 0,
parent_fingerprint: <<0x18C1259::32>>,
fingerprint: <<0x9680603F::32>>
}
}
@spec deserialize!(binary()) :: %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }
Deserialization of a public key from its xpub version
values from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
examples
Examples
iex> "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
...> |> BitcoinLib.Key.PublicKey.deserialize!()
%BitcoinLib.Key.PublicKey{
fingerprint: <<0x3442193E::32>>,
key: <<0x339A36013301597DAEF41FBE593A02CC513D0B55527EC2DF1050E2E8FF49C85C2::264>>,
chain_code: <<0x873DFF81C02F525623FD1FE5167EAC3A55A049DE3D314BB42EE227FFED37D508::256>>,
depth: 0,
index: 0,
parent_fingerprint: <<0::32>>
}
@spec deserialize(binary()) :: {:ok, %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }, :mainnet | :testnet, :bip32 | :bip49 | :bip84} | {:error, String.t()}
Deserialization of a public key from its xpub version
values from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
examples
Examples
iex> "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
...> |> BitcoinLib.Key.PublicKey.deserialize()
{
:ok,
%BitcoinLib.Key.PublicKey{
fingerprint: <<0x3442193E::32>>,
key: <<0x339A36013301597DAEF41FBE593A02CC513D0B55527EC2DF1050E2E8FF49C85C2::264>>,
chain_code: <<0x873DFF81C02F525623FD1FE5167EAC3A55A049DE3D314BB42EE227FFED37D508::256>>,
depth: 0,
index: 0,
parent_fingerprint: <<0::32>>
},
:mainnet,
:bip32
}
@spec from_derivation_path( %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }, %BitcoinLib.Key.HD.DerivationPath{ account: term(), address_index: term(), change: term(), coin_type: term(), purpose: term(), type: term() } ) :: {:ok, %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }}
Derives a child public key, following a derivation path
examples
Examples
iex> public_key = %BitcoinLib.Key.PublicKey{
...> key: <<0x252C616D91A2488C1FD1F0F172E98F7D1F6E51F8F389B2F8D632A8B490D5F6DA9::264>>,
...> chain_code: <<0x463223AAC10FB13F291A1BC76BC26003D98DA661CB76DF61E750C139826DEA8B::256>>
...> }
...> {:ok, derivation_path} = BitcoinLib.Key.HD.DerivationPath.parse("M/44'/0'/0'/0/0")
...> BitcoinLib.Key.PublicKey.from_derivation_path(public_key, derivation_path)
{
:ok,
%BitcoinLib.Key.PublicKey{
key: <<0x29DCAFD0D7D67B13657CC9EE7C8976E141F20F0684BF3FC83CAF068E74186BCDC::264>>,
chain_code: <<0x162EEE68F7C3823CAF8BD2615A4A33633673CAAB66FF6F338FB0653FC59D462D::256>>,
depth: 5,
index: 0,
parent_fingerprint: <<0xCA2A5281::32>>,
fingerprint: <<0xAEAAB1AD::32>>
}
}
@spec from_private_key(%BitcoinLib.Key.PrivateKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term() }) :: %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }
Derives an extended public key from an extended private key. Happens to be the same process as for regular keys.
Inspired by https://learnmeabitcoin.com/technical/hd-wallets#master-private-key
examples
Examples
iex> %BitcoinLib.Key.PrivateKey{
...> key: <<0x081549973BAFBBA825B31BCC402A3C4ED8E3185C2F3A31C75E55F423E9629AA3::264>>,
...> chain_code: <<0x1D7D2A4C940BE028B945302AD79DD2CE2AFE5ED55E1A2937A5AF57F8401E73DD::256>>
...> }
...> |> BitcoinLib.Key.PublicKey.from_private_key()
%BitcoinLib.Key.PublicKey{
fingerprint: <<0xED104CB8::32>>,
key: <<0x0343B337DEC65A47B3362C9620A6E6FF39A1DDFA908ABAB1666C8A30A3F8A7CCCC::264>>,
uncompressed_key: <<0x0443b337dec65a47b3362c9620a6e6ff39a1ddfa908abab1666c8a30a3f8a7ccccfc24a7914950b6405729a9313cec6ae5bb4a082f92d05ac49df4b6dd8387bfeb::520>>,
chain_code: <<0x1D7D2A4C940BE028B945302AD79DD2CE2AFE5ED55E1A2937A5AF57F8401E73DD::256>>,
depth: 0,
index: 0,
parent_fingerprint: <<0::32>>
}
@spec hash(%BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }) :: <<_::160>>
Creates a public key hash
examples
Examples
iex> %BitcoinLib.Key.PublicKey{
...> key: <<0x02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737::264>>
...> }
...> |> BitcoinLib.Key.PublicKey.hash()
<<0x93ce48570b55c42c2af816aeaba06cfee1224fae::160>>
@spec serialize!( %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }, :mainnet | :testnet, :bip32 | :bip49 | :bip84 ) :: binary()
Serialization of a master public key into its xpub version
values from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
examples
Examples
iex> %BitcoinLib.Key.PublicKey{
...> key: <<0x339A36013301597DAEF41FBE593A02CC513D0B55527EC2DF1050E2E8FF49C85C2::264>>,
...> chain_code: <<0x873DFF81C02F525623FD1FE5167EAC3A55A049DE3D314BB42EE227FFED37D508::256>>,
...> depth: 0,
...> index: 0,
...> parent_fingerprint: <<0::32>>
...> }
...> |> BitcoinLib.Key.PublicKey.serialize!()
"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
@spec serialize( %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }, :mainnet | :testnet, :bip32 | :bip49 | :bip84 ) :: {:ok, binary()} | {:error, binary()}
Serialization of a master public key into its xpub version
values from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
examples
Examples
iex> %BitcoinLib.Key.PublicKey{
...> key: <<0x339A36013301597DAEF41FBE593A02CC513D0B55527EC2DF1050E2E8FF49C85C2::264>>,
...> chain_code: <<0x873DFF81C02F525623FD1FE5167EAC3A55A049DE3D314BB42EE227FFED37D508::256>>,
...> depth: 0,
...> index: 0,
...> parent_fingerprint: <<0::32>>
...> }
...> |> BitcoinLib.Key.PublicKey.serialize()
{
:ok,
"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
}
Converts the public key to an address of the type specified as the second parameter
Defaults to bech32 address type
examples
Examples
iex> %BitcoinLib.Key.PublicKey{
...> key: <<0x3EB181FB7B5CF63D82307188B20828B83008F2D2511E5C6EDCBE171C63DD2CBC1::264>>,
...> chain_code: <<0x581F15490635CF8CD0AEEF556562F52C60179E0E87E0EA92977E364D949DC2E4::256>>,
...> depth: 0x5,
...> index: 0x0
...> }
...> |> BitcoinLib.Key.PublicKey.to_address(:p2pkh)
"1BRjWnoAVg3EASJHex5YeyDWC1zZ4CA5vc"
@spec validate_signature(bitstring(), bitstring(), %BitcoinLib.Key.PublicKey{ chain_code: term(), depth: term(), fingerprint: term(), index: term(), key: term(), parent_fingerprint: term(), uncompressed_key: term() }) :: boolean()
Checks a signature against a public key
examples
Examples
iex> message = <<0xf05750d5bd2c2b4bbbd57cb07082ba5aabb41863e33bf2cd187a9adb1443dbc3::256>>
...> public_key = %BitcoinLib.Key.PublicKey{
...> key: <<0x03f0e5a53db9f85e5b2eecf677925ffe21dd1409bcfe9a0730404053599b0901e5::264>>
...> }
...> <<0x30440220032a1544f599bf29981851e826e8a6f7c036958ba3543cf9778a0756dfc425f6022067eec131c0d73825633c0fddce1abfb14bb26bc9e0d6e9d644a77361f74cb55c::560>>
...> |> BitcoinLib.Key.PublicKey.validate_signature(message, public_key)
true