code.vegaprotocol.io/vega@v0.79.0/core/bridges/erc20_multisigcontrol.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package bridges
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/libs/num"
    20  
    21  	"github.com/ethereum/go-ethereum/accounts/abi"
    22  	ethcmn "github.com/ethereum/go-ethereum/common"
    23  )
    24  
    25  type ERC20MultiSigControl struct {
    26  	signer  Signer
    27  	chainID string
    28  	v1      bool
    29  }
    30  
    31  func NewERC20MultiSigControl(signer Signer, chainID string, v1 bool) *ERC20MultiSigControl {
    32  	return &ERC20MultiSigControl{
    33  		signer:  signer,
    34  		chainID: chainID,
    35  		v1:      v1,
    36  	}
    37  }
    38  
    39  func (e *ERC20MultiSigControl) BurnNonce(
    40  	submitter string,
    41  	nonce *num.Uint,
    42  ) (*SignaturePayload, error) {
    43  	typString, err := abi.NewType("string", "", nil)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	typU256, err := abi.NewType("uint256", "", nil)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	args := abi.Arguments([]abi.Argument{
    53  		{
    54  			Name: "nonce",
    55  			Type: typU256,
    56  		},
    57  		{
    58  			Name: "func_name",
    59  			Type: typString,
    60  		},
    61  	})
    62  
    63  	buf, err := args.Pack([]interface{}{nonce.BigInt(), "burnNonce"}...)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	msg, err := packScheme(buf, submitter, e.chainID, e.v1)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return sign(e.signer, msg)
    74  }
    75  
    76  func (e *ERC20MultiSigControl) SetThreshold(
    77  	newThreshold uint16,
    78  	submitter string,
    79  	nonce *num.Uint,
    80  ) (*SignaturePayload, error) {
    81  	typString, err := abi.NewType("string", "", nil)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	typU16, err := abi.NewType("uint16", "", nil)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	typU256, err := abi.NewType("uint256", "", nil)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	args := abi.Arguments([]abi.Argument{
    95  		{
    96  			Name: "newThreshold",
    97  			Type: typU16,
    98  		},
    99  		{
   100  			Name: "nonce",
   101  			Type: typU256,
   102  		},
   103  		{
   104  			Name: "func_name",
   105  			Type: typString,
   106  		},
   107  	})
   108  
   109  	buf, err := args.Pack([]interface{}{newThreshold, nonce.BigInt(), "setThreshold"}...)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	msg, err := packScheme(buf, submitter, e.chainID, e.v1)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return sign(e.signer, msg)
   119  }
   120  
   121  func (e *ERC20MultiSigControl) AddSigner(
   122  	newSigner, submitter string,
   123  	nonce *num.Uint,
   124  ) (*SignaturePayload, error) {
   125  	typAddr, err := abi.NewType("address", "", nil)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  	typString, err := abi.NewType("string", "", nil)
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  	typU256, err := abi.NewType("uint256", "", nil)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	args := abi.Arguments([]abi.Argument{
   139  		{
   140  			Name: "new_signer",
   141  			Type: typAddr,
   142  		},
   143  		{
   144  			Name: "nonce",
   145  			Type: typU256,
   146  		},
   147  		{
   148  			Name: "func_name",
   149  			Type: typString,
   150  		},
   151  	})
   152  
   153  	newSignerAddr := ethcmn.HexToAddress(newSigner)
   154  	buf, err := args.Pack([]interface{}{newSignerAddr, nonce.BigInt(), "addSigner"}...)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	msg, err := packScheme(buf, submitter, e.chainID, e.v1)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	return sign(e.signer, msg)
   165  }
   166  
   167  func (e *ERC20MultiSigControl) RemoveSigner(
   168  	oldSigner, submitter string,
   169  	nonce *num.Uint,
   170  ) (*SignaturePayload, error) {
   171  	typAddr, err := abi.NewType("address", "", nil)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	typString, err := abi.NewType("string", "", nil)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  	typU256, err := abi.NewType("uint256", "", nil)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	args := abi.Arguments([]abi.Argument{
   185  		{
   186  			Name: "old_signer",
   187  			Type: typAddr,
   188  		},
   189  		{
   190  			Name: "nonce",
   191  			Type: typU256,
   192  		},
   193  		{
   194  			Name: "func_name",
   195  			Type: typString,
   196  		},
   197  	})
   198  
   199  	oldSignerAddr := ethcmn.HexToAddress(oldSigner)
   200  	buf, err := args.Pack([]interface{}{oldSignerAddr, nonce.BigInt(), "removeSigner"}...)
   201  	if err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	msg, err := packScheme(buf, submitter, e.chainID, e.v1)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  
   210  	return sign(e.signer, msg)
   211  }