Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test-matrix-bitcoin_mixed: test-bins

.PHONY: test-matrix-bitcoin_lndlnd
test-matrix-bitcoin_lndlnd: test-bins
${INTEGRATION_TEST_ENV} go test ${INTEGRATION_TEST_OPTS} -run '^Test_Swap(In|Out)Matrix/(bitcoin_lndlnd).*$$' ./test
${INTEGRATION_TEST_ENV} go test ${INTEGRATION_TEST_OPTS} -run '^(Test_Swap(In|Out)Matrix/(bitcoin_lndlnd).*|Test_2Hop_Bitcoin_LND.*)$$' ./test

.PHONY: test-matrix-liquid_clncln
test-matrix-liquid_clncln: test-bins
Expand Down
111 changes: 111 additions & 0 deletions clightning/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
log2 "log"
"os"
"sort"
"time"

"github.com/btcsuite/btcd/chaincfg"
Expand Down Expand Up @@ -198,6 +199,7 @@ type PeerChannel struct {
PeerConnected bool `json:"peer_connected"`
State string `json:"state"`
ShortChannelId string `json:"short_channel_id,omitempty"`
Private bool `json:"private,omitempty"`
TotalMsat glightning.Amount `json:"total_msat,omitempty"`
ToUsMsat glightning.Amount `json:"to_us_msat,omitempty"`
ReceivableMsat glightning.Amount `json:"receivable_msat,omitempty"`
Expand Down Expand Up @@ -294,6 +296,55 @@ func (cl *ClightningClient) ReceivableMsat(scid string) (uint64, error) {
return 0, fmt.Errorf("could not find a channel with scid: %s", scid)
}

func (cl *ClightningClient) ChannelsToPeer(peerPubkey string) ([]string, error) {
var res ListPeerChannelsResponse
err := cl.glightning.Request(ListPeerChannelsRequest{Id: peerPubkey}, &res)
if err != nil {
return nil, err
}

var scids []string
for _, ch := range res.Channels {
if ch.PeerId != peerPubkey || ch.ShortChannelId == "" {
continue
}
if err := cl.checkChannel(ch); err != nil {
continue
}
scids = append(scids, lightning.Scid(ch.ShortChannelId).ClnStyle())
}
sort.Strings(scids)
return scids, nil
}

func (cl *ClightningClient) ListChannels(ctx context.Context) ([]peersync.Channel, error) {
var res ListPeerChannelsResponse
if err := cl.glightning.Request(ListPeerChannelsRequest{}, &res); err != nil {
return nil, err
}

channels := make([]peersync.Channel, 0, len(res.Channels))
for _, ch := range res.Channels {
if ch.PeerId == "" || ch.ShortChannelId == "" {
continue
}
peerID, err := peersync.NewPeerID(ch.PeerId)
if err != nil {
continue
}

channels = append(channels, peersync.Channel{
Peer: peerID,
ChannelID: 0,
ShortChannelID: lightning.Scid(ch.ShortChannelId).ClnStyle(),
Active: ch.PeerConnected && ch.State == "CHANNELD_NORMAL",
Public: !ch.Private,
})
}

return channels, nil
}

// checkChannel performs a set of sanity checks id the channel is eligible for
// a swap of amtSat
func (cl *ClightningClient) checkChannel(ch PeerChannel) error {
Expand Down Expand Up @@ -503,6 +554,66 @@ func (cl *ClightningClient) PayInvoiceViaChannel(payreq string, scid string) (pr
return preimage, nil
}

func (cl *ClightningClient) PayInvoiceVia2HopRoute(payreq string, outgoingScid string, incomingScid string, intermediaryPubkey string) (preimage string, err error) {
bolt11, err := cl.glightning.DecodeBolt11(payreq)
if err != nil {
return "", err
}

if bolt11.AmountMsat.MSat() == 0 {
return "", fmt.Errorf("invoice has no amount")
}

outgoingScid = lightning.Scid(outgoingScid).ClnStyle()
incomingScid = lightning.Scid(incomingScid).ClnStyle()

route, err := cl.glightning.GetRoute(
bolt11.Payee,
bolt11.AmountMsat.MSat(),
10.0,
uint(bolt11.MinFinalCltvExpiry+1),
"",
0.0001,
nil,
2,
)
if err != nil {
return "", err
}

if len(route) != 2 {
return "", fmt.Errorf("could not find 2-hop route")
}
if route[0].Id != intermediaryPubkey {
return "", fmt.Errorf("route does not use expected intermediary")
}
if route[0].ShortChannelId != outgoingScid || route[1].ShortChannelId != incomingScid {
return "", fmt.Errorf("route does not use expected channels")
}

label := randomString()
_, err = cl.glightning.SendPay(
route,
bolt11.PaymentHash,
label,
bolt11.AmountMsat.MSat(),
payreq,
bolt11.PaymentSecret,
0,
)
if err != nil {
return "", err
}

res, err := cl.glightning.WaitSendPay(bolt11.PaymentHash, 0)
if err != nil {
return "", err
}

preimage = res.PaymentPreimage
return preimage, nil
}

// RebalancePayment handles the lightning payment that should re-balance the
// channel.
func (cl *ClightningClient) RebalancePayment(payreq string, channel string) (preimage string, err error) {
Expand Down
37 changes: 31 additions & 6 deletions clightning/clightning_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ type SwapOut struct {
Asset string `json:"asset"`
PremiumLimitRatePPM int64 `json:"premium_rate_limit_ppm"`
Force bool `json:"force"`
PeerPubkey string `json:"peer_pubkey,omitempty"`
cl *ClightningClient `json:"-"`
}

Expand Down Expand Up @@ -227,14 +228,20 @@ func (l *SwapOut) Call() (jrpc2.Result, error) {
return nil, errors.New("fundingChannels is not connected")
}

intermediaryPeerId := fundingChannels.Id
peerId := intermediaryPeerId
if l.PeerPubkey != "" {
peerId = l.PeerPubkey
}

// Skip this check when `force` is set.
if !l.Force {
if l.cl.peerSync == nil || !l.cl.peerSync.HasCompatiblePeer(fundingChannels.Id) {
if l.cl.peerSync == nil || !l.cl.peerSync.HasCompatiblePeer(peerId) {
return nil, fmt.Errorf("peer does not run peerswap")
}
}

if !l.cl.isPeerConnected(fundingChannels.Id) {
if !l.cl.isPeerConnected(peerId) {
return nil, fmt.Errorf("peer is not connected")
}

Expand All @@ -255,7 +262,12 @@ func (l *SwapOut) Call() (jrpc2.Result, error) {
}

pk := l.cl.GetNodeId()
swapOut, err := l.cl.swaps.SwapOut(fundingChannels.Id, l.Asset, l.ShortChannelId, pk, l.SatAmt, l.PremiumLimitRatePPM)
var swapOut *swap.SwapStateMachine
if l.PeerPubkey != "" && l.PeerPubkey != intermediaryPeerId {
swapOut, err = l.cl.swaps.SwapOutTwoHop(peerId, l.Asset, l.ShortChannelId, pk, l.SatAmt, l.PremiumLimitRatePPM, intermediaryPeerId)
} else {
swapOut, err = l.cl.swaps.SwapOut(peerId, l.Asset, l.ShortChannelId, pk, l.SatAmt, l.PremiumLimitRatePPM)
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -302,6 +314,7 @@ type SwapIn struct {
Asset string `json:"asset"`
PremiumLimitRatePPM int64 `json:"premium_limit_ppm"`
Force bool `json:"force"`
PeerPubkey string `json:"peer_pubkey,omitempty"`
cl *ClightningClient `json:"-"`
}

Expand Down Expand Up @@ -349,14 +362,20 @@ func (l *SwapIn) Call() (jrpc2.Result, error) {
return nil, errors.New("fundingChannels is not connected")
}

intermediaryPeerId := fundingChannels.Id
peerId := intermediaryPeerId
if l.PeerPubkey != "" {
peerId = l.PeerPubkey
}

// Skip this check when `force` is set.
if !l.Force {
if l.cl.peerSync == nil || !l.cl.peerSync.HasCompatiblePeer(fundingChannels.Id) {
if l.cl.peerSync == nil || !l.cl.peerSync.HasCompatiblePeer(peerId) {
return nil, fmt.Errorf("peer does not run peerswap")
}
}

if !l.cl.isPeerConnected(fundingChannels.Id) {
if !l.cl.isPeerConnected(peerId) {
return nil, fmt.Errorf("peer is not connected")
}
switch l.Asset {
Expand All @@ -373,7 +392,12 @@ func (l *SwapIn) Call() (jrpc2.Result, error) {
}

pk := l.cl.GetNodeId()
swapIn, err := l.cl.swaps.SwapIn(fundingChannels.Id, l.Asset, l.ShortChannelId, pk, l.SatAmt, l.PremiumLimitRatePPM)
var swapIn *swap.SwapStateMachine
if l.PeerPubkey != "" && l.PeerPubkey != intermediaryPeerId {
swapIn, err = l.cl.swaps.SwapInTwoHop(peerId, l.Asset, l.ShortChannelId, pk, l.SatAmt, l.PremiumLimitRatePPM, intermediaryPeerId)
} else {
swapIn, err = l.cl.swaps.SwapIn(peerId, l.Asset, l.ShortChannelId, pk, l.SatAmt, l.PremiumLimitRatePPM)
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -586,6 +610,7 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {

view := format.BuildPeerView(peer.Id, capability, swaps)
peerSwapPeer := peerswaprpc.NewPeerSwapPeerFromView(view)
peerSwapPeer.ChannelAdjacency = peerswaprpc.ChannelAdjacencyFromPeerState(peerState)
channels, err := l.cl.glightning.ListChannelsBySource(peer.Id)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions clightning/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
liquidRpcUserOption = "peerswap-elementsd-rpcuser"
liquidRpcPasswordOption = "peerswap-elementsd-rpcpassword"
liquidRpcPasswordFilepathOption = "peerswap-elementsd-rpcpasswordfile"
liquidSwapsOption = "peerswap-elementsd-swaps"
liquidSwapsOption = "peerswap-elementsd-swaps"
liquidRpcWalletOption = "peerswap-elementsd-rpcwallet"

bitcoinRpcHostOption = "peerswap-bitcoin-rpchost"
Expand Down Expand Up @@ -53,7 +53,7 @@ type PeerswapClightningConfig struct {
LiquidRpcHost string `json:"liquid.rpchost"`
LiquidRpcPort uint `json:"liquid.rpcport"`
LiquidRpcWallet string `json:"liquid.rpcwallet"`
LiquidSwaps bool `json:"liquid.swaps"`
LiquidSwaps bool `json:"liquid.swaps"`

LWKSignerName string `json:"lwksignername"`
LWKWalletName string `json:"lwkwalletname"`
Expand Down
9 changes: 9 additions & 0 deletions cmd/peerswaplnd/pscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ var (
Name: "peer_pubkey",
Required: true,
}
swapPeerPubkeyFlag = cli.StringFlag{
Name: "peer_pubkey",
Usage: "swap negotiation peer pubkey (optional; set for 2-hop swaps)",
Required: false,
}
PremiumLimitRatePPMFlag = cli.Uint64Flag{
Name: "premium_limit_rate_ppm",
Usage: "premium limit for a swap in parts per million",
Expand Down Expand Up @@ -104,6 +109,7 @@ var (
channelIdFlag,
assetFlag,
PremiumLimitRatePPMFlag,
swapPeerPubkeyFlag,
},
Action: swapOut,
}
Expand All @@ -116,6 +122,7 @@ var (
channelIdFlag,
assetFlag,
PremiumLimitRatePPMFlag,
swapPeerPubkeyFlag,
},
Action: swapIn,
}
Expand Down Expand Up @@ -291,6 +298,7 @@ func swapIn(ctx *cli.Context) error {
SwapAmount: ctx.Uint64(satAmountFlag.Name),
Asset: ctx.String(assetFlag.Name),
PremiumLimitRatePpm: ctx.Int64(PremiumLimitRatePPMFlag.Name),
PeerPubkey: ctx.String(swapPeerPubkeyFlag.Name),
})
if err != nil {
return err
Expand All @@ -312,6 +320,7 @@ func swapOut(ctx *cli.Context) error {
SwapAmount: ctx.Uint64(satAmountFlag.Name),
Asset: ctx.String(assetFlag.Name),
PremiumLimitRatePpm: ctx.Int64(PremiumLimitRatePPMFlag.Name),
PeerPubkey: ctx.String(swapPeerPubkeyFlag.Name),
})
if err != nil {
return err
Expand Down
Loading
Loading