code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/verify/verifier.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 verify
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/base64"
    21  	"encoding/hex"
    22  	"errors"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"os"
    26  	"regexp"
    27  
    28  	"github.com/golang/protobuf/jsonpb"
    29  	"github.com/golang/protobuf/proto"
    30  )
    31  
    32  func verifier(params []string, f func(*reporter, []byte) string) error {
    33  	if len(params) <= 0 {
    34  		return errors.New("error: at least one file is required")
    35  	}
    36  	rprter := &reporter{}
    37  	for i, v := range params {
    38  		rprter.Start(v)
    39  		bs := readFile(rprter, v)
    40  		if rprter.HasCurrError() {
    41  			rprter.Dump("")
    42  			continue
    43  		}
    44  
    45  		result := f(rprter, bs)
    46  		rprter.Dump(result)
    47  		if i < len(params)-1 {
    48  			fmt.Println()
    49  		}
    50  	}
    51  	if rprter.HasError() {
    52  		return errors.New("error: one or more files are malformed or invalid")
    53  	}
    54  	return nil
    55  }
    56  
    57  func unmarshal(r *reporter, bs []byte, i proto.Message) bool {
    58  	u := jsonpb.Unmarshaler{
    59  		AllowUnknownFields: false,
    60  	}
    61  
    62  	err := u.Unmarshal(bytes.NewBuffer(bs), i)
    63  	if err != nil {
    64  		r.Err("unable to unmarshal file: %v", err)
    65  		return false
    66  	}
    67  
    68  	return true
    69  }
    70  
    71  func marshal(i proto.Message) string {
    72  	m := jsonpb.Marshaler{
    73  		Indent:       "  ",
    74  		EmitDefaults: true,
    75  	}
    76  	buf, _ := m.MarshalToString(i)
    77  	return buf
    78  }
    79  
    80  func readFile(r *reporter, path string) []byte {
    81  	f, err := os.Open(path)
    82  	if err != nil {
    83  		r.Err("%v, no such file or directory", path)
    84  		return nil
    85  	}
    86  	defer f.Close()
    87  
    88  	bytes, err := ioutil.ReadAll(f)
    89  	if err != nil {
    90  		r.Err("unable to read file: %v", err)
    91  		return nil
    92  	}
    93  
    94  	return bytes
    95  }
    96  
    97  func isValidParty(party string) bool {
    98  	if len(party) != 64 {
    99  		return false
   100  	}
   101  
   102  	_, err := hex.DecodeString(party)
   103  	return err == nil
   104  }
   105  
   106  func isValidCometBFTKey(key string) bool {
   107  	keybytes, err := base64.StdEncoding.DecodeString(key)
   108  	if err != nil {
   109  		return false
   110  	}
   111  	return len(keybytes) == 32
   112  }
   113  
   114  func isValidEthereumAddress(v string) bool {
   115  	re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$")
   116  	return re.MatchString(v)
   117  }