2020-01-22 15:24:58 +00:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-03-23 09:44:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-22 15:24:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-23 09:44:23 +00:00
|
|
|
// In this test we only check that needed interop
|
|
|
|
// is called with the provided arguments in the right order.
|
2020-01-22 15:24:58 +00:00
|
|
|
func TestVerifyGood(t *testing.T) {
|
|
|
|
msg := []byte("test message")
|
|
|
|
pub, sig := signMessage(t, msg)
|
2021-03-05 07:18:03 +00:00
|
|
|
src := getVerifyProg(pub, sig)
|
2020-01-22 15:24:58 +00:00
|
|
|
|
2020-03-23 09:44:23 +00:00
|
|
|
v, p := vmAndCompileInterop(t, src)
|
2021-03-05 07:18:03 +00:00
|
|
|
p.interops[interopnames.ToID([]byte(interopnames.NeoCryptoCheckSig))] = func(v *vm.VM) error {
|
2020-03-23 09:44:23 +00:00
|
|
|
assert.Equal(t, pub, v.Estack().Pop().Bytes())
|
|
|
|
assert.Equal(t, sig, v.Estack().Pop().Bytes())
|
|
|
|
v.Estack().PushVal(true)
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-22 15:24:58 +00:00
|
|
|
|
2020-03-23 09:44:23 +00:00
|
|
|
require.NoError(t, v.Run())
|
2020-01-22 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func signMessage(t *testing.T, msg []byte) ([]byte, []byte) {
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sig := key.Sign(msg)
|
|
|
|
pub := key.PublicKey().Bytes()
|
|
|
|
|
|
|
|
return pub, sig
|
|
|
|
}
|
|
|
|
|
2021-03-05 07:18:03 +00:00
|
|
|
func getVerifyProg(pub, sig []byte) string {
|
2020-01-22 15:24:58 +00:00
|
|
|
pubS := fmt.Sprintf("%#v", pub)
|
|
|
|
sigS := fmt.Sprintf("%#v", sig)
|
|
|
|
|
|
|
|
return `
|
|
|
|
package hello
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
2020-01-22 15:24:58 +00:00
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
pub := ` + pubS + `
|
|
|
|
sig := ` + sigS + `
|
2021-03-05 07:18:03 +00:00
|
|
|
return crypto.CheckSig(pub, sig)
|
2020-01-22 15:24:58 +00:00
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|