ui_lib.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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(SeedNode)
  113. ui.connected = true
  114. button.Set("enabled", false)
  115. }
  116. }
  117. func (ui *UiLib) ConnectToPeer(nodeURL string) {
  118. if err := ui.eth.SuggestPeer(nodeURL); err != nil {
  119. guilogger.Infoln("SuggestPeer error: " + err.Error())
  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["to"],
  147. object["value"],
  148. object["gas"],
  149. object["gasPrice"],
  150. object["data"],
  151. )
  152. }
  153. func (self *UiLib) Compile(code string) (string, error) {
  154. bcode, err := ethutil.Compile(code, false)
  155. if err != nil {
  156. return err.Error(), err
  157. }
  158. return ethutil.Bytes2Hex(bcode), err
  159. }
  160. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  161. object := mapToTxParams(params)
  162. return self.XEth.Execute(
  163. object["to"],
  164. object["value"],
  165. object["gas"],
  166. object["gasPrice"],
  167. object["data"],
  168. )
  169. }
  170. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  171. return self.miner.AddLocalTx(&miner.LocalTx{
  172. To: ethutil.Hex2Bytes(to),
  173. Data: ethutil.Hex2Bytes(data),
  174. Gas: gas,
  175. GasPrice: gasPrice,
  176. Value: value,
  177. }) - 1
  178. }
  179. func (self *UiLib) RemoveLocalTransaction(id int) {
  180. self.miner.RemoveLocalTx(id)
  181. }
  182. func (self *UiLib) SetGasPrice(price string) {
  183. self.miner.MinAcceptedGasPrice = ethutil.Big(price)
  184. }
  185. func (self *UiLib) SetExtra(extra string) {
  186. self.miner.Extra = extra
  187. }
  188. func (self *UiLib) ToggleMining() bool {
  189. if !self.miner.Mining() {
  190. self.miner.Start()
  191. return true
  192. } else {
  193. self.miner.Stop()
  194. return false
  195. }
  196. }
  197. func (self *UiLib) ToHex(data string) string {
  198. return "0x" + ethutil.Bytes2Hex([]byte(data))
  199. }
  200. func (self *UiLib) ToAscii(data string) string {
  201. start := 0
  202. if len(data) > 1 && data[0:2] == "0x" {
  203. start = 2
  204. }
  205. return string(ethutil.Hex2Bytes(data[start:]))
  206. }
  207. /// Ethereum filter methods
  208. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  209. /* TODO remove me
  210. filter := qt.NewFilterFromMap(object, self.eth)
  211. filter.MessageCallback = func(messages state.Messages) {
  212. view.Call("messages", xeth.ToMessages(messages), id)
  213. }
  214. id = self.filterManager.InstallFilter(filter)
  215. return id
  216. */
  217. return 0
  218. }
  219. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  220. /* TODO remove me
  221. filter := core.NewFilter(self.eth)
  222. filter.BlockCallback = func(block *types.Block) {
  223. view.Call("messages", "{}", id)
  224. }
  225. id = self.filterManager.InstallFilter(filter)
  226. return id
  227. */
  228. return 0
  229. }
  230. func (self *UiLib) Messages(id int) *ethutil.List {
  231. /* TODO remove me
  232. filter := self.filterManager.GetFilter(id)
  233. if filter != nil {
  234. messages := xeth.ToMessages(filter.Find())
  235. return messages
  236. }
  237. */
  238. return ethutil.EmptyList()
  239. }
  240. func (self *UiLib) ReadFile(p string) string {
  241. content, err := ioutil.ReadFile(self.AssetPath(path.Join("ext", p)))
  242. if err != nil {
  243. guilogger.Infoln("error reading file", p, ":", err)
  244. }
  245. return string(content)
  246. }
  247. func (self *UiLib) UninstallFilter(id int) {
  248. self.filterManager.UninstallFilter(id)
  249. }
  250. func mapToTxParams(object map[string]interface{}) map[string]string {
  251. // Default values
  252. if object["from"] == nil {
  253. object["from"] = ""
  254. }
  255. if object["to"] == nil {
  256. object["to"] = ""
  257. }
  258. if object["value"] == nil {
  259. object["value"] = ""
  260. }
  261. if object["gas"] == nil {
  262. object["gas"] = ""
  263. }
  264. if object["gasPrice"] == nil {
  265. object["gasPrice"] = ""
  266. }
  267. var dataStr string
  268. var data []string
  269. if list, ok := object["data"].(*qml.List); ok {
  270. list.Convert(&data)
  271. } else if str, ok := object["data"].(string); ok {
  272. data = []string{str}
  273. }
  274. for _, str := range data {
  275. if ethutil.IsHex(str) {
  276. str = str[2:]
  277. if len(str) != 64 {
  278. str = ethutil.LeftPadString(str, 64)
  279. }
  280. } else {
  281. str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32))
  282. }
  283. dataStr += str
  284. }
  285. object["data"] = dataStr
  286. conv := make(map[string]string)
  287. for key, value := range object {
  288. if v, ok := value.(string); ok {
  289. conv[key] = v
  290. }
  291. }
  292. return conv
  293. }