github.com/ontio/ontology@v1.14.4/vm/crossvm_codec/notify_codec.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 package crossvm_codec 19 20 import ( 21 "bytes" 22 "encoding/hex" 23 "fmt" 24 "math/big" 25 "reflect" 26 27 "github.com/ontio/ontology/common" 28 "github.com/ontio/ontology/common/log" 29 ) 30 31 func DeserializeNotify(input []byte) interface{} { 32 val, err := parseNotify(input) 33 if err != nil { 34 return input 35 } 36 37 return stringify(val) 38 } 39 40 func stringify(notify interface{}) interface{} { 41 switch val := notify.(type) { 42 case []byte: 43 return hex.EncodeToString(val) 44 case common.Address: 45 return val.ToBase58() 46 case bool, string: 47 return val 48 case common.Uint256: 49 return val.ToHexString() 50 case *big.Int: 51 return fmt.Sprintf("%d", val) 52 case []interface{}: 53 list := make([]interface{}, 0, len(val)) 54 for _, v := range val { 55 list = append(list, stringify(v)) 56 } 57 return list 58 default: 59 log.Warn("notify codec: unsupported type:", reflect.TypeOf(val).String()) 60 61 return val 62 } 63 } 64 65 // input byte array should be the following format 66 // evt\0(4byte) + type(1byte) + usize( bytearray or list) (4 bytes) + data... 67 func parseNotify(input []byte) (interface{}, error) { 68 if !bytes.HasPrefix(input, []byte("evt\x00")) { 69 return nil, ERROR_PARAM_FORMAT 70 } 71 72 source := common.NewZeroCopySource(input[4:]) 73 74 return DecodeValue(source) 75 }