-
Notifications
You must be signed in to change notification settings - Fork 1
/
confirmSellerBid.go
executable file
·136 lines (114 loc) · 4.79 KB
/
confirmSellerBid.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
package rest
import (
"fmt"
"github.com/asaskevich/govalidator"
"github.com/commitHub/commitBlockchain/kafka"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
cTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/commitHub/commitBlockchain/types"
rest2 "github.com/commitHub/commitBlockchain/client/rest"
"github.com/commitHub/commitBlockchain/modules/acl"
negotiationTypes "github.com/commitHub/commitBlockchain/modules/negotiation/internal/types"
)
type confirmSellerBidReq struct {
BaseReq rest.BaseReq `json:"base_req"`
To string `json:"to" valid:"required~Enter the ToAddress,matches(^commit[a-z0-9]{39}$)~ToAddress is Invalid"`
Bid int64 `json:"bid" valid:"required~Enter the Bid,matches(^[1-9]{1}[0-9]*$)~Enter valid Bid"`
Time int64 `json:"time" valid:"required~Enter the Time,matches(^[1-9]{1}[0-9]*$)~Enter valid Time"`
PegHash string `json:"pegHash" valid:"required~Enter the PegHash,matches(^[0-9]+$)~Invalid PegHash,length(2|40)~PegHash length between 2-40"`
SellerContractHash string `json:"sellerContractHash" valid:"required~Enter the SellerContractHash, matches(^.*$)~Invalid SellerContractHash,length(1|1000)~SellerContractHash length should be 1 to 1000"`
Password string `json:"password" valid:"required~Enter the Password"`
Mode string `json:"mode"`
}
func ConfirmSellerBidRequestHandlerFn(cliCtx context.CLIContext, kafkaBool bool, kafkaState kafka.KafkaState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req confirmSellerBidReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
_, err := govalidator.ValidateStruct(req)
if err != nil {
rest2.WriteErrorResponse(w, cTypes.NewError(negotiationTypes.DefaultCodeSpace, http.StatusBadRequest, err.Error()))
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
fromAddr, name, err := context.GetFromFields(req.BaseReq.From, false)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromAddress(fromAddr)
cliCtx = cliCtx.WithFromName(name)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", acl.QuerierRoute, "queryACLAccount", fromAddr), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("couldn't query account. Error: %s", err.Error()))
return
}
if len(res) == 0 {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("Unauthorized transaction. ACL is not defined for buyer"))
return
}
var account acl.ACLAccount
cliCtx.Codec.MustUnmarshalJSON(res, &account)
if !account.GetACL().ConfirmSellerBid {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Unauthorized transaction"))
return
}
to, err := cTypes.AccAddressFromBech32(req.To)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
pegHashHex, err := types.GetAssetPegHashHex(req.PegHash)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
negotiationID := negotiationTypes.NegotiationID(append(append(to.Bytes(), fromAddr.Bytes()...), pegHashHex.Bytes()...))
kb, err := keys.NewKeyBaseFromHomeFlag()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
SignBytes := negotiationTypes.NewSignNegotiationBody(to, fromAddr, pegHashHex, req.Bid, req.Time)
signature, _, err := kb.Sign(name, req.Password, SignBytes.GetSignBytes())
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposedNegotiation := &negotiationTypes.BaseNegotiation{
NegotiationID: negotiationID,
BuyerAddress: to,
SellerAddress: fromAddr,
PegHash: pegHashHex,
Bid: req.Bid,
Time: req.Time,
SellerContractHash: req.SellerContractHash,
BuyerSignature: nil,
SellerSignature: signature,
}
msg := negotiationTypes.BuildMsgConfirmSellerBid(proposedNegotiation)
if kafkaBool == true {
ticketID := kafka.TicketIDGenerator("COSB")
jsonResponse := kafka.SendToKafka(kafka.NewKafkaMsgFromRest(msg, ticketID, req.BaseReq, cliCtx, req.Mode, req.Password), kafkaState, cliCtx.Codec)
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write(jsonResponse)
} else {
output, err := rest2.SignAndBroadcast(req.BaseReq, cliCtx, req.Mode, req.Password, []cTypes.Msg{msg})
if err != nil {
rest2.WriteErrorResponse(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(output)
}
}
}