|
|
@ -109,6 +109,7 @@ var ( |
|
|
|
"isbn13": isISBN13, |
|
|
|
"isbn13": isISBN13, |
|
|
|
"eth_addr": isEthereumAddress, |
|
|
|
"eth_addr": isEthereumAddress, |
|
|
|
"btc_addr": isBitcoinAddress, |
|
|
|
"btc_addr": isBitcoinAddress, |
|
|
|
|
|
|
|
"btc_addr_bech32": isBitcoinBech32Address, |
|
|
|
"uuid": isUUID, |
|
|
|
"uuid": isUUID, |
|
|
|
"uuid3": isUUID3, |
|
|
|
"uuid3": isUUID3, |
|
|
|
"uuid4": isUUID4, |
|
|
|
"uuid4": isUUID4, |
|
|
@ -393,16 +394,28 @@ func isISBN10(fl FieldLevel) bool { |
|
|
|
|
|
|
|
|
|
|
|
// IsEthereumAddress is the validation function for validating if the field's value is a valid ethereum address based currently only on the format
|
|
|
|
// IsEthereumAddress is the validation function for validating if the field's value is a valid ethereum address based currently only on the format
|
|
|
|
func isEthereumAddress(fl FieldLevel) bool { |
|
|
|
func isEthereumAddress(fl FieldLevel) bool { |
|
|
|
field := fl.Field() |
|
|
|
address := fl.Field().String() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !ethAddressRegex.MatchString(address) { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ethaddressRegexUpper.MatchString(address) || ethAddressRegexLower.MatchString(address) { |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ethAddressRegex.MatchString(field.String()) |
|
|
|
// checksum validation is blocked by https://github.com/golang/crypto/pull/28
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// IsBitcoinAddress is the validation function for validating if the field's value is a valid btc address, currently only based on the format
|
|
|
|
// IsBitcoinAddress is the validation function for validating if the field's value is a valid btc address
|
|
|
|
func isBitcoinAddress(fl FieldLevel) bool { |
|
|
|
func isBitcoinAddress(fl FieldLevel) bool { |
|
|
|
address := fl.Field().String() |
|
|
|
address := fl.Field().String() |
|
|
|
|
|
|
|
|
|
|
|
if btcAddressRegex.MatchString(address) { |
|
|
|
if !btcAddressRegex.MatchString(address) { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
alphabet := []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |
|
|
|
alphabet := []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |
|
|
|
|
|
|
|
|
|
|
@ -411,10 +424,6 @@ func isBitcoinAddress(fl FieldLevel) bool { |
|
|
|
for _, n := range []byte(address) { |
|
|
|
for _, n := range []byte(address) { |
|
|
|
d := bytes.IndexByte(alphabet, n) |
|
|
|
d := bytes.IndexByte(alphabet, n) |
|
|
|
|
|
|
|
|
|
|
|
if(d == -1){ |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 24; i >= 0; i-- { |
|
|
|
for i := 24; i >= 0; i-- { |
|
|
|
d += 58 * int(decode[i]) |
|
|
|
d += 58 * int(decode[i]) |
|
|
|
decode[i] = byte(d % 256) |
|
|
|
decode[i] = byte(d % 256) |
|
|
@ -439,12 +448,96 @@ func isBitcoinAddress(fl FieldLevel) bool { |
|
|
|
copy(computedchecksum[:], h.Sum(d[:0])) |
|
|
|
copy(computedchecksum[:], h.Sum(d[:0])) |
|
|
|
copy(validchecksum[:], decode[21:]) |
|
|
|
copy(validchecksum[:], decode[21:]) |
|
|
|
|
|
|
|
|
|
|
|
println(address, "::", validchecksum[:], computedchecksum[:]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return validchecksum == computedchecksum |
|
|
|
return validchecksum == computedchecksum |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// IsBitcoinAddress is the validation function for validating if the field's value is a valid bech32 btc address
|
|
|
|
|
|
|
|
func isBitcoinBech32Address(fl FieldLevel) bool { |
|
|
|
|
|
|
|
address := fl.Field().String() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !btcAddressRegexBech32.MatchString(address){ |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
am := len(address) % 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if am == 0 || am == 3 || am == 5{ |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
address = strings.ToLower(address) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
alphabet := "qpzry9x8gf2tvdw0s3jn54khce6mua7l" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hr := []int{3, 3, 0, 2, 3} // the human readable part will always be bc
|
|
|
|
|
|
|
|
dp := []int{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, c := range []rune(address[3:]) { |
|
|
|
|
|
|
|
dp = append(dp, strings.IndexRune(alphabet, c)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ver := dp[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ver < 0 || ver > 16{ |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ver == 0 { |
|
|
|
|
|
|
|
if len(address) != 42 && len(address) != 62 { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
values := append(hr, dp...) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GEN := []int{ 0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3 } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p := 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range values { |
|
|
|
|
|
|
|
b := p >> 25 |
|
|
|
|
|
|
|
p = (p & 0x1ffffff) << 5 ^ v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ { |
|
|
|
|
|
|
|
if (b >> uint(i)) & 1 == 1 { |
|
|
|
|
|
|
|
p ^= GEN[i] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if p != 1 { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b := uint(0) |
|
|
|
|
|
|
|
acc := 0 |
|
|
|
|
|
|
|
mv := (1 << 5) - 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sw := []int{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dp = dp[:len(dp) - 6] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range dp[1:]{ |
|
|
|
|
|
|
|
if v < 0 || (v >> 5) != 0{ |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
acc = (acc << 5) | v |
|
|
|
|
|
|
|
b += 5 |
|
|
|
|
|
|
|
for b >= 8{ |
|
|
|
|
|
|
|
b -= 8 |
|
|
|
|
|
|
|
sw = append(sw, (acc>>b)&mv) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(sw) < 2 || len(sw) > 40{ |
|
|
|
|
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return btcAddressRegexBech32.MatchString(address) |
|
|
|
if ver == 0 && len(sw) != 20 && len(sw) != 32 { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ExcludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
|
|
|
|
// ExcludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
|
|
|
|