Implement Address interface.
Address is a generic interface for any type of "address" a transaction can be sent to, including but not limited to pay-to-pubkey, pay-to-pubkey-hash, and pay-to-script-hash. This change implements Address and concrete types for P2PKH and P2SH addresses with 100% test coverage. Pay-to-pubkey support will be added in the future. This API is intended to replace the old EncodeAddress/DecodeAddress functions which are now deprecated.
This commit is contained in:
parent
32f63f3abc
commit
58bae71f61
5 changed files with 585 additions and 6 deletions
|
@ -49,6 +49,8 @@ const (
|
|||
|
||||
// EncodeAddress takes a 20-byte raw payment address (hash160 of a pubkey)
|
||||
// and the Bitcoin network to create a human-readable payment address string.
|
||||
//
|
||||
// DEPRECATED - Use the EncodeAddress functions of the Address interface.
|
||||
func EncodeAddress(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err error) {
|
||||
if len(addrHash) != ripemd160.Size {
|
||||
return "", ErrMalformedAddress
|
||||
|
@ -69,6 +71,8 @@ func EncodeAddress(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err
|
|||
|
||||
// EncodeScriptHash takes a 20-byte raw script hash (hash160 of the SHA256 of the redeeming script)
|
||||
// and the Bitcoin network to create a human-readable payment address string.
|
||||
//
|
||||
// DEPRECATED - Use the EncodeAddress functions of the Address interface.
|
||||
func EncodeScriptHash(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err error) {
|
||||
if len(addrHash) != ripemd160.Size {
|
||||
return "", ErrMalformedAddress
|
||||
|
@ -104,6 +108,9 @@ func encodeHashWithNetId(netID byte, addrHash []byte) (encoded string, err error
|
|||
// DecodeAddress decodes a human-readable payment address string
|
||||
// returning the 20-byte decoded address, along with the Bitcoin
|
||||
// network for the address.
|
||||
//
|
||||
// DEPRECATED - Use DecodeAddr to decode a string encoded address to
|
||||
// the Address interface.
|
||||
func DecodeAddress(addr string) (addrHash []byte, net btcwire.BitcoinNet, err error) {
|
||||
decoded := Base58Decode(addr)
|
||||
|
||||
|
|
231
address.go
Normal file
231
address.go
Normal file
|
@ -0,0 +1,231 @@
|
|||
// 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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"code.google.com/p/go.crypto/ripemd160"
|
||||
"errors"
|
||||
"github.com/conformal/btcwire"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrChecksumMismatch describes an error where decoding failed due
|
||||
// to a bad checksum.
|
||||
ErrChecksumMismatch = errors.New("checksum mismatch")
|
||||
|
||||
// ErrUnknownIdentifier describes an error where decoding failed due
|
||||
// to an unknown magic byte identifier.
|
||||
ErrUnknownIdentifier = errors.New("unknown identifier byte")
|
||||
)
|
||||
|
||||
// Address is an interface type for any type of destination a transaction
|
||||
// output may spend to. This includes pay-to-pubkey (P2PK), pay-to-pubkey-hash
|
||||
// (P2PKH), and pay-to-script-hash (P2SH). Address is designed to be generic
|
||||
// enough that other kinds of addresses may be added in the future without
|
||||
// changing the decoding and encoding API.
|
||||
type Address interface {
|
||||
// EncodeAddress returns the string encoding of the address.
|
||||
EncodeAddress() string
|
||||
|
||||
// ScriptAddress returns the raw bytes of the address to be used
|
||||
// when inserting the address into a txout's script.
|
||||
ScriptAddress() []byte
|
||||
}
|
||||
|
||||
// DecodeAddr decodes the string encoding of an address and returns
|
||||
// the Address if addr is a valid encoding for a known address type.
|
||||
//
|
||||
// This is named DecodeAddr and not DecodeAddress due to DecodeAddress
|
||||
// already being defined for an old api. When the old api is eventually
|
||||
// removed, a proper DecodeAddress function will be added, and DecodeAddr
|
||||
// will become deprecated.
|
||||
func DecodeAddr(addr string) (Address, error) {
|
||||
decoded := Base58Decode(addr)
|
||||
|
||||
// Switch on decoded length to determine the type.
|
||||
switch len(decoded) {
|
||||
case 1 + ripemd160.Size + 4: // P2PKH or P2SH
|
||||
// Parse the network and hash type (pubkey hash vs script
|
||||
// hash) from the first byte.
|
||||
net := btcwire.MainNet
|
||||
isscript := false
|
||||
switch decoded[0] {
|
||||
case MainNetAddr:
|
||||
// Use defaults.
|
||||
|
||||
case TestNetAddr:
|
||||
net = btcwire.TestNet3
|
||||
|
||||
case MainNetScriptHash:
|
||||
isscript = true
|
||||
|
||||
case TestNetScriptHash:
|
||||
isscript = true
|
||||
net = btcwire.TestNet3
|
||||
|
||||
default:
|
||||
return nil, ErrUnknownIdentifier
|
||||
}
|
||||
|
||||
// Verify hash checksum. Checksum is calculated as the first
|
||||
// four bytes of double SHA256 of the network byte and hash.
|
||||
tosum := decoded[:ripemd160.Size+1]
|
||||
cksum := btcwire.DoubleSha256(tosum)[:4]
|
||||
if !bytes.Equal(cksum, decoded[len(decoded)-4:]) {
|
||||
return nil, ErrChecksumMismatch
|
||||
}
|
||||
|
||||
// Return concrete type.
|
||||
if isscript {
|
||||
return NewAddressScriptHashFromHash(
|
||||
decoded[1:ripemd160.Size+1], net)
|
||||
}
|
||||
return NewAddressPubKeyHash(decoded[1:ripemd160.Size+1],
|
||||
net)
|
||||
|
||||
case 33: // Compressed pubkey
|
||||
fallthrough
|
||||
|
||||
case 65: // Uncompressed pubkey
|
||||
// TODO(jrick)
|
||||
return nil, errors.New("pay-to-pubkey unimplemented")
|
||||
|
||||
default:
|
||||
return nil, errors.New("decoded address is of unknown size")
|
||||
}
|
||||
}
|
||||
|
||||
// AddressPubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH)
|
||||
// transaction.
|
||||
type AddressPubKeyHash struct {
|
||||
hash [ripemd160.Size]byte
|
||||
net btcwire.BitcoinNet
|
||||
}
|
||||
|
||||
// NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash must
|
||||
// be 20 bytes and net must be btcwire.MainNet or btcwire.TestNet3.
|
||||
func NewAddressPubKeyHash(pkHash []byte, net btcwire.BitcoinNet) (*AddressPubKeyHash, error) {
|
||||
// Check for a valid pubkey hash length.
|
||||
if len(pkHash) != ripemd160.Size {
|
||||
return nil, errors.New("pkHash must be 20 bytes")
|
||||
}
|
||||
|
||||
// Check for a valid bitcoin network.
|
||||
if !(net == btcwire.MainNet || net == btcwire.TestNet3) {
|
||||
return nil, ErrUnknownNet
|
||||
}
|
||||
|
||||
addr := &AddressPubKeyHash{net: net}
|
||||
copy(addr.hash[:], pkHash)
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
// EncodeAddress returns the string encoding of a pay-to-pubkey-hash
|
||||
// address. Part of the Address interface.
|
||||
func (a *AddressPubKeyHash) EncodeAddress() string {
|
||||
var netID byte
|
||||
switch a.net {
|
||||
case btcwire.MainNet:
|
||||
netID = MainNetAddr
|
||||
case btcwire.TestNet3:
|
||||
netID = TestNetAddr
|
||||
}
|
||||
|
||||
tosum := append([]byte{netID}, a.hash[:]...)
|
||||
cksum := btcwire.DoubleSha256(tosum)
|
||||
|
||||
// Address before base58 encoding is 1 byte for netID, 20 bytes for
|
||||
// hash, plus 4 bytes of checksum (total 25).
|
||||
b := make([]byte, 25, 25)
|
||||
b[0] = netID
|
||||
copy(b[1:], a.hash[:])
|
||||
copy(b[21:], cksum[:4])
|
||||
|
||||
return Base58Encode(b)
|
||||
}
|
||||
|
||||
// ScriptAddress returns the bytes to be included in a txout script to pay
|
||||
// to a pubkey hash. Part of the Address interface.
|
||||
func (a *AddressPubKeyHash) ScriptAddress() []byte {
|
||||
return a.hash[:]
|
||||
}
|
||||
|
||||
// Net returns the bitcoin network associated with the pay-to-pubkey-hash
|
||||
// address.
|
||||
func (a *AddressPubKeyHash) Net() btcwire.BitcoinNet {
|
||||
return a.net
|
||||
}
|
||||
|
||||
// AddressScriptHash is an Address for a pay-to-script-hash (P2SH)
|
||||
// transaction.
|
||||
type AddressScriptHash struct {
|
||||
hash [ripemd160.Size]byte
|
||||
net btcwire.BitcoinNet
|
||||
}
|
||||
|
||||
// NewAddressScriptHash returns a new AddressScriptHash. net must be
|
||||
// btcwire.MainNet or btcwire.TestNet3.
|
||||
func NewAddressScriptHash(serializedScript []byte, net btcwire.BitcoinNet) (*AddressScriptHash, error) {
|
||||
// Create hash of serialized script.
|
||||
scriptHash := Hash160(serializedScript)
|
||||
|
||||
return NewAddressScriptHashFromHash(scriptHash, net)
|
||||
}
|
||||
|
||||
// NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash
|
||||
// must be 20 bytes and net must be btcwire.MainNet or btcwire.TestNet3.
|
||||
func NewAddressScriptHashFromHash(scriptHash []byte, net btcwire.BitcoinNet) (*AddressScriptHash, error) {
|
||||
|
||||
// Check for a valid script hash length.
|
||||
if len(scriptHash) != ripemd160.Size {
|
||||
return nil, errors.New("scriptHash must be 20 bytes")
|
||||
}
|
||||
|
||||
// Check for a valid bitcoin network.
|
||||
if !(net == btcwire.MainNet || net == btcwire.TestNet3) {
|
||||
return nil, ErrUnknownNet
|
||||
}
|
||||
|
||||
addr := &AddressScriptHash{net: net}
|
||||
copy(addr.hash[:], scriptHash)
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
// EncodeAddress returns the string encoding of a pay-to-script-hash
|
||||
// address. Part of the Address interface.
|
||||
func (a *AddressScriptHash) EncodeAddress() string {
|
||||
var netID byte
|
||||
switch a.net {
|
||||
case btcwire.MainNet:
|
||||
netID = MainNetScriptHash
|
||||
case btcwire.TestNet3:
|
||||
netID = TestNetScriptHash
|
||||
}
|
||||
|
||||
tosum := append([]byte{netID}, a.hash[:]...)
|
||||
cksum := btcwire.DoubleSha256(tosum)
|
||||
|
||||
// P2SH address before base58 encoding is 1 byte for netID, 20 bytes
|
||||
// for hash, plus 4 bytes of checksum (total 25).
|
||||
b := make([]byte, 25, 25)
|
||||
b[0] = netID
|
||||
copy(b[1:], a.hash[:])
|
||||
copy(b[21:], cksum[:4])
|
||||
|
||||
return Base58Encode(b)
|
||||
}
|
||||
|
||||
// ScriptAddress returns the bytes to be included in a txout script to pay
|
||||
// to a script hash. Part of the Address interface.
|
||||
func (a *AddressScriptHash) ScriptAddress() []byte {
|
||||
return a.hash[:]
|
||||
}
|
||||
|
||||
// Net returns the bitcoin network associated with the pay-to-script-hash
|
||||
// address.
|
||||
func (a *AddressScriptHash) Net() btcwire.BitcoinNet {
|
||||
return a.net
|
||||
}
|
295
address_test.go
Normal file
295
address_test.go
Normal file
|
@ -0,0 +1,295 @@
|
|||
// 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"
|
||||
"code.google.com/p/go.crypto/ripemd160"
|
||||
"github.com/conformal/btcutil"
|
||||
"github.com/conformal/btcwire"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAddresses(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr string
|
||||
valid bool
|
||||
result btcutil.Address
|
||||
f func() (btcutil.Address, error)
|
||||
net btcwire.BitcoinNet // only checked for P2PKH and P2SH
|
||||
}{
|
||||
// Positive P2PKH tests.
|
||||
{
|
||||
name: "mainnet p2pkh",
|
||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
||||
valid: true,
|
||||
result: btcutil.TstAddressPubKeyHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
|
||||
0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
|
||||
btcwire.MainNet),
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
|
||||
0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84}
|
||||
return btcutil.NewAddressPubKeyHash(pkHash, btcwire.MainNet)
|
||||
},
|
||||
net: btcwire.MainNet,
|
||||
},
|
||||
{
|
||||
name: "mainnet p2pkh 2",
|
||||
addr: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
|
||||
valid: true,
|
||||
result: btcutil.TstAddressPubKeyHash(
|
||||
[ripemd160.Size]byte{
|
||||
0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
|
||||
0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa},
|
||||
btcwire.MainNet),
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
|
||||
0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa}
|
||||
return btcutil.NewAddressPubKeyHash(pkHash, btcwire.MainNet)
|
||||
},
|
||||
net: btcwire.MainNet,
|
||||
},
|
||||
{
|
||||
name: "testnet p2pkh",
|
||||
addr: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||
valid: true,
|
||||
result: btcutil.TstAddressPubKeyHash(
|
||||
[ripemd160.Size]byte{
|
||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||
0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
|
||||
btcwire.TestNet3),
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||
0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f}
|
||||
return btcutil.NewAddressPubKeyHash(pkHash, btcwire.TestNet3)
|
||||
},
|
||||
net: btcwire.TestNet3,
|
||||
},
|
||||
|
||||
// Negative P2PKH tests.
|
||||
{
|
||||
name: "p2pkh wrong byte identifier/net",
|
||||
addr: "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||
valid: false,
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||
0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f}
|
||||
return btcutil.NewAddressPubKeyHash(pkHash, btcwire.TestNet)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "p2pkh wrong hash length",
|
||||
addr: "",
|
||||
valid: false,
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b,
|
||||
0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad,
|
||||
0xaa}
|
||||
return btcutil.NewAddressPubKeyHash(pkHash, btcwire.MainNet)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "p2pkh bad checksum",
|
||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY",
|
||||
valid: false,
|
||||
},
|
||||
|
||||
// Positive P2SH tests.
|
||||
{
|
||||
// Taken from transactions:
|
||||
// output: 3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac
|
||||
// input: 837dea37ddc8b1e3ce646f1a656e79bbd8cc7f558ac56a169626d649ebe2a3ba.
|
||||
name: "mainnet p2sh",
|
||||
addr: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
|
||||
valid: true,
|
||||
result: btcutil.TstAddressScriptHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2,
|
||||
0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10},
|
||||
btcwire.MainNet),
|
||||
f: func() (btcutil.Address, error) {
|
||||
script := []byte{
|
||||
0x52, 0x41, 0x04, 0x91, 0xbb, 0xa2, 0x51, 0x09, 0x12, 0xa5,
|
||||
0xbd, 0x37, 0xda, 0x1f, 0xb5, 0xb1, 0x67, 0x30, 0x10, 0xe4,
|
||||
0x3d, 0x2c, 0x6d, 0x81, 0x2c, 0x51, 0x4e, 0x91, 0xbf, 0xa9,
|
||||
0xf2, 0xeb, 0x12, 0x9e, 0x1c, 0x18, 0x33, 0x29, 0xdb, 0x55,
|
||||
0xbd, 0x86, 0x8e, 0x20, 0x9a, 0xac, 0x2f, 0xbc, 0x02, 0xcb,
|
||||
0x33, 0xd9, 0x8f, 0xe7, 0x4b, 0xf2, 0x3f, 0x0c, 0x23, 0x5d,
|
||||
0x61, 0x26, 0xb1, 0xd8, 0x33, 0x4f, 0x86, 0x41, 0x04, 0x86,
|
||||
0x5c, 0x40, 0x29, 0x3a, 0x68, 0x0c, 0xb9, 0xc0, 0x20, 0xe7,
|
||||
0xb1, 0xe1, 0x06, 0xd8, 0xc1, 0x91, 0x6d, 0x3c, 0xef, 0x99,
|
||||
0xaa, 0x43, 0x1a, 0x56, 0xd2, 0x53, 0xe6, 0x92, 0x56, 0xda,
|
||||
0xc0, 0x9e, 0xf1, 0x22, 0xb1, 0xa9, 0x86, 0x81, 0x8a, 0x7c,
|
||||
0xb6, 0x24, 0x53, 0x2f, 0x06, 0x2c, 0x1d, 0x1f, 0x87, 0x22,
|
||||
0x08, 0x48, 0x61, 0xc5, 0xc3, 0x29, 0x1c, 0xcf, 0xfe, 0xf4,
|
||||
0xec, 0x68, 0x74, 0x41, 0x04, 0x8d, 0x24, 0x55, 0xd2, 0x40,
|
||||
0x3e, 0x08, 0x70, 0x8f, 0xc1, 0xf5, 0x56, 0x00, 0x2f, 0x1b,
|
||||
0x6c, 0xd8, 0x3f, 0x99, 0x2d, 0x08, 0x50, 0x97, 0xf9, 0x97,
|
||||
0x4a, 0xb0, 0x8a, 0x28, 0x83, 0x8f, 0x07, 0x89, 0x6f, 0xba,
|
||||
0xb0, 0x8f, 0x39, 0x49, 0x5e, 0x15, 0xfa, 0x6f, 0xad, 0x6e,
|
||||
0xdb, 0xfb, 0x1e, 0x75, 0x4e, 0x35, 0xfa, 0x1c, 0x78, 0x44,
|
||||
0xc4, 0x1f, 0x32, 0x2a, 0x18, 0x63, 0xd4, 0x62, 0x13, 0x53,
|
||||
0xae}
|
||||
return btcutil.NewAddressScriptHash(script, btcwire.MainNet)
|
||||
},
|
||||
net: btcwire.MainNet,
|
||||
},
|
||||
{
|
||||
// Taken from transactions:
|
||||
// output: b0539a45de13b3e0403909b8bd1a555b8cbe45fd4e3f3fda76f3a5f52835c29d
|
||||
// input: (not yet redeemed at time test was written)
|
||||
name: "mainnet p2sh 2",
|
||||
addr: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8",
|
||||
valid: true,
|
||||
result: btcutil.TstAddressScriptHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37,
|
||||
0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4},
|
||||
btcwire.MainNet),
|
||||
f: func() (btcutil.Address, error) {
|
||||
hash := []byte{
|
||||
0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37,
|
||||
0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4}
|
||||
return btcutil.NewAddressScriptHashFromHash(hash, btcwire.MainNet)
|
||||
},
|
||||
net: btcwire.MainNet,
|
||||
},
|
||||
{
|
||||
// Taken from bitcoind base58_keys_valid.
|
||||
name: "testnet p2sh",
|
||||
addr: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||
valid: true,
|
||||
result: btcutil.TstAddressScriptHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||
0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a},
|
||||
btcwire.TestNet3),
|
||||
f: func() (btcutil.Address, error) {
|
||||
hash := []byte{
|
||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||
0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a}
|
||||
return btcutil.NewAddressScriptHashFromHash(hash, btcwire.TestNet3)
|
||||
},
|
||||
net: btcwire.TestNet3,
|
||||
},
|
||||
|
||||
// Negative P2SH tests.
|
||||
{
|
||||
name: "p2sh wrong hash length",
|
||||
addr: "",
|
||||
valid: false,
|
||||
f: func() (btcutil.Address, error) {
|
||||
hash := []byte{
|
||||
0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9,
|
||||
0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55,
|
||||
0x10}
|
||||
return btcutil.NewAddressScriptHashFromHash(hash, btcwire.MainNet)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "p2sh wrong byte identifier/net",
|
||||
addr: "0NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||
valid: false,
|
||||
f: func() (btcutil.Address, error) {
|
||||
hash := []byte{
|
||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||
0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a}
|
||||
return btcutil.NewAddressScriptHashFromHash(hash, btcwire.TestNet)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
// Decode addr and compare error against valid.
|
||||
decoded, err := btcutil.DecodeAddr(test.addr)
|
||||
if (err == nil) != test.valid {
|
||||
t.Errorf("%v: decoding test failed", test.name)
|
||||
return
|
||||
}
|
||||
|
||||
// If decoding succeeded, encode again and compare against the original.
|
||||
if err == nil {
|
||||
encoded := decoded.EncodeAddress()
|
||||
|
||||
// Compare encoded addr against the original encoding.
|
||||
if test.addr != encoded {
|
||||
t.Errorf("%v: decoding and encoding produced different addressess: %v != %v",
|
||||
test.name, test.addr, encoded)
|
||||
return
|
||||
}
|
||||
|
||||
// Perform type-specific calculations.
|
||||
var saddr []byte
|
||||
var net btcwire.BitcoinNet
|
||||
switch d := decoded.(type) {
|
||||
case *btcutil.AddressPubKeyHash:
|
||||
saddr = btcutil.TstAddressSAddr(encoded)
|
||||
|
||||
// Net is not part of the Address interface and
|
||||
// must be calculated here.
|
||||
net = d.Net()
|
||||
|
||||
case *btcutil.AddressScriptHash:
|
||||
saddr = btcutil.TstAddressSAddr(encoded)
|
||||
|
||||
// Net is not part of the Address interface and
|
||||
// must be calculated here.
|
||||
net = d.Net()
|
||||
}
|
||||
|
||||
// Check script address.
|
||||
if !bytes.Equal(saddr, decoded.ScriptAddress()) {
|
||||
t.Errorf("%v: script addresses do not match:\n%v != \n%v",
|
||||
test.name, saddr, decoded.ScriptAddress())
|
||||
return
|
||||
}
|
||||
|
||||
// Check networks. This check always succeeds for non-P2PKH and
|
||||
// non-P2SH addresses as both nets will be Go's default zero value.
|
||||
if net != test.net {
|
||||
t.Errorf("%v: calculated network does not match expected",
|
||||
test.name)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if !test.valid {
|
||||
// If address is invalid, but a creation function exists,
|
||||
// verify that it returns a nil addr and non-nil error.
|
||||
if test.f != nil {
|
||||
_, err := test.f()
|
||||
if err == nil {
|
||||
t.Errorf("%v: address is invalid but creating new address succeeded",
|
||||
test.name)
|
||||
return
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Valid test, compare address created with f against expected result.
|
||||
addr, err := test.f()
|
||||
if err != nil {
|
||||
t.Errorf("%v: address is valid but creating new address failed with error %v",
|
||||
test.name, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(addr, test.result) {
|
||||
t.Errorf("%v: created address does not match expected result",
|
||||
test.name)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,6 +11,11 @@ interface. The functions are only exported while the tests are being run.
|
|||
|
||||
package btcutil
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.crypto/ripemd160"
|
||||
"github.com/conformal/btcwire"
|
||||
)
|
||||
|
||||
// SetBlockBytes sets the internal serialized block byte buffer to the passed
|
||||
// buffer. It is used to inject errors and is only available to the test
|
||||
// package.
|
||||
|
@ -23,3 +28,32 @@ func (b *Block) SetBlockBytes(buf []byte) {
|
|||
func TstAppDataDir(goos, appName string, roaming bool) string {
|
||||
return appDataDir(goos, appName, roaming)
|
||||
}
|
||||
|
||||
// TstAddressPubKeyHash makes an AddressPubKeyHash, setting the
|
||||
// unexported fields with the parameters hash and net.
|
||||
func TstAddressPubKeyHash(hash [ripemd160.Size]byte,
|
||||
net btcwire.BitcoinNet) *AddressPubKeyHash {
|
||||
|
||||
return &AddressPubKeyHash{
|
||||
hash: hash,
|
||||
net: net,
|
||||
}
|
||||
}
|
||||
|
||||
// TstAddressScriptHash makes an AddressScriptHash, setting the
|
||||
// unexported fields with the parameters hash and net.
|
||||
func TstAddressScriptHash(hash [ripemd160.Size]byte,
|
||||
net btcwire.BitcoinNet) *AddressScriptHash {
|
||||
|
||||
return &AddressScriptHash{
|
||||
hash: hash,
|
||||
net: net,
|
||||
}
|
||||
}
|
||||
|
||||
// TstAddressSAddr returns the expected script address bytes for
|
||||
// P2PKH and P2SH bitcoin addresses.
|
||||
func TstAddressSAddr(addr string) []byte {
|
||||
decoded := Base58Decode(addr)
|
||||
return decoded[1 : 1+ripemd160.Size]
|
||||
}
|
||||
|
|
|
@ -4,30 +4,42 @@ github.com/conformal/btcutil/base58.go Base58Encode 100.00% (15/15)
|
|||
github.com/conformal/btcutil/addrconvs.go DecodeAddress 100.00% (14/14)
|
||||
github.com/conformal/btcutil/block.go Block.Tx 100.00% (12/12)
|
||||
github.com/conformal/btcutil/block.go Block.Transactions 100.00% (11/11)
|
||||
github.com/conformal/btcutil/address.go AddressScriptHash.EncodeAddress 100.00% (11/11)
|
||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.EncodeAddress 100.00% (11/11)
|
||||
github.com/conformal/btcutil/block.go Block.TxShas 100.00% (10/10)
|
||||
github.com/conformal/btcutil/addrconvs.go EncodeAddress 100.00% (8/8)
|
||||
github.com/conformal/btcutil/addrconvs.go EncodeScriptHash 100.00% (8/8)
|
||||
github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (7/7)
|
||||
github.com/conformal/btcutil/addrconvs.go encodeHashWithNetId 100.00% (7/7)
|
||||
github.com/conformal/btcutil/address.go NewAddressPubKeyHash 100.00% (7/7)
|
||||
github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (7/7)
|
||||
github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (7/7)
|
||||
github.com/conformal/btcutil/address.go NewAddressScriptHashFromHash 100.00% (7/7)
|
||||
github.com/conformal/btcutil/addrconvs.go encodeHashWithNetId 100.00% (7/7)
|
||||
github.com/conformal/btcutil/tx.go Tx.Sha 100.00% (5/5)
|
||||
github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5)
|
||||
github.com/conformal/btcutil/block.go Block.TxSha 100.00% (4/4)
|
||||
github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1)
|
||||
github.com/conformal/btcutil/address.go NewAddressScriptHash 100.00% (2/2)
|
||||
github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2)
|
||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.Net 100.00% (1/1)
|
||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.ScriptAddress 100.00% (1/1)
|
||||
github.com/conformal/btcutil/address.go AddressScriptHash.ScriptAddress 100.00% (1/1)
|
||||
github.com/conformal/btcutil/address.go AddressScriptHash.Net 100.00% (1/1)
|
||||
github.com/conformal/btcutil/block.go OutOfRangeError.Error 100.00% (1/1)
|
||||
github.com/conformal/btcutil/block.go Block.MsgBlock 100.00% (1/1)
|
||||
github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1)
|
||||
github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1)
|
||||
github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1)
|
||||
github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1)
|
||||
github.com/conformal/btcutil/tx.go Tx.Index 100.00% (1/1)
|
||||
github.com/conformal/btcutil/tx.go Tx.SetIndex 100.00% (1/1)
|
||||
github.com/conformal/btcutil/tx.go NewTx 100.00% (1/1)
|
||||
github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1)
|
||||
github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1)
|
||||
github.com/conformal/btcutil/block.go Block.SetHeight 100.00% (1/1)
|
||||
github.com/conformal/btcutil/appdata.go appDataDir 92.00% (23/25)
|
||||
github.com/conformal/btcutil/addrconvs.go EncodePrivateKey 90.91% (20/22)
|
||||
github.com/conformal/btcutil/block.go Block.TxLoc 88.89% (8/9)
|
||||
github.com/conformal/btcutil/address.go DecodeAddr 90.00% (18/20)
|
||||
github.com/conformal/btcutil/block.go Block.Bytes 88.89% (8/9)
|
||||
github.com/conformal/btcutil/block.go Block.TxLoc 88.89% (8/9)
|
||||
github.com/conformal/btcutil/addrconvs.go DecodePrivateKey 82.61% (19/23)
|
||||
github.com/conformal/btcutil/appdata.go AppDataDir 0.00% (0/1)
|
||||
github.com/conformal/btcutil ------------------------- 95.26% (221/232)
|
||||
github.com/conformal/btcutil ------------------------------- 95.62% (284/297)
|
||||
|
||||
|
|
Loading…
Reference in a new issue