func TestEncodeDecodePrivateKey(t *testing.T) { for x, test := range encodePrivateKeyTests { wif, err := btcutil.EncodePrivateKey(test.in, test.net, test.compressed) if err != nil { t.Errorf("%x: %v", x, err) continue } if wif != test.out { t.Errorf("TestEncodeDecodePrivateKey failed: want '%s', got '%s'", test.out, wif) continue } key, _, compressed, err := btcutil.DecodePrivateKey(test.out) if err != nil { t.Error(err) continue } if !bytes.Equal(key, test.in) || compressed != test.compressed { t.Errorf("TestEncodeDecodePrivateKey failed: want '%x', got '%x'", test.out, key) } } }
// DumpWIFPrivateKey returns the WIF encoded private key for a // single wallet address. func (a *Account) DumpWIFPrivateKey(addr btcutil.Address) (string, error) { // Get private key from wallet if it exists. key, err := a.Wallet.AddressKey(addr) if err != nil { return "", err } // Get address info. This is needed to determine whether // the pubkey is compressed or not. info, err := a.Wallet.AddressInfo(addr) if err != nil { return "", err } // Return WIF-encoding of the private key. return btcutil.EncodePrivateKey(key.D.Bytes(), a.Net(), info.Compressed) }
// DumpPrivKeys returns the WIF-encoded private keys for all addresses with // private keys in a wallet. func (a *Account) DumpPrivKeys() ([]string, error) { // Iterate over each active address, appending the private // key to privkeys. var privkeys []string for addr, info := range a.Wallet.ActiveAddresses() { key, err := a.Wallet.AddressKey(addr) if err != nil { return nil, err } encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(), a.Wallet.Net(), info.Compressed) if err != nil { return nil, err } privkeys = append(privkeys, encKey) } return privkeys, nil }