lnwallet/btcwallet: update WalletController imp to latest btcwallet API

This commit is contained in:
Olaoluwa Osuntokun
2017-04-23 19:19:17 -07:00
parent 844cdba513
commit c41d673c7b
4 changed files with 51 additions and 51 deletions

2
lnd.go
View File

@@ -131,7 +131,7 @@ func lndMain() error {
CACert: rpcCert, CACert: rpcCert,
NetParams: activeNetParams.Params, NetParams: activeNetParams.Params,
} }
wc, err := btcwallet.New(walletConfig) wc, err := btcwallet.New(*walletConfig)
if err != nil { if err != nil {
fmt.Printf("unable to create wallet controller: %v\n", err) fmt.Printf("unable to create wallet controller: %v\n", err)
return err return err

View File

@@ -26,8 +26,9 @@ const (
) )
var ( var (
lnNamespace = []byte("ln") lnNamespace = []byte("ln")
rootKey = []byte("ln-root") rootKey = []byte("ln-root")
waddrmgrNamespaceKey = []byte("waddrmgr")
) )
// BtcWallet is an implementation of the lnwallet.WalletController interface // BtcWallet is an implementation of the lnwallet.WalletController interface
@@ -41,10 +42,9 @@ type BtcWallet struct {
// rpc is an an active RPC connection to btcd full-node. // rpc is an an active RPC connection to btcd full-node.
rpc *chain.RPCClient rpc *chain.RPCClient
// lnNamespace is a namespace within btcwallet's walletdb used to store db walletdb.DB
// persistent state required by the WalletController interface but not
// natively supported by btcwallet. cfg *Config
lnNamespace walletdb.Namespace
netParams *chaincfg.Params netParams *chaincfg.Params
@@ -60,7 +60,7 @@ var _ lnwallet.WalletController = (*BtcWallet)(nil)
// New returns a new fully initialized instance of BtcWallet given a valid // New returns a new fully initialized instance of BtcWallet given a valid
// configuration struct. // configuration struct.
func New(cfg *Config) (*BtcWallet, error) { func New(cfg Config) (*BtcWallet, error) {
// Ensure the wallet exists or create it when the create flag is set. // Ensure the wallet exists or create it when the create flag is set.
netDir := networkDir(cfg.DataDir, cfg.NetParams) netDir := networkDir(cfg.DataDir, cfg.NetParams)
@@ -102,18 +102,28 @@ func New(cfg *Config) (*BtcWallet, error) {
return nil, err return nil, err
} }
// Create a bucket within the wallet's database dedicated to storing
// our LN specific data.
db := wallet.Database() db := wallet.Database()
walletNamespace, err := db.Namespace(lnNamespace) err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
_, err := tx.CreateTopLevelBucket(lnNamespace)
if err != nil && err != walletdb.ErrBucketExists {
return err
}
return nil
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &BtcWallet{ return &BtcWallet{
wallet: wallet, cfg: &cfg,
rpc: rpcc, wallet: wallet,
lnNamespace: walletNamespace, db: db,
netParams: cfg.NetParams, rpc: rpcc,
utxoCache: make(map[wire.OutPoint]*wire.TxOut), netParams: cfg.NetParams,
utxoCache: make(map[wire.OutPoint]*wire.TxOut),
}, nil }, nil
} }
@@ -219,14 +229,9 @@ func (b *BtcWallet) NewAddress(t lnwallet.AddressType, change bool) (btcutil.Add
// //
// This is a part of the WalletController interface. // This is a part of the WalletController interface.
func (b *BtcWallet) GetPrivKey(a btcutil.Address) (*btcec.PrivateKey, error) { func (b *BtcWallet) GetPrivKey(a btcutil.Address) (*btcec.PrivateKey, error) {
// Using the ID address, request the private key coresponding to the // Using the ID address, request the private key corresponding to the
// address from the wallet's address manager. // address from the wallet's address manager.
walletAddr, err := b.wallet.Manager.Address(a) return b.wallet.PrivKeyForAddress(a)
if err != nil {
return nil, err
}
return walletAddr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
} }
// NewRawKey retrieves the next key within our HD key-chain for use within as a // NewRawKey retrieves the next key within our HD key-chain for use within as a
@@ -241,12 +246,7 @@ func (b *BtcWallet) NewRawKey() (*btcec.PublicKey, error) {
return nil, err return nil, err
} }
pkAddr, err := b.wallet.Manager.Address(addr) return b.wallet.PubKeyForAddress(addr)
if err != nil {
return nil, err
}
return pkAddr.(waddrmgr.ManagedPubKeyAddress).PubKey(), nil
} }
// FetchRootKey returns a root key which is intended to be used as an initial // FetchRootKey returns a root key which is intended to be used as an initial
@@ -258,10 +258,10 @@ func (b *BtcWallet) FetchRootKey() (*btcec.PrivateKey, error) {
// locally within the database, then used to obtain the key from the // locally within the database, then used to obtain the key from the
// wallet based on the address hash. // wallet based on the address hash.
var rootAddrHash []byte var rootAddrHash []byte
if err := b.lnNamespace.Update(func(tx walletdb.Tx) error { if err := walletdb.View(b.db, func(tx walletdb.ReadTx) error {
rootBucket := tx.RootBucket() lnBucket := tx.ReadBucket(lnNamespace)
rootAddrHash = rootBucket.Get(rootKey) rootAddrHash = lnBucket.Get(rootKey)
return nil return nil
}); err != nil { }); err != nil {
return nil, err return nil, err
@@ -271,17 +271,19 @@ func (b *BtcWallet) FetchRootKey() (*btcec.PrivateKey, error) {
// Otherwise, we need to generate a fresh address from the // Otherwise, we need to generate a fresh address from the
// wallet, then stores it's hash160 within the database so we // wallet, then stores it's hash160 within the database so we
// can look up the exact key later. // can look up the exact key later.
rootAddr, err := b.wallet.Manager.NextExternalAddresses(defaultAccount, if err := walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
1, waddrmgr.WitnessPubKey) addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
if err != nil { addrs, err := b.wallet.Manager.NextExternalAddresses(addrmgrNs,
return nil, err defaultAccount, 1, waddrmgr.WitnessPubKey)
} if err != nil {
return err
}
rootAddr := addrs[0].Address()
if err := b.lnNamespace.Update(func(tx walletdb.Tx) error { lnBucket := tx.ReadWriteBucket(lnNamespace)
rootBucket := tx.RootBucket()
rootAddrHash = rootAddr[0].Address().ScriptAddress() rootAddrHash = rootAddr.ScriptAddress()
return rootBucket.Put(rootKey, rootAddrHash) return lnBucket.Put(rootKey, rootAddrHash)
}); err != nil { }); err != nil {
return nil, err return nil, err
} }
@@ -295,12 +297,13 @@ func (b *BtcWallet) FetchRootKey() (*btcec.PrivateKey, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
walletAddr, err := b.wallet.Manager.Address(rootAddr)
priv, err := b.wallet.PrivKeyForAddress(rootAddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return walletAddr.(waddrmgr.ManagedPubKeyAddress).PrivKey() return priv, nil
} }
// SendOutputs funds, signs, and broadcasts a Bitcoin transaction paying out to // SendOutputs funds, signs, and broadcasts a Bitcoin transaction paying out to

View File

@@ -26,7 +26,7 @@ func createNewWallet(args ...interface{}) (lnwallet.WalletController, error) {
"incorrect, expected a *btcrpcclient.ConnConfig") "incorrect, expected a *btcrpcclient.ConnConfig")
} }
return New(config) return New(*config)
} }
// init registers a driver for the BtcWallet concrete implementation of the // init registers a driver for the BtcWallet concrete implementation of the

