Skip to content

Commit 0821c35

Browse files
committed
sweepremoteclosed: add command for sweeping closed channels
1 parent fe92337 commit 0821c35

File tree

9 files changed

+539
-14
lines changed

9 files changed

+539
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ Quick access:
316316
+ [showrootkey](doc/chantools_showrootkey.md)
317317
+ [signrescuefunding](doc/chantools_signrescuefunding.md)
318318
+ [summary](doc/chantools_summary.md)
319+
+ [sweepremoteclosed](doc/chantools_sweepremoteclosed.md)
319320
+ [sweeptimelock](doc/chantools_sweeptimelock.md)
320321
+ [sweeptimelockmanual](doc/chantools_sweeptimelockmanual.md)
321322
+ [vanitygen](doc/chantools_vanitygen.md)

btc/bitcoind.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/btcsuite/btcd/chaincfg"
9-
"github.com/btcsuite/btcd/txscript"
109
"github.com/btcsuite/btcd/wire"
1110
"github.com/btcsuite/btcutil"
1211
"github.com/btcsuite/btcutil/hdkeychain"
@@ -193,24 +192,15 @@ func (i *ImportWallet) Format(hdKey *hdkeychain.ExtendedKey,
193192
if err != nil {
194193
return "", fmt.Errorf("could not encode WIF: %v", err)
195194
}
196-
pubKey, err := hdKey.ECPubKey()
197-
if err != nil {
198-
return "", fmt.Errorf("could not derive private key: %v", err)
199-
}
200-
hash160 := btcutil.Hash160(pubKey.SerializeCompressed())
201-
addrP2PKH, err := btcutil.NewAddressPubKeyHash(hash160, params)
195+
addrP2PKH, err := lnd.P2PKHAddr(privKey.PubKey(), params)
202196
if err != nil {
203197
return "", fmt.Errorf("could not create address: %v", err)
204198
}
205-
addrP2WKH, err := btcutil.NewAddressWitnessPubKeyHash(hash160, params)
199+
addrP2WKH, err := lnd.P2WKHAddr(privKey.PubKey(), params)
206200
if err != nil {
207201
return "", fmt.Errorf("could not create address: %v", err)
208202
}
209-
script, err := txscript.PayToAddrScript(addrP2WKH)
210-
if err != nil {
211-
return "", fmt.Errorf("could not create script: %v", err)
212-
}
213-
addrNP2WKH, err := btcutil.NewAddressScriptHash(script, params)
203+
addrNP2WKH, err := lnd.NP2WKHAddr(privKey.PubKey(), params)
214204
if err != nil {
215205
return "", fmt.Errorf("could not create address: %v", err)
216206
}

btc/explorer_api.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ type Status struct {
5353
BlockHash string `json:"block_hash"`
5454
}
5555

56+
type Stats struct {
57+
FundedTXOCount uint32 `json:"funded_txo_count"`
58+
FundedTXOSum uint64 `json:"funded_txo_sum"`
59+
SpentTXOCount uint32 `json:"spent_txo_count"`
60+
SpentTXOSum uint64 `json:"spent_txo_sum"`
61+
TXCount uint32 `json:"tx_count"`
62+
}
63+
64+
type AddressStats struct {
65+
Address string `json:"address"`
66+
ChainStats *Stats `json:"chain_stats"`
67+
MempoolStats *Stats `json:"mempool_stats"`
68+
}
69+
5670
func (a *ExplorerAPI) Transaction(txid string) (*TX, error) {
5771
tx := &TX{}
5872
err := fetchJSON(fmt.Sprintf("%s/tx/%s", a.BaseURL, txid), tx)
@@ -90,6 +104,46 @@ func (a *ExplorerAPI) Outpoint(addr string) (*TX, int, error) {
90104
return nil, 0, fmt.Errorf("no tx found")
91105
}
92106

107+
func (a *ExplorerAPI) Unspent(addr string) ([]*Vout, error) {
108+
var (
109+
stats = &AddressStats{}
110+
outputs []*Vout
111+
txs []*TX
112+
err error
113+
)
114+
err = fetchJSON(fmt.Sprintf("%s/address/%s", a.BaseURL, addr), &stats)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
confirmedUnspent := stats.ChainStats.FundedTXOSum -
120+
stats.ChainStats.SpentTXOSum
121+
unconfirmedUnspent := stats.MempoolStats.FundedTXOSum -
122+
stats.MempoolStats.SpentTXOSum
123+
124+
if confirmedUnspent+unconfirmedUnspent == 0 {
125+
return nil, nil
126+
}
127+
128+
err = fetchJSON(fmt.Sprintf("%s/address/%s/txs", a.BaseURL, addr), &txs)
129+
if err != nil {
130+
return nil, err
131+
}
132+
for _, tx := range txs {
133+
for voutIdx, vout := range tx.Vout {
134+
if vout.ScriptPubkeyAddr == addr {
135+
vout.Outspend = &Outspend{
136+
Txid: tx.TXID,
137+
Vin: voutIdx,
138+
}
139+
outputs = append(outputs, vout)
140+
}
141+
}
142+
}
143+
144+
return outputs, nil
145+
}
146+
93147
func (a *ExplorerAPI) Address(outpoint string) (string, error) {
94148
parts := strings.Split(outpoint, ":")
95149

cmd/chantools/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func main() {
101101
newSummaryCommand(),
102102
newSweepTimeLockCommand(),
103103
newSweepTimeLockManualCommand(),
104+
newSweepRemoteClosedCommand(),
104105
newVanityGenCommand(),
105106
newWalletInfoCommand(),
106107
newZombieRecoveryCommand(),

0 commit comments

Comments
 (0)