Skip to content
Snippets Groups Projects
issuer.go 2.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • package credential
    
    import (
    	"net/http"
    	"time"
    
    
    	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite"
    	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/jsonwebsignature2020"
    
    	"github.com/hyperledger/aries-framework-go/pkg/doc/util"
    	"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
    
    	"github.com/hyperledger/aries-framework-go/pkg/vdr"
    	"github.com/hyperledger/aries-framework-go/pkg/vdr/web"
    
    	"github.com/piprate/json-gold/ld"
    )
    
    
    var defaultContexts = []string{
    	"https://www.w3.org/2018/credentials/v1",
    	"https://w3id.org/security/suites/jws-2020/v1",
    }
    
    
    type Issuer struct {
    
    	issuerURI  string
    	docLoader  *ld.CachingDocumentLoader
    	httpClient *http.Client
    
    func NewIssuer(issuerURI string, httpClient *http.Client) *Issuer {
    
    	loader := ld.NewDefaultDocumentLoader(httpClient)
    
    	return &Issuer{
    
    		issuerURI:  issuerURI,
    		docLoader:  ld.NewCachingDocumentLoader(loader),
    		httpClient: httpClient,
    
    	}
    }
    
    func (i *Issuer) NewCredential(contexts []string, subjectID string, subject map[string]interface{}, proof bool) (*verifiable.Credential, error) {
    
    	jsonldContexts := defaultContexts
    
    	jsonldContexts = append(jsonldContexts, contexts...)
    
    	vc := &verifiable.Credential{
    		Context: jsonldContexts,
    		Types:   []string{verifiable.VCType},
    
    		Issuer:  verifiable.Issuer{ID: i.issuerURI},
    
    		Issued:  &util.TimeWrapper{Time: time.Now()},
    		Subject: verifiable.Subject{
    			ID:           subjectID,
    			CustomFields: subject,
    		},
    	}
    
    	return vc, nil
    }
    
    func (i *Issuer) NewPresentation(contexts []string, vc ...*verifiable.Credential) (*verifiable.Presentation, error) {
    
    	jsonldContexts := defaultContexts
    
    	jsonldContexts = append(jsonldContexts, contexts...)
    
    	vp, err := verifiable.NewPresentation(verifiable.WithCredentials(vc...))
    	if err != nil {
    		return nil, err
    	}
    	vp.Context = jsonldContexts
    
    	vp.ID = i.issuerURI
    
    	vp.Type = []string{verifiable.VPType}
    
    	return vp, nil
    }
    
    
    func (i *Issuer) ParsePresentation(vpBytes []byte) (*verifiable.Presentation, error) {
    
    	webvdr := web.New()
    	registry := vdr.New(vdr.WithVDR(webvdr))
    	fetcher := verifiable.NewVDRKeyResolver(registry)
    
    
    	return verifiable.ParsePresentation(
    		vpBytes,
    
    		verifiable.WithPresPublicKeyFetcher(fetcher.PublicKeyFetcher()),
    
    		verifiable.WithPresEmbeddedSignatureSuites(
    			jsonwebsignature2020.New(suite.WithVerifier(jsonwebsignature2020.NewPublicKeyVerifier())),
    		),
    		verifiable.WithPresJSONLDDocumentLoader(i.docLoader),
    		verifiable.WithPresStrictValidation(),
    	)
    }