mirror of
https://github.com/neocities/neocities.git
synced 2025-04-25 01:32:36 +02:00
validate all types of btc addresses with tipper
This commit is contained in:
parent
7b7b3c05e1
commit
f78775fb3f
4 changed files with 8 additions and 50 deletions
1
Gemfile
1
Gemfile
|
@ -58,6 +58,7 @@ gem 'rss'
|
|||
gem 'webp-ffi'
|
||||
gem 'rszr'
|
||||
gem 'zip_tricks'
|
||||
gem 'adequate_crypto_address'
|
||||
|
||||
group :development, :test do
|
||||
gem 'pry'
|
||||
|
|
|
@ -32,8 +32,12 @@ GEM
|
|||
tzinfo (~> 2.0)
|
||||
addressable (2.8.1)
|
||||
public_suffix (>= 2.0.2, < 6.0)
|
||||
adequate_crypto_address (0.1.9)
|
||||
base58 (~> 0.2)
|
||||
keccak (~> 1.3)
|
||||
ansi (1.5.0)
|
||||
base32 (0.3.4)
|
||||
base58 (0.2.3)
|
||||
bcrypt (3.1.18)
|
||||
builder (3.2.4)
|
||||
capybara (3.38.0)
|
||||
|
@ -142,6 +146,7 @@ GEM
|
|||
io-extra (1.4.0)
|
||||
ipaddress (0.8.3)
|
||||
json (2.6.2)
|
||||
keccak (1.3.1)
|
||||
llhttp-ffi (0.4.0)
|
||||
ffi-compiler (~> 1.0)
|
||||
rake (~> 13.0)
|
||||
|
@ -318,6 +323,7 @@ DEPENDENCIES
|
|||
acme-client (~> 2.0.0)
|
||||
activesupport
|
||||
addressable (>= 2.8.0)
|
||||
adequate_crypto_address
|
||||
apparition!
|
||||
base32
|
||||
bcrypt
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
class BitcoinValidator
|
||||
class << self
|
||||
def address_version
|
||||
"00"
|
||||
end
|
||||
|
||||
def p2sh_version
|
||||
"05"
|
||||
end
|
||||
|
||||
def valid_address?(address)
|
||||
hex = decode_base58(address) rescue nil
|
||||
return false unless hex && hex.bytesize == 50
|
||||
return false unless [address_version, p2sh_version].include?(hex[0...2])
|
||||
base58_checksum?(address)
|
||||
end
|
||||
|
||||
def decode_base58(base58_val)
|
||||
s = base58_to_int(base58_val).to_s(16); s = (s.bytesize.odd? ? '0'+s : s)
|
||||
s = '' if s == '00'
|
||||
leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
|
||||
s = ("00"*leading_zero_bytes) + s if leading_zero_bytes > 0
|
||||
s
|
||||
end
|
||||
|
||||
def base58_checksum?(base58)
|
||||
hex = decode_base58(base58) rescue nil
|
||||
return false unless hex
|
||||
checksum( hex[0...42] ) == hex[-8..-1]
|
||||
end
|
||||
|
||||
def checksum(hex)
|
||||
b = [hex].pack("H*") # unpack hex
|
||||
Digest::SHA256.hexdigest( Digest::SHA256.digest(b) )[0...8]
|
||||
end
|
||||
|
||||
|
||||
def base58_to_int(base58_val)
|
||||
alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
int_val, base = 0, alpha.size
|
||||
base58_val.reverse.each_char.with_index do |char,index|
|
||||
raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = alpha.index(char)
|
||||
int_val += char_index*(base**index)
|
||||
end
|
||||
int_val
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -1119,7 +1119,7 @@ class Site < Sequel::Model
|
|||
errors.add :tipping_paypal, 'A valid PayPal tipping email address is required.'
|
||||
end
|
||||
|
||||
if !values[:tipping_bitcoin].blank? && !BitcoinValidator.valid_address?(values[:tipping_bitcoin])
|
||||
if !values[:tipping_bitcoin].blank? && !AdequateCryptoAddress.valid?(values[:tipping_bitcoin], 'BTC')
|
||||
errors.add :tipping_bitcoin, 'Bitcoin tipping address is not valid.'
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue