-
Notifications
You must be signed in to change notification settings - Fork 12
/
did.go
94 lines (78 loc) · 2.69 KB
/
did.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
86
87
88
89
90
91
92
93
94
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/allinbits/cosmos-cash/x/did/types"
)
func (k Keeper) SetDidDocument(ctx sdk.Context, key []byte, document types.DidDocument) {
k.Set(ctx, key, types.DidDocumentKey, document, k.Marshal)
}
func (k Keeper) GetDidDocument(ctx sdk.Context, key []byte) (types.DidDocument, bool) {
val, found := k.Get(ctx, key, types.DidDocumentKey, k.UnmarshalDidDocument)
return val.(types.DidDocument), found
}
// UnmarshalDidDocument unmarshall a did document= and check if it is empty
// ad DID document is empty if contains no context
func (k Keeper) UnmarshalDidDocument(value []byte) (interface{}, bool) {
data := types.DidDocument{}
k.Unmarshal(value, &data)
return data, types.IsValidDIDDocument(&data)
}
func (k Keeper) SetDidMetadata(ctx sdk.Context, key []byte, meta types.DidMetadata) {
k.Set(ctx, key, types.DidMetadataKey, meta, k.Marshal)
}
func (k Keeper) GetDidMetadata(ctx sdk.Context, key []byte) (types.DidMetadata, bool) {
val, found := k.Get(ctx, key, types.DidMetadataKey, k.UnmarshalDidMetadata)
return val.(types.DidMetadata), found
}
func (k Keeper) UnmarshalDidMetadata(value []byte) (interface{}, bool) {
data := types.DidMetadata{}
k.Unmarshal(value, &data)
return data, types.IsValidDIDMetadata(&data)
}
func (k Keeper) Marshal(value interface{}) (bytes []byte) {
switch value := value.(type) {
case types.DidDocument:
bytes, _ = k.cdc.MarshalBinaryBare(&value)
case types.DidMetadata:
bytes, _ = k.cdc.MarshalBinaryBare(&value)
}
return
}
// Unmarshal unmarshal a byte slice to a struct, return false in case of errors
func (k Keeper) Unmarshal(data []byte, val codec.ProtoMarshaler) bool {
if len(data) == 0 {
return false
}
if err := k.cdc.UnmarshalBinaryBare(data, val); err != nil {
return false
}
return true
}
// GetAllDidDocumentsWithCondition retrieve a list of
// did document by some arbitrary criteria. The selector filter has access
// to both the did and it's metadata
func (k Keeper) GetAllDidDocumentsWithCondition(
ctx sdk.Context,
key []byte,
didSelector func(did types.DidDocument) bool,
) (didDocs []types.DidDocument) {
iterator := k.GetAll(ctx, key)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
did, _ := k.UnmarshalDidDocument(iterator.Value())
didTyped := did.(types.DidDocument)
if didSelector(didTyped) {
didDocs = append(didDocs, didTyped)
}
}
return didDocs
}
// GetAllDidDocuments returns all the DidDocuments
func (k Keeper) GetAllDidDocuments(ctx sdk.Context) []types.DidDocument {
return k.GetAllDidDocumentsWithCondition(
ctx,
types.DidDocumentKey,
func(did types.DidDocument) bool { return true },
)
}