// Sleep will halt the console for arg[0] seconds. func (self *Jeth) Sleep(call otto.FunctionCall) (response otto.Value) { if len(call.ArgumentList) >= 1 { if call.Argument(0).IsNumber() { sleep, _ := call.Argument(0).ToInteger() time.Sleep(time.Duration(sleep) * time.Second) return otto.TrueValue() } } return throwJSExeception("usage: sleep(<sleep in seconds>)") }
// SleepBlocks will wait for a specified number of new blocks or max for a // given of seconds. sleepBlocks(nBlocks[, maxSleep]). func (self *Jeth) SleepBlocks(call otto.FunctionCall) (response otto.Value) { nBlocks := int64(0) maxSleep := int64(9999999999999999) // indefinitely nArgs := len(call.ArgumentList) if nArgs == 0 { throwJSExeception("usage: sleepBlocks(<n blocks>[, max sleep in seconds])") } if nArgs >= 1 { if call.Argument(0).IsNumber() { nBlocks, _ = call.Argument(0).ToInteger() } else { throwJSExeception("expected number as first argument") } } if nArgs >= 2 { if call.Argument(1).IsNumber() { maxSleep, _ = call.Argument(1).ToInteger() } else { throwJSExeception("expected number as second argument") } } // go through the console, this will allow web3 to call the appropriate // callbacks if a delayed response or notification is received. currentBlockNr := func() int64 { result, err := call.Otto.Run("eth.blockNumber") if err != nil { throwJSExeception(err.Error()) } blockNr, err := result.ToInteger() if err != nil { throwJSExeception(err.Error()) } return blockNr } targetBlockNr := currentBlockNr() + nBlocks deadline := time.Now().Add(time.Duration(maxSleep) * time.Second) for time.Now().Before(deadline) { if currentBlockNr() >= targetBlockNr { return otto.TrueValue() } time.Sleep(time.Second) } return otto.FalseValue() }
// loadScript executes a JS script from inside the currently executing JS code. func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value { file, err := call.Argument(0).ToString() if err != nil { // TODO: throw exception return otto.FalseValue() } file = common.AbsolutePath(self.assetPath, file) source, err := ioutil.ReadFile(file) if err != nil { // TODO: throw exception return otto.FalseValue() } if _, err := compileAndRun(call.Otto, file, source); err != nil { // TODO: throw exception fmt.Println("err:", err) return otto.FalseValue() } // TODO: return evaluation result return otto.TrueValue() }
// UnlockAccount asks the user for the password and than executes the jeth.UnlockAccount callback in the jsre func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) { var cmd, account, passwd string timeout := int64(300) var ok bool if len(call.ArgumentList) == 0 { fmt.Println("expected address of account to unlock") return otto.FalseValue() } if len(call.ArgumentList) >= 1 { if accountExport, err := call.Argument(0).Export(); err == nil { if account, ok = accountExport.(string); ok { if len(call.ArgumentList) == 1 { fmt.Printf("Unlock account %s\n", account) passwd, err = PromptPassword("Passphrase: ", true) if err != nil { return otto.FalseValue() } } } } } if len(call.ArgumentList) >= 2 { if passwdExport, err := call.Argument(1).Export(); err == nil { passwd, _ = passwdExport.(string) } } if len(call.ArgumentList) >= 3 { if timeoutExport, err := call.Argument(2).Export(); err == nil { timeout, _ = timeoutExport.(int64) } } cmd = fmt.Sprintf("jeth.unlockAccount('%s', '%s', %d)", account, passwd, timeout) if val, err := call.Otto.Run(cmd); err == nil { return val } return otto.FalseValue() }
func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { reqif, err := call.Argument(0).Export() if err != nil { return self.err(call, -32700, err.Error(), nil) } jsonreq, err := json.Marshal(reqif) var reqs []rpc.JSONRequest batch := true err = json.Unmarshal(jsonreq, &reqs) if err != nil { reqs = make([]rpc.JSONRequest, 1) err = json.Unmarshal(jsonreq, &reqs[0]) batch = false } call.Otto.Set("response_len", len(reqs)) call.Otto.Run("var ret_response = new Array(response_len);") for i, req := range reqs { err := self.client.Send(&req) if err != nil { return self.err(call, -32603, err.Error(), req.Id) } result := make(map[string]interface{}) err = self.client.Recv(&result) if err != nil { return self.err(call, -32603, err.Error(), req.Id) } _, isSuccessResponse := result["result"] _, isErrorResponse := result["error"] if !isSuccessResponse && !isErrorResponse { return self.err(call, -32603, fmt.Sprintf("Invalid response"), new(int64)) } id, _ := result["id"] call.Otto.Set("ret_id", id) jsonver, _ := result["jsonrpc"] call.Otto.Set("ret_jsonrpc", jsonver) var payload []byte if isSuccessResponse { payload, _ = json.Marshal(result["result"]) } else if isErrorResponse { payload, _ = json.Marshal(result["error"]) } call.Otto.Set("ret_result", string(payload)) call.Otto.Set("response_idx", i) response, err = call.Otto.Run(` ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, result: JSON.parse(ret_result) }; `) } if !batch { call.Otto.Run("ret_response = ret_response[0];") } if call.Argument(1).IsObject() { call.Otto.Set("callback", call.Argument(1)) call.Otto.Run(` if (Object.prototype.toString.call(callback) == '[object Function]') { callback(null, ret_response); } `) } return }