Skip to content

test: Support decoding segwit address in address_to_scriptpubkey() - #27269

Merged
fanquake merged 2 commits into
bitcoin:masterfrom
ismaelsadeeq:add-bech32-testnet-address-to-scriptpubkey
Mar 24, 2023
Merged

fanquake merged 2 commits into
bitcoin:masterfrom
ismaelsadeeq:add-bech32-testnet-address-to-scriptpubkey

Conversation

@ismaelsadeeq

Copy link
Copy Markdown
Member

rpc_scantxoutset.py sendtodestination only sends to legacy addresses and scriptPubkeys because wallet.py address_to_scriptpubkey does not support conversion of segwit address.

This update enables address_to_scriptpubkey to support the conversion of testnet segwit addresses to scriptPubkeys.

This change will enable rpc_scantxoutset.py ScantxoutsetTest to have more test coverage by adding more sendtodestination calls with bech32 and bech32m testnet addresses, then test the bech32 and bech32m derivation subsets UTXO amount in Test extended key derivation.

I will add the test coverage in a subsequent Pull request.

@DrahtBot

DrahtBot commented Mar 16, 2023 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK josibake, theStack, willcl-ark

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@DrahtBot DrahtBot added the Tests label Mar 16, 2023
@willcl-ark

Copy link
Copy Markdown
Member

Concept ACK

Thanks for working on this. Increasing the address to scriptpubkey decoding tests to include bech32 seems desirable. I think this would mean that we could remove the todo on L427 of wallet.py too?

The way the equivalent base58 function test_base58encodedecode() is implemented is slightly different from the new test_bech32_decode():

  1. It checks two functions simultaneously, base58_to_bytes() and byte_to_base58(), whereas your first commit only adds bech32_to_bytes() and not the equivalent byte_to_bech32().
  2. The test vectors provided to the new test are raw address strings (and raw bytes) vs hex-encoded bytestrings in the base58 version.

I think for completion it could be worth adding the equivalent byte_to_bech32() function and matching the test vector format used in the base58 checks, seeing as you are here making this change now.

Woudl be curious what others think on this though...

@ismaelsadeeq

ismaelsadeeq commented Mar 17, 2023 •

Copy link
Copy Markdown
Member Author

Yes, I agree the todo can be removed since there are no other address types to add.

  • I think there is no need to add byte_to_bech32(), there is an existing encode_segwit_address() function that does the same thing.
  • I can use bech32_to_bytes() and encode_segwit_address() with hex-encoded byte string and version as test vectors in test_bech32_decode() just like in test_base58encodedecode().

@ismaelsadeeq
ismaelsadeeq force-pushed the add-bech32-testnet-address-to-scriptpubkey branch from 4b95be0 to f0426fe Compare March 17, 2023 12:39
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

Thanks for reviewing, I made the update.

@josibake josibake left a comment

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.

Concept ACK

Thanks for tackling a TODO! I'm guessing there are other places in the functional tests where we would want coverage for more than just base58 addresses, so definitely a worthwhile change. Overall, looks good! I left a few suggestions and style comments in my review. Mainly:

  • I think for consistency you can use the decode_segwit_address function from segwit_addr.py. This avoids having the logic for decoding bech32 addresses in multiple places.
  • Even though a lot of these files don't adhere to PEP8 guidelines, we want to make sure any new code introduced does follow the guidelines. What I typically do is run flake8 on the file I plan to work on before making any changes and then run flake8 again after my changes to make sure all of my new changes follow PEP8 guidelines

@ismaelsadeeq
ismaelsadeeq force-pushed the add-bech32-testnet-address-to-scriptpubkey branch from f0426fe to 479e3e7 Compare March 20, 2023 11:44
@ismaelsadeeq
ismaelsadeeq force-pushed the add-bech32-testnet-address-to-scriptpubkey branch from 479e3e7 to 9aac917 Compare March 20, 2023 12:05
@josibake

Copy link
Copy Markdown
Member

ACK 9aac917

Thanks for incorporating the feedback and addressing the style comments! Looks good.

@ismaelsadeeq
ismaelsadeeq force-pushed the add-bech32-testnet-address-to-scriptpubkey branch from 9aac917 to becdc60 Compare March 22, 2023 12:12
@ismaelsadeeq
ismaelsadeeq force-pushed the add-bech32-testnet-address-to-scriptpubkey branch from becdc60 to 9e377f4 Compare March 22, 2023 13:07
@josibake

Copy link
Copy Markdown
Member

ACK 9e377f4

nice work!

@fanquake
fanquake requested a review from theStack March 22, 2023 17:42

@theStack theStack left a comment

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.

