-
Notifications
You must be signed in to change notification settings - Fork 175
/
transactionVerifier.go
235 lines (204 loc) · 6.75 KB
/
transactionVerifier.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
package fvm
import (
"fmt"
"github.com/opentracing/opentracing-go/log"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/programs"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
type signType int
const (
payloadSignature signType = 0
envelopeSignature signType = 1
)
type TransactionSignatureVerifier struct {
SignatureVerifier SignatureVerifier
KeyWeightThreshold int
}
func NewTransactionSignatureVerifier(keyWeightThreshold int) *TransactionSignatureVerifier {
return &TransactionSignatureVerifier{
SignatureVerifier: DefaultSignatureVerifier{},
KeyWeightThreshold: keyWeightThreshold,
}
}
func (v *TransactionSignatureVerifier) Process(
_ *VirtualMachine,
ctx *Context,
proc *TransactionProcedure,
sth *state.StateHolder,
programs *programs.Programs,
) error {
return v.verifyTransactionSignatures(proc, *ctx, sth)
}
func (v *TransactionSignatureVerifier) verifyTransactionSignatures(
proc *TransactionProcedure,
ctx Context,
sth *state.StateHolder,
) error {
if ctx.Tracer != nil && proc.TraceSpan != nil {
span := ctx.Tracer.StartSpanFromParent(proc.TraceSpan, trace.FVMVerifyTransaction)
span.LogFields(
log.String("transaction.ID", proc.ID.String()),
)
defer span.Finish()
}
tx := proc.Transaction
accounts := state.NewAccounts(sth)
if tx.Payer == flow.EmptyAddress {
err := errors.NewInvalidAddressErrorf(tx.Payer, "payer address is invalid")
return fmt.Errorf("transaction verification failed: %w", err)
}
var err error
var payloadWeights map[flow.Address]int
var proposalKeyVerifiedInPayload bool
payloadWeights, proposalKeyVerifiedInPayload, err = v.aggregateAccountSignatures(
accounts,
tx.PayloadSignatures,
tx.PayloadMessage(),
tx.ProposalKey,
payloadSignature,
)
if err != nil {
return errors.NewInvalidProposalSignatureError(tx.ProposalKey.Address, tx.ProposalKey.KeyIndex, err)
}
var envelopeWeights map[flow.Address]int
var proposalKeyVerifiedInEnvelope bool
envelopeWeights, proposalKeyVerifiedInEnvelope, err = v.aggregateAccountSignatures(
accounts,
tx.EnvelopeSignatures,
tx.EnvelopeMessage(),
tx.ProposalKey,
envelopeSignature,
)
if err != nil {
return errors.NewInvalidProposalSignatureError(tx.ProposalKey.Address, tx.ProposalKey.KeyIndex, err)
}
proposalKeyVerified := proposalKeyVerifiedInPayload || proposalKeyVerifiedInEnvelope
if !proposalKeyVerified {
err := fmt.Errorf("either the payload or the envelope should provide proposal signatures")
return errors.NewInvalidProposalSignatureError(tx.ProposalKey.Address, tx.ProposalKey.KeyIndex, err)
}
for _, addr := range tx.Authorizers {
// Skip this authorizer if it is also the payer. In the case where an account is
// both a PAYER as well as an AUTHORIZER or PROPOSER, that account is required
// to sign only the envelope.
if addr == tx.Payer {
continue
}
// hasSufficientKeyWeight
if !v.hasSufficientKeyWeight(payloadWeights, addr) {
msg := fmt.Sprintf("authorizer account does not have sufficient signatures (%d < %d)", payloadWeights[addr], v.KeyWeightThreshold)
return errors.NewAccountAuthorizationErrorf(addr, msg)
}
}
if !v.hasSufficientKeyWeight(envelopeWeights, tx.Payer) {
// TODO change this to payer error (needed for fees)
msg := fmt.Sprintf("payer account does not have sufficient signatures (%d < %d)", envelopeWeights[tx.Payer], v.KeyWeightThreshold)
return errors.NewAccountAuthorizationErrorf(tx.Payer, msg)
}
return nil
}
func (v *TransactionSignatureVerifier) aggregateAccountSignatures(
accounts *state.Accounts,
signatures []flow.TransactionSignature,
message []byte,
proposalKey flow.ProposalKey,
sType signType,
) (
weights map[flow.Address]int,
proposalKeyVerified bool,
err error,
) {
weights = make(map[flow.Address]int)
var accountKey *flow.AccountPublicKey
for _, txSig := range signatures {
accountKey, err = v.verifyAccountSignature(accounts, txSig, message, sType)
if err != nil {
return nil, false, err
}
if v.sigIsForProposalKey(txSig, proposalKey) {
proposalKeyVerified = true
}
weights[txSig.Address] += accountKey.Weight
}
return
}
// verifyAccountSignature verifies that an account signature is valid for the
// account and given message.
//
// If the signature is valid, this function returns the associated account key.
//
// An error is returned if the account does not contain a public key that
// correctly verifies the signature against the given message.
func (v *TransactionSignatureVerifier) verifyAccountSignature(
accounts *state.Accounts,
txSig flow.TransactionSignature,
message []byte,
sType signType,
) (*flow.AccountPublicKey, error) {
accountKey, err := accounts.GetPublicKey(txSig.Address, txSig.KeyIndex)
if err != nil {
if sType == envelopeSignature {
return nil, errors.NewInvalidEnvelopeSignatureError(txSig.Address, txSig.KeyIndex, err)
}
return nil, errors.NewInvalidPayloadSignatureError(txSig.Address, txSig.KeyIndex, err)
}
if accountKey.Revoked {
err = fmt.Errorf("account key has been revoked")
if sType == envelopeSignature {
return nil, errors.NewInvalidEnvelopeSignatureError(txSig.Address, txSig.KeyIndex, err)
}
return nil, errors.NewInvalidPayloadSignatureError(txSig.Address, txSig.KeyIndex, err)
}
validWithoutTag, err := v.SignatureVerifier.Verify(
txSig.Signature,
nil,
message,
accountKey.PublicKey,
accountKey.HashAlgo,
)
if err != nil {
if sType == envelopeSignature {
return nil, errors.NewInvalidEnvelopeSignatureError(txSig.Address, txSig.KeyIndex, err)
}
return nil, errors.NewInvalidPayloadSignatureError(txSig.Address, txSig.KeyIndex, err)
}
if validWithoutTag {
return &accountKey, nil
}
validWithTag, err := v.SignatureVerifier.Verify(
txSig.Signature,
flow.TransactionDomainTag[:],
message,
accountKey.PublicKey,
accountKey.HashAlgo,
)
if err != nil {
if sType == envelopeSignature {
return nil, errors.NewInvalidEnvelopeSignatureError(txSig.Address, txSig.KeyIndex, err)
}
return nil, errors.NewInvalidPayloadSignatureError(txSig.Address, txSig.KeyIndex, err)
}
if validWithTag {
return &accountKey, nil
}
err = fmt.Errorf("signature is not valid")
if sType == envelopeSignature {
return nil, errors.NewInvalidEnvelopeSignatureError(txSig.Address, txSig.KeyIndex, err)
}
return nil, errors.NewInvalidPayloadSignatureError(txSig.Address, txSig.KeyIndex, err)
}
func (v *TransactionSignatureVerifier) hasSufficientKeyWeight(
weights map[flow.Address]int,
address flow.Address,
) bool {
return weights[address] >= v.KeyWeightThreshold
}
func (v *TransactionSignatureVerifier) sigIsForProposalKey(
txSig flow.TransactionSignature,
proposalKey flow.ProposalKey,
) bool {
return txSig.Address == proposalKey.Address && txSig.KeyIndex == proposalKey.KeyIndex
}