github.com/core-coin/go-core/v2@v2.1.9/accounts/abi/topics.go (about) 1 // Copyright 2018 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core 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/core-coin/go-core/v2/common" 27 "github.com/core-coin/go-core/v2/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.SHA3Hash([]byte(rule)) 72 copy(topic[:], hash[:]) 73 case []byte: 74 hash := crypto.SHA3Hash(rule) 75 copy(topic[:], hash[:]) 76 77 default: 78 // todo(raisty) according ylem documentation, indexed event 79 // parameters that are not value types i.e. arrays and structs are not 80 // stored directly but instead a SHA3-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 SHA3 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 SHA3 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.Uint16(topics[i][0:2]); garbage != 0 { 156 return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) 157 } 158 if garbage := binary.BigEndian.Uint32(topics[i][2:6]); garbage != 0 { 159 return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) 160 } 161 var tmp [26]byte 162 copy(tmp[:], topics[i][6:32]) 163 reconstr = tmp 164 default: 165 var err error 166 reconstr, err = toGoType(0, arg.Type, topics[i].Bytes()) 167 if err != nil { 168 return err 169 } 170 } 171 // Use the setter function to store the value 172 setter(arg, reconstr) 173 } 174 175 return nil 176 }