crypto: use PrivateKey to generate a key pair

It makes no sense to provide an API for throw-away public keys, so obtain it
via a new real keypair generation where appropriate (and that's only needed
for testing).
This commit is contained in:
Roman Khimov 2019-09-04 23:03:25 +03:00
parent 1ffda460bd
commit 0b884b92b3
3 changed files with 9 additions and 23 deletions

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -18,9 +17,11 @@ func TestDecodeEncodeAccountState(t *testing.T) {
) )
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
balances[randomUint256()] = util.Fixed8(int64(randomInt(1, 10000))) balances[randomUint256()] = util.Fixed8(int64(randomInt(1, 10000)))
votes[i] = &keys.PublicKey{ k, err := keys.NewPrivateKey()
ECPoint: crypto.RandomECPoint(), assert.Nil(t, err)
} p, err := k.PublicKey()
assert.Nil(t, err)
votes[i] = p
} }
a := &AccountState{ a := &AccountState{

View file

@ -5,7 +5,6 @@ package crypto
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"errors" "errors"
@ -63,23 +62,6 @@ func NewEllipticCurve() EllipticCurve {
return c return c
} }
// RandomECPoint returns a random generated ECPoint, mostly used
// for testing.
func RandomECPoint() ECPoint {
c := NewEllipticCurve()
b := make([]byte, c.N.BitLen()/8+8)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return ECPoint{}
}
d := new(big.Int).SetBytes(b)
d.Mod(d, new(big.Int).Sub(c.N, big.NewInt(1)))
d.Add(d, big.NewInt(1))
q := new(big.Int).SetBytes(d.Bytes())
return c.ScalarBaseMult(q)
}
// ECPointFromReader return a new point from the given reader. // ECPointFromReader return a new point from the given reader.
// f == 4, 6 or 7 are not implemented. // f == 4, 6 or 7 are not implemented.
func ECPointFromReader(r io.Reader) (point ECPoint, err error) { func ECPointFromReader(r io.Reader) (point ECPoint, err error) {

View file

@ -22,7 +22,10 @@ func TestEncodeDecodeInfinity(t *testing.T) {
func TestEncodeDecodePublicKey(t *testing.T) { func TestEncodeDecodePublicKey(t *testing.T) {
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
p := &PublicKey{crypto.RandomECPoint()} k, err := NewPrivateKey()
assert.Nil(t, err)
p, err := k.PublicKey()
assert.Nil(t, err)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
assert.Nil(t, p.EncodeBinary(buf)) assert.Nil(t, p.EncodeBinary(buf))