bridge.go 11 KB

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