Skip to content

Commit

Permalink
Merge pull request #54 from ElrondNetwork/fix-crypto
Browse files Browse the repository at this point in the history
Adjust according to newer crypto hook interface
  • Loading branch information
sasurobert authored Dec 3, 2019
2 parents 391145a + da4b7b4 commit e2f3918
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 39 deletions.
18 changes: 3 additions & 15 deletions arwen/context/arwen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package context

import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"unsafe"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions arwen/crypto/cryptoei.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
26 changes: 8 additions & 18 deletions arwen/ethapi/predefinedContracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down

0 comments on commit e2f3918

Please sign in to comment.