ui_lib.go 7.9 KB

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