Skip to content

Commit

Permalink
lnd: label the tx to make it easier to audit
Browse files Browse the repository at this point in the history
Ensure that all lnd transactions related to peerswaps are identifiable
by the label `peerswap`.
This makes it easier to audit the transactions from faraday.
This is performed by LND's LabelTransaction RPC.
Since CLN has no such function, no label is assigned.

#254
  • Loading branch information
YusukeShimizu committed Feb 11, 2024
1 parent aef7098 commit b07c2b3
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions clightning/clightning_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func (cl *ClightningClient) CreateCoopSpendingTransaction(swapParams *swap.Openi
return spendingTx.TxHash().String(), txHex, nil
}

func (cl *ClightningClient) LabelTransaction(txId, label string) error {
// todo implement
return nil
}

func (cl *ClightningClient) NewAddress() (string, error) {
newAddr, err := cl.glightning.NewAddr()
if err != nil {
Expand Down
14 changes: 14 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,19 @@ func (l *Client) CreateCoopSpendingTransaction(swapParams *swap.OpeningParams, c
return spendingTx.TxHash().String(), txHex, nil
}

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})
return err
}

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

func (b *BitcoinOnChain) LabelTransaction(txID, label string) error {
// TODO: implement label transaction
return nil
}

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

func (l *LiquidOnChain) LabelTransaction(txID, label string) error {
// TODO: implement label transaction
return nil
}

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

const (
BitcoinCsv = 1008
LiquidCsv = 60
BitcoinCsv = 1008
LiquidCsv = 60
peerswapLabel = "peerswap"
)

type CheckRequestWrapperAction struct {
Expand Down Expand Up @@ -194,6 +195,11 @@ func (s *ClaimSwapTransactionWithPreimageAction) Execute(services *SwapServices,
return Event_OnRetry
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, peerswapLabel)
if err != nil {
log.Infof("Error labeling trnasaction %v", err)
return Event_OnRetry
}
}

return Event_ActionSucceeded
Expand Down Expand Up @@ -249,6 +255,10 @@ func (c *CreateAndBroadcastOpeningTransaction) Execute(services *SwapServices, s
// todo: idempotent states
return swap.HandleError(err)
}
err = wallet.LabelTransaction(txId, peerswapLabel)
if err != nil {
return swap.HandleError(err)
}
startingHeight, err := txWatcher.GetBlockHeight()
if err != nil {
return swap.HandleError(err)
Expand Down Expand Up @@ -437,6 +447,11 @@ func (c *ClaimSwapTransactionWithCsv) Execute(services *SwapServices, swap *Swap
return Event_OnRetry
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, peerswapLabel)
if err != nil {
swap.HandleError(err)
return Event_OnRetry
}
}

return Event_ActionSucceeded
Expand All @@ -463,6 +478,10 @@ func (c *ClaimSwapTransactionCoop) Execute(services *SwapServices, swap *SwapDat
return swap.HandleError(err)
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, peerswapLabel)
if err != nil {
return swap.HandleError(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
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

0 comments on commit b07c2b3

Please sign in to comment.