github.com/decred/dcrlnd@v0.7.6/lnwallet/dcrlnd_util.go (about)

     1  package lnwallet
     2  
     3  import (
     4  	"github.com/decred/dcrd/hdkeychain/v3"
     5  	"github.com/decred/dcrd/txscript/v4/stdaddr"
     6  )
     7  
     8  // DeriveAddrsFromExtPub derives a number of internal and external addresses from
     9  // an xpub key.
    10  func DeriveAddrsFromExtPub(xpub *hdkeychain.ExtendedKey, addrParams stdaddr.AddressParamsV0, count int) ([]stdaddr.Address, []stdaddr.Address, error) {
    11  
    12  	const (
    13  		externalBranch uint32 = 0
    14  		internalBranch uint32 = 1
    15  	)
    16  
    17  	extKey, err := xpub.Child(externalBranch)
    18  	if err != nil {
    19  		return nil, nil, err
    20  	}
    21  	intKey, err := xpub.Child(internalBranch)
    22  	if err != nil {
    23  		return nil, nil, err
    24  	}
    25  
    26  	var intIdx, extIdx uint32
    27  	var intAddrs, extAddrs []stdaddr.Address
    28  	for i := 0; i < count; i++ {
    29  		// Derive internal address. Loop because sometimes the address
    30  		// is invalid.
    31  		var addr stdaddr.Address
    32  		child, err := intKey.Child(intIdx)
    33  		for ; err != nil; intIdx += 1 {
    34  			child, err = intKey.Child(intIdx)
    35  			if err != nil {
    36  				continue
    37  			}
    38  			addr, err = stdaddr.NewAddressPubKeyEcdsaSecp256k1V0Raw(child.SerializedPubKey(), addrParams)
    39  			if err != nil {
    40  				continue
    41  			}
    42  		}
    43  		intAddrs = append(intAddrs, addr)
    44  
    45  		// Derive external address. Loop because sometimes the address
    46  		// is invalid.
    47  		child, err = extKey.Child(extIdx)
    48  		for ; err != nil; extIdx += 1 {
    49  			child, err = extKey.Child(extIdx)
    50  			if err != nil {
    51  				continue
    52  			}
    53  			addr, err = stdaddr.NewAddressPubKeyEcdsaSecp256k1V0Raw(child.SerializedPubKey(), addrParams)
    54  			if err != nil {
    55  				continue
    56  			}
    57  		}
    58  		extAddrs = append(extAddrs, addr)
    59  	}
    60  
    61  	return intAddrs, extAddrs, nil
    62  }
    63  
    64  // cacheCommitmentTxHash fills the cachedTxHash of a *commitment for logging.
    65  func cacheCommitmentTxHash(commit *commitment) *commitment {
    66  	if commit != nil && commit.txn != nil {
    67  		commit.txn.CachedTxHash()
    68  	}
    69  	return commit
    70  }