mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-03 01:41:48 +00:00
native: implement CryptoLib contract
This commit is contained in:
parent
19a23a36e4
commit
100f2db3fb
22 changed files with 240 additions and 172 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
||||
|
@ -37,6 +38,7 @@ func TestContractHashes(t *testing.T) {
|
|||
require.Equal(t, []byte(ledger.Hash), cs.Ledger.Hash.BytesBE())
|
||||
require.Equal(t, []byte(management.Hash), cs.Management.Hash.BytesBE())
|
||||
require.Equal(t, []byte(notary.Hash), cs.Notary.Hash.BytesBE())
|
||||
require.Equal(t, []byte(crypto.Hash), cs.Crypto.Hash.BytesBE())
|
||||
}
|
||||
|
||||
// testPrintHash is a helper for updating contract hashes.
|
||||
|
@ -77,6 +79,11 @@ func TestNameServiceRecordType(t *testing.T) {
|
|||
require.EqualValues(t, native.RecordTypeAAAA, nameservice.TypeAAAA)
|
||||
}
|
||||
|
||||
func TestCryptoLibNamedCurve(t *testing.T) {
|
||||
require.EqualValues(t, native.Secp256k1, crypto.Secp256k1)
|
||||
require.EqualValues(t, native.Secp256r1, crypto.Secp256r1)
|
||||
}
|
||||
|
||||
type nativeTestCase struct {
|
||||
method string
|
||||
params []string
|
||||
|
@ -88,6 +95,7 @@ func TestNativeHelpersCompile(t *testing.T) {
|
|||
u160 := `interop.Hash160("aaaaaaaaaaaaaaaaaaaa")`
|
||||
u256 := `interop.Hash256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
||||
pub := `interop.PublicKey("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
||||
sig := `interop.Signature("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
||||
nep17TestCases := []nativeTestCase{
|
||||
{"balanceOf", []string{u160}},
|
||||
{"decimals", nil},
|
||||
|
@ -176,6 +184,11 @@ func TestNativeHelpersCompile(t *testing.T) {
|
|||
{"update", []string{"nil", "nil"}},
|
||||
{"updateWithData", []string{"nil", "nil", "123"}},
|
||||
})
|
||||
runNativeTestCases(t, cs.Crypto.ContractMD, "crypto", []nativeTestCase{
|
||||
{"sha256", []string{"[]byte{1, 2, 3}"}},
|
||||
{"ripemd160", []string{"[]byte{1, 2, 3}"}},
|
||||
{"verifyWithECDsa", []string{"[]byte{1, 2, 3}", pub, sig, "crypto.Secp256k1"}},
|
||||
})
|
||||
}
|
||||
|
||||
func runNativeTestCases(t *testing.T, ctr interop.ContractMD, name string, testCases []nativeTestCase) {
|
||||
|
|
|
@ -103,8 +103,6 @@ func TestSyscallExecution(t *testing.T) {
|
|||
"crypto.ECDsaSecp256k1Verify": {interopnames.NeoCryptoVerifyWithECDsaSecp256k1, []string{b, pub, sig}, false},
|
||||
"crypto.ECDSASecp256r1CheckMultisig": {interopnames.NeoCryptoCheckMultisigWithECDsaSecp256r1, []string{b, pubs, sigs}, false},
|
||||
"crypto.ECDSASecp256k1CheckMultisig": {interopnames.NeoCryptoCheckMultisigWithECDsaSecp256k1, []string{b, pubs, sigs}, false},
|
||||
"crypto.SHA256": {interopnames.NeoCryptoSHA256, []string{b}, false},
|
||||
"crypto.RIPEMD160": {interopnames.NeoCryptoRIPEMD160, []string{b}, false},
|
||||
}
|
||||
ic := &interop.Context{}
|
||||
core.SpawnVM(ic) // set Functions field
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package compiler_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSHA256(t *testing.T) {
|
||||
src := `
|
||||
package foo
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
||||
)
|
||||
func Main() []byte {
|
||||
src := []byte{0x97}
|
||||
hash := crypto.SHA256(src)
|
||||
return hash
|
||||
}
|
||||
`
|
||||
v := vmAndCompile(t, src)
|
||||
ic := &interop.Context{Trigger: trigger.Verification}
|
||||
ic.VM = v
|
||||
crypto.Register(ic)
|
||||
v.SyscallHandler = ic.SyscallHandler
|
||||
require.NoError(t, v.Run())
|
||||
require.True(t, v.Estack().Len() >= 1)
|
||||
|
||||
h := []byte{0x2a, 0xa, 0xb7, 0x32, 0xb4, 0xe9, 0xd8, 0x5e, 0xf7, 0xdc, 0x25, 0x30, 0x3b, 0x64, 0xab, 0x52, 0x7c, 0x25, 0xa4, 0xd7, 0x78, 0x15, 0xeb, 0xb5, 0x79, 0xf3, 0x96, 0xec, 0x6c, 0xac, 0xca, 0xd3}
|
||||
require.Equal(t, h, v.PopResult())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue