ui_lib.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "fmt"
  21. "io/ioutil"
  22. "path"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/eth"
  25. "github.com/ethereum/go-ethereum/ethutil"
  26. "github.com/ethereum/go-ethereum/event/filter"
  27. "github.com/ethereum/go-ethereum/javascript"
  28. "github.com/ethereum/go-ethereum/xeth"
  29. "github.com/obscuren/qml"
  30. )
  31. type memAddr struct {
  32. Num string
  33. Value string
  34. }
  35. // UI Library that has some basic functionality exposed
  36. type UiLib struct {
  37. *xeth.XEth
  38. engine *qml.Engine
  39. eth *eth.Ethereum
  40. connected bool
  41. assetPath string
  42. // The main application window
  43. win *qml.Window
  44. Db *Debugger
  45. DbWindow *DebuggerWindow
  46. jsEngine *javascript.JSRE
  47. filterCallbacks map[int][]int
  48. filterManager *filter.FilterManager
  49. }
  50. func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib {
  51. x := xeth.New(eth)
  52. lib := &UiLib{XEth: x, engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(x), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)}
  53. lib.filterManager = filter.NewFilterManager(eth.EventMux())
  54. go lib.filterManager.Start()
  55. return lib
  56. }
  57. func (self *UiLib) Notef(args []interface{}) {
  58. guilogger.Infoln(args...)
  59. }
  60. func (self *UiLib) ImportTx(rlpTx string) {
  61. tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(rlpTx))
  62. err := self.eth.TxPool().Add(tx)
  63. if err != nil {
  64. guilogger.Infoln("import tx failed ", err)
  65. }
  66. }
  67. func (self *UiLib) EvalJavascriptFile(path string) {
  68. self.jsEngine.LoadExtFile(path[7:])
  69. }
  70. func (self *UiLib) EvalJavascriptString(str string) string {
  71. value, err := self.jsEngine.Run(str)
  72. if err != nil {
  73. return err.Error()
  74. }
  75. return fmt.Sprintf("%v", value)
  76. }
  77. func (ui *UiLib) OpenQml(path string) {
  78. container := NewQmlApplication(path[7:], ui)
  79. app := NewExtApplication(container, ui)
  80. go app.run()
  81. }
  82. func (ui *UiLib) OpenHtml(path string) {
  83. container := NewHtmlApplication(path, ui)
  84. app := NewExtApplication(container, ui)
  85. go app.run()
  86. }
  87. func (ui *UiLib) OpenBrowser() {
  88. ui.OpenHtml("file://" + ui.AssetPath("ext/home.html"))
  89. }
  90. func (ui *UiLib) Muted(content string) {
  91. component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml"))
  92. if err != nil {
  93. guilogger.Debugln(err)
  94. return
  95. }
  96. win := component.CreateWindow(nil)
  97. go func() {
  98. path := "file://" + ui.AssetPath("muted/index.html")
  99. win.Set("url", path)
  100. win.Show()
  101. win.Wait()
  102. }()
  103. }
  104. func (ui *UiLib) Connect(button qml.Object) {
  105. if !ui.connected {
  106. ui.eth.Start()
  107. ui.connected = true
  108. button.Set("enabled", false)
  109. }
  110. }
  111. func (ui *UiLib) ConnectToPeer(nodeURL string) {
  112. if err := ui.eth.SuggestPeer(nodeURL); err != nil {
  113. guilogger.Infoln("SuggestPeer error: " + err.Error())
  114. }
  115. }
  116. func (ui *UiLib) AssetPath(p string) string {
  117. return path.Join(ui.assetPath, p)
  118. }
  119. func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
  120. dbWindow := NewDebuggerWindow(self)
  121. object := self.eth.ChainManager().State().GetStateObject(ethutil.Hex2Bytes(contractHash))
  122. if len(object.Code()) > 0 {
  123. dbWindow.SetCode(ethutil.Bytes2Hex(object.Code()))
  124. }
  125. dbWindow.SetData(data)
  126. dbWindow.Show()
  127. }
  128. func (self *UiLib) StartDbWithCode(code string) {
  129. dbWindow := NewDebuggerWindow(self)
  130. dbWindow.SetCode(code)
  131. dbWindow.Show()
  132. }
  133. func (self *UiLib) StartDebugger() {
  134. dbWindow := NewDebuggerWindow(self)
  135. dbWindow.Show()
  136. }
  137. func (self *UiLib) Transact(params map[string]interface{}) (string, error) {
  138. object := mapToTxParams(params)
  139. return self.XEth.Transact(
  140. object["to"],
  141. object["value"],
  142. object["gas"],
  143. object["gasPrice"],
  144. object["data"],
  145. )
  146. }
  147. func (self *UiLib) Compile(code string) (string, error) {
  148. bcode, err := ethutil.Compile(code, false)
  149. if err != nil {
  150. return err.Error(), err
  151. }
  152. return ethutil.Bytes2Hex(bcode), err
  153. }
  154. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  155. object := mapToTxParams(params)
  156. return self.XEth.Execute(
  157. object["to"],
  158. object["value"],
  159. object["gas"],
  160. object["gasPrice"],
  161. object["data"],
  162. )
  163. }
  164. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  165. return 0
  166. /*
  167. return self.miner.AddLocalTx(&miner.LocalTx{
  168. To: ethutil.Hex2Bytes(to),
  169. Data: ethutil.Hex2Bytes(data),
  170. Gas: gas,
  171. GasPrice: gasPrice,
  172. Value: value,
  173. }) - 1
  174. */
  175. }
  176. func (self *UiLib) RemoveLocalTransaction(id int) {
  177. //self.miner.RemoveLocalTx(id)
  178. }
  179. func (self *UiLib) SetGasPrice(price string) {
  180. self.Miner().MinAcceptedGasPrice = ethutil.Big(price)
  181. }
  182. func (self *UiLib) SetExtra(extra string) {
  183. self.Miner().Extra = extra
  184. }
  185. func (self *UiLib) ToggleMining() bool {
  186. if !self.Miner().Mining() {
  187. self.Miner().Start()
  188. return true
  189. } else {
  190. self.Miner().Stop()
  191. return false
  192. }
  193. }
  194. func (self *UiLib) ToHex(data string) string {
  195. return "0x" + ethutil.Bytes2Hex([]byte(data))
  196. }
  197. func (self *UiLib) ToAscii(data string) string {
  198. start := 0
  199. if len(data) > 1 && data[0:2] == "0x" {
  200. start = 2
  201. }
  202. return string(ethutil.Hex2Bytes(data[start:]))
  203. }
  204. /// Ethereum filter methods
  205. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  206. /* TODO remove me
  207. filter := qt.NewFilterFromMap(object, self.eth)
  208. filter.MessageCallback = func(messages state.Messages) {
  209. view.Call("messages", xeth.ToMessages(messages), id)
  210. }
  211. id = self.filterManager.InstallFilter(filter)
  212. return id
  213. */
  214. return 0
  215. }
  216. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  217. /* TODO remove me
  218. filter := core.NewFilter(self.eth)
  219. filter.BlockCallback = func(block *types.Block) {
  220. view.Call("messages", "{}", id)
  221. }
  222. id = self.filterManager.InstallFilter(filter)
  223. return id
  224. */
  225. return 0
  226. }
  227. func (self *UiLib) Messages(id int) *ethutil.List {
  228. /* TODO remove me
  229. filter := self.filterManager.GetFilter(id)
  230. if filter != nil {
  231. messages := xeth.ToMessages(filter.Find())
  232. return messages
  233. }
  234. */
  235. return ethutil.EmptyList()
  236. }
  237. func (self *UiLib) ReadFile(p string) string {
  238. content, err := ioutil.ReadFile(self.AssetPath(path.Join("ext", p)))
  239. if err != nil {
  240. guilogger.Infoln("error reading file", p, ":", err)
  241. }
  242. return string(content)
  243. }
  244. func (self *UiLib) UninstallFilter(id int) {
  245. self.filterManager.UninstallFilter(id)
  246. }
  247. func mapToTxParams(object map[string]interface{}) map[string]string {
  248. // Default values
  249. if object["from"] == nil {
  250. object["from"] = ""
  251. }
  252. if object["to"] == nil {
  253. object["to"] = ""
  254. }
  255. if object["value"] == nil {
  256. object["value"] = ""
  257. }
  258. if object["gas"] == nil {
  259. object["gas"] = ""
  260. }
  261. if object["gasPrice"] == nil {
  262. object["gasPrice"] = ""
  263. }
  264. var dataStr string
  265. var data []string
  266. if list, ok := object["data"].(*qml.List); ok {
  267. list.Convert(&data)
  268. } else if str, ok := object["data"].(string); ok {
  269. data = []string{str}
  270. }
  271. for _, str := range data {
  272. if ethutil.IsHex(str) {
  273. str = str[2:]
  274. if len(str) != 64 {
  275. str = ethutil.LeftPadString(str, 64)
  276. }
  277. } else {
  278. str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32))
  279. }
  280. dataStr += str
  281. }
  282. object["data"] = dataStr
  283. conv := make(map[string]string)
  284. for key, value := range object {
  285. if v, ok := value.(string); ok {
  286. conv[key] = v
  287. }
  288. }
  289. return conv
  290. }