bridge.go 9.5 KB

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