Skip to content

Add decode_type method to BitcoinEx.Address#12

Merged
philipglazman merged 3 commits into
masterfrom
script-types
Jun 12, 2020
Merged

Add decode_type method to BitcoinEx.Address#12
philipglazman merged 3 commits into
masterfrom
script-types

Conversation

@philipglazman
Copy link
Copy Markdown
Contributor

Adds a decode_type method to BitcoinEx.Address in order to return the type of address.

Split the current is_valid?() method in BitcoinEx.Address for segwit addresses into two methods and add a guard to check for witness program length.

We previously were returning is_valid true for segwit addresses that are not infact valid for mainnet because they were v1 segwit addresses. Think we should return false for for v1 segwit addresses.

@philipglazman philipglazman requested a review from kafaichoi June 10, 2020 20:51
Comment thread lib/address.ex Outdated
@spec decode_type(String.t(), Bitcoinex.Network.network_name()) ::
{:ok, address_type} | {:error, term()}
def decode_type(address, network_name) do
case Enum.find(@address_type, &is_valid?(address, network_name, &1)) do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should probably rename this to @address_types since it's a list

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, done

Comment thread lib/address.ex Outdated
end

@spec decode_type(String.t(), Bitcoinex.Network.network_name()) ::
{:ok, address_type} | {:error, term()}
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.

the only possible error is :decode_error. Let's change term() to :decode_error that help people using dialyzer for type checking.

Comment thread lib/address.ex Outdated
case Segwit.decode_address(address) do
{:ok, {^network_name, _, _}} ->
true
{:ok, {^network_name, witness_version, witness_program}} ->
Copy link
Copy Markdown
Contributor

@kafaichoi kafaichoi Jun 11, 2020

Choose a reason for hiding this comment

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

a cleaner way will be

       {:ok, {^network_name, 0, witness_program}} when length(witness_program) == 20 -> 
          true

So we can get ride of one unncessary if expression before. Same for line 61

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right! Changed

@philipglazman philipglazman requested a review from kafaichoi June 11, 2020 17:49
@philipglazman philipglazman merged commit 53634d9 into master Jun 12, 2020
@philipglazman philipglazman deleted the script-types branch June 12, 2020 17:33
SachinMeier added a commit that referenced this pull request Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants