neoneo-go/pkg/crypto/keys/wif_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

64 lines
1.6 KiB
Go

package keys
import (
"encoding/hex"
"testing"
)
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,
},
}
func TestWIFEncodeDecode(t *testing.T) {
for _, testCase := range wifTestCases {
b, err := hex.DecodeString(testCase.privateKey)
if err != nil {
t.Fatal(err)
}
wif, err := WIFEncode(b, testCase.version, testCase.compressed)
if err != nil {
t.Fatal(err)
}
if want, have := testCase.wif, wif; want != have {
t.Fatalf("expected %s got %s", want, have)
}
WIF, err := WIFDecode(wif, testCase.version)
if err != nil {
t.Fatal(err)
}
if want, have := testCase.privateKey, WIF.PrivateKey.String(); want != have {
t.Fatalf("expected %s got %s", want, have)
}
if want, have := testCase.compressed, WIF.Compressed; want != have {
t.Fatalf("expected %v got %v", want, have)
}
if want, have := testCase.version, WIF.Version; want != have {
t.Fatalf("expected %d got %d", want, have)
}
}
}