-
Notifications
You must be signed in to change notification settings - Fork 2
/
keeper.go
117 lines (97 loc) · 3.28 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
package nickname
import (
"github.com/hdac-io/friday/codec"
"github.com/hdac-io/friday/x/auth"
sdk "github.com/hdac-io/friday/types"
)
/*
Requirements:
- Provide account service with readable ID
Impl:
- [Readable ID : Public key] matching logic
- Key checking logic for account login
- Key change logic
- Duplicate readable ID check
*/
// NicknameKeeper is a store of all the account we've seen,
// and amino codec
// Will further check more why store struct should contain codec
type NicknameKeeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
AccountKeeper auth.AccountKeeper
}
// NewNicknameKeeper returns AccountStore DB object
func NewNicknameKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, k auth.AccountKeeper) NicknameKeeper {
return NicknameKeeper{
storeKey: storeKey,
cdc: cdc,
AccountKeeper: k,
}
}
// GetUnitAccount fetches the AccountInfo with the given unit account data
// If not found, acc.UnitAccount is nil.
func (k *NicknameKeeper) GetUnitAccount(ctx sdk.Context, name string) UnitAccount {
st := ctx.KVStore(k.storeKey)
if !st.Has([]byte(name)) {
return UnitAccount{}
}
val := st.Get([]byte(name))
var acc UnitAccount
k.cdc.MustUnmarshalBinaryBare(val, &acc)
return acc
}
// SetNickname adds the given unit account to the database.
// It returns false if the account is already stored.
func (k *NicknameKeeper) SetNickname(ctx sdk.Context, name string, address sdk.AccAddress) bool {
// check if we already have seen it
acc := k.GetUnitAccount(ctx, name)
if acc.Nickname.MustToString() != "" {
return false
}
// Constructring & Marshal
acc = NewUnitAccount(NewName(name), address)
accBytes := k.cdc.MustMarshalBinaryBare(acc)
// add it to the store
st := ctx.KVStore(k.storeKey)
st.Set([]byte(name), accBytes)
return true
}
// ChangeKey updates public key of the account and apply to the database
func (k *NicknameKeeper) ChangeKey(ctx sdk.Context, name string, oldAddr, newAddr sdk.AccAddress) bool {
// check if we already have seen it
acc := k.GetUnitAccount(ctx, name)
if acc.Address.String() != oldAddr.String() {
return false
}
k.SetAccountIfNotExists(ctx, newAddr)
acc = NewUnitAccount(NewName(name), newAddr)
accBytes := k.cdc.MustMarshalBinaryBare(acc)
// add it to the store
st := ctx.KVStore(k.storeKey)
st.Set([]byte(name), accBytes)
return true
}
// AddrCheck checks account by given address
func (k *NicknameKeeper) AddrCheck(ctx sdk.Context, name string, address sdk.AccAddress) bool {
acc := k.GetUnitAccount(ctx, name)
strName := acc.Nickname.MustToString()
if acc.Address.String() == address.String() && strName != "" {
return true
}
return false
}
// GetAccountIterator get iterator for listting all accounts.
func (k *NicknameKeeper) GetAccountIterator(ctx sdk.Context) sdk.Iterator {
str := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(str, nil)
}
// SetAccountIfNotExists runs if network has no given account
func (k NicknameKeeper) SetAccountIfNotExists(ctx sdk.Context, addr sdk.AccAddress) {
// Recepient account existence check, if not, create one
toAddressAccountObject := k.AccountKeeper.GetAccount(ctx, addr)
if toAddressAccountObject == nil {
toAddressAccountObject = k.AccountKeeper.NewAccountWithAddress(ctx, addr)
k.AccountKeeper.SetAccount(ctx, toAddressAccountObject)
}
}