Merge pull request #70 from bjornoj/master

loop+loopout: validate hash of swap invoice
pull/72/head
Joost Jager 5 years ago committed by GitHub
commit 9924583b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -86,12 +86,12 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
return nil, fmt.Errorf("cannot initiate swap: %v", err) return nil, fmt.Errorf("cannot initiate swap: %v", err)
} }
err = validateLoopOutContract(cfg.lnd, currentHeight, request, swapResp) err = validateLoopOutContract(cfg.lnd, currentHeight, request, swapHash, swapResp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Instantie a struct that contains all required data to start the swap. // Instantiate a struct that contains all required data to start the swap.
initiationTime := time.Now() initiationTime := time.Now()
contract := loopdb.LoopOutContract{ contract := loopdb.LoopOutContract{
@ -660,21 +660,26 @@ func (s *loopOutSwap) sweep(ctx context.Context,
// validateLoopOutContract validates the contract parameters against our // validateLoopOutContract validates the contract parameters against our
// request. // request.
func validateLoopOutContract(lnd *lndclient.LndServices, func validateLoopOutContract(lnd *lndclient.LndServices,
height int32, height int32, request *OutRequest, swapHash lntypes.Hash,
request *OutRequest,
response *newLoopOutResponse) error { response *newLoopOutResponse) error {
// Check invoice amounts. // Check invoice amounts.
chainParams := lnd.ChainParams chainParams := lnd.ChainParams
swapInvoiceAmt, err := swap.GetInvoiceAmt( swapInvoiceHash, swapInvoiceAmt, err := swap.DecodeInvoice(
chainParams, response.swapInvoice, chainParams, response.swapInvoice,
) )
if err != nil { if err != nil {
return err return err
} }
prepayInvoiceAmt, err := swap.GetInvoiceAmt( if swapInvoiceHash != swapHash {
return fmt.Errorf(
"cannot initiate swap, swap invoice hash %v not equal generated swap hash %v",
swapInvoiceHash, swapHash)
}
_, prepayInvoiceAmt, err := swap.DecodeInvoice(
chainParams, response.prepayInvoice, chainParams, response.prepayInvoice,
) )
if err != nil { if err != nil {

@ -5,6 +5,7 @@ import (
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/zpay32" "github.com/lightningnetwork/lnd/zpay32"
) )
@ -26,21 +27,24 @@ func FeeRateAsPercentage(feeRate int64) float64 {
return float64(feeRate) / (FeeRateTotalParts / 100) return float64(feeRate) / (FeeRateTotalParts / 100)
} }
// GetInvoiceAmt gets the invoice amount. It requires an amount to be // DecodeInvoice gets the hash and the amount of an invoice.
// specified. // It requires an amount to be specified.
func GetInvoiceAmt(params *chaincfg.Params, func DecodeInvoice(params *chaincfg.Params,
payReq string) (btcutil.Amount, error) { payReq string) (lntypes.Hash, btcutil.Amount, error) {
swapPayReq, err := zpay32.Decode( swapPayReq, err := zpay32.Decode(
payReq, params, payReq, params,
) )
if err != nil { if err != nil {
return 0, err return lntypes.Hash{}, 0, err
} }
if swapPayReq.MilliSat == nil { if swapPayReq.MilliSat == nil {
return 0, errors.New("no amount in invoice") return lntypes.Hash{}, 0, errors.New("no amount in invoice")
} }
return swapPayReq.MilliSat.ToSatoshis(), nil var hash lntypes.Hash
copy(hash[:], swapPayReq.PaymentHash[:])
return hash, swapPayReq.MilliSat.ToSatoshis(), nil
} }

Loading…
Cancel
Save