verifies p2pkh addresses

pull/355/head
josh 7 years ago
parent 36d83b0b83
commit 60417282a4
  1. 48
      baked_in.go
  2. 15
      validator_test.go

@ -11,6 +11,8 @@ import (
"sync" "sync"
"time" "time"
"unicode/utf8" "unicode/utf8"
"crypto/sha256"
"bytes"
) )
// Func accepts a FieldLevel interface for all validation needs. The return // Func accepts a FieldLevel interface for all validation needs. The return
@ -398,9 +400,51 @@ func isEthereumAddress(fl FieldLevel) bool {
// 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, currently only based on the format
func isBitcoinAddress(fl FieldLevel) bool { func isBitcoinAddress(fl FieldLevel) bool {
field := fl.Field() address := fl.Field().String()
if btcAddressRegex.MatchString(address) {
alphabet := []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
decode := [25]byte{}
for _, n := range []byte(address) {
d := bytes.IndexByte(alphabet, n)
if(d == -1){
return false
}
for i := 24; i >= 0; i-- {
d += 58 * int(decode[i])
decode[i] = byte(d % 256)
d /= 256
}
}
if decode[0] != 0 {
return false
}
h := sha256.New()
h.Write(decode[:21])
d := h.Sum([]byte{})
h = sha256.New()
h.Write(d)
validchecksum := [4]byte{}
computedchecksum := [4]byte{}
copy(computedchecksum[:], h.Sum(d[:0]))
copy(validchecksum[:], decode[21:])
println(address, "::", validchecksum[:], computedchecksum[:])
return validchecksum == computedchecksum
}
return btcAddressRegex.MatchString(field.String()) || btcAddressRegexBech32.MatchString(field.String()) return btcAddressRegexBech32.MatchString(address)
} }
// 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.

@ -4446,10 +4446,11 @@ func TestBitcoinAddressValidation(t *testing.T){
}{ }{
{"", false}, {"", false},
{"0x02F9AE5f22EA3fA88F05780B30385bEC", false}, {"0x02F9AE5f22EA3fA88F05780B30385bEC", false},
{"123f681646d4a755815f9cb19e1acc8565a0c2ac", false}, {"1A1zP1ePQGefi2DMPTifTL5SLmv7DivfNa", false},
{"1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2", true}, {"1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2", true}, // valid p2pkh address
{"3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy", true}, {"3P14159f73E4gFr7JterCCQh9QjiTjiZrG", true}, // valid p2sh address
{"bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", true}, {"bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", true}, // valid bech32 address
} }
@ -4460,15 +4461,15 @@ func TestBitcoinAddressValidation(t *testing.T){
if test.expected { if test.expected {
if !IsEqual(errs, nil) { if !IsEqual(errs, nil) {
t.Fatalf("Index: %d btc_addr failed Error: %s", i, errs) t.Fatalf("Index: %d btc_addr failed with Error: %s", i, errs)
} }
} else { } else {
if IsEqual(errs, nil) { if IsEqual(errs, nil) {
t.Fatalf("Index: %d btc_addr failed Error: %s", i, errs) t.Fatalf("Index: %d btc_addr failed with Error: %s", i, errs)
} else { } else {
val := getError(errs, "", "") val := getError(errs, "", "")
if val.Tag() != "btc_addr" { if val.Tag() != "btc_addr" {
t.Fatalf("Index: %d Latitude failed Error: %s", i, errs) t.Fatalf("Index: %d Latitude failed with Error: %s", i, errs)
} }
} }
} }

Loading…
Cancel
Save