neo-go/pkg/crypto/keys/wif_test.go

68 lines
1.7 KiB
Go
Raw Normal View History

package keys
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
2019-11-24 15:40:07 +00:00
"github.com/stretchr/testify/require"
)
type wifTestCase struct {
wif string
compressed bool
privateKey string
version byte
}
var wifTestCases = []wifTestCase{
{
wif: "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
compressed: true,
privateKey: "0000000000000000000000000000000000000000000000000000000000000001",
version: 0x80,
},
{
wif: "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf",
compressed: false,
privateKey: "0000000000000000000000000000000000000000000000000000000000000001",
version: 0x80,
},
{
wif: "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o",
compressed: true,
privateKey: "2bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e",
version: 0x80,
},
2019-11-24 15:40:07 +00:00
{
wif: "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o",
compressed: true,
privateKey: "2bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e",
version: 0x00,
},
}
func TestWIFEncodeDecode(t *testing.T) {
for _, testCase := range wifTestCases {
b, err := hex.DecodeString(testCase.privateKey)
assert.Nil(t, err)
wif, err := WIFEncode(b, testCase.version, testCase.compressed)
assert.Nil(t, err)
assert.Equal(t, testCase.wif, wif)
WIF, err := WIFDecode(wif, testCase.version)
assert.Nil(t, err)
assert.Equal(t, testCase.privateKey, WIF.PrivateKey.String())
assert.Equal(t, testCase.compressed, WIF.Compressed)
2019-11-24 15:40:07 +00:00
if testCase.version != 0 {
assert.Equal(t, testCase.version, WIF.Version)
} else {
assert.EqualValues(t, WIFVersion, WIF.Version)
}
}
2019-11-24 15:40:07 +00:00
wifInv := []byte{0, 1, 2}
_, err := WIFEncode(wifInv, 0, true)
require.Error(t, err)
}