Modify DecodeAddress to accept serialized pubkeys.
This commit modifies DecodeAddress to accept and decode pay-to-pubkey addresses (raw serialized public keys). Since the resulting Address needs to have a network associated with it, and a raw serialized public key does not encode the network with it, a new parameter has been added which requires the caller to specify a default network to use when decoding addresses. In the case the address has a network encoded with it such as for pay-to-pubkey-hash and pay-to-script-hash addresses, the network will be decoded from the address and the resulting Address instance will have that network associated with it. When the address does NOT have a network encoded with it, such as a pay-to-pubkey address, the provided default network will be associated with the returned Address instance. Also, the tests have been updated to test the new functionality. ok @owainga and @jrick.
This commit is contained in:
parent
53483d0843
commit
60d4bed78f
3 changed files with 156 additions and 144 deletions
17
address.go
17
address.go
|
@ -118,10 +118,23 @@ type Address interface {
|
||||||
|
|
||||||
// DecodeAddress decodes the string encoding of an address and returns
|
// DecodeAddress decodes the string encoding of an address and returns
|
||||||
// the Address if addr is a valid encoding for a known address type.
|
// the Address if addr is a valid encoding for a known address type.
|
||||||
func DecodeAddress(addr string) (Address, error) {
|
//
|
||||||
decoded := Base58Decode(addr)
|
// The bitcoin network the address is associated with is extracted if possible.
|
||||||
|
// When the address does not encode the network, such as in the case of a raw
|
||||||
|
// public key, the address will be associated with the passed defaultNet.
|
||||||
|
func DecodeAddress(addr string, defaultNet btcwire.BitcoinNet) (Address, error) {
|
||||||
|
// Serialized public keys are either 65 bytes (130 hex chars) if
|
||||||
|
// uncompressed/hybrid or 33 bytes (66 hex chars) if compressed.
|
||||||
|
if len(addr) == 130 || len(addr) == 66 {
|
||||||
|
serializedPubKey, err := hex.DecodeString(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewAddressPubKey(serializedPubKey, defaultNet)
|
||||||
|
}
|
||||||
|
|
||||||
// Switch on decoded length to determine the type.
|
// Switch on decoded length to determine the type.
|
||||||
|
decoded := Base58Decode(addr)
|
||||||
switch len(decoded) {
|
switch len(decoded) {
|
||||||
case 1 + ripemd160.Size + 4: // P2PKH or P2SH
|
case 1 + ripemd160.Size + 4: // P2PKH or P2SH
|
||||||
// Parse the network and hash type (pubkey hash vs script
|
// Parse the network and hash type (pubkey hash vs script
|
||||||
|
|
228
address_test.go
228
address_test.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"code.google.com/p/go.crypto/ripemd160"
|
"code.google.com/p/go.crypto/ripemd160"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -19,20 +20,20 @@ const invalidNet = btcwire.BitcoinNet(0xffffffff)
|
||||||
|
|
||||||
func TestAddresses(t *testing.T) {
|
func TestAddresses(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
addr string
|
addr string
|
||||||
valid bool
|
encoded string
|
||||||
canDecode bool
|
valid bool
|
||||||
result btcutil.Address
|
result btcutil.Address
|
||||||
f func() (btcutil.Address, error)
|
f func() (btcutil.Address, error)
|
||||||
net btcwire.BitcoinNet
|
net btcwire.BitcoinNet
|
||||||
}{
|
}{
|
||||||
// Positive P2PKH tests.
|
// Positive P2PKH tests.
|
||||||
{
|
{
|
||||||
name: "mainnet p2pkh",
|
name: "mainnet p2pkh",
|
||||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
||||||
valid: true,
|
encoded: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
||||||
canDecode: true,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKeyHash(
|
result: btcutil.TstAddressPubKeyHash(
|
||||||
[ripemd160.Size]byte{
|
[ripemd160.Size]byte{
|
||||||
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
|
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
|
||||||
|
@ -47,10 +48,10 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mainnet p2pkh 2",
|
name: "mainnet p2pkh 2",
|
||||||
addr: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
|
addr: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
|
||||||
valid: true,
|
encoded: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
|
||||||
canDecode: true,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKeyHash(
|
result: btcutil.TstAddressPubKeyHash(
|
||||||
[ripemd160.Size]byte{
|
[ripemd160.Size]byte{
|
||||||
0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
|
0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
|
||||||
|
@ -65,10 +66,10 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "testnet p2pkh",
|
name: "testnet p2pkh",
|
||||||
addr: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
addr: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||||
valid: true,
|
encoded: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||||
canDecode: true,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKeyHash(
|
result: btcutil.TstAddressPubKeyHash(
|
||||||
[ripemd160.Size]byte{
|
[ripemd160.Size]byte{
|
||||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||||
|
@ -85,10 +86,10 @@ func TestAddresses(t *testing.T) {
|
||||||
|
|
||||||
// Negative P2PKH tests.
|
// Negative P2PKH tests.
|
||||||
{
|
{
|
||||||
name: "p2pkh wrong byte identifier/net",
|
name: "p2pkh wrong byte identifier/net",
|
||||||
addr: "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
addr: "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||||
valid: false,
|
encoded: "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||||
canDecode: true,
|
valid: false,
|
||||||
f: func() (btcutil.Address, error) {
|
f: func() (btcutil.Address, error) {
|
||||||
pkHash := []byte{
|
pkHash := []byte{
|
||||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||||
|
@ -97,10 +98,9 @@ func TestAddresses(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "p2pkh wrong hash length",
|
name: "p2pkh wrong hash length",
|
||||||
addr: "",
|
addr: "",
|
||||||
valid: false,
|
valid: false,
|
||||||
canDecode: true,
|
|
||||||
f: func() (btcutil.Address, error) {
|
f: func() (btcutil.Address, error) {
|
||||||
pkHash := []byte{
|
pkHash := []byte{
|
||||||
0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b,
|
0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b,
|
||||||
|
@ -110,10 +110,9 @@ func TestAddresses(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "p2pkh bad checksum",
|
name: "p2pkh bad checksum",
|
||||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY",
|
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY",
|
||||||
valid: false,
|
valid: false,
|
||||||
canDecode: true,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Positive P2SH tests.
|
// Positive P2SH tests.
|
||||||
|
@ -121,10 +120,10 @@ func TestAddresses(t *testing.T) {
|
||||||
// Taken from transactions:
|
// Taken from transactions:
|
||||||
// output: 3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac
|
// output: 3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac
|
||||||
// input: 837dea37ddc8b1e3ce646f1a656e79bbd8cc7f558ac56a169626d649ebe2a3ba.
|
// input: 837dea37ddc8b1e3ce646f1a656e79bbd8cc7f558ac56a169626d649ebe2a3ba.
|
||||||
name: "mainnet p2sh",
|
name: "mainnet p2sh",
|
||||||
addr: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
|
addr: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
|
||||||
valid: true,
|
encoded: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
|
||||||
canDecode: true,
|
valid: true,
|
||||||
result: btcutil.TstAddressScriptHash(
|
result: btcutil.TstAddressScriptHash(
|
||||||
[ripemd160.Size]byte{
|
[ripemd160.Size]byte{
|
||||||
0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2,
|
0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2,
|
||||||
|
@ -161,10 +160,10 @@ func TestAddresses(t *testing.T) {
|
||||||
// Taken from transactions:
|
// Taken from transactions:
|
||||||
// output: b0539a45de13b3e0403909b8bd1a555b8cbe45fd4e3f3fda76f3a5f52835c29d
|
// output: b0539a45de13b3e0403909b8bd1a555b8cbe45fd4e3f3fda76f3a5f52835c29d
|
||||||
// input: (not yet redeemed at time test was written)
|
// input: (not yet redeemed at time test was written)
|
||||||
name: "mainnet p2sh 2",
|
name: "mainnet p2sh 2",
|
||||||
addr: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8",
|
addr: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8",
|
||||||
valid: true,
|
encoded: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8",
|
||||||
canDecode: true,
|
valid: true,
|
||||||
result: btcutil.TstAddressScriptHash(
|
result: btcutil.TstAddressScriptHash(
|
||||||
[ripemd160.Size]byte{
|
[ripemd160.Size]byte{
|
||||||
0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37,
|
0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37,
|
||||||
|
@ -180,10 +179,10 @@ func TestAddresses(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Taken from bitcoind base58_keys_valid.
|
// Taken from bitcoind base58_keys_valid.
|
||||||
name: "testnet p2sh",
|
name: "testnet p2sh",
|
||||||
addr: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
addr: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||||
valid: true,
|
encoded: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||||
canDecode: true,
|
valid: true,
|
||||||
result: btcutil.TstAddressScriptHash(
|
result: btcutil.TstAddressScriptHash(
|
||||||
[ripemd160.Size]byte{
|
[ripemd160.Size]byte{
|
||||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||||
|
@ -200,10 +199,9 @@ func TestAddresses(t *testing.T) {
|
||||||
|
|
||||||
// Negative P2SH tests.
|
// Negative P2SH tests.
|
||||||
{
|
{
|
||||||
name: "p2sh wrong hash length",
|
name: "p2sh wrong hash length",
|
||||||
addr: "",
|
addr: "",
|
||||||
valid: false,
|
valid: false,
|
||||||
canDecode: true,
|
|
||||||
f: func() (btcutil.Address, error) {
|
f: func() (btcutil.Address, error) {
|
||||||
hash := []byte{
|
hash := []byte{
|
||||||
0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9,
|
0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9,
|
||||||
|
@ -213,10 +211,9 @@ func TestAddresses(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "p2sh wrong byte identifier/net",
|
name: "p2sh wrong byte identifier/net",
|
||||||
addr: "0NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
addr: "0NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||||
valid: false,
|
valid: false,
|
||||||
canDecode: true,
|
|
||||||
f: func() (btcutil.Address, error) {
|
f: func() (btcutil.Address, error) {
|
||||||
hash := []byte{
|
hash := []byte{
|
||||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||||
|
@ -227,10 +224,10 @@ func TestAddresses(t *testing.T) {
|
||||||
|
|
||||||
// Positive P2PK tests.
|
// Positive P2PK tests.
|
||||||
{
|
{
|
||||||
name: "mainnet p2pk compressed (0x02)",
|
name: "mainnet p2pk compressed (0x02)",
|
||||||
addr: "13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg",
|
addr: "02192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4",
|
||||||
valid: true,
|
encoded: "13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg",
|
||||||
canDecode: false,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
||||||
|
@ -249,10 +246,10 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mainnet p2pk compressed (0x03)",
|
name: "mainnet p2pk compressed (0x03)",
|
||||||
addr: "15sHANNUBSh6nDp8XkDPmQcW6n3EFwmvE6",
|
addr: "03b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65",
|
||||||
valid: true,
|
encoded: "15sHANNUBSh6nDp8XkDPmQcW6n3EFwmvE6",
|
||||||
canDecode: false,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
||||||
|
@ -271,10 +268,11 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mainnet p2pk uncompressed (0x04)",
|
name: "mainnet p2pk uncompressed (0x04)",
|
||||||
addr: "12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S",
|
addr: "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2" +
|
||||||
valid: true,
|
"e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3",
|
||||||
canDecode: false,
|
encoded: "12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S",
|
||||||
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b,
|
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b,
|
||||||
|
@ -299,10 +297,11 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mainnet p2pk hybrid (0x06)",
|
name: "mainnet p2pk hybrid (0x06)",
|
||||||
addr: "1Ja5rs7XBZnK88EuLVcFqYGMEbBitzchmX",
|
addr: "06192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4" +
|
||||||
valid: true,
|
"0d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453e",
|
||||||
canDecode: false,
|
encoded: "1Ja5rs7XBZnK88EuLVcFqYGMEbBitzchmX",
|
||||||
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
||||||
|
@ -327,10 +326,11 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mainnet p2pk hybrid (0x07)",
|
name: "mainnet p2pk hybrid (0x07)",
|
||||||
addr: "1ExqMmf6yMxcBMzHjbj41wbqYuqoX6uBLG",
|
addr: "07b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65" +
|
||||||
valid: true,
|
"37a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b",
|
||||||
canDecode: false,
|
encoded: "1ExqMmf6yMxcBMzHjbj41wbqYuqoX6uBLG",
|
||||||
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
||||||
|
@ -355,10 +355,10 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.MainNet,
|
net: btcwire.MainNet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "testnet p2pk compressed (0x02)",
|
name: "testnet p2pk compressed (0x02)",
|
||||||
addr: "mhiDPVP2nJunaAgTjzWSHCYfAqxxrxzjmo",
|
addr: "02192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4",
|
||||||
valid: true,
|
encoded: "mhiDPVP2nJunaAgTjzWSHCYfAqxxrxzjmo",
|
||||||
canDecode: false,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
||||||
|
@ -377,10 +377,10 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.TestNet3,
|
net: btcwire.TestNet3,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "testnet p2pk compressed (0x03)",
|
name: "testnet p2pk compressed (0x03)",
|
||||||
addr: "mkPETRTSzU8MZLHkFKBmbKppxmdw9qT42t",
|
addr: "03b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65",
|
||||||
valid: true,
|
encoded: "mkPETRTSzU8MZLHkFKBmbKppxmdw9qT42t",
|
||||||
canDecode: false,
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
||||||
|
@ -399,10 +399,11 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.TestNet3,
|
net: btcwire.TestNet3,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "testnet p2pk uncompressed (0x04)",
|
name: "testnet p2pk uncompressed (0x04)",
|
||||||
addr: "mh8YhPYEAYs3E7EVyKtB5xrcfMExkkdEMF",
|
addr: "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5" +
|
||||||
valid: true,
|
"cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3",
|
||||||
canDecode: false,
|
encoded: "mh8YhPYEAYs3E7EVyKtB5xrcfMExkkdEMF",
|
||||||
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b,
|
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b,
|
||||||
|
@ -427,10 +428,11 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.TestNet3,
|
net: btcwire.TestNet3,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "testnet p2pk hybrid (0x06)",
|
name: "testnet p2pk hybrid (0x06)",
|
||||||
addr: "my639vCVzbDZuEiX44adfTUg6anRomZLEP",
|
addr: "06192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b" +
|
||||||
valid: true,
|
"40d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453e",
|
||||||
canDecode: false,
|
encoded: "my639vCVzbDZuEiX44adfTUg6anRomZLEP",
|
||||||
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
|
||||||
|
@ -455,10 +457,11 @@ func TestAddresses(t *testing.T) {
|
||||||
net: btcwire.TestNet3,
|
net: btcwire.TestNet3,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "testnet p2pk hybrid (0x07)",
|
name: "testnet p2pk hybrid (0x07)",
|
||||||
addr: "muUnepk5nPPrxUTuTAhRqrpAQuSWS5fVii",
|
addr: "07b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6" +
|
||||||
valid: true,
|
"537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b",
|
||||||
canDecode: false,
|
encoded: "muUnepk5nPPrxUTuTAhRqrpAQuSWS5fVii",
|
||||||
|
valid: true,
|
||||||
result: btcutil.TstAddressPubKey(
|
result: btcutil.TstAddressPubKey(
|
||||||
[]byte{
|
[]byte{
|
||||||
0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1,
|
||||||
|
@ -485,33 +488,29 @@ func TestAddresses(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
var decoded btcutil.Address
|
// Decode addr and compare error against valid.
|
||||||
var err error
|
decoded, err := btcutil.DecodeAddress(test.addr, test.net)
|
||||||
if test.canDecode {
|
if (err == nil) != test.valid {
|
||||||
// Decode addr and compare error against valid.
|
t.Errorf("%v: decoding test failed: %v", test.name, err)
|
||||||
decoded, err = btcutil.DecodeAddress(test.addr)
|
return
|
||||||
if (err == nil) != test.valid {
|
|
||||||
t.Errorf("%v: decoding test failed", test.name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// The address can't be decoded directly, so instead
|
|
||||||
// call the creation function.
|
|
||||||
decoded, err = test.f()
|
|
||||||
if (err == nil) != test.valid {
|
|
||||||
t.Errorf("%v: creation test failed", test.name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If decoding succeeded, encode again and compare against the original.
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
encoded := decoded.EncodeAddress()
|
// Ensure the stringer returns the same address as the
|
||||||
|
// original.
|
||||||
|
if decodedStringer, ok := decoded.(fmt.Stringer); ok {
|
||||||
|
if test.addr != decodedStringer.String() {
|
||||||
|
t.Errorf("%v: String on decoded value does not match expected value: %v != %v",
|
||||||
|
test.name, test.addr, decodedStringer.String())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Compare encoded addr against the original encoding.
|
// Encode again and compare against the original.
|
||||||
if test.addr != encoded {
|
encoded := decoded.EncodeAddress()
|
||||||
|
if test.encoded != encoded {
|
||||||
t.Errorf("%v: decoding and encoding produced different addressess: %v != %v",
|
t.Errorf("%v: decoding and encoding produced different addressess: %v != %v",
|
||||||
test.name, test.addr, encoded)
|
test.name, test.encoded, encoded)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -537,8 +536,7 @@ func TestAddresses(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check networks. This check always succeeds for non-P2PKH and
|
// Ensure the address is for the expected network.
|
||||||
// non-P2SH addresses as both nets will be Go's default zero value.
|
|
||||||
if !decoded.IsForNet(test.net) {
|
if !decoded.IsForNet(test.net) {
|
||||||
t.Errorf("%v: calculated network does not match expected",
|
t.Errorf("%v: calculated network does not match expected",
|
||||||
test.name)
|
test.name)
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
|
|
||||||
github.com/conformal/btcutil/base58.go Base58Decode 100.00% (20/20)
|
github.com/conformal/btcutil/base58.go Base58Decode 100.00% (20/20)
|
||||||
github.com/conformal/btcutil/address.go DecodeAddr 100.00% (18/18)
|
|
||||||
github.com/conformal/btcutil/base58.go Base58Encode 100.00% (15/15)
|
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/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.Tx 100.00% (12/12)
|
||||||
|
@ -9,50 +8,52 @@ github.com/conformal/btcutil/block.go Block.TxShas 100.00% (10/10)
|
||||||
github.com/conformal/btcutil/address.go encodeAddress 100.00% (9/9)
|
github.com/conformal/btcutil/address.go encodeAddress 100.00% (9/9)
|
||||||
github.com/conformal/btcutil/addrconvs.go EncodeAddress 100.00% (8/8)
|
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/addrconvs.go EncodeScriptHash 100.00% (8/8)
|
||||||
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/addrconvs.go encodeHashWithNetId 100.00% (7/7)
|
||||||
github.com/conformal/btcutil/address.go NewAddressPubKeyHash 100.00% (7/7)
|
|
||||||
github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (7/7)
|
github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (7/7)
|
||||||
github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (7/7)
|
github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (7/7)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.EncodeAddress 100.00% (5/5)
|
|
||||||
github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5)
|
github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5)
|
||||||
github.com/conformal/btcutil/address.go AddressScriptHash.EncodeAddress 100.00% (5/5)
|
|
||||||
github.com/conformal/btcutil/tx.go Tx.Sha 100.00% (5/5)
|
github.com/conformal/btcutil/tx.go Tx.Sha 100.00% (5/5)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKey.EncodeAddress 100.00% (5/5)
|
|
||||||
github.com/conformal/btcutil/block.go Block.TxSha 100.00% (4/4)
|
github.com/conformal/btcutil/block.go Block.TxSha 100.00% (4/4)
|
||||||
github.com/conformal/btcutil/address.go checkBitcoinNet 100.00% (3/3)
|
|
||||||
github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2)
|
github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2)
|
||||||
github.com/conformal/btcutil/address.go NewAddressScriptHash 100.00% (2/2)
|
github.com/conformal/btcutil/address.go NewAddressScriptHash 100.00% (2/2)
|
||||||
github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1)
|
github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.ScriptAddress 100.00% (1/1)
|
github.com/conformal/btcutil/tx.go NewTx 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.Net 100.00% (1/1)
|
github.com/conformal/btcutil/address.go AddressScriptHash.String 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/address.go AddressScriptHash.ScriptAddress 100.00% (1/1)
|
github.com/conformal/btcutil/tx.go Tx.SetIndex 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/address.go AddressPubKey.ScriptAddress 100.00% (1/1)
|
|
||||||
github.com/conformal/btcutil/address.go AddressPubKey.Net 100.00% (1/1)
|
|
||||||
github.com/conformal/btcutil/address.go AddressPubKey.String 100.00% (1/1)
|
|
||||||
github.com/conformal/btcutil/block.go Block.MsgBlock 100.00% (1/1)
|
github.com/conformal/btcutil/block.go Block.MsgBlock 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/address.go AddressScriptHash.ScriptAddress 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/address.go AddressPubKeyHash.ScriptAddress 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/address.go AddressPubKeyHash.EncodeAddress 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/block.go Block.Height 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/block.go Block.SetHeight 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1)
|
github.com/conformal/btcutil/address.go AddressPubKey.EncodeAddress 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1)
|
github.com/conformal/btcutil/address.go AddressPubKey.ScriptAddress 100.00% (1/1)
|
||||||
github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1)
|
|
||||||
github.com/conformal/btcutil/tx.go Tx.Index 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/address.go AddressPubKey.String 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/address.go AddressScriptHash.EncodeAddress 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/address.go AddressPubKeyHash.String 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/block.go OutOfRangeError.Error 100.00% (1/1)
|
||||||
|
github.com/conformal/btcutil/address.go DecodeAddr 95.65% (22/23)
|
||||||
github.com/conformal/btcutil/appdata.go appDataDir 92.00% (23/25)
|
github.com/conformal/btcutil/appdata.go appDataDir 92.00% (23/25)
|
||||||
github.com/conformal/btcutil/address.go NewAddressPubKey 91.67% (11/12)
|
github.com/conformal/btcutil/address.go NewAddressPubKeyHash 91.67% (11/12)
|
||||||
|
github.com/conformal/btcutil/address.go NewAddressScriptHashFromHash 91.67% (11/12)
|
||||||
github.com/conformal/btcutil/addrconvs.go EncodePrivateKey 90.91% (20/22)
|
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/block.go Block.Bytes 88.89% (8/9)
|
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/address.go AddressPubKey.serialize 85.71% (6/7)
|
github.com/conformal/btcutil/address.go AddressPubKey.serialize 85.71% (6/7)
|
||||||
|
github.com/conformal/btcutil/address.go NewAddressPubKey 83.33% (15/18)
|
||||||
|
github.com/conformal/btcutil/address.go checkBitcoinNet 83.33% (5/6)
|
||||||
github.com/conformal/btcutil/addrconvs.go DecodePrivateKey 82.61% (19/23)
|
github.com/conformal/btcutil/addrconvs.go DecodePrivateKey 82.61% (19/23)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKey.AddressPubKeyHash 0.00% (0/2)
|
github.com/conformal/btcutil/address.go AddressPubKeyHash.IsForNet 60.00% (3/5)
|
||||||
|
github.com/conformal/btcutil/address.go AddressPubKey.IsForNet 60.00% (3/5)
|
||||||
|
github.com/conformal/btcutil/address.go AddressScriptHash.IsForNet 60.00% (3/5)
|
||||||
|
github.com/conformal/btcutil/certgen.go NewTLSCertPair 0.00% (0/50)
|
||||||
|
github.com/conformal/btcutil/address.go AddressPubKey.AddressPubKeyHash 0.00% (0/3)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKey.Format 0.00% (0/1)
|
github.com/conformal/btcutil/address.go AddressPubKey.Format 0.00% (0/1)
|
||||||
github.com/conformal/btcutil/address.go AddressPubKey.SetFormat 0.00% (0/1)
|
github.com/conformal/btcutil/address.go AddressPubKey.SetFormat 0.00% (0/1)
|
||||||
github.com/conformal/btcutil/address.go AddressScriptHash.String 0.00% (0/1)
|
|
||||||
github.com/conformal/btcutil/address.go AddressPubKeyHash.String 0.00% (0/1)
|
|
||||||
github.com/conformal/btcutil/appdata.go AppDataDir 0.00% (0/1)
|
github.com/conformal/btcutil/appdata.go AppDataDir 0.00% (0/1)
|
||||||
github.com/conformal/btcutil ------------------------------- 94.21% (309/328)
|
github.com/conformal/btcutil ------------------------------- 80.15% (323/403)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue