hexface.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package xeth
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "sync/atomic"
  6. "github.com/ethereum/go-ethereum/core"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. "github.com/ethereum/go-ethereum/ethutil"
  10. "github.com/ethereum/go-ethereum/state"
  11. )
  12. type JSXEth struct {
  13. *XEth
  14. }
  15. func NewJSXEth(eth core.EthManager) *JSXEth {
  16. return &JSXEth{New(eth)}
  17. }
  18. func (self *JSXEth) BlockByHash(strHash string) *JSBlock {
  19. hash := ethutil.Hex2Bytes(strHash)
  20. block := self.obj.ChainManager().GetBlock(hash)
  21. return NewJSBlock(block)
  22. }
  23. func (self *JSXEth) BlockByNumber(num int32) *JSBlock {
  24. if num == -1 {
  25. return NewJSBlock(self.obj.ChainManager().CurrentBlock)
  26. }
  27. return NewJSBlock(self.obj.ChainManager().GetBlockByNumber(uint64(num)))
  28. }
  29. func (self *JSXEth) Block(v interface{}) *JSBlock {
  30. if n, ok := v.(int32); ok {
  31. return self.BlockByNumber(n)
  32. } else if str, ok := v.(string); ok {
  33. return self.BlockByHash(str)
  34. } else if f, ok := v.(float64); ok { // Don't ask ...
  35. return self.BlockByNumber(int32(f))
  36. }
  37. return nil
  38. }
  39. func (self *JSXEth) Key() *JSKey {
  40. return NewJSKey(self.obj.KeyManager().KeyPair())
  41. }
  42. func (self *JSXEth) StateObject(addr string) *JSObject {
  43. object := &Object{self.World().safeGet(ethutil.Hex2Bytes(addr))}
  44. return NewJSObject(object)
  45. }
  46. func (self *JSXEth) PeerCount() int {
  47. return self.obj.PeerCount()
  48. }
  49. func (self *JSXEth) Peers() []JSPeer {
  50. var peers []JSPeer
  51. for peer := self.obj.Peers().Front(); peer != nil; peer = peer.Next() {
  52. p := peer.Value.(core.Peer)
  53. // we only want connected peers
  54. if atomic.LoadInt32(p.Connected()) != 0 {
  55. peers = append(peers, *NewJSPeer(p))
  56. }
  57. }
  58. return peers
  59. }
  60. func (self *JSXEth) IsMining() bool {
  61. return self.obj.IsMining()
  62. }
  63. func (self *JSXEth) IsListening() bool {
  64. return self.obj.IsListening()
  65. }
  66. func (self *JSXEth) CoinBase() string {
  67. return ethutil.Bytes2Hex(self.obj.KeyManager().Address())
  68. }
  69. func (self *JSXEth) NumberToHuman(balance string) string {
  70. b := ethutil.Big(balance)
  71. return ethutil.CurrencyToString(b)
  72. }
  73. func (self *JSXEth) StorageAt(addr, storageAddr string) string {
  74. storage := self.World().SafeGet(ethutil.Hex2Bytes(addr)).Storage(ethutil.Hex2Bytes(storageAddr))
  75. return ethutil.Bytes2Hex(storage.Bytes())
  76. }
  77. func (self *JSXEth) BalanceAt(addr string) string {
  78. return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance().String()
  79. }
  80. func (self *JSXEth) TxCountAt(address string) int {
  81. return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce)
  82. }
  83. func (self *JSXEth) CodeAt(address string) string {
  84. return ethutil.Bytes2Hex(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code)
  85. }
  86. func (self *JSXEth) IsContract(address string) bool {
  87. return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0
  88. }
  89. func (self *JSXEth) SecretToAddress(key string) string {
  90. pair, err := crypto.NewKeyPairFromSec(ethutil.Hex2Bytes(key))
  91. if err != nil {
  92. return ""
  93. }
  94. return ethutil.Bytes2Hex(pair.Address())
  95. }
  96. func (self *JSXEth) Execute(addr, value, gas, price, data string) (string, error) {
  97. ret, err := self.ExecuteObject(&Object{
  98. self.World().safeGet(ethutil.Hex2Bytes(addr))},
  99. ethutil.Hex2Bytes(data),
  100. ethutil.NewValue(value),
  101. ethutil.NewValue(gas),
  102. ethutil.NewValue(price),
  103. )
  104. return ethutil.Bytes2Hex(ret), err
  105. }
  106. type KeyVal struct {
  107. Key string `json:"key"`
  108. Value string `json:"value"`
  109. }
  110. func (self *JSXEth) EachStorage(addr string) string {
  111. var values []KeyVal
  112. object := self.World().SafeGet(ethutil.Hex2Bytes(addr))
  113. object.EachStorage(func(name string, value *ethutil.Value) {
  114. value.Decode()
  115. values = append(values, KeyVal{ethutil.Bytes2Hex([]byte(name)), ethutil.Bytes2Hex(value.Bytes())})
  116. })
  117. valuesJson, err := json.Marshal(values)
  118. if err != nil {
  119. return ""
  120. }
  121. return string(valuesJson)
  122. }
  123. func (self *JSXEth) ToAscii(str string) string {
  124. padded := ethutil.RightPadBytes([]byte(str), 32)
  125. return "0x" + ethutil.Bytes2Hex(padded)
  126. }
  127. func (self *JSXEth) FromAscii(str string) string {
  128. if ethutil.IsHex(str) {
  129. str = str[2:]
  130. }
  131. return string(bytes.Trim(ethutil.Hex2Bytes(str), "\x00"))
  132. }
  133. func (self *JSXEth) FromNumber(str string) string {
  134. if ethutil.IsHex(str) {
  135. str = str[2:]
  136. }
  137. return ethutil.BigD(ethutil.Hex2Bytes(str)).String()
  138. }
  139. func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
  140. var (
  141. to []byte
  142. value = ethutil.NewValue(valueStr)
  143. gas = ethutil.NewValue(gasStr)
  144. gasPrice = ethutil.NewValue(gasPriceStr)
  145. data []byte
  146. )
  147. if ethutil.IsHex(codeStr) {
  148. data = ethutil.Hex2Bytes(codeStr[2:])
  149. } else {
  150. data = ethutil.Hex2Bytes(codeStr)
  151. }
  152. if ethutil.IsHex(toStr) {
  153. to = ethutil.Hex2Bytes(toStr[2:])
  154. } else {
  155. to = ethutil.Hex2Bytes(toStr)
  156. }
  157. var keyPair *crypto.KeyPair
  158. var err error
  159. if ethutil.IsHex(key) {
  160. keyPair, err = crypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key[2:])))
  161. } else {
  162. keyPair, err = crypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key)))
  163. }
  164. if err != nil {
  165. return "", err
  166. }
  167. tx, err := self.XEth.Transact(keyPair, to, value, gas, gasPrice, data)
  168. if err != nil {
  169. return "", err
  170. }
  171. if types.IsContractAddr(to) {
  172. return ethutil.Bytes2Hex(tx.CreationAddress(nil)), nil
  173. }
  174. return ethutil.Bytes2Hex(tx.Hash()), nil
  175. }
  176. func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) {
  177. tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr))
  178. err := self.obj.TxPool().Add(tx)
  179. if err != nil {
  180. return nil, err
  181. }
  182. return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(self.World().State()), tx.Hash(), tx.Sender()), nil
  183. }
  184. func (self *JSXEth) CompileMutan(code string) string {
  185. data, err := self.XEth.CompileMutan(code)
  186. if err != nil {
  187. return err.Error()
  188. }
  189. return ethutil.Bytes2Hex(data)
  190. }
  191. func (self *JSXEth) FindInConfig(str string) string {
  192. return ethutil.Bytes2Hex(self.World().Config().Get(str).Address())
  193. }
  194. func ToJSMessages(messages state.Messages) *ethutil.List {
  195. var msgs []JSMessage
  196. for _, m := range messages {
  197. msgs = append(msgs, NewJSMessage(m))
  198. }
  199. return ethutil.NewList(msgs)
  200. }