Bitcoin Core Test Cases: PKH() Nested in TR() is Allowed
The Bitcoin Core test suite has been updated to allow pkh()
nested within tr()
in certain cases. This feature was noted as invalid in the original Test Vector, but it is now enabled.
What is PKH() and TR()?
pkh()
is a command that converts a key (address) from one format to another (e.g., from Bitcoin’s script format to human-readable text). tr()
is a command that reverses the order of characters in a string. In the context of Bitcoin, both commands are used for formatting and debugging purposes.
The original Test Vector
In the original Test Vector, the following code was noted as invalid:
// Test Vector: PKH() nested in TR()
int main() {
printf("%s\n", pkh(tr("addr1M4nLp9zJfRt2F7VwQqG5dXxSTK3yWYrP"))); // invalid
return 0;
}
The tr()
command was used to convert the address from script format to text, but then the pkh()
command was used to convert the resulting string back to Bitcoin’s script format. This nested usage of both commands was noted as invalid.
Update to Bitcoin Core Test Suite
However, in the updated test suite, this behavior is no longer considered an error. The code in question can now be rewritten without using tr()
:
// Test Vector: PKH() nested in TR() (updated)
int main() {
printf("%s\n", pkh("addr1M4nLp9zJfRt2F7VwQqG5dXxSTK3yWYrP")); // valid
return 0;
}
In this updated version, the pkh()
command is used directly to convert the address to script format, without using the tr()
command.
Conclusion
The Bitcoin Core test suite has been updated to allow pkh()
nested within tr()
. This feature can be used to simplify debugging and formatting operations, but it may introduce performance issues if used excessively. It is essential to review the code carefully before updating any existing tests or applications that use this behavior.