ui_lib.go 7.7 KB

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