github.com/benorgera/go-ethereum@v1.10.18-0.20220401011646-b3f57b1a73ba/accounts/abi/topics.go (about) 1 // Copyright 2018 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 abi 18 19 import ( 20 "encoding/binary" 21 "errors" 22 "fmt" 23 "math/big" 24 "reflect" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/crypto" 28 ) 29 30 // MakeTopics converts a filter query argument list into a filter topic set. 31 func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) { 32 topics := make([][]common.Hash, len(query)) 33 for i, filter := range query { 34 for _, rule := range filter { 35 var topic common.Hash 36 37 // Try to generate the topic based on simple types 38 switch rule := rule.(type) { 39 case common.Hash: 40 copy(topic[:], rule[:]) 41 case common.Address: 42 copy(topic[common.HashLength-common.AddressLength:], rule[:]) 43 case *big.Int: 44 blob := rule.Bytes() 45 copy(topic[common.HashLength-len(blob):], blob) 46 case bool: 47 if rule { 48 topic[common.HashLength-1] = 1 49 } 50 case int8: 51 copy(topic[:], genIntType(int64(rule), 1)) 52 case int16: 53 copy(topic[:], genIntType(int64(rule), 2)) 54 case int32: 55 copy(topic[:], genIntType(int64(rule), 4)) 56 case int64: 57 copy(topic[:], genIntType(rule, 8)) 58 case uint8: 59 blob := new(big.Int).SetUint64(uint64(rule)).Bytes() 60 copy(topic[common.HashLength-len(blob):], blob) 61 case uint16: 62 blob := new(big.Int).SetUint64(uint64(rule)).Bytes() 63 copy(topic[common.HashLength-len(blob):], blob) 64 case uint32: 65 blob := new(big.Int).SetUint64(uint64(rule)).Bytes() 66 copy(topic[common.HashLength-len(blob):], blob) 67 case uint64: 68 blob := new(big.Int).SetUint64(rule).Bytes() 69 copy(topic[common.HashLength-len(blob):], blob) 70 case string: 71 hash := crypto.Keccak256Hash([]byte(rule)) 72 copy(topic[:], hash[:]) 73 case []byte: 74 hash := crypto.Keccak256Hash(rule) 75 copy(topic[:], hash[:]) 76 77 default: 78 // todo(rjl493456442) according solidity documentation, indexed event 79 // parameters that are not value types i.e. arrays and structs are not 80 // stored directly but instead a keccak256-hash of an encoding is stored. 81 // 82 // We only convert stringS and bytes to hash, still need to deal with 83 // array(both fixed-size and dynamic-size) and struct. 84 85 // Attempt to generate the topic from funky types 86 val := reflect.ValueOf(rule) 87 switch { 88 // static byte array 89 case val.Kind() == reflect.Array && reflect.TypeOf(rule).Elem().Kind() == reflect.Uint8: 90 reflect.Copy(reflect.ValueOf(topic[:val.Len()]), val) 91 default: 92 return nil, fmt.Errorf("unsupported indexed type: %T", rule) 93 } 94 } 95 topics[i] = append(topics[i], topic) 96 } 97 } 98 return topics, nil 99 } 100 101 func genIntType(rule int64, size uint) []byte { 102 var topic [common.HashLength]byte 103 if rule < 0 { 104 // if a rule is negative, we need to put it into two's complement. 105 // extended to common.HashLength bytes. 106 topic = [common.HashLength]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 107 } 108 for i := uint(0); i < size; i++ { 109 topic[common.HashLength-i-1] = byte(rule >> (i * 8)) 110 } 111 return topic[:] 112 } 113 114 // ParseTopics converts the indexed topic fields into actual log field values. 115 func ParseTopics(out interface{}, fields Arguments, topics []common.Hash) error { 116 return parseTopicWithSetter(fields, topics, 117 func(arg Argument, reconstr interface{}) { 118 field := reflect.ValueOf(out).Elem().FieldByName(ToCamelCase(arg.Name)) 119 field.Set(reflect.ValueOf(reconstr)) 120 }) 121 } 122 123 // ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs. 124 func ParseTopicsIntoMap(out map[string]interface{}, fields Arguments, topics []common.Hash) error { 125 return parseTopicWithSetter(fields, topics, 126 func(arg Argument, reconstr interface{}) { 127 out[arg.Name] = reconstr 128 }) 129 } 130 131 // parseTopicWithSetter converts the indexed topic field-value pairs and stores them using the 132 // provided set function. 133 // 134 // Note, dynamic types cannot be reconstructed since they get mapped to Keccak256 135 // hashes as the topic value! 136 func parseTopicWithSetter(fields Arguments, topics []common.Hash, setter func(Argument, interface{})) error { 137 // Sanity check that the fields and topics match up 138 if len(fields) != len(topics) { 139 return errors.New("topic/field count mismatch") 140 } 141 // Iterate over all the fields and reconstruct them from topics 142 for i, arg := range fields { 143 if !arg.Indexed { 144 return errors.New("non-indexed field in topic reconstruction") 145 } 146 var reconstr interface{} 147 switch arg.Type.T { 148 case TupleTy: 149 return errors.New("tuple type in topic reconstruction") 150 case StringTy, BytesTy, SliceTy, ArrayTy: 151 // Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash 152 // whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash 153 reconstr = topics[i] 154 case FunctionTy: 155 if garbage := binary.BigEndian.Uint64(topics[i][0:8]); garbage != 0 { 156 return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) 157 } 158 var tmp [24]byte 159 copy(tmp[:], topics[i][8:32]) 160 reconstr = tmp 161 default: 162 var err error 163 reconstr, err = toGoType(0, arg.Type, topics[i].Bytes()) 164 if err != nil { 165 return err 166 } 167 } 168 // Use the setter function to store the value 169 setter(arg, reconstr) 170 } 171 172 return nil 173 }