Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 4, 2020
1 parent 26e1986 commit 98075a7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 40 deletions.
13 changes: 9 additions & 4 deletions .golangci.yml
Expand Up @@ -17,8 +17,13 @@ linters-settings:
linters:
enable-all: true
disable:
# Init functions are used by loggers throughout the codebase.
- gochecknoinits

# Global variables are used by loggers.
- gochecknoglobals
- gosec
- funlen
- maligned

issues:
exclude-rules:
- path: cmd/chantools
linters:
- lll
17 changes: 9 additions & 8 deletions btc/explorer_api.go
Expand Up @@ -13,8 +13,8 @@ var (
ErrTxNotFound = errors.New("transaction not found")
)

type ExplorerApi struct {
BaseUrl string
type ExplorerAPI struct {
BaseURL string
}

type TX struct {
Expand Down Expand Up @@ -51,15 +51,15 @@ type Status struct {
BlockHash string `json:"block_hash"`
}

func (a *ExplorerApi) Transaction(txid string) (*TX, error) {
func (a *ExplorerAPI) Transaction(txid string) (*TX, error) {
tx := &TX{}
err := fetchJSON(fmt.Sprintf("%s/tx/%s", a.BaseUrl, txid), tx)
err := fetchJSON(fmt.Sprintf("%s/tx/%s", a.BaseURL, txid), tx)
if err != nil {
return nil, err
}
for idx, vout := range tx.Vout {
url := fmt.Sprintf(
"%s/tx/%s/outspend/%d", a.BaseUrl, txid, idx,
"%s/tx/%s/outspend/%d", a.BaseURL, txid, idx,
)
outspend := Outspend{}
err := fetchJSON(url, &outspend)
Expand All @@ -71,12 +71,13 @@ func (a *ExplorerApi) Transaction(txid string) (*TX, error) {
return tx, nil
}

func (a *ExplorerApi) PublishTx(rawTxHex string) (string, error) {
url := fmt.Sprintf("%s/tx", a.BaseUrl)
func (a *ExplorerAPI) PublishTx(rawTxHex string) (string, error) {
url := fmt.Sprintf("%s/tx", a.BaseURL)
resp, err := http.Post(url, "text/plain", strings.NewReader(rawTxHex))
if err != nil {
return "", err
}
defer resp.Body.Close()
body := new(bytes.Buffer)
_, err = body.ReadFrom(resp.Body)
if err != nil {
Expand All @@ -99,7 +100,7 @@ func fetchJSON(url string, target interface{}) error {
}
err = json.Unmarshal(body.Bytes(), target)
if err != nil {
if string(body.Bytes()) == "Transaction not found" {
if body.String() == "Transaction not found" {
return ErrTxNotFound
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/chantools/derivekey.go
Expand Up @@ -19,9 +19,9 @@ func (c *deriveKeyCommand) Execute(_ []string) error {

var (
extendedKey *hdkeychain.ExtendedKey
err error
err error
)

// Check that root key is valid or fall back to console input.
switch {
case c.RootKey != "":
Expand Down
2 changes: 1 addition & 1 deletion cmd/chantools/dumpbackup.go
Expand Up @@ -20,7 +20,7 @@ func (c *dumpBackupCommand) Execute(_ []string) error {

var (
extendedKey *hdkeychain.ExtendedKey
err error
err error
)

// Check that root key is valid or fall back to console input.
Expand Down
6 changes: 3 additions & 3 deletions cmd/chantools/forceclose.go
Expand Up @@ -27,7 +27,7 @@ type forceCloseCommand struct {
func (c *forceCloseCommand) Execute(_ []string) error {
var (
extendedKey *hdkeychain.ExtendedKey
err error
err error
)

// Check that root key is valid or fall back to console input.
Expand Down Expand Up @@ -67,7 +67,7 @@ func forceCloseChannels(extendedKey *hdkeychain.ExtendedKey,
if err != nil {
return err
}
chainApi := &btc.ExplorerApi{BaseUrl: cfg.ApiUrl}
api := &btc.ExplorerAPI{BaseURL: cfg.APIURL}
signer := &btc.Signer{ExtendedKey: extendedKey}

// Go through all channels in the DB, find the still open ones and
Expand Down Expand Up @@ -170,7 +170,7 @@ func forceCloseChannels(extendedKey *hdkeychain.ExtendedKey,

// Publish TX.
if publish {
response, err := chainApi.PublishTx(serialized)
response, err := api.PublishTx(serialized)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/chantools/main.go
Expand Up @@ -5,28 +5,28 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/lightningnetwork/lnd/aezeed"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"os"
"strings"
"syscall"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/guggero/chantools/dataformat"
"github.com/jessevdk/go-flags"
"github.com/lightningnetwork/lnd/aezeed"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/channeldb"
"golang.org/x/crypto/ssh/terminal"
)

const (
defaultApiUrl = "https://blockstream.info/api"
defaultAPIURL = "https://blockstream.info/api"
)

type config struct {
Testnet bool `long:"testnet" description:"Set to true if testnet parameters should be used."`
ApiUrl string `long:"apiurl" description:"API URL to use (must be esplora compatible)."`
APIURL string `long:"apiurl" description:"API URL to use (must be esplora compatible)."`
ListChannels string `long:"listchannels" description:"The channel input is in the format of lncli's listchannels format. Specify '-' to read from stdin."`
PendingChannels string `long:"pendingchannels" description:"The channel input is in the format of lncli's pendingchannels format. Specify '-' to read from stdin."`
FromSummary string `long:"fromsummary" description:"The channel input is in the format of this tool's channel summary. Specify '-' to read from stdin."`
Expand All @@ -37,7 +37,7 @@ var (
logWriter = build.NewRotatingLogWriter()
log = build.NewSubLogger("CHAN", logWriter.GenSubLogger)
cfg = &config{
ApiUrl: defaultApiUrl,
APIURL: defaultAPIURL,
}
chainParams = &chaincfg.MainNetParams
)
Expand Down Expand Up @@ -184,7 +184,7 @@ func rootKeyFromConsole() (*hdkeychain.ExtendedKey, error) {
}

var mnemonic aezeed.Mnemonic
copy(mnemonic[:], cipherSeedMnemonic[:])
copy(mnemonic[:], cipherSeedMnemonic)

// If we're unable to map it back into the ciphertext, then either the
// mnemonic is wrong, or the passphrase is wrong.
Expand Down
4 changes: 2 additions & 2 deletions cmd/chantools/rescueclosed.go
Expand Up @@ -39,7 +39,7 @@ type rescueClosedCommand struct {
func (c *rescueClosedCommand) Execute(_ []string) error {
var (
extendedKey *hdkeychain.ExtendedKey
err error
err error
)

// Check that root key is valid or fall back to console input.
Expand Down Expand Up @@ -169,7 +169,7 @@ func addrInCache(addr string, perCommitPoint *btcec.PublicKey) (string, error) {
tweakedPubKey.SerializeCompressed(),
)
equal := subtle.ConstantTimeCompare(
targetPubKeyHash[:], hashedPubKey[:],
targetPubKeyHash, hashedPubKey,
)
if equal == 1 {
wif, err := btcutil.NewWIF(
Expand Down
14 changes: 7 additions & 7 deletions cmd/chantools/summary.go
Expand Up @@ -18,19 +18,19 @@ func (c *summaryCommand) Execute(_ []string) error {
if err != nil {
return err
}
return summarizeChannels(cfg.ApiUrl, entries)
return summarizeChannels(cfg.APIURL, entries)
}

func summarizeChannels(apiUrl string,
func summarizeChannels(apiURL string,
channels []*dataformat.SummaryEntry) error {

summaryFile := &dataformat.SummaryEntryFile{
Channels: channels,
}
chainApi := &btc.ExplorerApi{BaseUrl: apiUrl}
api := &btc.ExplorerAPI{BaseURL: apiURL}

for idx, channel := range channels {
tx, err := chainApi.Transaction(channel.FundingTXID)
tx, err := api.Transaction(channel.FundingTXID)
if err == btc.ErrTxNotFound {
log.Errorf("Funding TX %s not found. Ignoring.",
channel.FundingTXID)
Expand All @@ -52,7 +52,7 @@ func summarizeChannels(apiUrl string,
}

err := reportOutspend(
chainApi, summaryFile, channel, outspend,
api, summaryFile, channel, outspend,
)
if err != nil {
log.Errorf("Problem with channel %d (%s): %v.",
Expand Down Expand Up @@ -104,7 +104,7 @@ func summarizeChannels(apiUrl string,
return ioutil.WriteFile(fileName, summaryBytes, 0644)
}

func reportOutspend(api *btc.ExplorerApi,
func reportOutspend(api *btc.ExplorerAPI,
summaryFile *dataformat.SummaryEntryFile,
entry *dataformat.SummaryEntry, os *btc.Outspend) error {

Expand Down Expand Up @@ -150,7 +150,7 @@ func reportOutspend(api *btc.ExplorerApi,
// Could maybe be brute forced.
if len(utxo) == 1 &&
utxo[0].ScriptPubkeyType == "v0_p2wpkh" &&
utxo[0].Outspend.Spent == false {
!utxo[0].Outspend.Spent {

entry.ClosingTX.OurAddr = utxo[0].ScriptPubkeyAddr
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/chantools/sweeptimelock.go
Expand Up @@ -29,7 +29,7 @@ type sweepTimeLockCommand struct {
func (c *sweepTimeLockCommand) Execute(_ []string) error {
var (
extendedKey *hdkeychain.ExtendedKey
err error
err error
)

// Check that root key is valid or fall back to console input.
Expand Down Expand Up @@ -60,12 +60,12 @@ func (c *sweepTimeLockCommand) Execute(_ []string) error {
c.MaxCsvLimit = 2000
}
return sweepTimeLock(
extendedKey, cfg.ApiUrl, entries, c.SweepAddr, c.MaxCsvLimit,
extendedKey, cfg.APIURL, entries, c.SweepAddr, c.MaxCsvLimit,
c.Publish,
)
}

func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiUrl string,
func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiURL string,
entries []*dataformat.SummaryEntry, sweepAddr string, maxCsvTimeout int,
publish bool) error {

Expand All @@ -74,7 +74,7 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiUrl string,
ExtendedKey: extendedKey,
ChainParams: chainParams,
}
chainApi := &btc.ExplorerApi{BaseUrl: apiUrl}
api := &btc.ExplorerAPI{BaseURL: apiURL}

sweepTx := wire.NewMsgTx(2)
totalOutputValue := int64(0)
Expand Down Expand Up @@ -230,7 +230,7 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiUrl string,

// Publish TX.
if publish {
response, err := chainApi.PublishTx(
response, err := api.PublishTx(
hex.EncodeToString(buf.Bytes()),
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion dataformat/input.go
Expand Up @@ -171,7 +171,7 @@ func fundingTXID(chanPoint string) string {
func fundingTXIndex(chanPoint string) uint32 {
parts := strings.Split(chanPoint, ":")
if len(parts) != 2 {
panic(fmt.Errorf("channel point not in format <txid>:<idx>",
panic(fmt.Errorf("channel point %s not in format <txid>:<idx>",
chanPoint))
}
return uint32(parseInt(parts[1]))
Expand Down

0 comments on commit 98075a7

Please sign in to comment.