lbcutil/addrconvs_test.go
2014-01-08 23:46:05 -06:00

185 lines
6.3 KiB
Go

// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcutil_test
import (
"bytes"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
"testing"
)
var encodePrivateKeyTests = []struct {
in []byte
net btcwire.BitcoinNet
compressed bool
out string
}{
{[]byte{
0x0c, 0x28, 0xfc, 0xa3, 0x86, 0xc7, 0xa2, 0x27,
0x60, 0x0b, 0x2f, 0xe5, 0x0b, 0x7c, 0xae, 0x11,
0xec, 0x86, 0xd3, 0xbf, 0x1f, 0xbe, 0x47, 0x1b,
0xe8, 0x98, 0x27, 0xe1, 0x9d, 0x72, 0xaa, 0x1d,
}, btcwire.MainNet, false, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"},
{[]byte{
0xdd, 0xa3, 0x5a, 0x14, 0x88, 0xfb, 0x97, 0xb6,
0xeb, 0x3f, 0xe6, 0xe9, 0xef, 0x2a, 0x25, 0x81,
0x4e, 0x39, 0x6f, 0xb5, 0xdc, 0x29, 0x5f, 0xe9,
0x94, 0xb9, 0x67, 0x89, 0xb2, 0x1a, 0x03, 0x98,
}, btcwire.TestNet3, true, "cV1Y7ARUr9Yx7BR55nTdnR7ZXNJphZtCCMBTEZBJe1hXt2kB684q"},
}
var encodeTests = []struct {
raw []byte
net btcwire.BitcoinNet
res string
err error
}{
{[]byte{0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
btcwire.MainNet, "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil},
{[]byte{0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa},
btcwire.MainNet, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", nil},
{[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet, "", btcutil.ErrUnknownNet},
{[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet3, "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", nil},
// Raw address not 20 bytes (padded with leading 0s)
{[]byte{0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa},
btcwire.MainNet, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", btcutil.ErrMalformedAddress},
// Bad network
{make([]byte, 20), 0, "", btcutil.ErrUnknownNet},
}
func TestEncodeAddresses(t *testing.T) {
for i := range encodeTests {
res, err := btcutil.EncodeAddress(encodeTests[i].raw,
encodeTests[i].net)
if err != encodeTests[i].err {
t.Error(err)
continue
}
if err == nil && res != encodeTests[i].res {
t.Errorf("Results differ: Expected '%s', returned '%s'",
encodeTests[i].res, res)
}
}
}
var decodeTests = []struct {
addr string
res []byte
net btcwire.BitcoinNet
err error
}{
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
[]byte{0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
btcwire.MainNet, nil},
{"mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet3, nil},
// Wrong length
{"01MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
// Bad magic
{"2MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrUnknownNet},
// Bad checksum
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2dpuqz", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
}
func TestDecodeAddresses(t *testing.T) {
for i := range decodeTests {
res, net, err := btcutil.DecodeAddress(decodeTests[i].addr)
if err != decodeTests[i].err {
t.Error(err)
}
if err != nil {
continue
}
if !bytes.Equal(res, decodeTests[i].res) {
t.Errorf("Results differ: Expected '%v', returned '%v'",
decodeTests[i].res, res)
}
if net != decodeTests[i].net {
t.Errorf("Networks differ: Expected '%v', returned '%v'",
decodeTests[i].net, net)
}
}
}
func TestEncodeDecodePrivateKey(t *testing.T) {
for x, test := range encodePrivateKeyTests {
wif, err := btcutil.EncodePrivateKey(test.in, test.net, test.compressed)
if err != nil {
t.Errorf("%x: %v", x, err)
continue
}
if wif != test.out {
t.Errorf("TestEncodeDecodePrivateKey failed: want '%s', got '%s'",
test.out, wif)
continue
}
key, _, compressed, err := btcutil.DecodePrivateKey(test.out)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(key, test.in) || compressed != test.compressed {
t.Errorf("TestEncodeDecodePrivateKey failed: want '%x', got '%x'",
test.out, key)
}
}
}
var encodeScriptHashTests = []struct {
raw []byte
net btcwire.BitcoinNet
res string
err error
}{
{[]byte{0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10},
btcwire.MainNet, "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC", nil},
{[]byte{0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37, 0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4},
btcwire.MainNet, "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8", nil},
// Raw address not 20 bytes (padded with leading 0s)
{[]byte{0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10},
btcwire.MainNet, "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC", btcutil.ErrMalformedAddress},
{[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet, "", btcutil.ErrUnknownNet},
// from bitcoind base58_keys_valid
{[]byte{0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e, 0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a},
btcwire.TestNet3, "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n", nil},
// from bitcoind base58_keys_valid
{[]byte{0x63, 0xbc, 0xc5, 0x65, 0xf9, 0xe6, 0x8e, 0xe0, 0x18, 0x9d, 0xd5, 0xcc, 0x67, 0xf1, 0xb0, 0xe5, 0xf0, 0x2f, 0x45, 0xcb},
btcwire.MainNet, "3AnNxabYGoTxYiTEZwFEnerUoeFXK2Zoks", nil},
// Bad network
{make([]byte, 20), 0, "", btcutil.ErrUnknownNet},
}
func TestEncodeScriptHashes(t *testing.T) {
for i := range encodeScriptHashTests {
res, err := btcutil.EncodeScriptHash(encodeScriptHashTests[i].raw,
encodeScriptHashTests[i].net)
if err != encodeScriptHashTests[i].err {
t.Errorf("Error Results differ: Expected '%v', returned '%v'", encodeScriptHashTests[i].err, err)
continue
}
if err == nil && res != encodeScriptHashTests[i].res {
t.Errorf("Results differ: Expected '%s', returned '%s'",
encodeScriptHashTests[i].res, res)
}
}
}