0

I'm looking into bc1 addresses and I want to verify that the method I've implemented for generating bc1 addresses is correct. While I'm confident that I'm generating correct bc1 addresses I just had a thought that pumping the serialized bc1 address through the function to generate bc1 addresses is probably a bad idea. So, can anyone recommend to me the process by which we deserialize a bc1 address?

8
  • 1
    Out of curiosity, why are you reimplementing all of this instead of using an existing library that does all of this out of the box? If you are looking to do scientific work, surely you will want to look at usage patterns. It’s not clear to me how that is furthered by having your own implementation to parse the blockchain data. Are you sure that your Professor knew how much effort it would be to reimplement all of this and that they would still think it’s a good use of your time to do it from scratch if they were caught up on your progress?
    – Murch
    Mar 8, 2023 at 15:43
  • 1
    If they were presented between the choice of accurate data (which to clean up will probably be more than enough work!) and thorough investigation of the data, or some data made with a home-cooked blockchain parser based on regular expressions and a lot of assumptions taking up most of your time and a hasty investigation tacked on at the end, don’t you think the first would be more attractive?
    – Murch
    Mar 8, 2023 at 15:49
  • 1
    This all sounds like a heavy case of not-invented-here syndrome to me. All of the libraries we mentioned are opensource, most of them have very permissive licenses—just saying.
    – Murch
    Mar 8, 2023 at 15:50
  • @Murch I think the conundrum is that the professor wants this data parsed in real time v. from the blockchain. Do you have any libraries you suggest? Mar 8, 2023 at 16:01
  • 2
    @bitcoinluvr6969 Perhaps you should open a question about that then, explaining exactly what you're trying to do ("parse bitcoin addresses from transactions received through the P2P protocol", with clarification of your constraints like what programming language you need to use) rather than half a dozen XY problem questions (xyproblem.info). Mar 8, 2023 at 16:14

1 Answer 1

3

bc1 addresses are specified in BIP173 and BIP350. You should read and implement the algorithms there, and verify your implementation against the test vectors. There are reference implementations too that you can look at for inspiration.

Roughly the process is:

  • Check the prefix (before the 1) is as expected (bc for mainnet, tb for testnet, ...).
  • Expand the prefix to base 32 numbers.
  • Convert the data section to base 32 numbers.
  • Verify the checksum on the expanded/converted data, for both bech32 and bech32m, and remember which one succeeded. If both fail, give up.
  • Drop the last 6 values of the converted data section, interpret the first value as witness version, and convert the rest from 5 bit/symbol to 8 bit/symbol.
  • If the witness version is 0, check that the checksum matched the bech32 algorithm. If the witness version is 1..16, check that the checksum matched the bech32m algorithm.
  • Output the script OP_n (with n the witness version) followed by a push of the to-8-bit-converted data. Note that OP_0 is encoded as 0x00, while OP_1 through OP_16 are encoded as 0x51 through 0x60.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.