-
Notifications
You must be signed in to change notification settings - Fork 566
/
spy_authenticator.go
209 lines (174 loc) · 5.71 KB
/
spy_authenticator.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
package testutils
import (
"encoding/json"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/osmosis-labs/osmosis/v25/x/smart-account/authenticator"
)
var _ authenticator.Authenticator = &SpyAuthenticator{}
type SpyTrackRequest struct {
AuthenticatorId string `json:"authenticator_id"`
Account sdk.AccAddress `json:"account"`
Msg authenticator.LocalAny `json:"msg"`
MsgIndex uint64 `json:"msg_index"`
}
type SpyAddRequest struct {
Account sdk.AccAddress `json:"account"`
Data []byte `json:"data"`
AuthenticatorId string `json:"authenticator_id"`
}
type SpyRemoveRequest struct {
Account sdk.AccAddress `json:"account"`
Data []byte `json:"data"`
AuthenticatorId string `json:"authenticator_id"`
}
type LatestCalls struct {
Authenticate authenticator.AuthenticationRequest
Track SpyTrackRequest
ConfirmExecution authenticator.AuthenticationRequest
OnAuthenticatorAdded SpyAddRequest
OnAuthenticatorRemoved SpyRemoveRequest
}
type FailureFlag = uint8
const (
AUTHENTICATE_FAIL FailureFlag = 1 << iota
CONFIRM_EXECUTION_FAIL
)
func Has(fixed, toCheck FailureFlag) bool {
return fixed&toCheck != 0
}
type SpyAuthenticatorData struct {
Name string `json:"name"`
Failure FailureFlag `json:"failure"` // bit flag representing authenticator failure
}
// SpyAuthenticator tracks latest call and can be used to test the authenticator
type SpyAuthenticator struct {
KvStoreKey storetypes.StoreKey
Name string
Failure FailureFlag
}
func NewSpyAuthenticator(kvStoreKey storetypes.StoreKey) SpyAuthenticator {
return SpyAuthenticator{KvStoreKey: kvStoreKey}
}
func (s SpyAuthenticator) Type() string {
return "Spy"
}
func (s SpyAuthenticator) StaticGas() uint64 {
return 1000
}
func (s SpyAuthenticator) Initialize(config []byte) (authenticator.Authenticator, error) {
var spyData SpyAuthenticatorData
err := json.Unmarshal(config, &spyData)
if err != nil {
return nil, err
}
s.Name = spyData.Name
s.Failure = spyData.Failure
return s, nil
}
func (s SpyAuthenticator) Authenticate(ctx sdk.Context, request authenticator.AuthenticationRequest) error {
s.UpdateLatestCalls(ctx, func(calls LatestCalls) LatestCalls {
calls.Authenticate = request
return calls
})
if Has(s.Failure, AUTHENTICATE_FAIL) {
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "not authenticated")
}
return nil
}
func (s SpyAuthenticator) Track(ctx sdk.Context, request authenticator.AuthenticationRequest) error {
s.UpdateLatestCalls(ctx, func(calls LatestCalls) LatestCalls {
calls.Track = SpyTrackRequest{
AuthenticatorId: request.AuthenticatorId,
Account: request.Account,
Msg: request.Msg,
MsgIndex: request.MsgIndex,
}
return calls
})
return nil
}
func (s SpyAuthenticator) ConfirmExecution(ctx sdk.Context, request authenticator.AuthenticationRequest) error {
// intentionlly call update before check to test state revert
s.UpdateLatestCalls(ctx, func(calls LatestCalls) LatestCalls {
calls.ConfirmExecution = request
return calls
})
if Has(s.Failure, CONFIRM_EXECUTION_FAIL) {
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "not authenticated")
}
return nil
}
func (s SpyAuthenticator) OnAuthenticatorAdded(ctx sdk.Context, account sdk.AccAddress, config []byte, authenticatorId string) error {
spy, err := s.Initialize(config)
if err != nil {
return err
}
spyAuth, ok := spy.(SpyAuthenticator)
if !ok {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "failed to cast authenticator to SpyAuthenticator")
}
spyAuth.UpdateLatestCalls(ctx, func(calls LatestCalls) LatestCalls {
calls.OnAuthenticatorAdded = SpyAddRequest{
Account: account,
Data: config,
AuthenticatorId: authenticatorId,
}
return calls
})
return nil
}
func (s SpyAuthenticator) OnAuthenticatorRemoved(ctx sdk.Context, account sdk.AccAddress, config []byte, authenticatorId string) error {
spy, err := s.Initialize(config)
if err != nil {
return err
}
spyAuth, ok := spy.(SpyAuthenticator)
if !ok {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "failed to cast authenticator to SpyAuthenticator")
}
spyAuth.UpdateLatestCalls(ctx, func(calls LatestCalls) LatestCalls {
calls.OnAuthenticatorRemoved = SpyRemoveRequest{
Account: account,
Data: config,
AuthenticatorId: authenticatorId,
}
return calls
})
return nil
}
func (s SpyAuthenticator) GetLatestCalls(ctx sdk.Context) LatestCalls {
if s.Name == "" {
panic("SpyAuthenticator is not initialized")
}
kvStore := s.storeByName(ctx)
bz := kvStore.Get([]byte("calls"))
var calls LatestCalls
_ = json.Unmarshal(bz, &calls)
return calls
}
func (s SpyAuthenticator) UpdateLatestCalls(ctx sdk.Context, f func(calls LatestCalls) LatestCalls) LatestCalls {
if s.Name == "" {
panic("SpyAuthenticator is not initialized")
}
kvStore := s.storeByName(ctx)
bz := kvStore.Get([]byte("calls"))
var calls LatestCalls
_ = json.Unmarshal(bz, &calls)
calls = f(calls)
newBz, _ := json.Marshal(calls)
kvStore.Set([]byte("calls"), newBz)
return calls
}
func (s SpyAuthenticator) ResetLatestCalls(ctx sdk.Context) {
s.store(ctx).Delete([]byte("calls"))
}
func (s SpyAuthenticator) store(ctx sdk.Context) storetypes.KVStore {
return prefix.NewStore(ctx.KVStore(s.KvStoreKey), []byte(s.Type()))
}
func (s SpyAuthenticator) storeByName(ctx sdk.Context) storetypes.KVStore {
return prefix.NewStore(ctx.KVStore(s.KvStoreKey), []byte(s.Name))
}