add LightningClient interface

This commit is contained in:
Jesse de Wit
2022-11-17 14:19:07 +01:00
parent 02ceb92286
commit 6f292003f9
3 changed files with 97 additions and 0 deletions

31
lightning_client.go Normal file
View File

@@ -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)
}

19
outpoint.go Normal file
View File

@@ -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
}

47
short_channel_id.go Normal file
View File

@@ -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)
}