Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lnd: label the tx to make it easier to audit #282

Merged
merged 3 commits into from
Feb 19, 2024
Merged
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
8 changes: 8 additions & 0 deletions clightning/clightning_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ func (cl *ClightningClient) CreateCoopSpendingTransaction(swapParams *swap.Openi
return spendingTx.TxHash().String(), txHex, nil
}

func (cl *ClightningClient) LabelTransaction(txId, label string) error {
// todo implement
// This function assigns an identifiable label to the target transaction based on the txid.
// Currently no such functionality is available, so it has not been implemented.
// Supported by lnd only.
return nil
}

func (cl *ClightningClient) NewAddress() (string, error) {
newAddr, err := cl.glightning.NewAddr()
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions labels/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package labels

import "fmt"

const (
// peerswapLabelPattern is the pattern that peerswap uses to label on-chain transactions.
peerswapLabelPattern = "peerswap -- %s(swap id=%s)"
// opening is the label used for the opening transaction.
opening = "Opening"
// claimByInvoice is the label used for the claim by invoice transaction.
claimByInvoice = "ClaimByInvoice"
// claimByCoop is the label used for the claim by cooperative close transaction.
claimByCoop = "ClaimByCoop"
// ClaimByCsv is the label used for the claim by CSV transaction.
claimByCsv = "ClaimByCsv"
)

// Opening returns the label used for the opening transaction.
func Opening(swapID string) string {
return fmt.Sprintf(peerswapLabelPattern, opening, swapID)
}

// ClaimByInvoice returns the label used for the claim by invoice transaction.
func ClaimByInvoice(swapID string) string {
return fmt.Sprintf(peerswapLabelPattern, claimByInvoice, swapID)
}

// ClaimByCoop returns the label used for the claim by cooperative close transaction.
func ClaimByCoop(swapID string) string {
return fmt.Sprintf(peerswapLabelPattern, claimByCoop, swapID)
}

// ClaimByCsv returns the label used for the claim by CSV transaction.
func ClaimByCsv(swapID string) string {
return fmt.Sprintf(peerswapLabelPattern, claimByCsv, swapID)
}
17 changes: 17 additions & 0 deletions lnd/lnd_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"

"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/elementsproject/peerswap/lightning"
"github.com/elementsproject/peerswap/onchain"
Expand Down Expand Up @@ -207,6 +208,22 @@ func (l *Client) CreateCoopSpendingTransaction(swapParams *swap.OpeningParams, c
return spendingTx.TxHash().String(), txHex, nil
}

// LabelTransaction labels a transaction with a given label.
// This makes it easier to audit the transactions from faraday.
// This is performed by LND's LabelTransaction RPC.
func (l *Client) LabelTransaction(txID, label string) error {
txIDHash, err := chainhash.NewHashFromStr(txID)
if err != nil {
return err
}
_, err = l.walletClient.LabelTransaction(l.ctx,
&walletrpc.LabelTransactionRequest{
Txid: txIDHash.CloneBytes(),
Label: label,
Overwrite: true})
Comment on lines +220 to +223
Copy link

@mrfelton mrfelton Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should include a reference to the swap that the transaction relates to in the label. e.g. the swap id, or first few characters of swap id (this is what Lightning Loop does, I think just to keep the labels shorter and more readable)

Here are some examples of the onchain transaction labels added by Loop:

loopd -- OutSweepSuccess(swap=40f86b)
loopd -- InHtlc(swap=e5145f)
loopd -- InSweepTimeout(swap=3cc561)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With reference to loopd, the following labels are set.

  • peerswap -- Opening(swap id=b171ee)
  • peerswap -- ClaimByCoop(swap id=b171ee)
  • peerswap -- ClaimByCsv(swap id=b171ee)
  • peerswap -- ClaimByInvoice(swap id=b171ee)

return err
}

func (l *Client) GetOnchainBalance() (uint64, error) {
res, err := l.lndClient.WalletBalance(l.ctx, &lnrpc.WalletBalanceRequest{})
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions onchain/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ func (b *BitcoinOnChain) PrepareSpendingTransaction(swapParams *swap.OpeningPara
return spendingTx, sigHash, redeemScript, nil
}

func (b *BitcoinOnChain) LabelTransaction(txID, label string) error {
// TODO: implement
// This function assigns an identifiable label to the target transaction based on the txid.
// Currently no such functionality is available, so it has not been implemented.
// Supported by lnd only.
return nil
}

func (b *BitcoinOnChain) CreateOpeningAddress(params *swap.OpeningParams, csv uint32) (string, error) {
redeemScript, err := ParamsToTxScript(params, csv)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions onchain/liquid.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ func (l *LiquidOnChain) CreateCoopSpendingTransaction(swapParams *swap.OpeningPa
return txId, txHex, nil
}

func (l *LiquidOnChain) LabelTransaction(txID, label string) error {
// TODO: implement
// This function assigns an identifiable label to the target transaction based on the txid.
// Currently no such functionality is available, so it has not been implemented.
// Supported by lnd only.
return nil
}

func (l *LiquidOnChain) AddBlindingRandomFactors(claimParams *swap.ClaimParams) (err error) {
claimParams.OutputAssetBlindingFactor = generateRandom32Bytes()
claimParams.BlindingSeed = generateRandom32Bytes()
Expand Down
21 changes: 21 additions & 0 deletions swap/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/btcsuite/btcd/btcec/v2"
"github.com/elementsproject/peerswap/isdev"
"github.com/elementsproject/peerswap/labels"
"github.com/elementsproject/peerswap/lightning"
"github.com/elementsproject/peerswap/messages"
)
Expand Down Expand Up @@ -194,6 +195,11 @@ func (s *ClaimSwapTransactionWithPreimageAction) Execute(services *SwapServices,
return Event_OnRetry
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, labels.ClaimByInvoice(swap.GetId().Short()))
if err != nil {
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.ClaimByInvoice(swap.GetId().Short()), err)
}
}

return Event_ActionSucceeded
Expand Down Expand Up @@ -249,6 +255,11 @@ func (c *CreateAndBroadcastOpeningTransaction) Execute(services *SwapServices, s
// todo: idempotent states
return swap.HandleError(err)
}
err = wallet.LabelTransaction(txId, labels.Opening(swap.GetId().Short()))
if err != nil {
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.Opening(swap.GetId().Short()), err)
}
startingHeight, err := txWatcher.GetBlockHeight()
if err != nil {
return swap.HandleError(err)
Expand Down Expand Up @@ -437,6 +448,11 @@ func (c *ClaimSwapTransactionWithCsv) Execute(services *SwapServices, swap *Swap
return Event_OnRetry
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, labels.ClaimByCsv(swap.GetId().Short()))
if err != nil {
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.ClaimByCsv(swap.GetId().Short()), err)
}
}

