diff --git a/crypto/test.go b/crypto/test.go index f5265d7250..eeaff687c5 100644 --- a/crypto/test.go +++ b/crypto/test.go @@ -146,7 +146,7 @@ func newKeyReference(t *testing.T, client *Crypto, kid string) (*orm.KeyReferenc ref, publicKey, err := client.New(audit.TestContext(), StringNamingFunc(kid)) require.NoError(t, err) DID := orm.DID{ID: "did:test:" + t.Name(), Subject: "subject"} - DIDDoc := orm.DIDDocument{ + DIDDoc := orm.DidDocument{ DID: DID, VerificationMethods: []orm.VerificationMethod{ { diff --git a/storage/orm/changelog.go b/storage/orm/changelog.go index b6c6762a21..d8ea7fdbd3 100644 --- a/storage/orm/changelog.go +++ b/storage/orm/changelog.go @@ -35,7 +35,7 @@ type DIDChangeLog struct { DIDDocumentVersionID string `gorm:"primaryKey;column:did_document_version_id"` Type string TransactionID string `gorm:"column:transaction_id"` - DIDDocumentVersion DIDDocument `gorm:"foreignKey:DIDDocumentVersionID;references:ID"` + DIDDocumentVersion DidDocument `gorm:"foreignKey:DIDDocumentVersionID;references:ID"` } func (d DIDChangeLog) TableName() string { diff --git a/storage/orm/changelog_test.go b/storage/orm/changelog_test.go index 1f43783738..e95a713eea 100644 --- a/storage/orm/changelog_test.go +++ b/storage/orm/changelog_test.go @@ -29,7 +29,7 @@ func TestDIDEventLog_DID(t *testing.T) { t.Run("ok", func(t *testing.T) { id := did.MustParseDID("did:example:123") didEventLog := DIDChangeLog{ - DIDDocumentVersion: DIDDocument{ + DIDDocumentVersion: DidDocument{ DID: DID{ID: id.String()}, }, } @@ -38,7 +38,7 @@ func TestDIDEventLog_DID(t *testing.T) { }) t.Run("malformed DID", func(t *testing.T) { didEventLog := DIDChangeLog{ - DIDDocumentVersion: DIDDocument{ + DIDDocumentVersion: DidDocument{ DID: DID{ID: "malformed"}, }, } @@ -51,7 +51,7 @@ func TestDIDEventLog_Method(t *testing.T) { t.Run("ok", func(t *testing.T) { id := did.MustParseDID("did:example:123") didEventLog := DIDChangeLog{ - DIDDocumentVersion: DIDDocument{ + DIDDocumentVersion: DidDocument{ DID: DID{ID: id.String()}, }, } @@ -60,7 +60,7 @@ func TestDIDEventLog_Method(t *testing.T) { }) t.Run("malformed DID", func(t *testing.T) { didEventLog := DIDChangeLog{ - DIDDocumentVersion: DIDDocument{ + DIDDocumentVersion: DidDocument{ DID: DID{ID: "malformed"}, }, } diff --git a/storage/orm/did_document.go b/storage/orm/did_document.go index 8b9131a856..b5da3fc19a 100644 --- a/storage/orm/did_document.go +++ b/storage/orm/did_document.go @@ -26,8 +26,8 @@ import ( "gorm.io/gorm/schema" ) -// DIDDocument is the gorm representation of the did_document_version table -type DIDDocument struct { +// DidDocument is the gorm representation of the did_document_version table +type DidDocument struct { ID string `gorm:"primaryKey"` DidID string `gorm:"column:did"` DID DID `gorm:"foreignKey:DidID;references:ID"` @@ -36,19 +36,19 @@ type DIDDocument struct { // Also used to purge DID document changes that haven't been committed within a certain time frame UpdatedAt int64 `gorm:"autoUpdateTime:false"` Version int - VerificationMethods []VerificationMethod `gorm:"foreignKey:DIDDocumentID;references:ID"` - Services []Service `gorm:"foreignKey:DIDDocumentID;references:ID"` + VerificationMethods []VerificationMethod `gorm:"many2many:did_document_to_verification_method"` + Services []Service `gorm:"many2many:did_document_to_service"` // Raw contains the DID Document as generated by the specific method, important for hashing. Raw string } -func (d DIDDocument) TableName() string { +func (d DidDocument) TableName() string { return "did_document_version" } var _ schema.Tabler = (*DID)(nil) -func (sqlDoc DIDDocument) ToDIDDocument() (did.Document, error) { +func (sqlDoc DidDocument) ToDIDDocument() (did.Document, error) { if len(sqlDoc.Raw) > 0 { document := did.Document{} err := json.Unmarshal([]byte(sqlDoc.Raw), &document) @@ -60,7 +60,7 @@ func (sqlDoc DIDDocument) ToDIDDocument() (did.Document, error) { return sqlDoc.GenerateDIDDocument() } -func (sqlDoc DIDDocument) GenerateDIDDocument() (did.Document, error) { +func (sqlDoc DidDocument) GenerateDIDDocument() (did.Document, error) { id, _ := did.ParseDID(sqlDoc.DID.ID) others := make([]ssi.URI, 0) for _, alias := range sqlDoc.DID.Aka { diff --git a/storage/orm/did_document_test.go b/storage/orm/did_document_test.go index 947648845c..67aacd4238 100644 --- a/storage/orm/did_document_test.go +++ b/storage/orm/did_document_test.go @@ -26,7 +26,7 @@ func TestDIDDocument_ToDIDDocument(t *testing.T) { ID: "#2", Data: []byte(serviceData), } - document := DIDDocument{ + document := DidDocument{ ID: "id", DID: DID{ID: alice.String(), Aka: []DID{{ID: bob.String()}}}, Version: 1, diff --git a/storage/orm/service.go b/storage/orm/service.go index 02483582d9..e2417dfb76 100644 --- a/storage/orm/service.go +++ b/storage/orm/service.go @@ -24,9 +24,8 @@ var _ schema.Tabler = (*Service)(nil) // Service is the gorm representation of the did_service table type Service struct { - ID string `gorm:"primaryKey"` - DIDDocumentID string `gorm:"column:did_document_id"` - Data []byte + ID string `gorm:"primaryKey"` + Data []byte } func (v Service) TableName() string { diff --git a/storage/orm/verification_method.go b/storage/orm/verification_method.go index 6aba3d8244..caa57f33de 100644 --- a/storage/orm/verification_method.go +++ b/storage/orm/verification_method.go @@ -18,23 +18,20 @@ package orm -import ( - "gorm.io/gorm/schema" -) +import "gorm.io/gorm/schema" var _ schema.Tabler = (*VerificationMethod)(nil) // VerificationMethod is the gorm representation of the did_verificationmethod table type VerificationMethod struct { - ID string `gorm:"primaryKey"` - DIDDocumentID string `gorm:"column:did_document_id"` - KeyTypes VerificationMethodKeyType - Weight int16 - Data []byte + ID string `gorm:"primaryKey"` + KeyTypes VerificationMethodKeyType + Weight int16 + Data []byte } func (v VerificationMethod) TableName() string { - return "did_verificationmethod" + return "did_verification_method" } // VerificationMethodKeyType is used to marshal and unmarshal the key type to the DB diff --git a/storage/sql_migrations/003_did.sql b/storage/sql_migrations/003_did.sql index cefff6cb8a..1c06320508 100644 --- a/storage/sql_migrations/003_did.sql +++ b/storage/sql_migrations/003_did.sql @@ -21,8 +21,7 @@ create table did_document_version version int not null, raw $TEXT_TYPE, -- make not nil in future PR unique (did, version), - foreign key (did) references did (id) on delete cascade, - unique (did, version) + foreign key (did) references did (id) on delete cascade ); -- this table is used for the poor-mans 2-phase commit @@ -49,12 +48,10 @@ create table key_reference ); -- this table is used to store the verification methods for locally managed DIDs -create table did_verificationmethod +create table did_verification_method ( -- id is the unique id of the verification method as it appears in the DID document using the fully qualified representation. id varchar(415) not null primary key, - -- did_document_id references the DID document version - did_document_id varchar(36) not null, -- key_types is a base64 encoded bitmask of the key types supported by the verification method. -- 0x01 - AssertionMethod -- 0x02 - Authentication @@ -67,8 +64,19 @@ create table did_verificationmethod weight SMALLINT default 0, -- data is a JSON object containing the verification method data, e.g. the public key. -- When producing the verificationMethod, data is used as JSON base object and the id and type are added. - data $TEXT_TYPE not null, - foreign key (did_document_id) references did_document_version (id) on delete cascade + data $TEXT_TYPE not null +); + +-- this table is used to link unique verification methods to all DID document versions they are used in +create table did_document_to_verification_method +( + -- did_document_id references the DID document version + did_document_id varchar(36) not null, + -- verification_method_id references the verification method + verification_method_id varchar(415) not null, + primary key (did_document_id,verification_method_id), + foreign key (did_document_id) references did_document_version (id) on delete cascade, + foreign key (verification_method_id) references did_verification_method (id) on delete cascade ); -- this table is used to store the services for locally managed DIDs @@ -76,16 +84,25 @@ create table did_service ( -- id is the unique id of the service as it appears in the DID document using the shorthand representation. id varchar(254) not null primary key, - -- did_document_id references the DID document version - did_document_id varchar(36) not null, -- data is a JSON object containing the service data, e.g. the serviceEndpoint. -- When producing the service, data is used as JSON base object and the id and type are added. - data $TEXT_TYPE not null, - foreign key (did_document_id) references did_document_version (id) on delete cascade + data $TEXT_TYPE not null +); + +-- this table is used to link unique services to all DID document versions they are used in +create table did_document_to_service +( + -- did_document_id references the DID document version + did_document_id varchar(36) not null, + -- service_id references the DID service + service_id varchar(254) not null, + primary key (did_document_id,service_id), + foreign key (did_document_id) references did_document_version (id) on delete cascade, + foreign key (service_id) references did_service (id) on delete cascade ); -- +goose Down -drop table did_verificationmethod; +drop table did_verification_method; drop table did_service; drop table did_change_log; drop table did_document_version; diff --git a/vdr/didnuts/manager.go b/vdr/didnuts/manager.go index dc47c89111..f5cae0639b 100644 --- a/vdr/didnuts/manager.go +++ b/vdr/didnuts/manager.go @@ -252,7 +252,7 @@ func (m Manager) Update(ctx context.Context, id did.DID, next did.Document) erro * New style DID Method Manager ******************************/ -func (m Manager) NewDocument(ctx context.Context, _ orm.DIDKeyFlags) (*orm.DIDDocument, error) { +func (m Manager) NewDocument(ctx context.Context, _ orm.DIDKeyFlags) (*orm.DidDocument, error) { keyRef, publicKey, err := m.keyStore.New(ctx, DIDKIDNamingFunc) if err != nil { return nil, err @@ -272,7 +272,7 @@ func (m Manager) NewDocument(ctx context.Context, _ orm.DIDKeyFlags) (*orm.DIDDo } vmAsJson, _ := json.Marshal(verificationMethod) now := time.Now().Unix() - sqlDoc := orm.DIDDocument{ + sqlDoc := orm.DidDocument{ DID: orm.DID{ ID: keyID.DID.String(), }, diff --git a/vdr/didnuts/manager_test.go b/vdr/didnuts/manager_test.go index 85f6b9fa65..bd17138ed0 100644 --- a/vdr/didnuts/manager_test.go +++ b/vdr/didnuts/manager_test.go @@ -332,7 +332,7 @@ func TestManager_Commit(t *testing.T) { data, _ := json.Marshal(document.VerificationMethod[0]) return orm.DIDChangeLog{ Type: orm.DIDChangeCreated, - DIDDocumentVersion: orm.DIDDocument{ + DIDDocumentVersion: orm.DidDocument{ ID: uuid.New().String(), DID: orm.DID{ ID: document.ID.String(), @@ -418,7 +418,7 @@ func TestManager_IsCommitted(t *testing.T) { vmData, _ := json.Marshal(document.VerificationMethod[0]) eventLog := orm.DIDChangeLog{ Type: orm.DIDChangeCreated, - DIDDocumentVersion: orm.DIDDocument{ + DIDDocumentVersion: orm.DidDocument{ ID: uuid.New().String(), DID: orm.DID{ ID: document.ID.String(), diff --git a/vdr/didsubject/did_document.go b/vdr/didsubject/did_document.go index c31fb10376..dc265ed3d5 100644 --- a/vdr/didsubject/did_document.go +++ b/vdr/didsubject/did_document.go @@ -37,10 +37,10 @@ type DIDDocumentManager interface { // If the DID does not exist yet, it will be created // It adds all verification methods, services, alsoKnownAs to the DID document // Not passing any verification methods will create an empty DID document, deactivation checking should be done by the caller - CreateOrUpdate(did orm.DID, verificationMethods []orm.VerificationMethod, services []orm.Service) (*orm.DIDDocument, error) + CreateOrUpdate(did orm.DID, verificationMethods []orm.VerificationMethod, services []orm.Service) (*orm.DidDocument, error) // Latest returns the latest version of a DID document // if notAfter is given, it will return the latest version before that time - Latest(did did.DID, notAfter *time.Time) (*orm.DIDDocument, error) + Latest(did did.DID, notAfter *time.Time) (*orm.DidDocument, error) } // SqlDIDDocumentManager is the implementation of the DIDDocumentManager interface @@ -53,28 +53,19 @@ func NewDIDDocumentManager(tx *gorm.DB) *SqlDIDDocumentManager { return &SqlDIDDocumentManager{tx: tx} } -func (s *SqlDIDDocumentManager) CreateOrUpdate(did orm.DID, verificationMethods []orm.VerificationMethod, services []orm.Service) (*orm.DIDDocument, error) { - latest := orm.DIDDocument{Version: -1} // -1 means no document exists, will be overwritten below if there is a document +func (s *SqlDIDDocumentManager) CreateOrUpdate(did orm.DID, verificationMethods []orm.VerificationMethod, services []orm.Service) (*orm.DidDocument, error) { + latest := orm.DidDocument{Version: -1} // -1 means no document exists, will be overwritten below if there is a document err := s.tx.Preload("DID").Where("did = ?", did.ID).Order("version desc").First(&latest).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return nil, err } - version := latest.Version + 1 - id := uuid.New().String() - // update DIDDocumentID for all VMs and services - for i := range verificationMethods { - verificationMethods[i].DIDDocumentID = id - } - for i := range services { - services[i].DIDDocumentID = id - } - now := time.Now().Unix() - doc := orm.DIDDocument{ - ID: id, + + doc := orm.DidDocument{ + ID: uuid.New().String(), DID: did, CreatedAt: latest.CreatedAt, - UpdatedAt: now, - Version: version, + UpdatedAt: time.Now().Unix(), + Version: latest.Version + 1, VerificationMethods: verificationMethods, Services: services, } @@ -87,8 +78,8 @@ func (s *SqlDIDDocumentManager) CreateOrUpdate(did orm.DID, verificationMethods return &doc, err } -func (s *SqlDIDDocumentManager) Latest(did did.DID, resolveTime *time.Time) (*orm.DIDDocument, error) { - doc := orm.DIDDocument{} +func (s *SqlDIDDocumentManager) Latest(did did.DID, resolveTime *time.Time) (*orm.DidDocument, error) { + doc := orm.DidDocument{} notAfter := time.Now().Add(time.Hour).Unix() if resolveTime != nil { notAfter = resolveTime.Unix() diff --git a/vdr/didsubject/did_document_test.go b/vdr/didsubject/did_document_test.go index 067aa0c243..e9f0657187 100644 --- a/vdr/didsubject/did_document_test.go +++ b/vdr/didsubject/did_document_test.go @@ -34,12 +34,12 @@ var sqlDidAlice = orm.DID{ID: alice.String(), Subject: "alice"} func TestSqlDIDDocumentManager_CreateOrUpdate(t *testing.T) { keyUsageFlag := orm.VerificationMethodKeyType(31) vm := orm.VerificationMethod{ - ID: "#1", + ID: "#key-1", Data: []byte("{}"), KeyTypes: keyUsageFlag, } service := orm.Service{ - ID: "#2", + ID: "#service-1", Data: []byte("{}"), } sqlDidBob := orm.DID{ID: bob.String(), Subject: "bob"} @@ -74,31 +74,54 @@ func TestSqlDIDDocumentManager_CreateOrUpdate(t *testing.T) { assert.Equal(t, []byte("{}"), doc.VerificationMethods[0].Data) assert.Equal(t, keyUsageFlag, doc.VerificationMethods[0].KeyTypes) assert.Equal(t, []byte("{}"), doc.Services[0].Data) - }) t.Run("update", func(t *testing.T) { - tx := db.Begin() - docManager := NewDIDDocumentManager(tx) + countRows := func(t *testing.T, table interface{}) int { + var count int64 + err := db.Model(table).Count(&count).Error + if err != nil { + t.Fatal(err) + } + return int(count) + } + vm2 := orm.VerificationMethod{ + ID: "#key-2", + Data: []byte("{}"), + KeyTypes: keyUsageFlag, + } + service2 := orm.Service{ + ID: "#service-2", + Data: []byte("{}"), + } + + docManager := NewDIDDocumentManager(db) docRoot, err := docManager.CreateOrUpdate(sqlDidBob, []orm.VerificationMethod{vm}, []orm.Service{service}) require.NoError(t, err) - require.NoError(t, tx.Commit().Error) // rewrite timestamps to make sure docRoot.CreatedAt < doc.UpdatedAt createdAt := time.Now().Add(-2 * time.Second).Unix() docRoot.CreatedAt, docRoot.UpdatedAt = createdAt, createdAt db.Save(&docRoot) - docManager = NewDIDDocumentManager(transaction(t, db)) + // update service + doc, err := docManager.CreateOrUpdate(sqlDidBob, []orm.VerificationMethod{vm}, []orm.Service{service2}) require.NoError(t, err) + require.Len(t, doc.Services, 1) + assert.Equal(t, 2, countRows(t, &orm.Service{})) + assert.Equal(t, 1, countRows(t, &orm.VerificationMethod{})) - doc, err := docManager.CreateOrUpdate(sqlDidBob, []orm.VerificationMethod{vm}, []orm.Service{service}) + // update vm, re-add service1 + doc, err = docManager.CreateOrUpdate(sqlDidBob, []orm.VerificationMethod{vm2}, []orm.Service{service, service2}) + require.NoError(t, err) + assert.Equal(t, 2, countRows(t, &orm.Service{})) + assert.Equal(t, 2, countRows(t, &orm.VerificationMethod{})) assert.Len(t, doc.ID, 36) // uuid v4 require.Len(t, doc.VerificationMethods, 1) - require.Len(t, doc.Services, 1) + require.Len(t, doc.Services, 2) assert.Equal(t, docRoot.CreatedAt, doc.CreatedAt) assert.Less(t, doc.CreatedAt, doc.UpdatedAt) - assert.Equal(t, 1, doc.Version) + assert.Equal(t, 2, doc.Version) }) } diff --git a/vdr/didsubject/management.go b/vdr/didsubject/management.go index 75ed0c04d4..5fd703160a 100644 --- a/vdr/didsubject/management.go +++ b/vdr/didsubject/management.go @@ -37,7 +37,7 @@ var ErrUnsupportedDIDMethod = errors.New("unsupported DID method") type MethodManager interface { // NewDocument generates a new DID document for the given subject. // This is done by the method manager since the DID might depend on method specific rules. - NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*orm.DIDDocument, error) + NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*orm.DidDocument, error) // NewVerificationMethod generates a new VerificationMethod for the given subject. // This is done by the method manager since the VM ID might depend on method specific rules. // If keyUsage includes management.KeyAgreement, an RSA key is generated, otherwise an EC key. diff --git a/vdr/didsubject/management_mock.go b/vdr/didsubject/management_mock.go index 19c8c15df5..850febc296 100644 --- a/vdr/didsubject/management_mock.go +++ b/vdr/didsubject/management_mock.go @@ -72,10 +72,10 @@ func (mr *MockMethodManagerMockRecorder) IsCommitted(ctx, event any) *gomock.Cal } // NewDocument mocks base method. -func (m *MockMethodManager) NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*orm.DIDDocument, error) { +func (m *MockMethodManager) NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*orm.DidDocument, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "NewDocument", ctx, keyFlags) - ret0, _ := ret[0].(*orm.DIDDocument) + ret0, _ := ret[0].(*orm.DidDocument) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/vdr/didsubject/manager.go b/vdr/didsubject/manager.go index 9c3d21b38a..2810b690e8 100644 --- a/vdr/didsubject/manager.go +++ b/vdr/didsubject/manager.go @@ -128,7 +128,7 @@ func (r *Manager) Create(ctx context.Context, options CreationOptions) ([]did.Do } } - sqlDocs := make(map[string]orm.DIDDocument) + sqlDocs := make(map[string]orm.DidDocument) err := r.transactionHelper(ctx, func(tx *gorm.DB) (map[string]orm.DIDChangeLog, error) { // check existence sqlDIDManager := NewDIDManager(tx) @@ -251,7 +251,7 @@ func (r *Manager) CreateService(ctx context.Context, subject string, service did services := make([]did.Service, 0) serviceIDFragment := NewIDForService(service) - err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DIDDocument) (*orm.DIDDocument, error) { + err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DidDocument) (*orm.DidDocument, error) { // use a generated ID where the fragment equals the hash of the service service.ID = id.URI() service.ID.Fragment = serviceIDFragment @@ -320,7 +320,7 @@ func (r *Manager) FindServices(_ context.Context, subject string, serviceType *s // DeleteService removes a service from the DID document identified by subjectDID. func (r *Manager) DeleteService(ctx context.Context, subject string, serviceID ssi.URI) error { - err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DIDDocument) (*orm.DIDDocument, error) { + err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DidDocument) (*orm.DidDocument, error) { j := 0 for i, s := range current.Services { sID, _ := ssi.ParseURI(s.ID) @@ -348,7 +348,7 @@ func (r *Manager) UpdateService(ctx context.Context, subject string, serviceID s // use a generated ID where the fragment equals the hash of the service serviceIDFragment := NewIDForService(service) - err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DIDDocument) (*orm.DIDDocument, error) { + err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DidDocument) (*orm.DidDocument, error) { j := 0 for i, s := range current.Services { sID, _ := ssi.ParseURI(s.ID) @@ -388,7 +388,7 @@ func (r *Manager) AddVerificationMethod(ctx context.Context, subject string, key verificationMethods := make([]did.VerificationMethod, 0) var vmIDs []string - err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DIDDocument) (*orm.DIDDocument, error) { + err := r.applyToDIDDocuments(ctx, subject, func(tx *gorm.DB, id did.DID, current *orm.DidDocument) (*orm.DidDocument, error) { // known limitation if keyUsage.Is(orm.KeyAgreementUsage) && id.Method == "web" { return nil, errors.New("key agreement not supported for did:web") @@ -464,7 +464,7 @@ func (r *Manager) transactionHelper(ctx context.Context, operation func(tx *gorm // Delete the DID Document versions for _, change := range changes { // will also remove changelog via cascade - if err := tx.Where("id = ?", change.DIDDocumentVersionID).Delete(&orm.DIDDocument{}).Error; err != nil { + if err := tx.Where("id = ?", change.DIDDocumentVersionID).Delete(&orm.DidDocument{}).Error; err != nil { return err } } @@ -491,7 +491,7 @@ func (r *Manager) transactionHelper(ctx context.Context, operation func(tx *gorm // applyToDIDDocuments is a helper function that applies an operation to all DID documents of a subject (1 per did method). // It uses transactionHelper to perform the operation in a transaction. // if the operation returns nil then no changes are made. -func (r *Manager) applyToDIDDocuments(ctx context.Context, subject string, operation func(tx *gorm.DB, id did.DID, current *orm.DIDDocument) (*orm.DIDDocument, error)) error { +func (r *Manager) applyToDIDDocuments(ctx context.Context, subject string, operation func(tx *gorm.DB, id did.DID, current *orm.DidDocument) (*orm.DidDocument, error)) error { return r.transactionHelper(ctx, func(tx *gorm.DB) (map[string]orm.DIDChangeLog, error) { eventLog := make(map[string]orm.DIDChangeLog) sqlDIDManager := NewDIDManager(tx) @@ -578,7 +578,7 @@ func (r *Manager) Rollback(ctx context.Context) { // if one failed, delete all document versions for this transaction_id if !committed { for _, change := range versionChanges { - err := tx.Where("id = ?", change.DIDDocumentVersionID).Delete(&orm.DIDDocument{}).Error + err := tx.Where("id = ?", change.DIDDocumentVersionID).Delete(&orm.DidDocument{}).Error if err != nil { return err } diff --git a/vdr/didsubject/manager_test.go b/vdr/didsubject/manager_test.go index c8c304ae4c..477df126a0 100644 --- a/vdr/didsubject/manager_test.go +++ b/vdr/didsubject/manager_test.go @@ -314,7 +314,7 @@ func TestManager_rollback(t *testing.T) { ID: "did:example:123", Subject: "subject", } - didDocument := orm.DIDDocument{ + didDocument := orm.DidDocument{ ID: "1", DidID: "did:example:123", UpdatedAt: time.Now().Add(-time.Hour).Unix(), @@ -344,7 +344,7 @@ func TestManager_rollback(t *testing.T) { assert.Len(t, didChangeLog, 0) // check removal of DIDDocument - didDocuments := make([]orm.DIDDocument, 0) + didDocuments := make([]orm.DidDocument, 0) require.NoError(t, db.Find(&didDocuments).Error) assert.Len(t, didDocuments, 0) }) @@ -365,7 +365,7 @@ func TestManager_rollback(t *testing.T) { assert.Len(t, didChangeLog, 1) // check existence of DIDDocument - didDocuments := make([]orm.DIDDocument, 0) + didDocuments := make([]orm.DidDocument, 0) require.NoError(t, db.Find(&didDocuments).Error) assert.Len(t, didDocuments, 1) }) @@ -386,7 +386,7 @@ func TestManager_rollback(t *testing.T) { assert.Len(t, didChangeLog, 0) // check existence of DIDDocument - didDocuments := make([]orm.DIDDocument, 0) + didDocuments := make([]orm.DidDocument, 0) require.NoError(t, db.Find(&didDocuments).Error) assert.Len(t, didDocuments, 1) }) @@ -398,7 +398,7 @@ func TestManager_rollback(t *testing.T) { ID: "did:example:321", Subject: "subject", } - didDocument2 := orm.DIDDocument{ + didDocument2 := orm.DidDocument{ ID: "2", DidID: "did:example:321", UpdatedAt: time.Now().Add(-time.Hour).Unix(), @@ -420,7 +420,7 @@ func TestManager_rollback(t *testing.T) { assert.Len(t, didChangeLog, 0) // check removal of DIDDocument - didDocuments := make([]orm.DIDDocument, 0) + didDocuments := make([]orm.DidDocument, 0) require.NoError(t, db.Find(&didDocuments).Error) assert.Len(t, didDocuments, 0) }) @@ -443,13 +443,13 @@ type testMethod struct { method string } -func (t testMethod) NewDocument(_ context.Context, _ orm.DIDKeyFlags) (*orm.DIDDocument, error) { +func (t testMethod) NewDocument(_ context.Context, _ orm.DIDKeyFlags) (*orm.DidDocument, error) { method := t.method if method == "" { method = "example" } id := fmt.Sprintf("did:%s:%s", method, uuid.New().String()) - return &orm.DIDDocument{DID: orm.DID{ID: id}}, t.error + return &orm.DidDocument{DID: orm.DID{ID: id}}, t.error } func (t testMethod) NewVerificationMethod(_ context.Context, controller did.DID, _ orm.DIDKeyFlags) (*did.VerificationMethod, error) { diff --git a/vdr/didweb/manager.go b/vdr/didweb/manager.go index fdab5a9039..f0734de40d 100644 --- a/vdr/didweb/manager.go +++ b/vdr/didweb/manager.go @@ -55,7 +55,7 @@ type Manager struct { tenantPath string } -func (m Manager) NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*orm.DIDDocument, error) { +func (m Manager) NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*orm.DidDocument, error) { newDID, _ := did.ParseDID(fmt.Sprintf("%s:%s:%s", m.rootDID.String(), m.tenantPath, uuid.New())) var sqlVerificationMethods []orm.VerificationMethod @@ -75,9 +75,9 @@ func (m Manager) NewDocument(ctx context.Context, keyFlags orm.DIDKeyFlags) (*or } } - // Create sql.DIDDocument + // Create sql.DidDocument now := time.Now().Unix() - sqlDoc := orm.DIDDocument{ + sqlDoc := orm.DidDocument{ DID: orm.DID{ ID: newDID.String(), },