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:
parent
1ffda460bd
commit
0b884b92b3
3 changed files with 9 additions and 23 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/crypto"
|
||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
||||
"github.com/CityOfZion/neo-go/pkg/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -18,9 +17,11 @@ func TestDecodeEncodeAccountState(t *testing.T) {
|
|||
)
|
||||
for i := 0; i < n; i++ {
|
||||
balances[randomUint256()] = util.Fixed8(int64(randomInt(1, 10000)))
|
||||
votes[i] = &keys.PublicKey{
|
||||
ECPoint: crypto.RandomECPoint(),
|
||||
}
|
||||
k, err := keys.NewPrivateKey()
|
||||
assert.Nil(t, err)
|
||||
p, err := k.PublicKey()
|
||||
assert.Nil(t, err)
|
||||
votes[i] = p
|
||||
}
|
||||
|
||||
a := &AccountState{
|
||||
|
|
|
@ -5,7 +5,6 @@ package crypto
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
@ -63,23 +62,6 @@ func NewEllipticCurve() EllipticCurve {
|
|||
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.
|
||||
// f == 4, 6 or 7 are not implemented.
|
||||
func ECPointFromReader(r io.Reader) (point ECPoint, err error) {
|
||||
|
|
|
@ -22,7 +22,10 @@ func TestEncodeDecodeInfinity(t *testing.T) {
|
|||
|
||||
func TestEncodeDecodePublicKey(t *testing.T) {
|
||||
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)
|
||||
assert.Nil(t, p.EncodeBinary(buf))
|
||||
|
||||
|
|
Loading…
Reference in a new issue