-
Notifications
You must be signed in to change notification settings - Fork 41
/
stateValue.go
67 lines (52 loc) · 1.63 KB
/
stateValue.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
package v1
import (
"errors"
"reflect"
"time"
"github.com/cheqd/cheqd-node/x/did/utils"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
)
// StateValueData is interface uniting possible types to be used for stateValue.data field
type StateValueData interface {
proto.Message
}
var _ types.UnpackInterfacesMessage = &StateValue{}
func (m *StateValue) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var data StateValueData
return unpacker.UnpackAny(m.Data, &data)
}
func NewStateValue(data StateValueData, metadata *Metadata) (StateValue, error) {
any, err := types.NewAnyWithValue(data)
if err != nil {
return StateValue{}, err
}
return StateValue{Data: any, Metadata: metadata}, nil
}
func NewMetadataFromContext(ctx sdk.Context) Metadata {
created := ctx.BlockTime().Format(time.RFC3339)
txHash := utils.GetTxHash(ctx.TxBytes())
return Metadata{Created: created, Deactivated: false, VersionId: txHash}
}
func (m *Metadata) Update(ctx sdk.Context) {
m.Updated = ctx.BlockTime().Format(time.RFC3339)
}
func (m StateValue) UnpackData() (StateValueData, error) {
value, isOk := m.Data.GetCachedValue().(StateValueData)
if !isOk {
return nil, errors.New("failed to unpack state value data: " + reflect.TypeOf(m.Data.GetCachedValue()).String())
}
return value, nil
}
func (m StateValue) UnpackDataAsDid() (*Did, error) {
data, err := m.UnpackData()
if err != nil {
return nil, err
}
value, isValue := data.(*Did)
if !isValue {
return nil, errors.New("failed to unpack state value data as did: " + reflect.TypeOf(data).String())
}
return value, nil
}