ui_lib.go 9.0 KB

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