Expect JSON for requests (#7)

This commit is contained in:
David Hill 2020-05-14 12:52:21 -05:00 committed by GitHub
parent d0236e5c04
commit 48f7a584ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 23 deletions

View File

@ -5,7 +5,6 @@ import (
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
@ -60,8 +59,17 @@ func feeAddress(c *gin.Context) {
// signature - signmessage signature using the ticket commitment address
// - message = "vsp v3 getfeeaddress ticketHash"
dec := json.NewDecoder(c.Request.Body)
var feeAddressRequest FeeAddressRequest
err := dec.Decode(&feeAddressRequest)
if err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid json"))
return
}
// ticketHash
ticketHashStr := c.Param("ticketHash")
ticketHashStr := feeAddressRequest.TicketHash
if len(ticketHashStr) != chainhash.MaxHashStringSize {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid ticket hash"))
return
@ -73,7 +81,7 @@ func feeAddress(c *gin.Context) {
}
// signature - sanity check signature is in base64 encoding
signature := c.Param("signature")
signature := feeAddressRequest.Signature
if _, err = base64.StdEncoding.DecodeString(signature); err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
return
@ -174,31 +182,25 @@ func payFee(c *gin.Context) {
// votingKey - WIF private key for ticket stakesubmission address
// voteBits - voting preferences in little endian
votingKey := c.Param("votingKey")
dec := json.NewDecoder(c.Request.Body)
var payFeeRequest PayFeeRequest
err := dec.Decode(&payFeeRequest)
if err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid json"))
return
}
votingKey := payFeeRequest.VotingKey
votingWIF, err := dcrutil.DecodeWIF(votingKey, cfg.netParams.PrivateKeyID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
feeTxStr := c.Param("feeTx")
feeTxBytes, err := hex.DecodeString(feeTxStr)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("invalid transaction"))
return
}
voteBitsStr := c.Param("voteBits")
voteBitsBytes, err := hex.DecodeString(voteBitsStr)
if err != nil || len(voteBitsBytes) != 2 {
c.AbortWithError(http.StatusInternalServerError, errors.New("invalid votebits"))
return
}
voteBits := binary.LittleEndian.Uint16(voteBitsBytes)
voteBits := payFeeRequest.VoteBits
feeTx := wire.NewMsgTx()
err = feeTx.FromBytes(feeTxBytes)
err = feeTx.FromBytes(payFeeRequest.Hex)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("unable to deserialize transaction"))
return

View File

@ -10,6 +10,11 @@ type feeResponse struct {
Fee float64 `json:"fee"`
}
type FeeAddressRequest struct {
TicketHash string `json:"ticketHash"`
Signature string `json:"signature"`
}
type feeAddressResponse struct {
Timestamp int64 `json:"timestamp"`
TicketHash string `json:"ticketHash"`
@ -17,6 +22,12 @@ type feeAddressResponse struct {
FeeAddress string `json:"feeAddress"`
}
type PayFeeRequest struct {
Hex []byte `json:"feeTx"`
VotingKey string `json:"votingKey"`
VoteBits uint16 `json:"voteBits"`
}
type payFeeResponse struct {
Timestamp int64 `json:"timestamp"`
TxHash string `json:"txHash"`

View File

@ -12,9 +12,9 @@ func newRouter() *gin.Engine {
api.Use()
{
router.GET("/fee", fee)
router.GET("/feeaddress", feeAddress)
router.POST("/feeaddress", feeAddress)
router.GET("/pubkey", pubKey)
router.GET("/payfee", payFee)
router.POST("/payfee", payFee)
}
return router