github.com/core-coin/go-core/v2@v2.1.9/signer/core/gnosis_safe.go (about)

     1  // Copyright 2020 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 core
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  
    23  	"github.com/core-coin/go-core/v2/common"
    24  	"github.com/core-coin/go-core/v2/common/hexutil"
    25  	"github.com/core-coin/go-core/v2/common/math"
    26  )
    27  
    28  // GnosisSafeTx is a type to parse the safe-tx returned by the relayer,
    29  // it also conforms to the API required by the Gnosis Safe tx relay service.
    30  // See 'SafeMultisigTransaction' on https://safe-transaction.mainnet.gnosis.io/
    31  type GnosisSafeTx struct {
    32  	// These fields are only used on output
    33  	Signature  hexutil.Bytes  `json:"signature"`
    34  	SafeTxHash common.Hash    `json:"contractTransactionHash"`
    35  	Sender     common.Address `json:"sender"`
    36  	// These fields are used both on input and output
    37  	Safe           common.Address  `json:"safe"`
    38  	To             common.Address  `json:"to"`
    39  	Value          math.Decimal256 `json:"value"`
    40  	EnergyPrice    math.Decimal256 `json:"energyPrice"`
    41  	Data           *hexutil.Bytes  `json:"data"`
    42  	Operation      uint8           `json:"operation"`
    43  	EnergyToken    common.Address  `json:"energyToken"`
    44  	RefundReceiver common.Address  `json:"refundReceiver"`
    45  	BaseEnergy     big.Int         `json:"baseEnergy"`
    46  	SafeTxEnergy   big.Int         `json:"safeTxEnergy"`
    47  	Nonce          big.Int         `json:"nonce"`
    48  	InputExpHash   common.Hash     `json:"safeTxHash"`
    49  }
    50  
    51  // ToTypedData converts the tx to a CIP-712 Typed Data structure for signing
    52  func (tx *GnosisSafeTx) ToTypedData() TypedData {
    53  	var data hexutil.Bytes
    54  	if tx.Data != nil {
    55  		data = *tx.Data
    56  	}
    57  	gnosisTypedData := TypedData{
    58  		Types: Types{
    59  			"CIP712Domain": []Type{{Name: "verifyingContract", Type: "address"}},
    60  			"SafeTx": []Type{
    61  				{Name: "to", Type: "address"},
    62  				{Name: "value", Type: "uint256"},
    63  				{Name: "data", Type: "bytes"},
    64  				{Name: "operation", Type: "uint8"},
    65  				{Name: "safeTxEnergy", Type: "uint256"},
    66  				{Name: "baseEnergy", Type: "uint256"},
    67  				{Name: "energyPrice", Type: "uint256"},
    68  				{Name: "energyToken", Type: "address"},
    69  				{Name: "refundReceiver", Type: "address"},
    70  				{Name: "nonce", Type: "uint256"},
    71  			},
    72  		},
    73  		Domain: TypedDataDomain{
    74  			VerifyingContract: tx.Safe.Hex(),
    75  		},
    76  		PrimaryType: "SafeTx",
    77  		Message: TypedDataMessage{
    78  			"to":             tx.To.Hex(),
    79  			"value":          tx.Value.String(),
    80  			"data":           data,
    81  			"operation":      fmt.Sprintf("%d", tx.Operation),
    82  			"safeTxEnergy":   fmt.Sprintf("%#d", &tx.SafeTxEnergy),
    83  			"baseEnergy":     fmt.Sprintf("%#d", &tx.BaseEnergy),
    84  			"energyPrice":    tx.EnergyPrice.String(),
    85  			"energyToken":    tx.EnergyToken.Hex(),
    86  			"refundReceiver": tx.RefundReceiver.Hex(),
    87  			"nonce":          fmt.Sprintf("%d", tx.Nonce.Uint64()),
    88  		},
    89  	}
    90  	return gnosisTypedData
    91  }
    92  
    93  // ArgsForValidation returns a SendTxArgs struct, which can be used for the
    94  // common validations, e.g. look up 4byte destinations
    95  func (tx *GnosisSafeTx) ArgsForValidation() *SendTxArgs {
    96  	args := &SendTxArgs{
    97  		From:        tx.Safe,
    98  		To:          &tx.To,
    99  		Energy:      hexutil.Uint64(tx.SafeTxEnergy.Uint64()),
   100  		EnergyPrice: hexutil.Big(tx.EnergyPrice),
   101  		Value:       hexutil.Big(tx.Value),
   102  		Nonce:       hexutil.Uint64(tx.Nonce.Uint64()),
   103  		Data:        tx.Data,
   104  		Input:       nil,
   105  	}
   106  	return args
   107  }