2018-04-09 08:18:22 +00:00
|
|
|
package sign
|
2018-01-26 05:59:21 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-09 08:18:22 +00:00
|
|
|
"context"
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
"github.com/status-im/status-go/geth/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// EventTransactionQueued is triggered when send transaction request is queued
|
|
|
|
EventTransactionQueued = "transaction.queued"
|
|
|
|
// EventTransactionFailed is triggered when send transaction request fails
|
|
|
|
EventTransactionFailed = "transaction.failed"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SendTransactionNoErrorCode is sent when no error occurred.
|
|
|
|
SendTransactionNoErrorCode = iota
|
|
|
|
// SendTransactionDefaultErrorCode is every case when there is no special tx return code.
|
|
|
|
SendTransactionDefaultErrorCode
|
|
|
|
// SendTransactionPasswordErrorCode is sent when account failed verification.
|
|
|
|
SendTransactionPasswordErrorCode
|
|
|
|
// SendTransactionTimeoutErrorCode is sent when tx is timed out.
|
|
|
|
SendTransactionTimeoutErrorCode
|
|
|
|
// SendTransactionDiscardedErrorCode is sent when tx was discarded.
|
|
|
|
SendTransactionDiscardedErrorCode
|
|
|
|
)
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
const (
|
|
|
|
// MessageIDKey is a key for message ID
|
|
|
|
// This ID is required to track from which chat a given send transaction request is coming.
|
|
|
|
MessageIDKey = contextKey("message_id")
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey string // in order to make sure that our context key does not collide with keys from other packages
|
|
|
|
|
|
|
|
// messageIDFromContext returns message id from context (if exists)
|
|
|
|
func messageIDFromContext(ctx context.Context) string {
|
|
|
|
if ctx == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if messageID, ok := ctx.Value(MessageIDKey).(string); ok {
|
|
|
|
return messageID
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
var txReturnCodes = map[error]int{
|
2018-04-09 08:18:22 +00:00
|
|
|
nil: SendTransactionNoErrorCode,
|
|
|
|
keystore.ErrDecrypt: SendTransactionPasswordErrorCode,
|
|
|
|
ErrSignReqTimedOut: SendTransactionTimeoutErrorCode,
|
|
|
|
ErrSignReqDiscarded: SendTransactionDiscardedErrorCode,
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransactionEvent is a signal sent on a send transaction request
|
|
|
|
type SendTransactionEvent struct {
|
2018-04-09 08:18:22 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Args interface{} `json:"args"`
|
|
|
|
MessageID string `json:"message_id"`
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyOnEnqueue returns handler that processes incoming tx queue requests
|
2018-04-09 08:18:22 +00:00
|
|
|
func NotifyOnEnqueue(request *Request) {
|
2018-01-26 05:59:21 +00:00
|
|
|
signal.Send(signal.Envelope{
|
|
|
|
Type: EventTransactionQueued,
|
|
|
|
Event: SendTransactionEvent{
|
2018-04-09 08:18:22 +00:00
|
|
|
ID: request.ID,
|
|
|
|
Args: request.Meta,
|
|
|
|
MessageID: messageIDFromContext(request.context),
|
2018-01-26 05:59:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReturnSendTransactionEvent is a JSON returned whenever transaction send is returned
|
|
|
|
type ReturnSendTransactionEvent struct {
|
2018-04-09 08:18:22 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Args interface{} `json:"args"`
|
|
|
|
MessageID string `json:"message_id"`
|
|
|
|
ErrorMessage string `json:"error_message"`
|
|
|
|
ErrorCode int `json:"error_code,string"`
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyOnReturn returns handler that processes responses from internal tx manager
|
2018-04-09 08:18:22 +00:00
|
|
|
func NotifyOnReturn(request *Request, err error) {
|
2018-01-05 20:58:17 +00:00
|
|
|
// we don't want to notify a user if tx was sent successfully
|
|
|
|
if err == nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
signal.Send(signal.Envelope{
|
|
|
|
Type: EventTransactionFailed,
|
|
|
|
Event: ReturnSendTransactionEvent{
|
2018-04-09 08:18:22 +00:00
|
|
|
ID: request.ID,
|
|
|
|
Args: request.Meta,
|
|
|
|
MessageID: messageIDFromContext(request.context),
|
2018-01-05 20:58:17 +00:00
|
|
|
ErrorMessage: err.Error(),
|
|
|
|
ErrorCode: sendTransactionErrorCode(err),
|
2018-01-26 05:59:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTransactionErrorCode(err error) int {
|
|
|
|
if code, ok := txReturnCodes[err]; ok {
|
|
|
|
return code
|
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
return SendTransactionDefaultErrorCode
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|