Encode and decode addresses with a btcwire.BitcoinNet, not a byte.
While here, fix a couple of append() leaks.
This commit is contained in:
parent
867149f470
commit
e433a02e4b
2 changed files with 63 additions and 38 deletions
61
addrconvs.go
61
addrconvs.go
|
@ -11,10 +11,9 @@ import (
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrAddrUnknownNet describes an error where the address identifier
|
// ErrUnknownNet describes an error where the Bitcoin network is
|
||||||
// byte is not recognized as belonging to neither the Bitcoin MainNet nor
|
// not recognized.
|
||||||
// TestNet.
|
var ErrUnknownNet = errors.New("unrecognized bitcoin network")
|
||||||
var ErrAddrUnknownNet = errors.New("unrecognized network identifier byte")
|
|
||||||
|
|
||||||
// ErrMalformedAddress describes an error where an address is improperly
|
// ErrMalformedAddress describes an error where an address is improperly
|
||||||
// formatted, either due to an incorrect length of the hashed pubkey or
|
// formatted, either due to an incorrect length of the hashed pubkey or
|
||||||
|
@ -31,30 +30,40 @@ const (
|
||||||
TestNetAddr = 0x6f
|
TestNetAddr = 0x6f
|
||||||
)
|
)
|
||||||
|
|
||||||
// EncodeAddress takes a 20-byte raw payment address (hash160 of the
|
// EncodeAddress takes a 20-byte raw payment address (hash160 of a pubkey)
|
||||||
// uncompressed pubkey) and a network identifying byte and encodes the
|
// and the Bitcoin network to create a human-readable payment address string.
|
||||||
// payment address in a human readable string.
|
func EncodeAddress(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err error) {
|
||||||
func EncodeAddress(addrHash []byte, netID byte) (encoded string, err error) {
|
|
||||||
if len(addrHash) != ripemd160.Size {
|
if len(addrHash) != ripemd160.Size {
|
||||||
return "", ErrMalformedAddress
|
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...)
|
tosum := append([]byte{netID}, addrHash...)
|
||||||
cksum := btcwire.DoubleSha256(tosum)
|
cksum := btcwire.DoubleSha256(tosum)
|
||||||
|
|
||||||
a := append([]byte{netID}, addrHash...)
|
// Address before base58 encoding is 1 byte for netID, 20 bytes for
|
||||||
a = append(a, cksum[:4]...)
|
// 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
|
return Base58Encode(a), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeAddress decodes a human readable payment address string
|
// DecodeAddress decodes a human-readable payment address string
|
||||||
// returning the 20-byte decoded address, along with the network
|
// returning the 20-byte decoded address, along with the Bitcoin
|
||||||
// identifying byte.
|
// network for the address.
|
||||||
func DecodeAddress(addr string) (addrHash []byte, netID byte, err error) {
|
func DecodeAddress(addr string) (addrHash []byte, net btcwire.BitcoinNet, err error) {
|
||||||
decoded := Base58Decode(addr)
|
decoded := Base58Decode(addr)
|
||||||
|
|
||||||
// Length of decoded address must be 20 bytes + 1 byte for a network
|
// 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
|
return nil, 0x00, ErrMalformedAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
netID = decoded[0]
|
switch decoded[0] {
|
||||||
if netID != MainNetAddr && netID != TestNetAddr {
|
case MainNetAddr:
|
||||||
return nil, 0x00, ErrAddrUnknownNet
|
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
|
// Checksum is first four bytes of double SHA256 of the network byte
|
||||||
// and addrHash. Verify this matches the final 4 bytes of the decoded
|
// and addrHash. Verify this matches the final 4 bytes of the decoded
|
||||||
// address.
|
// address.
|
||||||
tosum := append([]byte{netID}, addrHash...)
|
tosum := decoded[:ripemd160.Size+1]
|
||||||
cksum := btcwire.DoubleSha256(tosum)[:4]
|
cksum := btcwire.DoubleSha256(tosum)[:4]
|
||||||
if !bytes.Equal(cksum, decoded[len(decoded)-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
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,28 +7,31 @@ package btcutil_test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
|
"github.com/conformal/btcwire"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var encodeTests = []struct {
|
var encodeTests = []struct {
|
||||||
raw []byte
|
raw []byte
|
||||||
net byte
|
net btcwire.BitcoinNet
|
||||||
res string
|
res string
|
||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{[]byte{0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
|
{[]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},
|
{[]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},
|
{[]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)
|
// 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},
|
{[]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
|
// Bad network
|
||||||
{make([]byte, 20), btcutil.MainNetAddr + 1, "", btcutil.ErrAddrUnknownNet},
|
{make([]byte, 20), 0, "", btcutil.ErrUnknownNet},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeAddresses(t *testing.T) {
|
func TestEncodeAddresses(t *testing.T) {
|
||||||
|
@ -49,35 +52,42 @@ func TestEncodeAddresses(t *testing.T) {
|
||||||
var decodeTests = []struct {
|
var decodeTests = []struct {
|
||||||
addr string
|
addr string
|
||||||
res []byte
|
res []byte
|
||||||
net byte
|
net btcwire.BitcoinNet
|
||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
||||||
[]byte{0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
|
[]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",
|
{"mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||||
[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
|
[]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
|
// Wrong length
|
||||||
{"01MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcutil.MainNetAddr, btcutil.ErrMalformedAddress},
|
{"01MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
|
||||||
|
|
||||||
// Bad magic
|
// Bad magic
|
||||||
{"2MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcutil.MainNetAddr, btcutil.ErrAddrUnknownNet},
|
{"2MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrUnknownNet},
|
||||||
|
|
||||||
// Bad checksum
|
// Bad checksum
|
||||||
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2dpuqz", nil, btcutil.MainNetAddr, btcutil.ErrMalformedAddress},
|
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2dpuqz", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeAddresses(t *testing.T) {
|
func TestDecodeAddresses(t *testing.T) {
|
||||||
for i := range decodeTests {
|
for i := range decodeTests {
|
||||||
res, _, err := btcutil.DecodeAddress(decodeTests[i].addr)
|
res, net, err := btcutil.DecodeAddress(decodeTests[i].addr)
|
||||||
if err != decodeTests[i].err {
|
if err != decodeTests[i].err {
|
||||||
t.Error(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'",
|
t.Errorf("Results differ: Expected '%v', returned '%v'",
|
||||||
decodeTests[i].res, res)
|
decodeTests[i].res, res)
|
||||||
}
|
}
|
||||||
|
if net != decodeTests[i].net {
|
||||||
|
t.Errorf("Networks differ: Expected '%v', returned '%v'",
|
||||||
|
decodeTests[i].net, net)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue