ui_lib.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. // MA 02110-1301 USA
  17. package main
  18. import (
  19. "bytes"
  20. "fmt"
  21. "path"
  22. "strconv"
  23. "strings"
  24. "github.com/ethereum/go-ethereum"
  25. "github.com/ethereum/go-ethereum/chain"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/ethutil"
  28. "github.com/ethereum/go-ethereum/javascript"
  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. }
  52. func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib {
  53. return &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)}
  54. }
  55. func (self *UiLib) Notef(args []interface{}) {
  56. guilogger.Infoln(args...)
  57. }
  58. func (self *UiLib) LookupDomain(domain string) string {
  59. world := self.World()
  60. if len(domain) > 32 {
  61. domain = string(crypto.Sha3([]byte(domain)))
  62. }
  63. data := world.Config().Get("DnsReg").StorageString(domain).Bytes()
  64. // Left padded = A record, Right padded = CNAME
  65. if len(data) > 0 && data[0] == 0 {
  66. data = bytes.TrimLeft(data, "\x00")
  67. var ipSlice []string
  68. for _, d := range data {
  69. ipSlice = append(ipSlice, strconv.Itoa(int(d)))
  70. }
  71. return strings.Join(ipSlice, ".")
  72. } else {
  73. data = bytes.TrimRight(data, "\x00")
  74. return string(data)
  75. }
  76. }
  77. func (self *UiLib) LookupName(addr string) string {
  78. var (
  79. nameReg = self.World().Config().Get("NameReg")
  80. lookup = nameReg.Storage(ethutil.Hex2Bytes(addr))
  81. )
  82. if lookup.Len() != 0 {
  83. return strings.Trim(lookup.Str(), "\x00")
  84. }
  85. return addr
  86. }
  87. func (self *UiLib) LookupAddress(name string) string {
  88. var (
  89. nameReg = self.World().Config().Get("NameReg")
  90. lookup = nameReg.Storage(ethutil.RightPadBytes([]byte(name), 32))
  91. )
  92. if lookup.Len() != 0 {
  93. return ethutil.Bytes2Hex(lookup.Bytes())
  94. }
  95. return ""
  96. }
  97. func (self *UiLib) PastPeers() *ethutil.List {
  98. return ethutil.NewList(eth.PastPeers())
  99. }
  100. func (self *UiLib) ImportTx(rlpTx string) {
  101. tx := chain.NewTransactionFromBytes(ethutil.Hex2Bytes(rlpTx))
  102. self.eth.TxPool().QueueTransaction(tx)
  103. }
  104. func (self *UiLib) EvalJavascriptFile(path string) {
  105. self.jsEngine.LoadExtFile(path[7:])
  106. }
  107. func (self *UiLib) EvalJavascriptString(str string) string {
  108. value, err := self.jsEngine.Run(str)
  109. if err != nil {
  110. return err.Error()
  111. }
  112. return fmt.Sprintf("%v", value)
  113. }
  114. func (ui *UiLib) OpenQml(path string) {
  115. container := NewQmlApplication(path[7:], ui)
  116. app := NewExtApplication(container, ui)
  117. go app.run()
  118. }
  119. func (ui *UiLib) OpenHtml(path string) {
  120. container := NewHtmlApplication(path, ui)
  121. app := NewExtApplication(container, ui)
  122. go app.run()
  123. }
  124. func (ui *UiLib) OpenBrowser() {
  125. ui.OpenHtml("file://" + ui.AssetPath("ext/home.html"))
  126. }
  127. func (ui *UiLib) Muted(content string) {
  128. component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml"))
  129. if err != nil {
  130. guilogger.Debugln(err)
  131. return
  132. }
  133. win := component.CreateWindow(nil)
  134. go func() {
  135. path := "file://" + ui.AssetPath("muted/index.html")
  136. win.Set("url", path)
  137. win.Show()
  138. win.Wait()
  139. }()
  140. }
  141. func (ui *UiLib) Connect(button qml.Object) {
  142. if !ui.connected {
  143. ui.eth.Start(true)
  144. ui.connected = true
  145. button.Set("enabled", false)
  146. }
  147. }
  148. func (ui *UiLib) ConnectToPeer(addr string) {
  149. ui.eth.ConnectToPeer(addr)
  150. }
  151. func (ui *UiLib) AssetPath(p string) string {
  152. return path.Join(ui.assetPath, p)
  153. }
  154. func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
  155. dbWindow := NewDebuggerWindow(self)
  156. object := self.eth.BlockManager().CurrentState().GetStateObject(ethutil.Hex2Bytes(contractHash))
  157. if len(object.Code) > 0 {
  158. dbWindow.SetCode("0x" + ethutil.Bytes2Hex(object.Code))
  159. }
  160. dbWindow.SetData("0x" + data)
  161. dbWindow.Show()
  162. }
  163. func (self *UiLib) StartDbWithCode(code string) {
  164. dbWindow := NewDebuggerWindow(self)
  165. dbWindow.SetCode("0x" + code)
  166. dbWindow.Show()
  167. }
  168. func (self *UiLib) StartDebugger() {
  169. dbWindow := NewDebuggerWindow(self)
  170. dbWindow.Show()
  171. }
  172. func (self *UiLib) NewFilter(object map[string]interface{}) (id int) {
  173. filter := qt.NewFilterFromMap(object, self.eth)
  174. filter.MessageCallback = func(messages state.Messages) {
  175. self.win.Root().Call("invokeFilterCallback", xeth.ToJSMessages(messages), id)
  176. }
  177. id = self.eth.InstallFilter(filter)
  178. return id
  179. }
  180. func (self *UiLib) NewFilterString(typ string) (id int) {
  181. filter := chain.NewFilter(self.eth)
  182. filter.BlockCallback = func(block *chain.Block) {
  183. self.win.Root().Call("invokeFilterCallback", "{}", id)
  184. }
  185. id = self.eth.InstallFilter(filter)
  186. return id
  187. }
  188. func (self *UiLib) Messages(id int) *ethutil.List {
  189. filter := self.eth.GetFilter(id)
  190. if filter != nil {
  191. messages := xeth.ToJSMessages(filter.Find())
  192. return messages
  193. }
  194. return ethutil.EmptyList()
  195. }
  196. func (self *UiLib) UninstallFilter(id int) {
  197. self.eth.UninstallFilter(id)
  198. }
  199. func mapToTxParams(object map[string]interface{}) map[string]string {
  200. // Default values
  201. if object["from"] == nil {
  202. object["from"] = ""
  203. }
  204. if object["to"] == nil {
  205. object["to"] = ""
  206. }
  207. if object["value"] == nil {
  208. object["value"] = ""
  209. }
  210. if object["gas"] == nil {
  211. object["gas"] = ""
  212. }
  213. if object["gasPrice"] == nil {
  214. object["gasPrice"] = ""
  215. }
  216. var dataStr string
  217. var data []string
  218. if list, ok := object["data"].(*qml.List); ok {
  219. list.Convert(&data)
  220. } else if str, ok := object["data"].(string); ok {
  221. data = []string{str}
  222. }
  223. for _, str := range data {
  224. if ethutil.IsHex(str) {
  225. str = str[2:]
  226. if len(str) != 64 {
  227. str = ethutil.LeftPadString(str, 64)
  228. }
  229. } else {
  230. str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32))
  231. }
  232. dataStr += str
  233. }
  234. object["data"] = dataStr
  235. conv := make(map[string]string)
  236. for key, value := range object {
  237. if v, ok := value.(string); ok {
  238. conv[key] = v
  239. }
  240. }
  241. return conv
  242. }
  243. func (self *UiLib) Transact(params map[string]interface{}) (*xeth.JSReceipt, error) {
  244. object := mapToTxParams(params)
  245. return self.JSXEth.Transact(
  246. object["from"],
  247. object["to"],
  248. object["value"],
  249. object["gas"],
  250. object["gasPrice"],
  251. object["data"],
  252. )
  253. }
  254. func (self *UiLib) Compile(code string) (string, error) {
  255. bcode, err := ethutil.Compile(code, false)
  256. if err != nil {
  257. return err.Error(), err
  258. }
  259. return ethutil.Bytes2Hex(bcode), err
  260. }
  261. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  262. object := mapToTxParams(params)
  263. return self.JSXEth.Execute(
  264. object["to"],
  265. object["value"],
  266. object["gas"],
  267. object["gasPrice"],
  268. object["data"],
  269. )
  270. }