Skip to content

Commit

Permalink
Add basic vc create command
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe committed Mar 19, 2024
1 parent 7abe57a commit a727947
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/web5/cmd_did_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type didCreateCMD struct {
JWK didCreateJWKCMD `cmd:"" help:"Create did:jwk's."`
Web didCreateWebCMD `cmd:"" help:"Create did:web's."`
JWK didCreateJWKCMD `cmd:"" help:"Create a did:jwk."`
Web didCreateWebCMD `cmd:"" help:"Create a did:web."`
}

type didCreateJWKCMD struct{}
Expand Down
57 changes: 57 additions & 0 deletions cmd/web5/cmd_vc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"encoding/json"
"fmt"
"time"

"github.com/tbd54566975/web5-go/vc"
)

type vcCreateCMD struct {
CredentialSubjectID string `arg:"" help:"The Credential Subject's ID"`
Claims string `help:"Add additional credentialSubject claims (JSON string). ex: '{\"name\": \"John Doe\"}'." default:"{}"`
Contexts []string `help:"Add additional @context's to the default [\"https://www.w3.org/2018/credentials/v1\"]."`
Types []string `help:"Add additional type's to the default [\"VerifiableCredential\"]."`
ID string `help:"Override the default ID of format urn:vc:uuid:<uuid>."`
IssuanceDate time.Time `help:"Override the default issuanceDate of time.Now()."`
ExpirationDate time.Time `help:"Override the default expirationDate of nil."`
}

func (c *vcCreateCMD) Run() error {
opts := []vc.CreateOption{}
if len(c.Contexts) > 0 {
opts = append(opts, vc.Contexts(c.Contexts...))
}
if len(c.Types) > 0 {
opts = append(opts, vc.Types(c.Types...))
}
if c.ID != "" {
opts = append(opts, vc.ID(c.ID))
}
if (c.IssuanceDate != time.Time{}) {
opts = append(opts, vc.IssuanceDate(c.IssuanceDate))
}
if (c.ExpirationDate != time.Time{}) {
opts = append(opts, vc.ExpirationDate(c.ExpirationDate))
}

var claims vc.Claims
err := json.Unmarshal([]byte(c.Claims), &claims)
if err != nil {
return fmt.Errorf("%s: %w", "invalid claims", err)
}

claims["id"] = c.CredentialSubjectID

credential := vc.Create(claims, opts...)

jsonDID, err := json.MarshalIndent(credential, "", " ")
if err != nil {
return err
}

fmt.Println(string(jsonDID))

return nil
}
3 changes: 3 additions & 0 deletions cmd/web5/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type CLI struct {
Resolve didResolveCMD `cmd:"" help:"Resolve a DID."`
Create didCreateCMD `cmd:"" help:"Create a DID."`
} `cmd:"" help:"Interface with DID's."`
VC struct {
Create vcCreateCMD `cmd:"" help:"Create a VC."`
} `cmd:"" help:"Interface with VC's."`
}

func main() {
Expand Down

0 comments on commit a727947

Please sign in to comment.