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"
|
2018-05-03 07:35:58 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
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-05-03 07:35:58 +00:00
|
|
|
// SendSignRequestAdded sends a signal when a sign request is added.
|
|
|
|
func SendSignRequestAdded(request *Request) {
|
|
|
|
signal.SendSignRequestAdded(
|
|
|
|
signal.PendingRequestEvent{
|
|
|
|
ID: request.ID,
|
|
|
|
Args: request.Meta,
|
|
|
|
Method: request.Method,
|
|
|
|
MessageID: messageIDFromContext(request.context),
|
|
|
|
})
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
// SendSignRequestFailed sends a signal only if error had happened
|
|
|
|
func SendSignRequestFailed(request *Request, err error) {
|
|
|
|
signal.SendSignRequestFailed(
|
|
|
|
signal.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-05-03 07:35:58 +00:00
|
|
|
err, sendTransactionErrorCode(err))
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
const (
|
|
|
|
// 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-05-03 07:35:58 +00:00
|
|
|
var txReturnCodes = map[error]int{
|
|
|
|
nil: SignRequestNoErrorCode,
|
|
|
|
keystore.ErrDecrypt: SignRequestPasswordErrorCode,
|
|
|
|
ErrSignReqTimedOut: SignRequestTimeoutErrorCode,
|
|
|
|
ErrSignReqDiscarded: SignRequestDiscardedErrorCode,
|
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
|
|
|
}
|