coins_address_validator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +23 -0
- data/README.md +59 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/coins_address_validator.gemspec +27 -0
- data/lib/coins_address_validator/version.rb +3 -0
- data/lib/coins_address_validator.rb +156 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 409b4dd853df9b7869ff0ae28b68380175f01319
|
4
|
+
data.tar.gz: 604aa52440b8b4bbeccb179ddcb28532df75d446
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af6ba42856b06c5c39d3a72174946d1157ab9ac5bb0eac8bf8962d1ad2c1f36cc235e1c8ea5a2f11d8d202a50cdae60941195bf7a791db10217e9743d82a28fe
|
7
|
+
data.tar.gz: 5e31f243f491bf9bd561a988d91a4d5248293b941eea738927099639e17f931596c7dc3f58bf573905f24b7b4adba815d0328ffb19b9bfb512786c475a88c01e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2015, Hothza
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
17
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
18
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
20
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
21
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
22
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
23
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# CoinsAddressValidator
|
2
|
+
|
3
|
+
This gem allows you to check if virtual coin address is valid and retrieve information about it.
|
4
|
+
|
5
|
+
Supported coins:
|
6
|
+
- Bitcoin (BTC),
|
7
|
+
- DASH (DASH),
|
8
|
+
- Dogecoin (DOGE),
|
9
|
+
- Litecoin (LTC),
|
10
|
+
- Namecoin (NMC),
|
11
|
+
- Peercoin (PPC),
|
12
|
+
- Primecoin (XPM)
|
13
|
+
|
14
|
+
If you find this gem useful please send few coins for coffee:
|
15
|
+
|
16
|
+
- BTC: 1HRqmR2dbuHKeNWp478W77NxLzPi63QoKi
|
17
|
+
- LTC: LUzmQEYEMHxh7Q8JWh3vjW2BYCGd8VxANF
|
18
|
+
- DASH: XtPu4gA71zMdp37x3XiYdG2U25UA85Gq1w
|
19
|
+
|
20
|
+
Thank you! :)
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'coins_address_validator'
|
28
|
+
```
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install coins_address_validator
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
This gem has two public methods:
|
41
|
+
```ruby
|
42
|
+
is_address_valid?()
|
43
|
+
```
|
44
|
+
which checks if address passed as a parameter is valid BASE58 string, has correct length
|
45
|
+
and checksum from decoded address is equal to first four bytes of SHA256(SHA256(h160))
|
46
|
+
|
47
|
+
Second one:
|
48
|
+
```ruby
|
49
|
+
get_address_info()
|
50
|
+
```
|
51
|
+
returns information about network (Bitcoin, Litecoin, etc.), network symbol and type of address.
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it ( https://github.com/Hothza/coins_address_validator/fork )
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "coins_address_validator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'coins_address_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "coins_address_validator"
|
8
|
+
spec.version = CoinsAddressValidator::VERSION
|
9
|
+
spec.authors = ["Hothza"]
|
10
|
+
spec.email = ["hothza@users.noreply.github.com"]
|
11
|
+
|
12
|
+
# if spec.respond_to?(:metadata)
|
13
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
14
|
+
# end
|
15
|
+
|
16
|
+
spec.summary = %q{CoinsAddressValidator - gem which allows you to check if virtual coin address is valid and retrieve information about it.}
|
17
|
+
spec.homepage = "https://github.com/Hothza/coin_address_validator"
|
18
|
+
spec.license = "BSD"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# Copyright (c) 2015, Hothza
|
2
|
+
# All rights reserved.
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without
|
5
|
+
# modification, are permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
8
|
+
# list of conditions and the following disclaimer.
|
9
|
+
#
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer in the documentation
|
12
|
+
# and/or other materials provided with the distribution.
|
13
|
+
#
|
14
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
17
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
18
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
20
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
21
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
22
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
23
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
24
|
+
|
25
|
+
require "coins_address_validator/version"
|
26
|
+
require 'base58'
|
27
|
+
require 'digest'
|
28
|
+
|
29
|
+
module CoinsAddressValidator
|
30
|
+
class Validator
|
31
|
+
def get_address_info(address)
|
32
|
+
if !address.nil? && !address.empty?
|
33
|
+
decoded = decode(address)
|
34
|
+
if has_valid_length?(address, decoded[:version].to_i(16)) && is_checksum_valid?(decoded)
|
35
|
+
info = NETWORKS[decoded[:version].to_i(16)]
|
36
|
+
info = {} if info.nil? # Version is not supported (yet) - return empty info for such coin
|
37
|
+
|
38
|
+
return { :valid => true, :info => info }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
return { :valid => false, :info => {} }
|
42
|
+
end
|
43
|
+
|
44
|
+
def is_address_valid?(address)
|
45
|
+
if !address.nil? && !address.empty?
|
46
|
+
decoded = decode(address)
|
47
|
+
is_checksum_valid?(decoded) && has_valid_length?(address, decoded[:version].to_i(16))
|
48
|
+
else
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
NETWORKS = {
|
55
|
+
## Bitcoin
|
56
|
+
0x00 => { :name => 'Bitcoin', :network => 'mainnet', :symbol => 'BTC', :type => 'Pay-to-PubkeyHash' },
|
57
|
+
0x05 => { :name => 'Bitcoin', :network => 'mainnet', :symbol => 'BTC', :type => 'Pay-to-Script-Hash' },
|
58
|
+
0x6f => { :name => 'Bitcoin', :network => 'testnet3', :symbol => 'XTN', :type => 'Pay-to-PubkeyHash' },
|
59
|
+
0xc4 => { :name => 'Bitcoin', :network => 'testnet3', :symbol => 'XTN', :type => 'Pay-to-Script-Hash' },
|
60
|
+
|
61
|
+
## Litecoin
|
62
|
+
0x30 => { :name => 'Litecoin', :network => 'mainnet', :symbol => 'LTC', :type => 'Pay-to-PubkeyHash' },
|
63
|
+
|
64
|
+
# Litecoin Multisig -> version will be changed after hard-fork
|
65
|
+
# to some number which gives 'M' character in address (probably: 0x31 - 0x33)
|
66
|
+
|
67
|
+
# 0x05 => { :name => 'Litecoin', :network => 'mainnet', :symbol => 'LTC', :type => 'Pay-to-Script-Hash' },
|
68
|
+
|
69
|
+
# Litecoin version will be changed probably in similar way like in mainnet
|
70
|
+
|
71
|
+
# 0x6f => { :name => 'Litecoin', :network => 'testnet', :symbol => 'XLT', :type => 'Pay-to-PubkeyHash' },
|
72
|
+
# 0xc4 => { :name => 'Litecoin', :network => 'testnet', :symbol => 'XLT', :type => 'Pay-to-Script-Hash' },
|
73
|
+
|
74
|
+
## Dogecoin
|
75
|
+
0x1e => { :name => 'Dogecoin', :network => 'mainnet', :symbol => 'DOGE', :type => 'Pay-to-PubkeyHash' },
|
76
|
+
0x16 => { :name => 'Dogecoin', :network => 'mainnet', :symbol => 'DOGE', :type => 'Pay-to-Script-Hash' },
|
77
|
+
0x71 => { :name => 'Dogecoin', :network => 'testnet', :symbol => 'XDT', :type => 'Pay-to-PubkeyHash' },
|
78
|
+
#{ 0xc4 => { :name => 'Dogecoin', :network => 'testnet', :symbol => 'XDT', :type => 'Pay-to-Script-Hash' },
|
79
|
+
|
80
|
+
## DASH / DRK
|
81
|
+
0x4c => { :name => 'DASH', :network => 'mainnet', :symbol => 'DASH', :type => 'Pay-to-PubkeyHash' },
|
82
|
+
0x10 => { :name => 'DASH', :network => 'mainnet', :symbol => 'DASH', :type => 'Pay-to-Script-Hash' },
|
83
|
+
0x8b => { :name => 'DASH', :network => 'testnet', :symbol => 'tDASH', :type => 'Pay-to-PubkeyHash' },
|
84
|
+
0x13 => { :name => 'DASH', :network => 'testnet', :symbol => 'tDASH', :type => 'Pay-to-Script-Hash' },
|
85
|
+
|
86
|
+
## Feathercoin
|
87
|
+
0x0e => { :name => 'Feathercoin', :network => 'mainnet', :symbol => 'FTC', :type => 'Pay-to-PubkeyHash' },
|
88
|
+
0x60 => { :name => 'Feathercoin', :network => 'mainnet', :symbol => 'FTC', :type => 'Pay-to-Script-Hash' },
|
89
|
+
0x41 => { :name => 'Feathercoin', :network => 'testnet', :symbol => 'FTX', :type => 'Pay-to-PubkeyHash' },
|
90
|
+
#0xc4 => { :name => 'Feathercoin', :network => 'mainnet', :symbol => 'FTX', :type => 'Pay-to-Script-Hash' },
|
91
|
+
|
92
|
+
## Namecoin
|
93
|
+
0x34 => { :name => 'Namecoin', :network => 'mainnet', :symbol => 'NMC', :type => 'Pay-to-PubkeyHash' },
|
94
|
+
#0x6f => { :name => 'Namecoin', :network => 'testnet', :symbol => 'NMC', :type => 'Pay-to-PubkeyHash' },
|
95
|
+
|
96
|
+
## Peercoin
|
97
|
+
0x37 => { :name => 'Peercoin', :network => 'mainnet', :symbol => 'PPC', :type => 'Pay-to-PubkeyHash' },
|
98
|
+
0x75 => { :name => 'Peercoin', :network => 'mainnet', :symbol => 'PPC', :type => 'Pay-to-Script-Hash' },
|
99
|
+
|
100
|
+
## Primecoin
|
101
|
+
0x17 => { :name => 'Primecoin', :network => 'mainnet', :symbol => 'XPM', :type => 'Pay-to-PubkeyHash' },
|
102
|
+
0x53 => { :name => 'Primecoin', :network => 'mainnet', :symbol => 'XPM', :type => 'Pay-to-Script-Hash' },
|
103
|
+
}
|
104
|
+
BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
105
|
+
LENGTH = BASE58.length
|
106
|
+
|
107
|
+
def is_checksum_valid?(decoded)
|
108
|
+
double_sha256 = (Digest::SHA256.new << (Digest::SHA256.new << [decoded[:h160]].pack('H*')).digest)
|
109
|
+
double_sha256.hexdigest[0, 8] == decoded[:checksum]
|
110
|
+
end
|
111
|
+
|
112
|
+
def has_valid_length?(address, version)
|
113
|
+
if version == 0
|
114
|
+
return address.length < 35
|
115
|
+
elsif version == 1
|
116
|
+
return address.length == 33
|
117
|
+
elsif version == 2
|
118
|
+
return address.length == 33 || address.length == 34
|
119
|
+
elsif version > 2 && version < 144
|
120
|
+
return address.length == 34
|
121
|
+
elsif version == 144
|
122
|
+
return address.length == 34 || address.length == 35
|
123
|
+
elsif version > 144 && version < 256
|
124
|
+
return address.length == 35
|
125
|
+
else
|
126
|
+
return false
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def b58tos(string)
|
131
|
+
value = 0
|
132
|
+
prefix = 0
|
133
|
+
string.each_char do |char|
|
134
|
+
index = BASE58.index(char)
|
135
|
+
raise ArgumentError, 'String passed as a parameter is not a valid BASE58 string.' if index.nil?
|
136
|
+
value = (value * LENGTH) + index
|
137
|
+
if value == 0
|
138
|
+
prefix += 1
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
output = value.to_s(16)
|
143
|
+
|
144
|
+
# Stupid workaround for missing leading zeros
|
145
|
+
output = "0" + output if (output.length % 2) == 1
|
146
|
+
|
147
|
+
output = ("00" * prefix) + output if prefix > 0
|
148
|
+
output
|
149
|
+
end
|
150
|
+
|
151
|
+
def decode(string)
|
152
|
+
decoded = b58tos(string)
|
153
|
+
{ :version => decoded[0, 2], :h160 => decoded[0, decoded.length - 8 ], :checksum => decoded[-8, decoded.length] }
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coins_address_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hothza
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- hothza@users.noreply.github.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- coins_address_validator.gemspec
|
57
|
+
- lib/coins_address_validator.rb
|
58
|
+
- lib/coins_address_validator/version.rb
|
59
|
+
homepage: https://github.com/Hothza/coin_address_validator
|
60
|
+
licenses:
|
61
|
+
- BSD
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.4.8
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: CoinsAddressValidator - gem which allows you to check if virtual coin address
|
83
|
+
is valid and retrieve information about it.
|
84
|
+
test_files: []
|