bitcoin-ruby 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +8 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +46 -0
  5. data/.travis.yml +5 -1
  6. data/Gemfile +11 -9
  7. data/Gemfile.lock +64 -12
  8. data/README.rdoc +17 -4
  9. data/Rakefile +58 -83
  10. data/bitcoin-ruby.gemspec +5 -2
  11. data/lib/bitcoin.rb +31 -14
  12. data/lib/bitcoin/bech32.rb +126 -132
  13. data/lib/bitcoin/bloom_filter.rb +24 -21
  14. data/lib/bitcoin/builder.rb +168 -126
  15. data/lib/bitcoin/connection.rb +21 -24
  16. data/lib/bitcoin/contracthash.rb +20 -24
  17. data/lib/bitcoin/dogecoin.rb +79 -77
  18. data/lib/bitcoin/electrum/mnemonic.rb +28 -25
  19. data/lib/bitcoin/ext_key.rb +3 -3
  20. data/lib/bitcoin/ffi/bitcoinconsensus.rb +17 -13
  21. data/lib/bitcoin/ffi/openssl.rb +355 -338
  22. data/lib/bitcoin/ffi/secp256k1.rb +97 -64
  23. data/lib/bitcoin/protocol.rb +6 -3
  24. data/lib/bitcoin/protocol/address.rb +15 -13
  25. data/lib/bitcoin/protocol/aux_pow.rb +12 -15
  26. data/lib/bitcoin/protocol/block.rb +102 -76
  27. data/lib/bitcoin/protocol/handler.rb +2 -4
  28. data/lib/bitcoin/protocol/parser.rb +108 -92
  29. data/lib/bitcoin/protocol/partial_merkle_tree.rb +59 -47
  30. data/lib/bitcoin/protocol/reject.rb +26 -28
  31. data/lib/bitcoin/protocol/script_witness.rb +3 -8
  32. data/lib/bitcoin/protocol/tx.rb +250 -137
  33. data/lib/bitcoin/protocol/txin.rb +44 -38
  34. data/lib/bitcoin/protocol/txout.rb +27 -20
  35. data/lib/bitcoin/protocol/version.rb +47 -34
  36. data/lib/bitcoin/script.rb +18 -17
  37. data/lib/bitcoin/trezor/mnemonic.rb +113 -98
  38. data/lib/bitcoin/version.rb +1 -1
  39. data/spec/examples.txt +399 -0
  40. data/spec/{bitcoin/fixtures → fixtures}/000000000000056b1a3d84a1e2b33cde8915a4b61c0cae14fca6d3e1490b4f98.json +0 -0
  41. data/spec/{bitcoin/fixtures → fixtures}/03d7e1fa4d5fefa169431f24f7798552861b255cd55d377066fedcd088fb0e99.json +0 -0
  42. data/spec/{bitcoin/fixtures → fixtures}/0961c660358478829505e16a1f028757e54b5bbf9758341a7546573738f31429.json +0 -0
  43. data/spec/{bitcoin/fixtures → fixtures}/0f24294a1d23efbb49c1765cf443fba7930702752aba6d765870082fe4f13cae.json +0 -0
  44. data/spec/{bitcoin/fixtures → fixtures}/156e6e1b84c5c3bd3a0927b25e4119fadce6e6d5186f363317511d1d680fae9a.json +0 -0
  45. data/spec/{bitcoin/fixtures → fixtures}/23b397edccd3740a74adb603c9756370fafcde9bcc4483eb271ecad09a94dd63.json +0 -0
  46. data/spec/{bitcoin/fixtures → fixtures}/315ac7d4c26d69668129cc352851d9389b4a6868f1509c6c8b66bead11e2619f.json +0 -0
  47. data/spec/{bitcoin/fixtures → fixtures}/35e2001b428891fefa0bfb73167c7360669d3cbd7b3aa78e7cad125ddfc51131.json +0 -0
  48. data/spec/{bitcoin/fixtures → fixtures}/3a17dace09ffb919ed627a93f1873220f4c975c1248558b18d16bce25d38c4b7.json +0 -0
  49. data/spec/{bitcoin/fixtures → fixtures}/3e58b7eed0fdb599019af08578effea25c8666bbe8e200845453cacce6314477.json +0 -0
  50. data/spec/{bitcoin/fixtures → fixtures}/514c46f0b61714092f15c8dfcb576c9f79b3f959989b98de3944b19d98832b58.json +0 -0
  51. data/spec/{bitcoin/fixtures → fixtures}/51bf528ecf3c161e7c021224197dbe84f9a8564212f6207baa014c01a1668e1e.json +0 -0
  52. data/spec/{bitcoin/fixtures → fixtures}/60a20bd93aa49ab4b28d514ec10b06e1829ce6818ec06cd3aabd013ebcdc4bb1.json +0 -0
  53. data/spec/{bitcoin/fixtures → fixtures}/69216b8aaa35b76d6613e5f527f4858640d986e1046238583bdad79b35e938dc.json +0 -0
  54. data/spec/{bitcoin/fixtures → fixtures}/7208e5edf525f04e705fb3390194e316205b8f995c8c9fcd8c6093abe04fa27d.json +0 -0
  55. data/spec/{bitcoin/fixtures → fixtures}/761d8c5210fdfd505f6dff38f740ae3728eb93d7d0971fb433f685d40a4c04f6.json +0 -0
  56. data/spec/{bitcoin/fixtures → fixtures}/8d0b238a06b5a70be75d543902d02d7a514d68d3252a949a513865ac3538874c.json +0 -0
  57. data/spec/{bitcoin/fixtures → fixtures}/aea682d68a3ea5e3583e088dcbd699a5d44d4b083f02ad0aaf2598fe1fa4dfd4.json +0 -0
  58. data/spec/{bitcoin/fixtures → fixtures}/base58_keys_invalid.json +0 -0
  59. data/spec/{bitcoin/fixtures → fixtures}/base58_keys_valid.json +0 -0
  60. data/spec/{bitcoin/fixtures → fixtures}/bc179baab547b7d7c1d5d8d6f8b0cc6318eaa4b0dd0a093ad6ac7f5a1cb6b3ba.json +0 -0
  61. data/spec/{bitcoin/fixtures → fixtures}/bd1715f1abfdc62bea3f605bdb461b3ba1f2cca6ec0d73a18a548b7717ca8531.json +0 -0
  62. data/spec/{bitcoin/fixtures → fixtures}/block-testnet-0000000000ac85bb2530a05a4214a387e6be02b22d3348abc5e7a5d9c4ce8dab.bin +0 -0
  63. data/spec/{bitcoin/fixtures → fixtures}/cd874fa8cb0e2ec2d385735d5e1fd482c4fe648533efb4c50ee53bda58e15ae2.json +0 -0
  64. data/spec/{bitcoin/fixtures → fixtures}/ce5fad9b4ef094d8f4937b0707edaf0a6e6ceeaf67d5edbfd51f660eac8f398b.json +0 -0
  65. data/spec/{bitcoin/fixtures → fixtures}/coinbase-toshi.json +0 -0
  66. data/spec/{bitcoin/fixtures → fixtures}/coinbase.json +0 -0
  67. data/spec/{bitcoin/fixtures → fixtures}/dogecoin-block-60323982f9c5ff1b5a954eac9dc1269352835f47c2c5222691d80f0d50dcf053.bin +0 -0
  68. data/spec/{bitcoin/fixtures → fixtures}/f003f0c1193019db2497a675fd05d9f2edddf9b67c59e677c48d3dbd4ed5f00b.json +0 -0
  69. data/spec/{bitcoin/fixtures → fixtures}/filteredblock-0.bin +0 -0
  70. data/spec/{bitcoin/fixtures → fixtures}/litecoin-block-80ca095ed10b02e53d769eb6eaf92cd04e9e0759e5be4a8477b42911ba49c78f.bin +0 -0
  71. data/spec/{bitcoin/fixtures → fixtures}/litecoin-block-80ca095ed10b02e53d769eb6eaf92cd04e9e0759e5be4a8477b42911ba49c78f.json +0 -0
  72. data/spec/{bitcoin/fixtures → fixtures}/litecoin-genesis-block-12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2.bin +0 -0
  73. data/spec/{bitcoin/fixtures → fixtures}/litecoin-genesis-block-12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2.json +0 -0
  74. data/spec/{bitcoin/fixtures → fixtures}/litecoin-tx-f5aa30f574e3b6f1a3d99c07a6356ba812aabb9661e1d5f71edff828cbd5c996.json +0 -0
  75. data/spec/{bitcoin/fixtures → fixtures}/rawblock-0.bin +0 -0
  76. data/spec/{bitcoin/fixtures → fixtures}/rawblock-0.json +0 -0
  77. data/spec/{bitcoin/fixtures → fixtures}/rawblock-1.bin +0 -0
  78. data/spec/{bitcoin/fixtures → fixtures}/rawblock-1.json +0 -0
  79. data/spec/{bitcoin/fixtures → fixtures}/rawblock-131025.bin +0 -0
  80. data/spec/{bitcoin/fixtures → fixtures}/rawblock-131025.json +0 -0
  81. data/spec/{bitcoin/fixtures → fixtures}/rawblock-170.bin +0 -0
  82. data/spec/{bitcoin/fixtures → fixtures}/rawblock-9.bin +0 -0
  83. data/spec/{bitcoin/fixtures → fixtures}/rawblock-auxpow.bin +0 -0
  84. data/spec/{bitcoin/fixtures → fixtures}/rawblock-testnet-1151351.bin +0 -0
  85. data/spec/{bitcoin/fixtures → fixtures}/rawblock-testnet-26478.bin +0 -0
  86. data/spec/{bitcoin/fixtures → fixtures}/rawblock-testnet-26478.json +0 -0
  87. data/spec/{bitcoin/fixtures → fixtures}/rawblock-testnet-265322.bin +0 -0
  88. data/spec/{bitcoin/fixtures → fixtures}/rawtx-01-toshi.json +0 -0
  89. data/spec/{bitcoin/fixtures → fixtures}/rawtx-01.bin +0 -0
  90. data/spec/{bitcoin/fixtures → fixtures}/rawtx-01.json +0 -0
  91. data/spec/{bitcoin/fixtures → fixtures}/rawtx-02-toshi.json +0 -0
  92. data/spec/{bitcoin/fixtures → fixtures}/rawtx-02.bin +0 -0
  93. data/spec/{bitcoin/fixtures → fixtures}/rawtx-02.json +0 -0
  94. data/spec/{bitcoin/fixtures → fixtures}/rawtx-03-toshi.json +0 -0
  95. data/spec/{bitcoin/fixtures → fixtures}/rawtx-03.bin +0 -0
  96. data/spec/{bitcoin/fixtures → fixtures}/rawtx-03.json +0 -0
  97. data/spec/{bitcoin/fixtures → fixtures}/rawtx-04.json +0 -0
  98. data/spec/{bitcoin/fixtures → fixtures}/rawtx-0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9.bin +0 -0
  99. data/spec/{bitcoin/fixtures → fixtures}/rawtx-05.json +0 -0
  100. data/spec/{bitcoin/fixtures → fixtures}/rawtx-14be6fff8c6014f7c9493b4a6e4a741699173f39d74431b6b844fcb41ebb9984.bin +0 -0
  101. data/spec/{bitcoin/fixtures → fixtures}/rawtx-2f4a2717ec8c9f077a87dde6cbe0274d5238793a3f3f492b63c744837285e58a.bin +0 -0
  102. data/spec/{bitcoin/fixtures → fixtures}/rawtx-2f4a2717ec8c9f077a87dde6cbe0274d5238793a3f3f492b63c744837285e58a.json +0 -0
  103. data/spec/{bitcoin/fixtures → fixtures}/rawtx-406b2b06bcd34d3c8733e6b79f7a394c8a431fbf4ff5ac705c93f4076bb77602.json +0 -0
  104. data/spec/{bitcoin/fixtures → fixtures}/rawtx-52250a162c7d03d2e1fbc5ebd1801a88612463314b55102171c5b5d817d2d7b2.bin +0 -0
  105. data/spec/{bitcoin/fixtures → fixtures}/rawtx-b5d4e8883533f99e5903ea2cf001a133a322fa6b1370b18a16c57c946a40823d.bin +0 -0
  106. data/spec/{bitcoin/fixtures → fixtures}/rawtx-ba1ff5cd66713133c062a871a8adab92416f1e38d17786b2bf56ac5f6ffdfdf5.json +0 -0
  107. data/spec/{bitcoin/fixtures → fixtures}/rawtx-c99c49da4c38af669dea436d3e73780dfdb6c1ecf9958baa52960e8baee30e73.json +0 -0
  108. data/spec/{bitcoin/fixtures → fixtures}/rawtx-de35d060663750b3975b7997bde7fb76307cec5b270d12fcd9c4ad98b279c28c.json +0 -0
  109. data/spec/{bitcoin/fixtures → fixtures}/rawtx-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16.bin +0 -0
  110. data/spec/{bitcoin/fixtures → fixtures}/rawtx-p2wpkh.bin +0 -0
  111. data/spec/{bitcoin/fixtures → fixtures}/rawtx-p2wpkh.json +0 -0
  112. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-04fdc38d6722ab4b12d79113fc4b2896bdcc5169710690ee4e78541b98e467b4.bin +0 -0
  113. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-0b294c7d11dd21bcccb8393e6744fed7d4d1981a08c00e3e88838cc421f33c9f.bin +0 -0
  114. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-3bc52ac063291ad92d95ddda5fd776a342083b95607ad32ed8bc6f8f7d30449e.bin +0 -0
  115. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-6f0bbdd4e71a8af4305018d738184df32dbb6f27284fdebd5b56d16947f7c181.bin +0 -0
  116. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-a220adf1902c46a39db25a24bc4178b6a88440f977a7e2cabfdd8b5c1dd35cfb.json +0 -0
  117. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-a7c9b06e275e8674cc19a5f7d3e557c72c6d93576e635b33212dbe08ab7cdb60.bin +0 -0
  118. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-e232e0055dbdca88bbaa79458683195a0b7c17c5b6c524a8d146721d4d4d652f.bin +0 -0
  119. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-e232e0055dbdca88bbaa79458683195a0b7c17c5b6c524a8d146721d4d4d652f.json +0 -0
  120. data/spec/{bitcoin/fixtures → fixtures}/rawtx-testnet-f80acbd2f594d04ddb0e1cacba662132104909157dff526935a3c88abe9201a5.bin +0 -0
  121. data/spec/{bitcoin/fixtures → fixtures}/script_tests.json +0 -0
  122. data/spec/{bitcoin/fixtures → fixtures}/sighash.json +0 -0
  123. data/spec/{bitcoin/fixtures → fixtures}/tx-0295028ef826b2a188409cb905b631faebb9bb3cdf14510571c5f4bd8591338f.json +0 -0
  124. data/spec/{bitcoin/fixtures → fixtures}/tx-03339a725007a279484fb6f5361f522dd1cf4d0923d30e6b973290dba4275f92.json +0 -0
  125. data/spec/{bitcoin/fixtures → fixtures}/tx-0a6a357e2f7796444e02638749d9611c008b253fb55f5dc88b739b230ed0c4c3.json +0 -0
  126. data/spec/{bitcoin/fixtures → fixtures}/tx-0ce7e5238fbdb6c086cf1b384b21b827e91cc23f360417265874a5a0d86ce367.json +0 -0
  127. data/spec/{bitcoin/fixtures → fixtures}/tx-0ef34c49f630aea17df0080728b0fc67bf5f87fbda936934a4b11b4a69d7821e.json +0 -0
  128. data/spec/{bitcoin/fixtures → fixtures}/tx-1129d2a8bd5bb3a81e54dc96a90f1f6b2544575748caa17243470935c5dd91b7.json +0 -0
  129. data/spec/{bitcoin/fixtures → fixtures}/tx-19aa42fee0fa57c45d3b16488198b27caaacc4ff5794510d0c17f173f05587ff.json +0 -0
  130. data/spec/{bitcoin/fixtures → fixtures}/tx-1a4f3b9dc4494aeedeb39f30dd37e60541b2abe3ed4977992017cc0ad4f44956.json +0 -0
  131. data/spec/{bitcoin/fixtures → fixtures}/tx-1f9191dcf2b1844ca28c6ef4b969e1d5fab70a5e3c56b7007949e55851cb0c4f.json +0 -0
  132. data/spec/{bitcoin/fixtures → fixtures}/tx-22cd5fef23684d7b304e119bedffde6f54538d3d54a5bfa237e20dc2d9b4b5ad.json +0 -0
  133. data/spec/{bitcoin/fixtures → fixtures}/tx-28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f.json +0 -0
  134. data/spec/{bitcoin/fixtures → fixtures}/tx-2958fb00b4fd6fe0353503b886eb9a193d502f4fd5fc042d5e03216ba918bbd6.json +0 -0
  135. data/spec/{bitcoin/fixtures → fixtures}/tx-29f277145749ad6efbed3ae6ce301f8d33c585ec26b7c044ad93c2f866e9e942.json +0 -0
  136. data/spec/{bitcoin/fixtures → fixtures}/tx-2c5e5376c20e9cc78d0fb771730e5d840cc2096eff0ef045b599fe92475ace1c.json +0 -0
  137. data/spec/{bitcoin/fixtures → fixtures}/tx-2c63aa814701cef5dbd4bbaddab3fea9117028f2434dddcdab8339141e9b14d1.json +0 -0
  138. data/spec/{bitcoin/fixtures → fixtures}/tx-313897799b1e37e9ecae15010e56156dddde4e683c96b0e713af95272c38aee0.json +0 -0
  139. data/spec/{bitcoin/fixtures → fixtures}/tx-326882a7f22b5191f1a0cc9962ca4b878cd969cf3b3a70887aece4d801a0ba5e.json +0 -0
  140. data/spec/{bitcoin/fixtures → fixtures}/tx-345bed8785c3282a264ffb0dbee61cde54854f10e16f1b3e75b7f2d9f62946f2.json +0 -0
  141. data/spec/{bitcoin/fixtures → fixtures}/tx-39ba7440b7103557560cc8ce258009936796485aaf8b478e66ab4cb97c66e31b.json +0 -0
  142. data/spec/{bitcoin/fixtures → fixtures}/tx-3a04d57a833367f1655cc5ec3beb587888ef4977a86caa8c8ad4ba7cc717eae7.json +0 -0
  143. data/spec/{bitcoin/fixtures → fixtures}/tx-3da75972766f0ad13319b0b461fd16823a731e44f6e9de4eb3c52d6a6fb6c8ae.json +0 -0
  144. data/spec/{bitcoin/fixtures → fixtures}/tx-4142ee4877eb116abf955a7ec6ef2dc38133b793df762b76d75e3d7d4d8badc9.json +0 -0
  145. data/spec/{bitcoin/fixtures → fixtures}/tx-44b833074e671120ba33106877b49e86ece510824b9af477a3853972bcd8d06a.json +0 -0
  146. data/spec/{bitcoin/fixtures → fixtures}/tx-46224764c7870f95b58f155bce1e38d4da8e99d42dbb632d0dd7c07e092ee5aa.json +0 -0
  147. data/spec/{bitcoin/fixtures → fixtures}/tx-5df1375ffe61ac35ca178ebb0cab9ea26dedbd0e96005dfcee7e379fa513232f.json +0 -0
  148. data/spec/{bitcoin/fixtures → fixtures}/tx-62d9a565bd7b5344c5352e3e9e5f40fa4bbd467fa19c87357216ec8777ba1cce.json +0 -0
  149. data/spec/{bitcoin/fixtures → fixtures}/tx-6327783a064d4e350c454ad5cd90201aedf65b1fc524e73709c52f0163739190.json +0 -0
  150. data/spec/{bitcoin/fixtures → fixtures}/tx-6606c366a487bff9e412d0b6c09c14916319932db5954bf5d8719f43f828a3ba.json +0 -0
  151. data/spec/{bitcoin/fixtures → fixtures}/tx-6aaf18b9f1283b939d8e5d40ff5f8a435229f4178372659cc3a0bce4e262bf78.json +0 -0
  152. data/spec/{bitcoin/fixtures → fixtures}/tx-6b48bba6f6d2286d7ec0883c0fc3085955090813a4c94980466611c798b868cc.json +0 -0
  153. data/spec/{bitcoin/fixtures → fixtures}/tx-70cfbc6690f9ab46712db44e3079ac227962b2771a9341d4233d898b521619ef.json +0 -0
  154. data/spec/{bitcoin/fixtures → fixtures}/tx-7a1a9db42f065f75110fcdb1bc415549c8ef7670417ba1d35a67f1b8adc562c1.json +0 -0
  155. data/spec/{bitcoin/fixtures → fixtures}/tx-9a768fc7d0c4bdc86e25154357ef7c0063ca21310e5740a2f12f90b7455184a7.json +0 -0
  156. data/spec/{bitcoin/fixtures → fixtures}/tx-9cad8d523a0694f2509d092c39cebc8046adae62b4e4297102d568191d9478d8.json +0 -0
  157. data/spec/{bitcoin/fixtures → fixtures}/tx-9e052eb694bd7e15906433f064dff0161a12fd325c1124537766377004023c6f.json +0 -0
  158. data/spec/{bitcoin/fixtures → fixtures}/tx-9fb65b7304aaa77ac9580823c2c06b259cc42591e5cce66d76a81b6f51cc5c28.json +0 -0
  159. data/spec/{bitcoin/fixtures → fixtures}/tx-a6ce7081addade7676cd2af75c4129eba6bf5e179a19c40c7d4cf6a5fe595954.json +0 -0
  160. data/spec/{bitcoin/fixtures → fixtures}/tx-a955032f4d6b0c9bfe8cad8f00a8933790b9c1dc28c82e0f48e75b35da0e4944.json +0 -0
  161. data/spec/{bitcoin/fixtures → fixtures}/tx-aab7ef280abbb9cc6fbaf524d2645c3daf4fcca2b3f53370e618d9cedf65f1f8.json +0 -0
  162. data/spec/{bitcoin/fixtures → fixtures}/tx-ab9805c6d57d7070d9a42c5176e47bb705023e6b67249fb6760880548298e742.json +0 -0
  163. data/spec/{bitcoin/fixtures → fixtures}/tx-ad4bcf3241e5d2ad140564e20db3567d41594cf4c2012433fe46a2b70e0d87b8.json +0 -0
  164. data/spec/{bitcoin/fixtures → fixtures}/tx-b5b598de91787439afd5938116654e0b16b7a0d0f82742ba37564219c5afcbf9.json +0 -0
  165. data/spec/{bitcoin/fixtures → fixtures}/tx-b8fd633e7713a43d5ac87266adc78444669b987a56b3a65fb92d58c2c4b0e84d.json +0 -0
  166. data/spec/{bitcoin/fixtures → fixtures}/tx-bbca0628c42cb8bf7c3f4b2ad688fa56da5308dd2a10255da89fb1f46e6e413d.json +0 -0
  167. data/spec/{bitcoin/fixtures → fixtures}/tx-bc7fd132fcf817918334822ee6d9bd95c889099c96e07ca2c1eb2cc70db63224.json +0 -0
  168. data/spec/{bitcoin/fixtures → fixtures}/tx-c192b74844e4837a34c4a5a97b438f1c111405b01b99e2d12b7c96d07fc74c04.json +0 -0
  169. data/spec/{bitcoin/fixtures → fixtures}/tx-d3d77d63709e47d9ef58f0b557800115a6b676c6a423012fbb96f45d8fcef830.json +0 -0
  170. data/spec/{bitcoin/fixtures → fixtures}/tx-e335562f7e297aadeed88e5954bc4eeb8dc00b31d829eedb232e39d672b0c009.json +0 -0
  171. data/spec/{bitcoin/fixtures → fixtures}/tx-eb3b82c0884e3efa6d8b0be55b4915eb20be124c9766245bcc7f34fdac32bccb.json +0 -0
  172. data/spec/{bitcoin/fixtures → fixtures}/tx-fee1b9b85531c8fb6cd7831f83490c7f2aa768b6eefe29854ef5e89ce7b9ecb1.json +0 -0
  173. data/spec/{bitcoin/fixtures → fixtures}/txscript-invalid-too-many-sigops-followed-by-invalid-pushdata.bin +0 -0
  174. data/spec/helpers/block_helpers.rb +58 -0
  175. data/spec/helpers/fixture_helpers.rb +20 -0
  176. data/spec/helpers/library_helpers.rb +15 -0
  177. data/spec/spec_helper.rb +109 -0
  178. data/spec/unit/bitcoin/bech32_spec.rb +187 -0
  179. data/spec/unit/bitcoin/bitcoin_spec.rb +1079 -0
  180. data/spec/unit/bitcoin/bloom_filter_spec.rb +33 -0
  181. data/spec/unit/bitcoin/builder_spec.rb +559 -0
  182. data/spec/unit/bitcoin/contracthash_spec.rb +52 -0
  183. data/spec/unit/bitcoin/ext_key_spec.rb +281 -0
  184. data/spec/unit/bitcoin/key_spec.rb +457 -0
  185. data/spec/unit/bitcoin/network_spec.rb +71 -0
  186. data/spec/unit/bitcoin/protocol/addr_spec.rb +90 -0
  187. data/spec/unit/bitcoin/protocol/aux_pow_spec.rb +45 -0
  188. data/spec/unit/bitcoin/protocol/bip143_spec.rb +334 -0
  189. data/spec/unit/bitcoin/protocol/block_spec.rb +280 -0
  190. data/spec/unit/bitcoin/protocol/getblocks_spec.rb +44 -0
  191. data/spec/unit/bitcoin/protocol/inv_spec.rb +166 -0
  192. data/spec/unit/bitcoin/protocol/notfound_spec.rb +44 -0
  193. data/spec/unit/bitcoin/protocol/parser_spec.rb +69 -0
  194. data/spec/unit/bitcoin/protocol/partial_merkle_tree_spec.rb +47 -0
  195. data/spec/unit/bitcoin/protocol/ping_spec.rb +62 -0
  196. data/spec/unit/bitcoin/protocol/tx_spec.rb +1515 -0
  197. data/spec/unit/bitcoin/protocol/txin_spec.rb +47 -0
  198. data/spec/unit/bitcoin/protocol/txout_spec.rb +36 -0
  199. data/spec/unit/bitcoin/protocol/version_spec.rb +121 -0
  200. data/spec/unit/bitcoin/script/opcodes_spec.rb +864 -0
  201. data/spec/unit/bitcoin/script/script_spec.rb +1610 -0
  202. data/spec/unit/bitcoin/secp256k1_spec.rb +138 -0
  203. data/spec/unit/bitcoin/trezor/mnemonic_spec.rb +193 -0
  204. data/spec/unit/integrations/dogecoin_spec.rb +215 -0
  205. metadata +381 -372
  206. data/lib/bitcoin/logger.rb +0 -86
  207. data/lib/bitcoin/protocol/alert.rb +0 -46
  208. data/spec/bitcoin/bech32_spec.rb +0 -160
  209. data/spec/bitcoin/bitcoin_spec.rb +0 -666
  210. data/spec/bitcoin/bloom_filter_spec.rb +0 -23
  211. data/spec/bitcoin/builder_spec.rb +0 -375
  212. data/spec/bitcoin/contracthash_spec.rb +0 -45
  213. data/spec/bitcoin/dogecoin_spec.rb +0 -176
  214. data/spec/bitcoin/ext_key_spec.rb +0 -180
  215. data/spec/bitcoin/ffi_openssl.rb +0 -45
  216. data/spec/bitcoin/fixtures/rawblock-170.json +0 -68
  217. data/spec/bitcoin/fixtures/rawblock-9.json +0 -39
  218. data/spec/bitcoin/fixtures/reorg/blk_0_to_4.dat +0 -0
  219. data/spec/bitcoin/fixtures/reorg/blk_3A.dat +0 -0
  220. data/spec/bitcoin/fixtures/reorg/blk_4A.dat +0 -0
  221. data/spec/bitcoin/fixtures/reorg/blk_5A.dat +0 -0
  222. data/spec/bitcoin/fixtures/testnet/block_0.bin +0 -0
  223. data/spec/bitcoin/fixtures/testnet/block_1.bin +0 -0
  224. data/spec/bitcoin/fixtures/testnet/block_2.bin +0 -0
  225. data/spec/bitcoin/fixtures/testnet/block_3.bin +0 -0
  226. data/spec/bitcoin/fixtures/testnet/block_4.bin +0 -0
  227. data/spec/bitcoin/fixtures/testnet/block_5.bin +0 -0
  228. data/spec/bitcoin/fixtures/txdp-1.txt +0 -32
  229. data/spec/bitcoin/fixtures/txdp-2-signed.txt +0 -19
  230. data/spec/bitcoin/fixtures/txdp-2-unsigned.txt +0 -14
  231. data/spec/bitcoin/helpers/fake_blockchain.rb +0 -183
  232. data/spec/bitcoin/key_spec.rb +0 -326
  233. data/spec/bitcoin/network_spec.rb +0 -50
  234. data/spec/bitcoin/performance/storage_spec.rb +0 -41
  235. data/spec/bitcoin/protocol/addr_spec.rb +0 -82
  236. data/spec/bitcoin/protocol/alert_spec.rb +0 -22
  237. data/spec/bitcoin/protocol/aux_pow_spec.rb +0 -45
  238. data/spec/bitcoin/protocol/bip143_spec.rb +0 -116
  239. data/spec/bitcoin/protocol/block_spec.rb +0 -208
  240. data/spec/bitcoin/protocol/getblocks_spec.rb +0 -32
  241. data/spec/bitcoin/protocol/inv_spec.rb +0 -134
  242. data/spec/bitcoin/protocol/notfound_spec.rb +0 -31
  243. data/spec/bitcoin/protocol/parser_spec.rb +0 -50
  244. data/spec/bitcoin/protocol/partial_merkle_tree_spec.rb +0 -38
  245. data/spec/bitcoin/protocol/ping_spec.rb +0 -51
  246. data/spec/bitcoin/protocol/reject.rb +0 -17
  247. data/spec/bitcoin/protocol/tx_spec.rb +0 -894
  248. data/spec/bitcoin/protocol/txin_spec.rb +0 -45
  249. data/spec/bitcoin/protocol/txout_spec.rb +0 -33
  250. data/spec/bitcoin/protocol/version_spec.rb +0 -110
  251. data/spec/bitcoin/script/opcodes_spec.rb +0 -773
  252. data/spec/bitcoin/script/script_spec.rb +0 -977
  253. data/spec/bitcoin/secp256k1_spec.rb +0 -78
  254. data/spec/bitcoin/spec_helper.rb +0 -108
  255. data/spec/bitcoin/trezor/mnemonic_spec.rb +0 -161
