-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
267 lines (223 loc) · 8 KB
/
keeper.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package keeper
import (
"errors"
"fmt"
"time"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/btcsuite/btcutil/base58"
"github.com/cosmos/cosmos-sdk/codec"
cryptosecp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/furya-official/fury-blockchain/x/entity/types"
entitycontracts "github.com/furya-official/fury-blockchain/x/entity/types/contracts"
iidkeeper "github.com/furya-official/fury-blockchain/x/iid/keeper"
iidtypes "github.com/furya-official/fury-blockchain/x/iid/types"
)
type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
memStoreKey sdk.StoreKey
IidKeeper iidkeeper.Keeper
WasmKeeper wasmtypes.ContractOpsKeeper
AccountKeeper authkeeper.AccountKeeper
ParamSpace paramstypes.Subspace
}
func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey, memStoreKey sdk.StoreKey, iidKeeper iidkeeper.Keeper, wasmKeeper wasmkeeper.Keeper,
accountKeeper authkeeper.AccountKeeper, paramSpace paramstypes.Subspace) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: key,
memStoreKey: memStoreKey,
IidKeeper: iidKeeper,
WasmKeeper: wasmkeeper.NewDefaultPermissionKeeper(wasmKeeper),
AccountKeeper: accountKeeper,
ParamSpace: paramSpace,
}
}
func (k Keeper) UpdateEntityConfig(ctx sdk.Context, key string, value string) error {
ctx.KVStore(k.storeKey).Set(nil, nil)
return nil
}
func (k Keeper) CreateEntity(ctx sdk.Context, msg *types.MsgCreateEntity) (types.MsgCreateEntityResponse, error) {
params := k.GetParams(ctx)
nftContractAddressParam := params.NftContractAddress
if len(nftContractAddressParam) == 0 {
return types.MsgCreateEntityResponse{}, errors.New("nftContractAddress not set")
}
nftContractAddress, err := sdk.AccAddressFromBech32(nftContractAddressParam)
if err != nil {
return types.MsgCreateEntityResponse{}, err
}
privKey := cryptosecp256k1.GenPrivKey()
pubKey := privKey.PubKey()
address, err := sdk.AccAddressFromBech32(params.NftContractMinter)
if err != nil {
return types.MsgCreateEntityResponse{}, err
}
account := k.AccountKeeper.NewAccount(ctx, authtypes.NewBaseAccount(address, pubKey, 0, 0))
entityId := fmt.Sprintf("did:fury:entity:%s:%s", msg.EntityType, base58.Encode(pubKey.Bytes()[:16]))
verification := iidtypes.NewVerification(
iidtypes.NewVerificationMethod(
entityId,
iidtypes.DID(entityId),
iidtypes.NewBlockchainAccountID(ctx.ChainID(), account.GetAddress().String()),
),
[]string{iidtypes.Authentication},
nil,
)
defaultVerification := iidtypes.NewVerification(
iidtypes.NewVerificationMethod(
iidtypes.DID(entityId).NewVerificationMethodID(account.GetAddress().String()),
iidtypes.DID(entityId),
iidtypes.NewBlockchainAccountID(ctx.ChainID(), account.GetAddress().String()),
),
[]string{iidtypes.Authentication},
nil,
)
did, err := iidtypes.NewDidDocument(entityId,
iidtypes.WithServices(msg.Service...),
iidtypes.WithRights(msg.AccordedRight...),
iidtypes.WithResources(msg.LinkedResource...),
iidtypes.WithVerifications(append(msg.Verification, defaultVerification, verification)...),
iidtypes.WithControllers(append(msg.Controller, entityId, msg.OwnerDid.Did())...),
)
if err != nil {
// k.Logger(ctx).Error(err.Error())
return types.MsgCreateEntityResponse{}, err
}
// check that the did is not already taken
_, found := k.IidKeeper.GetDidDocument(ctx, []byte(entityId))
if found {
err := sdkerrors.Wrapf(iidtypes.ErrDidDocumentFound, "a document with did %s already exists", entityId)
// k.Logger(ctx).Error(err.Error())
return types.MsgCreateEntityResponse{}, err
}
// persist the did document
currentTimeUtc := time.Now().UTC()
// now create and persist the metadata
did.Context = msg.Context
didM := iidtypes.NewDidMetadata(ctx.TxBytes(), ctx.BlockTime())
didM.Id = entityId
didM.EntityType = msg.EntityType
didM.Deactivated = msg.Deactivated
didM.Created = ¤tTimeUtc
didM.Updated = ¤tTimeUtc
didM.VersionId = fmt.Sprintf("%s:%d", entityId, 0)
didM.Stage = msg.Stage
didM.Credentials = msg.VerifiableCredential
didM.VerifiableCredential = msg.VerificationStatus
didM.StartDate = msg.StartDate
didM.EndDate = msg.EndDate
didM.RelayerNode = msg.RelayerNode
k.IidKeeper.SetDidDocument(ctx, []byte(entityId), did)
k.IidKeeper.SetDidMetadata(ctx, []byte(entityId), didM)
//nftAddresBytes, err := k.GetEntityConfig(ctx, types.ConfigNftContractAddress)
nftMint := entitycontracts.WasmMsgMint{
Mint: entitycontracts.Mint{
TokenId: did.Id,
Owner: msg.OwnerAddress,
TokenUri: did.Id,
Extension: msg.Data,
},
}
finalMessage, err := nftMint.Marshal()
if err != nil {
return types.MsgCreateEntityResponse{}, err
}
_, err = k.WasmKeeper.Execute(ctx, nftContractAddress, address, finalMessage, sdk.NewCoins(sdk.NewCoin("ufury", sdk.ZeroInt())))
if err != nil {
return types.MsgCreateEntityResponse{}, err
}
// emit the event
if err := ctx.EventManager().EmitTypedEvents(iidtypes.NewIidDocumentCreatedEvent(entityId, msg.OwnerDid.Did())); err != nil {
// k.Logger(ctx).Error("failed to emit DidDocumentCreatedEvent", "did", msg.Id, "signer", msg.Signer, "err", err)
return types.MsgCreateEntityResponse{}, err
}
resp := types.MsgCreateEntityResponse{
EntityId: entityId,
EntityType: didM.EntityType,
EntityStatus: didM.Status,
}
return resp, err
}
func (k Keeper) TransferEntity(ctx sdk.Context, msg *types.MsgTransferEntity) (*types.MsgTransferEntityResponse, error) {
params := k.GetParams(ctx)
nftContractAddressParam := params.NftContractAddress
if len(nftContractAddressParam) == 0 {
return nil, errors.New("nftContractAddress not set")
}
controllerAddress, err := sdk.AccAddressFromBech32(msg.OwnerAddress)
if err != nil {
return nil, err
}
recipientDidDoc, found := k.IidKeeper.GetDidDocument(ctx, []byte(msg.RecipientDid))
if !found {
return nil, errors.New("recipient did not found")
}
recipientAddress, err := recipientDidDoc.GetVerificationMethodBlockchainAddress(recipientDidDoc.Id)
if err != nil {
return nil, err
}
nftContractAddress, err := sdk.AccAddressFromBech32(nftContractAddressParam)
if err != nil {
return nil, err
}
err = iidkeeper.ExecuteOnDidWithRelationships(
ctx,
&k.IidKeeper,
[]string{iidtypes.Authentication},
msg.EntityDid,
msg.OwnerDid.Did(),
func(document *iidtypes.IidDocument) error {
document.Controller = []string{
document.Id,
msg.RecipientDid.Did(),
}
return nil
},
)
if err != nil {
return nil, err
}
nftTranferMsg := entitycontracts.WasmMsgTransferNft{
TransferNft: entitycontracts.TransferNft{
TokenId: msg.EntityDid,
Recipient: recipientAddress.String(),
},
}
finalMessage, err := nftTranferMsg.Marshal()
if err != nil {
return nil, err
}
_, err = k.WasmKeeper.Execute(ctx, nftContractAddress, controllerAddress, finalMessage, sdk.NewCoins(sdk.NewCoin("ufury", sdk.ZeroInt())))
if err != nil {
return nil, err
}
return &types.MsgTransferEntityResponse{}, err
}
// GetParams returns the total set of project parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.ParamSpace.GetParamSet(ctx, ¶ms)
return params
}
// SetParams sets the total set of project parameters.
func (k Keeper) SetParams(ctx sdk.Context, params *types.Params) {
k.ParamSpace.SetParamSet(ctx, params)
}
func (k Keeper) GetEntityDocIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.EntityKey)
}
func (k Keeper) EntityExists(ctx sdk.Context, entityDid string) bool {
// store := ctx.KVStore(k.storeKey)
_, exists := k.IidKeeper.GetDidDocument(ctx, []byte(entityDid))
return exists
}