From e798f221dd9d6aaffaa709d559f01a41447e4896 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 2 May 2014 13:55:43 +0200 Subject: Added public interface --- ethpub/pub.go | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 ethpub/pub.go (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go new file mode 100644 index 000000000..4950f6eb5 --- /dev/null +++ b/ethpub/pub.go @@ -0,0 +1,108 @@ +package ethpub + +import ( + "github.com/ethereum/eth-go" + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethutil" +) + +type PEthereum struct { + stateManager *ethchain.StateManager + blockChain *ethchain.BlockChain + txPool *ethchain.TxPool +} + +func NewPEthereum(eth *eth.Ethereum) *PEthereum { + return &PEthereum{ + eth.StateManager(), + eth.BlockChain(), + eth.TxPool(), + } +} + +func (lib *PEthereum) GetBlock(hexHash string) *PBlock { + hash := ethutil.FromHex(hexHash) + + block := lib.blockChain.GetBlock(hash) + + return &PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())} +} + +func (lib *PEthereum) GetKey() *PKey { + keyPair, err := ethchain.NewKeyPairFromSec(ethutil.Config.Db.GetKeys()[0].PrivateKey) + if err != nil { + return nil + } + + return NewPKey(keyPair) +} + +func (lib *PEthereum) GetStateObject(address string) *PStateObject { + stateObject := lib.stateManager.ProcState().GetContract(ethutil.FromHex(address)) + if stateObject != nil { + return NewPStateObject(stateObject) + } + + // See GetStorage for explanation on "nil" + return NewPStateObject(nil) +} + +func (lib *PEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) { + return lib.createTx(key, recipient, valueStr, gasStr, gasPriceStr, dataStr, "") +} + +func (lib *PEthereum) Create(key, valueStr, gasStr, gasPriceStr, initStr, bodyStr string) (string, error) { + return lib.createTx(key, "", valueStr, gasStr, gasPriceStr, initStr, bodyStr) +} + +func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, initStr, scriptStr string) (string, error) { + var hash []byte + var contractCreation bool + if len(recipient) == 0 { + contractCreation = true + } else { + hash = ethutil.FromHex(recipient) + } + + keyPair, err := ethchain.NewKeyPairFromSec([]byte(ethutil.FromHex(key))) + if err != nil { + return "", err + } + + value := ethutil.Big(valueStr) + gas := ethutil.Big(gasStr) + gasPrice := ethutil.Big(gasPriceStr) + var tx *ethchain.Transaction + // Compile and assemble the given data + if contractCreation { + initScript, err := ethutil.Compile(initStr) + if err != nil { + return "", err + } + mainScript, err := ethutil.Compile(scriptStr) + if err != nil { + return "", err + } + + tx = ethchain.NewContractCreationTx(value, gas, gasPrice, mainScript, initScript) + } else { + // Just in case it was submitted as a 0x prefixed string + if initStr[0:2] == "0x" { + initStr = initStr[2:len(initStr)] + } + tx = ethchain.NewTransactionMessage(hash, value, gas, gasPrice, ethutil.FromHex(initStr)) + } + + acc := lib.stateManager.GetAddrState(keyPair.Address()) + tx.Nonce = acc.Nonce + tx.Sign(keyPair.PrivateKey) + lib.txPool.QueueTransaction(tx) + + if contractCreation { + 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()), nil +} -- cgit v1.2.3 From 1f6df0cd522842095c5ca723d2e11fc6b97b8b6a Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 2 May 2014 14:08:54 +0200 Subject: Added receipts for tx creation --- ethpub/pub.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index 4950f6eb5..c6f177124 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -47,15 +47,15 @@ func (lib *PEthereum) GetStateObject(address string) *PStateObject { return NewPStateObject(nil) } -func (lib *PEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) { +func (lib *PEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) (*PReceipt, error) { return lib.createTx(key, recipient, valueStr, gasStr, gasPriceStr, dataStr, "") } -func (lib *PEthereum) Create(key, valueStr, gasStr, gasPriceStr, initStr, bodyStr string) (string, error) { +func (lib *PEthereum) Create(key, valueStr, gasStr, gasPriceStr, initStr, bodyStr string) (*PReceipt, error) { return lib.createTx(key, "", valueStr, gasStr, gasPriceStr, initStr, bodyStr) } -func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, initStr, scriptStr string) (string, error) { +func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, initStr, scriptStr string) (*PReceipt, error) { var hash []byte var contractCreation bool if len(recipient) == 0 { @@ -66,7 +66,7 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in keyPair, err := ethchain.NewKeyPairFromSec([]byte(ethutil.FromHex(key))) if err != nil { - return "", err + return nil, err } value := ethutil.Big(valueStr) @@ -77,11 +77,11 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in if contractCreation { initScript, err := ethutil.Compile(initStr) if err != nil { - return "", err + return nil, err } mainScript, err := ethutil.Compile(scriptStr) if err != nil { - return "", err + return nil, err } tx = ethchain.NewContractCreationTx(value, gas, gasPrice, mainScript, initScript) @@ -99,10 +99,10 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in lib.txPool.QueueTransaction(tx) if contractCreation { - ethutil.Config.Log.Infof("Contract addr %x", tx.Hash()[12:]) + ethutil.Config.Log.Infof("Contract addr %x", tx.CreationAddress()) } else { ethutil.Config.Log.Infof("Tx hash %x", tx.Hash()) } - return ethutil.Hex(tx.Hash()), nil + return NewPReciept(contractCreation, tx.CreationAddress(), tx.Hash(), keyPair.Address()), nil } -- cgit v1.2.3 From 4f20e8f649a19168718a7f0fe7619e3bdb626aa8 Mon Sep 17 00:00:00 2001 From: Maran Date: Fri, 2 May 2014 20:00:58 +0200 Subject: Implemented first few methods via public api --- ethpub/pub.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index c6f177124..3c579001e 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -87,7 +87,7 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in tx = ethchain.NewContractCreationTx(value, gas, gasPrice, mainScript, initScript) } else { // Just in case it was submitted as a 0x prefixed string - if initStr[0:2] == "0x" { + if len(initStr) > 0 && initStr[0:2] == "0x" { initStr = initStr[2:len(initStr)] } tx = ethchain.NewTransactionMessage(hash, value, gas, gasPrice, ethutil.FromHex(initStr)) -- cgit v1.2.3 From 7c91159449c528daa099aec5a3744aa8a6b5a826 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 5 May 2014 11:56:25 +0200 Subject: Added different storage notification object --- ethpub/pub.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index c6f177124..5f23018f7 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -47,6 +47,18 @@ func (lib *PEthereum) GetStateObject(address string) *PStateObject { return NewPStateObject(nil) } +func (lib *PEthereum) GetStorage(address, storageAddress string) string { + return lib.GetStateObject(address).GetStorage(storageAddress) +} + +func (lib *PEthereum) GetTxCount(address string) int { + return lib.GetStateObject(address).Nonce() +} + +func (lib *PEthereum) IsContract(address string) bool { + return lib.GetStateObject(address).IsContract() +} + func (lib *PEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) (*PReceipt, error) { return lib.createTx(key, recipient, valueStr, gasStr, gasPriceStr, dataStr, "") } -- cgit v1.2.3 From 5757f5df2a5ffc4207ab2be5f1280d4f2e95d303 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 5 May 2014 13:09:29 +0200 Subject: Added secret to address method --- ethpub/pub.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index 5f23018f7..f9d2ebc72 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -59,6 +59,15 @@ func (lib *PEthereum) IsContract(address string) bool { return lib.GetStateObject(address).IsContract() } +func (lib *PEthereum) SecretToAddress(key string) string { + pair, err := ethchain.NewKeyPairFromSec(ethutil.FromHex(key)) + if err != nil { + return "" + } + + return ethutil.Hex(pair.Address()) +} + func (lib *PEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) (*PReceipt, error) { return lib.createTx(key, recipient, valueStr, gasStr, gasPriceStr, dataStr, "") } -- cgit v1.2.3 From fde3e01f80420c367139a9fe647c618b0223842d Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 5 May 2014 14:15:31 +0200 Subject: Fixed import cycle --- ethpub/pub.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index 3c579001e..5dc08160f 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -1,7 +1,6 @@ package ethpub import ( - "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" ) @@ -12,11 +11,11 @@ type PEthereum struct { txPool *ethchain.TxPool } -func NewPEthereum(eth *eth.Ethereum) *PEthereum { +func NewPEthereum(sm *ethchain.StateManager, bc *ethchain.BlockChain, txp *ethchain.TxPool) *PEthereum { return &PEthereum{ - eth.StateManager(), - eth.BlockChain(), - eth.TxPool(), + sm, + bc, + txp, } } -- cgit v1.2.3 From b98cc2fb4e296c7a9efe50293cc43d6f9ef6f23d Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 5 May 2014 14:15:58 +0200 Subject: Fixed GetBlock when no block can be found --- ethpub/pub.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index 5dc08160f..64109dbfa 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -24,7 +24,15 @@ func (lib *PEthereum) GetBlock(hexHash string) *PBlock { block := lib.blockChain.GetBlock(hash) - return &PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())} + var blockInfo *PBlock + + if block != nil { + blockInfo = &PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())} + } else { + blockInfo = &PBlock{Number: -1, Hash: ""} + } + + return blockInfo } func (lib *PEthereum) GetKey() *PKey { -- cgit v1.2.3 From 14a6e6a9cea48bcc7f6d5286e4f85b1a0cce84d8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 5 May 2014 15:48:17 +0200 Subject: Added the ability to submit byte code for contracts instead of Mutan code. --- ethpub/pub.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index 431f173a0..4ec9877b2 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -103,13 +103,24 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in var tx *ethchain.Transaction // Compile and assemble the given data if contractCreation { - initScript, err := ethutil.Compile(initStr) - if err != nil { - return nil, err + var initScript, mainScript []byte + var err error + if ethutil.IsHex(initStr) { + initScript = ethutil.FromHex(initStr) + } else { + initScript, err = ethutil.Compile(initStr[2:]) + if err != nil { + return nil, err + } } - mainScript, err := ethutil.Compile(scriptStr) - if err != nil { - return nil, err + + if ethutil.IsHex(scriptStr) { + mainScript = ethutil.FromHex(scriptStr[2:]) + } else { + mainScript, err = ethutil.Compile(scriptStr) + if err != nil { + return nil, err + } } tx = ethchain.NewContractCreationTx(value, gas, gasPrice, mainScript, initScript) -- cgit v1.2.3 From 78cb04cca3daafa178558a3dec5f4814f824dc95 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 5 May 2014 15:51:43 +0200 Subject: wrong string --- ethpub/pub.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethpub/pub.go') diff --git a/ethpub/pub.go b/ethpub/pub.go index 4ec9877b2..5e7792a9f 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -106,9 +106,9 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in var initScript, mainScript []byte var err error if ethutil.IsHex(initStr) { - initScript = ethutil.FromHex(initStr) + initScript = ethutil.FromHex(initStr[2:]) } else { - initScript, err = ethutil.Compile(initStr[2:]) + initScript, err = ethutil.Compile(initStr) if err != nil { return nil, err } -- cgit v1.2.3