@@ -3,6 +3,9 @@ package dataformat
33import (
44 "encoding/hex"
55 "fmt"
6+ "regexp"
7+ "strconv"
8+ "strings"
69
710 "github.com/btcsuite/btcd/btcec/v2"
811 "github.com/lightningnetwork/lnd/keychain"
@@ -62,18 +65,19 @@ type ForceClose struct {
6265}
6366
6467type SummaryEntry struct {
65- RemotePubkey string `json:"remote_pubkey"`
66- ChannelPoint string `json:"channel_point"`
67- FundingTXID string `json:"funding_txid"`
68- FundingTXIndex uint32 `json:"funding_tx_index"`
69- Capacity uint64 `json:"capacity"`
70- Initiator bool `json:"initiator"`
71- LocalBalance uint64 `json:"local_balance"`
72- RemoteBalance uint64 `json:"remote_balance"`
73- ChanExists bool `json:"chan_exists_onchain"`
74- HasPotential bool `json:"has_potential_funds"`
75- ClosingTX * ClosingTX `json:"closing_tx,omitempty"`
76- ForceClose * ForceClose `json:"force_close"`
68+ RemotePubkey string `json:"remote_pubkey"`
69+ ChannelPoint string `json:"channel_point"`
70+ FundingTXID string `json:"funding_txid"`
71+ FundingTXIndex uint32 `json:"funding_tx_index"`
72+ Capacity uint64 `json:"capacity"`
73+ Initiator bool `json:"initiator"`
74+ LocalBalance uint64 `json:"local_balance"`
75+ RemoteBalance uint64 `json:"remote_balance"`
76+ ChanExists bool `json:"chan_exists_onchain"`
77+ HasPotential bool `json:"has_potential_funds"`
78+ LocalUnrevokedCommitPoint string `json:"local_unrevoked_commit_point"`
79+ ClosingTX * ClosingTX `json:"closing_tx,omitempty"`
80+ ForceClose * ForceClose `json:"force_close"`
7781}
7882
7983type SummaryEntryFile struct {
@@ -91,3 +95,48 @@ type SummaryEntryFile struct {
9195 FundsForceClose uint64 `json:"funds_force_closed_maybe_ours"`
9296 FundsCoopClose uint64 `json:"funds_coop_closed_maybe_ours"`
9397}
98+
99+ func ExtractSummaryFromDump (data string ) ([]* SummaryEntry , error ) {
100+ // Regex to match the data pattern.
101+ pattern := `(?ms) ChanPoint: \(string\) \(len=\d+\) "(.*?)",.*? ` +
102+ `Capacity: \(btcutil\.Amount\) ([\d\.]+) BTC,.*? ` +
103+ `LocalUnrevokedCommitPoint: \(string\) \(len=66\) "(.*?)"`
104+ re := regexp .MustCompile (pattern )
105+
106+ var results []* SummaryEntry
107+ matches := re .FindAllStringSubmatch (data , - 1 )
108+ for _ , match := range matches {
109+ if len (match ) == 4 {
110+ chanPoint := strings .TrimSpace (match [1 ])
111+ chanPointParts := strings .Split (chanPoint , ":" )
112+ if len (chanPointParts ) != 2 {
113+ return nil , fmt .Errorf ("invalid chanPoint: %s" ,
114+ chanPoint )
115+ }
116+ txid := chanPointParts [0 ]
117+ index , err := strconv .Atoi (chanPointParts [1 ])
118+ if err != nil {
119+ return nil , fmt .Errorf ("unable to parse " +
120+ "index: %w" , err )
121+ }
122+
123+ capacity , err := strconv .ParseFloat (match [2 ], 64 )
124+ if err != nil {
125+ return nil , fmt .Errorf ("unable to parse " +
126+ "capacity: %w" , err )
127+ }
128+
129+ results = append (results , & SummaryEntry {
130+ ChannelPoint : chanPoint ,
131+ FundingTXID : txid ,
132+ FundingTXIndex : uint32 (index ),
133+ Capacity : uint64 (
134+ capacity * 1e8 ,
135+ ),
136+ LocalUnrevokedCommitPoint : match [3 ],
137+ })
138+ }
139+ }
140+
141+ return results , nil
142+ }
0 commit comments