Skip to content

Commit 1066397

Browse files
dafernaalexjba
authored andcommittedAug 20, 2024
signer/core: accept all solidity primitive types for EIP-712 signing (ethereum#26770)
Accept all primitive types in Solidity for EIP-712 from intN, uintN, intN[], uintN[] for N as 0 to 256 in multiples of 8 --------- Co-authored-by: Martin Holst Swende <martin@swende.se> (cherry picked from commit 02796f6)
1 parent 5fa0248 commit 1066397

File tree

2 files changed

+60
-102
lines changed

2 files changed

+60
-102
lines changed
 

‎signer/core/apitypes/types.go

+20-102
Original file line numberDiff line numberDiff line change
@@ -720,112 +720,30 @@ func isPrimitiveTypeValid(primitiveType string) bool {
720720
primitiveType == "bool" ||
721721
primitiveType == "bool[]" ||
722722
primitiveType == "string" ||
723-
primitiveType == "string[]" {
724-
return true
725-
}
726-
if primitiveType == "bytes" ||
723+
primitiveType == "string[]" ||
724+
primitiveType == "bytes" ||
727725
primitiveType == "bytes[]" ||
728-
primitiveType == "bytes1" ||
729-
primitiveType == "bytes1[]" ||
730-
primitiveType == "bytes2" ||
731-
primitiveType == "bytes2[]" ||
732-
primitiveType == "bytes3" ||
733-
primitiveType == "bytes3[]" ||
734-
primitiveType == "bytes4" ||
735-
primitiveType == "bytes4[]" ||
736-
primitiveType == "bytes5" ||
737-
primitiveType == "bytes5[]" ||
738-
primitiveType == "bytes6" ||
739-
primitiveType == "bytes6[]" ||
740-
primitiveType == "bytes7" ||
741-
primitiveType == "bytes7[]" ||
742-
primitiveType == "bytes8" ||
743-
primitiveType == "bytes8[]" ||
744-
primitiveType == "bytes9" ||
745-
primitiveType == "bytes9[]" ||
746-
primitiveType == "bytes10" ||
747-
primitiveType == "bytes10[]" ||
748-
primitiveType == "bytes11" ||
749-
primitiveType == "bytes11[]" ||
750-
primitiveType == "bytes12" ||
751-
primitiveType == "bytes12[]" ||
752-
primitiveType == "bytes13" ||
753-
primitiveType == "bytes13[]" ||
754-
primitiveType == "bytes14" ||
755-
primitiveType == "bytes14[]" ||
756-
primitiveType == "bytes15" ||
757-
primitiveType == "bytes15[]" ||
758-
primitiveType == "bytes16" ||
759-
primitiveType == "bytes16[]" ||
760-
primitiveType == "bytes17" ||
761-
primitiveType == "bytes17[]" ||
762-
primitiveType == "bytes18" ||
763-
primitiveType == "bytes18[]" ||
764-
primitiveType == "bytes19" ||
765-
primitiveType == "bytes19[]" ||
766-
primitiveType == "bytes20" ||
767-
primitiveType == "bytes20[]" ||
768-
primitiveType == "bytes21" ||
769-
primitiveType == "bytes21[]" ||
770-
primitiveType == "bytes22" ||
771-
primitiveType == "bytes22[]" ||
772-
primitiveType == "bytes23" ||
773-
primitiveType == "bytes23[]" ||
774-
primitiveType == "bytes24" ||
775-
primitiveType == "bytes24[]" ||
776-
primitiveType == "bytes25" ||
777-
primitiveType == "bytes25[]" ||
778-
primitiveType == "bytes26" ||
779-
primitiveType == "bytes26[]" ||
780-
primitiveType == "bytes27" ||
781-
primitiveType == "bytes27[]" ||
782-
primitiveType == "bytes28" ||
783-
primitiveType == "bytes28[]" ||
784-
primitiveType == "bytes29" ||
785-
primitiveType == "bytes29[]" ||
786-
primitiveType == "bytes30" ||
787-
primitiveType == "bytes30[]" ||
788-
primitiveType == "bytes31" ||
789-
primitiveType == "bytes31[]" ||
790-
primitiveType == "bytes32" ||
791-
primitiveType == "bytes32[]" {
792-
return true
793-
}
794-
if primitiveType == "int" ||
726+
primitiveType == "int" ||
795727
primitiveType == "int[]" ||
796-
primitiveType == "int8" ||
797-
primitiveType == "int8[]" ||
798-
primitiveType == "int16" ||
799-
primitiveType == "int16[]" ||
800-
primitiveType == "int32" ||
801-
primitiveType == "int32[]" ||
802-
primitiveType == "int64" ||
803-
primitiveType == "int64[]" ||
804-
primitiveType == "int96" ||
805-
primitiveType == "int96[]" ||
806-
primitiveType == "int128" ||
807-
primitiveType == "int128[]" ||
808-
primitiveType == "int256" ||
809-
primitiveType == "int256[]" {
728+
primitiveType == "uint" ||
729+
primitiveType == "uint[]" {
810730
return true
811731
}
812-
if primitiveType == "uint" ||
813-
primitiveType == "uint[]" ||
814-
primitiveType == "uint8" ||
815-
primitiveType == "uint8[]" ||
816-
primitiveType == "uint16" ||
817-
primitiveType == "uint16[]" ||
818-
primitiveType == "uint32" ||
819-
primitiveType == "uint32[]" ||
820-
primitiveType == "uint64" ||
821-
primitiveType == "uint64[]" ||
822-
primitiveType == "uint96" ||
823-
primitiveType == "uint96[]" ||
824-
primitiveType == "uint128" ||
825-
primitiveType == "uint128[]" ||
826-
primitiveType == "uint256" ||
827-
primitiveType == "uint256[]" {
828-
return true
732+
// For 'bytesN', 'bytesN[]', we allow N from 1 to 32
733+
for n := 1; n <= 32; n++ {
734+
// e.g. 'bytes28' or 'bytes28[]'
735+
if primitiveType == fmt.Sprintf("bytes%d", n) || primitiveType == fmt.Sprintf("bytes%d[]", n) {
736+
return true
737+
}
738+
}
739+
// For 'intN','intN[]' and 'uintN','uintN[]' we allow N in increments of 8, from 8 up to 256
740+
for n := 8; n <= 256; n += 8 {
741+
if primitiveType == fmt.Sprintf("int%d", n) || primitiveType == fmt.Sprintf("int%d[]", n) {
742+
return true
743+
}
744+
if primitiveType == fmt.Sprintf("uint%d", n) || primitiveType == fmt.Sprintf("uint%d[]", n) {
745+
return true
746+
}
829747
}
830748
return false
831749
}

‎signer/core/apitypes/types_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package apitypes
18+
19+
import "testing"
20+
21+
func TestIsPrimitive(t *testing.T) {
22+
// Expected positives
23+
for i, tc := range []string{
24+
"int24", "int24[]", "uint88", "uint88[]", "uint", "uint[]", "int256", "int256[]",
25+
"uint96", "uint96[]", "int96", "int96[]", "bytes17[]", "bytes17",
26+
} {
27+
if !isPrimitiveTypeValid(tc) {
28+
t.Errorf("test %d: expected '%v' to be a valid primitive", i, tc)
29+
}
30+
}
31+
// Expected negatives
32+
for i, tc := range []string{
33+
"int257", "int257[]", "uint88 ", "uint88 []", "uint257", "uint-1[]",
34+
"uint0", "uint0[]", "int95", "int95[]", "uint1", "uint1[]", "bytes33[]", "bytess",
35+
} {
36+
if isPrimitiveTypeValid(tc) {
37+
t.Errorf("test %d: expected '%v' to not be a valid primitive", i, tc)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)