btc_address_validator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1cb2caf8a58e0813579ab784a47223982a4bb579
4
+ data.tar.gz: 836c5ab1a99a71188233fb93f6096ac4b8851e53
5
+ SHA512:
6
+ metadata.gz: 35f605941abebe1f2da8eb16d11d18d37c2c39f74ab32c023f417809e55cd864023c4165b0178a7d94d725f4f7923373f1663d87592c8d243340bfd3c72046f1
7
+ data.tar.gz: 37f9438a7d5334b0ec8abbf883ae381de20f477f08564b38ace4582b49b324f7898b6bfe9ed0e451bfc80c08a0b59afaccc36fd7e13af70de181656f49742446
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'BtcAddressValidator'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+ Bundler::GemHelper.install_tasks
19
+
20
+ require 'rake/testtask'
21
+
22
+ Rake::TestTask.new(:test) do |t|
23
+ t.libs << 'lib'
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+
30
+ task default: :test
@@ -0,0 +1,54 @@
1
+ class BtcAddressValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ if value.present? && !valid_address?(value)
5
+ record.errors[attribute] << (options[:message] || "is not a valid BTC address")
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ # check if given +address+ is valid.
12
+ # this means having a correct version byte, length and checksum.
13
+ def valid_address?(address)
14
+ # FOR BITCOINS
15
+ address_version = "00"
16
+ p2sh_version = "05"
17
+
18
+ hex = decode_base58(address) rescue nil
19
+ return false unless hex && hex.bytesize == 50
20
+ return false unless [address_version, p2sh_version].include?(hex[0...2])
21
+ base58_checksum?(address)
22
+ end
23
+
24
+ def decode_base58(base58_val)
25
+ s = base58_to_int(base58_val).to_s(16); s = (s.bytesize.odd? ? '0'+s : s)
26
+ s = '' if s == '00'
27
+ leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
28
+ s = ("00"*leading_zero_bytes) + s if leading_zero_bytes > 0
29
+ s
30
+ end
31
+
32
+ def base58_to_int(base58_val)
33
+ alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
34
+ int_val, base = 0, alpha.size
35
+ base58_val.reverse.each_char.with_index do |char,index|
36
+ raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = alpha.index(char)
37
+ int_val += char_index*(base**index)
38
+ end
39
+ int_val
40
+ end
41
+
42
+ # verify base58 checksum for given +base58+ data.
43
+ def base58_checksum?(base58)
44
+ hex = decode_base58(base58) rescue nil
45
+ return false unless hex
46
+ checksum( hex[0...42] ) == hex[-8..-1]
47
+ end
48
+
49
+ # checksum is a 4 bytes sha256-sha256 hexdigest.
50
+ def checksum(hex)
51
+ b = [hex].pack("H*") # unpack hex
52
+ Digest::SHA256.hexdigest( Digest::SHA256.digest(b) )[0...8]
53
+ end
54
+ end
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+
3
+ class Validatable
4
+ include ActiveModel::Validations
5
+ attr_accessor :btc_address
6
+ validates :btc_address, btc_address: {message: "your copy pasting skills are not so good"}
7
+ end
8
+
9
+ class BtcAddressValidatorTest < ActiveSupport::TestCase
10
+
11
+ def setup
12
+ @model = Validatable.new
13
+ end
14
+
15
+ test "without address" do
16
+ assert @model.valid?, "Should not be checking presence, only validity of address"
17
+ end
18
+
19
+ test "with valid address" do
20
+ @model.btc_address = "1C1NCedACSukWyKEjgSUYpVuiXQEtYwez4"
21
+ assert @model.valid?
22
+ end
23
+
24
+ test "with invalid address" do
25
+ @model.btc_address = "1234567890"
26
+ refute @model.valid?
27
+ end
28
+
29
+
30
+ end
31
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'active_model'
4
+
5
+ # Coloring
6
+ require "minitest/reporters"
7
+ Minitest::Reporters.use!
8
+
9
+
10
+ require 'btc_address_validator'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: btc_address_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Elfassy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-reporters
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Check if a BTC address looks valid.
56
+ email:
57
+ - michaelelfassy@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - Rakefile
64
+ - lib/btc_address_validator.rb
65
+ - test/btc_address_validator_test.rb
66
+ - test/test_helper.rb
67
+ homepage: https://github.com/elfassy/btc_address_validator
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Simple BTC address validator
91
+ test_files:
92
+ - test/btc_address_validator_test.rb
93
+ - test/test_helper.rb