coin-address-validators 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +59 -0
- data/coin-address-validators.gemspec +29 -0
- data/lib/coin-address-validators.rb +96 -0
- data/lib/coin-address-validators/version.rb +3 -0
- data/spec/lib/coin-address-validators_spec.rb +52 -0
- data/spec/spec_helper.rb +20 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 39237dc4cfffa22cbf9e2706130e34b16b685ae8
|
4
|
+
data.tar.gz: 4ba01e5c580ceda5a47e30305c0fb3efacdc3ca2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7ac81e40649316e1adec1c8e92d4ee4ce17ffcd2f334d28c9dcf5af5d9dc58e8dc0a61e1c43f4ed642875839495cb537d85b1d4404c139f66175573640b8424
|
7
|
+
data.tar.gz: 0480aa904d2e2e407fbf862bab92bdd2fe4cf2dd320e3e1972c6914eafd5b040f7965cd047cad9a17ed217b0c307353a1c3ff728105aecd60f02f56f1176e4af
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.byebug_history
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
coin-address-validators (0.0.1)
|
5
|
+
base_x
|
6
|
+
bitcoin-ruby
|
7
|
+
cashaddress
|
8
|
+
eth
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
base_x (0.8.1)
|
14
|
+
bitcoin-ruby (0.0.18)
|
15
|
+
byebug (9.0.6)
|
16
|
+
cashaddress (0.1.0)
|
17
|
+
diff-lcs (1.3)
|
18
|
+
digest-sha3 (1.1.0)
|
19
|
+
eth (0.4.8)
|
20
|
+
digest-sha3 (~> 1.1)
|
21
|
+
ffi (~> 1.0)
|
22
|
+
money-tree (~> 0.10.0)
|
23
|
+
rlp (~> 0.7.3)
|
24
|
+
scrypt (~> 3.0.5)
|
25
|
+
ffi (1.9.25)
|
26
|
+
ffi-compiler (1.0.1)
|
27
|
+
ffi (>= 1.0.0)
|
28
|
+
rake
|
29
|
+
money-tree (0.10.0)
|
30
|
+
ffi
|
31
|
+
rake (12.3.1)
|
32
|
+
rlp (0.7.3)
|
33
|
+
rspec (3.7.0)
|
34
|
+
rspec-core (~> 3.7.0)
|
35
|
+
rspec-expectations (~> 3.7.0)
|
36
|
+
rspec-mocks (~> 3.7.0)
|
37
|
+
rspec-core (3.7.1)
|
38
|
+
rspec-support (~> 3.7.0)
|
39
|
+
rspec-expectations (3.7.0)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.7.0)
|
42
|
+
rspec-mocks (3.7.0)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.7.0)
|
45
|
+
rspec-support (3.7.1)
|
46
|
+
scrypt (3.0.5)
|
47
|
+
ffi-compiler (>= 1.0, < 2.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
bundler (~> 1.7)
|
54
|
+
byebug
|
55
|
+
coin-address-validators!
|
56
|
+
rspec
|
57
|
+
|
58
|
+
BUNDLED WITH
|
59
|
+
1.16.1
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'coin-address-validators/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "coin-address-validators"
|
8
|
+
spec.version = CoinAddressValidators::VERSION
|
9
|
+
spec.authors = ["Phuong Nguyen"]
|
10
|
+
spec.email = ["phuongnd08@gmail.com"]
|
11
|
+
spec.summary = %q{Validators for btc, bch, ltc, eth, xrp addresses}
|
12
|
+
spec.description = %q{Validators for btc, bch, ltc, eth, xrp addresses}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "bitcoin-ruby"
|
22
|
+
spec.add_dependency "base_x"
|
23
|
+
spec.add_dependency "cashaddress"
|
24
|
+
spec.add_dependency "eth"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
27
|
+
spec.add_development_dependency("rspec", [">= 0"])
|
28
|
+
spec.add_development_dependency("byebug", [">= 0"])
|
29
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'bitcoin'
|
2
|
+
require 'base_x'
|
3
|
+
require 'cashaddress'
|
4
|
+
require 'eth'
|
5
|
+
|
6
|
+
class CoinAddressValidators
|
7
|
+
require 'coin-address-validators/version'
|
8
|
+
|
9
|
+
attr_reader :coin
|
10
|
+
|
11
|
+
def initialize(coin)
|
12
|
+
@coin = coin
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?(address)
|
16
|
+
inner_validator.valid?(address)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.[](coin)
|
20
|
+
@singletons ||= {}
|
21
|
+
@singletons[coin] ||= new(coin)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def inner_validator
|
27
|
+
@inner_validator ||= inner_validator_class.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def inner_validator_class
|
31
|
+
prefix = coin == "usdt" ? "btc" : coin
|
32
|
+
CoinAddressValidators.const_get("#{prefix.capitalize}Validator")
|
33
|
+
end
|
34
|
+
|
35
|
+
class BtcValidator
|
36
|
+
include Bitcoin::Util
|
37
|
+
def valid?(address)
|
38
|
+
valid_address?(address)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class LtcValidator
|
43
|
+
include Bitcoin::Util
|
44
|
+
|
45
|
+
def with_network(network)
|
46
|
+
old_network = Bitcoin.network[:project]
|
47
|
+
Bitcoin.network = network
|
48
|
+
yield
|
49
|
+
ensure
|
50
|
+
Bitcoin.network = old_network
|
51
|
+
end
|
52
|
+
|
53
|
+
def valid?(address)
|
54
|
+
with_network(:litecoin) { valid_address?(address) }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class XrpValidator
|
59
|
+
XRPBASE58 = BaseX.new("rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz")
|
60
|
+
|
61
|
+
def valid?(address)
|
62
|
+
hex = XRPBASE58.decode(address).bth
|
63
|
+
addr = hex[0...-8]
|
64
|
+
checksum = hex[-8..-1]
|
65
|
+
Digest::SHA256.hexdigest(Digest::SHA256.digest(addr.htb))[0...8] == checksum
|
66
|
+
rescue BaseX::InvalidNumeral => e
|
67
|
+
false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class BchValidator
|
72
|
+
include Bitcoin::Util
|
73
|
+
def valid?(address)
|
74
|
+
return true if valid_address?(address)
|
75
|
+
begin
|
76
|
+
legacy_address = Cashaddress.to_legacy(address)
|
77
|
+
return true
|
78
|
+
rescue Cashaddress::Error
|
79
|
+
return false if address.start_with?("bitcoincash:")
|
80
|
+
begin
|
81
|
+
legacy_address = Cashaddress.to_legacy("bitcoincash:" + address)
|
82
|
+
return true
|
83
|
+
rescue Cashaddress::Error
|
84
|
+
return false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class EthValidator
|
91
|
+
def valid?(address)
|
92
|
+
Eth::Utils.valid_address? address
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe CoinAddressValidators do
|
4
|
+
context "btc" do
|
5
|
+
it "returns correctly" do
|
6
|
+
expect(CoinAddressValidators["btc"].valid?("14YoCUBKcrK3r8Hn7VxYGQ5bbfHf8qRtch")).to be_truthy
|
7
|
+
expect(CoinAddressValidators["btc"].valid?("14YoCUBKcrK3r8Hn7VxYGQ5bbfHf8qRtcg")).to be_falsey
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "bch" do
|
12
|
+
context "cashaddr format" do
|
13
|
+
it "returns correctly" do
|
14
|
+
expect(CoinAddressValidators["btc"].valid?("14YoCUBKcrK3r8Hn7VxYGQ5bbfHf8qRtch")).to be_truthy
|
15
|
+
expect(CoinAddressValidators["btc"].valid?("14YoCUBKcrK3r8Hn7VxYGQ5bbfHf8qRtcg")).to be_falsey
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "legacy format" do
|
20
|
+
it "returns correctly" do
|
21
|
+
expect(CoinAddressValidators["bch"].valid?("bitcoincash:qp85dztm50l8x0rdkr8laartkcygjpkkxg0qqf93kf")).to be_truthy
|
22
|
+
expect(CoinAddressValidators["bch"].valid?("qp85dztm50l8x0rdkr8laartkcygjpkkxg0qqf93kf")).to be_truthy
|
23
|
+
|
24
|
+
expect(CoinAddressValidators["bch"].valid?("bitcoincash:qp85dztm50l8x0rdkr8laartkcygjpkkxg0qqf93ka")).to be_falsey
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "eth" do
|
30
|
+
it "returns correctly" do
|
31
|
+
expect(CoinAddressValidators["eth"].valid?("0xff0a8531f38e5f143a0444d066fad0a0f0833853")).to be_truthy
|
32
|
+
expect(CoinAddressValidators["eth"].valid?("0xff0a8531f38e5f143a0444d066fad0a0f08338534")).to be_falsey
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "ltc" do
|
37
|
+
it "returns correctly" do
|
38
|
+
expect(CoinAddressValidators["ltc"].valid?("LajyQBeZaBA1NkZDeY8YT5RYYVRkXMvb2T")).to be_truthy
|
39
|
+
expect(CoinAddressValidators["ltc"].valid?("M9k7JpqrWewzzQK8bPT2GFNzbijRHzDaU4")).to be_truthy
|
40
|
+
expect(CoinAddressValidators["ltc"].valid?("M9k7JpqrWewzzQK8bPT2GFNzbijRHzDaU5")).to be_falsey
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "xrp" do
|
45
|
+
it "returns correctly" do
|
46
|
+
expect(CoinAddressValidators["xrp"].valid?("r!!!")).to be_falsey
|
47
|
+
expect(CoinAddressValidators["xrp"].valid?("rQGwKPE63Mmi9QdswwxKkVUxp6joqFbjzm")).to be_falsey
|
48
|
+
expect(CoinAddressValidators["xrp"].valid?("rEuLyBCvcw4CFmzv8RepSiAoNgF8tTGJQC")).to be_truthy
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'byebug'
|
4
|
+
require 'coin-address-validators'
|
5
|
+
|
6
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
7
|
+
# in spec/support/ and its subdirectories.
|
8
|
+
Dir[File.expand_path("./spec/support/**/*.rb")].each { |f| require f }
|
9
|
+
|
10
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coin-address-validators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Phuong Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bitcoin-ruby
|
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: base_x
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: cashaddress
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: eth
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.7'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Validators for btc, bch, ltc, eth, xrp addresses
|
112
|
+
email:
|
113
|
+
- phuongnd08@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- Gemfile.lock
|
121
|
+
- coin-address-validators.gemspec
|
122
|
+
- lib/coin-address-validators.rb
|
123
|
+
- lib/coin-address-validators/version.rb
|
124
|
+
- spec/lib/coin-address-validators_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
homepage: ''
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.4.5.2
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Validators for btc, bch, ltc, eth, xrp addresses
|
150
|
+
test_files:
|
151
|
+
- spec/lib/coin-address-validators_spec.rb
|
152
|
+
- spec/spec_helper.rb
|