2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-25 10:45:54 +00:00
|
|
|
"encoding/hex"
|
2018-03-21 16:11:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-21 16:11:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
func TestEncodeDecodeInfinity(t *testing.T) {
|
2019-09-04 21:12:39 +00:00
|
|
|
key := &PublicKey{}
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
assert.Nil(t, key.EncodeBinary(buf.BinWriter))
|
|
|
|
assert.Nil(t, buf.Err)
|
|
|
|
b := buf.Bytes()
|
|
|
|
assert.Equal(t, 1, len(b))
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
keyDecode := &PublicKey{}
|
2019-09-16 09:18:13 +00:00
|
|
|
assert.Nil(t, keyDecode.DecodeBytes(b))
|
2018-03-25 10:45:54 +00:00
|
|
|
assert.Equal(t, []byte{0x00}, keyDecode.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:11:04 +00:00
|
|
|
func TestEncodeDecodePublicKey(t *testing.T) {
|
|
|
|
for i := 0; i < 4; i++ {
|
2019-09-04 20:03:25 +00:00
|
|
|
k, err := NewPrivateKey()
|
|
|
|
assert.Nil(t, err)
|
2019-09-05 06:35:02 +00:00
|
|
|
p := k.PublicKey()
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
assert.Nil(t, p.EncodeBinary(buf.BinWriter))
|
|
|
|
assert.Nil(t, buf.Err)
|
|
|
|
b := buf.Bytes()
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
pDecode := &PublicKey{}
|
2019-09-16 09:18:13 +00:00
|
|
|
assert.Nil(t, pDecode.DecodeBytes(b))
|
2018-03-21 16:11:04 +00:00
|
|
|
assert.Equal(t, p.X, pDecode.X)
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
func TestDecodeFromString(t *testing.T) {
|
|
|
|
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
|
|
|
|
pubKey, err := NewPublicKeyFromString(str)
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
2018-03-25 10:45:54 +00:00
|
|
|
assert.Equal(t, str, hex.EncodeToString(pubKey.Bytes()))
|
|
|
|
}
|
2019-08-27 13:29:42 +00:00
|
|
|
|
|
|
|
func TestPubkeyToAddress(t *testing.T) {
|
|
|
|
pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4")
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
2019-08-27 14:16:33 +00:00
|
|
|
actual := pubKey.Address()
|
2019-08-27 13:29:42 +00:00
|
|
|
expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx"
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|