lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
package lnwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-16 02:56:25 +01:00
|
|
|
"crypto/sha256"
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2017-01-14 09:17:00 +01:00
|
|
|
"time"
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestCommitmentSpendValidation test the spendability of both outputs within
|
|
|
|
// the commitment transaction.
|
|
|
|
//
|
|
|
|
// The following spending cases are covered by this test:
|
2016-10-16 01:02:09 +02:00
|
|
|
// * Alice's spend from the delayed output on her commitment transaction.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// * Bob's spend from Alice's delayed output when she broadcasts a revoked
|
|
|
|
// commitment transaction.
|
|
|
|
// * Bob's spend from his unencumbered output within Alice's commitment
|
|
|
|
// transaction.
|
|
|
|
func TestCommitmentSpendValidation(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-10-16 01:02:09 +02:00
|
|
|
// We generate a fake output, and the corresponding txin. This output
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// doesn't need to exist, as we'll only be validating spending from the
|
|
|
|
// transaction that references this.
|
|
|
|
fundingOut := &wire.OutPoint{
|
|
|
|
Hash: testHdSeed,
|
|
|
|
Index: 50,
|
|
|
|
}
|
|
|
|
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
|
|
|
|
|
|
|
// We also set up set some resources for the commitment transaction.
|
|
|
|
// Each side currently has 1 BTC within the channel, with a total
|
|
|
|
// channel capacity of 2BTC.
|
|
|
|
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
testWalletPrivKey)
|
|
|
|
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
bobsPrivKey)
|
|
|
|
channelBalance := btcutil.Amount(1 * 10e8)
|
|
|
|
csvTimeout := uint32(5)
|
2016-06-30 21:12:01 +02:00
|
|
|
revocationPreimage := testHdSeed[:]
|
2016-08-13 00:37:35 +02:00
|
|
|
revokePubKey := DeriveRevocationPubkey(bobKeyPub, revocationPreimage)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
|
2016-09-09 02:04:08 +02:00
|
|
|
aliceSelfOutputSigner := &mockSigner{aliceKeyPriv}
|
|
|
|
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// With all the test data set up, we create the commitment transaction.
|
|
|
|
// We only focus on a single party's transactions, as the scripts are
|
|
|
|
// identical with the roles reversed.
|
|
|
|
//
|
|
|
|
// This is Alice's commitment transaction, so she must wait a CSV delay
|
|
|
|
// of 5 blocks before sweeping the output, while bob can spend
|
|
|
|
// immediately with either the revocation key, or his regular key.
|
2016-08-13 00:37:35 +02:00
|
|
|
commitmentTx, err := CreateCommitTx(fakeFundingTxIn, aliceKeyPub,
|
2017-02-22 11:11:34 +01:00
|
|
|
bobKeyPub, revokePubKey, csvTimeout, channelBalance,
|
|
|
|
channelBalance, DefaultDustLimit())
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create commitment transaction: %v", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
delayOutput := commitmentTx.TxOut[0]
|
|
|
|
regularOutput := commitmentTx.TxOut[1]
|
|
|
|
|
|
|
|
// We're testing an uncooperative close, output sweep, so construct a
|
|
|
|
// transaction which sweeps the funds to a random address.
|
|
|
|
targetOutput, err := commitScriptUnencumbered(aliceKeyPub)
|
|
|
|
if err != nil {
|
2017-02-10 00:28:32 +01:00
|
|
|
t.Fatalf("unable to create target output: %v", err)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
}
|
2017-01-05 22:56:27 +01:00
|
|
|
sweepTx := wire.NewMsgTx(2)
|
2017-02-10 00:28:32 +01:00
|
|
|
sweepTx.AddTxIn(wire.NewTxIn(&wire.OutPoint{
|
|
|
|
Hash: commitmentTx.TxHash(),
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
nil),
|
|
|
|
)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
sweepTx.AddTxOut(&wire.TxOut{
|
|
|
|
PkScript: targetOutput,
|
|
|
|
Value: 0.5 * 10e8,
|
|
|
|
})
|
|
|
|
|
|
|
|
// First, we'll test spending with Alice's key after the timeout.
|
|
|
|
delayScript, err := commitScriptToSelf(csvTimeout, aliceKeyPub, revokePubKey)
|
|
|
|
if err != nil {
|
2017-02-10 00:28:32 +01:00
|
|
|
t.Fatalf("unable to generate alice delay script: %v", err)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
}
|
2016-09-10 22:48:36 +02:00
|
|
|
sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout)
|
2016-09-09 02:04:08 +02:00
|
|
|
signDesc := &SignDescriptor{
|
2016-10-16 01:02:09 +02:00
|
|
|
WitnessScript: delayScript,
|
2016-11-15 03:34:59 +01:00
|
|
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
2016-09-09 02:04:08 +02:00
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: int64(channelBalance),
|
|
|
|
},
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
InputIndex: 0,
|
|
|
|
}
|
|
|
|
aliceWitnessSpend, err := CommitSpendTimeout(aliceSelfOutputSigner,
|
2016-09-10 22:48:36 +02:00
|
|
|
signDesc, sweepTx)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
if err != nil {
|
2017-02-10 00:28:32 +01:00
|
|
|
t.Fatalf("unable to generate delay commit spend witness: %v", err)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
}
|
|
|
|
sweepTx.TxIn[0].Witness = aliceWitnessSpend
|
|
|
|
vm, err := txscript.NewEngine(delayOutput.PkScript,
|
|
|
|
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
2016-09-10 22:48:36 +02:00
|
|
|
nil, int64(channelBalance))
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
if err := vm.Execute(); err != nil {
|
|
|
|
t.Fatalf("spend from delay output is invalid: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll test bob spending with the derived revocation key to
|
2016-11-19 00:23:26 +01:00
|
|
|
// simulate the scenario when alice broadcasts this commitment
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// transaction after it's been revoked.
|
2016-08-13 00:37:35 +02:00
|
|
|
revokePrivKey := DeriveRevocationPrivKey(bobKeyPriv, revocationPreimage)
|
2016-11-19 00:23:26 +01:00
|
|
|
bobRevokeSigner := &mockSigner{revokePrivKey}
|
|
|
|
signDesc = &SignDescriptor{
|
|
|
|
WitnessScript: delayScript,
|
|
|
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: int64(channelBalance),
|
|
|
|
},
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
InputIndex: 0,
|
|
|
|
}
|
|
|
|
bobWitnessSpend, err := CommitSpendRevoke(bobRevokeSigner, signDesc,
|
|
|
|
sweepTx)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate revocation witness: %v", err)
|
|
|
|
}
|
|
|
|
sweepTx.TxIn[0].Witness = bobWitnessSpend
|
|
|
|
vm, err = txscript.NewEngine(delayOutput.PkScript,
|
|
|
|
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
|
|
|
nil, int64(channelBalance))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
if err := vm.Execute(); err != nil {
|
|
|
|
t.Fatalf("revocation spend is invalid: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we test bob sweeping his output as normal in the case that
|
|
|
|
// alice broadcasts this commitment transaction.
|
2016-11-19 00:23:26 +01:00
|
|
|
bobSigner := &mockSigner{bobKeyPriv}
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
bobScriptp2wkh, err := commitScriptUnencumbered(bobKeyPub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create bob p2wkh script: %v", err)
|
|
|
|
}
|
2016-11-19 00:23:26 +01:00
|
|
|
signDesc = &SignDescriptor{
|
|
|
|
WitnessScript: bobScriptp2wkh,
|
|
|
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: int64(channelBalance),
|
|
|
|
PkScript: bobScriptp2wkh,
|
|
|
|
},
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
InputIndex: 0,
|
|
|
|
}
|
|
|
|
bobRegularSpend, err := CommitSpendNoDelay(bobSigner, signDesc,
|
|
|
|
sweepTx)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create bob regular spend: %v", err)
|
|
|
|
}
|
|
|
|
sweepTx.TxIn[0].Witness = bobRegularSpend
|
|
|
|
vm, err = txscript.NewEngine(regularOutput.PkScript,
|
|
|
|
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
|
|
|
nil, int64(channelBalance))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
if err := vm.Execute(); err != nil {
|
|
|
|
t.Fatalf("bob p2wkh spend is invalid: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRevocationKeyDerivation tests that given a public key, and a revocation
|
|
|
|
// hash, the homomorphic revocation public and private key derivation work
|
|
|
|
// properly.
|
|
|
|
func TestRevocationKeyDerivation(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
revocationPreimage := testHdSeed[:]
|
|
|
|
|
|
|
|
priv, pub := btcec.PrivKeyFromBytes(btcec.S256(), testWalletPrivKey)
|
|
|
|
|
2016-08-13 00:37:35 +02:00
|
|
|
revocationPub := DeriveRevocationPubkey(pub, revocationPreimage)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
|
2016-08-13 00:37:35 +02:00
|
|
|
revocationPriv := DeriveRevocationPrivKey(priv, revocationPreimage)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
x, y := btcec.S256().ScalarBaseMult(revocationPriv.D.Bytes())
|
|
|
|
derivedRevPub := &btcec.PublicKey{
|
|
|
|
Curve: btcec.S256(),
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
}
|
|
|
|
|
|
|
|
// The the revocation public key derived from the original public key,
|
|
|
|
// and the one derived from the private key should be identical.
|
|
|
|
if !revocationPub.IsEqual(derivedRevPub) {
|
|
|
|
t.Fatalf("derived public keys don't match!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:31:29 +02:00
|
|
|
// makeWitnessTestCase is a helper function used within test cases involving
|
|
|
|
// the validity of a crafted witness. This function is a wrapper function which
|
|
|
|
// allows constructing table-driven tests. In the case of an error while
|
|
|
|
// constructing the witness, the test fails fataly.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
func makeWitnessTestCase(t *testing.T, f func() (wire.TxWitness, error)) func() wire.TxWitness {
|
|
|
|
return func() wire.TxWitness {
|
|
|
|
witness, err := f()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create witness test case: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return witness
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHTLCSenderSpendValidation tests all possible valid+invalid redemption
|
|
|
|
// paths in the script used within the sender's commitment transaction for an
|
|
|
|
// outgoing HTLC.
|
|
|
|
//
|
|
|
|
// The following cases are exercised by this test:
|
|
|
|
// sender script:
|
2017-01-13 06:01:50 +01:00
|
|
|
// * receiver spends
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// * revoke w/ sig
|
2017-01-13 06:01:50 +01:00
|
|
|
// * HTLC with invalid preimage size
|
|
|
|
// * HTLC with valid preimage size + sig
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// * sender spends
|
|
|
|
// * invalid lock-time for CLTV
|
|
|
|
// * invalid sequence for CSV
|
|
|
|
// * valid lock-time+sequence, valid sig
|
|
|
|
func TestHTLCSenderSpendValidation(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-06-29 20:31:29 +02:00
|
|
|
// TODO(roasbeef): eliminate duplication with other HTLC tests.
|
|
|
|
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// We generate a fake output, and the coresponding txin. This output
|
|
|
|
// doesn't need to exist, as we'll only be validating spending from the
|
|
|
|
// transaction that references this.
|
|
|
|
fundingOut := &wire.OutPoint{
|
|
|
|
Hash: testHdSeed,
|
|
|
|
Index: 50,
|
|
|
|
}
|
|
|
|
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
|
|
|
|
2017-01-13 06:01:50 +01:00
|
|
|
// Generate a payment and revocation preimage to be used below.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
revokePreimage := testHdSeed[:]
|
2017-03-16 02:56:25 +01:00
|
|
|
revokeHash := sha256.Sum256(revokePreimage)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
paymentPreimage := revokeHash
|
|
|
|
paymentPreimage[0] ^= 1
|
2017-03-16 02:56:25 +01:00
|
|
|
paymentHash := sha256.Sum256(paymentPreimage[:])
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
|
2017-01-13 06:01:50 +01:00
|
|
|
// We'll also need some tests keys for alice and bob, and metadata of
|
2016-06-29 20:31:29 +02:00
|
|
|
// the HTLC output.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
testWalletPrivKey)
|
|
|
|
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
bobsPrivKey)
|
|
|
|
paymentAmt := btcutil.Amount(1 * 10e8)
|
|
|
|
cltvTimeout := uint32(8)
|
|
|
|
csvTimeout := uint32(5)
|
|
|
|
|
2016-06-29 20:31:29 +02:00
|
|
|
// Generate the raw HTLC redemption scripts, and its p2wsh counterpart.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
htlcScript, err := senderHTLCScript(cltvTimeout, csvTimeout,
|
|
|
|
aliceKeyPub, bobKeyPub, revokeHash[:], paymentHash[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create htlc sender script: %v", err)
|
|
|
|
}
|
|
|
|
htlcWitnessScript, err := witnessScriptHash(htlcScript)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create p2wsh htlc script: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will be Alice's commitment transaction. In this scenario Alice
|
|
|
|
// is sending an HTLC to a node she has a a path to (could be Bob,
|
|
|
|
// could be multiple hops down, it doesn't really matter).
|
2017-01-05 22:56:27 +01:00
|
|
|
senderCommitTx := wire.NewMsgTx(2)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
senderCommitTx.AddTxIn(fakeFundingTxIn)
|
|
|
|
senderCommitTx.AddTxOut(&wire.TxOut{
|
|
|
|
Value: int64(paymentAmt),
|
|
|
|
PkScript: htlcWitnessScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
prevOut := &wire.OutPoint{
|
2017-01-05 22:56:27 +01:00
|
|
|
Hash: senderCommitTx.TxHash(),
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:56:27 +01:00
|
|
|
sweepTx := wire.NewMsgTx(2)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil))
|
|
|
|
sweepTx.AddTxOut(
|
|
|
|
&wire.TxOut{
|
|
|
|
PkScript: []byte("doesn't matter"),
|
|
|
|
Value: 1 * 10e8,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
witness func() wire.TxWitness
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// revoke w/ sig
|
|
|
|
// TODO(roasbeef): test invalid revoke
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return senderHtlcSpendRevoke(htlcScript, paymentAmt,
|
|
|
|
bobKeyPriv, sweepTx,
|
|
|
|
revokePreimage)
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2017-01-13 06:01:50 +01:00
|
|
|
// HTLC with invalid preimage size
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return senderHtlcSpendRedeem(htlcScript, paymentAmt,
|
|
|
|
bobKeyPriv, sweepTx,
|
2017-01-13 06:01:50 +01:00
|
|
|
// Invalid preimage length
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
bytes.Repeat([]byte{1}, 45))
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2017-01-13 06:01:50 +01:00
|
|
|
// HTLC with valid preimage size + sig
|
|
|
|
// TODO(roabeef): invalid preimage
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return senderHtlcSpendRedeem(htlcScript, paymentAmt,
|
|
|
|
bobKeyPriv, sweepTx,
|
|
|
|
paymentPreimage[:])
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// invalid lock-time for CLTV
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return senderHtlcSpendTimeout(htlcScript, paymentAmt,
|
|
|
|
aliceKeyPriv, sweepTx, cltvTimeout-2, csvTimeout)
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// invalid sequence for CSV
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return senderHtlcSpendTimeout(htlcScript, paymentAmt,
|
|
|
|
aliceKeyPriv, sweepTx, cltvTimeout, csvTimeout-2)
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// valid lock-time+sequence, valid sig
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return senderHtlcSpendTimeout(htlcScript, paymentAmt,
|
|
|
|
aliceKeyPriv, sweepTx, cltvTimeout, csvTimeout)
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
sweepTx.TxIn[0].Witness = testCase.witness()
|
|
|
|
|
|
|
|
vm, err := txscript.NewEngine(htlcWitnessScript,
|
|
|
|
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
|
|
|
nil, int64(paymentAmt))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:31:29 +02:00
|
|
|
// This buffer will trace execution of the Script, only dumping
|
|
|
|
// out to stdout in the case that a test fails.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
var debugBuf bytes.Buffer
|
|
|
|
|
|
|
|
done := false
|
|
|
|
for !done {
|
|
|
|
dis, err := vm.DisasmPC()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("stepping (%v)\n", err)
|
|
|
|
}
|
|
|
|
debugBuf.WriteString(fmt.Sprintf("stepping %v\n", dis))
|
|
|
|
|
|
|
|
done, err = vm.Step()
|
|
|
|
if err != nil && testCase.valid {
|
|
|
|
fmt.Println(debugBuf.String())
|
|
|
|
t.Fatalf("spend test case #%v failed, spend should be valid: %v", i, err)
|
|
|
|
} else if err == nil && !testCase.valid && done {
|
|
|
|
fmt.Println(debugBuf.String())
|
|
|
|
t.Fatalf("spend test case #%v succeed, spend should be invalid: %v", i, err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 00:28:32 +01:00
|
|
|
debugBuf.WriteString(fmt.Sprintf("Stack: %v", vm.GetStack()))
|
|
|
|
debugBuf.WriteString(fmt.Sprintf("AltStack: %v", vm.GetAltStack()))
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHTLCReceiverSpendValidation tests all possible valid+invalid redemption
|
2017-01-13 06:01:50 +01:00
|
|
|
// paths in the script used within the receiver's commitment transaction for an
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// incoming HTLC.
|
|
|
|
//
|
|
|
|
// The following cases are exercised by this test:
|
2017-01-13 06:01:50 +01:00
|
|
|
// * receiver spends
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// * HTLC redemption w/ invalid preimage size
|
|
|
|
// * HTLC redemption w/ invalid sequence
|
|
|
|
// * HTLC redemption w/ valid preimage size
|
|
|
|
// * sender spends
|
|
|
|
// * revoke w/ sig
|
|
|
|
// * refund w/ invalid lock time
|
|
|
|
// * refund w/ valid lock time
|
|
|
|
func TestHTLCReceiverSpendValidation(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
// We generate a fake output, and the coresponding txin. This output
|
|
|
|
// doesn't need to exist, as we'll only be validating spending from the
|
|
|
|
// transaction that references this.
|
|
|
|
fundingOut := &wire.OutPoint{
|
|
|
|
Hash: testHdSeed,
|
|
|
|
Index: 50,
|
|
|
|
}
|
|
|
|
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
|
|
|
|
2017-01-13 06:01:50 +01:00
|
|
|
// Generate a payment and revocation preimage to be used below.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
revokePreimage := testHdSeed[:]
|
2017-03-16 02:56:25 +01:00
|
|
|
revokeHash := sha256.Sum256(revokePreimage)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
paymentPreimage := revokeHash
|
|
|
|
paymentPreimage[0] ^= 1
|
2017-03-16 02:56:25 +01:00
|
|
|
paymentHash := sha256.Sum256(paymentPreimage[:])
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
|
2017-01-13 06:01:50 +01:00
|
|
|
// We'll also need some tests keys for alice and bob, and metadata of
|
2016-06-29 20:31:29 +02:00
|
|
|
// the HTLC output.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
testWalletPrivKey)
|
|
|
|
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
bobsPrivKey)
|
|
|
|
paymentAmt := btcutil.Amount(1 * 10e8)
|
|
|
|
cltvTimeout := uint32(8)
|
|
|
|
csvTimeout := uint32(5)
|
|
|
|
|
2016-06-29 20:31:29 +02:00
|
|
|
// Generate the raw HTLC redemption scripts, and its p2wsh counterpart.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
htlcScript, err := receiverHTLCScript(cltvTimeout, csvTimeout,
|
|
|
|
aliceKeyPub, bobKeyPub, revokeHash[:], paymentHash[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create htlc sender script: %v", err)
|
|
|
|
}
|
|
|
|
htlcWitnessScript, err := witnessScriptHash(htlcScript)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create p2wsh htlc script: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will be Bob's commitment transaction. In this scenario Alice
|
|
|
|
// is sending an HTLC to a node she has a a path to (could be Bob,
|
|
|
|
// could be multiple hops down, it doesn't really matter).
|
2017-01-13 06:01:50 +01:00
|
|
|
receiverCommitTx := wire.NewMsgTx(2)
|
|
|
|
receiverCommitTx.AddTxIn(fakeFundingTxIn)
|
|
|
|
receiverCommitTx.AddTxOut(&wire.TxOut{
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
Value: int64(paymentAmt),
|
|
|
|
PkScript: htlcWitnessScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
prevOut := &wire.OutPoint{
|
2017-01-13 06:01:50 +01:00
|
|
|
Hash: receiverCommitTx.TxHash(),
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:56:27 +01:00
|
|
|
sweepTx := wire.NewMsgTx(2)
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil))
|
|
|
|
sweepTx.AddTxOut(
|
|
|
|
&wire.TxOut{
|
|
|
|
PkScript: []byte("doesn't matter"),
|
|
|
|
Value: 1 * 10e8,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
witness func() wire.TxWitness
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// HTLC redemption w/ invalid preimage size
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return receiverHtlcSpendRedeem(htlcScript,
|
|
|
|
paymentAmt, bobKeyPriv, sweepTx,
|
|
|
|
bytes.Repeat([]byte{1}, 45), csvTimeout,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// HTLC redemption w/ invalid sequence
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return receiverHtlcSpendRedeem(htlcScript,
|
|
|
|
paymentAmt, bobKeyPriv, sweepTx,
|
|
|
|
paymentPreimage[:], csvTimeout-2,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// HTLC redemption w/ valid preimage size
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return receiverHtlcSpendRedeem(htlcScript,
|
|
|
|
paymentAmt, bobKeyPriv, sweepTx,
|
|
|
|
paymentPreimage[:], csvTimeout,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// revoke w/ sig
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return receiverHtlcSpendRevoke(htlcScript, paymentAmt,
|
|
|
|
aliceKeyPriv, sweepTx, revokePreimage[:],
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// refund w/ invalid lock time
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return receiverHtlcSpendTimeout(htlcScript, paymentAmt,
|
|
|
|
aliceKeyPriv, sweepTx, cltvTimeout-2)
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// refund w/ valid lock time
|
|
|
|
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
|
|
|
return receiverHtlcSpendTimeout(htlcScript, paymentAmt,
|
|
|
|
aliceKeyPriv, sweepTx, cltvTimeout)
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
sweepTx.TxIn[0].Witness = testCase.witness()
|
|
|
|
|
|
|
|
vm, err := txscript.NewEngine(htlcWitnessScript,
|
|
|
|
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
|
|
|
nil, int64(paymentAmt))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:31:29 +02:00
|
|
|
// This buffer will trace execution of the Script, only dumping
|
|
|
|
// out to stdout in the case that a test fails.
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
var debugBuf bytes.Buffer
|
|
|
|
|
|
|
|
done := false
|
|
|
|
for !done {
|
|
|
|
dis, err := vm.DisasmPC()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("stepping (%v)\n", err)
|
|
|
|
}
|
|
|
|
debugBuf.WriteString(fmt.Sprintf("stepping %v\n", dis))
|
|
|
|
|
|
|
|
done, err = vm.Step()
|
|
|
|
if err != nil && testCase.valid {
|
|
|
|
fmt.Println(debugBuf.String())
|
|
|
|
t.Fatalf("spend test case #%v failed, spend should be valid: %v", i, err)
|
|
|
|
} else if err == nil && !testCase.valid && done {
|
|
|
|
fmt.Println(debugBuf.String())
|
|
|
|
t.Fatalf("spend test case #%v succeed, spend should be invalid: %v", i, err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 00:28:32 +01:00
|
|
|
debugBuf.WriteString(fmt.Sprintf("Stack: %v", vm.GetStack()))
|
|
|
|
debugBuf.WriteString(fmt.Sprintf("AltStack: %v", vm.GetAltStack()))
|
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.
The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.
The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.
Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.
A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 20:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-15 03:34:59 +01:00
|
|
|
|
2017-01-09 19:03:19 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 03:34:59 +01:00
|
|
|
func TestCommitTxStateHint(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
2016-11-15 03:34:59 +01:00
|
|
|
|
2017-07-30 03:24:47 +02:00
|
|
|
stateHintTests := []struct {
|
|
|
|
name string
|
|
|
|
from uint64
|
|
|
|
to uint64
|
|
|
|
inputs int
|
|
|
|
shouldFail bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "states 0 to 1000",
|
|
|
|
from: 0,
|
|
|
|
to: 1000,
|
|
|
|
inputs: 1,
|
|
|
|
shouldFail: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "states 'maxStateHint-1000' to 'maxStateHint'",
|
|
|
|
from: maxStateHint - 1000,
|
|
|
|
to: maxStateHint,
|
|
|
|
inputs: 1,
|
|
|
|
shouldFail: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "state 'maxStateHint+1'",
|
|
|
|
from: maxStateHint + 1,
|
|
|
|
to: maxStateHint + 10,
|
|
|
|
inputs: 1,
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "commit transaction with two inputs",
|
|
|
|
inputs: 2,
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-15 03:34:59 +01:00
|
|
|
var obsfucator [StateHintSize]byte
|
|
|
|
copy(obsfucator[:], testHdSeed[:StateHintSize])
|
2017-01-14 09:17:00 +01:00
|
|
|
timeYesterday := uint32(time.Now().Unix() - 24*60*60)
|
2016-11-15 03:34:59 +01:00
|
|
|
|
2017-01-09 19:03:19 +01:00
|
|
|
for _, test := range stateHintTests {
|
|
|
|
commitTx := wire.NewMsgTx(2)
|
2016-11-15 03:34:59 +01:00
|
|
|
|
2017-01-09 19:03:19 +01:00
|
|
|
// Add supplied number of inputs to the commitment transaction.
|
|
|
|
for i := 0; i < test.inputs; i++ {
|
|
|
|
commitTx.AddTxIn(&wire.TxIn{})
|
2016-11-15 03:34:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 19:03:19 +01:00
|
|
|
for i := test.from; i <= test.to; i++ {
|
|
|
|
stateNum := uint64(i)
|
2017-01-09 14:58:58 +01:00
|
|
|
|
2017-01-09 19:03:19 +01:00
|
|
|
err := SetStateNumHint(commitTx, stateNum, obsfucator)
|
|
|
|
if err != nil && !test.shouldFail {
|
|
|
|
t.Fatalf("unable to set state num %v: %v", i, err)
|
|
|
|
} else if err == nil && test.shouldFail {
|
|
|
|
t.Fatalf("Failed(%v): test should fail but did not", test.name)
|
|
|
|
}
|
2017-01-09 14:58:58 +01:00
|
|
|
|
2017-01-14 09:17:00 +01:00
|
|
|
locktime := commitTx.LockTime
|
|
|
|
sequence := commitTx.TxIn[0].Sequence
|
|
|
|
|
|
|
|
// Locktime should not be less than 500,000,000 and not larger
|
|
|
|
// than the time 24 hours ago. One day should provide a good
|
|
|
|
// enough buffer for the tests.
|
|
|
|
if locktime < 5e8 || locktime > timeYesterday {
|
|
|
|
if !test.shouldFail {
|
|
|
|
t.Fatalf("The value of locktime (%v) may cause the commitment "+
|
|
|
|
"transaction to be unspendable", locktime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sequence&wire.SequenceLockTimeDisabled == 0 {
|
|
|
|
if !test.shouldFail {
|
|
|
|
t.Fatalf("Sequence locktime is NOT disabled when it should be")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 19:03:19 +01:00
|
|
|
extractedStateNum := GetStateNumHint(commitTx, obsfucator)
|
|
|
|
if extractedStateNum != stateNum && !test.shouldFail {
|
|
|
|
t.Fatalf("state number mismatched, expected %v, got %v",
|
|
|
|
stateNum, extractedStateNum)
|
|
|
|
} else if extractedStateNum == stateNum && test.shouldFail {
|
|
|
|
t.Fatalf("Failed(%v): test should fail but did not", test.name)
|
|
|
|
}
|
2017-01-09 14:58:58 +01:00
|
|
|
}
|
2017-01-09 19:03:19 +01:00
|
|
|
t.Logf("Passed: %v", test.name)
|
2016-11-15 03:34:59 +01:00
|
|
|
}
|
|
|
|
}
|