diff options
author | obscuren <geffobscura@gmail.com> | 2014-07-02 02:09:21 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-07-02 02:09:21 +0800 |
commit | 7a2a63f32b209f0358fa6be1e76b49319d8f309f (patch) | |
tree | 4634a73cb4b04c4d1bd9a002b49305fff5981af8 | |
parent | 28ef7d228c4e8e24315eb585e4e0d03a4c652ccb (diff) | |
download | dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.tar dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.tar.gz dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.tar.bz2 dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.tar.lz dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.tar.xz dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.tar.zst dexon-7a2a63f32b209f0358fa6be1e76b49319d8f309f.zip |
Added utility wrapper for Ethereum Config contract
Note, this utility function has to move elsewhere
-rw-r--r-- | ethpub/pub.go | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/ethpub/pub.go b/ethpub/pub.go index ee4140899..5852f93be 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -14,6 +14,31 @@ import ( var logger = ethlog.NewLogger("PUB") +// TODO this has to move elsewhere +var cnfCtr = ethutil.Hex2Bytes("661005d2720d855f1d9976f88bb10c1a3398c77f") + +type helper struct { + sm *ethchain.StateManager +} + +func EthereumConfig(stateManager *ethchain.StateManager) helper { + return helper{stateManager} +} +func (self helper) obj() *ethchain.StateObject { + return self.sm.CurrentState().GetStateObject(cnfCtr) +} + +func (self helper) NameReg() *ethchain.StateObject { + if self.obj() != nil { + addr := self.obj().GetStorage(big.NewInt(0)) + if len(addr.Bytes()) > 0 { + return self.sm.CurrentState().GetStateObject(addr.Bytes()) + } + } + + return nil +} + type PEthereum struct { manager ethchain.EthManager stateManager *ethchain.StateManager @@ -141,19 +166,31 @@ func (lib *PEthereum) Create(key, valueStr, gasStr, gasPriceStr, script string) return lib.createTx(key, "", valueStr, gasStr, gasPriceStr, script) } -var namereg = ethutil.Hex2Bytes("bb5f186604d057c1c5240ca2ae0f6430138ac010") - func GetAddressFromNameReg(stateManager *ethchain.StateManager, name string) []byte { - recp := new(big.Int).SetBytes([]byte(name)) - object := stateManager.CurrentState().GetStateObject(namereg) - if object != nil { - reg := object.GetStorage(recp) + nameReg := EthereumConfig(stateManager).NameReg() + if nameReg != nil { + addr := ethutil.RightPadBytes([]byte(name), 32) + + reg := nameReg.GetStorage(ethutil.BigD(addr)) return reg.Bytes() } return nil + + /* + recp := new(big.Int).SetBytes([]byte(name)) + object := stateManager.CurrentState().GetStateObject(namereg) + if object != nil { + reg := object.GetStorage(recp) + + return reg.Bytes() + } + + return nil + */ } + func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, scriptStr string) (*PReceipt, error) { var hash []byte var contractCreation bool @@ -171,7 +208,7 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, sc var keyPair *ethcrypto.KeyPair var err error - if key[0:2] == "0x" { + if ethutil.IsHex(key) { keyPair, err = ethcrypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key[2:]))) } else { keyPair, err = ethcrypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key))) |