Why does the decode function of the segwit bech32 encoder/decoder take a hrp (human readable part) as input?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















Looking at the code referenced in bip-0173, specifically for example here:
https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js



 function decode (hrp, addr) {
var dec = bech32.decode(addr);
if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16)
return null;

...


I understand that this is an error checking statement, but why check a (user?) inputted hrp vs the decoded hrp from bech32.decode?



Also I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?










share|improve this question


























    1















    Looking at the code referenced in bip-0173, specifically for example here:
    https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js



     function decode (hrp, addr) {
    var dec = bech32.decode(addr);
    if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16)
    return null;

    ...


    I understand that this is an error checking statement, but why check a (user?) inputted hrp vs the decoded hrp from bech32.decode?



    Also I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?










    share|improve this question
























      1












      1








      1








      Looking at the code referenced in bip-0173, specifically for example here:
      https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js



       function decode (hrp, addr) {
      var dec = bech32.decode(addr);
      if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16)
      return null;

      ...


      I understand that this is an error checking statement, but why check a (user?) inputted hrp vs the decoded hrp from bech32.decode?



      Also I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?










      share|improve this question














      Looking at the code referenced in bip-0173, specifically for example here:
      https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js



       function decode (hrp, addr) {
      var dec = bech32.decode(addr);
      if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16)
      return null;

      ...


      I understand that this is an error checking statement, but why check a (user?) inputted hrp vs the decoded hrp from bech32.decode?



      Also I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?







      segregated-witness bech32-address






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 7 at 21:21









      kawthuldrokkawthuldrok

      488




      488




















          3 Answers
          3






          active

          oldest

          votes


















          5














          The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.



          We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.



          Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.






          share|improve this answer






























            3















            I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?




            No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).






            share|improve this answer























            • Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

              – kawthuldrok
              Jan 9 at 17:37


















            0














            From BIP173 > Specification > Segwit address format > Decoding:




            Software interpreting a segwit address:



            • MUST verify that the human-readable part is "bc" for mainnet and "tb" for testnet.

            • MUST verify that the first decoded data value (the witness version) is between 0 and 16, inclusive






            share|improve this answer
























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "308"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              noCode: true, onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fbitcoin.stackexchange.com%2fquestions%2f83454%2fwhy-does-the-decode-function-of-the-segwit-bech32-encoder-decoder-take-a-hrp-hu%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.



              We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.



              Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.






              share|improve this answer



























                5














                The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.



                We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.



                Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.






                share|improve this answer

























                  5












                  5








                  5







                  The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.



                  We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.



                  Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.






                  share|improve this answer













                  The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.



                  We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.



                  Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 7 at 21:39









                  Pieter WuillePieter Wuille

                  46k395155




                  46k395155





















                      3















                      I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?




                      No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).






                      share|improve this answer























                      • Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

                        – kawthuldrok
                        Jan 9 at 17:37















                      3















                      I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?




                      No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).






                      share|improve this answer























                      • Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

                        – kawthuldrok
                        Jan 9 at 17:37













                      3












                      3








                      3








                      I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?




                      No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).






                      share|improve this answer














                      I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?




                      No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 7 at 22:15









                      G. MaxwellG. Maxwell

                      3,8701634




                      3,8701634












                      • Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

                        – kawthuldrok
                        Jan 9 at 17:37

















                      • Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

                        – kawthuldrok
                        Jan 9 at 17:37
















                      Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

                      – kawthuldrok
                      Jan 9 at 17:37





                      Ah, I see now. Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.

                      – kawthuldrok
                      Jan 9 at 17:37











                      0














                      From BIP173 > Specification > Segwit address format > Decoding:




                      Software interpreting a segwit address:



                      • MUST verify that the human-readable part is "bc" for mainnet and "tb" for testnet.

                      • MUST verify that the first decoded data value (the witness version) is between 0 and 16, inclusive






                      share|improve this answer





























                        0














                        From BIP173 > Specification > Segwit address format > Decoding:




                        Software interpreting a segwit address:



                        • MUST verify that the human-readable part is "bc" for mainnet and "tb" for testnet.

                        • MUST verify that the first decoded data value (the witness version) is between 0 and 16, inclusive






                        share|improve this answer



























                          0












                          0








                          0







                          From BIP173 > Specification > Segwit address format > Decoding:




                          Software interpreting a segwit address:



                          • MUST verify that the human-readable part is "bc" for mainnet and "tb" for testnet.

                          • MUST verify that the first decoded data value (the witness version) is between 0 and 16, inclusive






                          share|improve this answer















                          From BIP173 > Specification > Segwit address format > Decoding:




                          Software interpreting a segwit address:



                          • MUST verify that the human-readable part is "bc" for mainnet and "tb" for testnet.

                          • MUST verify that the first decoded data value (the witness version) is between 0 and 16, inclusive







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 10 at 17:06

























                          answered Jan 9 at 18:47









                          kawthuldrokkawthuldrok

                          488




                          488



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Bitcoin Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fbitcoin.stackexchange.com%2fquestions%2f83454%2fwhy-does-the-decode-function-of-the-segwit-bech32-encoder-decoder-take-a-hrp-hu%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown






                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay