github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/compiler/verify_test.go (about)

     1  package compiler_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
     8  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
     9  	"github.com/nspcc-dev/neo-go/pkg/vm"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  // In this test, we only check that needed interop
    15  // is called with the provided arguments in the right order.
    16  func TestVerifyGood(t *testing.T) {
    17  	msg := []byte("test message")
    18  	pub, sig := signMessage(t, msg)
    19  	src := getVerifyProg(pub, sig)
    20  
    21  	v, p, _ := vmAndCompileInterop(t, src)
    22  	p.interops[interopnames.ToID([]byte(interopnames.SystemCryptoCheckSig))] = func(v *vm.VM) error {
    23  		assert.Equal(t, pub, v.Estack().Pop().Bytes())
    24  		assert.Equal(t, sig, v.Estack().Pop().Bytes())
    25  		v.Estack().PushVal(true)
    26  		return nil
    27  	}
    28  
    29  	require.NoError(t, v.Run())
    30  }
    31  
    32  func signMessage(t *testing.T, msg []byte) ([]byte, []byte) {
    33  	key, err := keys.NewPrivateKey()
    34  	require.NoError(t, err)
    35  
    36  	sig := key.Sign(msg)
    37  	pub := key.PublicKey().Bytes()
    38  
    39  	return pub, sig
    40  }
    41  
    42  func getVerifyProg(pub, sig []byte) string {
    43  	pubS := fmt.Sprintf("%#v", pub)
    44  	sigS := fmt.Sprintf("%#v", sig)
    45  
    46  	return `
    47  		package hello
    48  
    49  		import "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
    50  
    51  		func Main() bool {
    52  			pub := ` + pubS + `
    53  			sig := ` + sigS + `
    54  			return crypto.CheckSig(pub, sig)
    55  		}
    56  	`
    57  }