@@ -1,19 +1,18 @@
1
1
  # encoding: ascii-8bit
2
2
 
3
- # bindings for secp256k1 inside bitcoin (https://github.com/bitcoin/bitcoin/tree/v0.13.1/src/secp256k1)
4
- # tag: v0.13.1
5
- # commit: 03422e564b552c1d3c16ae854f8471f7cb39e25d
6
- # bitcoin@master% git checkout v0.13.1
7
- # bitcoin@tags/v0.13.1^0% cd src/secp256k1
8
- # bitcoin@tags/v0.13.1^0 src/secp256k1% ./autogen.sh
9
- # bitcoin@tags/v0.13.1^0 src/secp256k1% ./configure --enable-module-recovery
10
- # bitcoin@tags/v0.13.1^0 src/secp256k1% make libsecp256k1.la
11
- # bitcoin@tags/v0.13.1^0 src/secp256k1% nm -D .libs/libsecp256k1.so.0.0.0 | grep secp
12
- # export SECP256K1_LIB_PATH=/path/to/bitcoin/src/secp256k1/.libs/libsecp256k1.so.0.0.0
13
-
14
3
  require 'ffi'
15
4
 
16
5
  module Bitcoin
6
+ # bindings for secp256k1 inside bitcoin (https://github.com/bitcoin/bitcoin/tree/v0.13.1/src/secp256k1)
7
+ # tag: v0.13.1
8
+ # commit: 03422e564b552c1d3c16ae854f8471f7cb39e25d
9
+ # bitcoin@master% git checkout v0.13.1
10
+ # bitcoin@tags/v0.13.1^0% cd src/secp256k1
11
+ # bitcoin@tags/v0.13.1^0 src/secp256k1% ./autogen.sh
12
+ # bitcoin@tags/v0.13.1^0 src/secp256k1% ./configure --enable-module-recovery
13
+ # bitcoin@tags/v0.13.1^0 src/secp256k1% make libsecp256k1.la
14
+ # bitcoin@tags/v0.13.1^0 src/secp256k1% nm -D .libs/libsecp256k1.so.0.0.0 | grep secp
15
+ # export SECP256K1_LIB_PATH=/path/to/bitcoin/src/secp256k1/.libs/libsecp256k1.so.0.0.0
17
16
  module Secp256k1
