Skip to content
This repository has been archived by the owner on Sep 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #114 from stellar/compliance-pending-response
Browse files Browse the repository at this point in the history
Persist AuthData to resubmit after "pending" response
  • Loading branch information
bartekn authored Jun 13, 2018
2 parents b4294ac + 168e002 commit f3ef60a
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 38 deletions.
2 changes: 1 addition & 1 deletion readme_bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Every request must contain required parameters from the following list. Addition

name | | description
--- | --- | ---
`id` | optional | Unique ID of the payment. If you send another request with the same `id` previously sent transaction will be resubmitted to the network.
`id` | optional | Unique ID of the payment. If you send another request with the same `id` previously sent transaction will be resubmitted to the network. This parameter is required when sending a payment using Compliance protocol.
`source` | optional | Secret seed of transaction source account. If ommitted it will use the `base_seed` specified in the config file.
`sender` | optional | Payment address (ex. `bob*stellar.org`) of payment sender account. Required for when sending using Compliance protocol.
`destination` | required | Account ID or payment address (ex. `bob*stellar.org`) of payment destination account
Expand Down
1 change: 1 addition & 0 deletions readme_compliance.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Typically called by the bridge server when a user initiates a payment. This endp

name | | description
--- | --- | ---
`id` | required | ID of the payment/transaction. In case of `pending` response or errors, you should resubmit the request with the same `id` value.
`source` | required | Account ID of transaction source account.
`sender` | required | Stellar address (ex. `bob*stellar.org`) of payment sender account.
`destination` | required | Account ID or Stellar address (ex. `bob*stellar.org`) of payment destination account
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,6 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) {
return
}

var paymentID *string

if request.ID != "" {
sentTransaction, err := rh.Repository.GetSentTransactionByPaymentID(request.ID)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error getting sent transaction")
server.Write(w, protocols.InternalServerError)
return
}

if sentTransaction == nil {
paymentID = &request.ID
} else {
log.WithFields(log.Fields{"paymentID": request.ID, "tx": sentTransaction.EnvelopeXdr}).Info("Transaction with given ID already exists, resubmitting...")
submitResponse, err := rh.Horizon.SubmitTransaction(sentTransaction.EnvelopeXdr)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Error submitting transaction")
server.Write(w, protocols.InternalServerError)
return
}

rh.handleSubmitterResponse(w, submitResponse)
return
}
}

if request.Source == "" {
request.Source = rh.Config.Accounts.BaseSeed
}
Expand All @@ -76,13 +50,19 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) {
// * User explicitly wants to use compliance protocol
if rh.Config.Compliance != "" &&
(request.ExtraMemo != "" || (request.ExtraMemo == "" && request.UseCompliance)) {
rh.complianceProtocolPayment(w, request, paymentID)
rh.complianceProtocolPayment(w, request)
} else {
rh.standardPayment(w, request, paymentID)
rh.standardPayment(w, request)
}
}

