|
| 1 | +// Copyright 2017 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 usbwallet |
| 18 | + |
| 19 | +import ( |
| 20 | +"fmt" |
| 21 | +"regexp" |
| 22 | +"strconv" |
| 23 | +"strings" |
| 24 | + |
| 25 | +"github.com/ethereum/go-ethereum/signer/core/apitypes" |
| 26 | +) |
| 27 | + |
| 28 | +type dataType byte |
| 29 | + |
| 30 | +const ( |
| 31 | +CustomType dataType = iota |
| 32 | +IntType |
| 33 | +UintType |
| 34 | +AddressType |
| 35 | +BoolType |
| 36 | +StringType |
| 37 | +FixedBytesType |
| 38 | +BytesType |
| 39 | +) |
| 40 | + |
| 41 | +var nameToType = map[string]dataType{ |
| 42 | +"int": IntType, |
| 43 | +"uint": UintType, |
| 44 | +"address": AddressType, |
| 45 | +"bool": BoolType, |
| 46 | +"string": StringType, |
| 47 | +"bytes": BytesType, |
| 48 | +} |
| 49 | + |
| 50 | +func parseType(data apitypes.TypedData, field apitypes.Type) (dt dataType, name string, byteLength int, arrayLevels []*int, err error) { |
| 51 | +name = strings.TrimSpace(field.Type) |
| 52 | +arrayLengths := regexp.MustCompile(`\[(\d*)]`).FindAllStringSubmatch(name, -1) |
| 53 | +if len(arrayLengths) > 0 { |
| 54 | +arrayLevels = make([]*int, len(arrayLengths)) |
| 55 | +for i, arrayLength := range arrayLengths { |
| 56 | +if len(arrayLength[1]) == 0 { |
| 57 | +arrayLevels[i] = nil // nil means dynamic length |
| 58 | +} else { |
| 59 | +length, _ := strconv.Atoi(arrayLength[1]) // guaranteed to be a digit, ignore error |
| 60 | +arrayLevels[i] = &length |
| 61 | +} |
| 62 | +} |
| 63 | +name = name[0:strings.Index(name, "[")] |
| 64 | +} |
| 65 | +if data.Types[name] != nil { |
| 66 | +dt = CustomType |
| 67 | +return |
| 68 | +} |
| 69 | + |
| 70 | +matches := regexp.MustCompile(`^(.+?)(\d*)$`).FindStringSubmatch(name) |
| 71 | +name = matches[1] |
| 72 | +lengthStr := matches[2] |
| 73 | + |
| 74 | +var ok bool |
| 75 | +if dt, ok = nameToType[name]; !ok { |
| 76 | +err = fmt.Errorf("unknown type: %s", field.Type) |
| 77 | +return |
| 78 | +} |
| 79 | + |
| 80 | +byteLength, _ = strconv.Atoi(lengthStr) |
| 81 | +if dt == UintType || dt == IntType { |
| 82 | +if lengthStr == "" { |
| 83 | +byteLength = 32 |
| 84 | +} else if byteLength%8 != 0 { |
| 85 | +err = fmt.Errorf("invalid length for %s: %s", field.Type, lengthStr) |
| 86 | +return |
| 87 | +} else { |
| 88 | +byteLength /= 8 |
| 89 | +} |
| 90 | +} else if lengthStr != "" { |
| 91 | +if dt == BytesType { |
| 92 | +dt = FixedBytesType |
| 93 | +} else { |
| 94 | +err = fmt.Errorf("invalid type: %s", field.Type) |
| 95 | +return |
| 96 | +} |
| 97 | +} else if dt == AddressType { |
| 98 | +byteLength = 20 // address is always 20 bytes |
| 99 | +} |
| 100 | +if lengthStr != "" && (byteLength < 1 || byteLength > 32) { |
| 101 | +err = fmt.Errorf("invalid length for %s: %s", field.Type, lengthStr) |
| 102 | +return |
| 103 | +} |
| 104 | + |
| 105 | +return |
| 106 | +} |
0 commit comments