spec/bitcoin/bitcoin_spec.rb in bitcoin-ruby-0.0.1 vs spec/bitcoin/bitcoin_spec.rb in bitcoin-ruby-0.0.2

- old
+ new

@@ -1,5 +1,7 @@ +# encoding: ascii-8bit + require_relative 'spec_helper.rb' require 'bitcoin' describe 'Bitcoin Address/Hash160/PubKey' do @@ -162,19 +164,24 @@ "186640daf908156e2616790d7c816235b0c43f668c3c38351b348c08ca44d457", "ef3928700309b4deceac9a992a19a7481b4e520cbc0b1ab74e2645eee39c8da0", "688c53517f62f7a65c0e87519c18a4de98f2ccafbf389b269d0bb867f88d166a", "01889506f7fe9210045f588361881e2d16a034a62bc48ebd7b6b0a3edeaf5a6d", + "74f3a7df861d6a58957b84a3e425a8cf57e1e2e3a3def046dd200baeb8714f00" ] Bitcoin.hash_mrkl_tree( mrkl_tree[0...3] ).should == mrkl_tree Bitcoin.bitcoin_mrkl( mrkl_tree[0], mrkl_tree[1] ).should == mrkl_tree[3] Bitcoin.bitcoin_mrkl( mrkl_tree[2], mrkl_tree[2] ).should == mrkl_tree[4] Bitcoin.bitcoin_mrkl( mrkl_tree[3], mrkl_tree[4] ).should == mrkl_tree[5] + mrkl_tree[0...3].each.with_index do |target, idx| + branch = Bitcoin.hash_mrkl_branch( mrkl_tree[0...3], target ) + Bitcoin.mrkl_branch_root( branch, target, idx ).should == mrkl_tree[-1] + end mrkl_tree = [ "349f717b6630e1f305f95964a2d94117dacca76e0b715d4d7a5657698ec96c6c", # 0 "7f44a84349200473455bcfc05ee68036e23993a6f58dce3f6a7faab46a754440", # 1 "6b3ba3fdfb7eeb6c2e5fd0e36e5bb4634da294521f7b1b808286c214981f9b17", # 2 @@ -201,10 +208,14 @@ Bitcoin.bitcoin_mrkl( mrkl_tree[9], mrkl_tree[10] ).should == mrkl_tree[11] Bitcoin.hash_mrkl_tree(mrkl_tree[0...6]).should == mrkl_tree + mrkl_tree[0...5].each.with_index do |target, idx| + branch = Bitcoin.hash_mrkl_branch( mrkl_tree[0..5], target ) + Bitcoin.mrkl_branch_root( branch, target, idx ).should == mrkl_tree[-1] + end mrkl_tree = [ "627c859b5af6d537930fd16148eb0597542bea543f65fc2b0e5f188b5a458529", # 0 "d00b90525820a74f30ce26488db7f77c6ee9577e650568a051edd8560bbf83a1", # 1 "706b8ac1a433bc28385450626e12c1c7806032dc8b7e12221f417c5f22059d70", # 2 @@ -245,12 +256,23 @@ Bitcoin.bitcoin_mrkl( mrkl_tree[16], mrkl_tree[16] ).should == mrkl_tree[18] Bitcoin.bitcoin_mrkl( mrkl_tree[17], mrkl_tree[18] ).should == mrkl_tree[19] Bitcoin.hash_mrkl_tree(mrkl_tree[0...9]).should == mrkl_tree + + mrkl_tree[0..8].each.with_index do |target, idx| + branch = Bitcoin.hash_mrkl_branch( mrkl_tree[0..8], target ) + Bitcoin.mrkl_branch_root( branch, target, idx ).should == mrkl_tree[-1] + end + end + it 'should not allow duplicate hash in merkle trees' do + Bitcoin.hash_mrkl_tree(["aa", "bb", "cc"]).last.should != + Bitcoin.hash_mrkl_tree(["aa", "bb", "cc", "cc"]).last + end + it 'nonce compact bits to bignum hex' do Bitcoin.decode_compact_bits( "1b00b5ac".to_i(16) ).index(/[^0]/).should == 12 Bitcoin.decode_compact_bits( "1b00b5ac".to_i(16) ).to_i(16).should == "000000000000b5ac000000000000000000000000000000000000000000000000".to_i(16) @@ -328,20 +350,39 @@ end begin Bitcoin::OpenSSL_EC it 'opens key from private key and resolves public key' do - private_key, public_key = Bitcoin.generate_key - key = Bitcoin.open_key(private_key) - [ key.private_key_hex, key.public_key_hex ].should == [ private_key, public_key ] + 50.times.all?{ + private_key, public_key = Bitcoin.generate_key + key = Bitcoin.open_key(private_key) + [ key.private_key_hex, key.public_key_hex ] == [ private_key, public_key ] + }.should == true end it 'extract private key from uncompressed DER format' do der = "308201130201010420a29fe0f28b2936dbc89f889f74cd1f0662d18a873ac15d6cd417b808db1ccd0aa081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a14403420004768cfc6c44b927b0e69e9dd343e96132f7cd1d360d8cb8d65c83d89d7beaceadfd19918e076606a099344156acdb026b1065a958e39f098cfd0a34dd976291d6" Bitcoin::OpenSSL_EC.der_to_private_key(der).should == "a29fe0f28b2936dbc89f889f74cd1f0662d18a873ac15d6cd417b808db1ccd0a" end + + it 'sign and verify text messages (signmessage/verifymessage)' do + [ + ["1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ", "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747", + "040b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a7447b5f0bba9e01e6fe4735c8383e6e7a3347a0fd72381b8f797a19f694054e5a69"], + ["1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs", "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747", + "030b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a744"] + ].each{|address, privkey, pubkey| + key = Bitcoin.open_key(privkey) + 16.times.all?{|n| + #10_000.times.all?{|n| + # puts 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip if (n % 1_000) == 0 + s = Bitcoin.sign_message(key.private_key_hex, key.public_key_hex, "Very secret message %d: 11" % n) + Bitcoin.verify_message(s['address'], s['signature'], s['message']) + }.should == true + } + end rescue LoadError end it 'generates new bitcoin-address' do address, private_key, public_key, hash160 = Bitcoin.generate_address @@ -408,9 +449,13 @@ "0.0000000000000001329335625003267087884673003372881794348" end it '#block_average_hashing_time' do Bitcoin.block_average_hashing_time(436835377, 630_000_000).should == 11940563 + end + + it '#block_average_mining_time' do + Bitcoin.block_average_mining_time(0x1a022fbe, 231337, 270.0, 1.0).should == 56.50855038530773 # days end it '#blockchain_total_btc' do # 0.step(6930000, 210000){|height| # p total_btc(height-1) unless height == 0