func (rh *RequestHandler) complianceProtocolPayment(w http.ResponseWriter, request *bridge.PaymentRequest, paymentID *string) {
func (rh *RequestHandler) complianceProtocolPayment(w http.ResponseWriter, request *bridge.PaymentRequest) {
var paymentID *string

if request.ID != "" {
paymentID = &request.ID
}

// Compliance server part
sendRequest := request.ToComplianceSendRequest()

Expand Down Expand Up @@ -153,7 +133,33 @@ func (rh *RequestHandler) complianceProtocolPayment(w http.ResponseWriter, reque
rh.handleSubmitterResponse(w, submitResponse)
}

func (rh *RequestHandler) standardPayment(w http.ResponseWriter, request *bridge.PaymentRequest, paymentID *string) {
func (rh *RequestHandler) standardPayment(w http.ResponseWriter, request *bridge.PaymentRequest) {
var paymentID *string

if request.ID != "" {
sentTransaction, err := rh.Repository.GetSentTransactionByPaymentID(request.ID)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error getting sent transaction")
server.Write(w, protocols.InternalServerError)
return
}

if sentTransaction == nil {
paymentID = &request.ID
} else {
log.WithFields(log.Fields{"paymentID": request.ID, "tx": sentTransaction.EnvelopeXdr}).Info("Transaction with given ID already exists, resubmitting...")
submitResponse, err := rh.Horizon.SubmitTransaction(sentTransaction.EnvelopeXdr)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Error submitting transaction")
server.Write(w, protocols.InternalServerError)
return
}

rh.handleSubmitterResponse(w, submitResponse)
return
}
}

destinationObject := &federation.NameResponse{}
var err error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

log "github.com/sirupsen/logrus"
"github.com/stellar/gateway/db/entities"
"github.com/stellar/gateway/protocols"
callback "github.com/stellar/gateway/protocols/compliance"
"github.com/stellar/gateway/server"
Expand Down Expand Up @@ -36,6 +37,34 @@ func (rh *RequestHandler) HandlerSend(c web.C, w http.ResponseWriter, r *http.Re
return
}

authDataEntity, err := rh.Repository.GetAuthData(request.ID)
if err != nil {
log.Error(err.Error())
server.Write(w, protocols.InternalServerError)
return
}

if authDataEntity != nil {
stellarToml, err := rh.StellarTomlResolver.GetStellarToml(authDataEntity.Domain)
if err != nil {
log.WithFields(log.Fields{
"destination": request.Destination,
"err": err,
}).Print("Cannot resolve address")
server.Write(w, callback.CannotResolveDestination)
return
}

if stellarToml.AuthServer == "" {
log.Print("No AUTH_SERVER in stellar.toml")
server.Write(w, callback.AuthServerNotDefined)
return
}

rh.sendAuthData(w, stellarToml.AuthServer, []byte(authDataEntity.AuthData))
return
}

var domain string
var destinationObject *federation.NameResponse

Expand Down Expand Up @@ -247,6 +276,31 @@ func (rh *RequestHandler) HandlerSend(c web.C, w http.ResponseWriter, r *http.Re
server.Write(w, protocols.InternalServerError)
return
}

authDataEntity = &entities.AuthData{
RequestID: request.ID,
Domain: domain,
AuthData: string(data),
}
err = rh.EntityManager.Persist(authDataEntity)
if err != nil {
log.WithFields(log.Fields{"err": err}).Warn("Error persisting authDataEntity")
server.Write(w, protocols.InternalServerError)
return
}

rh.sendAuthData(w, stellarToml.AuthServer, data)
}

func (rh *RequestHandler) sendAuthData(w http.ResponseWriter, authServer string, data []byte) {
var authData compliance.AuthData
err := json.Unmarshal(data, &authData)
if err != nil {
log.Error(err)
server.Write(w, protocols.InternalServerError)
return
}

sig, err := rh.SignatureSignerVerifier.Sign(rh.Config.Keys.SigningSeed, data)
if err != nil {
log.Error("Error signing authData")
Expand All @@ -259,12 +313,12 @@ func (rh *RequestHandler) HandlerSend(c web.C, w http.ResponseWriter, r *http.Re
Signature: sig,
}
resp, err := rh.Client.PostForm(
stellarToml.AuthServer,
authServer,
authRequest.ToURLValues(),
)
if err != nil {
log.WithFields(log.Fields{
"auth_server": stellarToml.AuthServer,
"auth_server": authServer,
"err": err,
}).Error("Error sending request to auth server")
server.Write(w, protocols.InternalServerError)
Expand Down Expand Up @@ -301,7 +355,7 @@ func (rh *RequestHandler) HandlerSend(c web.C, w http.ResponseWriter, r *http.Re

response := callback.SendResponse{
AuthResponse: authResponse,
TransactionXdr: txBase64,
TransactionXdr: authData.Tx,
}
server.Write(w, &response)
}
Loading

0 comments on commit f3ef60a

Please sign in to comment.