View Source BitcoinLib.Address.Bech32 (BitcoinLib v0.3.0)

Implementation of Bech32 addresses

BIP173: https://en.bitcoin.it/wiki/BIP_0173

Sources:

Link to this section Summary

Functions

Creates a Bech32 address, which is starting by bc1, out of an Extended Public Key

Creates a Bech32 address, which is starting by bc1, out of a 160 bits public key hash

Creates either of these Bech32 native address types

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

Link to this section Functions

Link to this function

from_public_key(public_key, network \\ :mainnet)

View Source
@spec from_public_key(
  %BitcoinLib.Key.PublicKey{
    chain_code: term(),
    depth: term(),
    fingerprint: term(),
    index: term(),
    key: term(),
    parent_fingerprint: term(),
    uncompressed_key: term()
  },
  :mainnet | :testnet
) :: binary()

Creates a Bech32 address, which is starting by bc1, out of an Extended Public Key

examples

Examples

iex> %BitcoinLib.Key.PublicKey{
...>  key: <<0x0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798::264>>,
...>  chain_code: 0
...> } |> BitcoinLib.Address.Bech32.from_public_key()
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"
Link to this function

from_public_key_hash(public_key_hash, network \\ :mainnet)

View Source
@spec from_public_key_hash(<<_::160>>, :mainnet | :testnet) :: binary()

Creates a Bech32 address, which is starting by bc1, out of a 160 bits public key hash

examples

Examples

iex> <<0x751e76e8199196d454941c45d1b3a323f1433bd6::160>>
...> |> BitcoinLib.Address.Bech32.from_public_key_hash()
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"
Link to this function

from_script_hash(data, network \\ :mainnet)

View Source
@spec from_script_hash(<<_::176>>, :mainnet | :testnet) :: binary()
@spec from_script_hash(<<_::272>>, :mainnet | :testnet) :: binary()

Creates either of these Bech32 native address types

  • pay to witness public key hash address out of a 20 bytes pub key
  • pay to witness script hash address out of a 32 bytes script hash

examples

Examples

iex> <<0x001400d21980ae3e9641db6897dad7b8b69b07d9aaac::176>>
...> |> BitcoinLib.Address.Bech32.from_script_hash(:testnet)
"tb1qqrfpnq9w86tyrkmgjldd0w9knvran24v2hzspx"

iex> <<0x00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262::272>>
...> |> BitcoinLib.Address.Bech32.from_script_hash(:testnet)
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7"
@spec valid?(binary()) :: boolean()

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

examples

Examples

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