Merge pull request #1469 from cfromknecht/expose-lnwallet-commit-scripts

lnwallet: export commit script helper methods
This commit is contained in:
Olaoluwa Osuntokun authored and GitHub committed 2018-07-03 21:53:17 -05:00
commit 184f1e41fc
4 files changed
+24 -24

No files matched your search

+12 -12
View File
@@ -1412,7 +1412,7 @@ func (lc *LightningChannel) createSignDesc() error {
return err return err
} }
fundingPkScript, err := witnessScriptHash(multiSigScript) fundingPkScript, err := WitnessScriptHash(multiSigScript)
if err != nil { if err != nil {
return err 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 // number so we can have the proper witness script to sign and include
// within the final witness. // within the final witness.
remoteDelay := uint32(chanState.RemoteChanCfg.CsvDelay) remoteDelay := uint32(chanState.RemoteChanCfg.CsvDelay)
remotePkScript, err := commitScriptToSelf( remotePkScript, err := CommitScriptToSelf(
remoteDelay, keyRing.DelayKey, keyRing.RevocationKey, remoteDelay, keyRing.DelayKey, keyRing.RevocationKey,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
remoteWitnessHash, err := witnessScriptHash(remotePkScript) remoteWitnessHash, err := WitnessScriptHash(remotePkScript)
if err != nil { if err != nil {
return nil, err 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 // Now that we have the redeem scripts, create the P2WSH public key
// script for the output itself. // script for the output itself.
htlcP2WSH, err := witnessScriptHash(witnessScript) htlcP2WSH, err := WitnessScriptHash(witnessScript)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@@ -5071,7 +5071,7 @@ func newOutgoingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcScriptHash, err := witnessScriptHash(htlcReceiverScript) htlcScriptHash, err := WitnessScriptHash(htlcReceiverScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -5151,7 +5151,7 @@ func newOutgoingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcScriptHash, err := witnessScriptHash(htlcSweepScript) htlcScriptHash, err := WitnessScriptHash(htlcSweepScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -5209,7 +5209,7 @@ func newIncomingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcScriptHash, err := witnessScriptHash(htlcSenderScript) htlcScriptHash, err := WitnessScriptHash(htlcSenderScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -5287,7 +5287,7 @@ func newIncomingHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelCon
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcScriptHash, err := witnessScriptHash(htlcSweepScript) htlcScriptHash, err := WitnessScriptHash(htlcSweepScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -5473,12 +5473,12 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
commitPoint := ComputeCommitmentPoint(revocation[:]) commitPoint := ComputeCommitmentPoint(revocation[:])
keyRing := deriveCommitmentKeys(commitPoint, true, &chanState.LocalChanCfg, keyRing := deriveCommitmentKeys(commitPoint, true, &chanState.LocalChanCfg,
&chanState.RemoteChanCfg) &chanState.RemoteChanCfg)
selfScript, err := commitScriptToSelf(csvTimeout, keyRing.DelayKey, selfScript, err := CommitScriptToSelf(csvTimeout, keyRing.DelayKey,
keyRing.RevocationKey) keyRing.RevocationKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
payToUsScriptHash, err := witnessScriptHash(selfScript) payToUsScriptHash, err := WitnessScriptHash(selfScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -5894,12 +5894,12 @@ func CreateCommitTx(fundingOutput wire.TxIn,
// output after a relative block delay, or the remote node can claim // output after a relative block delay, or the remote node can claim
// the funds with the revocation key if we broadcast a revoked // the funds with the revocation key if we broadcast a revoked
// commitment transaction. // commitment transaction.
ourRedeemScript, err := commitScriptToSelf(csvTimeout, keyRing.DelayKey, ourRedeemScript, err := CommitScriptToSelf(csvTimeout, keyRing.DelayKey,
keyRing.RevocationKey) keyRing.RevocationKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
payToUsScriptHash, err := witnessScriptHash(ourRedeemScript) payToUsScriptHash, err := WitnessScriptHash(ourRedeemScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+7 -7
View File
@@ -46,9 +46,9 @@ const (
maxStateHint uint64 = (1 << 48) - 1 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. // 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 := txscript.NewScriptBuilder()
bldr.AddOp(txscript.OP_0) 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 // With the 2-of-2 script in had, generate a p2wsh script which pays
// to the funding script. // to the funding script.
pkScript, err := witnessScriptHash(witnessScript) pkScript, err := WitnessScriptHash(witnessScript)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@@ -662,7 +662,7 @@ func createHtlcTimeoutTx(htlcOutput wire.OutPoint, htlcAmt btcutil.Amount,
if err != nil { if err != nil {
return nil, err return nil, err
} }
pkScript, err := witnessScriptHash(witnessScript) pkScript, err := WitnessScriptHash(witnessScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -709,7 +709,7 @@ func createHtlcSuccessTx(htlcOutput wire.OutPoint, htlcAmt btcutil.Amount,
if err != nil { if err != nil {
return nil, err return nil, err
} }
pkScript, err := witnessScriptHash(witnessScript) pkScript, err := WitnessScriptHash(witnessScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -902,7 +902,7 @@ func lockTimeToSequence(isSeconds bool, locktime uint32) uint32 {
return SequenceLockTimeSeconds | (locktime >> 9) 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. // commitment transaction paying to the "owner" of said commitment transaction.
// If the other party learns of the preimage to the revocation hash, then they // 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. // can claim all the settled funds in the channel, plus the unsettled funds.
@@ -919,7 +919,7 @@ func lockTimeToSequence(isSeconds bool, locktime uint32) uint32 {
// <timeKey> // <timeKey>
// OP_ENDIF // OP_ENDIF
// OP_CHECKSIG // 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 // This script is spendable under two conditions: either the
// 'csvTimeout' has passed and we can redeem our funds, or they can // 'csvTimeout' has passed and we can redeem our funds, or they can
// produce a valid signature with the revocation public key. The // produce a valid signature with the revocation public key. The
+4 -4
View File
@@ -105,7 +105,7 @@ func TestCommitmentSpendValidation(t *testing.T) {
}) })
// First, we'll test spending with Alice's key after the timeout. // First, we'll test spending with Alice's key after the timeout.
delayScript, err := commitScriptToSelf(csvTimeout, aliceDelayKey, delayScript, err := CommitScriptToSelf(csvTimeout, aliceDelayKey,
revokePubKey) revokePubKey)
if err != nil { if err != nil {
t.Fatalf("unable to generate alice delay script: %v", err) t.Fatalf("unable to generate alice delay script: %v", err)
@@ -357,7 +357,7 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err) t.Fatalf("unable to create htlc sender script: %v", err)
} }
htlcPkScript, err := witnessScriptHash(htlcWitnessScript) htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
if err != nil { if err != nil {
t.Fatalf("unable to create p2wsh htlc script: %v", err) t.Fatalf("unable to create p2wsh htlc script: %v", err)
} }
@@ -612,7 +612,7 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err) t.Fatalf("unable to create htlc sender script: %v", err)
} }
htlcPkScript, err := witnessScriptHash(htlcWitnessScript) htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
if err != nil { if err != nil {
t.Fatalf("unable to create p2wsh htlc script: %v", err) t.Fatalf("unable to create p2wsh htlc script: %v", err)
} }
@@ -885,7 +885,7 @@ func TestSecondLevelHtlcSpends(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to create htlc script: %v", err) t.Fatalf("unable to create htlc script: %v", err)
} }
htlcPkScript, err := witnessScriptHash(htlcWitnessScript) htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
if err != nil { if err != nil {
t.Fatalf("unable to create htlc output: %v", err) t.Fatalf("unable to create htlc output: %v", err)
} }
+1 -1
View File
@@ -1209,7 +1209,7 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
// With their signature for our version of the commitment transactions // With their signature for our version of the commitment transactions
// verified, we can now generate a signature for their version, // verified, we can now generate a signature for their version,
// allowing the funding transaction to be safely broadcast. // allowing the funding transaction to be safely broadcast.
p2wsh, err := witnessScriptHash(witnessScript) p2wsh, err := WitnessScriptHash(witnessScript)
if err != nil { if err != nil {
req.err <- err req.err <- err
req.completeChan <- nil req.completeChan <- nil