pass errors up rather than logging directly
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -3,7 +3,7 @@ package bitcoinElectrum
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
@@ -43,13 +43,10 @@ func addressToScriptHash(address btcutil.Address) (string, error) {
|
||||
}
|
||||
|
||||
// Scans a derivation branch (m/0 or m/1) for balances until the gap limit is reached.
|
||||
func scanBranch(masterKey *hdkeychain.ExtendedKey, branch uint32, gapLimit int, spvServer string) int64 {
|
||||
log.Printf("Scanning branch m/%d/k...", branch)
|
||||
|
||||
func scanBranch(masterKey *hdkeychain.ExtendedKey, branch uint32, gapLimit int, spvServer string) (int64, error) {
|
||||
branchKey, err := masterKey.Derive(branch)
|
||||
if err != nil {
|
||||
log.Printf("Error deriving branch %d: %v", branch, err)
|
||||
return 0
|
||||
return 0, fmt.Errorf("error deriving branch %d: %v", branch, err)
|
||||
}
|
||||
|
||||
var branchTotalBalance int64
|
||||
@@ -57,35 +54,28 @@ func scanBranch(masterKey *hdkeychain.ExtendedKey, branch uint32, gapLimit int,
|
||||
|
||||
for i := uint32(0); ; i++ {
|
||||
if unusedAddressCount >= gapLimit {
|
||||
log.Printf("Gap limit of %d reached on branch m/%d. Stopping scan.", gapLimit, branch)
|
||||
break
|
||||
}
|
||||
|
||||
// 3. Derive child address
|
||||
address, err := deriveAddress(branchKey, i)
|
||||
if err != nil {
|
||||
log.Printf("Could not derive address at index %d on branch %d: %v", i, branch, err)
|
||||
continue
|
||||
return 0, fmt.Errorf("could not derive address at index %d on branch %d: %v", i, branch, err)
|
||||
}
|
||||
|
||||
// 4. Get balance from Electrum server
|
||||
balance, err := getAddressBalance(spvServer, address)
|
||||
if err != nil {
|
||||
log.Printf("Error getting balance for %s: %v", address.EncodeAddress(), err)
|
||||
// On error, we can't know if it's used, so we continue scanning.
|
||||
unusedAddressCount = 0
|
||||
continue
|
||||
return 0, fmt.Errorf("error getting balance for %s: %v", address.EncodeAddress(), err)
|
||||
}
|
||||
|
||||
total := balance.Result.Confirmed + balance.Result.Unconfirmed
|
||||
if total > 0 {
|
||||
log.Printf("Found balance for m/%d/%d (%s): %.8f BTC", branch, i, address.EncodeAddress(), btcutil.Amount(total).ToBTC())
|
||||
atomic.AddInt64(&branchTotalBalance, total)
|
||||
unusedAddressCount = 0 // Reset gap counter on finding a used address
|
||||
} else {
|
||||
log.Printf("No balance for m/%d/%d (%s)", branch, i, address.EncodeAddress())
|
||||
unusedAddressCount++
|
||||
}
|
||||
}
|
||||
return branchTotalBalance
|
||||
return branchTotalBalance, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user