github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/signer/core/gnosis_safe.go (about)

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