2022-07-22 02:41:08 +02:00
|
|
|
package lntemp
|
|
|
|
|
|
|
|
import (
|
2022-07-22 11:33:26 +02:00
|
|
|
"fmt"
|
2022-07-22 02:41:08 +02:00
|
|
|
"io"
|
2022-07-26 10:59:57 +02:00
|
|
|
"math"
|
2022-07-22 02:41:08 +02:00
|
|
|
"os"
|
2022-08-18 11:30:34 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-07-22 02:41:08 +02:00
|
|
|
|
2022-08-03 21:38:09 +02:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2022-08-03 20:36:12 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2022-08-12 07:07:16 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2022-07-22 02:41:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NeutrinoBackendName is the name of the neutrino backend.
|
|
|
|
NeutrinoBackendName = "neutrino"
|
|
|
|
|
2022-08-12 07:07:16 +02:00
|
|
|
DefaultTimeout = wait.DefaultTimeout
|
2022-07-26 10:59:57 +02:00
|
|
|
|
|
|
|
// noFeeLimitMsat is used to specify we will put no requirements on fee
|
|
|
|
// charged when choosing a route path.
|
|
|
|
noFeeLimitMsat = math.MaxInt64
|
|
|
|
|
|
|
|
// defaultPaymentTimeout specifies the default timeout in seconds when
|
|
|
|
// sending a payment.
|
|
|
|
defaultPaymentTimeout = 60
|
2022-07-22 02:41:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CopyFile copies the file src to dest.
|
|
|
|
func CopyFile(dest, src string) error {
|
|
|
|
s, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
d, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(d, s); err != nil {
|
|
|
|
d.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Close()
|
|
|
|
}
|
2022-07-22 11:33:26 +02:00
|
|
|
|
|
|
|
// errNumNotMatched is a helper method to return a nicely formatted error.
|
|
|
|
func errNumNotMatched(name string, subject string,
|
|
|
|
want, got, total, old int) error {
|
|
|
|
|
|
|
|
return fmt.Errorf("%s: assert %s failed: want %d, got: %d, total: "+
|
|
|
|
"%d, previously had: %d", name, subject, want, got, total, old)
|
|
|
|
}
|
2022-08-18 11:30:34 +02:00
|
|
|
|
|
|
|
// parseDerivationPath parses a path in the form of m/x'/y'/z'/a/b into a slice
|
|
|
|
// of [x, y, z, a, b], meaning that the apostrophe is ignored and 2^31 is _not_
|
|
|
|
// added to the numbers.
|
|
|
|
func ParseDerivationPath(path string) ([]uint32, error) {
|
|
|
|
path = strings.TrimSpace(path)
|
|
|
|
if len(path) == 0 {
|
|
|
|
return nil, fmt.Errorf("path cannot be empty")
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(path, "m/") {
|
|
|
|
return nil, fmt.Errorf("path must start with m/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just the root key, no path was provided. This is valid but not useful
|
|
|
|
// in most cases.
|
|
|
|
rest := strings.ReplaceAll(path, "m/", "")
|
|
|
|
if rest == "" {
|
|
|
|
return []uint32{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(rest, "/")
|
|
|
|
indices := make([]uint32, len(parts))
|
|
|
|
for i := 0; i < len(parts); i++ {
|
|
|
|
part := parts[i]
|
|
|
|
if strings.Contains(parts[i], "'") {
|
|
|
|
part = strings.TrimRight(parts[i], "'")
|
|
|
|
}
|
|
|
|
parsed, err := strconv.ParseInt(part, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse part \"%s\": "+
|
|
|
|
"%v", part, err)
|
|
|
|
}
|
|
|
|
indices[i] = uint32(parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices, nil
|
|
|
|
}
|
2022-08-03 20:36:12 +02:00
|
|
|
|
|
|
|
// ChanPointFromPendingUpdate constructs a channel point from a lnrpc pending
|
|
|
|
// update.
|
|
|
|
func ChanPointFromPendingUpdate(pu *lnrpc.PendingUpdate) *lnrpc.ChannelPoint {
|
|
|
|
chanPoint := &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: pu.Txid,
|
|
|
|
},
|
|
|
|
OutputIndex: pu.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
return chanPoint
|
|
|
|
}
|
2022-08-03 21:38:09 +02:00
|
|
|
|
|
|
|
// channelPointStr returns the string representation of the channel's
|
|
|
|
// funding transaction.
|
|
|
|
func channelPointStr(chanPoint *lnrpc.ChannelPoint) string {
|
|
|
|
fundingTxID, err := lnrpc.GetChanPointFundingTxid(chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
cp := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cp.String()
|
|
|
|
}
|