Concept ACK

Thanks for tackling this, and warm welcome as a new contributor!
With this change, we don't have to issue getaddressinfo or validateaddress RPCs anymore to convert from address to scriptPubKey.

To ensure that the helper address_to_scriptpubkey works as expected, I think it would be good to call it least once with a segwit address in a functional test. One possible candidate is e.g. replacing the RPC call in this line:

spk = bytes.fromhex(w.getaddressinfo(addr)['scriptPubKey'])

This reveals that the added decoding support currently doesn't work as expected, see review comment below.

Adds bech32_to_bytes() which can decode a bech32 address and return the
version as an `int` and the payload in bytes.

bech32_to_bytes() is used by the test_bech32_decode unit test to test
decoding of segwit addresses.
This permits functional tests to decode bech32 addresses to scriptpubkeys.
@ismaelsadeeq
ismaelsadeeq force-pushed the add-bech32-testnet-address-to-scriptpubkey branch from 9e377f4 to d178082 Compare March 23, 2023 11:09
@josibake

Copy link
Copy Markdown
Member

ACK d178082

I tested this by making sure the unittests run and also modifying test/functional/wallet_fast_rescan.py to use address_to_scriptpubkey, per @theStack 's suggestion. Works great!

I'd love to see a follow-up PR targeting areas where we are a) only testing base58 addresses but should be testing bech32 as well and b) anywhere we are making a getaddressinfo RPC call to get the spk to instead use address_to_scriptpubkey

@fanquake
fanquake requested a review from theStack March 23, 2023 11:36

@theStack theStack left a comment

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.

ACK d178082 ✔️

I'd love to see a follow-up PR targeting areas where we are a) only testing base58 addresses but should be testing bech32 as well and b) anywhere we are making a getaddressinfo RPC call to get the spk to instead use address_to_scriptpubkey

👍 If anyone wants to work on this, feel free to tag me as reviewer.

@josibake

Copy link
Copy Markdown
Member

review-beg: @willcl-ark

@willcl-ark willcl-ark left a comment

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.

ACK d178082

I also tested two of the BIP173 Test Vectors:

check_bech32_decode(bytes.fromhex('0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433'), 1)
check_bech32_decode(bytes.fromhex('00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262'), 1)

...which also passed. In doing so I noticed the hardcoding of human readable part hrp in address.py#L196 was limiting which test vectors I could use, and I think un-hardcoding this would be great to do as a followup.


def bech32_to_bytes(address):
hrp = address.split('1')[0]
if hrp not in ['bc', 'tb', 'bcrt']:

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.

I think this is totally fine as is here for now, but in the future we may want to put these all in a list/enum


def test_bech32_decode(self):
def check_bech32_decode(payload, version):
hrp = "tb"

@willcl-ark willcl-ark Mar 24, 2023 •

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.

Instead of hardcoding the hrp here, why not have check_bech32_decode() take hrp as a parameter, then we can add bc1 test vectors instead of being limited to tb1?

edit: I don't consider this a blocker or needing reponse before merging, but will be nice to consider in the future.

@fanquake
fanquake merged commit 873a506 into bitcoin:master Mar 24, 2023
fanquake added a commit to bitcoin-core/gui that referenced this pull request Mar 29, 2023
…d of RPC call

e47ce42 refactor: use address_to_scriptpubkey to retrieve addresses scriptpubkey (ismaelsadeeq)
4142d19 refactor: move address_to_scriptpubkey to address.py (ismaelsadeeq)

Pull request description:

  PR #27269 enables the function address_to_scriptpubkey() to decode all address types and return their corresponding scriptpubkeys. As a result, there is no longer any need to call getaddressinfo or validateaddress RPCs in order to retrieve an address scriptpubkey, as explained in the comments on this pull request (see bitcoin/bitcoin#27269 (review) and bitcoin/bitcoin#27269 (comment)).

  Instead of using RPC calls, this update replaces the process of obtaining an address scriptPubkey with the address_to_scriptpubkey method, resulting in improved performance for functional tests.

ACKs for top commit:
  josibake:
    re-ACK bitcoin/bitcoin@e47ce42
  theStack:
    ACK e47ce42 🌱

Tree-SHA512: 05285349a7d5ce7097b8f2582e573a5135c6deef85ea9936f68f6ce94e9ebb1d84d94f7fc7e5ed833a698e01585addd80deb52e6338f8aee985bf14db45417d2
@bitcoin bitcoin locked and limited conversation to collaborators Mar 23, 2024
@ismaelsadeeq
ismaelsadeeq deleted the add-bech32-testnet-address-to-scriptpubkey branch June 27, 2024 11:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants