github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/verify/lc_verify.go (about)

     1  package verify
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/fatih/color"
     8  	"github.com/schollz/progressbar/v3"
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/viper"
    11  	"github.com/vchain-us/vcn/pkg/api"
    12  	"github.com/vchain-us/vcn/pkg/cmd/internal/cli"
    13  	"github.com/vchain-us/vcn/pkg/cmd/internal/types"
    14  	"github.com/vchain-us/vcn/pkg/meta"
    15  )
    16  
    17  func lcVerify(cmd *cobra.Command, a *api.Artifact, user *api.LcUser, signerID string, uid string, attach string, lcAttachForce bool, verbose bool, output string) (err error) {
    18  	hook := newHook(cmd, a)
    19  	err = hook.lcFinalizeWithoutAlert(user, output, 0)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	var attachmentList []api.Attachment
    24  
    25  	if attach != "" {
    26  		attachmentList, uid, err = user.GetArtifactAttachmentListByLabel(a.Hash, signerID, attach)
    27  		if err != nil {
    28  			return err
    29  		}
    30  	}
    31  
    32  	ar, verified, err := user.LoadArtifact(
    33  		a.Hash,
    34  		signerID,
    35  		uid,
    36  		0,
    37  		map[string][]string{meta.VcnLCCmdHeaderName: {meta.VcnLCVerifyCmdHeaderValue}})
    38  	if err != nil {
    39  		if err == api.ErrNotFound {
    40  			err = fmt.Errorf("%s was not notarized", a.Hash)
    41  			viper.Set("exit-code", strconv.Itoa(meta.StatusUnknown.Int()))
    42  		}
    43  		if err == api.ErrNotVerified {
    44  			color.Set(meta.StyleError())
    45  			fmt.Println("the ledger is compromised. Please contact the Codenotary Cloud administrators")
    46  			color.Unset()
    47  			fmt.Println()
    48  			viper.Set("exit-code", strconv.Itoa(meta.StatusUnknown.Int()))
    49  		}
    50  		return cli.PrintWarning(output, err.Error())
    51  	}
    52  	if ar.Revoked != nil && !ar.Revoked.IsZero() {
    53  		viper.Set("exit-code", strconv.Itoa(meta.StatusApikeyRevoked.Int()))
    54  		ar.Status = meta.StatusApikeyRevoked
    55  	}
    56  
    57  	if len(attachmentList) == 0 && ar.Attachments != nil {
    58  		attachmentList = ar.Attachments
    59  	}
    60  	ar.IncludedIn = a.IncludedIn
    61  	ar.Deps = a.Deps
    62  
    63  	if output == "attachments" {
    64  		cli.PrintAttachmentList(attach, attachmentList)
    65  
    66  		var bar *progressbar.ProgressBar
    67  		lenAttachments := len(attachmentList)
    68  		if lenAttachments >= 1 {
    69  			bar = progressbar.Default(int64(lenAttachments))
    70  		}
    71  
    72  		for _, a := range attachmentList {
    73  			_ = bar.Add(1)
    74  			err := user.DownloadAttachment(&a, ar, 0, lcAttachForce)
    75  			if err != nil {
    76  				return err
    77  			}
    78  		}
    79  		fmt.Println()
    80  	}
    81  	if !verified {
    82  		color.Set(meta.StyleError())
    83  		fmt.Println("the ledger is compromised. Please contact the Codenotary Cloud administrators")
    84  		color.Unset()
    85  		fmt.Println()
    86  		viper.Set("exit-code", strconv.Itoa(meta.StatusUnknown.Int()))
    87  		ar.Status = meta.StatusUnknown
    88  	}
    89  
    90  	exitCode, err := cmd.Flags().GetInt("exit-code")
    91  	if err != nil {
    92  		return err
    93  	}
    94  	// if exitCode == VcnDefaultExitCode user didn't specify to use a custom exit code in case of success.
    95  	// In that case we return the ar.Status as exit code.
    96  	// User defined exit code is returned only if the viper exit-code status is == 0 (status trusted)
    97  	if exitCode == meta.VcnDefaultExitCode && viper.GetInt("exit-code") == 0 {
    98  		viper.Set("exit-code", strconv.Itoa(ar.Status.Int()))
    99  	}
   100  	var verbInfos *types.LcVerboseInfo
   101  	if verbose {
   102  		verbInfos = &types.LcVerboseInfo{
   103  			LedgerName: ar.Ledger,
   104  			LocalSID:   api.GetSignerIDByApiKey(user.Client.ApiKey),
   105  			ApiKey:     user.Client.ApiKey,
   106  		}
   107  	}
   108  	cli.PrintLc(output, types.NewLcResult(ar, verified, verbInfos))
   109  
   110  	return
   111  }