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
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
4 changed files with 51 additions and 51 deletions

2
lnd.go
View File

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

View File

@ -26,8 +26,9 @@ const (
)
var (
lnNamespace = []byte("ln")
rootKey = []byte("ln-root")
lnNamespace = []byte("ln")
rootKey = []byte("ln-root")
waddrmgrNamespaceKey = []byte("waddrmgr")
)
// 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 *chain.RPCClient
// lnNamespace is a namespace within btcwallet's walletdb used to store
// persistent state required by the WalletController interface but not
// natively supported by btcwallet.
lnNamespace walletdb.Namespace
db walletdb.DB
cfg *Config
netParams *chaincfg.Params
@ -60,7 +60,7 @@ var _ lnwallet.WalletController = (*BtcWallet)(nil)
// New returns a new fully initialized instance of BtcWallet given a valid
// 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.
netDir := networkDir(cfg.DataDir, cfg.NetParams)
@ -102,18 +102,28 @@ func New(cfg *Config) (*BtcWallet, error) {
return nil, err
}
// Create a bucket within the wallet's database dedicated to storing
// our LN specific data.
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 {
return nil, err
}
return &BtcWallet{
wallet: wallet,
rpc: rpcc,
lnNamespace: walletNamespace,
netParams: cfg.NetParams,
utxoCache: make(map[wire.OutPoint]*wire.TxOut),
cfg: &cfg,
wallet: wallet,
db: db,
rpc: rpcc,
netParams: cfg.NetParams,
utxoCache: make(map[wire.OutPoint]*wire.TxOut),
}, nil
}
@ -219,14 +229,9 @@ func (b *BtcWallet) NewAddress(t lnwallet.AddressType, change bool) (btcutil.Add
//
// 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 coresponding to the
// Using the ID address, request the private key corresponding to the
// address from the wallet's address manager.
walletAddr, err := b.wallet.Manager.Address(a)
if err != nil {
return nil, err
}
return walletAddr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
return b.wallet.PrivKeyForAddress(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
}
pkAddr, err := b.wallet.Manager.Address(addr)
if err != nil {
return nil, err
}
return pkAddr.(waddrmgr.ManagedPubKeyAddress).PubKey(), nil
return b.wallet.PubKeyForAddress(addr)
}
// 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
// wallet based on the address hash.
var rootAddrHash []byte
if err := b.lnNamespace.Update(func(tx walletdb.Tx) error {
rootBucket := tx.RootBucket()
if err := walletdb.View(b.db, func(tx walletdb.ReadTx) error {
lnBucket := tx.ReadBucket(lnNamespace)
rootAddrHash = rootBucket.Get(rootKey)
rootAddrHash = lnBucket.Get(rootKey)
return nil
}); err != nil {
return nil, err
@ -271,17 +271,19 @@ func (b *BtcWallet) FetchRootKey() (*btcec.PrivateKey, error) {
// Otherwise, we need to generate a fresh address from the
// wallet, then stores it's hash160 within the database so we
// can look up the exact key later.
rootAddr, err := b.wallet.Manager.NextExternalAddresses(defaultAccount,
1, waddrmgr.WitnessPubKey)
if err != nil {
return nil, err
}
if err := walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
addrs, err := b.wallet.Manager.NextExternalAddresses(addrmgrNs,
defaultAccount, 1, waddrmgr.WitnessPubKey)
if err != nil {
return err
}
rootAddr := addrs[0].Address()
if err := b.lnNamespace.Update(func(tx walletdb.Tx) error {
rootBucket := tx.RootBucket()
lnBucket := tx.ReadWriteBucket(lnNamespace)
rootAddrHash = rootAddr[0].Address().ScriptAddress()
return rootBucket.Put(rootKey, rootAddrHash)
rootAddrHash = rootAddr.ScriptAddress()
return lnBucket.Put(rootKey, rootAddrHash)
}); err != nil {
return nil, err
}
@ -295,12 +297,13 @@ func (b *BtcWallet) FetchRootKey() (*btcec.PrivateKey, error) {
if err != nil {
return nil, err
}
walletAddr, err := b.wallet.Manager.Address(rootAddr)
priv, err := b.wallet.PrivKeyForAddress(rootAddr)
if err != nil {
return nil, err
}
return walletAddr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
return priv, nil
}
// 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")
}
return New(config)
return New(*config)
}
// 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/btcutil"
"github.com/roasbeef/btcwallet/waddrmgr"
base "github.com/roasbeef/btcwallet/wallet"
)
// 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()
// 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 {
return nil, err
} 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
// of.
for _, addr := range addrs {
wAddr, err := b.wallet.Manager.Address(addr)
addr, err := b.wallet.AddressInfo(addr)
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
}
walletddr, err := b.wallet.Manager.Address(addr)
if err != nil {
return nil, err
}
return walletddr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
return b.wallet.PrivKeyForAddress(addr)
}
// SignOutputRaw generates a signature for the passed transaction according to