return Event_ActionSucceeded
Expand All @@ -463,6 +479,11 @@ func (c *ClaimSwapTransactionCoop) Execute(services *SwapServices, swap *SwapDat
return swap.HandleError(err)
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, labels.ClaimByCoop(swap.GetId().Short()))
if err != nil {
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.ClaimByCoop(swap.GetId().Short()), err)
}
}

return Event_ActionSucceeded
Expand Down
1 change: 1 addition & 0 deletions swap/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Wallet interface {
CreatePreimageSpendingTransaction(swapParams *OpeningParams, claimParams *ClaimParams) (string, string, error)
CreateCsvSpendingTransaction(swapParams *OpeningParams, claimParams *ClaimParams) (txId, txHex string, error error)
CreateCoopSpendingTransaction(swapParams *OpeningParams, claimParams *ClaimParams, takerSigner Signer) (txId, txHex string, error error)
LabelTransaction(txId, label string) error
GetOutputScript(params *OpeningParams) ([]byte, error)
NewAddress() (string, error)
GetRefundFee() (uint64, error)
Expand Down
5 changes: 5 additions & 0 deletions swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ func (s *SwapId) FromString(str string) error {
return nil
}

// Short returns a shortened version of the id suitable for use in observing.
func (s *SwapId) Short() string {
return s.String()[:6]
}

func ParseSwapIdFromString(str string) (*SwapId, error) {
data, err := hex.DecodeString(str)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions swap/swap_out_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ func (d *dummyChain) AddWaitForConfirmationTx(swapId, txId string, vout, startin

}

func (d *dummyChain) LabelTransaction(txId, label string) error {
return nil
}

func (d *dummyChain) AddWaitForCsvTx(swapId, txId string, vout uint32, startingHeight uint32, wantscript []byte) {

}
Expand Down
Loading