Closed l0rb closed 6 years ago
Hi, thank you for pointing out this ambiguity. address()
is meant to return an address for scriptPubkey
s that support it (in our case, we consider only P2PKH
, P2SH
, P2WPKH
and P2WSH
to properly support an address). to_address(segwit_version=None)
is instead implemented for any script type and is meant to produce a P2(W)SH
address for the script.
I can see the semantic ambiguity, so instead of fixing this as proposed in #28, I'd suggest renaming the two methods to something more appropriate. Any suggestion?
I think it would be best to put the intention in the name to avoid the confusion and rename to_address
to to_p2sh_address
or to_scripthash_address
?
The
to_address()
method on theP2pkhScript
returns a p2sh address, while theaddress()
method returns the expected p2pkh address. This happens because the class itself does not implement theto_address()
and only inherits it fromScriptPubKey
where it is hardcoded to always give a p2sh if it's not segwit. I can see two ways to fix this, either in the parent or the child class: In the parent class change theaddress()
method to default to p2sh and change theelse
part in to_address toreturn self.address()
. This preserves the old behaviour for child classes that do not implementaddress()
but fixes it for those that do. Or change the child class(es) to implementto_address()
.Edit: created a PR with a proposed solution (only touches parent class)
Below a minimal test case that shows the unexpected/surprising behaviour: