How to check if bitcoin address is valid

0 votes

I was wondering if there exists a way to check whether a particular bitcoin address is valid or not. Any idea?

Aug 20, 2018 in Blockchain by digger
• 26,740 points
3,092 views

2 answers to this question.

0 votes

There is a validator on github to check if a bitcoin address is valid or not. (Link: https://gist.github.com/alexandrz/4491729)

require 'digest'


class BitcoinAddressValidator < ActiveModel::EachValidator

  def validate(record, field, value)

    unless valid_bitcoin_address?(value)

      record.errors[field] << "Bitcoin address is invalid"

    end

  end


  private


  B58Chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

  B58Base = B58Chars.length


  def self.valid_bitcoin_address?(address)

    (address =~ /^[a-zA-Z1-9]{33,35}$/) and version(address)

  end


  def self.version(address)

    decoded = b58_decode(address, 25)


    version = decoded[0, 1]

    checksum = decoded[-4, decoded.length]

    vh160 = decoded[0, decoded.length - 4]


    hashed = (Digest::SHA2.new << (Digest::SHA2.new << vh160).digest).digest


    hashed[0, 4] == checksum ? version[0] : nil

  end


  def self.b58_decode(value, length)

    long_value = 0

    index = 0

    result = ""


    value.reverse.each_char do |c|

      long_value += B58Chars.index(c) * (B58Base ** index)

      index += 1

    end


    while long_value >= 256 do

      div, mod = long_value.divmod 256

      result = mod.chr + result

      long_value = div

    end


    result = long_value.chr + result


    if result.length < length

      result = 0.chr * (length - result.length) + result

    end


    result

  end

end
answered Aug 20, 2018 by slayer
• 29,360 points
0 votes

For Java, have a look at the validateAddress(String) method in this class:https://github.com/jim618/multibit/blob/master/src/main/java/org/multibit/viewsystem/swing/action/Validator.java

To know how to do it in Python, Javascript or PHP, visit: https://www.edureka.co/community/15096/how-to-validate-bitcoin-address

answered Aug 20, 2018 by Omkar
• 69,230 points

Related Questions In Blockchain

0 votes
1 answer

How to detect if an ethereum address is an ERC20 token contract?

There are many possible ways to achieve ...READ MORE

answered Sep 25, 2018 in Blockchain by Christine
• 15,790 points
7,266 views
+1 vote
0 answers

How to check a Bitcoin wallet balance from first generated address (m/44'/0'/0'/0/0)

Is there a way to check a ...READ MORE

Mar 4, 2022 in Blockchain by Soham
• 9,700 points
2,076 views
+1 vote
0 answers

How to check a Bitcoin wallet balance from first generated address (m/44'/0'/0'/0/0)

Is there a way to check a ...READ MORE

Mar 9, 2022 in Blockchain by Soham
• 9,700 points
1,549 views
0 votes
1 answer

How to check Bitcoin Wallet ?

This can be done using Extended Public ...READ MORE

answered Jul 13, 2018 in Blockchain by Perry
• 17,100 points
1,235 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,222 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,789 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,312 views
0 votes
1 answer

How to Check if string is in list in javascript?

At first, make sure that the string ...READ MORE

answered Sep 19, 2018 in Blockchain by slayer
• 29,360 points
2,828 views
+3 votes
3 answers

How to validate bitcoin address?

I found these 2 scripts online: Javascript: <html> <head> <script type="text/javascript" ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,360 points
6,160 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP