diff options
author | obscuren <geffobscura@gmail.com> | 2014-02-28 19:20:47 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-02-28 19:20:47 +0800 |
commit | 0adfa489de3fb88c995246d0b70af8bbd06f8db7 (patch) | |
tree | af34f0c678fc75dda0d186285240beef9aa5b42c /ui/library.go | |
parent | 6db8b5d06a41ef573ec43394a11fd0e668372860 (diff) | |
parent | 560a7073f457a32c9d053f1a4b20f76d949f519d (diff) | |
download | go-tangerine-0.3.0.tar go-tangerine-0.3.0.tar.gz go-tangerine-0.3.0.tar.bz2 go-tangerine-0.3.0.tar.lz go-tangerine-0.3.0.tar.xz go-tangerine-0.3.0.tar.zst go-tangerine-0.3.0.zip |
Merge branch 'release/0.3.0'0.3.0
Diffstat (limited to 'ui/library.go')
-rw-r--r-- | ui/library.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/ui/library.go b/ui/library.go new file mode 100644 index 000000000..3bbb01314 --- /dev/null +++ b/ui/library.go @@ -0,0 +1,60 @@ +package ethui + +import ( + "encoding/hex" + "fmt" + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethutil" + "strings" +) + +type EthLib struct { + blockManager *ethchain.BlockManager + blockChain *ethchain.BlockChain + txPool *ethchain.TxPool +} + +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() + } + } + + k, _ := ethutil.Config.Db.Get([]byte("KeyRing")) + keyRing := ethutil.NewValueFromBytes(k) + + amount := ethutil.Big(a) + code := ethchain.Compile(strings.Split(data, "\n")) + tx := ethchain.NewTransaction(hash, amount, code) + tx.Nonce = lib.blockManager.GetAddrState(keyRing.Get(1).Bytes()).Nonce + + tx.Sign(keyRing.Get(0).Bytes()) + + lib.txPool.QueueTransaction(tx) + + if len(receiver) == 0 { + ethutil.Config.Log.Infof("Contract addr %x", tx.Hash()[12:]) + } else { + ethutil.Config.Log.Infof("Tx hash %x", tx.Hash()) + } + + 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) + + return &Block{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())} +} |