-
Notifications
You must be signed in to change notification settings - Fork 199
/
vmCrypto.go
44 lines (36 loc) · 1.34 KB
/
vmCrypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package hooks
import (
"github.com/ElrondNetwork/elrond-go/hashing/keccak"
"github.com/ElrondNetwork/elrond-go/hashing/sha256"
"golang.org/x/crypto/ripemd160"
)
// VMCryptoHook is a wrapper used in vm implementation
type VMCryptoHook struct {
}
// NewVMCryptoHook creates a new instance of a vm crypto hook
func NewVMCryptoHook() *VMCryptoHook {
return &VMCryptoHook{}
}
// Sha256 returns a sha 256 hash of the input string. Should return in hex format.
func (vmch *VMCryptoHook) Sha256(data []byte) ([]byte, error) {
return sha256.Sha256{}.Compute(string(data)), nil
}
// Keccak256 returns a keccak 256 hash of the input string. Should return in hex format.
func (vmch *VMCryptoHook) Keccak256(data []byte) ([]byte, error) {
return keccak.Keccak{}.Compute(string(data)), nil
}
// Ripemd160 is a legacy hash and should not be used for new applications
func (vmch *VMCryptoHook) Ripemd160(data []byte) ([]byte, error) {
hash := ripemd160.New()
_, err := hash.Write(data)
if err != nil {
return nil, err
}
result := hash.Sum(nil)
return result, nil
}
// Ecrecover calculates the corresponding Ethereum address for the public key which created the given signature
// https://ewasm.readthedocs.io/en/mkdocs/system_contracts/
func (vmch *VMCryptoHook) Ecrecover(_ []byte, _ []byte, _ []byte, _ []byte) ([]byte, error) {
return nil, ErrNotImplemented
}