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
• 27,620 points
205 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,050 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
• 67,120 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 24, 2018 in Blockchain by Christine
• 15,790 points
86 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,010 points
54 views
0 votes
1 answer

How to extract h160 address of bitcoin blockchain?

You can extract the data using the ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 27,620 points
75 views
0 votes
1 answer

How to generate Bitcoin address?

Convert the hex string back to bytes ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,050 points
53 views
0 votes
1 answer

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

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,320 points
56 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
103 views
0 votes
1 answer
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,050 points
37 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,050 points
664 views