ui_lib.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. lib := &UiLib{XEth: xeth.New(eth), engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(eth), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)}
  52. lib.filterManager = filter.NewFilterManager(eth.EventMux())
  53. go lib.filterManager.Start()
  54. return lib
  55. }
  56. func (self *UiLib) Notef(args []interface{}) {
  57. guilogger.Infoln(args...)
  58. }
  59. func (self *UiLib) ImportTx(rlpTx string) {
  60. tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(rlpTx))
  61. err := self.eth.TxPool().Add(tx)
  62. if err != nil {
  63. guilogger.Infoln("import tx failed ", err)
  64. }
  65. }
  66. func (self *UiLib) EvalJavascriptFile(path string) {
  67. self.jsEngine.LoadExtFile(path[7:])
  68. }
  69. func (self *UiLib) EvalJavascriptString(str string) string {
  70. value, err := self.jsEngine.Run(str)
  71. if err != nil {
  72. return err.Error()
  73. }
  74. return fmt.Sprintf("%v", value)
  75. }
  76. func (ui *UiLib) OpenQml(path string) {
  77. container := NewQmlApplication(path[7:], ui)
  78. app := NewExtApplication(container, ui)
  79. go app.run()
  80. }
  81. func (ui *UiLib) OpenHtml(path string) {
  82. container := NewHtmlApplication(path, ui)
  83. app := NewExtApplication(container, ui)
  84. go app.run()
  85. }
  86. func (ui *UiLib) OpenBrowser() {
  87. ui.OpenHtml("file://" + ui.AssetPath("ext/home.html"))
  88. }
  89. func (ui *UiLib) Muted(content string) {
  90. component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml"))
  91. if err != nil {
  92. guilogger.Debugln(err)
  93. return
  94. }
  95. win := component.CreateWindow(nil)
  96. go func() {
  97. path := "file://" + ui.AssetPath("muted/index.html")
  98. win.Set("url", path)
  99. win.Show()
  100. win.Wait()
  101. }()
  102. }
  103. func (ui *UiLib) Connect(button qml.Object) {
  104. if !ui.connected {
  105. ui.eth.Start()
  106. ui.connected = true
  107. button.Set("enabled", false)
  108. }
  109. }
  110. func (ui *UiLib) ConnectToPeer(nodeURL string) {
  111. if err := ui.eth.SuggestPeer(nodeURL); err != nil {
  112. guilogger.Infoln("SuggestPeer error: " + err.Error())
  113. }
  114. }
  115. func (ui *UiLib) AssetPath(p string) string {
  116. return path.Join(ui.assetPath, p)
  117. }
  118. func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
  119. dbWindow := NewDebuggerWindow(self)
  120. object := self.eth.ChainManager().State().GetStateObject(ethutil.Hex2Bytes(contractHash))
  121. if len(object.Code()) > 0 {
  122. dbWindow.SetCode(ethutil.Bytes2Hex(object.Code()))
  123. }
  124. dbWindow.SetData(data)
  125. dbWindow.Show()
  126. }
  127. func (self *UiLib) StartDbWithCode(code string) {
  128. dbWindow := NewDebuggerWindow(self)
  129. dbWindow.SetCode(code)
  130. dbWindow.Show()
  131. }
  132. func (self *UiLib) StartDebugger() {
  133. dbWindow := NewDebuggerWindow(self)
  134. dbWindow.Show()
  135. }
  136. func (self *UiLib) Transact(params map[string]interface{}) (string, error) {
  137. object := mapToTxParams(params)
  138. return self.XEth.Transact(
  139. object["to"],
  140. object["value"],
  141. object["gas"],
  142. object["gasPrice"],
  143. object["data"],
  144. )
  145. }
  146. func (self *UiLib) Compile(code string) (string, error) {
  147. bcode, err := ethutil.Compile(code, false)
  148. if err != nil {
  149. return err.Error(), err
  150. }
  151. return ethutil.Bytes2Hex(bcode), err
  152. }
  153. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  154. object := mapToTxParams(params)
  155. return self.XEth.Execute(
  156. object["to"],
  157. object["value"],
  158. object["gas"],
  159. object["gasPrice"],
  160. object["data"],
  161. )
  162. }
  163. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  164. return 0
  165. /*
  166. return self.miner.AddLocalTx(&miner.LocalTx{
  167. To: ethutil.Hex2Bytes(to),
  168. Data: ethutil.Hex2Bytes(data),
  169. Gas: gas,
  170. GasPrice: gasPrice,
  171. Value: value,
  172. }) - 1
  173. */
  174. }
  175. func (self *UiLib) RemoveLocalTransaction(id int) {
  176. //self.miner.RemoveLocalTx(id)
  177. }
  178. func (self *UiLib) SetGasPrice(price string) {
  179. self.Miner().MinAcceptedGasPrice = ethutil.Big(price)
  180. }
  181. func (self *UiLib) SetExtra(extra string) {
  182. self.Miner().Extra = extra
  183. }
  184. func (self *UiLib) ToggleMining() bool {
  185. if !self.Miner().Mining() {
  186. self.Miner().Start()
  187. return true
  188. } else {
  189. self.Miner().Stop()
  190. return false
  191. }
  192. }
  193. func (self *UiLib) ToHex(data string) string {
  194. return "0x" + ethutil.Bytes2Hex([]byte(data))
  195. }
  196. func (self *UiLib) ToAscii(data string) string {
  197. start := 0
  198. if len(data) > 1 && data[0:2] == "0x" {
  199. start = 2
  200. }
  201. return string(ethutil.Hex2Bytes(data[start:]))
  202. }
  203. /// Ethereum filter methods
  204. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  205. /* TODO remove me
  206. filter := qt.NewFilterFromMap(object, self.eth)
  207. filter.MessageCallback = func(messages state.Messages) {
  208. view.Call("messages", xeth.ToMessages(messages), id)
  209. }
  210. id = self.filterManager.InstallFilter(filter)
  211. return id
  212. */
  213. return 0
  214. }
  215. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  216. /* TODO remove me
  217. filter := core.NewFilter(self.eth)
  218. filter.BlockCallback = func(block *types.Block) {
  219. view.Call("messages", "{}", id)
  220. }
  221. id = self.filterManager.InstallFilter(filter)
  222. return id
  223. */
  224. return 0
  225. }
  226. func (self *UiLib) Messages(id int) *ethutil.List {
  227. /* TODO remove me
  228. filter := self.filterManager.GetFilter(id)
  229. if filter != nil {
  230. messages := xeth.ToMessages(filter.Find())
  231. return messages
  232. }
  233. */
  234. return ethutil.EmptyList()
  235. }
  236. func (self *UiLib) ReadFile(p string) string {
  237. content, err := ioutil.ReadFile(self.AssetPath(path.Join("ext", p)))
  238. if err != nil {
  239. guilogger.Infoln("error reading file", p, ":", err)
  240. }
  241. return string(content)
  242. }
  243. func (self *UiLib) UninstallFilter(id int) {
  244. self.filterManager.UninstallFilter(id)
  245. }
  246. func mapToTxParams(object map[string]interface{}) map[string]string {
  247. // Default values
  248. if object["from"] == nil {
  249. object["from"] = ""
  250. }
  251. if object["to"] == nil {
  252. object["to"] = ""
  253. }
  254. if object["value"] == nil {
  255. object["value"] = ""
  256. }
  257. if object["gas"] == nil {
  258. object["gas"] = ""
  259. }
  260. if object["gasPrice"] == nil {
  261. object["gasPrice"] = ""
  262. }
  263. var dataStr string
  264. var data []string
  265. if list, ok := object["data"].(*qml.List); ok {
  266. list.Convert(&data)
  267. } else if str, ok := object["data"].(string); ok {
  268. data = []string{str}
  269. }
  270. for _, str := range data {
  271. if ethutil.IsHex(str) {
  272. str = str[2:]
  273. if len(str) != 64 {
  274. str = ethutil.LeftPadString(str, 64)
  275. }
  276. } else {
  277. str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32))
  278. }
  279. dataStr += str
  280. }
  281. object["data"] = dataStr
  282. conv := make(map[string]string)
  283. for key, value := range object {
  284. if v, ok := value.(string); ok {
  285. conv[key] = v
  286. }
  287. }
  288. return conv
  289. }