Skip to content

Commit

Permalink
lnwallet: remove need for lnwallet to have access to the private key
Browse files Browse the repository at this point in the history
This paves the way for lnd to work with hardware wallets, in which case it will not have access to the private key.
  • Loading branch information
CirroStorm authored and Roasbeef committed Sep 28, 2018
1 parent 6afee3d commit f594a57
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
12 changes: 4 additions & 8 deletions lnwallet/btcwallet/btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"
"time"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
Expand Down Expand Up @@ -255,15 +254,12 @@ func (b *BtcWallet) NewAddress(t lnwallet.AddressType, change bool) (btcutil.Add
return b.wallet.NewAddress(defaultAccount, keyScope)
}

// GetPrivKey retrieves the underlying private key associated with the passed
// address. If the we're unable to locate the proper private key, then a
// non-nil error will be returned.
// IsOurAddress checks if the passed address belongs to this wallet
//
// This is a part of the WalletController interface.
func (b *BtcWallet) GetPrivKey(a btcutil.Address) (*btcec.PrivateKey, error) {
// Using the ID address, request the private key corresponding to the
// address from the wallet's address manager.
return b.wallet.PrivKeyForAddress(a)
func (b *BtcWallet) IsOurAddress(a btcutil.Address) bool {
result, err := b.wallet.HaveAddress(a)
return result && (err == nil)
}

// SendOutputs funds, signs, and broadcasts a Bitcoin transaction paying out to
Expand Down
7 changes: 2 additions & 5 deletions lnwallet/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,8 @@ type WalletController interface {
// p2wsh, etc.
NewAddress(addrType AddressType, change bool) (btcutil.Address, error)

// GetPrivKey retrieves the underlying private key associated with the
// passed address. If the wallet is unable to locate this private key
// due to the address not being under control of the wallet, then an
// error should be returned.
GetPrivKey(a btcutil.Address) (*btcec.PrivateKey, error)
// IsOurAddress checks if the passed address belongs to this wallet
IsOurAddress(a btcutil.Address) bool

// SendOutputs funds, signs, and broadcasts a Bitcoin transaction paying
// out to the specified outputs. In the case the wallet has insufficient
Expand Down
4 changes: 2 additions & 2 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func (m *mockWalletController) NewAddress(addrType lnwallet.AddressType,
m.rootKey.PubKey().SerializeCompressed(), &chaincfg.MainNetParams)
return addr, nil
}
func (*mockWalletController) GetPrivKey(a btcutil.Address) (*btcec.PrivateKey, error) {
return nil, nil
func (*mockWalletController) IsOurAddress(a btcutil.Address) bool {
return false
}

func (*mockWalletController) SendOutputs(outputs []*wire.TxOut,
Expand Down
5 changes: 1 addition & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl,
s.htlcSwitch.RemoveLink(chanID)
return nil
},
IsOurAddress: func(addr btcutil.Address) bool {
_, err := cc.wallet.GetPrivKey(addr)
return err == nil
},
IsOurAddress: cc.wallet.IsOurAddress,
ContractBreach: func(chanPoint wire.OutPoint,
breachRet *lnwallet.BreachRetribution) error {
event := &ContractBreachEvent{
Expand Down

0 comments on commit f594a57

Please sign in to comment.