bridge.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package console
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io"
  21. "time"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. "github.com/ethereum/go-ethereum/rpc"
  25. "github.com/robertkrimen/otto"
  26. )
  27. // bridge is a collection of JavaScript utility methods to bride the .js runtime
  28. // environment and the Go RPC connection backing the remote method calls.
  29. type bridge struct {
  30. client *rpc.Client // RPC client to execute Ethereum requests through
  31. prompter UserPrompter // Input prompter to allow interactive user feedback
  32. printer io.Writer // Output writer to serialize any display strings to
  33. }
  34. // newBridge creates a new JavaScript wrapper around an RPC client.
  35. func newBridge(client *rpc.Client, prompter UserPrompter, printer io.Writer) *bridge {
  36. return &bridge{
  37. client: client,
  38. prompter: prompter,
  39. printer: printer,
  40. }
  41. }
  42. // NewAccount is a wrapper around the personal.newAccount RPC method that uses a
  43. // non-echoing password prompt to acquire the passphrase and executes the original
  44. // RPC method (saved in jeth.newAccount) with it to actually execute the RPC call.
  45. func (b *bridge) NewAccount(call otto.FunctionCall) (response otto.Value) {
  46. var (
  47. password string
  48. confirm string
  49. err error
  50. )
  51. switch {
  52. // No password was specified, prompt the user for it
  53. case len(call.ArgumentList) == 0:
  54. if password, err = b.prompter.PromptPassword("Passphrase: "); err != nil {
  55. throwJSException(err.Error())
  56. }
  57. if confirm, err = b.prompter.PromptPassword("Repeat passphrase: "); err != nil {
  58. throwJSException(err.Error())
  59. }
  60. if password != confirm {
  61. throwJSException("passphrases don't match!")
  62. }
  63. // A single string password was specified, use that
  64. case len(call.ArgumentList) == 1 && call.Argument(0).IsString():
  65. password, _ = call.Argument(0).ToString()
  66. // Otherwise fail with some error
  67. default:
  68. throwJSException("expected 0 or 1 string argument")
  69. }
  70. // Password acquired, execute the call and return
  71. ret, err := call.Otto.Call("jeth.newAccount", nil, password)
  72. if err != nil {
  73. throwJSException(err.Error())
  74. }
  75. return ret
  76. }
  77. // UnlockAccount is a wrapper around the personal.unlockAccount RPC method that
  78. // uses a non-echoing password prompt to acquire the passphrase and executes the
  79. // original RPC method (saved in jeth.unlockAccount) with it to actually execute
  80. // the RPC call.
  81. func (b *bridge) UnlockAccount(call otto.FunctionCall) (response otto.Value) {
  82. // Make sure we have an account specified to unlock
  83. if !call.Argument(0).IsString() {
  84. throwJSException("first argument must be the account to unlock")
  85. }
  86. account := call.Argument(0)
  87. // If password is not given or is the null value, prompt the user for it
  88. var passwd otto.Value
  89. if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() {
  90. fmt.Fprintf(b.printer, "Unlock account %s\n", account)
  91. if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil {
  92. throwJSException(err.Error())
  93. } else {
  94. passwd, _ = otto.ToValue(input)
  95. }
  96. } else {
  97. if !call.Argument(1).IsString() {
  98. throwJSException("password must be a string")
  99. }
  100. passwd = call.Argument(1)
  101. }
  102. // Third argument is the duration how long the account must be unlocked.
  103. duration := otto.NullValue()
  104. if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() {
  105. if !call.Argument(2).IsNumber() {
  106. throwJSException("unlock duration must be a number")
  107. }
  108. duration = call.Argument(2)
  109. }
  110. // Send the request to the backend and return
  111. val, err := call.Otto.Call("jeth.unlockAccount", nil, account, passwd, duration)
  112. if err != nil {
  113. throwJSException(err.Error())
  114. }
  115. return val
  116. }
  117. // Sign is a wrapper around the personal.sign RPC method that uses a non-echoing password
  118. // prompt to acquire the passphrase and executes the original RPC method (saved in
  119. // jeth.sign) with it to actually execute the RPC call.
  120. func (b *bridge) Sign(call otto.FunctionCall) (response otto.Value) {
  121. var (
  122. message = call.Argument(0)
  123. account = call.Argument(1)
  124. passwd = call.Argument(2)
  125. )
  126. if !message.IsString() {
  127. throwJSException("first argument must be the message to sign")
  128. }
  129. if !account.IsString() {
  130. throwJSException("second argument must be the account to sign with")
  131. }
  132. // if the password is not given or null ask the user and ensure password is a string
  133. if passwd.IsUndefined() || passwd.IsNull() {
  134. fmt.Fprintf(b.printer, "Give password for account %s\n", account)
  135. if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil {
  136. throwJSException(err.Error())
  137. } else {
  138. passwd, _ = otto.ToValue(input)
  139. }
  140. }
  141. if !passwd.IsString() {
  142. throwJSException("third argument must be the password to unlock the account")
  143. }
  144. // Send the request to the backend and return
  145. val, err := call.Otto.Call("jeth.sign", nil, message, account, passwd)
  146. if err != nil {
  147. throwJSException(err.Error())
  148. }
  149. return val
  150. }
  151. // Sleep will block the console for the specified number of seconds.
  152. func (b *bridge) Sleep(call otto.FunctionCall) (response otto.Value) {
  153. if call.Argument(0).IsNumber() {
  154. sleep, _ := call.Argument(0).ToInteger()
  155. time.Sleep(time.Duration(sleep) * time.Second)
  156. return otto.TrueValue()
  157. }
  158. return throwJSException("usage: sleep(<number of seconds>)")
  159. }
  160. // SleepBlocks will block the console for a specified number of new blocks optionally
  161. // until the given timeout is reached.
  162. func (b *bridge) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
  163. var (
  164. blocks = int64(0)
  165. sleep = int64(9999999999999999) // indefinitely
  166. )
  167. // Parse the input parameters for the sleep
  168. nArgs := len(call.ArgumentList)
  169. if nArgs == 0 {
  170. throwJSException("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
  171. }
  172. if nArgs >= 1 {
  173. if call.Argument(0).IsNumber() {
  174. blocks, _ = call.Argument(0).ToInteger()
  175. } else {
  176. throwJSException("expected number as first argument")
  177. }
  178. }
  179. if nArgs >= 2 {
  180. if call.Argument(1).IsNumber() {
  181. sleep, _ = call.Argument(1).ToInteger()
  182. } else {
  183. throwJSException("expected number as second argument")
  184. }
  185. }
  186. // go through the console, this will allow web3 to call the appropriate
  187. // callbacks if a delayed response or notification is received.
  188. blockNumber := func() int64 {
  189. result, err := call.Otto.Run("eth.blockNumber")
  190. if err != nil {
  191. throwJSException(err.Error())
  192. }
  193. block, err := result.ToInteger()
  194. if err != nil {
  195. throwJSException(err.Error())
  196. }
  197. return block
  198. }
  199. // Poll the current block number until either it ot a timeout is reached
  200. targetBlockNr := blockNumber() + blocks
  201. deadline := time.Now().Add(time.Duration(sleep) * time.Second)
  202. for time.Now().Before(deadline) {
  203. if blockNumber() >= targetBlockNr {
  204. return otto.TrueValue()
  205. }
  206. time.Sleep(time.Second)
  207. }
  208. return otto.FalseValue()
  209. }
  210. type jsonrpcCall struct {
  211. Id int64
  212. Method string
  213. Params []interface{}
  214. }
  215. // Send implements the web3 provider "send" method.
  216. func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) {
  217. // Remarshal the request into a Go value.
  218. JSON, _ := call.Otto.Object("JSON")
  219. reqVal, err := JSON.Call("stringify", call.Argument(0))
  220. if err != nil {
  221. throwJSException(err.Error())
  222. }
  223. var (
  224. rawReq = []byte(reqVal.String())
  225. reqs []jsonrpcCall
  226. batch bool
  227. )
  228. if rawReq[0] == '[' {
  229. batch = true
  230. json.Unmarshal(rawReq, &reqs)
  231. } else {
  232. batch = false
  233. reqs = make([]jsonrpcCall, 1)
  234. json.Unmarshal(rawReq, &reqs[0])
  235. }
  236. // Execute the requests.
  237. resps, _ := call.Otto.Object("new Array()")
  238. for _, req := range reqs {
  239. resp, _ := call.Otto.Object(`({"jsonrpc":"2.0"})`)
  240. resp.Set("id", req.Id)
  241. var result json.RawMessage
  242. err = b.client.Call(&result, req.Method, req.Params...)
  243. switch err := err.(type) {
  244. case nil:
  245. if result == nil {
  246. // Special case null because it is decoded as an empty
  247. // raw message for some reason.
  248. resp.Set("result", otto.NullValue())
  249. } else {
  250. resultVal, err := JSON.Call("parse", string(result))
  251. if err != nil {
  252. setError(resp, -32603, err.Error())
  253. } else {
  254. resp.Set("result", resultVal)
  255. }
  256. }
  257. case rpc.Error:
  258. setError(resp, err.ErrorCode(), err.Error())
  259. default:
  260. setError(resp, -32603, err.Error())
  261. }
  262. resps.Call("push", resp)
  263. }
  264. // Return the responses either to the callback (if supplied)
  265. // or directly as the return value.
  266. if batch {
  267. response = resps.Value()
  268. } else {
  269. response, _ = resps.Get("0")
  270. }
  271. if fn := call.Argument(1); fn.Class() == "Function" {
  272. fn.Call(otto.NullValue(), otto.NullValue(), response)
  273. return otto.UndefinedValue()
  274. }
  275. return response
  276. }
  277. func setError(resp *otto.Object, code int, msg string) {
  278. resp.Set("error", map[string]interface{}{"code": code, "message": msg})
  279. }
  280. // throwJSException panics on an otto.Value. The Otto VM will recover from the
  281. // Go panic and throw msg as a JavaScript error.
  282. func throwJSException(msg interface{}) otto.Value {
  283. val, err := otto.ToValue(msg)
  284. if err != nil {
  285. glog.V(logger.Error).Infof("Failed to serialize JavaScript exception %v: %v", msg, err)
  286. }
  287. panic(val)
  288. }