spec/bitcoin/bitcoin_spec.rb in bitcoin-ruby-0.0.14 vs spec/bitcoin/bitcoin_spec.rb in bitcoin-ruby-0.0.15

- old
+ new

@@ -88,10 +88,12 @@ .should == "0000daec8d6f05e949710f202c4f73258aa7791e" Bitcoin.hash160_from_address("11119uLoMQCBHmKevdsFKHMaUoyrwLa9Y") .should == "00000090c66372823859c935149e2e32d276a1e6" Bitcoin.hash160_from_address("1111136sgL8UNSTVL9ize2uGFPxFDGwFp") .should == "0000000096d3ad65d030a36e2c23f7fdd5dfcadb" + Bitcoin.hash160_from_address("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4") + .should == "751e76e8199196d454941c45d1b3a323f1433bd6" end it 'should survive rounds of hash160 <-> address' do hex = "62e907b15cbf27d5425399ebf6f0fb50ebb88f18" @@ -185,10 +187,13 @@ Bitcoin.address_type("1D3KpY5kXnYhTbdCbZ9kXb2ZY7ZapD85cW").should == nil Bitcoin.network = :bitcoin Bitcoin.address_type("3CkxTG25waxsmd13FFgRChPuGYba3ar36B").should == :p2sh Bitcoin.address_type("1D3KpY5kXnYhTbdCbZ9kXb2ZY7ZapD85cW").should == :hash160 Bitcoin.address_type("mpXwg4jMtRhuSpVq4xS3HFHmCmWp9NyGKt").should == nil + Bitcoin.address_type("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4").should == :witness_v0_keyhash + Bitcoin.address_type("bc1qw508d6qejxtdg4y5r3zarvayr0c5xw7kv8f3t4").should == nil + Bitcoin.address_type("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3").should == :witness_v0_scripthash end it 'Bitcoin#checksum' do Bitcoin.checksum("0062e907b15cbf27d5425399ebf6f0fb50ebb88f18").should == "c29b7d93" end @@ -559,9 +564,64 @@ [0, 209999, 210000, 419999, 420000, 1680000].map{|height| Bitcoin.block_creation_reward(height) }.should == [ 5000000000, 5000000000, 2500000000, 2500000000, 1250000000, 19531250 ] end + # Port of Bitcoin Core test vectors. + # https://github.com/bitcoin/bitcoin/blob/595a7bab23bc21049526229054ea1fff1a29c0bf/src/test/base58_tests.cpp#L139 + it "valid address JSON tests" do + restore_network = Bitcoin.network_name + + test_cases = JSON.parse(fixtures_file('base58_keys_valid.json')) + test_cases.each do |test_case| + # Single element arrays in tests are comments. + next if test_case.length == 1 + + address = test_case[0] + script = test_case[1].htb + is_privkey = test_case[2].fetch('isPrivkey') + is_swapcase_valid = test_case[2].fetch('tryCaseFlip', false) + + Bitcoin.network = + case test_case[2].fetch('chain').to_sym + when :main then :bitcoin + when :test then :testnet3 + when :regtest then :regtest + end + + # This spec only tests address generation, not base58 private key encoding + next if is_privkey + + computed_script = Bitcoin::Script.to_address_script(address) + computed_script.should == script + + Bitcoin.valid_address?(address.swapcase).should == is_swapcase_valid + + computed_address = Bitcoin::Script.new(script).get_address + computed_address.should == address + end + + Bitcoin.network = restore_network + end + + # Port of Bitcoin Core test vectors. + # https://github.com/bitcoin/bitcoin/blob/595a7bab23bc21049526229054ea1fff1a29c0bf/src/test/base58_tests.cpp#L179 + it "invalid address JSON tests" do + restore_network = Bitcoin.network_name + + test_cases = JSON.parse(fixtures_file('base58_keys_invalid.json')) + test_cases.each do |test_case| + address = test_case[0] + + %i(bitcoin testnet3 regtest).each do |network_name| + Bitcoin.network = network_name + + Bitcoin.valid_address?(address).should == false + end + end + + Bitcoin.network = restore_network + end end __END__ describe 'Bitcoin-Wiki - Common Standards - Hashes' do