View Source BitcoinLib.Address (BitcoinLib v0.4.7)

Bitcoin address management

Inspired by https://learnmeabitcoin.com/technical/public-key-hash

Link to this section Summary

Functions

Extracts the public key hash from an address, and make sure the checkum is ok

Turns a public key into an address of the specified format

Convert public key hash into a P2PKH Bitcoin address.

Applies the address's checksum to make sure it's valid

Link to this section Functions

Link to this function

destructure(bech32_address)

View Source
@spec destructure(binary()) ::
  {:ok, <<_::272>> | <<_::160>>, :p2pkh | :p2sh | :p2wpkh, :mainnet | :testnet}
  | {:error, binary()}

Extracts the public key hash from an address, and make sure the checkum is ok

examples

Examples

iex> address = "tb1qxrd42xz49clfrs5mz6thglwlu5vxmdqxsvpnks"
...> BitcoinLib.Address.destructure(address)
{:ok, <<0x30db5518552e3e91c29b1697747ddfe5186db406::160>>, :p2wpkh, :testnet}

iex> address = "mwYKDe7uJcgqyVHJAPURddeZvM5zBVQj5L"
...> BitcoinLib.Address.destructure(address)
{:ok, <<0xafc3e518577316386188af748a816cd14ce333f2::160>>, :p2pkh, :testnet}
Link to this function

from_public_key(public_key, script_type, network \\ :mainnet)

View Source
@spec from_public_key(
  BitcoinLib.Key.PublicKey.t(),
  :p2pkh | :p2sh | :p2wpkh,
  :mainnet | :testnet
) ::
  binary()

Turns a public key into an address of the specified format

examples

Examples

iex> %BitcoinLib.Key.PublicKey{
...>   key: <<0x0343B337DEC65A47B3362C9620A6E6FF39A1DDFA908ABAB1666C8A30A3F8A7CCCC::264>>
...> }
...> |> BitcoinLib.Address.from_public_key(:p2wpkh, :mainnet)
"bc1qa5gyew808tdta3wjh6qh3jvcglukjsnfg0qx4u"
Link to this function

from_public_key_hash(public_key_hash, network \\ :mainnet)

View Source
@spec from_public_key_hash(binary(), :mainnet | :testnet) :: bitstring()

Convert public key hash into a P2PKH Bitcoin address.

Details can be found here: https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses

examples

Examples

iex> <<0x6ae201797de3fa7d1d95510f50c1a9c50ce4cc36::160>>
...> |> BitcoinLib.Address.from_public_key_hash()
"1Ak9NVPmwCHEpsSWvM6cNRC7dsYniRmwMG"
Link to this function

from_script_hash(public_key_hash, network \\ :mainnet)

View Source
@spec valid?(binary()) :: boolean()

Applies the address's checksum to make sure it's valid

examples

Examples

iex> "tb1qxrd42xz49clfrs5mz6thglwlu5vxmdqxsvpnks"
...> |> BitcoinLib.Address.valid?()
true