18
17
  extend FFI::Library
19
18
 
@@ -32,10 +31,10 @@ module Bitcoin
32
31
 
33
32
  # Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export.
34
33
  SECP256K1_EC_COMPRESSED = (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
35
- SECP256K1_EC_UNCOMPRESSED = (SECP256K1_FLAGS_TYPE_COMPRESSION)
34
+ SECP256K1_EC_UNCOMPRESSED = SECP256K1_FLAGS_TYPE_COMPRESSION
36
35
 
37
36
  def self.ffi_load_functions(file)
38
- class_eval <<-RUBY
37
+ class_eval <<-RUBY # rubocop:disable Style/EvalWithLocation
39
38
  ffi_lib [ %[#{file}] ]
40
39
 
41
40
  ##
@@ -91,28 +90,35 @@ module Bitcoin
91
90
  attach_function :secp256k1_ecdsa_signature_parse_der, [:pointer, :pointer, :pointer, :size_t], :int
92
91
 
93
92
  # TODO: add or port
94
- # # int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen)
93
+ # # int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen)
95
94
  # attach_function :ecdsa_signature_parse_der_lax, [:pointer, :pointer, :pointer, :size_t], :int
96
95
  RUBY
97
96
  end
98
97
 
99
98
  def self.init
99
+ @loaded ||= false
100
100
  return if @loaded
101
- lib_path = [ ENV['SECP256K1_LIB_PATH'], 'vendor/bitcoin/src/secp256k1/.libs/libsecp256k1.so' ].find{|f| File.exists?(f.to_s) }
101
+ lib_path = [
102
+ ENV['SECP256K1_LIB_PATH'], 'vendor/bitcoin/src/secp256k1/.libs/libsecp256k1.so'
103
+ ].find { |f| File.exist?(f.to_s) }
102
104
  ffi_load_functions(lib_path)
103
105
  @loaded = true
104
106
  end
105
107
 
106
- def self.with_context(flags=nil, seed=nil)
108
+ def self.with_context(flags = nil, seed = nil)
107
109
  init
108
- flags = flags || (SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN)
110
+ flags ||= (SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN)
109
111
  context = secp256k1_context_create(flags)
110
112
 
111
- ret, tries, max = 0, 0, 20
113
+ ret = 0
114
+ tries = 0
115
+ max = 20
112
116
  while ret != 1
113
- raise "secp256k1_context_randomize failed." if tries >= max
117
+ raise 'secp256k1_context_randomize failed.' if tries >= max
114
118
  tries += 1
115
- ret = secp256k1_context_randomize(context, FFI::MemoryPointer.from_string(seed || SecureRandom.random_bytes(32)))
119
+ ret = secp256k1_context_randomize(
120
+ context, FFI::MemoryPointer.from_string(seed || SecureRandom.random_bytes(32))
121
+ )
116
122
  end
117
123
 
118
124
  yield(context) if block_given?
@@ -120,12 +126,13 @@ module Bitcoin
120
126
  secp256k1_context_destroy(context)
121
127
  end
122
128
 
123
- def self.generate_key_pair(compressed=true)
129
+ def self.generate_key_pair(compressed = true)
124
130
  with_context do |context|
125
-
126
- ret, tries, max = 0, 0, 20
131
+ ret = 0
132
+ tries = 0
133
+ max = 20
127
134
  while ret != 1
128
- raise "secp256k1_ec_seckey_verify in generate_key_pair failed." if tries >= max
135
+ raise 'secp256k1_ec_seckey_verify in generate_key_pair failed.' if tries >= max
129
136
  tries += 1
130
137
 
131
138
  seckey = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, SecureRandom.random_bytes(32))
@@ -134,46 +141,56 @@ module Bitcoin
134
141
 
135
142
  internal_pubkey = FFI::MemoryPointer.new(:uchar, 64)
136
143
  result = secp256k1_ec_pubkey_create(context, internal_pubkey, seckey)
137
- raise "error creating pubkey" unless result == 1
144
+ raise 'error creating pubkey' unless result == 1
138
145
 
139
- pubkey, pubkey_len = FFI::MemoryPointer.new(:uchar, 65), FFI::MemoryPointer.new(:uint64)
146
+ pubkey = FFI::MemoryPointer.new(:uchar, 65)
147
+ pubkey_len = FFI::MemoryPointer.new(:uint64)
140
148
  result = if compressed
141
- pubkey_len.put_uint64(0, 33)
142
- secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_COMPRESSED)
143
- else
144
- pubkey_len.put_uint64(0, 65)
145
- secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_UNCOMPRESSED)
146
- end
147
- raise "error serialize pubkey" unless (result == 1) || pubkey_len.read_uint64 > 0
148
-
149
- [ seckey.read_string(32), pubkey.read_string(pubkey_len.read_uint64) ]
149
+ pubkey_len.put_uint64(0, 33)
150
+ secp256k1_ec_pubkey_serialize(
151
+ context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_COMPRESSED
152
+ )
153
+ else
154
+ pubkey_len.put_uint64(0, 65)
155
+ secp256k1_ec_pubkey_serialize(
156
+ context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_UNCOMPRESSED
157
+ )
158
+ end
159
+ raise 'error serialize pubkey' unless (result == 1) || pubkey_len.read_uint64 > 0
160
+
161
+ [seckey.read_string(32), pubkey.read_string(pubkey_len.read_uint64)]
150
162
  end
