Skip to content

Commit

Permalink
Add valid_pubkey?
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim-s committed Nov 11, 2014
1 parent 0e58a0b commit b65e748
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/bitcoin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def valid_address?(address)
address_checksum?(address)
end

# check if given +pubkey+ is valid.
def valid_pubkey?(pubkey)
::OpenSSL::PKey::EC::Point.from_hex(bitcoin_elliptic_curve.group, pubkey)
true
rescue OpenSSL::PKey::EC::Point::Error
false
end

# get hash160 for given +address+. returns nil if address is invalid.
def hash160_from_address(address)
return nil unless valid_address?(address)
Expand Down
8 changes: 8 additions & 0 deletions spec/bitcoin/bitcoin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@
success.should == true
end

it 'validate bitcoin public key' do
key = Bitcoin::Key.generate
Bitcoin.valid_pubkey?(key.pub_compressed).should == true
Bitcoin.valid_pubkey?(key.pub_uncompressed).should == true
Bitcoin.valid_pubkey?(key.addr).should == false
Bitcoin.valid_pubkey?(key.priv).should == false
end

it 'validate p2sh address' do
Bitcoin.network = :testnet
Bitcoin.valid_address?("2MyLngQnhzjzatKsB7XfHYoP9e2XUXSiBMM").should == true
Expand Down

0 comments on commit b65e748

Please sign in to comment.