Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ defmodule Bitcoinex.Address do
Base58.encode(<<decimal_prefix>> <> script_hash)
end

def encode(script_hash, network_name, :p2wpkh) do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename script_hash to pubkey_hash? Otherwise LGTM

case Segwit.encode_address(network_name, 0, :binary.bin_to_list(script_hash)) do
{:ok, address} -> address
{:error, _} -> ""
end
end

@doc """
Checks if the address is valid.
Both encoding and network is checked.
Expand Down
9 changes: 9 additions & 0 deletions test/address_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,14 @@ defmodule Bitcoinex.AddressTest do
script_hash = Base.decode16!("6d77fa9de297e9c536c6b23cfda1a8450bb5f765", case: :lower)
assert "3BfqJjn7H2jsbKd2NVHGP4sQWQ2bQWBRLv" == Address.encode(script_hash, :mainnet, :p2sh)
end

test "return true for encoding p2wpkh" do
script_hash = "751e76e8199196d454941c45d1b3a323f1433bd6"

script_hash = Base.decode16!(script_hash, case: :lower)

assert "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" ==
Address.encode(script_hash, :mainnet, :p2wpkh)
end
end
end