bridge.go 14 KB

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