Remove deprecated address functions.
This commit removes the deprecated address functions which have been replaced by the Address interface and concrete implementations.
This commit is contained in:
parent
e0ce788881
commit
d7ea478de2
2 changed files with 0 additions and 225 deletions
98
addrconvs.go
98
addrconvs.go
|
@ -6,7 +6,6 @@ package btcutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"code.google.com/p/go.crypto/ripemd160"
|
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
)
|
)
|
||||||
|
@ -47,103 +46,6 @@ const (
|
||||||
TestNetScriptHash = 0xc4
|
TestNetScriptHash = 0xc4
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
var netID byte
|
|
||||||
switch net {
|
|
||||||
case btcwire.MainNet:
|
|
||||||
netID = MainNetAddr
|
|
||||||
case btcwire.TestNet3:
|
|
||||||
netID = TestNetAddr
|
|
||||||
default:
|
|
||||||
return "", ErrUnknownNet
|
|
||||||
}
|
|
||||||
|
|
||||||
return encodeHashWithNetId(netID, addrHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
var netID byte
|
|
||||||
switch net {
|
|
||||||
case btcwire.MainNet:
|
|
||||||
netID = MainNetScriptHash
|
|
||||||
case btcwire.TestNet3:
|
|
||||||
netID = TestNetScriptHash
|
|
||||||
default:
|
|
||||||
return "", ErrUnknownNet
|
|
||||||
}
|
|
||||||
|
|
||||||
return encodeHashWithNetId(netID, addrHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeHashWithNetId(netID byte, addrHash []byte) (encoded string, err error) {
|
|
||||||
tosum := append([]byte{netID}, addrHash...)
|
|
||||||
cksum := btcwire.DoubleSha256(tosum)
|
|
||||||
|
|
||||||
// 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 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)
|
|
||||||
|
|
||||||
// Length of decoded address must be 20 bytes + 1 byte for a network
|
|
||||||
// identifier byte + 4 bytes of checksum.
|
|
||||||
if len(decoded) != ripemd160.Size+5 {
|
|
||||||
return nil, 0x00, ErrMalformedAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
switch decoded[0] {
|
|
||||||
case MainNetAddr:
|
|
||||||
net = btcwire.MainNet
|
|
||||||
case TestNetAddr:
|
|
||||||
net = btcwire.TestNet3
|
|
||||||
default:
|
|
||||||
return nil, 0, ErrUnknownNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 := decoded[:ripemd160.Size+1]
|
|
||||||
cksum := btcwire.DoubleSha256(tosum)[:4]
|
|
||||||
if !bytes.Equal(cksum, decoded[len(decoded)-4:]) {
|
|
||||||
return nil, net, ErrMalformedAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
addrHash = make([]byte, ripemd160.Size, ripemd160.Size)
|
|
||||||
copy(addrHash, decoded[1:ripemd160.Size+1])
|
|
||||||
|
|
||||||
return addrHash, net, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// EncodePrivateKey takes a 32-byte private key and encodes it into the
|
// EncodePrivateKey takes a 32-byte private key and encodes it into the
|
||||||
// Wallet Import Format (WIF).
|
// Wallet Import Format (WIF).
|
||||||
func EncodePrivateKey(privKey []byte, net btcwire.BitcoinNet, compressed bool) (string, error) {
|
func EncodePrivateKey(privKey []byte, net btcwire.BitcoinNet, compressed bool) (string, error) {
|
||||||
|
|
|
@ -31,87 +31,6 @@ var encodePrivateKeyTests = []struct {
|
||||||
}, btcwire.TestNet3, true, "cV1Y7ARUr9Yx7BR55nTdnR7ZXNJphZtCCMBTEZBJe1hXt2kB684q"},
|
}, 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) {
|
func TestEncodeDecodePrivateKey(t *testing.T) {
|
||||||
for x, test := range encodePrivateKeyTests {
|
for x, test := range encodePrivateKeyTests {
|
||||||
wif, err := btcutil.EncodePrivateKey(test.in, test.net, test.compressed)
|
wif, err := btcutil.EncodePrivateKey(test.in, test.net, test.compressed)
|
||||||
|
@ -137,49 +56,3 @@ func TestEncodeDecodePrivateKey(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue