From 227045bc35ef8e1f3f55d0b5c5b115b01d34e831 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Tue, 3 Sep 2024 11:44:51 +0200 Subject: [PATCH] docs(credential): add documention comment --- credential/generate.go | 35 ++++++++++++++++++++++++++--------- credential/generate_test.go | 10 +++++----- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/credential/generate.go b/credential/generate.go index fc28f1f..6522615 100644 --- a/credential/generate.go +++ b/credential/generate.go @@ -18,6 +18,7 @@ import ( //go:embed template/vc-gov-tpl.jsonld var governanceTemplate string +// Generator is a verifiable credential generator. type Generator struct { vc Descriptor signer verifiable.Signer @@ -25,7 +26,18 @@ type Generator struct { parser *credentialParser } -func NewGenerator(descriptor Descriptor) *Generator { +// New allow to generate a verifiable credential with the given credential descriptor. +// Example: +// +// vc, err := credential.New( +// NewGovernanceVC(). +// WithDatasetDID("did:key:..."). +// WithGovAddr("axone1234..."), +// ). +// WithParser(parser). +// WithSignature(signer, "did:key:..."). // Signature is optional and generate a not signed VC if not provided. +// Generate() +func New(descriptor Descriptor) *Generator { return &Generator{ vc: descriptor, } @@ -73,6 +85,7 @@ func (generator *Generator) Generate() (*verifiable.Credential, error) { return cred, nil } +// Descriptor is an interface representing the description of a verifiable credential. type Descriptor interface { issuedAt() *time.Time generate() (*bytes.Buffer, error) @@ -81,6 +94,7 @@ type Descriptor interface { var _ Descriptor = NewGovernanceVC() +// GovernanceVCDescriptor is a descriptor for a governance verifiable credential. type GovernanceVCDescriptor struct { id string datasetDID string @@ -88,14 +102,9 @@ type GovernanceVCDescriptor struct { issuanceDate *time.Time } -func (g *GovernanceVCDescriptor) issuedAt() *time.Time { - return g.issuanceDate -} - -func (g *GovernanceVCDescriptor) proofPurpose() string { - return "authentication" -} - +// NewGovernanceVC creates a new governance verifiable credential descriptor. +// DatasetDID and GovAddr are required. If ID is not provided, it will be generated. +// If issuance date is not provided, it will be set to the current time at the generation. func NewGovernanceVC() *GovernanceVCDescriptor { return &GovernanceVCDescriptor{} } @@ -137,6 +146,14 @@ func (g *GovernanceVCDescriptor) prepare() error { return nil } +func (g *GovernanceVCDescriptor) issuedAt() *time.Time { + return g.issuanceDate +} + +func (g *GovernanceVCDescriptor) proofPurpose() string { + return "authentication" +} + func (g *GovernanceVCDescriptor) generate() (*bytes.Buffer, error) { err := g.prepare() if err != nil { diff --git a/credential/generate_test.go b/credential/generate_test.go index e25e77c..4923d4a 100644 --- a/credential/generate_test.go +++ b/credential/generate_test.go @@ -87,7 +87,7 @@ func TestGenerateGovernanceVC(t *testing.T) { So(err, ShouldBeNil) parser := credential.NewCredentialParser(docLoader) - generator := credential.NewGenerator(test.vc). + generator := credential.New(test.vc). WithParser(parser) Convey("When a governance VC is generated", func() { @@ -135,17 +135,17 @@ func TestGenerator_Generate(t *testing.T) { }{ { name: "without parser", - generator: credential.NewGenerator(credential.NewGovernanceVC().WithDatasetDID("datasetID").WithGovAddr("addr")), + generator: credential.New(credential.NewGovernanceVC().WithDatasetDID("datasetID").WithGovAddr("addr")), wantErr: errors.New("no parser provided"), }, { name: "with descriptor error", - generator: credential.NewGenerator(credential.NewGovernanceVC().WithDatasetDID("datasetID")), + generator: credential.New(credential.NewGovernanceVC().WithDatasetDID("datasetID")), wantErr: credential.NewVCError(credential.ErrGenerate, errors.New("governance address is required")), }, { name: "without signature", - generator: credential.NewGenerator(credential.NewGovernanceVC().WithDatasetDID("datasetID").WithGovAddr("addr")). + generator: credential.New(credential.NewGovernanceVC().WithDatasetDID("datasetID").WithGovAddr("addr")). WithParser(credential.NewCredentialParser(loader)), wantErr: nil, check: func(vc *verifiable.Credential) { @@ -154,7 +154,7 @@ func TestGenerator_Generate(t *testing.T) { }, { name: "with signature", - generator: credential.NewGenerator(credential.NewGovernanceVC().WithDatasetDID("datasetID").WithGovAddr("addr")). + generator: credential.New(credential.NewGovernanceVC().WithDatasetDID("datasetID").WithGovAddr("addr")). WithParser(credential.NewCredentialParser(loader)). WithSignature(mockSigner, "did:example:123"), wantErr: nil,