From b507ea0ca3af226924dcc9f3c42a199095435889 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 28 Jun 2018 16:47:15 -0700 Subject: [PATCH 1/4] lnwallet/script_utils: expose commit script helpers Makes the helper methods for constructing witness script hash and to-local outputs. This will allow watchtowers to import and reuse this logic when sweeping outputs. --- lnwallet/script_utils.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index b422a30ad..3f68ecbb8 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -46,9 +46,9 @@ const ( maxStateHint uint64 = (1 << 48) - 1 ) -// witnessScriptHash generates a pay-to-witness-script-hash public key script +// WitnessScriptHash generates a pay-to-witness-script-hash public key script // paying to a version 0 witness program paying to the passed redeem script. -func witnessScriptHash(witnessScript []byte) ([]byte, error) { +func WitnessScriptHash(witnessScript []byte) ([]byte, error) { bldr := txscript.NewScriptBuilder() bldr.AddOp(txscript.OP_0) @@ -98,7 +98,7 @@ func GenFundingPkScript(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, erro // With the 2-of-2 script in had, generate a p2wsh script which pays // to the funding script. - pkScript, err := witnessScriptHash(witnessScript) + pkScript, err := WitnessScriptHash(witnessScript) if err != nil { return nil, nil, err } @@ -662,7 +662,7 @@ func createHtlcTimeoutTx(htlcOutput wire.OutPoint, htlcAmt btcutil.Amount, if err != nil { return nil, err } - pkScript, err := witnessScriptHash(witnessScript) + pkScript, err := WitnessScriptHash(witnessScript) if err != nil { return nil, err } @@ -709,7 +709,7 @@ func createHtlcSuccessTx(htlcOutput wire.OutPoint, htlcAmt btcutil.Amount, if err != nil { return nil, err } - pkScript, err := witnessScriptHash(witnessScript) + pkScript, err := WitnessScriptHash(witnessScript) if err != nil { return nil, err } @@ -902,7 +902,7 @@ func lockTimeToSequence(isSeconds bool, locktime uint32) uint32 { return SequenceLockTimeSeconds | (locktime >> 9) } -// commitScriptToSelf constructs the public key script for the output on the +// CommitScriptToSelf constructs the public key script for the output on the // commitment transaction paying to the "owner" of said commitment transaction. // If the other party learns of the preimage to the revocation hash, then they // can claim all the settled funds in the channel, plus the unsettled funds. @@ -919,7 +919,7 @@ func lockTimeToSequence(isSeconds bool, locktime uint32) uint32 { // // OP_ENDIF // OP_CHECKSIG -func commitScriptToSelf(csvTimeout uint32, selfKey, revokeKey *btcec.PublicKey) ([]byte, error) { +func CommitScriptToSelf(csvTimeout uint32, selfKey, revokeKey *btcec.PublicKey) ([]byte, error) { // This script is spendable under two conditions: either the // 'csvTimeout' has passed and we can redeem our funds, or they can // produce a valid signature with the revocation public key. The From bb808a1937fc629693ec34130d70a1f3ef523ddb Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 28 Jun 2018 16:50:45 -0700 Subject: [PATCH 2/4] lnwallet/channel: use public variant of script helpers --- lnwallet/channel.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 50bd723ba..c80651eee 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1412,7 +1412,7 @@ func (lc *LightningChannel) createSignDesc() error { return err } - fundingPkScript, err := witnessScriptHash(multiSigScript) + fundingPkScript, err := WitnessScriptHash(multiSigScript) if err != nil { return err } @@ -1943,13 +1943,13 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, // number so we can have the proper witness script to sign and include // within the final witness. remoteDelay := uint32(chanState.RemoteChanCfg.CsvDelay) - remotePkScript, err := commitScriptToSelf( + remotePkScript, err := CommitScriptToSelf( remoteDelay, keyRing.DelayKey, keyRing.RevocationKey, ) if err != nil { return nil, err } - remoteWitnessHash, err := witnessScriptHash(remotePkScript) + remoteWitnessHash, err := WitnessScriptHash(remotePkScript) if err != nil { return nil, err } @@ -4716,7 +4716,7 @@ func genHtlcScript(isIncoming, ourCommit bool, timeout uint32, rHash [32]byte, // Now that we have the redeem scripts, create the P2WSH public key // script for the output itself. - htlcP2WSH, err := witnessScriptHash(witnessScript) + htlcP2WSH, err := WitnessScriptHash(witnessScript) if err != nil { return nil, nil, err } @@ -5071,7 +5071,7 @@ func newOutgoingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon if err != nil { return nil, err } - htlcScriptHash, err := witnessScriptHash(htlcReceiverScript) + htlcScriptHash, err := WitnessScriptHash(htlcReceiverScript) if err != nil { return nil, err } @@ -5151,7 +5151,7 @@ func newOutgoingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon if err != nil { return nil, err } - htlcScriptHash, err := witnessScriptHash(htlcSweepScript) + htlcScriptHash, err := WitnessScriptHash(htlcSweepScript) if err != nil { return nil, err } @@ -5209,7 +5209,7 @@ func newIncomingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon if err != nil { return nil, err } - htlcScriptHash, err := witnessScriptHash(htlcSenderScript) + htlcScriptHash, err := WitnessScriptHash(htlcSenderScript) if err != nil { return nil, err } @@ -5287,7 +5287,7 @@ func newIncomingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon if err != nil { return nil, err } - htlcScriptHash, err := witnessScriptHash(htlcSweepScript) + htlcScriptHash, err := WitnessScriptHash(htlcSweepScript) if err != nil { return nil, err } @@ -5473,12 +5473,12 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel, signer Signer, commitPoint := ComputeCommitmentPoint(revocation[:]) keyRing := deriveCommitmentKeys(commitPoint, true, &chanState.LocalChanCfg, &chanState.RemoteChanCfg) - selfScript, err := commitScriptToSelf(csvTimeout, keyRing.DelayKey, + selfScript, err := CommitScriptToSelf(csvTimeout, keyRing.DelayKey, keyRing.RevocationKey) if err != nil { return nil, err } - payToUsScriptHash, err := witnessScriptHash(selfScript) + payToUsScriptHash, err := WitnessScriptHash(selfScript) if err != nil { return nil, err } @@ -5894,12 +5894,12 @@ func CreateCommitTx(fundingOutput wire.TxIn, // output after a relative block delay, or the remote node can claim // the funds with the revocation key if we broadcast a revoked // commitment transaction. - ourRedeemScript, err := commitScriptToSelf(csvTimeout, keyRing.DelayKey, + ourRedeemScript, err := CommitScriptToSelf(csvTimeout, keyRing.DelayKey, keyRing.RevocationKey) if err != nil { return nil, err } - payToUsScriptHash, err := witnessScriptHash(ourRedeemScript) + payToUsScriptHash, err := WitnessScriptHash(ourRedeemScript) if err != nil { return nil, err } From a66c4885466427d213fc3bb6c3af358b4dcd0d1a Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 28 Jun 2018 16:51:32 -0700 Subject: [PATCH 3/4] lnwallet/wallet: use public witness script hash helper --- lnwallet/wallet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index db30dad68..581457b84 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -1209,7 +1209,7 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) { // With their signature for our version of the commitment transactions // verified, we can now generate a signature for their version, // allowing the funding transaction to be safely broadcast. - p2wsh, err := witnessScriptHash(witnessScript) + p2wsh, err := WitnessScriptHash(witnessScript) if err != nil { req.err <- err req.completeChan <- nil From 2843df88c062269efc29c8f422d41e566efb7124 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 28 Jun 2018 19:24:44 -0700 Subject: [PATCH 4/4] lnwallet/script_utils_test: use public commit script helpers --- lnwallet/script_utils_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lnwallet/script_utils_test.go b/lnwallet/script_utils_test.go index 32138f2f2..1ffc9c331 100644 --- a/lnwallet/script_utils_test.go +++ b/lnwallet/script_utils_test.go @@ -105,7 +105,7 @@ func TestCommitmentSpendValidation(t *testing.T) { }) // First, we'll test spending with Alice's key after the timeout. - delayScript, err := commitScriptToSelf(csvTimeout, aliceDelayKey, + delayScript, err := CommitScriptToSelf(csvTimeout, aliceDelayKey, revokePubKey) if err != nil { t.Fatalf("unable to generate alice delay script: %v", err) @@ -357,7 +357,7 @@ func TestHTLCSenderSpendValidation(t *testing.T) { if err != nil { t.Fatalf("unable to create htlc sender script: %v", err) } - htlcPkScript, err := witnessScriptHash(htlcWitnessScript) + htlcPkScript, err := WitnessScriptHash(htlcWitnessScript) if err != nil { t.Fatalf("unable to create p2wsh htlc script: %v", err) } @@ -612,7 +612,7 @@ func TestHTLCReceiverSpendValidation(t *testing.T) { if err != nil { t.Fatalf("unable to create htlc sender script: %v", err) } - htlcPkScript, err := witnessScriptHash(htlcWitnessScript) + htlcPkScript, err := WitnessScriptHash(htlcWitnessScript) if err != nil { t.Fatalf("unable to create p2wsh htlc script: %v", err) } @@ -885,7 +885,7 @@ func TestSecondLevelHtlcSpends(t *testing.T) { if err != nil { t.Fatalf("unable to create htlc script: %v", err) } - htlcPkScript, err := witnessScriptHash(htlcWitnessScript) + htlcPkScript, err := WitnessScriptHash(htlcWitnessScript) if err != nil { t.Fatalf("unable to create htlc output: %v", err) }