monacoin-ruby 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +5 -0
  4. data/COPYING +18 -0
  5. data/Gemfile +15 -0
  6. data/Gemfile.lock +33 -0
  7. data/README.rdoc +210 -0
  8. data/Rakefile +112 -0
  9. data/bin/monacoin_shell +12 -0
  10. data/bitcoin-ruby.gemspec +23 -0
  11. data/examples/bbe_verify_tx.rb +60 -0
  12. data/examples/concept-blockchain-pow.rb +151 -0
  13. data/examples/connect.rb +36 -0
  14. data/examples/generate_tx.rb +34 -0
  15. data/examples/simple_network_monitor_and_util.rb +195 -0
  16. data/spec/bitcoin/bitcoin_spec.rb +598 -0
  17. data/spec/bitcoin/bloom_filter_spec.rb +23 -0
  18. data/spec/bitcoin/builder_spec.rb +342 -0
  19. data/spec/bitcoin/contracthash_spec.rb +45 -0
  20. data/spec/bitcoin/dogecoin_spec.rb +176 -0
  21. data/spec/bitcoin/ext_key_spec.rb +180 -0
  22. data/spec/bitcoin/ffi_openssl.rb +45 -0
  23. data/spec/bitcoin/fixtures/000000000000056b1a3d84a1e2b33cde8915a4b61c0cae14fca6d3e1490b4f98.json +3697 -0
  24. data/spec/bitcoin/fixtures/03d7e1fa4d5fefa169431f24f7798552861b255cd55d377066fedcd088fb0e99.json +23 -0
  25. data/spec/bitcoin/fixtures/0961c660358478829505e16a1f028757e54b5bbf9758341a7546573738f31429.json +24 -0
  26. data/spec/bitcoin/fixtures/0f24294a1d23efbb49c1765cf443fba7930702752aba6d765870082fe4f13cae.json +37 -0
  27. data/spec/bitcoin/fixtures/156e6e1b84c5c3bd3a0927b25e4119fadce6e6d5186f363317511d1d680fae9a.json +24 -0
  28. data/spec/bitcoin/fixtures/23b397edccd3740a74adb603c9756370fafcde9bcc4483eb271ecad09a94dd63.json +23 -0
  29. data/spec/bitcoin/fixtures/315ac7d4c26d69668129cc352851d9389b4a6868f1509c6c8b66bead11e2619f.json +31 -0
  30. data/spec/bitcoin/fixtures/35e2001b428891fefa0bfb73167c7360669d3cbd7b3aa78e7cad125ddfc51131.json +27 -0
  31. data/spec/bitcoin/fixtures/3a17dace09ffb919ed627a93f1873220f4c975c1248558b18d16bce25d38c4b7.json +72 -0
  32. data/spec/bitcoin/fixtures/3e58b7eed0fdb599019af08578effea25c8666bbe8e200845453cacce6314477.json +27 -0
  33. data/spec/bitcoin/fixtures/514c46f0b61714092f15c8dfcb576c9f79b3f959989b98de3944b19d98832b58.json +24 -0
  34. data/spec/bitcoin/fixtures/51bf528ecf3c161e7c021224197dbe84f9a8564212f6207baa014c01a1668e1e.json +30 -0
  35. data/spec/bitcoin/fixtures/60a20bd93aa49ab4b28d514ec10b06e1829ce6818ec06cd3aabd013ebcdc4bb1.json +45 -0
  36. data/spec/bitcoin/fixtures/69216b8aaa35b76d6613e5f527f4858640d986e1046238583bdad79b35e938dc.json +28 -0
  37. data/spec/bitcoin/fixtures/7208e5edf525f04e705fb3390194e316205b8f995c8c9fcd8c6093abe04fa27d.json +27 -0
  38. data/spec/bitcoin/fixtures/761d8c5210fdfd505f6dff38f740ae3728eb93d7d0971fb433f685d40a4c04f6.json +27 -0
  39. data/spec/bitcoin/fixtures/8d0b238a06b5a70be75d543902d02d7a514d68d3252a949a513865ac3538874c.json +24 -0
  40. data/spec/bitcoin/fixtures/aea682d68a3ea5e3583e088dcbd699a5d44d4b083f02ad0aaf2598fe1fa4dfd4.json +27 -0
  41. data/spec/bitcoin/fixtures/bc179baab547b7d7c1d5d8d6f8b0cc6318eaa4b0dd0a093ad6ac7f5a1cb6b3ba.json +34 -0
  42. data/spec/bitcoin/fixtures/bd1715f1abfdc62bea3f605bdb461b3ba1f2cca6ec0d73a18a548b7717ca8531.json +34 -0
  43. data/spec/bitcoin/fixtures/block-testnet-0000000000ac85bb2530a05a4214a387e6be02b22d3348abc5e7a5d9c4ce8dab.bin +0 -0
  44. data/spec/bitcoin/fixtures/cd874fa8cb0e2ec2d385735d5e1fd482c4fe648533efb4c50ee53bda58e15ae2.json +24 -0
  45. data/spec/bitcoin/fixtures/ce5fad9b4ef094d8f4937b0707edaf0a6e6ceeaf67d5edbfd51f660eac8f398b.json +41 -0
  46. data/spec/bitcoin/fixtures/coinbase-toshi.json +33 -0
  47. data/spec/bitcoin/fixtures/coinbase.json +24 -0
  48. data/spec/bitcoin/fixtures/dogecoin-block-60323982f9c5ff1b5a954eac9dc1269352835f47c2c5222691d80f0d50dcf053.bin +0 -0
  49. data/spec/bitcoin/fixtures/f003f0c1193019db2497a675fd05d9f2edddf9b67c59e677c48d3dbd4ed5f00b.json +23 -0
  50. data/spec/bitcoin/fixtures/filteredblock-0.bin +0 -0
  51. data/spec/bitcoin/fixtures/litecoin-block-80ca095ed10b02e53d769eb6eaf92cd04e9e0759e5be4a8477b42911ba49c78f.bin +0 -0
  52. data/spec/bitcoin/fixtures/litecoin-block-80ca095ed10b02e53d769eb6eaf92cd04e9e0759e5be4a8477b42911ba49c78f.json +39 -0
  53. data/spec/bitcoin/fixtures/litecoin-genesis-block-12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2.bin +0 -0
  54. data/spec/bitcoin/fixtures/litecoin-genesis-block-12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2.json +39 -0
  55. data/spec/bitcoin/fixtures/litecoin-tx-f5aa30f574e3b6f1a3d99c07a6356ba812aabb9661e1d5f71edff828cbd5c996.json +259 -0
  56. data/spec/bitcoin/fixtures/rawblock-0.bin +0 -0
  57. data/spec/bitcoin/fixtures/rawblock-0.json +39 -0
  58. data/spec/bitcoin/fixtures/rawblock-1.bin +0 -0
  59. data/spec/bitcoin/fixtures/rawblock-1.json +39 -0
  60. data/spec/bitcoin/fixtures/rawblock-131025.bin +0 -0
  61. data/spec/bitcoin/fixtures/rawblock-131025.json +5063 -0
  62. data/spec/bitcoin/fixtures/rawblock-170.bin +0 -0
  63. data/spec/bitcoin/fixtures/rawblock-170.json +68 -0
  64. data/spec/bitcoin/fixtures/rawblock-9.bin +0 -0
  65. data/spec/bitcoin/fixtures/rawblock-9.json +39 -0
  66. data/spec/bitcoin/fixtures/rawblock-auxpow.bin +0 -0
  67. data/spec/bitcoin/fixtures/rawblock-testnet-1151351.bin +0 -0
  68. data/spec/bitcoin/fixtures/rawblock-testnet-26478.bin +0 -0
  69. data/spec/bitcoin/fixtures/rawblock-testnet-26478.json +64 -0
  70. data/spec/bitcoin/fixtures/rawblock-testnet-265322.bin +0 -0
  71. data/spec/bitcoin/fixtures/rawtx-01-toshi.json +46 -0
  72. data/spec/bitcoin/fixtures/rawtx-01.bin +0 -0
  73. data/spec/bitcoin/fixtures/rawtx-01.json +27 -0
  74. data/spec/bitcoin/fixtures/rawtx-02-toshi.json +46 -0
  75. data/spec/bitcoin/fixtures/rawtx-02.bin +0 -0
  76. data/spec/bitcoin/fixtures/rawtx-02.json +27 -0
  77. data/spec/bitcoin/fixtures/rawtx-03-toshi.json +73 -0
  78. data/spec/bitcoin/fixtures/rawtx-03.bin +0 -0
  79. data/spec/bitcoin/fixtures/rawtx-03.json +48 -0
  80. data/spec/bitcoin/fixtures/rawtx-04.json +27 -0
  81. data/spec/bitcoin/fixtures/rawtx-0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9.bin +0 -0
  82. data/spec/bitcoin/fixtures/rawtx-05.json +23 -0
  83. data/spec/bitcoin/fixtures/rawtx-14be6fff8c6014f7c9493b4a6e4a741699173f39d74431b6b844fcb41ebb9984.bin +0 -0
  84. data/spec/bitcoin/fixtures/rawtx-2f4a2717ec8c9f077a87dde6cbe0274d5238793a3f3f492b63c744837285e58a.bin +0 -0
  85. data/spec/bitcoin/fixtures/rawtx-2f4a2717ec8c9f077a87dde6cbe0274d5238793a3f3f492b63c744837285e58a.json +27 -0
  86. data/spec/bitcoin/fixtures/rawtx-406b2b06bcd34d3c8733e6b79f7a394c8a431fbf4ff5ac705c93f4076bb77602.json +23 -0
  87. data/spec/bitcoin/fixtures/rawtx-52250a162c7d03d2e1fbc5ebd1801a88612463314b55102171c5b5d817d2d7b2.bin +0 -0
  88. data/spec/bitcoin/fixtures/rawtx-b5d4e8883533f99e5903ea2cf001a133a322fa6b1370b18a16c57c946a40823d.bin +0 -0
  89. data/spec/bitcoin/fixtures/rawtx-ba1ff5cd66713133c062a871a8adab92416f1e38d17786b2bf56ac5f6ffdfdf5.json +37 -0
  90. data/spec/bitcoin/fixtures/rawtx-c99c49da4c38af669dea436d3e73780dfdb6c1ecf9958baa52960e8baee30e73.json +24 -0
  91. data/spec/bitcoin/fixtures/rawtx-de35d060663750b3975b7997bde7fb76307cec5b270d12fcd9c4ad98b279c28c.json +23 -0
  92. data/spec/bitcoin/fixtures/rawtx-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16.bin +0 -0
  93. data/spec/bitcoin/fixtures/rawtx-p2wpkh.bin +0 -0
  94. data/spec/bitcoin/fixtures/rawtx-p2wpkh.json +67 -0
  95. data/spec/bitcoin/fixtures/rawtx-testnet-04fdc38d6722ab4b12d79113fc4b2896bdcc5169710690ee4e78541b98e467b4.bin +0 -0
  96. data/spec/bitcoin/fixtures/rawtx-testnet-0b294c7d11dd21bcccb8393e6744fed7d4d1981a08c00e3e88838cc421f33c9f.bin +0 -0
  97. data/spec/bitcoin/fixtures/rawtx-testnet-3bc52ac063291ad92d95ddda5fd776a342083b95607ad32ed8bc6f8f7d30449e.bin +0 -0
  98. data/spec/bitcoin/fixtures/rawtx-testnet-6f0bbdd4e71a8af4305018d738184df32dbb6f27284fdebd5b56d16947f7c181.bin +0 -0
  99. data/spec/bitcoin/fixtures/rawtx-testnet-a220adf1902c46a39db25a24bc4178b6a88440f977a7e2cabfdd8b5c1dd35cfb.json +27 -0
  100. data/spec/bitcoin/fixtures/rawtx-testnet-a7c9b06e275e8674cc19a5f7d3e557c72c6d93576e635b33212dbe08ab7cdb60.bin +0 -0
  101. data/spec/bitcoin/fixtures/rawtx-testnet-e232e0055dbdca88bbaa79458683195a0b7c17c5b6c524a8d146721d4d4d652f.bin +0 -0
  102. data/spec/bitcoin/fixtures/rawtx-testnet-e232e0055dbdca88bbaa79458683195a0b7c17c5b6c524a8d146721d4d4d652f.json +41 -0
  103. data/spec/bitcoin/fixtures/rawtx-testnet-f80acbd2f594d04ddb0e1cacba662132104909157dff526935a3c88abe9201a5.bin +0 -0
  104. data/spec/bitcoin/fixtures/reorg/blk_0_to_4.dat +0 -0
  105. data/spec/bitcoin/fixtures/reorg/blk_3A.dat +0 -0
  106. data/spec/bitcoin/fixtures/reorg/blk_4A.dat +0 -0
  107. data/spec/bitcoin/fixtures/reorg/blk_5A.dat +0 -0
  108. data/spec/bitcoin/fixtures/script_tests.json +1947 -0
  109. data/spec/bitcoin/fixtures/sighash.json +1004 -0
  110. data/spec/bitcoin/fixtures/testnet/block_0.bin +0 -0
  111. data/spec/bitcoin/fixtures/testnet/block_1.bin +0 -0
  112. data/spec/bitcoin/fixtures/testnet/block_2.bin +0 -0
  113. data/spec/bitcoin/fixtures/testnet/block_3.bin +0 -0
  114. data/spec/bitcoin/fixtures/testnet/block_4.bin +0 -0
  115. data/spec/bitcoin/fixtures/testnet/block_5.bin +0 -0
  116. data/spec/bitcoin/fixtures/tx-0295028ef826b2a188409cb905b631faebb9bb3cdf14510571c5f4bd8591338f.json +64 -0
  117. data/spec/bitcoin/fixtures/tx-03339a725007a279484fb6f5361f522dd1cf4d0923d30e6b973290dba4275f92.json +64 -0
  118. data/spec/bitcoin/fixtures/tx-0a6a357e2f7796444e02638749d9611c008b253fb55f5dc88b739b230ed0c4c3.json +139 -0
  119. data/spec/bitcoin/fixtures/tx-0ce7e5238fbdb6c086cf1b384b21b827e91cc23f360417265874a5a0d86ce367.json +64 -0
  120. data/spec/bitcoin/fixtures/tx-0ef34c49f630aea17df0080728b0fc67bf5f87fbda936934a4b11b4a69d7821e.json +64 -0
  121. data/spec/bitcoin/fixtures/tx-1129d2a8bd5bb3a81e54dc96a90f1f6b2544575748caa17243470935c5dd91b7.json +28 -0
  122. data/spec/bitcoin/fixtures/tx-19aa42fee0fa57c45d3b16488198b27caaacc4ff5794510d0c17f173f05587ff.json +23 -0
  123. data/spec/bitcoin/fixtures/tx-1a4f3b9dc4494aeedeb39f30dd37e60541b2abe3ed4977992017cc0ad4f44956.json +64 -0
  124. data/spec/bitcoin/fixtures/tx-1f9191dcf2b1844ca28c6ef4b969e1d5fab70a5e3c56b7007949e55851cb0c4f.json +64 -0
  125. data/spec/bitcoin/fixtures/tx-22cd5fef23684d7b304e119bedffde6f54538d3d54a5bfa237e20dc2d9b4b5ad.json +64 -0
  126. data/spec/bitcoin/fixtures/tx-28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f.json +34 -0
  127. data/spec/bitcoin/fixtures/tx-2958fb00b4fd6fe0353503b886eb9a193d502f4fd5fc042d5e03216ba918bbd6.json +64 -0
  128. data/spec/bitcoin/fixtures/tx-29f277145749ad6efbed3ae6ce301f8d33c585ec26b7c044ad93c2f866e9e942.json +64 -0
  129. data/spec/bitcoin/fixtures/tx-2c5e5376c20e9cc78d0fb771730e5d840cc2096eff0ef045b599fe92475ace1c.json +28 -0
  130. data/spec/bitcoin/fixtures/tx-2c63aa814701cef5dbd4bbaddab3fea9117028f2434dddcdab8339141e9b14d1.json +30 -0
  131. data/spec/bitcoin/fixtures/tx-313897799b1e37e9ecae15010e56156dddde4e683c96b0e713af95272c38aee0.json +30 -0
  132. data/spec/bitcoin/fixtures/tx-326882a7f22b5191f1a0cc9962ca4b878cd969cf3b3a70887aece4d801a0ba5e.json +23 -0
  133. data/spec/bitcoin/fixtures/tx-345bed8785c3282a264ffb0dbee61cde54854f10e16f1b3e75b7f2d9f62946f2.json +64 -0
  134. data/spec/bitcoin/fixtures/tx-39ba7440b7103557560cc8ce258009936796485aaf8b478e66ab4cb97c66e31b.json +32 -0
  135. data/spec/bitcoin/fixtures/tx-3a04d57a833367f1655cc5ec3beb587888ef4977a86caa8c8ad4ba7cc717eae7.json +64 -0
  136. data/spec/bitcoin/fixtures/tx-3da75972766f0ad13319b0b461fd16823a731e44f6e9de4eb3c52d6a6fb6c8ae.json +23 -0
  137. data/spec/bitcoin/fixtures/tx-4142ee4877eb116abf955a7ec6ef2dc38133b793df762b76d75e3d7d4d8badc9.json +38 -0
  138. data/spec/bitcoin/fixtures/tx-44b833074e671120ba33106877b49e86ece510824b9af477a3853972bcd8d06a.json +30 -0
  139. data/spec/bitcoin/fixtures/tx-46224764c7870f95b58f155bce1e38d4da8e99d42dbb632d0dd7c07e092ee5aa.json +23 -0
  140. data/spec/bitcoin/fixtures/tx-5df1375ffe61ac35ca178ebb0cab9ea26dedbd0e96005dfcee7e379fa513232f.json +30 -0
  141. data/spec/bitcoin/fixtures/tx-62d9a565bd7b5344c5352e3e9e5f40fa4bbd467fa19c87357216ec8777ba1cce.json +64 -0
  142. data/spec/bitcoin/fixtures/tx-6327783a064d4e350c454ad5cd90201aedf65b1fc524e73709c52f0163739190.json +23 -0
  143. data/spec/bitcoin/fixtures/tx-6606c366a487bff9e412d0b6c09c14916319932db5954bf5d8719f43f828a3ba.json +27 -0
  144. data/spec/bitcoin/fixtures/tx-6aaf18b9f1283b939d8e5d40ff5f8a435229f4178372659cc3a0bce4e262bf78.json +28 -0
  145. data/spec/bitcoin/fixtures/tx-6b48bba6f6d2286d7ec0883c0fc3085955090813a4c94980466611c798b868cc.json +64 -0
  146. data/spec/bitcoin/fixtures/tx-70cfbc6690f9ab46712db44e3079ac227962b2771a9341d4233d898b521619ef.json +40 -0
  147. data/spec/bitcoin/fixtures/tx-7a1a9db42f065f75110fcdb1bc415549c8ef7670417ba1d35a67f1b8adc562c1.json +64 -0
  148. data/spec/bitcoin/fixtures/tx-9a768fc7d0c4bdc86e25154357ef7c0063ca21310e5740a2f12f90b7455184a7.json +64 -0
  149. data/spec/bitcoin/fixtures/tx-9cad8d523a0694f2509d092c39cebc8046adae62b4e4297102d568191d9478d8.json +64 -0
  150. data/spec/bitcoin/fixtures/tx-9e052eb694bd7e15906433f064dff0161a12fd325c1124537766377004023c6f.json +64 -0
  151. data/spec/bitcoin/fixtures/tx-9fb65b7304aaa77ac9580823c2c06b259cc42591e5cce66d76a81b6f51cc5c28.json +23 -0
  152. data/spec/bitcoin/fixtures/tx-a6ce7081addade7676cd2af75c4129eba6bf5e179a19c40c7d4cf6a5fe595954.json +30 -0
  153. data/spec/bitcoin/fixtures/tx-a955032f4d6b0c9bfe8cad8f00a8933790b9c1dc28c82e0f48e75b35da0e4944.json +23 -0
  154. data/spec/bitcoin/fixtures/tx-aab7ef280abbb9cc6fbaf524d2645c3daf4fcca2b3f53370e618d9cedf65f1f8.json +23 -0
  155. data/spec/bitcoin/fixtures/tx-ab9805c6d57d7070d9a42c5176e47bb705023e6b67249fb6760880548298e742.json +27 -0
  156. data/spec/bitcoin/fixtures/tx-ad4bcf3241e5d2ad140564e20db3567d41594cf4c2012433fe46a2b70e0d87b8.json +64 -0
  157. data/spec/bitcoin/fixtures/tx-b5b598de91787439afd5938116654e0b16b7a0d0f82742ba37564219c5afcbf9.json +27 -0
  158. data/spec/bitcoin/fixtures/tx-b8fd633e7713a43d5ac87266adc78444669b987a56b3a65fb92d58c2c4b0e84d.json +28 -0
  159. data/spec/bitcoin/fixtures/tx-bbca0628c42cb8bf7c3f4b2ad688fa56da5308dd2a10255da89fb1f46e6e413d.json +36 -0
  160. data/spec/bitcoin/fixtures/tx-bc7fd132fcf817918334822ee6d9bd95c889099c96e07ca2c1eb2cc70db63224.json +23 -0
  161. data/spec/bitcoin/fixtures/tx-c192b74844e4837a34c4a5a97b438f1c111405b01b99e2d12b7c96d07fc74c04.json +28 -0
  162. data/spec/bitcoin/fixtures/tx-d3d77d63709e47d9ef58f0b557800115a6b676c6a423012fbb96f45d8fcef830.json +28 -0
  163. data/spec/bitcoin/fixtures/tx-e335562f7e297aadeed88e5954bc4eeb8dc00b31d829eedb232e39d672b0c009.json +406 -0
  164. data/spec/bitcoin/fixtures/tx-eb3b82c0884e3efa6d8b0be55b4915eb20be124c9766245bcc7f34fdac32bccb.json +35 -0
  165. data/spec/bitcoin/fixtures/tx-fee1b9b85531c8fb6cd7831f83490c7f2aa768b6eefe29854ef5e89ce7b9ecb1.json +64 -0
  166. data/spec/bitcoin/fixtures/txdp-1.txt +32 -0
  167. data/spec/bitcoin/fixtures/txdp-2-signed.txt +19 -0
  168. data/spec/bitcoin/fixtures/txdp-2-unsigned.txt +14 -0
  169. data/spec/bitcoin/fixtures/txscript-invalid-too-many-sigops-followed-by-invalid-pushdata.bin +1 -0
  170. data/spec/bitcoin/helpers/fake_blockchain.rb +183 -0
  171. data/spec/bitcoin/key_spec.rb +326 -0
  172. data/spec/bitcoin/network_spec.rb +50 -0
  173. data/spec/bitcoin/performance/storage_spec.rb +41 -0
  174. data/spec/bitcoin/protocol/addr_spec.rb +82 -0
  175. data/spec/bitcoin/protocol/alert_spec.rb +22 -0
  176. data/spec/bitcoin/protocol/aux_pow_spec.rb +45 -0
  177. data/spec/bitcoin/protocol/bip143_spec.rb +116 -0
  178. data/spec/bitcoin/protocol/block_spec.rb +208 -0
  179. data/spec/bitcoin/protocol/getblocks_spec.rb +32 -0
  180. data/spec/bitcoin/protocol/inv_spec.rb +134 -0
  181. data/spec/bitcoin/protocol/notfound_spec.rb +31 -0
  182. data/spec/bitcoin/protocol/parser_spec.rb +50 -0
  183. data/spec/bitcoin/protocol/partial_merkle_tree_spec.rb +38 -0
  184. data/spec/bitcoin/protocol/ping_spec.rb +51 -0
  185. data/spec/bitcoin/protocol/reject.rb +17 -0
  186. data/spec/bitcoin/protocol/tx_spec.rb +894 -0
  187. data/spec/bitcoin/protocol/txin_spec.rb +45 -0
  188. data/spec/bitcoin/protocol/txout_spec.rb +27 -0
  189. data/spec/bitcoin/protocol/version_spec.rb +110 -0
  190. data/spec/bitcoin/script/opcodes_spec.rb +773 -0
  191. data/spec/bitcoin/script/script_spec.rb +971 -0
  192. data/spec/bitcoin/secp256k1_spec.rb +78 -0
  193. data/spec/bitcoin/spec_helper.rb +108 -0
  194. data/spec/bitcoin/trezor/mnemonic_spec.rb +161 -0
  195. metadata +237 -0
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Fetch a transaction from blockexplorer.com and verify all signatures.
4
+ #
5
+ # examples/bbe_verify_tx.rb <tx hash> [testnet]
6
+ # examples/bbe_verify_tx.rb f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16
7
+ # see Bitcoin::P::Tx and Bitcoin::Script.
8
+
9
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
10
+ require 'bitcoin'
11
+ require 'open-uri'
12
+
13
+ tx_hash = ARGV[0]
14
+ $testnet = ARGV.select{|i| i.downcase == 'testnet' }[0] ? true : false
15
+ $use_coinbase_bbe = ARGV.select{|i| i.downcase == 'coinbase' }[0] ? true : false
16
+
17
+ # fetch transaction from bbe as json and deserialize into Bitcoin::Protocol::Tx object
18
+ def get_tx(hash)
19
+ if $use_coinbase_bbe && !$testnet
20
+ url = "https://coinbase.com/network/tx/%s.json" % [hash]
21
+ else
22
+ url = "http://blockexplorer.com/%srawtx/%s" % [$testnet ? 'testnet/' : '', hash]
23
+ end
24
+ json = open(url).read
25
+ Bitcoin::Protocol::Tx.from_json(json)
26
+ rescue
27
+ nil
28
+ end
29
+
30
+ tx1 = get_tx(tx_hash)
31
+
32
+ unless tx1
33
+ puts "Tx #{tx_hash} not found."
34
+ exit
35
+ end
36
+
37
+ if tx1.in.all?{|txin| txin.coinbase? }
38
+ puts "Tx #{tx_hash} is a coinbase transaction. Check the block instead."
39
+ exit
40
+ end
41
+
42
+ tx1.in.each_with_index do |txin, idx|
43
+ if txin.coinbase?
44
+ puts "skipping coinbase transaction input.."; next
45
+ end
46
+
47
+ prev_tx = get_tx(txin.previous_output)
48
+ unless prev_tx
49
+ puts "Missing prev_out tx for input #{idx} of tx #{tx_hash}!"
50
+ exit
51
+ end
52
+
53
+ result = tx1.verify_input_signature(idx, prev_tx)
54
+ unless result
55
+ puts "Input #{idx} of tx #{tx_hash} is invalid!"
56
+ exit
57
+ end
58
+ end
59
+
60
+ puts "Tx #{tx_hash} is valid."
@@ -0,0 +1,151 @@
1
+ #
2
+ # Ruby Example of the 'proof of work' explanation
3
+ # from https://en.bitcoin.it/wiki/Proof_of_work
4
+ #
5
+ # note: block data passed to do_work is simplified.
6
+ # bitcoin blockchain is more complex too.
7
+ #
8
+
9
+ require 'digest/sha2'
10
+
11
+
12
+ def do_work(data, target, nonce=0)
13
+ found = nil
14
+ until found
15
+ d = data + [nonce].pack("I")
16
+ h = Digest::SHA256.hexdigest( Digest::SHA256.digest( d ) ).to_i(16)
17
+
18
+ if h <= target
19
+ found = [h.to_s(16).rjust(64, '0'), nonce]
20
+ break
21
+ end
22
+
23
+ nonce+=1
24
+ end
25
+ found
26
+ end
27
+
28
+
29
+ def next_block(blocks, target, data)
30
+ block_id = blocks.size
31
+ last_block = last_hash(blocks)
32
+ data = last_block + " " + data
33
+
34
+ hash, nonce = nil, nil
35
+
36
+ work_time = t{ hash, nonce = do_work(data, target.to_i(16)) }
37
+ verify_time = t{ hash, nonce = do_work(data, target.to_i(16), nonce) }
38
+
39
+ print_block( block_id, target, data, nonce, hash, work_time, verify_time )
40
+
41
+ [ hash, nonce, target, data, work_time, verify_time ]
42
+ end
43
+
44
+
45
+
46
+ def print_block(*args)
47
+ puts <<-TEXT % args
48
+ -------------------- block %s
49
+ target: %s
50
+ data: '%s' + %s (nonce)
51
+ found: %s
52
+
53
+ time:
54
+ took: %f
55
+ verify: %f
56
+
57
+ TEXT
58
+ end
59
+
60
+ def last_hash(blocks)
61
+ if blocks.empty?
62
+ "0000000000000000000000000000000000000000000000000000000000000000"
63
+ else
64
+ blocks.last[0]
65
+ end
66
+ end
67
+
68
+ def t; x = Time.now; yield; Time.now - x; end
69
+
70
+
71
+
72
+ if $0 == __FILE__
73
+
74
+ target = "00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
75
+ blocks = []
76
+
77
+
78
+ blocks << next_block( blocks, target, "hello ruby" )
79
+ blocks << next_block( blocks, target, "this is" )
80
+ blocks << next_block( blocks, target, "a blockchain" )
81
+ blocks << next_block( blocks, target, "and proof-of-work" )
82
+ blocks << next_block( blocks, target, "example!" )
83
+
84
+
85
+ puts <<-TEXT % [ blocks.size, target, blocks.inject(0){|e,i| e+=i[-2] }, blocks.inject(0){|e,i| e+=i[-1] } ]
86
+ -------------------- blockchain time summary
87
+ chain length: %d
88
+ difficulty: %s
89
+ total work time: %f
90
+ total verify time: %f
91
+
92
+ TEXT
93
+
94
+ end
95
+
96
+
97
+ __END__
98
+ % ruby concept-examples/blockchain-pow.rb
99
+
100
+ -------------------- block 0
101
+ target: 00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
102
+ data: '0000000000000000000000000000000000000000000000000000000000000000 hello ruby' + 1373297 (nonce)
103
+ found: 00000b1522d81f532d5e33c4fd22537b66f1ff052315b47000e61496510ceaa2
104
+
105
+ time:
106
+ took: 41.073509
107
+ verify: 0.000060
108
+
109
+ -------------------- block 1
110
+ target: 00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
111
+ data: '00000b1522d81f532d5e33c4fd22537b66f1ff052315b47000e61496510ceaa2 this is' + 2877742 (nonce)
112
+ found: 0000006f3ac527d921b57a88d3d6e5793f0813d4fafb17e0b31456ad1d652e05
113
+
114
+ time:
115
+ took: 86.098830
116
+ verify: 0.000058
117
+
118
+ -------------------- block 2
119
+ target: 00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
120
+ data: '0000006f3ac527d921b57a88d3d6e5793f0813d4fafb17e0b31456ad1d652e05 a blockchain' + 255946 (nonce)
121
+ found: 000000196f5e67ca66f281cf6b884b312984173651ddf12e151db6b1428a882b
122
+
123
+ time:
124
+ took: 7.648288
125
+ verify: 0.000057
126
+
127
+ -------------------- block 3
128
+ target: 00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
129
+ data: '000000196f5e67ca66f281cf6b884b312984173651ddf12e151db6b1428a882b and proof-of-work' + 2930300 (nonce)
130
+ found: 00000a197372b28e93479598afa504c92bcb50af2c5c6893686e91ce47ad0747
131
+
132
+ time:
133
+ took: 87.530473
134
+ verify: 0.000058
135
+
136
+ -------------------- block 4
137
+ target: 00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
138
+ data: '00000a197372b28e93479598afa504c92bcb50af2c5c6893686e91ce47ad0747 example!' + 85074 (nonce)
139
+ found: 00000a406be803d6a02c5aded00c159b664fe11ed768dbcb3ecb7b2ec6257706
140
+
141
+ time:
142
+ took: 2.555810
143
+ verify: 0.000058
144
+
145
+ -------------------- blockchain time summary
146
+ chain length: 5
147
+ difficulty: 00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
148
+ total work time: 224.906909
149
+ total verify time: 0.000292
150
+
151
+
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Connect to a random peer and download the first 500 blocks.
4
+ #
5
+ # examples/connect.rb [testnet]
6
+ #
7
+ # see Bitcoin::Connection and Bitcoin::Protocol.
8
+
9
+ $:.unshift( File.expand_path("../../lib", __FILE__) )
10
+ require 'bitcoin/connection'
11
+
12
+ Bitcoin::network = ARGV[0] || :bitcoin
13
+
14
+ class RawJSON_Connection < Bitcoin::Connection
15
+ def on_tx(tx)
16
+ p ['tx', tx.hash, Time.now]
17
+ # puts tx.to_json
18
+ end
19
+
20
+ def on_block(block)
21
+ p ['block', block.hash, Time.now]
22
+ # puts block.to_json
23
+ end
24
+ end
25
+
26
+ EM.run do
27
+
28
+ host = '127.0.0.1'
29
+ #host = '217.157.1.202'
30
+
31
+ connections = []
32
+ #RawJSON_Connection.connect(host, 8333, connections)
33
+
34
+ RawJSON_Connection.connect_random_from_dns(connections)
35
+
36
+ end
@@ -0,0 +1,34 @@
1
+ $:.unshift( File.expand_path("../../lib", __FILE__) )
2
+ require 'bitcoin'
3
+
4
+ # p Bitcoin.generate_address # returns address, privkey, pubkey, hash160
5
+
6
+ prev_tx = Bitcoin::Protocol::Tx.from_json_file('baedb362adba39753a7d2c58fd3dc4897a1b479859f707a819f096696f3facad.json') # <- redeeming transaction input fetchted by for example simple_network_monitor_and_util.rb
7
+ prev_tx_output_index = 0
8
+ value = prev_tx.outputs[prev_tx_output_index].value
9
+ #value = 1337 # maybe change the value (eg subtract for fees)
10
+
11
+
12
+ tx = Bitcoin::Protocol::Tx.new
13
+ tx.add_in Bitcoin::Protocol::TxIn.new(prev_tx.binary_hash, prev_tx_output_index, 0)
14
+
15
+ tx.add_out Bitcoin::Protocol::TxOut.value_to_address(value, "1MiQ3zD3hzZBZ4cUDfPd8Eqnjcedkwt5jy") # <- dest address (our donation address)
16
+
17
+ # if all in and outputs are defined, start signing inputs.
18
+ key = Bitcoin.open_key("9b2f08ebc186d435ffc1d10f3627f05ce4b983b72c76b0aee4fcce99e57b0342") # <- privkey
19
+ sig = Bitcoin.sign_data(key, tx.signature_hash_for_input(0, prev_tx))
20
+ tx.in[0].script_sig = Bitcoin::Script.to_signature_pubkey_script(sig, [key.public_key_hex].pack("H*"))
21
+ #tx.in[0].add_signature_pubkey_script(sig, key.public_key_hex)
22
+
23
+ # finish check
24
+ tx = Bitcoin::Protocol::Tx.new( tx.to_payload )
25
+ p tx.hash
26
+ p tx.verify_input_signature(0, prev_tx) == true
27
+
28
+ puts "json:\n"
29
+ puts tx.to_json # json
30
+ puts "\nhex:\n"
31
+ puts tx.to_payload.unpack("H*")[0] # hex binary
32
+
33
+ # use this json file for example with `ruby simple_network_monitor_and_util.rb send_tx=<filename>` to push/send it to the network
34
+ File.open(tx.hash + ".json", 'wb'){|f| f.print tx.to_json }
@@ -0,0 +1,195 @@
1
+ $:.unshift( File.expand_path("../../lib", __FILE__) )
2
+ require 'eventmachine'
3
+ require 'bitcoin'
4
+ require 'socket'
5
+
6
+ class Bitcoin::Protocol::Parser; def log; stub=Object.new; def stub.method_missing(*a); end; stub; end; end
7
+
8
+
9
+ module SimpleNode
10
+ class Connection < EM::Connection
11
+
12
+ def on_ping(nonce)
13
+ send_data(Bitcoin::Protocol.pong_pkt(nonce)) if nonce
14
+ end
15
+
16
+ def on_reject(reject)
17
+ log.info { "reject #{reject}" }
18
+ end
19
+
20
+ def on_tx(tx)
21
+ log.info { "received transaction: #{tx.hash}" }
22
+
23
+ puts tx.to_json
24
+
25
+ if tx.hash == @ask_tx
26
+ @args[:result] = tx
27
+ @args[:callback] ? (close_connection; @args[:callback].call(tx)) : EM.stop
28
+ end
29
+ end
30
+
31
+ def on_block(block)
32
+ log.info { "received block: #{block.hash}" }
33
+
34
+ puts block.to_json
35
+
36
+ if block.hash == @ask_block
37
+ if @ask_tx
38
+ if tx = block.tx.find{|tx| tx.hash == @ask_tx }
39
+ on_tx(tx)
40
+ else
41
+ log.info { "@ask_tx #{@ask_tx} not in @ask_block #{@ask_block}" }
42
+ @args[:result] = nil
43
+ @args[:callback] ? (close_connection; @args[:callback].call(nil)) : EM.stop
44
+ end
45
+ else
46
+ @args[:result] = block
47
+ @args[:callback] ? (close_connection; @args[:callback].call(block)) : EM.stop
48
+ end
49
+ end
50
+ end
51
+
52
+ def on_handshake_complete
53
+ return if @connected
54
+ @connected = true
55
+ log.info { "handshake complete" }
56
+
57
+ EM.add_timer(0.5){
58
+ if @ask_block
59
+ log.info { "ask for @ask_block: #{@ask_block}" }
60
+ send_data Bitcoin::Protocol.getdata_pkt(:block, [htb(@ask_block)])
61
+ else
62
+ if @ask_tx
63
+ log.info { "ask for @ask_tx: #{@ask_tx}" }
64
+ send_data Bitcoin::Protocol.getdata_pkt(:tx, [htb(@ask_tx)])
65
+ end
66
+ end
67
+ if @send_tx
68
+ tx = Bitcoin::P::Tx.from_json(File.read(@send_tx))
69
+ send_data(Bitcoin::Protocol.pkt('tx', tx.to_payload))
70
+ p [:sent, tx.hash]
71
+ end
72
+ }
73
+ end
74
+
75
+ def on_get_transaction(hash); end
76
+ def on_get_block(hash); end
77
+ def on_addr(addr); end
78
+ def on_inv_transaction(hash)
79
+ log.info { "peer told us about transaction: #{hth(hash)}" }
80
+ log.info { "asking peer for transaction: #{hth(hash)}" }
81
+ send_data Bitcoin::Protocol.getdata_pkt(:tx, [hash])
82
+ end
83
+
84
+ def on_inv_block(hash)
85
+ log.info { "peer told us about block: #{hth(hash)}" }
86
+ log.info { "asking peer for block: #{hth(hash)}" }
87
+ send_data Bitcoin::Protocol.getdata_pkt(:block, [hash])
88
+ end
89
+
90
+ def on_handshake_begin
91
+ log.info { "handshake started" }
92
+
93
+ version = Bitcoin::Protocol::Version.new({
94
+ :user_agent => "/Satoshi:0.8.1/",
95
+ :last_block => 0,
96
+ :from => "127.0.0.1:#{Bitcoin.network[:default_port]}",
97
+ :to => "#{@host}:#{@port}",
98
+ })
99
+
100
+ log.info { "sending version: Version:%d (%s) Block:%d" % version.fields.values_at(:version, :user_agent, :last_block) }
101
+ send_data(version.to_pkt)
102
+ end
103
+
104
+ def on_version(version)
105
+ @version ||= version
106
+ log.info { "received version: Version:%d (%s) Block:%d" % version.fields.values_at(:version, :user_agent, :last_block) }
107
+ send_data( Bitcoin::Protocol.verack_pkt )
108
+ on_handshake_complete
109
+ end
110
+
111
+ def initialize(host, port, node=nil, opts={})
112
+ set_host(host, port)
113
+ @node = node
114
+ @parser = Bitcoin::Protocol::Parser.new( self )
115
+
116
+ @args = opts
117
+ @ask_tx, @ask_block, @send_tx = opts.values_at(:ask_tx, :ask_block, :send_tx)
118
+ end
119
+
120
+ def receive_data(data); @parser.parse(data); end
121
+ def post_init; log.info { "peer connected" }; on_handshake_begin; end
122
+ def unbind; log.info { "peer disconnected" }; end
123
+ def set_host(host, port=8333); @host, @port = host, port; end
124
+
125
+ def log
126
+ return @log if @log
127
+ return (@log = (stub=Object.new; def stub.method_missing(*a); end; stub)) if @args[:nolog]
128
+ @logger ||= Bitcoin::Logger.create(:network, :info) unless @node.respond_to?(:log)
129
+ @log = Bitcoin::Logger::LogWrapper.new("#@host:#@port", @logger || @node.log)
130
+ end
131
+
132
+ def hth(h); h.unpack("H*")[0]; end
133
+ def htb(h); [h].pack("H*"); end
134
+
135
+
136
+ def self.connect(host, port, *args)
137
+ EM.connect(host, port, self, host, port, *args)
138
+ end
139
+
140
+ def self.connect_random_from_dns(seeds=[], count=1, *args)
141
+ seeds = Bitcoin.network[:dns_seeds] unless seeds.any?
142
+ if seeds.any?
143
+ seeds.sample(count).map{|dns|
144
+ host = IPSocket.getaddress(dns)
145
+ connect(host, Bitcoin.network[:default_port], *args)
146
+ }
147
+ else
148
+ raise "No DNS seeds available. Provide IP, configure seeds, or use different network."
149
+ end
150
+ end
151
+
152
+ def self.connect_known_nodes(count=1)
153
+ connect_random_from_dns(Bitcoin.network[:known_nodes], count)
154
+ end
155
+ end
156
+ end
157
+
158
+
159
+ if $0 == __FILE__
160
+
161
+ args = {
162
+ ask_tx: ARGV.find{|a| a[/tx=(.+)/, 1] } && $1,
163
+ ask_block: ARGV.find{|a| a[/block=(.+)/, 1] } && $1,
164
+ use_node: ARGV.find{|a| a[/node=(.+)/, 1] } && $1,
165
+ send_tx: ARGV.find{|a| a[/send_tx=(.+)/, 1] } && $1,
166
+ set_project: ARGV.find{|a| a[/project=(.+)/, 1] } && $1,
167
+ callback: proc{|i|
168
+ case i
169
+ when Bitcoin::Protocol::Block
170
+ puts "INFO network: SAVING @ask_block: #{i.hash}"
171
+ File.open("block-#{i.hash}.bin", 'wb'){|f| f.print i.payload }
172
+ File.open("block-#{i.hash}.json", 'wb'){|f| f.print i.to_json }
173
+ when Bitcoin::Protocol::Tx
174
+ puts "INFO network: SAVING @ask_tx: #{i.hash}"
175
+ File.open("tx-#{i.hash}.bin", 'wb'){|f| f.print i.payload }
176
+ File.open("tx-#{i.hash}.json", 'wb'){|f| f.print i.to_json }
177
+ end
178
+ EM.stop
179
+ }
180
+ }
181
+
182
+
183
+ EM.run do
184
+ if args[:set_project]
185
+ Bitcoin.network = args[:set_project].to_sym
186
+ p Bitcoin.network_project
187
+ end
188
+ if args[:use_node]
189
+ SimpleNode::Connection.connect_random_from_dns([args[:use_node]], 1, nil, args)
190
+ else
191
+ SimpleNode::Connection.connect_random_from_dns([], 1, nil, args)
192
+ end
193
+ end
194
+
195
+ end