mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
61 lines
1.3 KiB
11 years ago
|
package ethui
|
||
|
|
||
|
import (
|
||
|
"encoding/hex"
|
||
|
"fmt"
|
||
|
"github.com/ethereum/eth-go/ethchain"
|
||
|
"github.com/ethereum/eth-go/ethutil"
|
||
11 years ago
|
"strings"
|
||
11 years ago
|
)
|
||
|
|
||
|
type EthLib struct {
|
||
11 years ago
|
stateManager *ethchain.StateManager
|
||
11 years ago
|
blockChain *ethchain.BlockChain
|
||
|
txPool *ethchain.TxPool
|
||
|
}
|
||
|
|
||
11 years ago
|
func (lib *EthLib) CreateTx(receiver, a, data string) string {
|
||
|
var hash []byte
|
||
|
if len(receiver) == 0 {
|
||
|
hash = ethchain.ContractAddr
|
||
|
} else {
|
||
|
var err error
|
||
|
hash, err = hex.DecodeString(receiver)
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
k, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
|
||
11 years ago
|
keyPair := ethutil.NewKeyFromBytes(k)
|
||
11 years ago
|
|
||
|
amount := ethutil.Big(a)
|
||
11 years ago
|
code := ethchain.Compile(strings.Split(data, "\n"))
|
||
|
tx := ethchain.NewTransaction(hash, amount, code)
|
||
11 years ago
|
tx.Nonce = lib.stateManager.GetAddrState(keyPair.Address()).Nonce
|
||
11 years ago
|
|
||
11 years ago
|
tx.Sign(keyPair.PrivateKey)
|
||
11 years ago
|
|
||
|
lib.txPool.QueueTransaction(tx)
|
||
|
|
||
11 years ago
|
if len(receiver) == 0 {
|
||
|
ethutil.Config.Log.Infof("Contract addr %x", tx.Hash()[12:])
|
||
11 years ago
|
} else {
|
||
|
ethutil.Config.Log.Infof("Tx hash %x", tx.Hash())
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
return ethutil.Hex(tx.Hash())
|
||
|
}
|
||
|
|
||
|
func (lib *EthLib) GetBlock(hexHash string) *Block {
|
||
|
hash, err := hex.DecodeString(hexHash)
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
block := lib.blockChain.GetBlock(hash)
|
||
|
fmt.Println(block)
|
||
11 years ago
|
|
||
11 years ago
|
return &Block{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())}
|
||
|
}
|