Pad byte slice passed to btcutil.EncodePrivateKey.

Calling the Bytes method for a big.Int does not pad the result to
required size for EncodePrivateKey.  This change adds the leading
padding, preventing seemingly-random "malformed private key" errors
from being returned to users of dumpprivkey.
This commit is contained in:
Josh Rickmar 2014-03-21 12:47:10 -05:00
parent 19fd6406e8
commit 6024e0ecb6

View file

@ -302,6 +302,17 @@ func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) {
return txInfoList, nil return txInfoList, nil
} }
func pad(size int, b []byte) []byte {
// Prevent a possible panic if the input exceeds the expected size.
if len(b) > size {
size = len(b)
}
p := make([]byte, size)
copy(p[size-len(b):], b)
return p
}
// DumpPrivKeys returns the WIF-encoded private keys for all addresses with // DumpPrivKeys returns the WIF-encoded private keys for all addresses with
// private keys in a wallet. // private keys in a wallet.
func (a *Account) DumpPrivKeys() ([]string, error) { func (a *Account) DumpPrivKeys() ([]string, error) {
@ -318,7 +329,7 @@ func (a *Account) DumpPrivKeys() ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(), encKey, err := btcutil.EncodePrivateKey(pad(32, key.D.Bytes()),
a.Wallet.Net(), info.Compressed()) a.Wallet.Net(), info.Compressed())
if err != nil { if err != nil {
return nil, err return nil, err
@ -346,7 +357,7 @@ func (a *Account) DumpWIFPrivateKey(addr btcutil.Address) (string, error) {
} }
// Return WIF-encoding of the private key. // Return WIF-encoding of the private key.
return btcutil.EncodePrivateKey(key.D.Bytes(), a.Net(), return btcutil.EncodePrivateKey(pad(32, key.D.Bytes()), a.Net(),
info.Compressed()) info.Compressed())
} }