Skip to content

Commit 4407f4d

Browse files
authored
Add checksum validation for Ethereum address (#630)
1 parent 8941cbd commit 4407f4d

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

baked_in.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"crypto/sha256"
7+
"encoding/hex"
78
"encoding/json"
89
"fmt"
910
"net"
@@ -16,6 +17,8 @@ import (
1617
"time"
1718
"unicode/utf8"
1819

20+
"golang.org/x/crypto/sha3"
21+
1922
urn "github.com/leodido/go-urn"
2023
)
2124

@@ -532,7 +535,7 @@ func isISBN10(fl FieldLevel) bool {
532535
return checksum%11 == 0
533536
}
534537

535-
// IsEthereumAddress is the validation function for validating if the field's value is a valid ethereum address based currently only on the format
538+
// IsEthereumAddress is the validation function for validating if the field's value is a valid Ethereum address.
536539
func isEthereumAddress(fl FieldLevel) bool {
537540
address := fl.Field().String()
538541

@@ -544,7 +547,21 @@ func isEthereumAddress(fl FieldLevel) bool {
544547
return true
545548
}
546549

547-
// checksum validation is blocked by https://github.com/golang/crypto/pull/28
550+
// Checksum validation. Reference: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
551+
address = address[2:] // Skip "0x" prefix.
552+
h := sha3.NewLegacyKeccak256()
553+
// hash.Hash's io.Writer implementation says it never returns an error. https://golang.org/pkg/hash/#Hash
554+
_, _ = h.Write([]byte(strings.ToLower(address)))
555+
hash := hex.EncodeToString(h.Sum(nil))
556+
557+
for i := 0; i < len(address); i++ {
558+
if address[i] <= '9' { // Skip 0-9 digits: they don't have upper/lower-case.
559+
continue
560+
}
561+
if hash[i] > '7' && address[i] >= 'a' || hash[i] <= '7' && address[i] <= 'F' {
562+
return false
563+
}
564+
}
548565

549566
return true
550567
}

doc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,7 @@ Special thanks to Pieter Wuille for providng reference implementations.
879879
Ethereum Address
880880
881881
This validates that a string value contains a valid ethereum address.
882-
The format of the string is checked to ensure it matches the standard Ethereum address format
883-
Full validation is blocked by https://github.com/golang/crypto/pull/28
882+
The format of the string is checked to ensure it matches the standard Ethereum address format.
884883
885884
Usage: eth_addr
886885

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require (
77
github.com/go-playground/locales v0.13.0
88
github.com/go-playground/universal-translator v0.17.0
99
github.com/leodido/go-urn v1.2.0
10+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
1011
)

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
1313
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1414
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1515
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
16+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
17+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
18+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
19+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
20+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1623
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
1724
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
1825
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

validator_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5207,11 +5207,29 @@ func TestEthereumAddressValidation(t *testing.T) {
52075207
param string
52085208
expected bool
52095209
}{
5210-
{"", false},
5211-
{"0x02F9AE5f22EA3fA88F05780B30385bEC", false},
5212-
{"123f681646d4a755815f9cb19e1acc8565a0c2ac", false},
5213-
{"0x02F9AE5f22EA3fA88F05780B30385bECFacbf130", true},
5210+
// All caps.
5211+
{"0x52908400098527886E0F7030069857D2E4169EE7", true},
5212+
{"0x8617E340B3D01FA5F11F306F4090FD50E238070D", true},
5213+
5214+
// All lower.
5215+
{"0xde709f2102306220921060314715629080e2fb77", true},
5216+
{"0x27b1fdb04752bbc536007a920d24acb045561c26", true},
52145217
{"0x123f681646d4a755815f9cb19e1acc8565a0c2ac", true},
5218+
5219+
// Mixed case: runs checksum validation.
5220+
{"0x02F9AE5f22EA3fA88F05780B30385bECFacbf130", true},
5221+
{"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed", true},
5222+
{"0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", true},
5223+
{"0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB", true},
5224+
{"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb", true},
5225+
{"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDB", false}, // Invalid checksum.
5226+
5227+
// Other.
5228+
{"", false},
5229+
{"D1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb", false}, // Missing "0x" prefix.
5230+
{"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDbc", false}, // More than 40 hex digits.
5231+
{"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aD", false}, // Less than 40 hex digits.
5232+
{"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDw", false}, // Invalid hex digit "w".
52155233
}
52165234

52175235
for i, test := range tests {

0 commit comments

Comments
 (0)