github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/ethereum_transaction_data.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"encoding/json"
    25  
    26  	"github.com/pkg/errors"
    27  
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  	"github.com/ethereum/go-ethereum/rlp"
    30  )
    31  
    32  // Represents the data of an Ethereum transaction.
    33  type EthereumTransactionData struct {
    34  	eip1559 *types.DynamicFeeTx
    35  	legacy  *types.LegacyTx
    36  }
    37  
    38  // EthereumTransactionDataFromBytes constructs an EthereumTransactionData from a raw byte array.
    39  func EthereumTransactionDataFromBytes(b []byte) (*EthereumTransactionData, error) {
    40  	var transactionData EthereumTransactionData
    41  	if b[0] == 2 {
    42  		byt := b
    43  		byt = append(byt[:0], byt[0+1:]...)
    44  		err := rlp.DecodeBytes(byt, &transactionData.eip1559)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  
    49  		return &transactionData, nil
    50  	}
    51  
    52  	err := rlp.DecodeBytes(b, &transactionData.legacy)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return &transactionData, nil
    58  }
    59  
    60  // ToBytes returns the raw bytes of the Ethereum transaction.
    61  func (ethereumTxData *EthereumTransactionData) ToBytes() ([]byte, error) {
    62  	var byt []byte
    63  	var err error
    64  	if ethereumTxData.eip1559 != nil {
    65  		byt, err = rlp.EncodeToBytes(ethereumTxData.eip1559)
    66  		if err != nil {
    67  			return []byte{}, err
    68  		}
    69  		byt = append([]byte{2}, byt...)
    70  
    71  		return byt, nil
    72  	}
    73  
    74  	byt, err = rlp.EncodeToBytes(ethereumTxData.legacy)
    75  	if err != nil {
    76  		return []byte{}, err
    77  	}
    78  
    79  	return byt, nil
    80  }
    81  
    82  func (ethereumTxData *EthereumTransactionData) _GetData() []byte {
    83  	if ethereumTxData.eip1559 != nil {
    84  		return ethereumTxData.eip1559.Data
    85  	}
    86  
    87  	return ethereumTxData.legacy.Data
    88  }
    89  
    90  func (ethereumTxData *EthereumTransactionData) _SetData(data []byte) *EthereumTransactionData {
    91  	if ethereumTxData.eip1559 != nil {
    92  		ethereumTxData.eip1559.Data = data
    93  		return ethereumTxData
    94  	}
    95  
    96  	ethereumTxData.legacy.Data = data
    97  	return ethereumTxData
    98  }
    99  
   100  // ToJson returns a JSON representation of the Ethereum transaction.
   101  func (ethereumTxData *EthereumTransactionData) ToJson() ([]byte, error) {
   102  	var byt []byte
   103  	var err error
   104  	if ethereumTxData.eip1559 != nil {
   105  		byt, err = json.Marshal(ethereumTxData.eip1559)
   106  		if err != nil {
   107  			return []byte{}, err
   108  		}
   109  
   110  		return byt, nil
   111  	}
   112  
   113  	byt, err = json.Marshal(ethereumTxData.legacy)
   114  	if err != nil {
   115  		return []byte{}, err
   116  	}
   117  
   118  	return byt, nil
   119  }
   120  
   121  // EthereumTransactionDataFromJson constructs an EthereumTransactionData from a JSON string.
   122  func EthereumTransactionDataFromJson(b []byte) (*EthereumTransactionData, error) {
   123  	var eip1559 types.DynamicFeeTx
   124  	var leg types.LegacyTx
   125  	err := json.Unmarshal(b, &eip1559)
   126  	if err != nil {
   127  		err = json.Unmarshal(b, &leg)
   128  		if err != nil {
   129  			return nil, errors.New("Json bytes are neither eip1559 or legacy format")
   130  		}
   131  
   132  		return &EthereumTransactionData{
   133  			legacy: &leg,
   134  		}, nil
   135  	}
   136  
   137  	return &EthereumTransactionData{
   138  		eip1559: &eip1559,
   139  	}, nil
   140  }