diff --git a/arwen/context/arwen.go b/arwen/context/arwen.go index 087ffa1e2..b6460887c 100644 --- a/arwen/context/arwen.go +++ b/arwen/context/arwen.go @@ -2,7 +2,6 @@ package context import ( "bytes" - "encoding/hex" "fmt" "math/big" "unsafe" @@ -614,13 +613,7 @@ func (host *vmContext) GetCodeHash(addr []byte) ([]byte, error) { return nil, err } - codeHash, err := host.cryptoHook.Keccak256(string(code)) - if err != nil { - return nil, err - } - - result := []byte(codeHash) - return result, nil + return host.cryptoHook.Keccak256(code) } func (host *vmContext) GetCode(addr []byte) ([]byte, error) { @@ -999,17 +992,12 @@ func (host *vmContext) createETHCallInput() []byte { newInput := make([]byte, 0) if len(host.callFunction) > 0 { - hashOfFunction, err := host.cryptoHook.Keccak256(host.callFunction) - if err != nil { - return nil - } - - methodSelectors, err := hex.DecodeString(hashOfFunction) + hashOfFunction, err := host.cryptoHook.Keccak256([]byte(host.callFunction)) if err != nil { return nil } - newInput = append(newInput, methodSelectors[0:4]...) + newInput = append(newInput, hashOfFunction[0:4]...) } for _, arg := range host.vmInput.Arguments { diff --git a/arwen/crypto/cryptoei.go b/arwen/crypto/cryptoei.go index 2178756e1..30ddc4296 100644 --- a/arwen/crypto/cryptoei.go +++ b/arwen/crypto/cryptoei.go @@ -41,12 +41,12 @@ func sha256(context unsafe.Pointer, dataOffset int32, length int32, resultOffset return 1 } - result, err := cryptoContext.CryptoHooks().Sha256(string(data)) + result, err := cryptoContext.CryptoHooks().Sha256(data) if err != nil { return 1 } - err = arwen.StoreBytes(instCtx.Memory(), resultOffset, []byte(result)) + err = arwen.StoreBytes(instCtx.Memory(), resultOffset, result) if withFault(err, context) { return 1 } @@ -67,12 +67,12 @@ func keccak256(context unsafe.Pointer, dataOffset int32, length int32, resultOff return 1 } - result, err := cryptoContext.CryptoHooks().Keccak256(string(data)) + result, err := cryptoContext.CryptoHooks().Keccak256(data) if err != nil { return 1 } - err = arwen.StoreBytes(instCtx.Memory(), resultOffset, []byte(result)) + err = arwen.StoreBytes(instCtx.Memory(), resultOffset, result) if withFault(err, context) { return 1 } diff --git a/arwen/ethapi/predefinedContracts.go b/arwen/ethapi/predefinedContracts.go index ce7904f5a..78e89b476 100644 --- a/arwen/ethapi/predefinedContracts.go +++ b/arwen/ethapi/predefinedContracts.go @@ -19,12 +19,14 @@ var contractsMap = map[string]func(unsafe.Pointer, []byte) ([]byte, error){ "0000000000000000000000000000000000000009": keccak256, } +// IsAddressForPredefinedContract returns whether the address is recognized as a eth "precompiled contract" (predefined contract) func IsAddressForPredefinedContract(address []byte) bool { contractKey := hex.EncodeToString(address) _, ok := contractsMap[contractKey] return ok } +// CallPredefinedContract executes a predefined contract specified by address func CallPredefinedContract(ctx unsafe.Pointer, address []byte, data []byte) error { instCtx := wasmer.IntoInstanceContext(ctx) ethCtx := arwen.GetEthContext(instCtx.Data()) @@ -52,33 +54,21 @@ func ecrecover(context unsafe.Pointer, data []byte) ([]byte, error) { func sha2(context unsafe.Pointer, data []byte) ([]byte, error) { instCtx := wasmer.IntoInstanceContext(context) cryptoCtx := arwen.GetCryptoContext(instCtx.Data()) - - resultString, err := cryptoCtx.CryptoHooks().Sha256(string(data)) - if err != nil { - return nil, err - } - - result, _ := hex.DecodeString(resultString) - return result, nil + return cryptoCtx.CryptoHooks().Sha256(data) } func ripemd160(context unsafe.Pointer, data []byte) ([]byte, error) { - return nil, fmt.Errorf("EEI system contract not implemented: ripemd160") + instCtx := wasmer.IntoInstanceContext(context) + cryptoCtx := arwen.GetCryptoContext(instCtx.Data()) + return cryptoCtx.CryptoHooks().Ripemd160(data) } func identity(context unsafe.Pointer, data []byte) ([]byte, error) { - return nil, fmt.Errorf("EEI system contract not implemented: identity") + return data, nil } func keccak256(context unsafe.Pointer, data []byte) ([]byte, error) { instCtx := wasmer.IntoInstanceContext(context) cryptoCtx := arwen.GetCryptoContext(instCtx.Data()) - - resultString, err := cryptoCtx.CryptoHooks().Keccak256(string(data)) - if err != nil { - return nil, err - } - - result, _ := hex.DecodeString(resultString) - return result, nil + return cryptoCtx.CryptoHooks().Keccak256(data) } diff --git a/go.mod b/go.mod index ec86913e7..7127b690d 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.13 require ( github.com/ElrondNetwork/big-int-util v0.0.5 - github.com/ElrondNetwork/elrond-vm-common v0.1.4 - github.com/ElrondNetwork/elrond-vm-util v0.1.0 + github.com/ElrondNetwork/elrond-vm-common v0.1.5 + github.com/ElrondNetwork/elrond-vm-util v0.1.1 github.com/ElrondNetwork/go-ext-wasm v0.1.0 github.com/mitchellh/mapstructure v1.1.2 github.com/stretchr/testify v1.4.0 diff --git a/go.sum b/go.sum index 42eb84aca..955dd39ea 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/ElrondNetwork/big-int-util v0.0.5 h1:e/9kK++9ZH/SdIYqLSUPRFYrDZmDWDgff3/7SCydq5I= github.com/ElrondNetwork/big-int-util v0.0.5/go.mod h1:96viBvoTXLjZOhEvE0D+QnAwg1IJLPAK6GVHMbC7Aw4= +github.com/ElrondNetwork/elrond-vm-common v0.0.0-20191203115206-691b00a6e76a h1:/hGeyqQxE0E92BkMDs/1E6vRqJMA4nVhwSFol1dNopk= +github.com/ElrondNetwork/elrond-vm-common v0.0.0-20191203115206-691b00a6e76a/go.mod h1:ZakxPST/Wt8umnRtA9gobcy3Dw2bywxwkC54P5VhO9g= github.com/ElrondNetwork/elrond-vm-common v0.0.9 h1:Ff8vEJSKChRfmp+TVo7AgciRkMXjL4+TbNin6LQ7xKw= github.com/ElrondNetwork/elrond-vm-common v0.0.9/go.mod h1:VqCCN0cX0e4D/KDc7MGNV9ElrOsfnjuJnGvcODVjzbk= github.com/ElrondNetwork/elrond-vm-common v0.1.2 h1:QsCxMTrMqtOvjrnvkzGIVgG9s/jv4rqbZajEz/Wujqg= @@ -8,10 +10,16 @@ github.com/ElrondNetwork/elrond-vm-common v0.1.3 h1:b9KnmIDQx1Warrf2Hn9wtv5H2zMT github.com/ElrondNetwork/elrond-vm-common v0.1.3/go.mod h1:ZakxPST/Wt8umnRtA9gobcy3Dw2bywxwkC54P5VhO9g= github.com/ElrondNetwork/elrond-vm-common v0.1.4 h1:bBkALg70U/tLXXRhoTTWT50QN7DSyq4VuJd7aiVWuQA= github.com/ElrondNetwork/elrond-vm-common v0.1.4/go.mod h1:ZakxPST/Wt8umnRtA9gobcy3Dw2bywxwkC54P5VhO9g= +github.com/ElrondNetwork/elrond-vm-common v0.1.5 h1:JRMK3tgLGFaHgMjOwvL+zzhXapv4GQ9G7bpNOWlxP8Y= +github.com/ElrondNetwork/elrond-vm-common v0.1.5/go.mod h1:ZakxPST/Wt8umnRtA9gobcy3Dw2bywxwkC54P5VhO9g= +github.com/ElrondNetwork/elrond-vm-util v0.0.0-20191203121718-7c655ca5b632 h1:JdVO1uUQ+rdDXCEN3Uzs3ptZOY5GQui9C5xZkrggyMc= +github.com/ElrondNetwork/elrond-vm-util v0.0.0-20191203121718-7c655ca5b632/go.mod h1:02LPKFh/Z5rbejgW2dazwjWGnsniuLOhRM2JjaOA3Mg= github.com/ElrondNetwork/elrond-vm-util v0.0.9 h1:382+8DovbVgDraAnuNt4v7/UyWVNED1+1mX59hCuX1g= github.com/ElrondNetwork/elrond-vm-util v0.0.9/go.mod h1:iIniCNE+6dPLTKagN2mO4zH3obT7bPYQKBO30GQNSlQ= github.com/ElrondNetwork/elrond-vm-util v0.1.0 h1:G6zKk2A89H7/Zm1DBdxP5rSlJFFMcuAMDu/etn753Hc= github.com/ElrondNetwork/elrond-vm-util v0.1.0/go.mod h1:2dLgpzmy1PkmtSVWjM2rEPoQkKv/y7+F2vMsDCRtHmg= +github.com/ElrondNetwork/elrond-vm-util v0.1.1 h1:npoeG7OibHhNH5OTFvaRpznX8+hICrZNPYmqnM82dM4= +github.com/ElrondNetwork/elrond-vm-util v0.1.1/go.mod h1:02LPKFh/Z5rbejgW2dazwjWGnsniuLOhRM2JjaOA3Mg= github.com/ElrondNetwork/go-ext-wasm v0.1.0 h1:aSXdg60JLQMSYdQbq0VtqpoyMdFYZqz09BcGiKwGY5U= github.com/ElrondNetwork/go-ext-wasm v0.1.0/go.mod h1:wlns4D0OzJP+q/cLweFz1rGpoZOG8LbGYX2jD31HCgw= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=