ui_lib.go 9.0 KB

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