Fixed comments and added tests for scriptHash encoding
Fixed bad comments on new scriptHash constants and encoding function. Added encodeScriptHashTests to addrconvs_test.go to check correctness of output.
This commit is contained in:
parent
592d38d55f
commit
13fec7735c
2 changed files with 50 additions and 4 deletions
|
@ -40,11 +40,11 @@ const (
|
|||
// TestNetKey is the key identifier for TestNet
|
||||
TestNetKey = 0xef
|
||||
|
||||
// MainNetScriptHash is the address identifier for MainNet
|
||||
// MainNetScriptHash is the script hash identifier for MainNet
|
||||
MainNetScriptHash = 0x05
|
||||
|
||||
// TestNetScriptHash is the address identifier for TestNet
|
||||
TestNetScriptHash = 0xC4
|
||||
// TestNetScriptHash is the script hash identifier for TestNet
|
||||
TestNetScriptHash = 0xc4
|
||||
)
|
||||
|
||||
// EncodeAddress takes a 20-byte raw payment address (hash160 of a pubkey)
|
||||
|
@ -67,7 +67,7 @@ func EncodeAddress(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err
|
|||
return encodeHashWithNetId(netID, addrHash)
|
||||
}
|
||||
|
||||
// EncodeScriptHash takes a 20-byte raw script hash (hash160 of a pubkey)
|
||||
// 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.
|
||||
func EncodeScriptHash(addrHash []byte, net btcwire.BitcoinNet) (encoded string, err error) {
|
||||
if len(addrHash) != ripemd160.Size {
|
||||
|
|
|
@ -137,3 +137,49 @@ 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…
Add table
Reference in a new issue