Reworking the public js interface (pub) => (pipe)
This commit is contained in:
		
							
								
								
									
										168
									
								
								ethpipe/js_pipe.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										168
									
								
								ethpipe/js_pipe.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,168 @@ | |||||||
|  | package ethpipe | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"sync/atomic" | ||||||
|  |  | ||||||
|  | 	"github.com/ethereum/eth-go/ethchain" | ||||||
|  | 	"github.com/ethereum/eth-go/ethcrypto" | ||||||
|  | 	"github.com/ethereum/eth-go/ethutil" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type JSPipe struct { | ||||||
|  | 	*Pipe | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewJSPipe(eth ethchain.EthManager) *JSPipe { | ||||||
|  | 	return &JSPipe{New(eth)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetBlockByHash(strHash string) *JSBlock { | ||||||
|  | 	hash := ethutil.Hex2Bytes(strHash) | ||||||
|  | 	block := self.obj.BlockChain().GetBlock(hash) | ||||||
|  |  | ||||||
|  | 	return NewJSBlock(block) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetKey() *JSKey { | ||||||
|  | 	return NewJSKey(self.obj.KeyManager().KeyPair()) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetStateObject(addr string) *JSObject { | ||||||
|  | 	object := &Object{self.World().safeGet(ethutil.Hex2Bytes(addr))} | ||||||
|  |  | ||||||
|  | 	return NewJSObject(object) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetPeerCount() int { | ||||||
|  | 	return self.obj.PeerCount() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetPeers() []JSPeer { | ||||||
|  | 	var peers []JSPeer | ||||||
|  | 	for peer := self.obj.Peers().Front(); peer != nil; peer = peer.Next() { | ||||||
|  | 		p := peer.Value.(ethchain.Peer) | ||||||
|  | 		// we only want connected peers | ||||||
|  | 		if atomic.LoadInt32(p.Connected()) != 0 { | ||||||
|  | 			peers = append(peers, *NewJSPeer(p)) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return peers | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetIsMining() bool { | ||||||
|  | 	return self.obj.IsMining() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetIsListening() bool { | ||||||
|  | 	return self.obj.IsListening() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetCoinBase() string { | ||||||
|  | 	return ethutil.Bytes2Hex(self.obj.KeyManager().Address()) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetStorage(addr, storageAddr string) string { | ||||||
|  | 	return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Storage(ethutil.Hex2Bytes(storageAddr)).Str() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetTxCountAt(address string) int { | ||||||
|  | 	return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) IsContract(address string) bool { | ||||||
|  | 	return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) SecretToAddress(key string) string { | ||||||
|  | 	pair, err := ethcrypto.NewKeyPairFromSec(ethutil.Hex2Bytes(key)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "" | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return ethutil.Bytes2Hex(pair.Address()) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type KeyVal struct { | ||||||
|  | 	Key   string `json:"key"` | ||||||
|  | 	Value string `json:"value"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) GetEachStorage(addr string) string { | ||||||
|  | 	var values []KeyVal | ||||||
|  | 	object := self.World().SafeGet(ethutil.Hex2Bytes(addr)) | ||||||
|  | 	object.EachStorage(func(name string, value *ethutil.Value) { | ||||||
|  | 		value.Decode() | ||||||
|  | 		values = append(values, KeyVal{ethutil.Bytes2Hex([]byte(name)), ethutil.Bytes2Hex(value.Bytes())}) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	valuesJson, err := json.Marshal(values) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "" | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return string(valuesJson) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSPipe) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (*JSReceipt, error) { | ||||||
|  | 	var hash []byte | ||||||
|  | 	var contractCreation bool | ||||||
|  | 	if len(toStr) == 0 { | ||||||
|  | 		contractCreation = true | ||||||
|  | 	} else { | ||||||
|  | 		// Check if an address is stored by this address | ||||||
|  | 		addr := self.World().Config().Get("NameReg").StorageString(toStr).Bytes() | ||||||
|  | 		if len(addr) > 0 { | ||||||
|  | 			hash = addr | ||||||
|  | 		} else { | ||||||
|  | 			hash = ethutil.Hex2Bytes(toStr) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var keyPair *ethcrypto.KeyPair | ||||||
|  | 	var err error | ||||||
|  | 	if ethutil.IsHex(key) { | ||||||
|  | 		keyPair, err = ethcrypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key[2:]))) | ||||||
|  | 	} else { | ||||||
|  | 		keyPair, err = ethcrypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key))) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var ( | ||||||
|  | 		value    = ethutil.Big(valueStr) | ||||||
|  | 		gas      = ethutil.Big(gasStr) | ||||||
|  | 		gasPrice = ethutil.Big(gasPriceStr) | ||||||
|  | 		data     []byte | ||||||
|  | 		tx       *ethchain.Transaction | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	if ethutil.IsHex(codeStr) { | ||||||
|  | 		data = ethutil.Hex2Bytes(codeStr[2:]) | ||||||
|  | 	} else { | ||||||
|  | 		data = ethutil.Hex2Bytes(codeStr) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if contractCreation { | ||||||
|  | 		tx = ethchain.NewContractCreationTx(value, gas, gasPrice, data) | ||||||
|  | 	} else { | ||||||
|  | 		tx = ethchain.NewTransactionMessage(hash, value, gas, gasPrice, data) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	acc := self.obj.StateManager().TransState().GetOrNewStateObject(keyPair.Address()) | ||||||
|  | 	tx.Nonce = acc.Nonce | ||||||
|  | 	acc.Nonce += 1 | ||||||
|  | 	self.obj.StateManager().TransState().UpdateStateObject(acc) | ||||||
|  |  | ||||||
|  | 	tx.Sign(keyPair.PrivateKey) | ||||||
|  | 	self.obj.TxPool().QueueTransaction(tx) | ||||||
|  |  | ||||||
|  | 	if contractCreation { | ||||||
|  | 		logger.Infof("Contract addr %x", tx.CreationAddress()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return NewJSReciept(contractCreation, tx.CreationAddress(), tx.Hash(), keyPair.Address()), nil | ||||||
|  | } | ||||||
							
								
								
									
										177
									
								
								ethpipe/js_types.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										177
									
								
								ethpipe/js_types.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,177 @@ | |||||||
|  | package ethpipe | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
|  | 	"github.com/ethereum/eth-go/ethchain" | ||||||
|  | 	"github.com/ethereum/eth-go/ethcrypto" | ||||||
|  | 	"github.com/ethereum/eth-go/ethutil" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Block interface exposed to QML | ||||||
|  | type JSBlock struct { | ||||||
|  | 	ref          *ethchain.Block | ||||||
|  | 	Number       int    `json:"number"` | ||||||
|  | 	Hash         string `json:"hash"` | ||||||
|  | 	Transactions string `json:"transactions"` | ||||||
|  | 	Time         int64  `json:"time"` | ||||||
|  | 	Coinbase     string `json:"coinbase"` | ||||||
|  | 	Name         string `json:"name"` | ||||||
|  | 	GasLimit     string `json:"gasLimit"` | ||||||
|  | 	GasUsed      string `json:"gasUsed"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Creates a new QML Block from a chain block | ||||||
|  | func NewJSBlock(block *ethchain.Block) *JSBlock { | ||||||
|  | 	if block == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var ptxs []JSTransaction | ||||||
|  | 	for _, tx := range block.Transactions() { | ||||||
|  | 		ptxs = append(ptxs, *NewJSTx(tx)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	txJson, err := json.Marshal(ptxs) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return &JSBlock{ref: block, Number: int(block.Number.Uint64()), GasUsed: block.GasUsed.String(), GasLimit: block.GasLimit.String(), Hash: ethutil.Bytes2Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Bytes2Hex(block.Coinbase)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSBlock) ToString() string { | ||||||
|  | 	if self.ref != nil { | ||||||
|  | 		return self.ref.String() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return "" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSBlock) GetTransaction(hash string) *JSTransaction { | ||||||
|  | 	tx := self.ref.GetTransaction(ethutil.Hex2Bytes(hash)) | ||||||
|  | 	if tx == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return NewJSTx(tx) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type JSTransaction struct { | ||||||
|  | 	ref *ethchain.Transaction | ||||||
|  |  | ||||||
|  | 	Value           string `json:"value"` | ||||||
|  | 	Gas             string `json:"gas"` | ||||||
|  | 	GasPrice        string `json:"gasPrice"` | ||||||
|  | 	Hash            string `json:"hash"` | ||||||
|  | 	Address         string `json:"address"` | ||||||
|  | 	Sender          string `json:"sender"` | ||||||
|  | 	RawData         string `json:"rawData"` | ||||||
|  | 	Data            string `json:"data"` | ||||||
|  | 	Contract        bool   `json:"isContract"` | ||||||
|  | 	CreatesContract bool   `json:"createsContract"` | ||||||
|  | 	Confirmations   int    `json:"confirmations"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewJSTx(tx *ethchain.Transaction) *JSTransaction { | ||||||
|  | 	hash := ethutil.Bytes2Hex(tx.Hash()) | ||||||
|  | 	receiver := ethutil.Bytes2Hex(tx.Recipient) | ||||||
|  | 	if receiver == "0000000000000000000000000000000000000000" { | ||||||
|  | 		receiver = ethutil.Bytes2Hex(tx.CreationAddress()) | ||||||
|  | 	} | ||||||
|  | 	sender := ethutil.Bytes2Hex(tx.Sender()) | ||||||
|  | 	createsContract := tx.CreatesContract() | ||||||
|  |  | ||||||
|  | 	var data string | ||||||
|  | 	if tx.CreatesContract() { | ||||||
|  | 		data = strings.Join(ethchain.Disassemble(tx.Data), "\n") | ||||||
|  | 	} else { | ||||||
|  | 		data = ethutil.Bytes2Hex(tx.Data) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: receiver, Contract: tx.CreatesContract(), Gas: tx.Gas.String(), GasPrice: tx.GasPrice.String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (self *JSTransaction) ToString() string { | ||||||
|  | 	return self.ref.String() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type JSKey struct { | ||||||
|  | 	Address    string `json:"address"` | ||||||
|  | 	PrivateKey string `json:"privateKey"` | ||||||
|  | 	PublicKey  string `json:"publicKey"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewJSKey(key *ethcrypto.KeyPair) *JSKey { | ||||||
|  | 	return &JSKey{ethutil.Bytes2Hex(key.Address()), ethutil.Bytes2Hex(key.PrivateKey), ethutil.Bytes2Hex(key.PublicKey)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type JSObject struct { | ||||||
|  | 	*Object | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewJSObject(object *Object) *JSObject { | ||||||
|  | 	return &JSObject{object} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type PReceipt struct { | ||||||
|  | 	CreatedContract bool   `json:"createdContract"` | ||||||
|  | 	Address         string `json:"address"` | ||||||
|  | 	Hash            string `json:"hash"` | ||||||
|  | 	Sender          string `json:"sender"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { | ||||||
|  | 	return &PReceipt{ | ||||||
|  | 		contractCreation, | ||||||
|  | 		ethutil.Bytes2Hex(creationAddress), | ||||||
|  | 		ethutil.Bytes2Hex(hash), | ||||||
|  | 		ethutil.Bytes2Hex(address), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Peer interface exposed to QML | ||||||
|  |  | ||||||
|  | type JSPeer struct { | ||||||
|  | 	ref          *ethchain.Peer | ||||||
|  | 	Inbound      bool   `json:"isInbound"` | ||||||
|  | 	LastSend     int64  `json:"lastSend"` | ||||||
|  | 	LastPong     int64  `json:"lastPong"` | ||||||
|  | 	Ip           string `json:"ip"` | ||||||
|  | 	Port         int    `json:"port"` | ||||||
|  | 	Version      string `json:"version"` | ||||||
|  | 	LastResponse string `json:"lastResponse"` | ||||||
|  | 	Latency      string `json:"latency"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewJSPeer(peer ethchain.Peer) *JSPeer { | ||||||
|  | 	if peer == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var ip []string | ||||||
|  | 	for _, i := range peer.Host() { | ||||||
|  | 		ip = append(ip, strconv.Itoa(int(i))) | ||||||
|  | 	} | ||||||
|  | 	ipAddress := strings.Join(ip, ".") | ||||||
|  |  | ||||||
|  | 	return &JSPeer{ref: &peer, Inbound: peer.Inbound(), LastSend: peer.LastSend().Unix(), LastPong: peer.LastPong(), Version: peer.Version(), Ip: ipAddress, Port: int(peer.Port()), Latency: peer.PingTime()} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type JSReceipt struct { | ||||||
|  | 	CreatedContract bool   `json:"createdContract"` | ||||||
|  | 	Address         string `json:"address"` | ||||||
|  | 	Hash            string `json:"hash"` | ||||||
|  | 	Sender          string `json:"sender"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewJSReciept(contractCreation bool, creationAddress, hash, address []byte) *JSReceipt { | ||||||
|  | 	return &JSReceipt{ | ||||||
|  | 		contractCreation, | ||||||
|  | 		ethutil.Bytes2Hex(creationAddress), | ||||||
|  | 		ethutil.Bytes2Hex(hash), | ||||||
|  | 		ethutil.Bytes2Hex(address), | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -30,6 +30,10 @@ func (self *World) Get(addr []byte) *Object { | |||||||
| 	return &Object{self.State().GetStateObject(addr)} | 	return &Object{self.State().GetStateObject(addr)} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (self *World) SafeGet(addr []byte) *Object { | ||||||
|  | 	return &Object{self.safeGet(addr)} | ||||||
|  | } | ||||||
|  |  | ||||||
| func (self *World) safeGet(addr []byte) *ethstate.StateObject { | func (self *World) safeGet(addr []byte) *ethstate.StateObject { | ||||||
| 	object := self.State().GetStateObject(addr) | 	object := self.State().GetStateObject(addr) | ||||||
| 	if object == nil { | 	if object == nil { | ||||||
|   | |||||||
| @@ -52,12 +52,6 @@ type PEthereum struct { | |||||||
| 	keyManager   *ethcrypto.KeyManager | 	keyManager   *ethcrypto.KeyManager | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewPEthereum(manager ethchain.EthManager) *PEthereum { |  | ||||||
| 	logger.Warnln("DEPRECATED: ethpub.New should be used in favour of ethpub.NewPEthereum") |  | ||||||
|  |  | ||||||
| 	return New(manager) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func New(manager ethchain.EthManager) *PEthereum { | func New(manager ethchain.EthManager) *PEthereum { | ||||||
| 	return &PEthereum{ | 	return &PEthereum{ | ||||||
| 		manager, | 		manager, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user