Expect JSON for requests (#7)
This commit is contained in:
parent
d0236e5c04
commit
48f7a584ac
44
methods.go
44
methods.go
@ -5,7 +5,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@ -60,8 +59,17 @@ func feeAddress(c *gin.Context) {
|
|||||||
// signature - signmessage signature using the ticket commitment address
|
// signature - signmessage signature using the ticket commitment address
|
||||||
// - message = "vsp v3 getfeeaddress ticketHash"
|
// - 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
|
// ticketHash
|
||||||
ticketHashStr := c.Param("ticketHash")
|
ticketHashStr := feeAddressRequest.TicketHash
|
||||||
if len(ticketHashStr) != chainhash.MaxHashStringSize {
|
if len(ticketHashStr) != chainhash.MaxHashStringSize {
|
||||||
c.AbortWithError(http.StatusBadRequest, errors.New("invalid ticket hash"))
|
c.AbortWithError(http.StatusBadRequest, errors.New("invalid ticket hash"))
|
||||||
return
|
return
|
||||||
@ -73,7 +81,7 @@ func feeAddress(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// signature - sanity check signature is in base64 encoding
|
// signature - sanity check signature is in base64 encoding
|
||||||
signature := c.Param("signature")
|
signature := feeAddressRequest.Signature
|
||||||
if _, err = base64.StdEncoding.DecodeString(signature); err != nil {
|
if _, err = base64.StdEncoding.DecodeString(signature); err != nil {
|
||||||
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
|
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
|
||||||
return
|
return
|
||||||
@ -174,31 +182,25 @@ func payFee(c *gin.Context) {
|
|||||||
// votingKey - WIF private key for ticket stakesubmission address
|
// votingKey - WIF private key for ticket stakesubmission address
|
||||||
// voteBits - voting preferences in little endian
|
// 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)
|
votingWIF, err := dcrutil.DecodeWIF(votingKey, cfg.netParams.PrivateKeyID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
voteBits := payFeeRequest.VoteBits
|
||||||
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)
|
|
||||||
|
|
||||||
feeTx := wire.NewMsgTx()
|
feeTx := wire.NewMsgTx()
|
||||||
err = feeTx.FromBytes(feeTxBytes)
|
err = feeTx.FromBytes(payFeeRequest.Hex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, errors.New("unable to deserialize transaction"))
|
c.AbortWithError(http.StatusInternalServerError, errors.New("unable to deserialize transaction"))
|
||||||
return
|
return
|
||||||
|
|||||||
11
responses.go
11
responses.go
@ -10,6 +10,11 @@ type feeResponse struct {
|
|||||||
Fee float64 `json:"fee"`
|
Fee float64 `json:"fee"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FeeAddressRequest struct {
|
||||||
|
TicketHash string `json:"ticketHash"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
}
|
||||||
|
|
||||||
type feeAddressResponse struct {
|
type feeAddressResponse struct {
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
TicketHash string `json:"ticketHash"`
|
TicketHash string `json:"ticketHash"`
|
||||||
@ -17,6 +22,12 @@ type feeAddressResponse struct {
|
|||||||
FeeAddress string `json:"feeAddress"`
|
FeeAddress string `json:"feeAddress"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PayFeeRequest struct {
|
||||||
|
Hex []byte `json:"feeTx"`
|
||||||
|
VotingKey string `json:"votingKey"`
|
||||||
|
VoteBits uint16 `json:"voteBits"`
|
||||||
|
}
|
||||||
|
|
||||||
type payFeeResponse struct {
|
type payFeeResponse struct {
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
TxHash string `json:"txHash"`
|
TxHash string `json:"txHash"`
|
||||||
|
|||||||
@ -12,9 +12,9 @@ func newRouter() *gin.Engine {
|
|||||||
api.Use()
|
api.Use()
|
||||||
{
|
{
|
||||||
router.GET("/fee", fee)
|
router.GET("/fee", fee)
|
||||||
router.GET("/feeaddress", feeAddress)
|
router.POST("/feeaddress", feeAddress)
|
||||||
router.GET("/pubkey", pubKey)
|
router.GET("/pubkey", pubKey)
|
||||||
router.GET("/payfee", payFee)
|
router.POST("/payfee", payFee)
|
||||||
}
|
}
|
||||||
|
|
||||||
return router
|
return router
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user