151
163
  end
152
164
 
153
- def self.generate_key(compressed=true)
165
+ def self.generate_key(compressed = true)
154
166
  priv, pub = generate_key_pair(compressed)
155
- Bitcoin::Key.new(priv.unpack("H*")[0], pub.unpack("H*")[0])
167
+ Bitcoin::Key.new(priv.unpack('H*')[0], pub.unpack('H*')[0])
156
168
  end
157
169
 
158
170
  def self.sign(data, priv_key)
159
171
  with_context do |context|
160
172
  seckey = FFI::MemoryPointer.new(:uchar, priv_key.bytesize).put_bytes(0, priv_key)
161
- raise "priv_key invalid" unless secp256k1_ec_seckey_verify(context, seckey) == 1
173
+ raise 'priv_key invalid' unless secp256k1_ec_seckey_verify(context, seckey) == 1
162
174
 
163
175
  internal_signature = FFI::MemoryPointer.new(:uchar, 64)
164
176
  msg32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, data)
165
177
 
166
- ret, tries, max = 0, 0, 20
178
+ ret = 0
179
+ tries = 0
180
+ max = 20
167
181
  while ret != 1
168
- raise "secp256k1_ecdsa_sign failed." if tries >= max
182
+ raise 'secp256k1_ecdsa_sign failed.' if tries >= max
169
183
  tries += 1
