From 6f292003f94319d5314c834a6cc0520460dd1b66 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 17 Nov 2022 14:19:07 +0100 Subject: [PATCH] add LightningClient interface --- lightning_client.go | 31 ++++++++++++++++++++++++++++++ outpoint.go | 19 ++++++++++++++++++ short_channel_id.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 lightning_client.go create mode 100644 outpoint.go create mode 100644 short_channel_id.go diff --git a/lightning_client.go b/lightning_client.go new file mode 100644 index 0000000..f26116d --- /dev/null +++ b/lightning_client.go @@ -0,0 +1,31 @@ +package main + +import "github.com/btcsuite/btcd/wire" + +type GetInfoResult struct { + Alias string + Pubkey string +} + +type GetChannelResult struct { + InitialChannelID ShortChannelID + ConfirmedChannelID ShortChannelID +} + +type OpenChannelRequest struct { + Destination []byte + CapacitySat uint64 + MinHtlcMsat uint64 + TargetConf uint32 + IsPrivate bool + IsZeroConf bool +} + +type LightningClient interface { + GetInfo() (*GetInfoResult, error) + IsConnected(destination []byte) (*bool, error) + OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) + GetChannel(peerID []byte, channelPoint wire.OutPoint) (*GetChannelResult, error) + GetNodeChannelCount(nodeID []byte) (int, error) + GetClosedChannels(nodeID string, channelPoints map[string]uint64) (map[string]uint64, error) +} diff --git a/outpoint.go b/outpoint.go new file mode 100644 index 0000000..a25bf8b --- /dev/null +++ b/outpoint.go @@ -0,0 +1,19 @@ +package main + +import ( + "log" + + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/wire" +) + +func NewOutPoint(fundingTxID []byte, index uint32) (*wire.OutPoint, error) { + var h chainhash.Hash + err := h.SetBytes(fundingTxID) + if err != nil { + log.Printf("h.SetBytes(%x) error: %v", fundingTxID, err) + return nil, err + } + + return wire.NewOutPoint(&h, index), nil +} diff --git a/short_channel_id.go b/short_channel_id.go new file mode 100644 index 0000000..c8207a2 --- /dev/null +++ b/short_channel_id.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +type ShortChannelID uint64 + +func NewShortChannelIDFromString(channelID string) (*ShortChannelID, error) { + parts := strings.Split(channelID, "x") + if len(parts) != 3 { + return nil, fmt.Errorf("expected 3 parts, got %d", len(parts)) + } + + blockHeight, err := strconv.Atoi(parts[0]) + if err != nil { + return nil, err + } + + txIndex, err := strconv.Atoi(parts[1]) + if err != nil { + return nil, err + } + + outputIndex, err := strconv.Atoi(parts[2]) + if err != nil { + return nil, err + } + + result := ShortChannelID( + (uint64(outputIndex) & 0xFFFF) + + ((uint64(txIndex) << 16) & 0xFFFFFF0000) + + ((uint64(blockHeight) << 40) & 0xFFFFFF0000000000), + ) + + return &result, nil +} + +func (c *ShortChannelID) ToString() string { + u := uint64(*c) + blockHeight := (u >> 40) & 0xFFFFFF + txIndex := (u >> 16) & 0xFFFFFF + outputIndex := u & 0xFFFF + return fmt.Sprintf("%dx%dx%d", blockHeight, txIndex, outputIndex) +}