PageRenderTime 276ms CodeModel.GetById 51ms RepoModel.GetById 0ms app.codeStats 1ms

/t/bip-0350.t.sh

https://github.com/grondilu/bitcoin-bash-tools
Shell | 200 lines | 153 code | 21 blank | 26 comment | 31 complexity | 8a055bfb716d07ea6b6cb4597b347a27 MD5 | raw file
  1. #!/usr/bin/env bash
  2. #
  3. # Translated from javascript :
  4. # https://github.com/sipa/bech32/blob/master/ref/javascript/tests.js
  5. #
  6. # Original copyright notice and permission notice :
  7. #
  8. #// Copyright (c) 2017, 2021 Pieter Wuille
  9. #//
  10. #// Permission is hereby granted, free of charge, to any person obtaining a copy
  11. #// of this software and associated documentation files (the "Software"), to deal
  12. #// in the Software without restriction, including without limitation the rights
  13. #// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. #// copies of the Software, and to permit persons to whom the Software is
  15. #// furnished to do so, subject to the following conditions:
  16. #//
  17. #// The above copyright notice and this permission notice shall be included in
  18. #// all copies or substantial portions of the Software.
  19. #//
  20. #// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. #// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. #// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. #// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. #// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. #// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. #// THE SOFTWARE.
  27. . bitcoin.sh
  28. . bip-0350.sh
  29. declare -a valid_checksum_bech32=(
  30. A12UEL5L
  31. a12uel5l
  32. an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs
  33. abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw
  34. 11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j
  35. split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w
  36. ?1ezyfcl
  37. )
  38. declare -a valid_checksum_bech32m=(
  39. A1LQFN3A
  40. a1lqfn3a
  41. an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6
  42. abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx
  43. 11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8
  44. split1checkupstagehandshakeupstreamerranterredcaperredlc445v
  45. ?1v759aa
  46. )
  47. declare -a invalid_checksum_bech32=(
  48. " 1nwldj5"
  49. $'\x7f1axkwrx'
  50. $'\x801eym55h'
  51. an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx
  52. pzry9x0s0muk
  53. 1pzry9x0s0muk
  54. x1b4n0q5v
  55. li1dgmt3
  56. $'de1lg7wt\xff'
  57. A1G7SGD8
  58. 10a06t8
  59. 1qzzfhee
  60. )
  61. declare -a invalid_checksum_bech32m=(
  62. ' 1xj0phk'
  63. $'\x7F1g6xzxy'
  64. $'\x801vctc34'
  65. an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4
  66. qyrz8wqd2c9m
  67. 1qyrz8wqd2c9m
  68. y1b0jsk6g
  69. lt1igcx5c0
  70. in1muywd
  71. mm1crxm3i
  72. au1s5cgom
  73. M1VUXWEZ
  74. 16plkw9
  75. 1p2gdwpf
  76. )
  77. declare -A valid_address=(
  78. [BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4]=0014751e76e8199196d454941c45d1b3a323f1433bd6
  79. [tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7]=00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262
  80. [bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y]=5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6
  81. [BC1SW50QGDZ25J]=6002751e
  82. [bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs]=5210751e76e8199196d454941c45d1b3a323
  83. [tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy]=0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433
  84. [tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c]=5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433
  85. [bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0]=512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
  86. )
  87. declare -a invalid_address=(
  88. tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut
  89. bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd
  90. tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf
  91. BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL
  92. bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh
  93. tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47
  94. bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4
  95. BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R
  96. bc1pw5dgrnzv
  97. bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav
  98. BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P
  99. tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq
  100. bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf
  101. tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j
  102. bc1gmk9yu
  103. )
  104. declare -i n=0
  105. declare t
  106. echo 1..79
  107. for t in "${valid_checksum_bech32[@]}"
  108. do
  109. ((n++))
  110. if bech32_decode "$t" >/dev/null
  111. then echo "ok $n - $t"
  112. else echo "not ok $n - unexpected invalid test for $t"
  113. fi
  114. done
  115. for t in "${valid_checksum_bech32m[@]}"
  116. do
  117. ((n++))
  118. if bech32_decode -m "$t" >/dev/null
  119. then echo "ok $n - $t"
  120. else echo "not ok $n - unexpected invalid test for $t"
  121. fi
  122. done
  123. for t in "${invalid_checksum_bech32[@]}"
  124. do
  125. ((n++))
  126. if bech32_decode "$t" >/dev/null
  127. then echo "not ok $n - unexpected valid test for $t"
  128. else echo "ok $n - $t => $?"
  129. fi
  130. done
  131. for t in "${invalid_checksum_bech32m[@]}"
  132. do
  133. ((n++))
  134. if bech32_decode -m "$t" >/dev/null
  135. then echo "not ok $n - unexpected valid test for $t"
  136. else echo "ok $n - $t => $?"
  137. fi
  138. done
  139. for address in "${!valid_address[@]}"
  140. do
  141. scriptpubkey=${valid_address[$address]}
  142. hrp="${address%1*}" payload="${address##*1}"
  143. ((n++))
  144. if segwit_decode "$address" >/dev/null
  145. then echo "ok $n - could decode $address"
  146. else echo "not ok $n - could not decode $address"
  147. fi
  148. declare decoded_address="$(segwit_decode "$address" |tr '\n' ' ')"
  149. declare hrp program output recreated_address
  150. declare -i version=0 encodedversion
  151. ((n++))
  152. read hrp version program <<<"$decoded_address"
  153. encodedversion=$version
  154. (( version > 0 && (encodedversion+=0x50) ))
  155. printf -v output "%02x%02x%s" $encodedversion $((${#program}/2)) $program
  156. if [[ "$output" = "$scriptpubkey" ]]
  157. then echo "ok $n - $address -> $scriptpubkey"
  158. else echo "not ok $n - $address -> $output instead of $scriptpubkey"
  159. fi
  160. ((n++))
  161. if [[ "$address" =~ ^tb ]]
  162. then recreated_address="$(segwitAddress -t -v $version "$program")"
  163. else recreated_address="$(segwitAddress -v $version "$program")"
  164. fi
  165. if [[ "$recreated_address" = "${address,,}" ]]
  166. then echo "ok $n - could recreate $address"
  167. else echo "not ok $n - failed to recreate $address, got $recreated_address instead"
  168. fi
  169. done
  170. for address in "${invalid_address[@]}"
  171. do
  172. segwit_decode $address >/dev/null;
  173. declare -i error=$?
  174. ((n++))
  175. if ((error > 0))
  176. then echo "ok $n - got error code $error for $address"
  177. else echo "not ok $n - failed to detect error for $address"
  178. fi
  179. done