From 0a0b5f89c95e6b1382122322316710bc62a6403f Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 26 Apr 2021 15:29:59 +0200 Subject: [PATCH] input: create IsHtlcSpendRevoke --- input/script_utils.go | 60 ++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/input/script_utils.go b/input/script_utils.go index dc3c4ccf..7ce6fff5 100644 --- a/input/script_utils.go +++ b/input/script_utils.go @@ -313,21 +313,32 @@ func SenderHtlcSpendRevokeWithKey(signer Signer, signDesc *SignDescriptor, func SenderHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor, sweepTx *wire.MsgTx) (wire.TxWitness, error) { - if signDesc.KeyDesc.PubKey == nil { - return nil, fmt.Errorf("cannot generate witness with nil " + - "KeyDesc pubkey") + revokeKey, err := deriveRevokePubKey(signDesc) + if err != nil { + return nil, err } - // Derive the revocation key using the local revocation base point and - // commitment point. - revokeKey := DeriveRevocationPubkey( - signDesc.KeyDesc.PubKey, - signDesc.DoubleTweak.PubKey(), - ) - return SenderHtlcSpendRevokeWithKey(signer, signDesc, revokeKey, sweepTx) } +// IsHtlcSpendRevoke is used to determine if the passed spend is spending a +// HTLC output using the revocation key. +func IsHtlcSpendRevoke(txIn *wire.TxIn, signDesc *SignDescriptor) ( + bool, error) { + + revokeKey, err := deriveRevokePubKey(signDesc) + if err != nil { + return false, err + } + + if len(txIn.Witness) == 3 && + bytes.Equal(txIn.Witness[1], revokeKey.SerializeCompressed()) { + return true, nil + } + + return false, nil +} + // SenderHtlcSpendRedeem constructs a valid witness allowing the receiver of an // HTLC to redeem the pending output in the scenario that the sender broadcasts // their version of the commitment transaction. A valid spend requires @@ -575,16 +586,7 @@ func ReceiverHtlcSpendRevokeWithKey(signer Signer, signDesc *SignDescriptor, return witnessStack, nil } -// ReceiverHtlcSpendRevoke constructs a valid witness allowing the sender of an -// HTLC within a previously revoked commitment transaction to re-claim the -// pending funds in the case that the receiver broadcasts this revoked -// commitment transaction. This method first derives the appropriate revocation -// key, and requires that the provided SignDescriptor has a local revocation -// basepoint and commitment secret in the PubKey and DoubleTweak fields, -// respectively. -func ReceiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor, - sweepTx *wire.MsgTx) (wire.TxWitness, error) { - +func deriveRevokePubKey(signDesc *SignDescriptor) (*btcec.PublicKey, error) { if signDesc.KeyDesc.PubKey == nil { return nil, fmt.Errorf("cannot generate witness with nil " + "KeyDesc pubkey") @@ -597,6 +599,24 @@ func ReceiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor, signDesc.DoubleTweak.PubKey(), ) + return revokeKey, nil +} + +// ReceiverHtlcSpendRevoke constructs a valid witness allowing the sender of an +// HTLC within a previously revoked commitment transaction to re-claim the +// pending funds in the case that the receiver broadcasts this revoked +// commitment transaction. This method first derives the appropriate revocation +// key, and requires that the provided SignDescriptor has a local revocation +// basepoint and commitment secret in the PubKey and DoubleTweak fields, +// respectively. +func ReceiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor, + sweepTx *wire.MsgTx) (wire.TxWitness, error) { + + revokeKey, err := deriveRevokePubKey(signDesc) + if err != nil { + return nil, err + } + return ReceiverHtlcSpendRevokeWithKey(signer, signDesc, revokeKey, sweepTx) }