lnwallet: Extend Utxo struct with AddressType.

The Utxo struct now includes the address type and redeem/witness
scripts. This is necessary for accurate fee estimation.
This commit is contained in:
Jim Posen
2017-10-02 18:52:45 -07:00
committed by Olaoluwa Osuntokun
parent 10a336db46
commit c94130328a
3 changed files with 29 additions and 8 deletions

View File

@ -353,17 +353,28 @@ func (b *BtcWallet) ListUnspentWitness(minConfs int32) ([]*lnwallet.Utxo, error)
return nil, err return nil, err
} }
// TODO(roasbeef): this assumes all p2sh outputs returned by var addressType lnwallet.AddressType
// the wallet are nested p2sh... if txscript.IsPayToWitnessPubKeyHash(pkScript) {
if txscript.IsPayToWitnessPubKeyHash(pkScript) || addressType = lnwallet.WitnessPubKey
txscript.IsPayToScriptHash(pkScript) { } else if txscript.IsPayToScriptHash(pkScript) {
// TODO(roasbeef): This assumes all p2sh outputs returned by the
// wallet are nested p2pkh. We can't check the redeem script because
// the btcwallet service does not include it.
addressType = lnwallet.NestedWitnessPubKey
}
if addressType == lnwallet.WitnessPubKey ||
addressType == lnwallet.NestedWitnessPubKey {
txid, err := chainhash.NewHashFromStr(output.TxID) txid, err := chainhash.NewHashFromStr(output.TxID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
utxo := &lnwallet.Utxo{ utxo := &lnwallet.Utxo{
Value: btcutil.Amount(output.Amount * 1e8), AddressType: addressType,
Value: btcutil.Amount(output.Amount * 1e8),
PkScript: pkScript,
OutPoint: wire.OutPoint{ OutPoint: wire.OutPoint{
Hash: *txid, Hash: *txid,
Index: output.Vout, Index: output.Vout,

View File

@ -20,8 +20,12 @@ var ErrNotMine = errors.New("the passed output doesn't belong to the wallet")
type AddressType uint8 type AddressType uint8
const ( const (
// UnknownAddressType represents an output with an unknown or non-standard
// script.
UnknownAddressType AddressType = iota
// WitnessPubKey represents a p2wkh address. // WitnessPubKey represents a p2wkh address.
WitnessPubKey AddressType = iota WitnessPubKey
// NestedWitnessPubKey represents a p2sh output which is itself a // NestedWitnessPubKey represents a p2sh output which is itself a
// nested p2wkh output. // nested p2wkh output.
@ -34,7 +38,11 @@ const (
// Utxo is an unspent output denoted by its outpoint, and output value of the // Utxo is an unspent output denoted by its outpoint, and output value of the
// original output. // original output.
type Utxo struct { type Utxo struct {
Value btcutil.Amount AddressType AddressType
Value btcutil.Amount
PkScript []byte
RedeemScript []byte
WitnessScript []byte
wire.OutPoint wire.OutPoint
} }

View File

@ -149,7 +149,9 @@ func (*mockWalletController) SendOutputs(outputs []*wire.TxOut) (*chainhash.Hash
// need one unspent for the funding transaction. // need one unspent for the funding transaction.
func (*mockWalletController) ListUnspentWitness(confirms int32) ([]*lnwallet.Utxo, error) { func (*mockWalletController) ListUnspentWitness(confirms int32) ([]*lnwallet.Utxo, error) {
utxo := &lnwallet.Utxo{ utxo := &lnwallet.Utxo{
Value: btcutil.Amount(10 * btcutil.SatoshiPerBitcoin), AddressType: lnwallet.WitnessPubKey,
Value: btcutil.Amount(10 * btcutil.SatoshiPerBitcoin),
PkScript: make([]byte, 22),
OutPoint: wire.OutPoint{ OutPoint: wire.OutPoint{
Hash: chainhash.Hash{}, Hash: chainhash.Hash{},
Index: 0, Index: 0,