github.com/cryptogateway/go-paymex@v0.0.0-20210204174735-96277fb1e602/signer/core/gnosis_safe.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  
     7  	"github.com/cryptogateway/go-paymex/common"
     8  	"github.com/cryptogateway/go-paymex/common/hexutil"
     9  	"github.com/cryptogateway/go-paymex/common/math"
    10  )
    11  
    12  // GnosisSafeTx is a type to parse the safe-tx returned by the relayer,
    13  // it also conforms to the API required by the Gnosis Safe tx relay service.
    14  // See 'SafeMultisigTransaction' on https://safe-transaction.mainnet.gnosis.io/
    15  type GnosisSafeTx struct {
    16  	// These fields are only used on output
    17  	Signature  hexutil.Bytes           `json:"signature"`
    18  	SafeTxHash common.Hash             `json:"contractTransactionHash"`
    19  	Sender     common.MixedcaseAddress `json:"sender"`
    20  	// These fields are used both on input and output
    21  	Safe           common.MixedcaseAddress `json:"safe"`
    22  	To             common.MixedcaseAddress `json:"to"`
    23  	Value          math.Decimal256         `json:"value"`
    24  	GasPrice       math.Decimal256         `json:"gasPrice"`
    25  	Data           *hexutil.Bytes          `json:"data"`
    26  	Operation      uint8                   `json:"operation"`
    27  	GasToken       common.Address          `json:"gasToken"`
    28  	RefundReceiver common.Address          `json:"refundReceiver"`
    29  	BaseGas        big.Int                 `json:"baseGas"`
    30  	SafeTxGas      big.Int                 `json:"safeTxGas"`
    31  	Nonce          big.Int                 `json:"nonce"`
    32  	InputExpHash   common.Hash             `json:"safeTxHash"`
    33  }
    34  
    35  // ToTypedData converts the tx to a EIP-712 Typed Data structure for signing
    36  func (tx *GnosisSafeTx) ToTypedData() TypedData {
    37  	var data hexutil.Bytes
    38  	if tx.Data != nil {
    39  		data = *tx.Data
    40  	}
    41  	gnosisTypedData := TypedData{
    42  		Types: Types{
    43  			"EIP712Domain": []Type{{Name: "verifyingContract", Type: "address"}},
    44  			"SafeTx": []Type{
    45  				{Name: "to", Type: "address"},
    46  				{Name: "value", Type: "uint256"},
    47  				{Name: "data", Type: "bytes"},
    48  				{Name: "operation", Type: "uint8"},
    49  				{Name: "safeTxGas", Type: "uint256"},
    50  				{Name: "baseGas", Type: "uint256"},
    51  				{Name: "gasPrice", Type: "uint256"},
    52  				{Name: "gasToken", Type: "address"},
    53  				{Name: "refundReceiver", Type: "address"},
    54  				{Name: "nonce", Type: "uint256"},
    55  			},
    56  		},
    57  		Domain: TypedDataDomain{
    58  			VerifyingContract: tx.Safe.Address().Hex(),
    59  		},
    60  		PrimaryType: "SafeTx",
    61  		Message: TypedDataMessage{
    62  			"to":             tx.To.Address().Hex(),
    63  			"value":          tx.Value.String(),
    64  			"data":           data,
    65  			"operation":      fmt.Sprintf("%d", tx.Operation),
    66  			"safeTxGas":      fmt.Sprintf("%#d", &tx.SafeTxGas),
    67  			"baseGas":        fmt.Sprintf("%#d", &tx.BaseGas),
    68  			"gasPrice":       tx.GasPrice.String(),
    69  			"gasToken":       tx.GasToken.Hex(),
    70  			"refundReceiver": tx.RefundReceiver.Hex(),
    71  			"nonce":          fmt.Sprintf("%d", tx.Nonce.Uint64()),
    72  		},
    73  	}
    74  	return gnosisTypedData
    75  }
    76  
    77  // ArgsForValidation returns a SendTxArgs struct, which can be used for the
    78  // common validations, e.g. look up 4byte destinations
    79  func (tx *GnosisSafeTx) ArgsForValidation() *SendTxArgs {
    80  	args := &SendTxArgs{
    81  		From:     tx.Safe,
    82  		To:       &tx.To,
    83  		Gas:      hexutil.Uint64(tx.SafeTxGas.Uint64()),
    84  		GasPrice: hexutil.Big(tx.GasPrice),
    85  		Value:    hexutil.Big(tx.Value),
    86  		Nonce:    hexutil.Uint64(tx.Nonce.Uint64()),
    87  		Data:     tx.Data,
    88  		Input:    nil,
    89  	}
    90  	return args
    91  }