From f40fc9a6ba48f5bda7c0e21d81059d64cc5104dd Mon Sep 17 00:00:00 2001 From: Gerard Snaauw <33763579+gerardsn@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:25:20 +0200 Subject: [PATCH] backport: VDR once deactivated is always deactivated (#3414) --- vdr/didstore/store_test.go | 35 +++++++++++++++++++++++++++++++++++ vdr/didstore/writer.go | 5 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/vdr/didstore/store_test.go b/vdr/didstore/store_test.go index b1fd349f66..1c1f53eed0 100644 --- a/vdr/didstore/store_test.go +++ b/vdr/didstore/store_test.go @@ -205,6 +205,41 @@ func TestStore_Add(t *testing.T) { require.NoError(t, err) }) }) + + t.Run("once deactivated is always deactivated", func(t *testing.T) { + store := NewTestStore(t) + + deactivate := create + deactivate.Controller = nil + deactivate.VerificationMethod = nil + txDeactivate := newTestTransaction(deactivate, txCreate.Ref) + update := did.Document{ID: testDID, Controller: []did.DID{testDID}, Service: []did.Service{{ID: ssi.MustParseURI("service")}}} + txUpdate := newTestTransaction(update, txDeactivate.Ref) + + require.NoError(t, store.Add(create, txCreate)) + require.NoError(t, store.Add(deactivate, txDeactivate)) + require.NoError(t, store.Add(update, txUpdate)) + + t.Run("metadata ok", func(t *testing.T) { + err := store.db.ReadShelf(context.Background(), metadataShelf, func(reader stoabs.Reader) error { + for i := range []int{0, 1, 2} { + metaBytes, err := reader.Get(stoabs.BytesKey(fmt.Sprintf("%s%d", testDID.String(), i))) + if err != nil { + return err + } + metadata := documentMetadata{} + err = json.Unmarshal(metaBytes, &metadata) + if err != nil { + return err + } + assert.Equal(t, i > 0, metadata.Deactivated, "document version %d", i) + } + return nil + }) + require.NoError(t, err) + }) + + }) } func TestStore_Resolve(t *testing.T) { diff --git a/vdr/didstore/writer.go b/vdr/didstore/writer.go index 7470d29b37..ce6673454e 100644 --- a/vdr/didstore/writer.go +++ b/vdr/didstore/writer.go @@ -189,6 +189,9 @@ func applyEvent(tx stoabs.WriteTx, latestMetadata *documentMetadata, nextEvent e if err != nil { return nil, nil, fmt.Errorf("read document failed: %w", err) } + // create nextMetadata based on this event and some defaults. + // For the initial DID document this does not change. (latestMetadata = nil) + // For DID updates, applyDocument will make changes. nextMetadata := documentMetadata{ Created: nextEvent.SigningTime, Updated: nextEvent.SigningTime, @@ -229,6 +232,7 @@ func applyDocument(tx stoabs.ReadTx, currentMeta *documentMetadata, newDoc did.D newMeta.Version = currentMeta.Version + 1 newMeta.Created = currentMeta.Created newMeta.PreviousHash = ¤tMeta.Hash + newMeta.Deactivated = newMeta.Deactivated || currentMeta.Deactivated // once deactivated is always deactivated unconsumed := map[string]struct{}{} outer: @@ -266,7 +270,6 @@ outer: newDocBytes, _ := json.Marshal(newDoc) newMeta.Hash = hash.SHA256Sum(newDocBytes) - newMeta.Deactivated = isDeactivated(newDoc) return newDoc, newMeta, nil }