| 
									
										
										
										
											2014-10-31 14:30:08 +01:00
										 |  |  | package xeth | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2014-09-26 13:45:26 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2014-10-31 12:37:43 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							| 
									
										
										
										
											2014-10-23 15:01:27 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethutil" | 
					
						
							| 
									
										
										
										
											2014-10-31 14:43:14 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/state" | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Block interface exposed to QML | 
					
						
							|  |  |  | type JSBlock struct { | 
					
						
							| 
									
										
										
										
											2014-09-08 00:49:47 +02:00
										 |  |  | 	//Transactions string `json:"transactions"` | 
					
						
							| 
									
										
										
										
											2014-11-18 16:58:22 +01:00
										 |  |  | 	ref          *types.Block | 
					
						
							| 
									
										
										
										
											2014-09-08 00:49:47 +02:00
										 |  |  | 	Size         string        `json:"size"` | 
					
						
							|  |  |  | 	Number       int           `json:"number"` | 
					
						
							|  |  |  | 	Hash         string        `json:"hash"` | 
					
						
							|  |  |  | 	Transactions *ethutil.List `json:"transactions"` | 
					
						
							| 
									
										
										
										
											2014-10-31 10:50:16 +01:00
										 |  |  | 	Uncles       *ethutil.List `json:"uncles"` | 
					
						
							| 
									
										
										
										
											2014-09-08 00:49:47 +02:00
										 |  |  | 	Time         int64         `json:"time"` | 
					
						
							|  |  |  | 	Coinbase     string        `json:"coinbase"` | 
					
						
							|  |  |  | 	Name         string        `json:"name"` | 
					
						
							|  |  |  | 	GasLimit     string        `json:"gasLimit"` | 
					
						
							|  |  |  | 	GasUsed      string        `json:"gasUsed"` | 
					
						
							| 
									
										
										
										
											2014-09-25 16:57:49 +02:00
										 |  |  | 	PrevHash     string        `json:"prevHash"` | 
					
						
							| 
									
										
										
										
											2014-11-10 01:17:31 +01:00
										 |  |  | 	Bloom        string        `json:"bloom"` | 
					
						
							|  |  |  | 	Raw          string        `json:"raw"` | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Creates a new QML Block from a chain block | 
					
						
							| 
									
										
										
										
											2014-11-18 16:58:22 +01:00
										 |  |  | func NewJSBlock(block *types.Block) *JSBlock { | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	if block == nil { | 
					
						
							| 
									
										
										
										
											2014-09-25 16:57:49 +02:00
										 |  |  | 		return &JSBlock{} | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 10:50:16 +01:00
										 |  |  | 	ptxs := make([]*JSTransaction, len(block.Transactions())) | 
					
						
							|  |  |  | 	for i, tx := range block.Transactions() { | 
					
						
							|  |  |  | 		ptxs[i] = NewJSTx(tx, block.State()) | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-31 10:50:16 +01:00
										 |  |  | 	txlist := ethutil.NewList(ptxs) | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 10:50:16 +01:00
										 |  |  | 	puncles := make([]*JSBlock, len(block.Uncles)) | 
					
						
							|  |  |  | 	for i, uncle := range block.Uncles { | 
					
						
							|  |  |  | 		puncles[i] = NewJSBlock(uncle) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ulist := ethutil.NewList(puncles) | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 16:57:49 +02:00
										 |  |  | 	return &JSBlock{ | 
					
						
							|  |  |  | 		ref: block, Size: block.Size().String(), | 
					
						
							|  |  |  | 		Number: int(block.Number.Uint64()), GasUsed: block.GasUsed.String(), | 
					
						
							|  |  |  | 		GasLimit: block.GasLimit.String(), Hash: ethutil.Bytes2Hex(block.Hash()), | 
					
						
							| 
									
										
										
										
											2014-10-31 10:50:16 +01:00
										 |  |  | 		Transactions: txlist, Uncles: ulist, | 
					
						
							|  |  |  | 		Time:     block.Time, | 
					
						
							| 
									
										
										
										
											2014-09-25 16:57:49 +02:00
										 |  |  | 		Coinbase: ethutil.Bytes2Hex(block.Coinbase), | 
					
						
							|  |  |  | 		PrevHash: ethutil.Bytes2Hex(block.PrevHash), | 
					
						
							| 
									
										
										
										
											2014-11-10 01:17:31 +01:00
										 |  |  | 		Bloom:    ethutil.Bytes2Hex(block.LogsBloom), | 
					
						
							|  |  |  | 		Raw:      block.String(), | 
					
						
							| 
									
										
										
										
											2014-09-25 16:57:49 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 17:03:15 +02:00
										 |  |  | 	return NewJSTx(tx, self.ref.State()) | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type JSTransaction struct { | 
					
						
							| 
									
										
										
										
											2014-11-18 16:58:22 +01:00
										 |  |  | 	ref *types.Transaction | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 11:40:20 +01:00
										 |  |  | func NewJSTx(tx *types.Transaction, state *state.StateDB) *JSTransaction { | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	hash := ethutil.Bytes2Hex(tx.Hash()) | 
					
						
							|  |  |  | 	receiver := ethutil.Bytes2Hex(tx.Recipient) | 
					
						
							|  |  |  | 	if receiver == "0000000000000000000000000000000000000000" { | 
					
						
							| 
									
										
										
										
											2014-10-02 17:03:15 +02:00
										 |  |  | 		receiver = ethutil.Bytes2Hex(tx.CreationAddress(state)) | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	sender := ethutil.Bytes2Hex(tx.Sender()) | 
					
						
							|  |  |  | 	createsContract := tx.CreatesContract() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var data string | 
					
						
							|  |  |  | 	if tx.CreatesContract() { | 
					
						
							| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | 		data = strings.Join(core.Disassemble(tx.Data), "\n") | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	} 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"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 12:37:43 +01:00
										 |  |  | func NewJSKey(key *crypto.KeyPair) *JSKey { | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	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 { | 
					
						
							| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | 	ref          *core.Peer | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	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"` | 
					
						
							| 
									
										
										
										
											2014-09-26 13:45:26 +02:00
										 |  |  | 	Caps         string `json:"caps"` | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | func NewJSPeer(peer core.Peer) *JSPeer { | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | 	if peer == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ip []string | 
					
						
							|  |  |  | 	for _, i := range peer.Host() { | 
					
						
							|  |  |  | 		ip = append(ip, strconv.Itoa(int(i))) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ipAddress := strings.Join(ip, ".") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 13:45:26 +02:00
										 |  |  | 	var caps []string | 
					
						
							|  |  |  | 	capsIt := peer.Caps().NewIterator() | 
					
						
							|  |  |  | 	for capsIt.Next() { | 
					
						
							| 
									
										
										
										
											2014-11-04 01:47:02 +01:00
										 |  |  | 		cap := capsIt.Value().Get(0).Str() | 
					
						
							|  |  |  | 		ver := capsIt.Value().Get(1).Uint() | 
					
						
							|  |  |  | 		caps = append(caps, fmt.Sprintf("%s/%d", cap, ver)) | 
					
						
							| 
									
										
										
										
											2014-09-26 13:45:26 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-04 01:47:02 +01:00
										 |  |  | 	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(), Caps: "[" + strings.Join(caps, ", ") + "]"} | 
					
						
							| 
									
										
										
										
											2014-08-15 13:05:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-08-18 10:17:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | type JSMessage struct { | 
					
						
							|  |  |  | 	To        string `json:"to"` | 
					
						
							|  |  |  | 	From      string `json:"from"` | 
					
						
							|  |  |  | 	Input     string `json:"input"` | 
					
						
							|  |  |  | 	Output    string `json:"output"` | 
					
						
							|  |  |  | 	Path      int32  `json:"path"` | 
					
						
							|  |  |  | 	Origin    string `json:"origin"` | 
					
						
							|  |  |  | 	Timestamp int32  `json:"timestamp"` | 
					
						
							|  |  |  | 	Coinbase  string `json:"coinbase"` | 
					
						
							|  |  |  | 	Block     string `json:"block"` | 
					
						
							|  |  |  | 	Number    int32  `json:"number"` | 
					
						
							|  |  |  | 	Value     string `json:"value"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 14:43:14 +01:00
										 |  |  | func NewJSMessage(message *state.Message) JSMessage { | 
					
						
							| 
									
										
										
										
											2014-08-18 10:17:45 +02:00
										 |  |  | 	return JSMessage{ | 
					
						
							|  |  |  | 		To:        ethutil.Bytes2Hex(message.To), | 
					
						
							|  |  |  | 		From:      ethutil.Bytes2Hex(message.From), | 
					
						
							|  |  |  | 		Input:     ethutil.Bytes2Hex(message.Input), | 
					
						
							|  |  |  | 		Output:    ethutil.Bytes2Hex(message.Output), | 
					
						
							|  |  |  | 		Path:      int32(message.Path), | 
					
						
							|  |  |  | 		Origin:    ethutil.Bytes2Hex(message.Origin), | 
					
						
							|  |  |  | 		Timestamp: int32(message.Timestamp), | 
					
						
							|  |  |  | 		Coinbase:  ethutil.Bytes2Hex(message.Origin), | 
					
						
							|  |  |  | 		Block:     ethutil.Bytes2Hex(message.Block), | 
					
						
							|  |  |  | 		Number:    int32(message.Number.Int64()), | 
					
						
							|  |  |  | 		Value:     message.Value.String(), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |