neoneo-go/pkg/crypto/keys/sign_verify_test.go
Roman Khimov b77e533d13 crypto/wallet: move public/private key into the new keys package
And drop associated _pkg.dev remnants (refs. #307).

Original `dev` branch had two separate packages for public and private keys,
but those are so intertwined (`TestHelper` subpackage is a proof) that it's
better unite them and all associated code (like WIF and NEP-2) in one
package. This patch also:
 * creates internal `keytestcases` package to share things with wallet (maybe
   it'll be changed in some future)
 * ports some tests from `dev`
 * ports Verify() method for public key from `dev`
 * expands TestPrivateKey() with public key check
2019-08-27 17:45:51 +03:00

56 lines
1.2 KiB
Go
Executable file

package keys
import (
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/stretchr/testify/assert"
)
// SignDataWithRandomPrivateKey will sign data with
// a random private key, then verify said data
// returning true if Verify returns true
func SignDataWithRandomPrivateKey(data []byte) (bool, error) {
hashedData := hash.Sha256(data)
privKey, err := NewPrivateKey()
if err != nil {
return false, err
}
signedData, err := privKey.Sign(data)
if err != nil {
return false, err
}
pubKey, err := privKey.PublicKey()
if err != nil {
return false, err
}
result := pubKey.Verify(signedData, hashedData.Bytes())
return result, nil
}
func TestPubKeyVerify(t *testing.T) {
actual, err := SignDataWithRandomPrivateKey([]byte("sample"))
if err != nil {
t.Fatal(err)
}
expected := true
assert.Equal(t, expected, actual)
}
func TestWrongPubKey(t *testing.T) {
privKey, _ := NewPrivateKey()
sample := []byte("sample")
hashedData := hash.Sha256(sample)
signedData, _ := privKey.Sign(sample)
secondPrivKey, _ := NewPrivateKey()
wrongPubKey, _ := secondPrivKey.PublicKey()
actual := wrongPubKey.Verify(signedData, hashedData.Bytes())
expcted := false
assert.Equal(t, expcted, actual)
}