170
184
 
171
185
  ret = secp256k1_ecdsa_sign(context, internal_signature, msg32, seckey, nil, nil)
172
186
  end
173
187
 
174
- signature, signature_len = FFI::MemoryPointer.new(:uchar, 72), FFI::MemoryPointer.new(:uint64).put_uint64(0, 72)
175
- result = secp256k1_ecdsa_signature_serialize_der(context, signature, signature_len, internal_signature)
176
- raise "secp256k1_ecdsa_signature_serialize_der failed" unless result == 1
188
+ signature = FFI::MemoryPointer.new(:uchar, 72)
189
+ signature_len = FFI::MemoryPointer.new(:uint64).put_uint64(0, 72)
190
+ result = secp256k1_ecdsa_signature_serialize_der(
191
+ context, signature, signature_len, internal_signature
192
+ )
193
+ raise 'secp256k1_ecdsa_signature_serialize_der failed' unless result == 1
177
194
 
178
195
  signature.read_string(signature_len.read_uint64)
179
196
  end
@@ -181,7 +198,7 @@ module Bitcoin
181
198
 
182
199
  def self.verify(data, sig, pub_key)
183
200
  with_context do |context|
184
- return false if data.bytesize == 0
201
+ return false if data.bytesize.zero?
185
202
 
