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

Implementation of Bech32 addresses

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

Sources:

Link to this section Summary

Functions

Segment a bech32 address into its hrp and encoding values

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

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

Segment a bech32 address into its hrp and encoding values

examples

Examples

iex> "tb1qcq670zweall6zz4f96flfrefhr8myfxz9ll9l2"
...> |> BitcoinLib.Address.Bech32.destructure()
{:ok, <<0xc035e789d9efffa10aa92e93f48f29b8cfb224c2::160>>, :p2wpkh, :testnet}
Link to this function

from_public_key(public_key, network \\ :mainnet)

View Source
@spec from_public_key(BitcoinLib.Key.PublicKey.t(), :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_pub_key(data, network \\ :mainnet)

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

Creates either of these Bech32 native address types

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

examples

Examples

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

iex> <<0x00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262::272>>
...> |> BitcoinLib.Address.Bech32.from_script_pub_key(: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