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 (
|
2018-04-10 10:02:54 +00:00
|
|
|
// EventSignRequestAdded is triggered when send transaction request is queued
|
|
|
|
EventSignRequestAdded = "sign-request.queued"
|
|
|
|
// EventSignRequestFailed is triggered when send transaction request fails
|
|
|
|
EventSignRequestFailed = "sign-request.failed"
|
2018-01-26 05:59:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-04-10 10:02:54 +00:00
|
|
|
// SignRequestNoErrorCode is sent when no error occurred.
|
|
|
|
SignRequestNoErrorCode = iota
|
|
|
|
// SignRequestDefaultErrorCode is every case when there is no special tx return code.
|
|
|
|
SignRequestDefaultErrorCode
|
|
|
|
// SignRequestPasswordErrorCode is sent when account failed verification.
|
|
|
|
SignRequestPasswordErrorCode
|
|
|
|
// SignRequestTimeoutErrorCode is sent when tx is timed out.
|
|
|
|
SignRequestTimeoutErrorCode
|
|
|
|
// SignRequestDiscardedErrorCode is sent when tx was discarded.
|
|
|
|
SignRequestDiscardedErrorCode
|
2018-01-26 05:59:21 +00:00
|
|
|
)
|
|
|
|
|
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-10 10:02:54 +00:00
|
|
|
nil: SignRequestNoErrorCode,
|
|
|
|
keystore.ErrDecrypt: SignRequestPasswordErrorCode,
|
|
|
|
ErrSignReqTimedOut: SignRequestTimeoutErrorCode,
|
|
|
|
ErrSignReqDiscarded: SignRequestDiscardedErrorCode,
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// PendingRequestEvent is a signal sent when a sign request is added
|
|
|
|
type PendingRequestEvent struct {
|
2018-04-09 08:18:22 +00:00
|
|
|
ID string `json:"id"`
|
2018-04-10 10:02:54 +00:00
|
|
|
Method string `json:"method"`
|
2018-04-09 08:18:22 +00:00
|
|
|
Args interface{} `json:"args"`
|
|
|
|
MessageID string `json:"message_id"`
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// NotifyOnEnqueue sends a signal when a sign request is added
|
2018-04-09 08:18:22 +00:00
|
|
|
func NotifyOnEnqueue(request *Request) {
|
2018-01-26 05:59:21 +00:00
|
|
|
signal.Send(signal.Envelope{
|
2018-04-10 10:02:54 +00:00
|
|
|
Type: EventSignRequestAdded,
|
|
|
|
Event: PendingRequestEvent{
|
2018-04-09 08:18:22 +00:00
|
|
|
ID: request.ID,
|
|
|
|
Args: request.Meta,
|
2018-04-10 10:02:54 +00:00
|
|
|
Method: request.Method,
|
2018-04-09 08:18:22 +00:00
|
|
|
MessageID: messageIDFromContext(request.context),
|
2018-01-26 05:59:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// PendingRequestErrorEvent is a signal sent when sign request has failed
|
|
|
|
type PendingRequestErrorEvent struct {
|
|
|
|
PendingRequestEvent
|
|
|
|
ErrorMessage string `json:"error_message"`
|
|
|
|
ErrorCode int `json:"error_code,string"`
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// NotifyIfError sends a signal only if error had happened
|
|
|
|
func NotifyIfError(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{
|
2018-04-10 10:02:54 +00:00
|
|
|
Type: EventSignRequestFailed,
|
|
|
|
Event: PendingRequestErrorEvent{
|
|
|
|
PendingRequestEvent: PendingRequestEvent{
|
|
|
|
ID: request.ID,
|
|
|
|
Args: request.Meta,
|
|
|
|
Method: request.Method,
|
|
|
|
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-10 10:02:54 +00:00
|
|
|
return SignRequestDefaultErrorCode
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|