View File

@@ -11,6 +11,7 @@ import (
"github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil" "github.com/roasbeef/btcutil"
"github.com/roasbeef/btcwallet/waddrmgr" "github.com/roasbeef/btcwallet/waddrmgr"
base "github.com/roasbeef/btcwallet/wallet"
) )
// FetchInputInfo queries for the WalletController's knowledge of the passed // FetchInputInfo queries for the WalletController's knowledge of the passed
@@ -35,7 +36,8 @@ func (b *BtcWallet) FetchInputInfo(prevOut *wire.OutPoint) (*wire.TxOut, error)
b.cacheMtx.RUnlock() b.cacheMtx.RUnlock()
// Otherwse, we manually look up the output within the tx store. // Otherwse, we manually look up the output within the tx store.
txDetail, err := b.wallet.TxStore.TxDetails(&prevOut.Hash) txid := &prevOut.Hash
txDetail, err := base.UnstableAPI(b.wallet).TxDetails(txid)
if err != nil { if err != nil {
return nil, err return nil, err
} else if txDetail == nil { } else if txDetail == nil {
@@ -64,9 +66,9 @@ func (b *BtcWallet) fetchOutputAddr(script []byte) (waddrmgr.ManagedAddress, err
// Therefore, we simply select the key for the first address we know // Therefore, we simply select the key for the first address we know
// of. // of.
for _, addr := range addrs { for _, addr := range addrs {
wAddr, err := b.wallet.Manager.Address(addr) addr, err := b.wallet.AddressInfo(addr)
if err == nil { if err == nil {
return wAddr, nil return addr, nil
} }
} }
@@ -85,12 +87,7 @@ func (b *BtcWallet) fetchPrivKey(pub *btcec.PublicKey) (*btcec.PrivateKey, error
return nil, err return nil, err
} }
walletddr, err := b.wallet.Manager.Address(addr) return b.wallet.PrivKeyForAddress(addr)
if err != nil {
return nil, err
}
return walletddr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
} }
// SignOutputRaw generates a signature for the passed transaction according to // SignOutputRaw generates a signature for the passed transaction according to