generated from axone-protocol/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from axone-protocol/feat/create-vc
🔐 Create credentials
- Loading branch information
Showing
26 changed files
with
1,257 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package credential | ||
|
||
import ( | ||
"bytes" | ||
"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/ecdsasecp256k1signature2019" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" | ||
) | ||
|
||
// Generator is a verifiable credential generator. | ||
type Generator struct { | ||
vc Descriptor | ||
signer verifiable.Signer | ||
signerDID string | ||
parser *DefaultParser | ||
} | ||
|
||
// New allow to Generate a verifiable credential with the given credential descriptor. | ||
// Example: | ||
// | ||
// vc, err := credential.New( | ||
// template.NewGovernance( | ||
// "datasetID", | ||
// "addr", | ||
// WithID[*GovernanceDescriptor]("id") | ||
// ), | ||
// WithParser(parser), | ||
// WithSignature(signer, "did:key:...")). // Signature is optional and Generate a not signed VC if not provided. | ||
// Generate() | ||
func New(descriptor Descriptor, opts ...Option) *Generator { | ||
g := &Generator{ | ||
vc: descriptor, | ||
} | ||
for _, opt := range opts { | ||
opt(g) | ||
} | ||
return g | ||
} | ||
|
||
// Option is a function that configures a Generator. | ||
type Option func(*Generator) | ||
|
||
func WithParser(parser *DefaultParser) Option { | ||
return func(g *Generator) { | ||
g.parser = parser | ||
} | ||
} | ||
|
||
func WithSigner(signer verifiable.Signer, did string) Option { | ||
return func(g *Generator) { | ||
g.signer = signer | ||
g.signerDID = did | ||
} | ||
} | ||
|
||
func (generator *Generator) Generate() (*verifiable.Credential, error) { | ||
raw, err := generator.vc.Generate() | ||
if err != nil { | ||
return nil, NewVCError(ErrGenerate, err) | ||
} | ||
|
||
if generator.parser == nil { | ||
return nil, NewVCError(ErrNoParser, nil) | ||
} | ||
cred, err := generator.parser.parse(raw.Bytes()) | ||
if err != nil { | ||
return nil, NewVCError(ErrParse, err) | ||
} | ||
|
||
if generator.signer != nil { | ||
err := cred.AddLinkedDataProof(&verifiable.LinkedDataProofContext{ | ||
Created: generator.vc.IssuedAt(), | ||
SignatureType: "EcdsaSecp256k1Signature2019", | ||
Suite: ecdsasecp256k1signature2019.New(suite.WithSigner(generator.signer)), | ||
SignatureRepresentation: verifiable.SignatureJWS, | ||
VerificationMethod: generator.signerDID, | ||
Purpose: generator.vc.ProofPurpose(), | ||
}, jsonld.WithDocumentLoader(generator.parser.documentLoader)) | ||
if err != nil { | ||
return nil, NewVCError(ErrSign, err) | ||
} | ||
} | ||
|
||
return cred, nil | ||
} | ||
|
||
// Descriptor is an interface representing the description of a verifiable credential. | ||
type Descriptor interface { | ||
IssuedAt() *time.Time | ||
Generate() (*bytes.Buffer, error) | ||
ProofPurpose() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//nolint:lll | ||
package credential_test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/axone-protocol/axone-sdk/credential" | ||
"github.com/axone-protocol/axone-sdk/testutil" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestGenerator_Generate(t *testing.T) { | ||
t.Run("without parser", func(t *testing.T) { | ||
Convey("Given a credential generator with mocked descriptor", t, func() { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
mockDescriptor := testutil.NewMockDescriptor(controller) | ||
mockDescriptor.EXPECT().Generate().Return(nil, nil).Times(1) | ||
generator := credential.New(mockDescriptor) | ||
|
||
Convey("When generating a credential", func() { | ||
vc, err := generator.Generate() | ||
Convey("Then an error should be returned", func() { | ||
So(err, ShouldNotBeNil) | ||
So(err.Error(), ShouldEqual, "no parser provided") | ||
So(vc, ShouldBeNil) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
t.Run("error generate", func(t *testing.T) { | ||
Convey("Given a credential generator with mocked descriptor", t, func() { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
mockDescriptor := testutil.NewMockDescriptor(controller) | ||
mockDescriptor.EXPECT().Generate().Return(nil, errors.New("failed")).Times(1) | ||
generator := credential.New(mockDescriptor) | ||
|
||
Convey("When generating a credential", func() { | ||
vc, err := generator.Generate() | ||
Convey("Then an error should be returned", func() { | ||
So(err, ShouldNotBeNil) | ||
So(err.Error(), ShouldEqual, credential.NewVCError(credential.ErrGenerate, errors.New("failed")).Error()) | ||
So(vc, ShouldBeNil) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
t.Run("with signer", func(t *testing.T) { | ||
Convey("Given a credential generator with mocked descriptor", t, func() { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
buf := bytes.NewBufferString(`{"@context":["https://www.w3.org/2018/credentials/v1"],"type":["VerifiableCredential"],"credentialSubject":{"id":"did:example:123"}}`) | ||
mockDescriptor := testutil.NewMockDescriptor(controller) | ||
mockDescriptor.EXPECT().Generate().Return(buf, nil).Times(1) | ||
mockDescriptor.EXPECT().IssuedAt().Times(1) | ||
mockDescriptor.EXPECT().ProofPurpose().Return("proof").Times(1) | ||
|
||
mockSigner := testutil.NewMockSigner(controller) | ||
mockSigner.EXPECT().Sign(gomock.Any()).Return([]byte("signature"), nil).Times(1) | ||
mockSigner.EXPECT().Alg().AnyTimes() | ||
|
||
loader, _ := testutil.MockDocumentLoader() | ||
|
||
generator := credential.New(mockDescriptor, | ||
credential.WithParser(credential.NewDefaultParser(loader)), | ||
credential.WithSigner(mockSigner, "did:example:123"), | ||
) | ||
|
||
Convey("When generating a credential", func() { | ||
vc, err := generator.Generate() | ||
Convey("Then VC should be returned with proofs", func() { | ||
So(err, ShouldBeNil) | ||
So(vc, ShouldNotBeNil) | ||
So(len(vc.Proofs), ShouldEqual, 1) | ||
So(vc.Proofs[0]["verificationMethod"], ShouldEqual, "did:example:123") | ||
So(vc.Proofs[0]["proofPurpose"], ShouldEqual, "proof") | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
t.Run("without signer", func(t *testing.T) { | ||
Convey("Given a credential generator with mocked descriptor", t, func() { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
buf := bytes.NewBufferString(`{"@context":["https://www.w3.org/2018/credentials/v1"],"type":["VerifiableCredential"],"credentialSubject":{"id":"did:example:123"}}`) | ||
mockDescriptor := testutil.NewMockDescriptor(controller) | ||
mockDescriptor.EXPECT().Generate().Return(buf, nil).Times(1) | ||
|
||
loader, _ := testutil.MockDocumentLoader() | ||
|
||
generator := credential.New(mockDescriptor, | ||
credential.WithParser(credential.NewDefaultParser(loader)), | ||
) | ||
|
||
Convey("When generating a credential", func() { | ||
vc, err := generator.Generate() | ||
Convey("Then VC should be returned without proofs", func() { | ||
So(err, ShouldBeNil) | ||
So(vc, ShouldNotBeNil) | ||
So(len(vc.Proofs), ShouldEqual, 0) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.