Skip to content

Commit 7104903

Browse files
committed
Minor cleanups for new helpers.
- Ensure errors are properly wrapped with `%w` - Error strings should not starts with caps. - Add missing params to "Bad signature" error log
1 parent 4d4f9c8 commit 7104903

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

webapi/helpers.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func validateSignature(hash, commitmentAddress, signature, message string) error
112112
// first check if we have an alternate sign address for this ticket.
113113
altSigData, err := db.AltSignAddrData(hash)
114114
if err != nil {
115-
return fmt.Errorf("db.AltSignAddrData failed: %v", err)
115+
return fmt.Errorf("db.AltSignAddrData failed: %w", err)
116116
}
117117

118118
// If we have no alternate sign address, or if validating with the
@@ -158,7 +158,7 @@ func validateTicketHash(hash string) error {
158158
}
159159
_, err := chainhash.NewHashFromStr(hash)
160160
if err != nil {
161-
return fmt.Errorf("invalid hash: %v", err)
161+
return fmt.Errorf("invalid hash: %w", err)
162162

163163
}
164164

@@ -168,31 +168,25 @@ func validateTicketHash(hash string) error {
168168
// getCommitmentAddress gets the commitment address of the provided ticket hash
169169
// from the chain.
170170
func getCommitmentAddress(hash string, dcrdClient *rpc.DcrdRPC) (string, error) {
171-
var commitmentAddress string
172171
resp, err := dcrdClient.GetRawTransaction(hash)
173172
if err != nil {
174-
return commitmentAddress, fmt.Errorf("dcrd.GetRawTransaction for ticket failed: %v", err)
175-
173+
return "", fmt.Errorf("dcrd.GetRawTransaction for ticket failed: %w", err)
176174
}
177175

178176
msgTx, err := decodeTransaction(resp.Hex)
179177
if err != nil {
180-
return commitmentAddress, fmt.Errorf("Failed to decode ticket hex: %v", err)
181-
178+
return "", fmt.Errorf("failed to decode ticket hex: %w", err)
182179
}
183180

184181
err = isValidTicket(msgTx)
185182
if err != nil {
186-
return commitmentAddress, fmt.Errorf("Invalid ticket: %w", errInvalidTicket)
187-
183+
return "", fmt.Errorf("invalid ticket: %w", errInvalidTicket)
188184
}
189185

190186
addr, err := stake.AddrFromSStxPkScrCommitment(msgTx.TxOut[1].PkScript, cfg.NetParams)
191187
if err != nil {
192-
return commitmentAddress, fmt.Errorf("AddrFromSStxPkScrCommitment error: %v", err)
193-
188+
return "", fmt.Errorf("AddrFromSStxPkScrCommitment error: %w", err)
194189
}
195190

196-
commitmentAddress = addr.String()
197-
return commitmentAddress, nil
191+
return addr.String(), nil
198192
}

webapi/middleware.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ func broadcastTicket() gin.HandlerFunc {
143143
// Ensure the provided ticket hex is a valid ticket.
144144
msgTx, err := decodeTransaction(request.TicketHex)
145145
if err != nil {
146-
log.Errorf("%s: Failed to decode ticket hex (ticketHash=%s): %v", funcName, request.TicketHash, err)
146+
log.Errorf("%s: Failed to decode ticket hex (ticketHash=%s): %v",
147+
funcName, request.TicketHash, err)
147148
sendErrorWithMsg("cannot decode ticket hex", errBadRequest, c)
148149
return
149150
}
@@ -302,7 +303,6 @@ func vspAuth() gin.HandlerFunc {
302303

303304
// If the ticket was found in the database, we already know its
304305
// commitment address. Otherwise we need to get it from the chain.
305-
var commitmentAddress string
306306
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
307307
dcrdErr := c.MustGet(dcrdErrorKey)
308308
if dcrdErr != nil {
@@ -311,34 +311,39 @@ func vspAuth() gin.HandlerFunc {
311311
return
312312
}
313313

314+
var commitmentAddress string
314315
if ticketFound {
315316
commitmentAddress = ticket.CommitmentAddress
316317
} else {
317318
commitmentAddress, err = getCommitmentAddress(hash, dcrdClient)
318319
if err != nil {
320+
log.Errorf("%s: Failed to get commitment address (clientIP=%s, ticketHash=%s): %v",
321+
funcName, c.ClientIP(), hash, err)
322+
319323
var apiErr *apiError
320324
if errors.Is(err, apiErr) {
321325
sendError(errInvalidTicket, c)
322326
} else {
323327
sendError(errInternalError, c)
324328
}
325-
log.Errorf("%s: (clientIP: %s, ticketHash: %s): %v", funcName, c.ClientIP(), hash, err)
329+
326330
return
327331
}
328332
}
329333

330334
// Ensure a signature is provided.
331335
signature := c.GetHeader("VSP-Client-Signature")
332336
if signature == "" {
333-
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
337+
log.Warnf("%s: No VSP-Client-Signature header (clientIP=%s)", funcName, c.ClientIP())
334338
sendErrorWithMsg("no VSP-Client-Signature header", errBadRequest, c)
335339
return
336340
}
337341

338342
// Validate request signature to ensure ticket ownership.
339343
err = validateSignature(hash, commitmentAddress, signature, string(reqBytes))
340344
if err != nil {
341-
log.Errorf("%s: Bad signature (clientIP=%s, ticketHash=%s): %v", funcName, err)
345+
log.Errorf("%s: Couldn't validate signature (clientIP=%s, ticketHash=%s): %v",
346+
funcName, c.ClientIP(), hash, err)
342347
sendError(errBadSignature, c)
343348
return
344349
}

0 commit comments

Comments
 (0)