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

     1  package sign
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caarlos0/spin"
     7  	"github.com/fatih/color"
     8  	"github.com/schollz/progressbar/v3"
     9  	"github.com/vchain-us/vcn/pkg/api"
    10  	"github.com/vchain-us/vcn/pkg/cmd/internal/cli"
    11  	"github.com/vchain-us/vcn/pkg/cmd/internal/types"
    12  	"github.com/vchain-us/vcn/pkg/meta"
    13  )
    14  
    15  func LcSign(u *api.LcUser, artifacts []*api.Artifact, state meta.Status, output string, name string, metadata map[string]interface{}, attach []string, verbose bool, bomText string) error {
    16  	if output == "" {
    17  		color.Set(meta.StyleAffordance())
    18  		fmt.Print("Your assets will not be uploaded. They will be processed locally.")
    19  		color.Unset()
    20  		fmt.Println()
    21  		fmt.Println()
    22  	}
    23  
    24  	s := spin.New("%s Notarization in progress...")
    25  	s.Set(spin.Spin1)
    26  
    27  	var bar *progressbar.ProgressBar
    28  	lenArtifacts := len(artifacts)
    29  	if lenArtifacts > 1 && output == "" {
    30  		bar = progressbar.Default(int64(lenArtifacts))
    31  	}
    32  
    33  	var hook *hook
    34  	if len(artifacts) == 1 {
    35  		hook = newHook(artifacts[0])
    36  		// Override the asset's name, if provided by --name
    37  		if name != "" {
    38  			artifacts[0].Name = name
    39  		}
    40  	}
    41  
    42  	for _, a := range artifacts {
    43  		// Copy user provided custom attributes
    44  		a.Metadata.SetValues(metadata)
    45  
    46  		// @todo mmeloni use verified sign
    47  		verified, tx, err := u.Sign(
    48  			*a,
    49  			api.LcSignWithStatus(state),
    50  			api.LcSignWithAttachments(attach),
    51  			api.LcSignWithBom(bomText),
    52  		)
    53  		if err != nil {
    54  			if err == api.ErrNotVerified {
    55  				color.Set(meta.StyleError())
    56  				fmt.Println("the ledger is compromised. Please contact the Codenotary Cloud administrators")
    57  				color.Unset()
    58  				fmt.Println()
    59  				return nil
    60  			}
    61  			return err
    62  		}
    63  
    64  		// writingManifest
    65  		if hook != nil && len(artifacts) == 1 {
    66  			err = hook.finalizeWithoutVerification(false)
    67  			if err != nil {
    68  				return cli.PrintWarning(output, err.Error())
    69  			}
    70  		}
    71  
    72  		if err != nil {
    73  			return cli.PrintWarning(output, err.Error())
    74  		}
    75  		if output == "" && lenArtifacts == 0 {
    76  			fmt.Println()
    77  		}
    78  
    79  		artifact, verified, err := u.LoadArtifact(a.Hash, "", "", tx, nil)
    80  		if err != nil {
    81  			if err == api.ErrNotVerified {
    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  				return nil
    87  			}
    88  			return cli.PrintWarning(output, err.Error())
    89  		}
    90  
    91  		if bar != nil {
    92  			if err := bar.Add(1); err != nil {
    93  				return err
    94  			}
    95  		} else {
    96  			var verbInfos *types.LcVerboseInfo
    97  			if verbose {
    98  				verbInfos = &types.LcVerboseInfo{
    99  					LedgerName: artifact.Ledger,
   100  					LocalSID:   api.GetSignerIDByApiKey(u.Client.ApiKey),
   101  					ApiKey:     u.Client.ApiKey,
   102  				}
   103  			}
   104  			cli.PrintLc(output, types.NewLcResult(artifact, verified, verbInfos))
   105  		}
   106  	}
   107  	if lenArtifacts > 1 && output == "" {
   108  		color.Set(meta.StyleSuccess())
   109  		fmt.Printf("notarized %d items", lenArtifacts)
   110  		color.Unset()
   111  		fmt.Println()
   112  	}
   113  	return nil
   114  }