-
Notifications
You must be signed in to change notification settings - Fork 45
/
keeper_chain_links.go
97 lines (78 loc) · 3.17 KB
/
keeper_chain_links.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v3/x/profiles/types"
)
// SaveChainLink stores the given chain link
// Chain links are stored using two keys:
// 1. ChainLinkStoreKey (user + chain name + target) -> types.ChainLink
// 2. ChainLinkOwnerKey (chain name + target + user) -> 0x01
func (k Keeper) SaveChainLink(ctx sdk.Context, link types.ChainLink) error {
// Validate the chain link
err := link.Validate()
if err != nil {
return sdkerrors.Wrap(types.ErrInvalidChainLink, err.Error())
}
// Make sure the user has a profile
if !k.HasProfile(ctx, link.User) {
return sdkerrors.Wrap(types.ErrProfileNotFound, "a profile is required to link a chain")
}
// Validate the source address
srcAddrData, err := types.UnpackAddressData(k.cdc, link.Address)
if err != nil {
return err
}
err = srcAddrData.Validate()
if err != nil {
return sdkerrors.Wrap(types.ErrInvalidAddressData, err.Error())
}
// Verify the proof
err = link.Proof.Verify(k.cdc, k.legacyAmino, link.User, srcAddrData)
if err != nil {
return sdkerrors.Wrap(types.ErrInvalidProof, err.Error())
}
target := srcAddrData.GetValue()
if _, found := k.GetChainLink(ctx, link.User, link.ChainConfig.Name, target); found {
return types.ErrDuplicatedChainLink
}
// Store the data
store := ctx.KVStore(k.storeKey)
store.Set(types.ChainLinksStoreKey(link.User, link.ChainConfig.Name, target), types.MustMarshalChainLink(k.cdc, link))
store.Set(types.ChainLinkOwnerKey(link.ChainConfig.Name, target, link.User), []byte{0x01})
k.AfterChainLinkSaved(ctx, link)
return nil
}
// HasChainLink tells whether the given chain link exists or not
func (k Keeper) HasChainLink(ctx sdk.Context, owner, chainName, target string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.ChainLinksStoreKey(owner, chainName, target))
}
// GetChainLink returns the chain link for the given owner, chain name and target.
// If such link does not exist, returns false instead.
func (k Keeper) GetChainLink(ctx sdk.Context, owner, chainName, target string) (types.ChainLink, bool) {
store := ctx.KVStore(k.storeKey)
key := types.ChainLinksStoreKey(owner, chainName, target)
if !store.Has(key) {
return types.ChainLink{}, false
}
return types.MustUnmarshalChainLink(k.cdc, store.Get(key)), true
}
// DeleteChainLink deletes the link associated with the given address and chain name
func (k Keeper) DeleteChainLink(ctx sdk.Context, link types.ChainLink) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ChainLinksStoreKey(link.User, link.ChainConfig.Name, link.GetAddressData().GetValue()))
store.Delete(types.ChainLinkOwnerKey(link.ChainConfig.Name, link.GetAddressData().GetValue(), link.User))
k.AfterChainLinkDeleted(ctx, link)
}
// DeleteAllUserChainLinks deletes all the chain links associated with the given user
func (k Keeper) DeleteAllUserChainLinks(ctx sdk.Context, user string) {
var links []types.ChainLink
k.IterateUserChainLinks(ctx, user, func(index int64, link types.ChainLink) (stop bool) {
links = append(links, link)
return false
})
for _, link := range links {
k.DeleteChainLink(ctx, link)
}
}