github.com/klaytn/klaytn@v1.12.1/node/sc/event_parse.go (about)

     1  package sc
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  
     7  	"github.com/klaytn/klaytn/accounts/abi"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  var ErrUnknownEvent = errors.New("Unknown event type")
    12  
    13  var RequestValueTransferEncodeABIs = map[uint]string{
    14  	2: `[{
    15  			"anonymous":false,
    16  			"inputs": [{
    17  				"name": "uri",
    18  				"type": "string"
    19  			}],
    20  			"name": "packedURI",
    21  			"type": "event"
    22  		}]`,
    23  }
    24  
    25  func UnpackEncodedData(ver uint8, packed []byte) map[string]interface{} {
    26  	switch ver {
    27  	case 2:
    28  		encodedEvent := map[string]interface{}{}
    29  		abi, err := abi.JSON(strings.NewReader(RequestValueTransferEncodeABIs[2]))
    30  		if err != nil {
    31  			logger.Error("Failed to ABI setup", "err", err)
    32  			return nil
    33  		}
    34  		if err := abi.UnpackIntoMap(encodedEvent, "packedURI", packed); err != nil {
    35  			logger.Error("Failed to unpack the values", "err", err)
    36  			return nil
    37  		}
    38  		return encodedEvent
    39  	default:
    40  		logger.Error(ErrUnknownEvent.Error(), "encodingVer", ver)
    41  		return nil
    42  	}
    43  }
    44  
    45  func GetURI(ev IRequestValueTransferEvent) string {
    46  	switch evType := ev.(type) {
    47  	case RequestValueTransferEncodedEvent:
    48  		decoded := UnpackEncodedData(evType.EncodingVer, evType.EncodedData)
    49  		uri, ok := decoded["uri"].(string)
    50  		if !ok {
    51  			return ""
    52  		}
    53  		if len(uri) <= 64 {
    54  			return ""
    55  		}
    56  		return string(bytes.Trim([]byte(uri[64:]), "\x00"))
    57  	}
    58  	return ""
    59  }