From f12dfe2c452c9bba770c5835410a81dda00fc5f3 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 6 Nov 2017 14:22:00 +0100 Subject: [PATCH 1/4] lnwallet/btcwallet: Use signDesc.HashType when signing Tis commit makes the btcwallet signer implementation use signDesc.HashType instead of SigHashAll when signing transactions. This will allow the creator of the transaction to specify the sighash policy when creating the accompanying sign descriptior. --- lnwallet/btcwallet/signer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lnwallet/btcwallet/signer.go b/lnwallet/btcwallet/signer.go index 82cb6929..d8e01b4e 100644 --- a/lnwallet/btcwallet/signer.go +++ b/lnwallet/btcwallet/signer.go @@ -141,7 +141,7 @@ func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx, amt := signDesc.Output.Value sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, - signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll, + signDesc.InputIndex, amt, witnessScript, signDesc.HashType, privKey) if err != nil { return nil, err @@ -227,7 +227,7 @@ func (b *BtcWallet) ComputeInputScript(tx *wire.MsgTx, // TODO(roasbeef): adhere to passed HashType witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes, signDesc.InputIndex, signDesc.Output.Value, witnessProgram, - txscript.SigHashAll, privKey, true) + signDesc.HashType, privKey, true) if err != nil { return nil, err } From dd4996b4d504895f9f6d451fab4e05deec2002c9 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 6 Nov 2017 14:25:01 +0100 Subject: [PATCH 2/4] lnwallet: use signDesc.HashType for sweepsig in script_utils This commit changes the use of SigHash flags in the spend transactions created in scrit_utils. Instead of always using SigHashAll for the sweep signature, we instead use the sighash flag specified by the passed sign descriptor. --- lnwallet/script_utils.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index 20d9fdb7..74276053 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -294,7 +294,7 @@ func senderHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor, // manner in order to encode the revocation contract into a sig+key // pair. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = revokeKey.SerializeCompressed() witnessStack[2] = signDesc.WitnessScript @@ -336,7 +336,7 @@ func senderHtlcSpendRedeem(signer Signer, signDesc *SignDescriptor, // generated above under the receiver's public key, and the payment // pre-image. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = paymentPreimage witnessStack[2] = signDesc.WitnessScript @@ -362,7 +362,7 @@ func senderHtlcSpendTimeout(receiverSig []byte, signer Signer, witnessStack := wire.TxWitness(make([][]byte, 5)) witnessStack[0] = nil witnessStack[1] = append(receiverSig, byte(txscript.SigHashAll)) - witnessStack[2] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[2] = append(sweepSig, byte(signDesc.HashType)) witnessStack[3] = nil witnessStack[4] = signDesc.WitnessScript @@ -516,7 +516,7 @@ func receiverHtlcSpendRedeem(senderSig, paymentPreimage []byte, witnessStack := wire.TxWitness(make([][]byte, 5)) witnessStack[0] = nil witnessStack[1] = append(senderSig, byte(txscript.SigHashAll)) - witnessStack[2] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[2] = append(sweepSig, byte(signDesc.HashType)) witnessStack[3] = paymentPreimage witnessStack[4] = signDesc.WitnessScript @@ -543,7 +543,7 @@ func receiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor, // witness stack in order to force script execution to the HTLC // revocation clause. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = revokeKey.SerializeCompressed() witnessStack[2] = signDesc.WitnessScript @@ -594,7 +594,7 @@ func receiverHtlcSpendTimeout(signer Signer, signDesc *SignDescriptor, } witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = nil witnessStack[2] = signDesc.WitnessScript @@ -801,7 +801,7 @@ func htlcSpendSuccess(signer Signer, signDesc *SignDescriptor, // witness script), in order to force execution to the second portion // of the if clause. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = nil witnessStack[2] = signDesc.WitnessScript @@ -826,7 +826,7 @@ func htlcSpendRevoke(signer Signer, signDesc *SignDescriptor, // witness script), in order to force execution to the revocation // clause in the second level HTLC script. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = []byte{1} witnessStack[2] = signDesc.WitnessScript @@ -942,7 +942,7 @@ func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor, // place an empty byte in order to ensure our script is still valid // from the PoV of nodes that are enforcing minimal OP_IF/OP_NOTIF. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = nil witnessStack[2] = signDesc.WitnessScript @@ -967,7 +967,7 @@ func CommitSpendRevoke(signer Signer, signDesc *SignDescriptor, // Place a 1 as the first item in the evaluated witness stack to // force script execution to the revocation clause. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) + witnessStack[0] = append(sweepSig, byte(signDesc.HashType)) witnessStack[1] = []byte{1} witnessStack[2] = signDesc.WitnessScript @@ -995,7 +995,7 @@ func CommitSpendNoDelay(signer Signer, signDesc *SignDescriptor, // we use the tweaked public key as the last item in the witness stack // which was originally used to created the pkScript we're spending. witness := make([][]byte, 2) - witness[0] = append(sweepSig, byte(txscript.SigHashAll)) + witness[0] = append(sweepSig, byte(signDesc.HashType)) witness[1] = TweakPubKeyWithTweak( signDesc.PubKey, signDesc.SingleTweak, ).SerializeCompressed() From df17a680c419a77142fb81c64dec30dd29cb8ecf Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 6 Nov 2017 14:27:58 +0100 Subject: [PATCH 3/4] htlcswitch test: use signDesc.HashType instead of SigHashAll in mock signer. --- htlcswitch/mock.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 2c37c480..a420f360 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -456,7 +456,7 @@ func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescri } sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, - signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll, + signDesc.InputIndex, amt, witnessScript, signDesc.HashType, privKey) if err != nil { return nil, err @@ -482,7 +482,7 @@ func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *lnwallet.SignD witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes, signDesc.InputIndex, signDesc.Output.Value, signDesc.Output.PkScript, - txscript.SigHashAll, privKey, true) + signDesc.HashType, privKey, true) if err != nil { return nil, err } From f5e324684f11f85e937792377335d3c994a15ece Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 6 Nov 2017 14:29:05 +0100 Subject: [PATCH 4/4] test: use signDesc.HashType instead of SigHashAll in mockSigner --- mock.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mock.go b/mock.go index 63e4893d..daee7029 100644 --- a/mock.go +++ b/mock.go @@ -25,7 +25,7 @@ func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, privKey := m.key sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, - signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll, + signDesc.InputIndex, amt, witnessScript, signDesc.HashType, privKey) if err != nil { return nil, err @@ -38,7 +38,7 @@ func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) { witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes, signDesc.InputIndex, signDesc.Output.Value, - signDesc.Output.PkScript, txscript.SigHashAll, m.key, true) + signDesc.Output.PkScript, signDesc.HashType, m.key, true) if err != nil { return nil, err }