186
203
  pubkey = FFI::MemoryPointer.new(:uchar, pub_key.bytesize).put_bytes(0, pub_key)
187
204
  internal_pubkey = FFI::MemoryPointer.new(:uchar, 64)
@@ -190,11 +207,16 @@ module Bitcoin
190
207
 
191
208
  signature = FFI::MemoryPointer.new(:uchar, sig.bytesize).put_bytes(0, sig)
192
209
  internal_signature = FFI::MemoryPointer.new(:uchar, 64)
193
- result = secp256k1_ecdsa_signature_parse_der(context, internal_signature, signature, signature.size)
194
- #result = ecdsa_signature_parse_der_lax(context, internal_signature, signature, signature.size)
210
+ result = secp256k1_ecdsa_signature_parse_der(
211
+ context, internal_signature, signature, signature.size
212
+ )
213
+ # result = ecdsa_signature_parse_der_lax(
214
+ # context, internal_signature, signature, signature.size
215
+ # )
195
216
  return false unless result == 1
196
217
 
197
- # libsecp256k1's ECDSA verification requires lower-S signatures, which have not historically been enforced in Bitcoin, so normalize them first.
218
+ # libsecp256k1's ECDSA verification requires lower-S signatures, which have not historically
219
+ # been enforced in Bitcoin, so normalize them first.
198
220
  secp256k1_ecdsa_signature_normalize(context, internal_signature, internal_signature)
