2016-12-18 20:36:17 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/node"
|
2016-12-18 20:36:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-05-03 14:24:48 +00:00
|
|
|
EventLocalStorageSet = "local_storage.set"
|
2017-07-18 14:26:24 +00:00
|
|
|
EventSendMessage = "jail.send_message"
|
|
|
|
EventShowSuggestions = "jail.show_suggestions"
|
2016-12-18 20:36:17 +00:00
|
|
|
LocalStorageMaxDataLen = 256
|
|
|
|
)
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// registerHandlers augments and transforms a given jail cell's underlying VM,
|
|
|
|
// by adding and replacing method handlers.
|
|
|
|
func registerHandlers(jail *Jail, vm *otto.Otto, chatID string) (err error) {
|
|
|
|
jeth, err := vm.Get("jeth")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registerHandler := jeth.Object().Set
|
|
|
|
|
|
|
|
// register send handler
|
|
|
|
if err = registerHandler("send", makeSendHandler(jail, chatID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// register sendAsync handler
|
|
|
|
if err = registerHandler("sendAsync", makeSendHandler(jail, chatID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// register isConnected handler
|
|
|
|
if err = registerHandler("isConnected", makeJethIsConnectedHandler(jail)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// define localStorage
|
|
|
|
if err = vm.Set("localStorage", struct{}{}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// register localStorage.set handler
|
|
|
|
localStorage, err := vm.Get("localStorage")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = localStorage.Object().Set("set", makeLocalStorageSetHandler(chatID)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
// makeSendHandler returns jeth.send() and jeth.sendAsync() handler
|
2017-05-03 14:24:48 +00:00
|
|
|
func makeSendHandler(jail *Jail, chatID string) func(call otto.FunctionCall) (response otto.Value) {
|
2016-12-18 20:36:17 +00:00
|
|
|
return func(call otto.FunctionCall) (response otto.Value) {
|
2017-05-03 14:24:48 +00:00
|
|
|
return jail.Send(chatID, call)
|
2016-12-18 20:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeJethIsConnectedHandler returns jeth.isConnected() handler
|
|
|
|
func makeJethIsConnectedHandler(jail *Jail) func(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
2017-05-16 12:09:52 +00:00
|
|
|
client, err := jail.requestManager.RPCClient()
|
2016-12-18 20:36:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return newErrorResponse(call, -32603, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
var netListeningResult bool
|
|
|
|
if err := client.Call(&netListeningResult, "net_listening"); err != nil {
|
|
|
|
return newErrorResponse(call, -32603, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if !netListeningResult {
|
2017-05-16 12:09:52 +00:00
|
|
|
return newErrorResponse(call, -32603, node.ErrNoRunningNode.Error(), nil)
|
2016-12-18 20:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newResultResponse(call, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// LocalStorageSetEvent is a signal sent whenever local storage Set method is called
|
|
|
|
type LocalStorageSetEvent struct {
|
|
|
|
ChatID string `json:"chat_id"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
// makeLocalStorageSetHandler returns localStorage.set() handler
|
2017-05-03 14:24:48 +00:00
|
|
|
func makeLocalStorageSetHandler(chatID string) func(call otto.FunctionCall) (response otto.Value) {
|
2016-12-18 20:36:17 +00:00
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
|
|
|
data := call.Argument(0).String()
|
|
|
|
if len(data) > LocalStorageMaxDataLen { // cap input string
|
|
|
|
data = data[:LocalStorageMaxDataLen]
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
node.SendSignal(node.SignalEnvelope{
|
2016-12-18 20:36:17 +00:00
|
|
|
Type: EventLocalStorageSet,
|
2017-05-16 12:09:52 +00:00
|
|
|
Event: LocalStorageSetEvent{
|
2017-05-03 14:24:48 +00:00
|
|
|
ChatID: chatID,
|
2016-12-18 20:36:17 +00:00
|
|
|
Data: data,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return newResultResponse(call, true)
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 14:26:24 +00:00
|
|
|
|
|
|
|
// SendMessageEvent wraps Jail send signals
|
|
|
|
type SendMessageEvent struct {
|
|
|
|
ChatID string `json:"chat_id"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeSendMessageHandler(chatID string) func(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
|
|
|
message := call.Argument(0).String()
|
|
|
|
|
|
|
|
node.SendSignal(node.SignalEnvelope{
|
|
|
|
Type: EventSendMessage,
|
|
|
|
Event: SendMessageEvent{
|
|
|
|
ChatID: chatID,
|
|
|
|
Message: message,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return newResultResponse(call, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowSuggestionsEvent wraps Jail show suggestion signals
|
|
|
|
type ShowSuggestionsEvent struct {
|
|
|
|
ChatID string `json:"chat_id"`
|
|
|
|
Markup string `json:"markup"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeShowSuggestionsHandler(chatID string) func(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
|
|
|
suggestionsMarkup := call.Argument(0).String()
|
|
|
|
|
|
|
|
node.SendSignal(node.SignalEnvelope{
|
|
|
|
Type: EventShowSuggestions,
|
|
|
|
Event: ShowSuggestionsEvent{
|
|
|
|
ChatID: chatID,
|
|
|
|
Markup: suggestionsMarkup,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return newResultResponse(call, true)
|
|
|
|
}
|
|
|
|
}
|