Skip to content

Commit

Permalink
Merge pull request #828 from sandrask/issue-826
Browse files Browse the repository at this point in the history
feat: Add verifiable credential store
  • Loading branch information
sandrask authored Oct 21, 2021
2 parents 4aedfef + f95fccc commit acb0c90
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 73 deletions.
15 changes: 11 additions & 4 deletions cmd/orb-server/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ import (
"github.com/trustbloc/orb/pkg/resolver/resource"
"github.com/trustbloc/orb/pkg/resolver/resource/registry"
"github.com/trustbloc/orb/pkg/resolver/resource/registry/didanchorinfo"
anchoreventstore "github.com/trustbloc/orb/pkg/store/anchorevent"
casstore "github.com/trustbloc/orb/pkg/store/cas"
didanchorstore "github.com/trustbloc/orb/pkg/store/didanchor"
opstore "github.com/trustbloc/orb/pkg/store/operation"
unpublishedopstore "github.com/trustbloc/orb/pkg/store/operation/unpublished"
"github.com/trustbloc/orb/pkg/store/vcstatus"
vcstore "github.com/trustbloc/orb/pkg/store/verifiable"
proofstore "github.com/trustbloc/orb/pkg/store/witness"
"github.com/trustbloc/orb/pkg/store/wrapper"
"github.com/trustbloc/orb/pkg/vcsigner"
Expand Down Expand Up @@ -520,7 +520,7 @@ func startOrbServices(parameters *orbParameters) error {
return fmt.Errorf("failed to create vc builder: %s", err.Error())
}

vcStore, err := vcstore.New(storeProviders.provider, orbDocumentLoader)
anchorEventStore, err := anchoreventstore.New(storeProviders.provider, orbDocumentLoader)
if err != nil {
return fmt.Errorf("failed to create vc store: %s", err.Error())
}
Expand Down Expand Up @@ -602,7 +602,7 @@ func startOrbServices(parameters *orbParameters) error {

proofHandler := proof.New(
&proof.Providers{
AnchorEventStore: vcStore,
AnchorEventStore: anchorEventStore,
StatusStore: vcStatusStore,
MonitoringSvc: monitoringSvc,
DocLoader: orbDocumentLoader,
Expand Down Expand Up @@ -669,11 +669,16 @@ func startOrbServices(parameters *orbParameters) error {

o.Start()

vcStore, err := storeProviders.provider.OpenStore("verifiable")
if err != nil {
return fmt.Errorf("open store: %w", err)
}

anchorWriterProviders := &writer.Providers{
AnchorGraph: anchorGraph,
DidAnchors: didAnchors,
AnchorBuilder: vcBuilder,
AnchorEventStore: vcStore,
AnchorEventStore: anchorEventStore,
VCStatusStore: vcStatusStore,
OpProcessor: opProcessor,
Outbox: activityPubService.Outbox(),
Expand All @@ -683,6 +688,8 @@ func startOrbServices(parameters *orbParameters) error {
ActivityStore: apStore,
WitnessStore: witnessProofStore,
WFClient: wfClient,
DocumentLoader: orbDocumentLoader,
VCStore: vcStore,
}

anchorWriter, err := writer.New(parameters.didNamespace,
Expand Down
34 changes: 17 additions & 17 deletions pkg/anchor/handler/proof/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"github.com/trustbloc/orb/pkg/internal/testutil"
orbmocks "github.com/trustbloc/orb/pkg/mocks"
"github.com/trustbloc/orb/pkg/pubsub/mempubsub"
anchoreventstore "github.com/trustbloc/orb/pkg/store/anchorevent"
storemocks "github.com/trustbloc/orb/pkg/store/mocks"
"github.com/trustbloc/orb/pkg/store/vcstatus"
vcstore "github.com/trustbloc/orb/pkg/store/verifiable"
"github.com/trustbloc/orb/pkg/store/witness"
)

Expand All @@ -42,7 +42,7 @@ const (
func TestNew(t *testing.T) {
ps := mempubsub.New(mempubsub.Config{})

store, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
store, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

providers := &Providers{
Expand All @@ -66,7 +66,7 @@ func TestWitnessProofHandler(t *testing.T) {
expiryTime := time.Now().Add(60 * time.Second)

t.Run("success - witness policy not satisfied", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("success - witness policy satisfied", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("success - vc status is completed", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("success - policy satisfied but some witness proofs are empty", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - get vc status error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - second get vc status error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - set vc status to complete error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("VC status already completed", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - witness policy error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - vc status not found store error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -472,7 +472,7 @@ func TestWitnessProofHandler(t *testing.T) {
provider := &storemocks.Provider{}
provider.OpenStoreReturns(store, nil)

vcStore, err := vcstore.New(provider, testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(provider, testutil.GetLoader(t))
require.NoError(t, err)

vcStatusStore, err := vcstatus.New(mem.NewProvider())
Expand All @@ -498,7 +498,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - witness store add proof error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -531,7 +531,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - witness store add proof error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down Expand Up @@ -564,7 +564,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - unmarshal witness proof", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

vcStatusStore, err := vcstatus.New(mem.NewProvider())
Expand All @@ -590,7 +590,7 @@ func TestWitnessProofHandler(t *testing.T) {
})

t.Run("error - monitoring error", func(t *testing.T) {
vcStore, err := vcstore.New(mem.NewProvider(), testutil.GetLoader(t))
vcStore, err := anchoreventstore.New(mem.NewProvider(), testutil.GetLoader(t))
require.NoError(t, err)

ae := &vocab.AnchorEventType{}
Expand Down
40 changes: 39 additions & 1 deletion pkg/anchor/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"

"github.com/ThreeDotsLabs/watermill/message"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/hyperledger/aries-framework-go/spi/storage"
"github.com/piprate/json-gold/ld"
"github.com/trustbloc/edge-core/pkg/log"
"github.com/trustbloc/sidetree-core-go/pkg/api/operation"
"github.com/trustbloc/sidetree-core-go/pkg/api/protocol"
Expand Down Expand Up @@ -86,6 +89,8 @@ type Providers struct {
WitnessStore witnessStore
ActivityStore activityStore
WFClient webfingerClient
DocumentLoader ld.DocumentLoader
VCStore storage.Store
}

type webfingerClient interface {
Expand Down Expand Up @@ -435,7 +440,7 @@ func (c *Writer) signCredentialWithLocalWitnessLog(vc *verifiable.Credential) (*
return vc, nil
}

func (c *Writer) handle(anchorEvent *vocab.AnchorEventType) error {
func (c *Writer) handle(anchorEvent *vocab.AnchorEventType) error { //nolint:funlen
logger.Debugf("handling witnessed anchor event: %s", anchorEvent.Anchors())

startTime := time.Now()
Expand All @@ -445,13 +450,21 @@ func (c *Writer) handle(anchorEvent *vocab.AnchorEventType) error {
}()

// store anchor credential with witness proofs
// TODO: Change this to delete since we don't need anchor event in anchor event store(transient data, issue-827)
err := c.AnchorEventStore.Put(anchorEvent)
if err != nil {
logger.Warnf("failed to store witnessed anchor event[%s]: %s", anchorEvent.Anchors(), err.Error())

return errors.NewTransient(fmt.Errorf("store witnessed anchor event[%s]: %w", anchorEvent.Anchors(), err))
}

err = c.storeVC(anchorEvent)
if err != nil {
logger.Errorf("failed to store verifiable credential from anchor event[%s]: %s", anchorEvent.Anchors(), err.Error())

return fmt.Errorf("store verifiable credential from anchor event[%s]: %w", anchorEvent.Anchors(), err)
}

anchorEventRef, err := c.AnchorGraph.Add(anchorEvent)
if err != nil {
logger.Errorf("failed to add witnessed anchor event[%s] to anchor graph: %s", anchorEvent.Anchors(), err.Error())
Expand Down Expand Up @@ -493,6 +506,31 @@ func (c *Writer) handle(anchorEvent *vocab.AnchorEventType) error {
return nil
}

func (c *Writer) storeVC(anchorEvent *vocab.AnchorEventType) error {
vc, err := util.VerifiableCredentialFromAnchorEvent(anchorEvent,
verifiable.WithDisabledProofCheck(),
verifiable.WithJSONLDDocumentLoader(c.DocumentLoader),
)
if err != nil {
return fmt.Errorf("failed get verifiable credential from anchor event: %w", err)
}

vcBytes, err := json.Marshal(vc)
if err != nil {
return fmt.Errorf("failed to marshal vc[%s]: %w", vc.ID, err)
}

parts := strings.Split(vc.ID, "/")
id := parts[len(parts)-1]

err = c.VCStore.Put(id, vcBytes)
if err != nil {
return fmt.Errorf("failed to store vc[%s]: %w", id, err)
}

return nil
}

// postCreateActivity creates and posts create activity (announces anchor credential to followers).
func (c *Writer) postCreateActivity(anchorEvent *vocab.AnchorEventType, hl string) error { //nolint: interfacer
systemFollowers, err := url.Parse(c.apServiceIRI.String() + resthandler.FollowersPath)
Expand Down
Loading

0 comments on commit acb0c90

Please sign in to comment.