-
Notifications
You must be signed in to change notification settings - Fork 574
/
all_of.go
144 lines (122 loc) · 4.28 KB
/
all_of.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
package authenticator
import (
"encoding/json"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
type AllOf struct {
SubAuthenticators []Authenticator
am *AuthenticatorManager
signatureAssignment SignatureAssignment
}
var _ Authenticator = &AllOf{}
func NewAllOf(am *AuthenticatorManager) AllOf {
return AllOf{
am: am,
SubAuthenticators: []Authenticator{},
signatureAssignment: Single,
}
}
func NewPartitionedAllOf(am *AuthenticatorManager) AllOf {
return AllOf{
am: am,
SubAuthenticators: []Authenticator{},
signatureAssignment: Partitioned,
}
}
func (aoa AllOf) Type() string {
if aoa.signatureAssignment == Single {
return "AllOf"
}
return "PartitionedAllOf"
}
func (aoa AllOf) StaticGas() uint64 {
var totalGas uint64
for _, auth := range aoa.SubAuthenticators {
totalGas += auth.StaticGas()
}
return totalGas
}
func (aoa AllOf) Initialize(config []byte) (Authenticator, error) {
var initDatas []SubAuthenticatorInitData
if err := json.Unmarshal(config, &initDatas); err != nil {
return nil, errorsmod.Wrap(err, "failed to parse sub-authenticators initialization data")
}
if len(initDatas) == 0 {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "no sub-authenticators provided")
}
for _, initData := range initDatas {
authenticatorCode := aoa.am.GetAuthenticatorByType(initData.Type)
instance, err := authenticatorCode.Initialize(initData.Config)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed to initialize sub-authenticator (type = %s)", initData.Type)
}
aoa.SubAuthenticators = append(aoa.SubAuthenticators, instance)
}
// If not all sub-authenticators are registered, return an error
if len(aoa.SubAuthenticators) != len(initDatas) {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "failed to initialize all sub-authenticators")
}
return aoa, nil
}
func (aoa AllOf) Authenticate(ctx sdk.Context, request AuthenticationRequest) error {
if len(aoa.SubAuthenticators) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "no sub-authenticators provided")
}
var signatures [][]byte
var err error
if aoa.signatureAssignment == Partitioned {
// Partitioned signatures are decoded and passed one by one as the signature of the sub-authenticator
signatures, err = splitSignatures(request.Signature, len(aoa.SubAuthenticators))
if err != nil {
return err
}
}
baseId := request.AuthenticatorId
for i, auth := range aoa.SubAuthenticators {
// update the authenticator id to include the sub-authenticator id
request.AuthenticatorId = compositeId(baseId, i)
// update the request to include the sub-authenticator signature
if aoa.signatureAssignment == Partitioned {
request.Signature = signatures[i]
}
if err := auth.Authenticate(ctx, request); err != nil {
return err
}
}
return nil
}
func (aoa AllOf) Track(ctx sdk.Context, request AuthenticationRequest) error {
return subTrack(ctx, request, aoa.SubAuthenticators)
}
func (aoa AllOf) ConfirmExecution(ctx sdk.Context, request AuthenticationRequest) error {
var signatures [][]byte
var err error
if aoa.signatureAssignment == Partitioned {
// Partitioned signatures are decoded and passed one by one as the signature of the sub-authenticator
signatures, err = splitSignatures(request.Signature, len(aoa.SubAuthenticators))
if err != nil {
return err
}
}
baseId := request.AuthenticatorId
for i, auth := range aoa.SubAuthenticators {
// update the authenticator id to include the sub-authenticator id
request.AuthenticatorId = compositeId(baseId, i)
// update the request to include the sub-authenticator signature
if aoa.signatureAssignment == Partitioned {
request.Signature = signatures[i]
}
if err := auth.ConfirmExecution(ctx, request); err != nil {
return err
}
}
return nil
}
func (aoa AllOf) OnAuthenticatorAdded(ctx sdk.Context, account sdk.AccAddress, config []byte, authenticatorId string) error {
return onSubAuthenticatorsAdded(ctx, account, config, authenticatorId, aoa.am)
}
func (aoa AllOf) OnAuthenticatorRemoved(ctx sdk.Context, account sdk.AccAddress, config []byte, authenticatorId string) error {
return onSubAuthenticatorsRemoved(ctx, account, config, authenticatorId, aoa.am)
}