Skip to content

rpcserver: assert network for validateaddress rpc.#963

Merged
davecgh merged 1 commit intodecred:masterfrom
dnldd:validateaddressrpc
Jan 10, 2018
Merged

rpcserver: assert network for validateaddress rpc.#963
davecgh merged 1 commit intodecred:masterfrom
dnldd:validateaddressrpc

Conversation

@dnldd
Copy link
Copy Markdown
Member

@dnldd dnldd commented Jan 7, 2018

resolves #962

rpcserver.go Outdated
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.

is this correct? should it return the error?

Copy link
Copy Markdown
Member

@davecgh davecgh Jan 9, 2018

Choose a reason for hiding this comment

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

It definitely should not return an error.

rpcserver.go Outdated
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.

could remove the if and just do
result.Address = addr.EncodeAddress()
result.IsValid = addr.IsForNet(s.server.chainParams)

@dnldd dnldd force-pushed the validateaddressrpc branch from ea40f2c to 57ba790 Compare January 7, 2018 23:51
@dnldd
Copy link
Copy Markdown
Member Author

dnldd commented Jan 7, 2018

@dajohi updated.

rpcserver.go Outdated
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.

This would be a breaking change. It is supposed to return an empty result (where the isvalid field is to to false as the comment indicates) when it fails to decode, not an error.

rpcserver.go Outdated
Copy link
Copy Markdown
Member

@davecgh davecgh Jan 8, 2018

Choose a reason for hiding this comment

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

I think this should return an error when it's not for the current network as opposed to just being false. It is still a valid address, just not for the current network, so just returning false in that case would be misleading and incorrect.

It should match other handlers which do:

	if !addr.IsForNet(s.server.chainParams) {
		return nil, rpcAddressKeyError("Wrong network: %v", addr)
	}
}

It should also go just after the call to DecodeAddress, before spending cycles encoding the address into the result.

@dnldd dnldd force-pushed the validateaddressrpc branch from 57ba790 to 5aac813 Compare January 8, 2018 23:11
rpcserver.go Outdated
Copy link
Copy Markdown
Member

@davecgh davecgh Jan 10, 2018

Choose a reason for hiding this comment

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

Alright, after further discussion with the team, it seems that everyone would prefer it to not error in this case so it is consistent with both dcrwallet and other code in the ecosystem which expects that behavior.

Would you mind changing this back to the following?

result.IsValid = addr.IsForNet(s.server.chainParams)

Sorry for the back and forth!

@dnldd dnldd force-pushed the validateaddressrpc branch from 5aac813 to 7c3ec8c Compare January 10, 2018 01:07
rpcserver.go Outdated
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.

This shouldn't be set when it's for the wrong network.

Notice the difference between dcrwallet and dcrd below.

dcrd:

$ dcrctl validateaddress TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6n
{
  "isvalid": false,
  "address": "TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6n"
}

dcrwallet:

$ dcrctl --wallet validateaddress TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6n
{
  "isvalid": false,
}

Copy link
Copy Markdown
Member

@davecgh davecgh Jan 10, 2018

Choose a reason for hiding this comment

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

The following diff against master will result in the desired behavior:

diff --git a/rpcserver.go b/rpcserver.go
index 4ab9bee4..d1ec41c0 100644
--- a/rpcserver.go
+++ b/rpcserver.go
@@ -5566,7 +5566,7 @@ func handleValidateAddress(s *rpcServer, cmd interface{}, closeChan <-chan struc

        result := dcrjson.ValidateAddressChainResult{}
        addr, err := dcrutil.DecodeAddress(c.Address)
-       if err != nil {
+       if err != nil || !addr.IsForNet(s.server.chainParams) {
                // Return the default value (false) for IsValid.
                return result, nil
        }

@dnldd dnldd force-pushed the validateaddressrpc branch from 7c3ec8c to 609e489 Compare January 10, 2018 01:31
Copy link
Copy Markdown
Member

@davecgh davecgh left a comment

Choose a reason for hiding this comment

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

I've tested this on both mainnet and testnet with the following results:

Mainnet:

Address Description Expected Valid? Result
18cBEMRxXHqzWWCxZNtU91F5sbUNKhL5PX Valid Bitcoin p2pkh mainnet false {"isvalid": false}
18cBEMRxXHqzWWCxZNtU91F5sbUNKhL5PY Invalid Bitcoin p2pkh mainnet false {"isvalid": false}
Dsb5EYhZBjGCcVrciBjbNLg3AiypAUxW76n Valid Decred p2pkh mainnet true {"isvalid": true, "address": "Dsb5EYhZBjGCcVrciBjbNLg3AiypAUxW76n"}
Dsb5EYhZBjGCcVrciBjbNLg3AiypAUxW76o Invalid Decred p2pkh mainnet false {"isvalid": false}
Dcur2mcGjmENx4DhNqDctW5wJCVyT3Qeqkx Valid Decred p2sh mainnet true {"isvalid": true, "address": "Dcur2mcGjmENx4DhNqDctW5wJCVyT3Qeqkx"}
Dcur2mcGjmENx4DhNqDctW5wJCVyT3Qeqky Invalid Decred p2sh mainnet false {"isvalid": false}
TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6n Valid Decred p2pkh testnet false {"isvalid": false}
TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6o Invalid Decred p2pkh testnet false {"isvalid": false}

Testnet:

Address Description Expected Valid? Result
Dsb5EYhZBjGCcVrciBjbNLg3AiypAUxW76n Valid Decred p2pkh mainnet false {"isvalid": false}
Dsb5EYhZBjGCcVrciBjbNLg3AiypAUxW76o Invalid Decred p2pkh mainnet false {"isvalid": false}
Dcur2mcGjmENx4DhNqDctW5wJCVyT3Qeqkx Valid Decred p2sh mainnet false {"isvalid": false}
Dcur2mcGjmENx4DhNqDctW5wJCVyT3Qeqky Invalid Decred p2sh mainnet false {"isvalid": false}
TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6n Valid Decred p2pkh testnet true {"isvalid": true, "address": "TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6n"}
TsYEVnkLPSniXpqUNqPZKEktCLkBzjXjK6o Invalid Decred p2pkh testnet false {"isvalid": false}

The results are all as expected.

OK

@davecgh
Copy link
Copy Markdown
Member

davecgh commented Jan 10, 2018

I will merge this as soon as @dajohi approves since it's blocked due to his requested changes.

@davecgh davecgh merged commit 36c6c7f into decred:master Jan 10, 2018
@dnldd dnldd deleted the validateaddressrpc branch June 4, 2018 23:40
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.

validateaddress issue

3 participants