@@ -11,8 +11,6 @@ import (
1111 "net/http"
1212 "strings"
1313
14- "github.com/decred/dcrd/blockchain/stake/v4"
15- "github.com/decred/dcrd/chaincfg/chainhash"
1614 "github.com/decred/vspd/rpc"
1715 "github.com/gin-gonic/gin"
1816 "github.com/gin-gonic/gin/binding"
@@ -287,17 +285,9 @@ func vspAuth() gin.HandlerFunc {
287285 hash := request .TicketHash
288286
289287 // Before hitting the db or any RPC, ensure this is a valid ticket hash.
290- // A ticket hash should be 64 chars (MaxHashStringSize) and should parse
291- // into a chainhash.Hash without error.
292- if len (hash ) != chainhash .MaxHashStringSize {
293- log .Errorf ("%s: Incorrect hash length (clientIP=%s): got %d, expected %d" ,
294- funcName , c .ClientIP (), len (hash ), chainhash .MaxHashStringSize )
295- sendErrorWithMsg ("invalid ticket hash" , errBadRequest , c )
296- return
297- }
298- _ , err = chainhash .NewHashFromStr (hash )
288+ err = validateTicketHash (hash )
299289 if err != nil {
300- log .Errorf ("%s: Invalid hash (clientIP=%s): %v" , funcName , c .ClientIP (), err )
290+ log .Errorf ("%s: Bad request (clientIP=%s): %v" , funcName , c .ClientIP (), err )
301291 sendErrorWithMsg ("invalid ticket hash" , errBadRequest , c )
302292 return
303293 }
@@ -313,67 +303,44 @@ func vspAuth() gin.HandlerFunc {
313303 // If the ticket was found in the database, we already know its
314304 // commitment address. Otherwise we need to get it from the chain.
315305 var commitmentAddress string
306+ dcrdClient := c .MustGet (dcrdKey ).(* rpc.DcrdRPC )
307+ dcrdErr := c .MustGet (dcrdErrorKey )
308+ if dcrdErr != nil {
309+ log .Errorf ("%s: could not get dcrd client: %v" , funcName , dcrdErr .(error ))
310+ sendError (errInternalError , c )
311+ return
312+ }
313+
316314 if ticketFound {
317315 commitmentAddress = ticket .CommitmentAddress
318316 } else {
319- dcrdClient := c .MustGet (dcrdKey ).(* rpc.DcrdRPC )
320- dcrdErr := c .MustGet (dcrdErrorKey )
321- if dcrdErr != nil {
322- log .Errorf ("%s: could not get dcrd client: %v" , funcName , dcrdErr .(error ))
323- sendError (errInternalError , c )
324- return
325- }
326-
327- resp , err := dcrdClient .GetRawTransaction (hash )
317+ commitmentAddress , err = getCommitmentAddress (hash , dcrdClient )
328318 if err != nil {
329- log .Errorf ("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v" , funcName , hash , err )
330- sendError (errInternalError , c )
331- return
332- }
333-
334- msgTx , err := decodeTransaction (resp .Hex )
335- if err != nil {
336- log .Errorf ("%s: Failed to decode ticket hex (ticketHash=%s): %v" , funcName , ticket .Hash , err )
337- sendError (errInternalError , c )
338- return
339- }
340-
341- err = isValidTicket (msgTx )
342- if err != nil {
343- log .Warnf ("%s: Invalid ticket (clientIP=%s, ticketHash=%s): %v" , funcName , c .ClientIP (), hash , err )
344- sendError (errInvalidTicket , c )
345- return
346- }
347-
348- addr , err := stake .AddrFromSStxPkScrCommitment (msgTx .TxOut [1 ].PkScript , cfg .NetParams )
349- if err != nil {
350- log .Errorf ("%s: AddrFromSStxPkScrCommitment error (ticketHash=%s): %v" , funcName , hash , err )
351- sendError (errInternalError , c )
319+ var apiErr * apiError
320+ if errors .Is (err , apiErr ) {
321+ sendError (errInvalidTicket , c )
322+ } else {
323+ sendError (errInternalError , c )
324+ }
325+ log .Errorf ("%s: (clientIP: %s, ticketHash: %s): %v" , funcName , c .ClientIP (), hash , err )
352326 return
353327 }
328+ }
354329
355- commitmentAddress = addr .String ()
330+ // Ensure a signature is provided.
331+ signature := c .GetHeader ("VSP-Client-Signature" )
332+ if signature == "" {
333+ log .Warnf ("%s: Bad request (clientIP=%s): %v" , funcName , c .ClientIP (), err )
334+ sendErrorWithMsg ("no VSP-Client-Signature header" , errBadRequest , c )
335+ return
356336 }
357337
358338 // Validate request signature to ensure ticket ownership.
359- err = validateSignature (reqBytes , commitmentAddress , c )
339+ err = validateSignature (hash , commitmentAddress , signature , string ( reqBytes ) )
360340 if err != nil {
361- // Don't return an error straight away if sig validation fails -
362- // first check if we have an alternate sign address for this ticket.
363- altSigData , err := db .AltSignAddrData (hash )
364- if err != nil {
365- log .Errorf ("%s: db.AltSignAddrData failed (ticketHash=%s): %v" , funcName , hash , err )
366- sendError (errInternalError , c )
367- return
368- }
369-
370- // If we have no alternate sign address, or if validating with the
371- // alt sign addr fails, return an error to the client.
372- if altSigData == nil || validateSignature (reqBytes , altSigData .AltSignAddr , c ) != nil {
373- log .Warnf ("%s: Bad signature (clientIP=%s, ticketHash=%s)" , funcName , c .ClientIP (), hash )
374- sendError (errBadSignature , c )
375- return
376- }
341+ log .Errorf ("%s: Bad signature (clientIP=%s, ticketHash=%s): %v" , funcName , err )
342+ sendError (errBadSignature , c )
343+ return
377344 }
378345
379346 // Add ticket information to context so downstream handlers don't need
0 commit comments