Do not import privkeys resulting in duplicate addresses.

Fixes #35.
This commit is contained in:
Josh Rickmar 2014-01-15 14:07:08 -05:00
parent 30aff3a468
commit 7866fd8931
2 changed files with 17 additions and 0 deletions

View file

@ -462,6 +462,10 @@ func ImportPrivKey(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
// If the import was successful, reply with nil.
return nil, nil
case wallet.ErrDuplicate:
// Do not return duplicate key errors to the client.
return nil, nil
case wallet.ErrWalletLocked:
return nil, &btcjson.ErrWalletUnlockNeeded

View file

@ -60,6 +60,7 @@ const (
var (
ErrAddressNotFound = errors.New("address not found")
ErrChecksumMismatch = errors.New("checksum mismatch")
ErrDuplicate = errors.New("duplicate key or address")
ErrMalformedEntry = errors.New("malformed entry")
ErrNetworkMismatch = errors.New("network mismatch")
ErrWalletDoesNotExist = errors.New("non-existant wallet")
@ -1071,6 +1072,18 @@ func (w *Wallet) SetBetterEarliestBlockHeight(height int32) {
// user-provided private key and adds it to the wallet. If the
// import is successful, the payment address string is returned.
func (w *Wallet) ImportPrivateKey(privkey []byte, compressed bool, bs *BlockStamp) (string, error) {
// First, must check that the key being imported will not result
// in a duplicate address.
pkh := btcutil.Hash160(pubkeyFromPrivkey(privkey, compressed))
// Will always be valid inputs so omit error check.
apkh, err := btcutil.NewAddressPubKeyHash(pkh, w.Net())
if err != nil {
return "", err
}
if _, ok := w.addrMap[*apkh]; ok {
return "", ErrDuplicate
}
// The wallet's secret will be zeroed on lock, so make a local copy.
w.secret.Lock()
if len(w.secret.key) != 32 {