199
221
 
200
222
  msg32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, data)
@@ -204,30 +226,37 @@ module Bitcoin
204
226
  end
205
227
  end
206
228
 
207
- def self.sign_compact(message, priv_key, compressed=true)
229
+ def self.sign_compact(message, priv_key, compressed = true)
208
230
  with_context do |context|
209
231
  seckey = FFI::MemoryPointer.new(:uchar, priv_key.bytesize).put_bytes(0, priv_key)
210
- raise "priv_key invalid" unless secp256k1_ec_seckey_verify(context, seckey) == 1
232
+ raise 'priv_key invalid' unless secp256k1_ec_seckey_verify(context, seckey) == 1
211
233
 
212
234
  msg32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, message)
213
235
  internal_recoverable_signature = FFI::MemoryPointer.new(:uchar, 65)
214
236
  rec_id = FFI::MemoryPointer.new(:int).put_int(0, -1)
215
237
 
216
- ret, tries, max = 0, 0, 20
238
+ ret = 0
239
+ tries = 0
240
+ max = 20
217
241
  while ret != 1
218
- raise "secp256k1_ecdsa_sign_recoverable failed." if tries >= max
242
+ raise 'secp256k1_ecdsa_sign_recoverable failed.' if tries >= max
219
243
  tries += 1
220
244
 
221
- ret = secp256k1_ecdsa_sign_recoverable(context, internal_recoverable_signature, msg32, seckey, nil, nil)
245
+ ret = secp256k1_ecdsa_sign_recoverable(
246
+ context, internal_recoverable_signature, msg32, seckey, nil, nil
247
+ )
222
248
  end
223
249
 
224
250
  recoverable_signature = FFI::MemoryPointer.new(:uchar, 64)
225
- result = secp256k1_ecdsa_recoverable_signature_serialize_compact(context, recoverable_signature, rec_id, internal_recoverable_signature)
226
- raise "secp256k1_ecdsa_recoverable_signature_serialize_compact failed" unless result == 1
227
- raise "secp256k1_ecdsa_recoverable_signature_serialize_compact failed" unless rec_id.read_int != -1
228
-
229
- header = [27 + rec_id.read_int + (compressed ? 4 : 0)].pack("C")
230
- [ header, recoverable_signature.read_string(64) ].join
251
+ result = secp256k1_ecdsa_recoverable_signature_serialize_compact(
252
+ context, recoverable_signature, rec_id, internal_recoverable_signature
253
+ )
254
+ error_str = 'secp256k1_ecdsa_recoverable_signature_serialize_compact failed'
255
+ raise error_str unless result == 1
256
+ raise error_str unless rec_id.read_int != -1
257
+
258
+ header = [27 + rec_id.read_int + (compressed ? 4 : 0)].pack('C')
259
+ [header, recoverable_signature.read_string(64)].join
231
260
  end
232
261
  end
233
262
 
@@ -238,7 +267,7 @@ module Bitcoin
238
267
  version = signature.unpack('C')[0]
239
268
  return nil if version < 27 || version > 34
240
269
 
241
- compressed = version >= 31 ? true : false
270
+ compressed = version >= 31
242
271
  flag = compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED
243
272
  version -= 4 if compressed
244
273
 
@@ -247,20 +276,24 @@ module Bitcoin
247
276
  recoverable_signature = FFI::MemoryPointer.new(:uchar, 64).put_bytes(0, signature[1..-1])
248
277
 
249
278
  internal_recoverable_signature = FFI::MemoryPointer.new(:uchar, 65)
250
- result = secp256k1_ecdsa_recoverable_signature_parse_compact(context, internal_recoverable_signature, recoverable_signature, recid)
279
+ result = secp256k1_ecdsa_recoverable_signature_parse_compact(
280
+ context, internal_recoverable_signature, recoverable_signature, recid
281
+ )
251
282
  return nil unless result == 1
252
283
 
253
284
  internal_pubkey = FFI::MemoryPointer.new(:uchar, 64)
254
- result = secp256k1_ecdsa_recover(context, internal_pubkey, internal_recoverable_signature, msg32)
285
+ result = secp256k1_ecdsa_recover(
286
+ context, internal_pubkey, internal_recoverable_signature, msg32
287
+ )
255
288
  return nil unless result == 1
256
289
 
257
- pubkey, pubkey_len = FFI::MemoryPointer.new(:uchar, 65), FFI::MemoryPointer.new(:uint64).put_uint64(0, 65)
290
+ pubkey = FFI::MemoryPointer.new(:uchar, 65)
291
+ pubkey_len = FFI::MemoryPointer.new(:uint64).put_uint64(0, 65)
258
292
  result = secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, flag)
259
- raise "error serialize pubkey" unless (result == 1) || pubkey_len.read_uint64 > 0
293
+ raise 'error serialize pubkey' unless (result == 1) || pubkey_len.read_uint64 > 0
260
294
 
261
295
  pubkey.read_string(pubkey_len.read_uint64)
262
296
  end
263
297
  end
264
-
265
298
  end
266
299
  end
@@ -19,7 +19,6 @@ module Bitcoin
19
19
  autoload :Tx, 'bitcoin/protocol/tx'
20
20
  autoload :Block, 'bitcoin/protocol/block'
21
21
  autoload :Addr, 'bitcoin/protocol/address'
22
- autoload :Alert, 'bitcoin/protocol/alert'
23
22
  autoload :Reject, 'bitcoin/protocol/reject'
24
23
  autoload :Version, 'bitcoin/protocol/version'
25
24
  autoload :AuxPow, 'bitcoin/protocol/aux_pow'
@@ -62,7 +61,7 @@ module Bitcoin
62
61
 
63
62
  def self.unpack_var_string(payload)
64
63
  size, payload = unpack_var_int(payload)
65
- size > 0 ? (string, payload = payload.unpack("a#{size}a*")) : [nil, payload]
64
+ size > 0 ? (_, payload = payload.unpack("a#{size}a*")) : [nil, payload]
66
65
  end
67
66
 
68
67
  def self.unpack_var_string_from_io(buf)
