Skip to content
Snippets Groups Projects
issuer.go 2.69 KiB
Newer Older
  • Learn to ignore specific revisions
  • package credential
    
    import (
    	"net/http"
    	"time"
    
    	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
    	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite"
    	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2018"
    	"github.com/hyperledger/aries-framework-go/pkg/doc/util"
    	"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
    	"github.com/piprate/json-gold/ld"
    )
    
    type Signer interface {
    	Sign(data []byte) ([]byte, error)
    }
    
    type Issuer struct {
    	issuerName string
    	signer     Signer
    	keyname    string
    
    	// proofContext is used to generate linked data proof
    	proofContext *verifiable.LinkedDataProofContext
    	docLoader    *ld.CachingDocumentLoader
    }
    
    func NewIssuer(issuerName string, keyname string, signer Signer, httpClient *http.Client) *Issuer {
    	sigSuite := ed25519signature2018.New(
    		suite.WithSigner(signer),
    		suite.WithVerifier(ed25519signature2018.NewPublicKeyVerifier()))
    
    	proofContext := &verifiable.LinkedDataProofContext{
    		Suite:                   sigSuite,
    		SignatureType:           ed25519signature2018.SignatureType,
    		SignatureRepresentation: verifiable.SignatureProofValue,
    		VerificationMethod:      keyname,
    	}
    
    	loader := ld.NewDefaultDocumentLoader(httpClient)
    
    	return &Issuer{
    		issuerName:   issuerName,
    		signer:       signer,
    		keyname:      keyname,
    		docLoader:    ld.NewCachingDocumentLoader(loader),
    		proofContext: proofContext,
    	}
    }
    
    func (i *Issuer) NewCredential(contexts []string, subjectID string, subject map[string]interface{}, proof bool) (*verifiable.Credential, error) {
    	jsonldContexts := []string{"https://www.w3.org/2018/credentials/v1"}
    	jsonldContexts = append(jsonldContexts, contexts...)
    
    	vc := &verifiable.Credential{
    		Context: jsonldContexts,
    		Types:   []string{verifiable.VCType},
    		Issuer:  verifiable.Issuer{ID: i.issuerName},
    		Issued:  &util.TimeWrapper{Time: time.Now()},
    		Subject: verifiable.Subject{
    			ID:           subjectID,
    			CustomFields: subject,
    		},
    	}
    
    	if proof {
    		if err := vc.AddLinkedDataProof(i.proofContext, jsonld.WithDocumentLoader(i.docLoader)); err != nil {
    			return nil, err
    		}
    	}
    
    	return vc, nil
    }
    
    func (i *Issuer) NewPresentation(contexts []string, vc ...*verifiable.Credential) (*verifiable.Presentation, error) {
    	jsonldContexts := []string{"https://www.w3.org/2018/credentials/v1"}
    	jsonldContexts = append(jsonldContexts, contexts...)
    
    	vp, err := verifiable.NewPresentation(verifiable.WithCredentials(vc...))
    	if err != nil {
    		return nil, err
    	}
    	vp.Context = jsonldContexts
    	vp.ID = i.issuerName
    	vp.Type = []string{verifiable.VPType}
    
    	if err := vp.AddLinkedDataProof(i.proofContext, jsonld.WithDocumentLoader(i.docLoader)); err != nil {
    		return nil, err
    	}
    
    	return vp, nil
    }