wallet_validator 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.gitlab-ci.yml +35 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +42 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/wallet_validator/ada.rb +29 -0
- data/lib/wallet_validator/bcd.rb +5 -0
- data/lib/wallet_validator/bch.rb +57 -0
- data/lib/wallet_validator/bchsv.rb +5 -0
- data/lib/wallet_validator/bnb.rb +33 -0
- data/lib/wallet_validator/btc.rb +76 -0
- data/lib/wallet_validator/crypto/base32.rb +73 -0
- data/lib/wallet_validator/crypto/base58.rb +41 -0
- data/lib/wallet_validator/crypto/bech32.rb +40 -0
- data/lib/wallet_validator/doge.rb +22 -0
- data/lib/wallet_validator/eos.rb +15 -0
- data/lib/wallet_validator/eth.rb +54 -0
- data/lib/wallet_validator/ltc.rb +32 -0
- data/lib/wallet_validator/sda.rb +41 -0
- data/lib/wallet_validator/sol.rb +15 -0
- data/lib/wallet_validator/trx.rb +24 -0
- data/lib/wallet_validator/version.rb +3 -0
- data/lib/wallet_validator/xem.rb +19 -0
- data/lib/wallet_validator/xrp.rb +41 -0
- data/lib/wallet_validator.rb +35 -0
- data/wallet_validator.gemspec +29 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 625e53bd99b16075557b5128435fece20ee05dfa3ae80285a9094032b1ae48a7
|
4
|
+
data.tar.gz: c32ff56118342fe5a5f2a1bb14d4125ffde8ece700100435d7e215565491e51d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 01e6c190d7f29b886bf4bd1f6eafdf7467bd511a5d7fd0a85ad95066dd447a3cf856e20ae899b0c9d1d9ab7e2f477bca1616767a1b03e90b4564a72e0278ed23
|
7
|
+
data.tar.gz: cac58b015e6d63da27ba8e22a5979f60691b58a8d476918304809b25e5fd424775800166f07606a1a355259bb3b256440d5e1c3c54dfb47fd3dfd085faef7368
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
stages:
|
2
|
+
- test
|
3
|
+
|
4
|
+
variables:
|
5
|
+
CI_PRO_REGISTRY: 988486663374.dkr.ecr.ap-northeast-1.amazonaws.com
|
6
|
+
|
7
|
+
.base:
|
8
|
+
tags:
|
9
|
+
- "bitopro"
|
10
|
+
image: $CI_PRO_REGISTRY/base_ruby
|
11
|
+
cache:
|
12
|
+
key: 2021_gems_and_packages
|
13
|
+
paths:
|
14
|
+
- apt-cache
|
15
|
+
- vendor/ruby
|
16
|
+
- node_modules/
|
17
|
+
- .yarn
|
18
|
+
policy: pull
|
19
|
+
before_script:
|
20
|
+
- gem install bundler --no-document
|
21
|
+
- bundle install --jobs $(nproc) "${FLAGS[@]}" --path=vendor
|
22
|
+
|
23
|
+
rspec:
|
24
|
+
extends: .base
|
25
|
+
stage: test
|
26
|
+
cache:
|
27
|
+
policy: pull-push
|
28
|
+
artifacts:
|
29
|
+
name: coverage_report
|
30
|
+
expire_in: 1 week
|
31
|
+
when: always
|
32
|
+
paths:
|
33
|
+
- coverage/
|
34
|
+
script:
|
35
|
+
- bundle exec rspec
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
wallet_validator (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.3)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
digest-sha3 (1.1.0)
|
12
|
+
method_source (1.0.0)
|
13
|
+
pry (0.14.1)
|
14
|
+
coderay (~> 1.1)
|
15
|
+
method_source (~> 1.0)
|
16
|
+
rake (12.3.3)
|
17
|
+
rspec (3.10.0)
|
18
|
+
rspec-core (~> 3.10.0)
|
19
|
+
rspec-expectations (~> 3.10.0)
|
20
|
+
rspec-mocks (~> 3.10.0)
|
21
|
+
rspec-core (3.10.1)
|
22
|
+
rspec-support (~> 3.10.0)
|
23
|
+
rspec-expectations (3.10.1)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.10.0)
|
26
|
+
rspec-mocks (3.10.2)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.10.0)
|
29
|
+
rspec-support (3.10.2)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
digest-sha3
|
36
|
+
pry
|
37
|
+
rake (~> 12.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
wallet_validator!
|
40
|
+
|
41
|
+
BUNDLED WITH
|
42
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Nic
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# WalletValidator
|
2
|
+
|
3
|
+
錢包地址檢查器,包含了 mainnet / testnet
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "wallet_validator", git: "https://gitlab.gitlab.cc/rubygems/wallet_validator"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install wallet_validator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
第三個參數為 `is_testnet`,預設為 false,也就是預設為主鏈錢包格式檢查
|
24
|
+
|
25
|
+
mainnet
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
WalletValidator.valid?("BTC", "12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y")
|
29
|
+
```
|
30
|
+
|
31
|
+
如果需要檢查 testnet 請使用
|
32
|
+
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
WalletValidator.valid?("BTC", "2N3oefVeg6stiTb5Kh3ozCSkaqmx91FDbsm", true)
|
36
|
+
```
|
37
|
+
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.gitlab.cc/rubygems/wallet_validator.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wallet_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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Ada < Btc
|
4
|
+
CURRENCY_ATTR = {
|
5
|
+
"main" => {
|
6
|
+
"bech32" => {
|
7
|
+
"hrp" => "addr",
|
8
|
+
"separator" => "1"
|
9
|
+
},
|
10
|
+
},
|
11
|
+
"testnet" => {
|
12
|
+
"bech32" => {
|
13
|
+
"hrp" => "addr_test", # hrp
|
14
|
+
"separator" => "1", # separator
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
def valid?
|
20
|
+
Crypto::Bech32.valid?(address, attribute["bech32"]["hrp"], attribute["bech32"]["separator"], attribute["bech32"]["length_check"])
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def attribute
|
26
|
+
is_testnet && CURRENCY_ATTR["testnet"] ? CURRENCY_ATTR["testnet"] : CURRENCY_ATTR["main"]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module WalletValidator
|
4
|
+
class Bch
|
5
|
+
attr_reader :address, :is_testnet
|
6
|
+
|
7
|
+
def initialize(address, is_testnet)
|
8
|
+
@address = address
|
9
|
+
@is_testnet = is_testnet
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
match = address.match(/^(.+):(.+)$/)
|
14
|
+
|
15
|
+
return false unless match[1] == is_testnet ? "bchtest" : "bitcoincash"
|
16
|
+
|
17
|
+
prefix = is_testnet ? [2, 3, 8, 20, 5, 19, 20, 0] : [2, 9, 20, 3, 15, 9, 14, 3, 1, 19, 8, 0]
|
18
|
+
raw_payload = match[2].split("").map { |m| Crypto::Bech32::CHARSET.index(m) }
|
19
|
+
return false if bch_polymod(prefix + raw_payload) != 0 # raise "Address checksum invalid: #{cashaddress}"
|
20
|
+
|
21
|
+
payload = bch_convert_bits(raw_payload[0..-9], 5, 8, false)
|
22
|
+
return false if payload.empty? # raise "Converted payload was empty"
|
23
|
+
|
24
|
+
return true
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def bch_polymod(vector)
|
30
|
+
accum = 1
|
31
|
+
vector.each do |byte|
|
32
|
+
pivot = accum >> 35
|
33
|
+
accum = ((accum & 0x07ffffffff) << 5) ^ byte
|
34
|
+
[
|
35
|
+
[0x01, 0x98f2bc8e61],
|
36
|
+
[0x02, 0x79b76d99e2],
|
37
|
+
[0x04, 0xf33e5fb3c4],
|
38
|
+
[0x08, 0xae2eabe2a8],
|
39
|
+
[0x10, 0x1e4f43e470]
|
40
|
+
].each do |bit, mask|
|
41
|
+
accum ^= mask if (pivot & bit) != 0
|
42
|
+
end
|
43
|
+
end
|
44
|
+
accum ^ 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def bch_convert_bits(data, from, to, pad)
|
48
|
+
converted = data
|
49
|
+
.map { |i| format("%0*b", from, i) } # Get chunks <from> bits long
|
50
|
+
.join("") # In a long row of zeros and ones.
|
51
|
+
.chars.each_slice(to) # Then in bits that are <
|
52
|
+
.map { |bits| bits.join("").ljust(to, "0").to_i(2) }
|
53
|
+
converted = converted[0..-2] if converted.last.zero? && !pad
|
54
|
+
converted
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Bnb < Btc
|
4
|
+
attr_reader :address, :is_testnet
|
5
|
+
|
6
|
+
CURRENCY_ATTR = {
|
7
|
+
"main" => {
|
8
|
+
"bech32" => {
|
9
|
+
"hrp" => "bnb", # hrp
|
10
|
+
"separator" => "1", # separator
|
11
|
+
"length_check" => [38] # length check
|
12
|
+
},
|
13
|
+
},
|
14
|
+
"testnet" => {
|
15
|
+
"bech32" => {
|
16
|
+
"hrp" => "tbnb", # hrp
|
17
|
+
"separator" => "1", # separator
|
18
|
+
"length_check" => [38] # length check
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}.freeze
|
22
|
+
|
23
|
+
def valid?
|
24
|
+
Crypto::Bech32.valid?(address, attribute["bech32"]["hrp"], attribute["bech32"]["separator"], attribute["bech32"]["length_check"])
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def attribute
|
30
|
+
is_testnet && CURRENCY_ATTR["testnet"] ? CURRENCY_ATTR["testnet"] : CURRENCY_ATTR["main"]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "digest"
|
4
|
+
|
5
|
+
module WalletValidator
|
6
|
+
class Btc
|
7
|
+
ALPHA = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".freeze
|
8
|
+
|
9
|
+
CURRENCY_ATTR = {
|
10
|
+
"main" => {
|
11
|
+
"filter" => "13",
|
12
|
+
"addrv" => %w[00 05],
|
13
|
+
"bech32" => {
|
14
|
+
"hrp" => "bc",
|
15
|
+
"separator" => "1",
|
16
|
+
},
|
17
|
+
},
|
18
|
+
"testnet" => {
|
19
|
+
"filter" => "2mn", # 2Mzje457jS8ejm5B2vHemUBB6zBCn2M98MG , mhWkz6E1k6BNXc7Z5vaYWXhHjN3Tdg9Uk2 , tb1qx0zejt77yudv2rhgdcm228xal5hfwduahfweft
|
20
|
+
"addrv" => %w[c4 6f],
|
21
|
+
"bech32" => {
|
22
|
+
"hrp" => "tb",
|
23
|
+
"separator" => "1"
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}.freeze
|
27
|
+
|
28
|
+
attr_reader :address, :is_testnet
|
29
|
+
|
30
|
+
def initialize(address, is_testnet)
|
31
|
+
@address = address
|
32
|
+
@is_testnet = is_testnet
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid?
|
36
|
+
filter_def = attribute["bech32"]
|
37
|
+
|
38
|
+
if filter_def
|
39
|
+
# false時繼續往下
|
40
|
+
return true if Crypto::Bech32.valid?(address, filter_def["hrp"], filter_def["separator"], filter_def["length_check"])
|
41
|
+
end
|
42
|
+
|
43
|
+
filter = attribute["filter"]
|
44
|
+
addrv = attribute["addrv"]
|
45
|
+
return false if !address.match?(/^[#{filter}][#{ALPHA}]{25,34}$/)
|
46
|
+
|
47
|
+
# 此處不同於Base58 gem,必須獨立使用
|
48
|
+
int_val = 0
|
49
|
+
address.to_s.split(//).reverse.each_with_index do |char, index|
|
50
|
+
int_val += ALPHA.index(char) * (58**index)
|
51
|
+
end
|
52
|
+
|
53
|
+
hex = int_val.to_s(16)
|
54
|
+
hex = "0#{hex}" if hex.length.odd? # is_odd
|
55
|
+
|
56
|
+
if match = address.match(/^([1]+)/)
|
57
|
+
hex = ("00" * match[1].length) + hex
|
58
|
+
end
|
59
|
+
|
60
|
+
return false unless hex.length == 50
|
61
|
+
return false unless addrv.include?(hex[0...2])
|
62
|
+
|
63
|
+
Digest::SHA256.hexdigest(
|
64
|
+
Digest::SHA256.digest(
|
65
|
+
[hex[0...42]].pack("H*")
|
66
|
+
)
|
67
|
+
)[0...8] == hex[-8..-1]
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def attribute
|
73
|
+
is_testnet && CURRENCY_ATTR["testnet"] ? CURRENCY_ATTR["testnet"] : CURRENCY_ATTR["main"]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Crypto
|
2
|
+
module Base32
|
3
|
+
# code from https://github.com/stesla/base32
|
4
|
+
TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".freeze
|
5
|
+
HEX_ENCODE_ARRAY = %w[0 1 2 3 4 5 6 7 8 9 a b c d e f].freeze
|
6
|
+
|
7
|
+
def self.chunk_decode(ori_bytes)
|
8
|
+
bytes = []
|
9
|
+
ori_bytes.each do |c| # strip padding
|
10
|
+
break if c == 61
|
11
|
+
|
12
|
+
bytes << c
|
13
|
+
end
|
14
|
+
|
15
|
+
n = (bytes.length * 5.0 / 8.0).floor
|
16
|
+
p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
|
17
|
+
t = 0
|
18
|
+
|
19
|
+
bytes.each do |o|
|
20
|
+
i = TABLE.index(o.chr)
|
21
|
+
raise ArgumentError, "invalid character '#{o.chr}'" if i.nil?
|
22
|
+
|
23
|
+
t = (t << 5) + i
|
24
|
+
end
|
25
|
+
c = t >> p
|
26
|
+
ans = []
|
27
|
+
(0..n - 1).each do |i|
|
28
|
+
ans << i
|
29
|
+
end
|
30
|
+
ans = ans.reverse
|
31
|
+
ans.each_with_index do |i, index|
|
32
|
+
ans[index] = ((c >> i * 8) & 0xff).chr
|
33
|
+
end
|
34
|
+
ans
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.chunks(str, size)
|
38
|
+
result = []
|
39
|
+
bytes = str.bytes
|
40
|
+
result << bytes.shift(size) while bytes.length > 0
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.decode(str)
|
45
|
+
ans = []
|
46
|
+
chunked = chunks(str, 8)
|
47
|
+
chunked.each do |ori_bytes|
|
48
|
+
temp = chunk_decode(ori_bytes)
|
49
|
+
ans << temp
|
50
|
+
end
|
51
|
+
ans.flatten.join
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.ua2hex(ua)
|
55
|
+
memo = ""
|
56
|
+
ua.each do |el|
|
57
|
+
memo += "#{HEX_ENCODE_ARRAY[el >> 4]}#{HEX_ENCODE_ARRAY[el & 0x0f]}"
|
58
|
+
end
|
59
|
+
memo
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.valid?(address, _is_testnet)
|
63
|
+
address = address.upcase.delete("-")
|
64
|
+
addressHex = ua2hex(Base32.decode(address).bytes)
|
65
|
+
|
66
|
+
version_prefixed_ripemd160_hash = addressHex[0..41]
|
67
|
+
|
68
|
+
computed = Digest::SHA3.hexdigest([version_prefixed_ripemd160_hash].pack("H*"), 256)[0..7]
|
69
|
+
checksum = addressHex[42..-1]
|
70
|
+
computed == checksum
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Crypto
|
2
|
+
module Base58
|
3
|
+
ALPHABET = {
|
4
|
+
:flickr => "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", # This is the default
|
5
|
+
:ripple => "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
|
6
|
+
}.freeze
|
7
|
+
|
8
|
+
def self.base58_to_binary(base58_val, currency)
|
9
|
+
alphabet = currency.downcase == "xrp" ? ALPHABET[:ripple] : ALPHABET[:flickr]
|
10
|
+
|
11
|
+
nzeroes = nil
|
12
|
+
base58_val.chars.each_with_index do |c, index|
|
13
|
+
if c != alphabet[0]
|
14
|
+
nzeroes = index
|
15
|
+
break
|
16
|
+
end
|
17
|
+
end
|
18
|
+
nzeros = base58_val.length - 1 unless nzeroes
|
19
|
+
|
20
|
+
prefix = nzeroes < 0 ? "" : "00" * nzeroes
|
21
|
+
|
22
|
+
hex = base58_to_int(base58_val, currency).to_s(16)
|
23
|
+
hex = hex.length.even? ? hex : ("0" + hex)
|
24
|
+
|
25
|
+
[prefix + hex].pack("H*")
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.base58_to_int(base58_val, currency)
|
29
|
+
alphabet = currency.downcase == "xrp" ? ALPHABET[:ripple] : ALPHABET[:flickr]
|
30
|
+
|
31
|
+
int_val = 0
|
32
|
+
base58_val.reverse.split(//).each_with_index do |char, index|
|
33
|
+
char_index = alphabet.index(char)
|
34
|
+
raise ArgumentError, "Value passed not a valid Base58 String." if char_index.nil?
|
35
|
+
|
36
|
+
int_val += char_index * (alphabet.length**index)
|
37
|
+
end
|
38
|
+
int_val
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Crypto
|
2
|
+
module Bech32
|
3
|
+
LENGTH_CHECK_SET = [39, 59, 98].freeze
|
4
|
+
CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l".split(//)
|
5
|
+
GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3].freeze
|
6
|
+
|
7
|
+
def self.valid?(address, hrp, separator, length_set = LENGTH_CHECK_SET)
|
8
|
+
length_set ||= LENGTH_CHECK_SET # 歸正
|
9
|
+
address = address.downcase.strip
|
10
|
+
|
11
|
+
match = address.match(/^#{hrp}#{separator}([\da-z]+)$/)
|
12
|
+
return false if !match
|
13
|
+
return false if !length_set.include?(match[1].length)
|
14
|
+
|
15
|
+
pos = address.rindex(separator)
|
16
|
+
return false if pos.nil? || pos < 1 || pos + 7 > address.length || address.length > 108
|
17
|
+
|
18
|
+
address[pos + 1..-1].each_char do |c|
|
19
|
+
return false unless CHARSET.include?(c)
|
20
|
+
end
|
21
|
+
|
22
|
+
hrp = address[0..pos - 1]
|
23
|
+
data = address[pos + 1..-1].each_char.map { |c| CHARSET.index(c) }
|
24
|
+
chk = 1
|
25
|
+
|
26
|
+
(hrp.each_char.map { |c| c.ord >> 5 } + [0] + hrp.each_char.map { |c| c.ord & 31 } + data).each do |v|
|
27
|
+
top = chk >> 25
|
28
|
+
chk = (chk & 0x1ffffff) << 5 ^ v
|
29
|
+
(0..4).each do |i|
|
30
|
+
chk ^= if ((top >> i) & 1).zero?
|
31
|
+
0
|
32
|
+
else
|
33
|
+
GENERATOR[i]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
chk == 1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Doge < Btc
|
4
|
+
CURRENCY_ATTR = {
|
5
|
+
"main" => {
|
6
|
+
"filter" => "AD",
|
7
|
+
"addrv" => %w[1e 16],
|
8
|
+
},
|
9
|
+
"testnet" => {
|
10
|
+
"filter" => "np",
|
11
|
+
"addrv" => %w[71 c4]
|
12
|
+
}
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def attribute
|
19
|
+
is_testnet && CURRENCY_ATTR["testnet"] ? CURRENCY_ATTR["testnet"] : CURRENCY_ATTR["main"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Eos
|
4
|
+
attr_reader :address, :is_testnet
|
5
|
+
|
6
|
+
def initialize(address, is_testnet)
|
7
|
+
@address = address
|
8
|
+
@is_testnet = is_testnet
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
return !!address.match(/(^[a-z1-5.]{0,11}[a-z1-5]$)|(^[a-z1-5.]{12}[a-j1-5]$)/)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "digest"
|
2
|
+
|
3
|
+
module WalletValidator
|
4
|
+
class Eth
|
5
|
+
attr_reader :address
|
6
|
+
|
7
|
+
def initialize(address, is_testnet)
|
8
|
+
@address = address
|
9
|
+
@is_testnet = is_testnet
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
# 不 match/無法 vaild 但成功/可 vaild 且成功/可 vaild 但失敗
|
14
|
+
prefix_address = prefix_hex(address)
|
15
|
+
|
16
|
+
return false if !prefix_address.match?(/\A(?:0[xX])[a-fA-F0-9]{40}\z/)
|
17
|
+
|
18
|
+
checksumed_address = checksummed(prefix_address)
|
19
|
+
return true if prefix_address.match(/\A(?:0[xX])[A-F0-9]{40}\z/) || address.match(/\A(?:0[xX])[a-f0-9]{40}\z/)
|
20
|
+
|
21
|
+
prefix_address == checksumed_address
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def prefix_hex(str)
|
27
|
+
"0x#{str.downcase[0..1] == '0x' ? str[2..-1] : str}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def checksummed(wallet)
|
31
|
+
char_set = unprefixed(wallet).chars
|
32
|
+
check_set = Digest::SHA3.new(256).hexdigest(unprefixed(wallet).downcase).chars
|
33
|
+
cased = []
|
34
|
+
char_set.each_index do |index|
|
35
|
+
char = char_set[index]
|
36
|
+
check = check_set[index]
|
37
|
+
cased << if check.match?(/[0-7]/)
|
38
|
+
char.downcase
|
39
|
+
else
|
40
|
+
char.upcase
|
41
|
+
end
|
42
|
+
end
|
43
|
+
prefix_hex(cased.join)
|
44
|
+
end
|
45
|
+
|
46
|
+
def unprefixed(str)
|
47
|
+
if str[0, 2] == "0x"
|
48
|
+
str[2..-1]
|
49
|
+
else
|
50
|
+
str
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Ltc < Btc
|
4
|
+
attr_reader :address
|
5
|
+
|
6
|
+
CURRENCY_ATTR = {
|
7
|
+
"main" => {
|
8
|
+
"filter" => "LM3",
|
9
|
+
"addrv" => %w[30 32 05],
|
10
|
+
"bech32" => {
|
11
|
+
"hrp" => "ltc",
|
12
|
+
"separator" => "1"
|
13
|
+
},
|
14
|
+
},
|
15
|
+
"testnet" => {
|
16
|
+
"filter" => "Qmn", # mtmyVwSgiWByBaVYigmHyWFBugvsHT2ccM , n2dqkKbeVfY15GDM84Kb19KUSmNScSyKd3 , tltc1q7aqycqhe5vcsuau5u4w79zz79eladj893dlstn , QdCdgLrtMB5ssEt17xg5AfdCffpimhFPoN
|
17
|
+
"addrv" => %w[6f 3a],
|
18
|
+
"bech32" => {
|
19
|
+
"hrp" => "tltc",
|
20
|
+
"separator" => "1"
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def attribute
|
29
|
+
is_testnet && CURRENCY_ATTR["testnet"] ? CURRENCY_ATTR["testnet"] : CURRENCY_ATTR["main"]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Sda
|
4
|
+
EXPECTED_LENGTH = 50
|
5
|
+
NET_PREFIX = "00".freeze
|
6
|
+
|
7
|
+
attr_reader :address
|
8
|
+
|
9
|
+
def initialize(address, is_testnet)
|
10
|
+
@address = address
|
11
|
+
@is_testnet = is_testnet
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?
|
15
|
+
temp2 = []
|
16
|
+
temp = Crypto::Base58.base58_to_binary(address, "SDA")
|
17
|
+
temp.each_byte do |b|
|
18
|
+
temp2 << b.to_s(16).rjust(2, "0")
|
19
|
+
end
|
20
|
+
decoded = temp2.join
|
21
|
+
|
22
|
+
if decoded && decoded.bytesize == EXPECTED_LENGTH && valid_address_checksum?(decoded)
|
23
|
+
# 可能為 testnet 則 PREFIX 不一樣
|
24
|
+
return true if decoded.start_with?(NET_PREFIX)
|
25
|
+
end
|
26
|
+
false
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def valid_address_checksum?(decoded)
|
32
|
+
return false unless decoded
|
33
|
+
|
34
|
+
Digest::SHA256.hexdigest(
|
35
|
+
Digest::SHA256.digest(
|
36
|
+
[decoded[0...-8]].pack("H*")
|
37
|
+
)
|
38
|
+
)[0...8] == decoded[-8..-1]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Sol
|
4
|
+
attr_reader :address, :is_testnet
|
5
|
+
|
6
|
+
def initialize(address, is_testnet)
|
7
|
+
@address = address
|
8
|
+
@is_testnet = is_testnet
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
Crypto::Base58.base58_to_int(address, "SOL").size <= 33
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Trx < Btc
|
4
|
+
attr_reader :address
|
5
|
+
|
6
|
+
CURRENCY_ATTR = {
|
7
|
+
"main" => {
|
8
|
+
"filter" => "T",
|
9
|
+
"addrv" => ["41"],
|
10
|
+
},
|
11
|
+
"testnet" => {
|
12
|
+
"filter" => "T",
|
13
|
+
"addrv" => ["41"]
|
14
|
+
}
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def attribute
|
21
|
+
is_testnet && CURRENCY_ATTR["testnet"] ? CURRENCY_ATTR["testnet"] : CURRENCY_ATTR["main"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Xem
|
4
|
+
attr_reader :address, :is_testnet
|
5
|
+
|
6
|
+
def initialize(address, is_testnet)
|
7
|
+
@address = address
|
8
|
+
@is_testnet = is_testnet
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
Crypto::Base32.valid?(address, is_testnet)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.validate_sign_msg(msg)
|
16
|
+
!!msg.match(/^[\da-zA-Z]{,32}$/)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
module WalletValidator
|
3
|
+
class Xrp
|
4
|
+
EXPECTED_LENGTH = 50
|
5
|
+
NET_PREFIX = "00".freeze
|
6
|
+
|
7
|
+
attr_reader :address
|
8
|
+
|
9
|
+
def initialize(address, is_testnet)
|
10
|
+
@address = address
|
11
|
+
@is_testnet = is_testnet
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?
|
15
|
+
temp2 = []
|
16
|
+
temp = Crypto::Base58.base58_to_binary(address, "XRP")
|
17
|
+
temp.each_byte do |b|
|
18
|
+
temp2 << b.to_s(16).rjust(2, "0")
|
19
|
+
end
|
20
|
+
decoded = temp2.join
|
21
|
+
|
22
|
+
if decoded && decoded.bytesize == EXPECTED_LENGTH && valid_address_checksum?(decoded)
|
23
|
+
# 可能為 testnet 則 PREFIX 不一樣
|
24
|
+
return true if decoded.start_with?(NET_PREFIX)
|
25
|
+
end
|
26
|
+
false
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def valid_address_checksum?(decoded)
|
32
|
+
return false unless decoded
|
33
|
+
|
34
|
+
Digest::SHA256.hexdigest(
|
35
|
+
Digest::SHA256.digest(
|
36
|
+
[decoded[0...-8]].pack("H*")
|
37
|
+
)
|
38
|
+
)[0...8] == decoded[-8..-1]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "pry"
|
2
|
+
require "wallet_validator/crypto/bech32"
|
3
|
+
require "wallet_validator/crypto/base32"
|
4
|
+
require "wallet_validator/crypto/base58"
|
5
|
+
|
6
|
+
require "wallet_validator/version"
|
7
|
+
require "wallet_validator/btc"
|
8
|
+
require "wallet_validator/eos"
|
9
|
+
require "wallet_validator/eth"
|
10
|
+
require "wallet_validator/xrp"
|
11
|
+
require "wallet_validator/doge"
|
12
|
+
require "wallet_validator/ltc"
|
13
|
+
require "wallet_validator/trx"
|
14
|
+
require "wallet_validator/bch"
|
15
|
+
require "wallet_validator/bcd"
|
16
|
+
require "wallet_validator/bnb"
|
17
|
+
require "wallet_validator/ada"
|
18
|
+
require "wallet_validator/sol"
|
19
|
+
require "wallet_validator/xem"
|
20
|
+
require "wallet_validator/sda"
|
21
|
+
|
22
|
+
module WalletValidator
|
23
|
+
class UnknownCurrency < StandardError; end
|
24
|
+
module_function
|
25
|
+
|
26
|
+
def valid?(currency, address, is_testnet = false)
|
27
|
+
address(currency, address, is_testnet).valid?
|
28
|
+
end
|
29
|
+
|
30
|
+
def address(currency, address, is_testnet)
|
31
|
+
WalletValidator.const_get(currency.capitalize).new(address, is_testnet)
|
32
|
+
rescue NameError
|
33
|
+
raise UnknownCurrency, "Wrong currency #{currency}"
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/wallet_validator/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "wallet_validator"
|
5
|
+
spec.version = WalletValidator::VERSION
|
6
|
+
spec.authors = ["Nic"]
|
7
|
+
spec.email = ["niclin0226@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
10
|
+
spec.description = %q{Write a longer description or delete this line.}
|
11
|
+
spec.homepage = "https://github.com"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = "https://github.com"
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wallet_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nic
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Write a longer description or delete this line.
|
14
|
+
email:
|
15
|
+
- niclin0226@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".gitlab-ci.yml"
|
22
|
+
- ".rspec"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- bin/console
|
30
|
+
- bin/setup
|
31
|
+
- lib/wallet_validator.rb
|
32
|
+
- lib/wallet_validator/ada.rb
|
33
|
+
- lib/wallet_validator/bcd.rb
|
34
|
+
- lib/wallet_validator/bch.rb
|
35
|
+
- lib/wallet_validator/bchsv.rb
|
36
|
+
- lib/wallet_validator/bnb.rb
|
37
|
+
- lib/wallet_validator/btc.rb
|
38
|
+
- lib/wallet_validator/crypto/base32.rb
|
39
|
+
- lib/wallet_validator/crypto/base58.rb
|
40
|
+
- lib/wallet_validator/crypto/bech32.rb
|
41
|
+
- lib/wallet_validator/doge.rb
|
42
|
+
- lib/wallet_validator/eos.rb
|
43
|
+
- lib/wallet_validator/eth.rb
|
44
|
+
- lib/wallet_validator/ltc.rb
|
45
|
+
- lib/wallet_validator/sda.rb
|
46
|
+
- lib/wallet_validator/sol.rb
|
47
|
+
- lib/wallet_validator/trx.rb
|
48
|
+
- lib/wallet_validator/version.rb
|
49
|
+
- lib/wallet_validator/xem.rb
|
50
|
+
- lib/wallet_validator/xrp.rb
|
51
|
+
- wallet_validator.gemspec
|
52
|
+
homepage: https://github.com
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
56
|
+
allowed_push_host: https://rubygems.org/
|
57
|
+
homepage_uri: https://github.com
|
58
|
+
source_code_uri: https://github.com
|
59
|
+
changelog_uri: https://github.com
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.3.0
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.1.2
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Write a short summary, because RubyGems requires one.
|
79
|
+
test_files: []
|