@@ -114,7 +113,11 @@ module Bitcoin
114
113
  length = [payload.bytesize].pack("V")
115
114
  checksum = Digest::SHA256.digest(Digest::SHA256.digest(payload))[0...4]
116
115
  pkt = "".force_encoding(BINARY)
117
- pkt << Bitcoin.network[:magic_head].force_encoding(BINARY) << cmd.force_encoding(BINARY) << length << checksum << payload.force_encoding(BINARY)
116
+ pkt << Bitcoin.network[:magic_head].force_encoding(BINARY)
117
+ pkt << cmd.force_encoding(BINARY)
118
+ pkt << length
119
+ pkt << checksum
120
+ pkt << payload.dup.force_encoding(BINARY)
118
121
  end
119
122
 
120
123
  def self.version_pkt(from_id, from=nil, to=nil, last_block=nil, time=nil, user_agent=nil, version=nil)
@@ -2,9 +2,7 @@
2
2
 
3
3
  module Bitcoin
4
4
  module Protocol
5
-
6
- class Addr < Struct.new(:time, :service, :ip, :port)
7
-
5
+ Addr = Struct.new(:time, :service, :ip, :port) do
8
6
  # # IP Address / Port
9
7
  # attr_reader :ip, :port
10
8
 
@@ -17,22 +15,25 @@ module Bitcoin
17
15
  # create addr from raw binary +data+
18
16
  def initialize(data = nil)
19
17
  if data
20
- self[:time], self[:service], self[:ip], self[:port] = data.unpack("VQx12a4n")
21
- self[:ip] = ip.unpack("C*").join(".")
18
+ unpacked = data.unpack('VQx12a4n')
19
+ self[:time], self[:service], self[:ip], self[:port] = unpacked
20
+ self[:ip] = ip.unpack('C*').join('.')
22
21
  else
23
- self[:time], self[:service] = Time.now.to_i, 1
24
- self[:ip], self[:port] = "127.0.0.1", Bitcoin.network[:default_port]
22
+ self[:time] = Time.now.to_i
23
+ self[:service] = 1
24
+ self[:ip] = '127.0.0.1'
25
+ self[:port] = Bitcoin.network[:default_port]
25
26
  end
26
27
  end
27
28
 
28
29
  # is this address alive?
29
30
  def alive?
30
- (Time.now.tv_sec-7200) <= self[:time]
31
+ (Time.now.tv_sec - 7200) <= self[:time]
31
32
  end
32
33
 
33
34
  def to_payload
34
- ip = self[:ip].split(".").map(&:to_i)
35
- [ time, service, ("\x00"*10)+"\xff\xff", *ip, port ].pack("VQa12C4n")
35
+ ip = self[:ip].split('.').map(&:to_i)
36
+ [time, service, ("\x00" * 10) + "\xff\xff", *ip, port].pack('VQa12C4n')
36
37
  end
37
38
 
38
39
  def string
@@ -40,11 +41,12 @@ module Bitcoin
40
41
  end
41
42
 
42
43
  def self.pkt(*addrs)
43
- addrs = addrs.select{|i| i.is_a?(Bitcoin::Protocol::Addr) && i.ip =~ /^\d+\.\d+\.\d+\.\d+$/ }
44
+ addrs = addrs.select do |i|
45
+ i.is_a?(Bitcoin::Protocol::Addr) && i.ip =~ /^\d+\.\d+\.\d+\.\d+$/
46
+ end
44
47
  length = Bitcoin::Protocol.pack_var_int(addrs.size)
45
- Bitcoin::Protocol.pkt("addr", length + addrs.map(&:to_payload).join)
48
+ Bitcoin::Protocol.pkt('addr', length + addrs.map(&:to_payload).join)
46
49
  end
47
50
  end
48
-
49
51
  end
50
52
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  module Bitcoin
4
4
  module Protocol
5
-
6
5
  # Auxiliary Proof-of-Work for merge-mined blockchains
7
6
  # See https://en.bitcoin.it/wiki/Merged_mining_specification.
8
7
  #
@@ -20,7 +19,6 @@ module Bitcoin
20
19
  #
21
20
  # TODO: decode merged-mining data from +coinbase_tx+
22
21
  class AuxPow
23
-
24
22
  # Coinbase transaction of the parent block, linking to the child block
25
23
  attr_accessor :coinbase_tx
26
24
 
@@ -43,7 +41,7 @@ module Bitcoin
43
41
  attr_accessor :parent_block
44
42
 
45
43
  def initialize(data)
46
- parse_data (data) if data
44
+ parse_data data if data
47
45
  end
48
46
 
49
47
  def parse_data(data)
@@ -58,42 +56,43 @@ module Bitcoin
58
56
 
59
57
  @block_hash = data.read(32)
60
58
  coinbase_branch_count = P.unpack_var_int_from_io(data)
59
+
61
60
  @coinbase_branch = []
62
- coinbase_branch_count.times{
61
+ coinbase_branch_count.times do
63
62
  break if data.eof?
64
63
  @coinbase_branch << data.read(32).reverse.hth
65
- }
66
- @coinbase_index = data.read(4).unpack("I")[0]
64
+ end
65
+
66
+ @coinbase_index = data.read(4).unpack('I')[0]
67
67
 
68
68
  @chain_branch = []
69
69
  chain_branch_count = P.unpack_var_int_from_io(data)
70
- chain_branch_count.times{
70
+ chain_branch_count.times do
71
71
  break if data.eof?
72
72
  @chain_branch << data.read(32).reverse.hth
73
- }
73
+ end
74
74
 
75
- @chain_index = data.read(4).unpack("I")[0]
75
+ @chain_index = data.read(4).unpack('I')[0]
76
76
  block = data.read(80)
77
77
  @parent_block = P::Block.new(block)
78
78
 
79
79
  data
80
80
  end
81
81
 
82
-
83
82
  def to_payload
84
83
  payload = @coinbase_tx.to_payload
85
84
  payload << @block_hash
86
85
  payload << P.pack_var_int(@coinbase_branch.count)
87
86
  payload << @coinbase_branch.map(&:htb).map(&:reverse).join
88
- payload << [@coinbase_index].pack("I")
87
+ payload << [@coinbase_index].pack('I')
89
88
  payload << P.pack_var_int(@chain_branch.count)
90
89
  payload << @chain_branch.map(&:htb).map(&:reverse).join
91
- payload << [@chain_index].pack("I")
90
+ payload << [@chain_index].pack('I')
92
91
  payload << @parent_block.to_payload
93
92
  payload
94
93
  end
95
94
 
96
- def self.from_hash h
95
+ def self.from_hash(h)
97
96
  aux_pow = new(nil)
98
97
  aux_pow.instance_eval do
99
98
  @coinbase_tx = P::Tx.from_hash(h['coinbase_tx'])
@@ -116,8 +115,6 @@ module Bitcoin
116
115
  'chain_index' => @chain_index,
117
116
  'parent_block' => @parent_block.to_hash }
118
117
  end
119
-
120
118
  end
121
-
122
119
  end
123
120
  end