mirror of
https://github.com/aljazceru/breez-lnd.git
synced 2025-12-18 22:54:26 +01:00
lnwallet: update script_utils and tests due to SignDescriptor API change
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
|||||||
"golang.org/x/crypto/ripemd160"
|
"golang.org/x/crypto/ripemd160"
|
||||||
|
|
||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
|
||||||
"github.com/roasbeef/btcd/txscript"
|
"github.com/roasbeef/btcd/txscript"
|
||||||
"github.com/roasbeef/btcd/wire"
|
"github.com/roasbeef/btcd/wire"
|
||||||
"github.com/roasbeef/btcutil"
|
"github.com/roasbeef/btcutil"
|
||||||
@@ -312,10 +311,17 @@ func senderHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
|||||||
func SenderHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
func SenderHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
||||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||||
|
|
||||||
|
if signDesc.KeyDesc.PubKey == nil {
|
||||||
|
return nil, fmt.Errorf("cannot generate witness with nil " +
|
||||||
|
"KeyDesc pubkey")
|
||||||
|
}
|
||||||
|
|
||||||
// Derive the revocation key using the local revocation base point and
|
// Derive the revocation key using the local revocation base point and
|
||||||
// commitment point.
|
// commitment point.
|
||||||
revokeKey := DeriveRevocationPubkey(signDesc.PubKey,
|
revokeKey := DeriveRevocationPubkey(
|
||||||
signDesc.DoubleTweak.PubKey())
|
signDesc.KeyDesc.PubKey,
|
||||||
|
signDesc.DoubleTweak.PubKey(),
|
||||||
|
)
|
||||||
|
|
||||||
return senderHtlcSpendRevoke(signer, signDesc, revokeKey, sweepTx)
|
return senderHtlcSpendRevoke(signer, signDesc, revokeKey, sweepTx)
|
||||||
}
|
}
|
||||||
@@ -562,10 +568,17 @@ func receiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
|||||||
func ReceiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
func ReceiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
||||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||||
|
|
||||||
|
if signDesc.KeyDesc.PubKey == nil {
|
||||||
|
return nil, fmt.Errorf("cannot generate witness with nil " +
|
||||||
|
"KeyDesc pubkey")
|
||||||
|
}
|
||||||
|
|
||||||
// Derive the revocation key using the local revocation base point and
|
// Derive the revocation key using the local revocation base point and
|
||||||
// commitment point.
|
// commitment point.
|
||||||
revokeKey := DeriveRevocationPubkey(signDesc.PubKey,
|
revokeKey := DeriveRevocationPubkey(
|
||||||
signDesc.DoubleTweak.PubKey())
|
signDesc.KeyDesc.PubKey,
|
||||||
|
signDesc.DoubleTweak.PubKey(),
|
||||||
|
)
|
||||||
|
|
||||||
return receiverHtlcSpendRevoke(signer, signDesc, revokeKey, sweepTx)
|
return receiverHtlcSpendRevoke(signer, signDesc, revokeKey, sweepTx)
|
||||||
}
|
}
|
||||||
@@ -1023,6 +1036,11 @@ func CommitSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
|||||||
func CommitSpendNoDelay(signer Signer, signDesc *SignDescriptor,
|
func CommitSpendNoDelay(signer Signer, signDesc *SignDescriptor,
|
||||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||||
|
|
||||||
|
if signDesc.KeyDesc.PubKey == nil {
|
||||||
|
return nil, fmt.Errorf("cannot generate witness with nil " +
|
||||||
|
"KeyDesc pubkey")
|
||||||
|
}
|
||||||
|
|
||||||
// This is just a regular p2wkh spend which looks something like:
|
// This is just a regular p2wkh spend which looks something like:
|
||||||
// * witness: <sig> <pubkey>
|
// * witness: <sig> <pubkey>
|
||||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||||
@@ -1037,7 +1055,7 @@ func CommitSpendNoDelay(signer Signer, signDesc *SignDescriptor,
|
|||||||
witness := make([][]byte, 2)
|
witness := make([][]byte, 2)
|
||||||
witness[0] = append(sweepSig, byte(signDesc.HashType))
|
witness[0] = append(sweepSig, byte(signDesc.HashType))
|
||||||
witness[1] = TweakPubKeyWithTweak(
|
witness[1] = TweakPubKeyWithTweak(
|
||||||
signDesc.PubKey, signDesc.SingleTweak,
|
signDesc.KeyDesc.PubKey, signDesc.SingleTweak,
|
||||||
).SerializeCompressed()
|
).SerializeCompressed()
|
||||||
|
|
||||||
return witness, nil
|
return witness, nil
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/keychain"
|
||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
||||||
"github.com/roasbeef/btcd/txscript"
|
"github.com/roasbeef/btcd/txscript"
|
||||||
@@ -112,7 +113,9 @@ func TestCommitmentSpendValidation(t *testing.T) {
|
|||||||
sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout)
|
sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout)
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
WitnessScript: delayScript,
|
WitnessScript: delayScript,
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: aliceCommitTweak,
|
SingleTweak: aliceCommitTweak,
|
||||||
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
||||||
Output: &wire.TxOut{
|
Output: &wire.TxOut{
|
||||||
@@ -143,7 +146,9 @@ func TestCommitmentSpendValidation(t *testing.T) {
|
|||||||
// simulate the scenario when Alice broadcasts this commitment
|
// simulate the scenario when Alice broadcasts this commitment
|
||||||
// transaction after it's been revoked.
|
// transaction after it's been revoked.
|
||||||
signDesc = &SignDescriptor{
|
signDesc = &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
DoubleTweak: commitSecret,
|
DoubleTweak: commitSecret,
|
||||||
WitnessScript: delayScript,
|
WitnessScript: delayScript,
|
||||||
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
||||||
@@ -186,7 +191,9 @@ func TestCommitmentSpendValidation(t *testing.T) {
|
|||||||
t.Fatalf("unable to create bob p2wkh script: %v", err)
|
t.Fatalf("unable to create bob p2wkh script: %v", err)
|
||||||
}
|
}
|
||||||
signDesc = &SignDescriptor{
|
signDesc = &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: bobCommitTweak,
|
SingleTweak: bobCommitTweak,
|
||||||
WitnessScript: bobScriptP2WKH,
|
WitnessScript: bobScriptP2WKH,
|
||||||
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
||||||
@@ -394,7 +401,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
|
|||||||
// that will act as Bob's signature to Alice for the second level HTLC
|
// that will act as Bob's signature to Alice for the second level HTLC
|
||||||
// transaction.
|
// transaction.
|
||||||
bobSignDesc := SignDescriptor{
|
bobSignDesc := SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: bobCommitTweak,
|
SingleTweak: bobCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -416,7 +425,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
|
|||||||
// TODO(roasbeef): test invalid revoke
|
// TODO(roasbeef): test invalid revoke
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
DoubleTweak: commitSecret,
|
DoubleTweak: commitSecret,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -434,7 +445,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
|
|||||||
// HTLC with invalid preimage size
|
// HTLC with invalid preimage size
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: bobCommitTweak,
|
SingleTweak: bobCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -455,7 +468,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
|
|||||||
// TODO(roasbeef): invalid preimage
|
// TODO(roasbeef): invalid preimage
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: bobCommitTweak,
|
SingleTweak: bobCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -475,7 +490,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
|
|||||||
// transaction.
|
// transaction.
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: aliceCommitTweak,
|
SingleTweak: aliceCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -491,6 +508,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(roasbeef): set of cases to ensure able to sign w/ keypath and
|
||||||
|
// not
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
||||||
sweepTx.TxIn[0].Witness = testCase.witness()
|
sweepTx.TxIn[0].Witness = testCase.witness()
|
||||||
|
|
||||||
@@ -639,7 +659,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
|
|||||||
// that will act as Alice's signature to Bob for the second level HTLC
|
// that will act as Alice's signature to Bob for the second level HTLC
|
||||||
// transaction.
|
// transaction.
|
||||||
aliceSignDesc := SignDescriptor{
|
aliceSignDesc := SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: aliceCommitTweak,
|
SingleTweak: aliceCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -661,7 +683,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
|
|||||||
// HTLC redemption w/ invalid preimage size
|
// HTLC redemption w/ invalid preimage size
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: bobCommitTweak,
|
SingleTweak: bobCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -681,7 +705,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
|
|||||||
// HTLC redemption w/ valid preimage size
|
// HTLC redemption w/ valid preimage size
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: bobCommitTweak,
|
SingleTweak: bobCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -700,7 +726,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
|
|||||||
// revoke w/ sig
|
// revoke w/ sig
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
DoubleTweak: commitSecret,
|
DoubleTweak: commitSecret,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -718,7 +746,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
|
|||||||
// refund w/ invalid lock time
|
// refund w/ invalid lock time
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: aliceCommitTweak,
|
SingleTweak: aliceCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -736,7 +766,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
|
|||||||
// refund w/ valid lock time
|
// refund w/ valid lock time
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: aliceCommitTweak,
|
SingleTweak: aliceCommitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -881,7 +913,9 @@ func TestSecondLevelHtlcSpends(t *testing.T) {
|
|||||||
// use the double tweak in this case).
|
// use the double tweak in this case).
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
HashType: txscript.SigHashAll,
|
HashType: txscript.SigHashAll,
|
||||||
@@ -898,7 +932,9 @@ func TestSecondLevelHtlcSpends(t *testing.T) {
|
|||||||
// Sender of HTLC activates the revocation clause.
|
// Sender of HTLC activates the revocation clause.
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: aliceKeyPub,
|
PubKey: aliceKeyPub,
|
||||||
|
},
|
||||||
DoubleTweak: commitSecret,
|
DoubleTweak: commitSecret,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -918,7 +954,9 @@ func TestSecondLevelHtlcSpends(t *testing.T) {
|
|||||||
// blocks instead of 5 blocks).
|
// blocks instead of 5 blocks).
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: commitTweak,
|
SingleTweak: commitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
@@ -938,7 +976,9 @@ func TestSecondLevelHtlcSpends(t *testing.T) {
|
|||||||
// tweak).
|
// tweak).
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
HashType: txscript.SigHashAll,
|
HashType: txscript.SigHashAll,
|
||||||
@@ -956,7 +996,9 @@ func TestSecondLevelHtlcSpends(t *testing.T) {
|
|||||||
// delay, and the correct key.
|
// delay, and the correct key.
|
||||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||||
signDesc := &SignDescriptor{
|
signDesc := &SignDescriptor{
|
||||||
|
KeyDesc: keychain.KeyDescriptor{
|
||||||
PubKey: bobKeyPub,
|
PubKey: bobKeyPub,
|
||||||
|
},
|
||||||
SingleTweak: commitTweak,
|
SingleTweak: commitTweak,
|
||||||
WitnessScript: htlcWitnessScript,
|
WitnessScript: htlcWitnessScript,
|
||||||
Output: htlcOutput,
|
Output: htlcOutput,
|
||||||
|
|||||||
Reference in New Issue
Block a user