Encode and decode addresses with a btcwire.BitcoinNet, not a byte.

While here, fix a couple of append() leaks.
This commit is contained in:
Josh Rickmar 2013-10-08 11:52:12 -04:00
parent 867149f470
commit e433a02e4b
2 changed files with 63 additions and 38 deletions

View file

@ -11,10 +11,9 @@ import (
"github.com/conformal/btcwire"
)
// ErrAddrUnknownNet describes an error where the address identifier
// byte is not recognized as belonging to neither the Bitcoin MainNet nor
// TestNet.
var ErrAddrUnknownNet = errors.New("unrecognized network identifier byte")
// ErrUnknownNet describes an error where the Bitcoin network is
// not recognized.
var ErrUnknownNet = errors.New("unrecognized bitcoin network")
// ErrMalformedAddress describes an error where an address is improperly
// formatted, either due to an incorrect length of the hashed pubkey or
@ -31,30 +30,40 @@ const (
TestNetAddr = 0x6f
)
// EncodeAddress takes a 20-byte raw payment address (hash160 of the
// uncompressed pubkey) and a network identifying byte and encodes the
// payment address in a human readable string.
func EncodeAddress(addrHash []byte, netID byte) (encoded string, err error) {
// EncodeAddress takes a 20-byte raw payment address (hash160 of a pubkey)
// and the Bitcoin network to create a human-readable payment address string.
func EncodeAddress(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err error) {
if len(addrHash) != ripemd160.Size {
return "", ErrMalformedAddress
}
if netID != MainNetAddr && netID != TestNetAddr {
return "", ErrAddrUnknownNet
var netID byte
switch net {
case btcwire.MainNet:
netID = MainNetAddr
case btcwire.TestNet3:
netID = TestNetAddr
default:
return "", ErrUnknownNet
}
tosum := append([]byte{netID}, addrHash...)
cksum := btcwire.DoubleSha256(tosum)
a := append([]byte{netID}, addrHash...)
a = append(a, cksum[:4]...)
// Address before base58 encoding is 1 byte for netID, 20 bytes for
// hash, plus 4 bytes of checksum.
a := make([]byte, 25, 25)
a[0] = netID
copy(a[1:], addrHash)
copy(a[21:], cksum[:4])
return Base58Encode(a), nil
}
// DecodeAddress decodes a human readable payment address string
// returning the 20-byte decoded address, along with the network
// identifying byte.
func DecodeAddress(addr string) (addrHash []byte, netID byte, err error) {
// DecodeAddress decodes a human-readable payment address string
// returning the 20-byte decoded address, along with the Bitcoin
// network for the address.
func DecodeAddress(addr string) (addrHash []byte, net btcwire.BitcoinNet, err error) {
decoded := Base58Decode(addr)
// Length of decoded address must be 20 bytes + 1 byte for a network
@ -63,20 +72,26 @@ func DecodeAddress(addr string) (addrHash []byte, netID byte, err error) {
return nil, 0x00, ErrMalformedAddress
}
netID = decoded[0]
if netID != MainNetAddr && netID != TestNetAddr {
return nil, 0x00, ErrAddrUnknownNet
switch decoded[0] {
case MainNetAddr:
net = btcwire.MainNet
case TestNetAddr:
net = btcwire.TestNet3
default:
return nil, 0, ErrUnknownNet
}
addrHash = decoded[1:21]
// Checksum is first four bytes of double SHA256 of the network byte
// and addrHash. Verify this matches the final 4 bytes of the decoded
// address.
tosum := append([]byte{netID}, addrHash...)
tosum := decoded[:ripemd160.Size+1]
cksum := btcwire.DoubleSha256(tosum)[:4]
if !bytes.Equal(cksum, decoded[len(decoded)-4:]) {
return nil, 0x00, ErrMalformedAddress
return nil, net, ErrMalformedAddress
}
return addrHash, netID, nil
addrHash = make([]byte, ripemd160.Size, ripemd160.Size)
copy(addrHash, decoded[1:ripemd160.Size+1])
return addrHash, net, nil
}

View file

@ -7,28 +7,31 @@ package btcutil_test
import (
"bytes"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
"testing"
)
var encodeTests = []struct {
raw []byte
net 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},
btcutil.MainNetAddr, "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil},
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},
btcutil.MainNetAddr, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", nil},
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},
btcutil.TestNetAddr, "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", nil},
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},
btcutil.MainNetAddr, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", btcutil.ErrMalformedAddress},
btcwire.MainNet, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", btcutil.ErrMalformedAddress},
// Bad network byte
{make([]byte, 20), btcutil.MainNetAddr + 1, "", btcutil.ErrAddrUnknownNet},
// Bad network
{make([]byte, 20), 0, "", btcutil.ErrUnknownNet},
}
func TestEncodeAddresses(t *testing.T) {
@ -49,35 +52,42 @@ func TestEncodeAddresses(t *testing.T) {
var decodeTests = []struct {
addr string
res []byte
net 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},
btcutil.MainNetAddr, nil},
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},
btcutil.TestNetAddr, nil},
btcwire.TestNet3, nil},
// Wrong length
{"01MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcutil.MainNetAddr, btcutil.ErrMalformedAddress},
{"01MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
// Bad magic
{"2MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcutil.MainNetAddr, btcutil.ErrAddrUnknownNet},
{"2MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrUnknownNet},
// Bad checksum
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2dpuqz", nil, btcutil.MainNetAddr, btcutil.ErrMalformedAddress},
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2dpuqz", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
}
func TestDecodeAddresses(t *testing.T) {
for i := range decodeTests {
res, _, err := btcutil.DecodeAddress(decodeTests[i].addr)
res, net, err := btcutil.DecodeAddress(decodeTests[i].addr)
if err != decodeTests[i].err {
t.Error(err)
}
if err == nil && !bytes.Equal(res, decodeTests[i].res) {
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)
}
}
}