| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | // Copyright 2018 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | package abi | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 	"encoding/binary" | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"math/big" | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | // MakeTopics converts a filter query argument list into a filter topic set. | 
					
						
							|  |  |  | func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) { | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 	topics := make([][]common.Hash, len(query)) | 
					
						
							|  |  |  | 	for i, filter := range query { | 
					
						
							|  |  |  | 		for _, rule := range filter { | 
					
						
							|  |  |  | 			var topic common.Hash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Try to generate the topic based on simple types | 
					
						
							|  |  |  | 			switch rule := rule.(type) { | 
					
						
							|  |  |  | 			case common.Hash: | 
					
						
							|  |  |  | 				copy(topic[:], rule[:]) | 
					
						
							|  |  |  | 			case common.Address: | 
					
						
							|  |  |  | 				copy(topic[common.HashLength-common.AddressLength:], rule[:]) | 
					
						
							|  |  |  | 			case *big.Int: | 
					
						
							|  |  |  | 				blob := rule.Bytes() | 
					
						
							|  |  |  | 				copy(topic[common.HashLength-len(blob):], blob) | 
					
						
							|  |  |  | 			case bool: | 
					
						
							|  |  |  | 				if rule { | 
					
						
							|  |  |  | 					topic[common.HashLength-1] = 1 | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			case int8: | 
					
						
							| 
									
										
										
										
											2020-04-09 10:54:57 +02:00
										 |  |  | 				copy(topic[:], genIntType(int64(rule), 1)) | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 			case int16: | 
					
						
							| 
									
										
										
										
											2020-04-09 10:54:57 +02:00
										 |  |  | 				copy(topic[:], genIntType(int64(rule), 2)) | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 			case int32: | 
					
						
							| 
									
										
										
										
											2020-04-09 10:54:57 +02:00
										 |  |  | 				copy(topic[:], genIntType(int64(rule), 4)) | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 			case int64: | 
					
						
							| 
									
										
										
										
											2020-04-09 10:54:57 +02:00
										 |  |  | 				copy(topic[:], genIntType(rule, 8)) | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 			case uint8: | 
					
						
							|  |  |  | 				blob := new(big.Int).SetUint64(uint64(rule)).Bytes() | 
					
						
							|  |  |  | 				copy(topic[common.HashLength-len(blob):], blob) | 
					
						
							|  |  |  | 			case uint16: | 
					
						
							|  |  |  | 				blob := new(big.Int).SetUint64(uint64(rule)).Bytes() | 
					
						
							|  |  |  | 				copy(topic[common.HashLength-len(blob):], blob) | 
					
						
							|  |  |  | 			case uint32: | 
					
						
							|  |  |  | 				blob := new(big.Int).SetUint64(uint64(rule)).Bytes() | 
					
						
							|  |  |  | 				copy(topic[common.HashLength-len(blob):], blob) | 
					
						
							|  |  |  | 			case uint64: | 
					
						
							|  |  |  | 				blob := new(big.Int).SetUint64(rule).Bytes() | 
					
						
							|  |  |  | 				copy(topic[common.HashLength-len(blob):], blob) | 
					
						
							|  |  |  | 			case string: | 
					
						
							|  |  |  | 				hash := crypto.Keccak256Hash([]byte(rule)) | 
					
						
							|  |  |  | 				copy(topic[:], hash[:]) | 
					
						
							|  |  |  | 			case []byte: | 
					
						
							|  |  |  | 				hash := crypto.Keccak256Hash(rule) | 
					
						
							|  |  |  | 				copy(topic[:], hash[:]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			default: | 
					
						
							| 
									
										
										
										
											2019-10-31 21:17:51 +08:00
										 |  |  | 				// todo(rjl493456442) according solidity documentation, indexed event | 
					
						
							|  |  |  | 				// parameters that are not value types i.e. arrays and structs are not | 
					
						
							|  |  |  | 				// stored directly but instead a keccak256-hash of an encoding is stored. | 
					
						
							|  |  |  | 				// | 
					
						
							|  |  |  | 				// We only convert stringS and bytes to hash, still need to deal with | 
					
						
							|  |  |  | 				// array(both fixed-size and dynamic-size) and struct. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 				// Attempt to generate the topic from funky types | 
					
						
							|  |  |  | 				val := reflect.ValueOf(rule) | 
					
						
							|  |  |  | 				switch { | 
					
						
							| 
									
										
										
										
											2019-01-28 12:47:24 -06:00
										 |  |  | 				// static byte array | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 				case val.Kind() == reflect.Array && reflect.TypeOf(rule).Elem().Kind() == reflect.Uint8: | 
					
						
							| 
									
										
										
										
											2019-01-28 12:47:24 -06:00
										 |  |  | 					reflect.Copy(reflect.ValueOf(topic[:val.Len()]), val) | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | 				default: | 
					
						
							|  |  |  | 					return nil, fmt.Errorf("unsupported indexed type: %T", rule) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			topics[i] = append(topics[i], topic) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return topics, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 10:54:57 +02:00
										 |  |  | func genIntType(rule int64, size uint) []byte { | 
					
						
							|  |  |  | 	var topic [common.HashLength]byte | 
					
						
							|  |  |  | 	if rule < 0 { | 
					
						
							|  |  |  | 		// if a rule is negative, we need to put it into two's complement. | 
					
						
							| 
									
										
										
										
											2020-09-20 11:43:57 +03:00
										 |  |  | 		// extended to common.HashLength bytes. | 
					
						
							| 
									
										
										
										
											2020-04-09 10:54:57 +02:00
										 |  |  | 		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} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i := uint(0); i < size; i++ { | 
					
						
							|  |  |  | 		topic[common.HashLength-i-1] = byte(rule >> (i * 8)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return topic[:] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | // ParseTopics converts the indexed topic fields into actual log field values. | 
					
						
							|  |  |  | func ParseTopics(out interface{}, fields Arguments, topics []common.Hash) error { | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 	return parseTopicWithSetter(fields, topics, | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | 		func(arg Argument, reconstr interface{}) { | 
					
						
							|  |  |  | 			field := reflect.ValueOf(out).Elem().FieldByName(ToCamelCase(arg.Name)) | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			field.Set(reflect.ValueOf(reconstr)) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-01-05 12:39:24 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-20 11:43:57 +03:00
										 |  |  | // ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs. | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | func ParseTopicsIntoMap(out map[string]interface{}, fields Arguments, topics []common.Hash) error { | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 	return parseTopicWithSetter(fields, topics, | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | 		func(arg Argument, reconstr interface{}) { | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			out[arg.Name] = reconstr | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // parseTopicWithSetter converts the indexed topic field-value pairs and stores them using the | 
					
						
							|  |  |  | // provided set function. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Note, dynamic types cannot be reconstructed since they get mapped to Keccak256 | 
					
						
							|  |  |  | // hashes as the topic value! | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | func parseTopicWithSetter(fields Arguments, topics []common.Hash, setter func(Argument, interface{})) error { | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 	// Sanity check that the fields and topics match up | 
					
						
							|  |  |  | 	if len(fields) != len(topics) { | 
					
						
							|  |  |  | 		return errors.New("topic/field count mismatch") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Iterate over all the fields and reconstruct them from topics | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 	for i, arg := range fields { | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 		if !arg.Indexed { | 
					
						
							|  |  |  | 			return errors.New("non-indexed field in topic reconstruction") | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 		var reconstr interface{} | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 		switch arg.Type.T { | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | 		case TupleTy: | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			return errors.New("tuple type in topic reconstruction") | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | 		case StringTy, BytesTy, SliceTy, ArrayTy: | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 			// Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash | 
					
						
							|  |  |  | 			// whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			reconstr = topics[i] | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | 		case FunctionTy: | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			if garbage := binary.BigEndian.Uint64(topics[i][0:8]); garbage != 0 { | 
					
						
							|  |  |  | 				return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			var tmp [24]byte | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			copy(tmp[:], topics[i][8:32]) | 
					
						
							|  |  |  | 			reconstr = tmp | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			var err error | 
					
						
							| 
									
										
										
										
											2020-05-12 12:21:40 +02:00
										 |  |  | 			reconstr, err = toGoType(0, arg.Type, topics[i].Bytes()) | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-08 12:00:10 +02:00
										 |  |  | 		// Use the setter function to store the value | 
					
						
							|  |  |  | 		setter(arg, reconstr) | 
					
						
							| 
									
										
										
										
											2019-04-01 08:42:59 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |