/
entity.go
143 lines (119 loc) · 3.81 KB
/
entity.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package keeper
import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ixofoundation/ixo-blockchain/v2/x/entity/types"
nft "github.com/ixofoundation/ixo-blockchain/v2/x/entity/types/contracts"
iidTypes "github.com/ixofoundation/ixo-blockchain/v2/x/iid/types"
)
func (k Keeper) SetEntity(ctx sdk.Context, key []byte, meta types.Entity) {
k.Set(ctx, key, types.EntityKey, meta, k.Marshal)
}
func (k Keeper) GetEntity(ctx sdk.Context, key []byte) (types.Entity, bool) {
val, found := k.Get(ctx, key, types.EntityKey, k.UnmarshalEntity)
return val.(types.Entity), found
}
func (k Keeper) UnmarshalEntity(value []byte) (interface{}, bool) {
data := types.Entity{}
k.Unmarshal(value, &data)
return data, types.IsValidEntity(&data)
}
// ResolveEntity returns the Entity and IidDocument
func (k Keeper) ResolveEntity(ctx sdk.Context, entityId string) (iidDocument iidTypes.IidDocument, entity types.Entity, err error) {
iidDocument, err = k.IidKeeper.ResolveDid(ctx, iidTypes.DID(entityId))
if err != nil {
return
}
entity, found := k.GetEntity(ctx, []byte(entityId))
if !found {
err = types.ErrEntityNotFound
}
return
}
func (k Keeper) Marshal(value interface{}) (bytes []byte) {
switch value := value.(type) {
case types.Entity:
bytes = k.cdc.MustMarshal(&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.Unmarshal(data, val); err != nil {
return false
}
return true
}
// GetAllEntityWithCondition retrieve a list of
// entitiy docs by some arbitrary criteria.
func (k Keeper) GetAllEntityWithCondition(
ctx sdk.Context,
key []byte,
entitySelector func(entity types.Entity) bool,
) (entities []types.Entity) {
iterator := k.GetAll(ctx, key)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
entity, _ := k.UnmarshalEntity(iterator.Value())
entityTyped := entity.(types.Entity)
if entitySelector(entityTyped) {
entities = append(entities, entityTyped)
}
}
return entities
}
// GetAllEntity returns all the Entity docs
func (k Keeper) GetAllEntity(ctx sdk.Context) []types.Entity {
return k.GetAllEntityWithCondition(
ctx,
types.EntityKey,
func(did types.Entity) bool { return true },
)
}
// Create a module account for entity id and name of account as fragemnt in form: did#name
func (k Keeper) CreateNewAccount(ctx sdk.Context, entityId, name string) (sdk.AccAddress, error) {
address := types.GetModuleAccountAddress(entityId, name)
if k.AccountKeeper.GetAccount(ctx, address) != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "account already exists")
}
account := k.AccountKeeper.NewAccountWithAddress(ctx, address)
k.AccountKeeper.SetAccount(ctx, account)
return account.GetAddress(), nil
}
// checks if the provided address is the owner on the smart contract
func (k Keeper) CheckIfOwner(ctx sdk.Context, entityId, ownerAddress string) error {
// get cw721 contract address
params := k.GetParams(ctx)
nftContractAddress, err := sdk.AccAddressFromBech32(params.NftContractAddress)
if err != nil {
return err
}
// create the nft cw721 query
queryMessage, err := nft.Marshal(nft.WasmQueryOwnerOf{
OwnerOf: nft.OwnerOf{
TokenId: entityId,
},
})
if err != nil {
return err
}
// query smart contract
ownerOfBytes, err := k.WasmViewKeeper.QuerySmart(ctx, nftContractAddress, queryMessage)
if err != nil {
return err
}
var ownerOf nft.OwnerOfResponse
if err := json.Unmarshal([]byte(ownerOfBytes), &ownerOf); err != nil {
return err
}
// check if token owner is owner provided
if ownerOf.Owner == ownerAddress {
return nil
}
return types.ErrEntityUnauthorized
}