Created
November 8, 2017 08:24
-
-
Save savasadar/efd9e2a6a6540dd2b33b2a24a7996c8e to your computer and use it in GitHub Desktop.
Ruby code for validating bitcoin address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment