forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.go
85 lines (69 loc) · 2.5 KB
/
persistence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package didexchange
import (
"fmt"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/store/connection"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/store/did"
)
const (
myNSPrefix = "my"
// TODO: https://github.com/hyperledger/aries-framework-go/issues/556 It will not be constant, this namespace
// will need to be figured with verification key
theirNSPrefix = "their"
)
// newConnectionStore returns new connection store instance.
func newConnectionStore(p provider) (*connectionStore, error) {
recorder, err := connection.NewRecorder(p)
if err != nil {
return nil, fmt.Errorf("failed to initialize connection recorder: %w", err)
}
didConnStore, err := did.NewConnectionStore(p)
if err != nil {
return nil, fmt.Errorf("failed to initialize did connection store: %w", err)
}
return &connectionStore{Recorder: recorder, ConnectionStore: didConnStore}, nil
}
// connectionStore takes care of connection and DID related persistence features
// TODO merge connection stores [Issue #1004].
type connectionStore struct {
*connection.Recorder
*did.ConnectionStore
}
// saveConnectionRecord saves the connection record against the connection id in the store.
func (c *connectionStore) saveConnectionRecord(record *connection.Record) error {
err := c.SaveConnectionRecord(record)
if err != nil {
return fmt.Errorf(" failed to save connection record : %w", err)
}
// myDID may be empty if a record is being saved when a didexchange request is received
if record.MyDID != "" {
err := c.SaveDIDByResolving(record.MyDID)
if err != nil {
return fmt.Errorf("failed to save myDID by resolving : %w", err)
}
}
// theirDID may not be empty, such as when an incoming didexchange request is received
if record.State == StateIDCompleted {
if err := c.SaveDIDByResolving(record.TheirDID, record.RecipientKeys...); err != nil {
return fmt.Errorf("failed to save theirDID by resolving : %w", err)
}
}
return nil
}
// saveConnectionRecordWithMapping saves newly created connection record against the connection id in the store
// and it creates mapping from namespaced ThreadID to connection ID.
func (c *connectionStore) saveConnectionRecordWithMapping(record *connection.Record) error {
err := c.SaveConnectionRecordWithMappings(record)
if err != nil {
return err
}
if record.MyDID != "" {
if err := c.SaveDIDByResolving(record.MyDID); err != nil {
return err
}
}
return nil
}