Skip to content

Commit

Permalink
feat(cli): add --date to credential sign command
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Feb 13, 2024
1 parent 9c0b4c8 commit 993bd68
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions client/credential/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (

var (
flagOverwrite = "overwrite"
flagDate = "date"
)
var (
valueDash = "-"
)

var valueDash = "-"

func SignCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -48,6 +48,7 @@ It will read a verifiable credential from a file (or stdin), sign it, and print
}
cmd.Flags().String(flags.FlagFrom, "", "Name or address of private key with which to sign")
cmd.Flags().Bool(flagOverwrite, false, "Overwrite existing signatures with a new one. If disabled, new signature will be appended")
cmd.Flags().String(flagDate, "", "Date of the signature provided in RFC3339 format. If not provided, current time will be used")
_ = cmd.MarkFlagRequired(flags.FlagFrom)

flags.AddKeyringFlags(cmd.Flags())
Expand Down Expand Up @@ -92,7 +93,11 @@ func runSignCmd(cmd *cobra.Command, args []string) error {
vc.Proofs = nil
}

err = signVerifiableCredential(vc, signer)
date, err := getFlagAsDate(cmd, flagDate)
if err != nil {
return err
}
err = signVerifiableCredential(vc, signer, date)
if err != nil {
return errorsmod.Wrapf(sdkerr.ErrInvalidRequest, "failed to sign: %v", err)
}
Expand Down Expand Up @@ -187,19 +192,29 @@ func loadVerifiableCredential(bs []byte) (*verifiable.Credential, error) {
verifiable.WithJSONLDDocumentLoader(documentLoader))
}

func signVerifiableCredential(vc *verifiable.Credential, signer KeyringSigner) error {
func signVerifiableCredential(vc *verifiable.Credential, signer KeyringSigner, date time.Time) error {
documentLoader := ld.NewDefaultDocumentLoader(nil)
didKeyID, err := signer.DIDKeyID()
if err != nil {
return err
}

now := time.Now()
return vc.AddLinkedDataProof(&verifiable.LinkedDataProofContext{
Created: &now,
Created: &date,
SignatureType: "Ed25519Signature2018",
Suite: ed25519signature2018.New(suite.WithSigner(signer)),
SignatureRepresentation: verifiable.SignatureProofValue,
VerificationMethod: didKeyID,
}, jsonld.WithDocumentLoader(documentLoader))
}

func getFlagAsDate(cmd *cobra.Command, flag string) (time.Time, error) {
dateStr, err := cmd.Flags().GetString(flag)
if err != nil {
return time.Time{}, fmt.Errorf("%s: %w", flag, err)
}
if dateStr == "" {
return time.Now(), nil
}
return time.Parse(time.RFC3339, dateStr)
}

0 comments on commit 993bd68

Please sign in to comment.