names: implement FromID

Allow to convert interop id to it's name.
This commit is contained in:
Evgenii Stratonikov 2020-08-13 10:48:33 +03:00
parent 7854dcfd8f
commit a796f2b61d
3 changed files with 99 additions and 0 deletions

View file

@ -3,10 +3,23 @@ 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
}

View file

@ -0,0 +1,21 @@
package interopnames
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestFromID(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
id := ToID([]byte(names[0]))
name, err := FromID(id)
require.NoError(t, err)
require.Equal(t, names[0], name)
})
t.Run("Invalid", func(t *testing.T) {
_, err := FromID(0x42424242)
require.True(t, errors.Is(err, errNotFound))
})
}

View file

@ -65,3 +65,68 @@ const (
NeoNativeCall = "Neo.Native.Call"
NeoNativeDeploy = "Neo.Native.Deploy"
)
var names = []string{
SystemBinaryBase64Decode,
SystemBinaryBase64Encode,
SystemBinaryDeserialize,
SystemBinarySerialize,
SystemBlockchainGetBlock,
SystemBlockchainGetContract,
SystemBlockchainGetHeight,
SystemBlockchainGetTransaction,
SystemBlockchainGetTransactionFromBlock,
SystemBlockchainGetTransactionHeight,
SystemCallbackCreate,
SystemCallbackCreateFromMethod,
SystemCallbackCreateFromSyscall,
SystemCallbackInvoke,
SystemContractCall,
SystemContractCallEx,
SystemContractCreate,
SystemContractCreateStandardAccount,
SystemContractDestroy,
SystemContractIsStandard,
SystemContractGetCallFlags,
SystemContractUpdate,
SystemEnumeratorConcat,
SystemEnumeratorCreate,
SystemEnumeratorNext,
SystemEnumeratorValue,
SystemIteratorConcat,
SystemIteratorCreate,
SystemIteratorKey,
SystemIteratorKeys,
SystemIteratorValues,
SystemJSONDeserialize,
SystemJSONSerialize,
SystemRuntimeCheckWitness,
SystemRuntimeGasLeft,
SystemRuntimeGetCallingScriptHash,
SystemRuntimeGetEntryScriptHash,
SystemRuntimeGetExecutingScriptHash,
SystemRuntimeGetInvocationCounter,
SystemRuntimeGetNotifications,
SystemRuntimeGetScriptContainer,
SystemRuntimeGetTime,
SystemRuntimeGetTrigger,
SystemRuntimeLog,
SystemRuntimeNotify,
SystemRuntimePlatform,
SystemStorageDelete,
SystemStorageFind,
SystemStorageGet,
SystemStorageGetContext,
SystemStorageGetReadOnlyContext,
SystemStoragePut,
SystemStoragePutEx,
SystemStorageAsReadOnly,
NeoCryptoVerifyWithECDsaSecp256r1,
NeoCryptoVerifyWithECDsaSecp256k1,
NeoCryptoCheckMultisigWithECDsaSecp256r1,
NeoCryptoCheckMultisigWithECDsaSecp256k1,
NeoCryptoSHA256,
NeoCryptoRIPEMD160,
NeoNativeCall,
NeoNativeDeploy,
}