From 6024e0ecb68d1dc479a249694359324f071fc8d6 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 21 Mar 2014 12:47:10 -0500 Subject: [PATCH] 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. --- account.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/account.go b/account.go index 0cc904a..017a1b4 100644 --- a/account.go +++ b/account.go @@ -302,6 +302,17 @@ func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) { 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 // private keys in a wallet. func (a *Account) DumpPrivKeys() ([]string, error) { @@ -318,7 +329,7 @@ func (a *Account) DumpPrivKeys() ([]string, error) { if err != nil { return nil, err } - encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(), + encKey, err := btcutil.EncodePrivateKey(pad(32, key.D.Bytes()), a.Wallet.Net(), info.Compressed()) if err != nil { return nil, err @@ -346,7 +357,7 @@ func (a *Account) DumpWIFPrivateKey(addr btcutil.Address) (string, error) { } // 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()) }