neo-go/pkg/core/interop/interopnames/convert.go
Evgenii Stratonikov a796f2b61d names: implement FromID
Allow to convert interop id to it's name.
2020-08-14 14:22:45 +03:00

25 lines
502 B
Go

package interopnames
import (
"crypto/sha256"
"encoding/binary"
"errors"
)
var errNotFound = errors.New("interop not found")
// ToID returns an identificator of the method based on its name.
func ToID(name []byte) uint32 {
h := sha256.Sum256(name)
return binary.LittleEndian.Uint32(h[:4])
}
// FromID returns interop name from its id.
func FromID(id uint32) (string, error) {
for i := range names {
if id == ToID([]byte(names[i])) {
return names[i], nil
}
}
return "", errNotFound
}