multi: move Input interface and related code
This commit is a step to split the lnwallet package. It puts the Input interface and implementations in a separate package along with all their dependencies from lnwallet.
This commit is contained in:
66 files changed
+811
-689
No files matched your search
+178
@@ -0,0 +1,178 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// Input represents an abstract UTXO which is to be spent using a sweeping
|
||||
// transaction. The method provided give the caller all information needed to
|
||||
// construct a valid input within a sweeping transaction to sweep this
|
||||
// lingering UTXO.
|
||||
type Input interface {
|
||||
// Outpoint returns the reference to the output being spent, used to
|
||||
// construct the corresponding transaction input.
|
||||
OutPoint() *wire.OutPoint
|
||||
|
||||
// WitnessType returns an enum specifying the type of witness that must
|
||||
// be generated in order to spend this output.
|
||||
WitnessType() WitnessType
|
||||
|
||||
// SignDesc returns a reference to a spendable output's sign
|
||||
// descriptor, which is used during signing to compute a valid witness
|
||||
// that spends this output.
|
||||
SignDesc() *SignDescriptor
|
||||
|
||||
// CraftInputScript returns a valid set of input scripts allowing this
|
||||
// output to be spent. The returns input scripts should target the
|
||||
// input at location txIndex within the passed transaction. The input
|
||||
// scripts generated by this method support spending p2wkh, p2wsh, and
|
||||
// also nested p2sh outputs.
|
||||
CraftInputScript(signer Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes,
|
||||
txinIdx int) (*Script, error)
|
||||
|
||||
// BlocksToMaturity returns the relative timelock, as a number of
|
||||
// blocks, that must be built on top of the confirmation height before
|
||||
// the output can be spent. For non-CSV locked inputs this is always
|
||||
// zero.
|
||||
BlocksToMaturity() uint32
|
||||
|
||||
// HeightHint returns the minimum height at which a confirmed spending
|
||||
// tx can occur.
|
||||
HeightHint() uint32
|
||||
}
|
||||
|
||||
type inputKit struct {
|
||||
outpoint wire.OutPoint
|
||||
witnessType WitnessType
|
||||
signDesc SignDescriptor
|
||||
heightHint uint32
|
||||
}
|
||||
|
||||
// OutPoint returns the breached output's identifier that is to be included as
|
||||
// a transaction input.
|
||||
func (i *inputKit) OutPoint() *wire.OutPoint {
|
||||
return &i.outpoint
|
||||
}
|
||||
|
||||
// WitnessType returns the type of witness that must be generated to spend the
|
||||
// breached output.
|
||||
func (i *inputKit) WitnessType() WitnessType {
|
||||
return i.witnessType
|
||||
}
|
||||
|
||||
// SignDesc returns the breached output's SignDescriptor, which is used during
|
||||
// signing to compute the witness.
|
||||
func (i *inputKit) SignDesc() *SignDescriptor {
|
||||
return &i.signDesc
|
||||
}
|
||||
|
||||
// HeightHint returns the minimum height at which a confirmed spending
|
||||
// tx can occur.
|
||||
func (i *inputKit) HeightHint() uint32 {
|
||||
return i.heightHint
|
||||
}
|
||||
|
||||
// BaseInput contains all the information needed to sweep a basic output
|
||||
// (CSV/CLTV/no time lock)
|
||||
type BaseInput struct {
|
||||
inputKit
|
||||
}
|
||||
|
||||
// MakeBaseInput assembles a new BaseInput that can be used to construct a
|
||||
// sweep transaction.
|
||||
func MakeBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
|
||||
signDescriptor *SignDescriptor, heightHint uint32) BaseInput {
|
||||
|
||||
return BaseInput{
|
||||
inputKit{
|
||||
outpoint: *outpoint,
|
||||
witnessType: witnessType,
|
||||
signDesc: *signDescriptor,
|
||||
heightHint: heightHint,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// CraftInputScript returns a valid set of input scripts allowing this output
|
||||
// to be spent. The returns input scripts should target the input at location
|
||||
// txIndex within the passed transaction. The input scripts generated by this
|
||||
// method support spending p2wkh, p2wsh, and also nested p2sh outputs.
|
||||
func (bi *BaseInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes, txinIdx int) (*Script, error) {
|
||||
|
||||
witnessFunc := bi.witnessType.GenWitnessFunc(
|
||||
signer, bi.SignDesc(),
|
||||
)
|
||||
|
||||
return witnessFunc(txn, hashCache, txinIdx)
|
||||
}
|
||||
|
||||
// BlocksToMaturity returns the relative timelock, as a number of blocks, that
|
||||
// must be built on top of the confirmation height before the output can be
|
||||
// spent. For non-CSV locked inputs this is always zero.
|
||||
func (bi *BaseInput) BlocksToMaturity() uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// HtlcSucceedInput constitutes a sweep input that needs a pre-image. The input
|
||||
// is expected to reside on the commitment tx of the remote party and should
|
||||
// not be a second level tx output.
|
||||
type HtlcSucceedInput struct {
|
||||
inputKit
|
||||
|
||||
preimage []byte
|
||||
}
|
||||
|
||||
// MakeHtlcSucceedInput assembles a new redeem input that can be used to
|
||||
// construct a sweep transaction.
|
||||
func MakeHtlcSucceedInput(outpoint *wire.OutPoint,
|
||||
signDescriptor *SignDescriptor,
|
||||
preimage []byte, heightHint uint32) HtlcSucceedInput {
|
||||
|
||||
return HtlcSucceedInput{
|
||||
inputKit: inputKit{
|
||||
outpoint: *outpoint,
|
||||
witnessType: HtlcAcceptedRemoteSuccess,
|
||||
signDesc: *signDescriptor,
|
||||
heightHint: heightHint,
|
||||
},
|
||||
preimage: preimage,
|
||||
}
|
||||
}
|
||||
|
||||
// CraftInputScript returns a valid set of input scripts allowing this output
|
||||
// to be spent. The returns input scripts should target the input at location
|
||||
// txIndex within the passed transaction. The input scripts generated by this
|
||||
// method support spending p2wkh, p2wsh, and also nested p2sh outputs.
|
||||
func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes, txinIdx int) (*Script, error) {
|
||||
|
||||
desc := h.signDesc
|
||||
desc.SigHashes = hashCache
|
||||
desc.InputIndex = txinIdx
|
||||
|
||||
witness, err := SenderHtlcSpendRedeem(
|
||||
signer, &desc, txn, h.preimage,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// BlocksToMaturity returns the relative timelock, as a number of blocks, that
|
||||
// must be built on top of the confirmation height before the output can be
|
||||
// spent.
|
||||
func (h *HtlcSucceedInput) BlocksToMaturity() uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Compile-time constraints to ensure each input struct implement the Input
|
||||
// interface.
|
||||
var _ Input = (*BaseInput)(nil)
|
||||
var _ Input = (*HtlcSucceedInput)(nil)
|
||||
@@ -0,0 +1,1125 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"golang.org/x/crypto/ripemd160"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
)
|
||||
|
||||
var (
|
||||
// TODO(roasbeef): remove these and use the one's defined in txscript
|
||||
// within testnet-L.
|
||||
|
||||
// SequenceLockTimeSeconds is the 22nd bit which indicates the lock
|
||||
// time is in seconds.
|
||||
SequenceLockTimeSeconds = uint32(1 << 22)
|
||||
)
|
||||
|
||||
// WitnessScriptHash generates a pay-to-witness-script-hash public key script
|
||||
// paying to a version 0 witness program paying to the passed redeem script.
|
||||
func WitnessScriptHash(witnessScript []byte) ([]byte, error) {
|
||||
bldr := txscript.NewScriptBuilder()
|
||||
|
||||
bldr.AddOp(txscript.OP_0)
|
||||
scriptHash := sha256.Sum256(witnessScript)
|
||||
bldr.AddData(scriptHash[:])
|
||||
return bldr.Script()
|
||||
}
|
||||
|
||||
// GenMultiSigScript generates the non-p2sh'd multisig script for 2 of 2
|
||||
// pubkeys.
|
||||
func GenMultiSigScript(aPub, bPub []byte) ([]byte, error) {
|
||||
if len(aPub) != 33 || len(bPub) != 33 {
|
||||
return nil, fmt.Errorf("Pubkey size error. Compressed pubkeys only")
|
||||
}
|
||||
|
||||
// Swap to sort pubkeys if needed. Keys are sorted in lexicographical
|
||||
// order. The signatures within the scriptSig must also adhere to the
|
||||
// order, ensuring that the signatures for each public key appears in
|
||||
// the proper order on the stack.
|
||||
if bytes.Compare(aPub, bPub) == 1 {
|
||||
aPub, bPub = bPub, aPub
|
||||
}
|
||||
|
||||
bldr := txscript.NewScriptBuilder()
|
||||
bldr.AddOp(txscript.OP_2)
|
||||
bldr.AddData(aPub) // Add both pubkeys (sorted).
|
||||
bldr.AddData(bPub)
|
||||
bldr.AddOp(txscript.OP_2)
|
||||
bldr.AddOp(txscript.OP_CHECKMULTISIG)
|
||||
return bldr.Script()
|
||||
}
|
||||
|
||||
// GenFundingPkScript creates a redeem script, and its matching p2wsh
|
||||
// output for the funding transaction.
|
||||
func GenFundingPkScript(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, error) {
|
||||
// As a sanity check, ensure that the passed amount is above zero.
|
||||
if amt <= 0 {
|
||||
return nil, nil, fmt.Errorf("can't create FundTx script with " +
|
||||
"zero, or negative coins")
|
||||
}
|
||||
|
||||
// First, create the 2-of-2 multi-sig script itself.
|
||||
witnessScript, err := GenMultiSigScript(aPub, bPub)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// With the 2-of-2 script in had, generate a p2wsh script which pays
|
||||
// to the funding script.
|
||||
pkScript, err := WitnessScriptHash(witnessScript)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return witnessScript, wire.NewTxOut(amt, pkScript), nil
|
||||
}
|
||||
|
||||
// SpendMultiSig generates the witness stack required to redeem the 2-of-2 p2wsh
|
||||
// multi-sig output.
|
||||
func SpendMultiSig(witnessScript, pubA, sigA, pubB, sigB []byte) [][]byte {
|
||||
witness := make([][]byte, 4)
|
||||
|
||||
// When spending a p2wsh multi-sig script, rather than an OP_0, we add
|
||||
// a nil stack element to eat the extra pop.
|
||||
witness[0] = nil
|
||||
|
||||
// When initially generating the witnessScript, we sorted the serialized
|
||||
// public keys in descending order. So we do a quick comparison in order
|
||||
// ensure the signatures appear on the Script Virtual Machine stack in
|
||||
// the correct order.
|
||||
if bytes.Compare(pubA, pubB) == 1 {
|
||||
witness[1] = sigB
|
||||
witness[2] = sigA
|
||||
} else {
|
||||
witness[1] = sigA
|
||||
witness[2] = sigB
|
||||
}
|
||||
|
||||
// Finally, add the preimage as the last witness element.
|
||||
witness[3] = witnessScript
|
||||
|
||||
return witness
|
||||
}
|
||||
|
||||
// FindScriptOutputIndex finds the index of the public key script output
|
||||
// matching 'script'. Additionally, a boolean is returned indicating if a
|
||||
// matching output was found at all.
|
||||
//
|
||||
// NOTE: The search stops after the first matching script is found.
|
||||
func FindScriptOutputIndex(tx *wire.MsgTx, script []byte) (bool, uint32) {
|
||||
found := false
|
||||
index := uint32(0)
|
||||
for i, txOut := range tx.TxOut {
|
||||
if bytes.Equal(txOut.PkScript, script) {
|
||||
found = true
|
||||
index = uint32(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return found, index
|
||||
}
|
||||
|
||||
// Ripemd160H calculates the ripemd160 of the passed byte slice. This is used to
|
||||
// calculate the intermediate hash for payment pre-images. Payment hashes are
|
||||
// the result of ripemd160(sha256(paymentPreimage)). As a result, the value
|
||||
// passed in should be the sha256 of the payment hash.
|
||||
func Ripemd160H(d []byte) []byte {
|
||||
h := ripemd160.New()
|
||||
h.Write(d)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
// SenderHTLCScript constructs the public key script for an outgoing HTLC
|
||||
// output payment for the sender's version of the commitment transaction. The
|
||||
// possible script paths from this output include:
|
||||
//
|
||||
// * The sender timing out the HTLC using the second level HTLC timeout
|
||||
// transaction.
|
||||
// * The receiver of the HTLC claiming the output on-chain with the payment
|
||||
// preimage.
|
||||
// * The receiver of the HTLC sweeping all the funds in the case that a
|
||||
// revoked commitment transaction bearing this HTLC was broadcast.
|
||||
//
|
||||
// Possible Input Scripts:
|
||||
// SENDR: <0> <sendr sig> <recvr sig> <0> (spend using HTLC timeout transaction)
|
||||
// RECVR: <recvr sig> <preimage>
|
||||
// REVOK: <revoke sig> <revoke key>
|
||||
// * receiver revoke
|
||||
//
|
||||
// OP_DUP OP_HASH160 <revocation key hash160> OP_EQUAL
|
||||
// OP_IF
|
||||
// OP_CHECKSIG
|
||||
// OP_ELSE
|
||||
// <recv htlc key>
|
||||
// OP_SWAP OP_SIZE 32 OP_EQUAL
|
||||
// OP_NOTIF
|
||||
// OP_DROP 2 OP_SWAP <sender htlc key> 2 OP_CHECKMULTISIG
|
||||
// OP_ELSE
|
||||
// OP_HASH160 <ripemd160(payment hash)> OP_EQUALVERIFY
|
||||
// OP_CHECKSIG
|
||||
// OP_ENDIF
|
||||
// OP_ENDIF
|
||||
func SenderHTLCScript(senderHtlcKey, receiverHtlcKey,
|
||||
revocationKey *btcec.PublicKey, paymentHash []byte) ([]byte, error) {
|
||||
|
||||
builder := txscript.NewScriptBuilder()
|
||||
|
||||
// The opening operations are used to determine if this is the receiver
|
||||
// of the HTLC attempting to sweep all the funds due to a contract
|
||||
// breach. In this case, they'll place the revocation key at the top of
|
||||
// the stack.
|
||||
builder.AddOp(txscript.OP_DUP)
|
||||
builder.AddOp(txscript.OP_HASH160)
|
||||
builder.AddData(btcutil.Hash160(revocationKey.SerializeCompressed()))
|
||||
builder.AddOp(txscript.OP_EQUAL)
|
||||
|
||||
// If the hash matches, then this is the revocation clause. The output
|
||||
// can be spent if the check sig operation passes.
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
builder.AddOp(txscript.OP_CHECKSIG)
|
||||
|
||||
// Otherwise, this may either be the receiver of the HTLC claiming with
|
||||
// the pre-image, or the sender of the HTLC sweeping the output after
|
||||
// it has timed out.
|
||||
builder.AddOp(txscript.OP_ELSE)
|
||||
|
||||
// We'll do a bit of set up by pushing the receiver's key on the top of
|
||||
// the stack. This will be needed later if we decide that this is the
|
||||
// sender activating the time out clause with the HTLC timeout
|
||||
// transaction.
|
||||
builder.AddData(receiverHtlcKey.SerializeCompressed())
|
||||
|
||||
// Atm, the top item of the stack is the receiverKey's so we use a swap
|
||||
// to expose what is either the payment pre-image or a signature.
|
||||
builder.AddOp(txscript.OP_SWAP)
|
||||
|
||||
// With the top item swapped, check if it's 32 bytes. If so, then this
|
||||
// *may* be the payment pre-image.
|
||||
builder.AddOp(txscript.OP_SIZE)
|
||||
builder.AddInt64(32)
|
||||
builder.AddOp(txscript.OP_EQUAL)
|
||||
|
||||
// If it isn't then this might be the sender of the HTLC activating the
|
||||
// time out clause.
|
||||
builder.AddOp(txscript.OP_NOTIF)
|
||||
|
||||
// We'll drop the OP_IF return value off the top of the stack so we can
|
||||
// reconstruct the multi-sig script used as an off-chain covenant. If
|
||||
// two valid signatures are provided, ten then output will be deemed as
|
||||
// spendable.
|
||||
builder.AddOp(txscript.OP_DROP)
|
||||
builder.AddOp(txscript.OP_2)
|
||||
builder.AddOp(txscript.OP_SWAP)
|
||||
builder.AddData(senderHtlcKey.SerializeCompressed())
|
||||
builder.AddOp(txscript.OP_2)
|
||||
builder.AddOp(txscript.OP_CHECKMULTISIG)
|
||||
|
||||
// Otherwise, then the only other case is that this is the receiver of
|
||||
// the HTLC sweeping it on-chain with the payment pre-image.
|
||||
builder.AddOp(txscript.OP_ELSE)
|
||||
|
||||
// Hash the top item of the stack and compare it with the hash160 of
|
||||
// the payment hash, which is already the sha256 of the payment
|
||||
// pre-image. By using this little trick we're able save space on-chain
|
||||
// as the witness includes a 20-byte hash rather than a 32-byte hash.
|
||||
builder.AddOp(txscript.OP_HASH160)
|
||||
builder.AddData(Ripemd160H(paymentHash))
|
||||
builder.AddOp(txscript.OP_EQUALVERIFY)
|
||||
|
||||
// This checks the receiver's signature so that a third party with
|
||||
// knowledge of the payment preimage still cannot steal the output.
|
||||
builder.AddOp(txscript.OP_CHECKSIG)
|
||||
|
||||
// Close out the OP_IF statement above.
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
|
||||
// Close out the OP_IF statement at the top of the script.
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
|
||||
return builder.Script()
|
||||
}
|
||||
|
||||
// SenderHtlcSpendRevokeWithKey constructs a valid witness allowing the receiver of an
|
||||
// HTLC to claim the output with knowledge of the revocation private key in the
|
||||
// scenario that the sender of the HTLC broadcasts a previously revoked
|
||||
// commitment transaction. A valid spend requires knowledge of the private key
|
||||
// that corresponds to their revocation base point and also the private key fro
|
||||
// the per commitment point, and a valid signature under the combined public
|
||||
// key.
|
||||
func SenderHtlcSpendRevokeWithKey(signer Signer, signDesc *SignDescriptor,
|
||||
revokeKey *btcec.PublicKey, sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The stack required to sweep a revoke HTLC output consists simply of
|
||||
// the exact witness stack as one of a regular p2wkh spend. The only
|
||||
// difference is that the keys used were derived in an adversarial
|
||||
// manner in order to encode the revocation contract into a sig+key
|
||||
// pair.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = revokeKey.SerializeCompressed()
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// SenderHtlcSpendRevoke constructs a valid witness allowing the receiver of an
|
||||
// HTLC to claim the output with knowledge of the revocation private key in the
|
||||
// scenario that the sender of the HTLC broadcasts a previously revoked
|
||||
// commitment transaction. This method first derives the appropriate revocation
|
||||
// key, and requires that the provided SignDescriptor has a local revocation
|
||||
// basepoint and commitment secret in the PubKey and DoubleTweak fields,
|
||||
// respectively.
|
||||
func SenderHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
if signDesc.KeyDesc.PubKey == nil {
|
||||
return nil, fmt.Errorf("cannot generate witness with nil " +
|
||||
"KeyDesc pubkey")
|
||||
}
|
||||
|
||||
// Derive the revocation key using the local revocation base point and
|
||||
// commitment point.
|
||||
revokeKey := DeriveRevocationPubkey(
|
||||
signDesc.KeyDesc.PubKey,
|
||||
signDesc.DoubleTweak.PubKey(),
|
||||
)
|
||||
|
||||
return SenderHtlcSpendRevokeWithKey(signer, signDesc, revokeKey, sweepTx)
|
||||
}
|
||||
|
||||
// SenderHtlcSpendRedeem constructs a valid witness allowing the receiver of an
|
||||
// HTLC to redeem the pending output in the scenario that the sender broadcasts
|
||||
// their version of the commitment transaction. A valid spend requires
|
||||
// knowledge of the payment preimage, and a valid signature under the receivers
|
||||
// public key.
|
||||
func SenderHtlcSpendRedeem(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx, paymentPreimage []byte) (wire.TxWitness, error) {
|
||||
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The stack require to spend this output is simply the signature
|
||||
// generated above under the receiver's public key, and the payment
|
||||
// pre-image.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = paymentPreimage
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// SenderHtlcSpendTimeout constructs a valid witness allowing the sender of an
|
||||
// HTLC to activate the time locked covenant clause of a soon to be expired
|
||||
// HTLC. This script simply spends the multi-sig output using the
|
||||
// pre-generated HTLC timeout transaction.
|
||||
func SenderHtlcSpendTimeout(receiverSig []byte, signer Signer,
|
||||
signDesc *SignDescriptor, htlcTimeoutTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
sweepSig, err := signer.SignOutputRaw(htlcTimeoutTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We place a zero as the first item of the evaluated witness stack in
|
||||
// order to force Script execution to the HTLC timeout clause. The
|
||||
// second zero is require to consume the extra pop due to a bug in the
|
||||
// original OP_CHECKMULTISIG.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 5))
|
||||
witnessStack[0] = nil
|
||||
witnessStack[1] = append(receiverSig, byte(txscript.SigHashAll))
|
||||
witnessStack[2] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[3] = nil
|
||||
witnessStack[4] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// ReceiverHTLCScript constructs the public key script for an incoming HTLC
|
||||
// output payment for the receiver's version of the commitment transaction. The
|
||||
// possible execution paths from this script include:
|
||||
// * The receiver of the HTLC uses its second level HTLC transaction to
|
||||
// advance the state of the HTLC into the delay+claim state.
|
||||
// * The sender of the HTLC sweeps all the funds of the HTLC as a breached
|
||||
// commitment was broadcast.
|
||||
// * The sender of the HTLC sweeps the HTLC on-chain after the timeout period
|
||||
// of the HTLC has passed.
|
||||
//
|
||||
// Possible Input Scripts:
|
||||
// RECVR: <0> <sender sig> <recvr sig> <preimage> (spend using HTLC success transaction)
|
||||
// REVOK: <sig> <key>
|
||||
// SENDR: <sig> 0
|
||||
//
|
||||
//
|
||||
// OP_DUP OP_HASH160 <revocation key hash160> OP_EQUAL
|
||||
// OP_IF
|
||||
// OP_CHECKSIG
|
||||
// OP_ELSE
|
||||
// <sendr htlc key>
|
||||
// OP_SWAP OP_SIZE 32 OP_EQUAL
|
||||
// OP_IF
|
||||
// OP_HASH160 <ripemd160(payment hash)> OP_EQUALVERIFY
|
||||
// 2 OP_SWAP <recvr htlc key> 2 OP_CHECKMULTISIG
|
||||
// OP_ELSE
|
||||
// OP_DROP <cltv expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP
|
||||
// OP_CHECKSIG
|
||||
// OP_ENDIF
|
||||
// OP_ENDIF
|
||||
func ReceiverHTLCScript(cltvExpiry uint32, senderHtlcKey,
|
||||
receiverHtlcKey, revocationKey *btcec.PublicKey,
|
||||
paymentHash []byte) ([]byte, error) {
|
||||
|
||||
builder := txscript.NewScriptBuilder()
|
||||
|
||||
// The opening operations are used to determine if this is the sender
|
||||
// of the HTLC attempting to sweep all the funds due to a contract
|
||||
// breach. In this case, they'll place the revocation key at the top of
|
||||
// the stack.
|
||||
builder.AddOp(txscript.OP_DUP)
|
||||
builder.AddOp(txscript.OP_HASH160)
|
||||
builder.AddData(btcutil.Hash160(revocationKey.SerializeCompressed()))
|
||||
builder.AddOp(txscript.OP_EQUAL)
|
||||
|
||||
// If the hash matches, then this is the revocation clause. The output
|
||||
// can be spent if the check sig operation passes.
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
builder.AddOp(txscript.OP_CHECKSIG)
|
||||
|
||||
// Otherwise, this may either be the receiver of the HTLC starting the
|
||||
// claiming process via the second level HTLC success transaction and
|
||||
// the pre-image, or the sender of the HTLC sweeping the output after
|
||||
// it has timed out.
|
||||
builder.AddOp(txscript.OP_ELSE)
|
||||
|
||||
// We'll do a bit of set up by pushing the sender's key on the top of
|
||||
// the stack. This will be needed later if we decide that this is the
|
||||
// receiver transitioning the output to the claim state using their
|
||||
// second-level HTLC success transaction.
|
||||
builder.AddData(senderHtlcKey.SerializeCompressed())
|
||||
|
||||
// Atm, the top item of the stack is the sender's key so we use a swap
|
||||
// to expose what is either the payment pre-image or something else.
|
||||
builder.AddOp(txscript.OP_SWAP)
|
||||
|
||||
// With the top item swapped, check if it's 32 bytes. If so, then this
|
||||
// *may* be the payment pre-image.
|
||||
builder.AddOp(txscript.OP_SIZE)
|
||||
builder.AddInt64(32)
|
||||
builder.AddOp(txscript.OP_EQUAL)
|
||||
|
||||
// If the item on the top of the stack is 32-bytes, then it is the
|
||||
// proper size, so this indicates that the receiver of the HTLC is
|
||||
// attempting to claim the output on-chain by transitioning the state
|
||||
// of the HTLC to delay+claim.
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
|
||||
// Next we'll hash the item on the top of the stack, if it matches the
|
||||
// payment pre-image, then we'll continue. Otherwise, we'll end the
|
||||
// script here as this is the invalid payment pre-image.
|
||||
builder.AddOp(txscript.OP_HASH160)
|
||||
builder.AddData(Ripemd160H(paymentHash))
|
||||
builder.AddOp(txscript.OP_EQUALVERIFY)
|
||||
|
||||
// If the payment hash matches, then we'll also need to satisfy the
|
||||
// multi-sig covenant by providing both signatures of the sender and
|
||||
// receiver. If the convenient is met, then we'll allow the spending of
|
||||
// this output, but only by the HTLC success transaction.
|
||||
builder.AddOp(txscript.OP_2)
|
||||
builder.AddOp(txscript.OP_SWAP)
|
||||
builder.AddData(receiverHtlcKey.SerializeCompressed())
|
||||
builder.AddOp(txscript.OP_2)
|
||||
builder.AddOp(txscript.OP_CHECKMULTISIG)
|
||||
|
||||
// Otherwise, this might be the sender of the HTLC attempting to sweep
|
||||
// it on-chain after the timeout.
|
||||
builder.AddOp(txscript.OP_ELSE)
|
||||
|
||||
// We'll drop the extra item (which is the output from evaluating the
|
||||
// OP_EQUAL) above from the stack.
|
||||
builder.AddOp(txscript.OP_DROP)
|
||||
|
||||
// With that item dropped off, we can now enforce the absolute
|
||||
// lock-time required to timeout the HTLC. If the time has passed, then
|
||||
// we'll proceed with a checksig to ensure that this is actually the
|
||||
// sender of he original HTLC.
|
||||
builder.AddInt64(int64(cltvExpiry))
|
||||
builder.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
|
||||
builder.AddOp(txscript.OP_DROP)
|
||||
builder.AddOp(txscript.OP_CHECKSIG)
|
||||
|
||||
// Close out the inner if statement.
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
|
||||
// Close out the outer if statement.
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
|
||||
return builder.Script()
|
||||
}
|
||||
|
||||
// ReceiverHtlcSpendRedeem constructs a valid witness allowing the receiver of
|
||||
// an HTLC to redeem the conditional payment in the event that their commitment
|
||||
// transaction is broadcast. This clause transitions the state of the HLTC
|
||||
// output into the delay+claim state by activating the off-chain covenant bound
|
||||
// by the 2-of-2 multi-sig output. The HTLC success timeout transaction being
|
||||
// signed has a relative timelock delay enforced by its sequence number. This
|
||||
// delay give the sender of the HTLC enough time to revoke the output if this
|
||||
// is a breach commitment transaction.
|
||||
func ReceiverHtlcSpendRedeem(senderSig, paymentPreimage []byte,
|
||||
signer Signer, signDesc *SignDescriptor,
|
||||
htlcSuccessTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
// First, we'll generate a signature for the HTLC success transaction.
|
||||
// The signDesc should be signing with the public key used as the
|
||||
// receiver's public key and also the correct single tweak.
|
||||
sweepSig, err := signer.SignOutputRaw(htlcSuccessTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The final witness stack is used the provide the script with the
|
||||
// payment pre-image, and also execute the multi-sig clause after the
|
||||
// pre-images matches. We add a nil item at the bottom of the stack in
|
||||
// order to consume the extra pop within OP_CHECKMULTISIG.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 5))
|
||||
witnessStack[0] = nil
|
||||
witnessStack[1] = append(senderSig, byte(txscript.SigHashAll))
|
||||
witnessStack[2] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[3] = paymentPreimage
|
||||
witnessStack[4] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// ReceiverHtlcSpendRevokeWithKey constructs a valid witness allowing the sender of an
|
||||
// HTLC within a previously revoked commitment transaction to re-claim the
|
||||
// pending funds in the case that the receiver broadcasts this revoked
|
||||
// commitment transaction.
|
||||
func ReceiverHtlcSpendRevokeWithKey(signer Signer, signDesc *SignDescriptor,
|
||||
revokeKey *btcec.PublicKey, sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
// First, we'll generate a signature for the sweep transaction. The
|
||||
// signDesc should be signing with the public key used as the fully
|
||||
// derived revocation public key and also the correct double tweak
|
||||
// value.
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We place a zero, then one as the first items in the evaluated
|
||||
// witness stack in order to force script execution to the HTLC
|
||||
// revocation clause.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = revokeKey.SerializeCompressed()
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// ReceiverHtlcSpendRevoke constructs a valid witness allowing the sender of an
|
||||
// HTLC within a previously revoked commitment transaction to re-claim the
|
||||
// pending funds in the case that the receiver broadcasts this revoked
|
||||
// commitment transaction. This method first derives the appropriate revocation
|
||||
// key, and requires that the provided SignDescriptor has a local revocation
|
||||
// basepoint and commitment secret in the PubKey and DoubleTweak fields,
|
||||
// respectively.
|
||||
func ReceiverHtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
if signDesc.KeyDesc.PubKey == nil {
|
||||
return nil, fmt.Errorf("cannot generate witness with nil " +
|
||||
"KeyDesc pubkey")
|
||||
}
|
||||
|
||||
// Derive the revocation key using the local revocation base point and
|
||||
// commitment point.
|
||||
revokeKey := DeriveRevocationPubkey(
|
||||
signDesc.KeyDesc.PubKey,
|
||||
signDesc.DoubleTweak.PubKey(),
|
||||
)
|
||||
|
||||
return ReceiverHtlcSpendRevokeWithKey(signer, signDesc, revokeKey, sweepTx)
|
||||
}
|
||||
|
||||
// ReceiverHtlcSpendTimeout constructs a valid witness allowing the sender of
|
||||
// an HTLC to recover the pending funds after an absolute timeout in the
|
||||
// scenario that the receiver of the HTLC broadcasts their version of the
|
||||
// commitment transaction. If the caller has already set the lock time on the
|
||||
// spending transaction, than a value of -1 can be passed for the cltvExpiry
|
||||
// value.
|
||||
//
|
||||
// NOTE: The target input of the passed transaction MUST NOT have a final
|
||||
// sequence number. Otherwise, the OP_CHECKLOCKTIMEVERIFY check will fail.
|
||||
func ReceiverHtlcSpendTimeout(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx, cltvExpiry int32) (wire.TxWitness, error) {
|
||||
|
||||
// If the caller set a proper timeout value, then we'll apply it
|
||||
// directly to the transaction.
|
||||
if cltvExpiry != -1 {
|
||||
// The HTLC output has an absolute time period before we are
|
||||
// permitted to recover the pending funds. Therefore we need to
|
||||
// set the locktime on this sweeping transaction in order to
|
||||
// pass Script verification.
|
||||
sweepTx.LockTime = uint32(cltvExpiry)
|
||||
}
|
||||
|
||||
// With the lock time on the transaction set, we'll not generate a
|
||||
// signature for the sweep transaction. The passed sign descriptor
|
||||
// should be created using the raw public key of the sender (w/o the
|
||||
// single tweak applied), and the single tweak set to the proper value
|
||||
// taking into account the current state's point.
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = nil
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// SecondLevelHtlcScript is the uniform script that's used as the output for
|
||||
// the second-level HTLC transactions. The second level transaction act as a
|
||||
// sort of covenant, ensuring that a 2-of-2 multi-sig output can only be
|
||||
// spent in a particular way, and to a particular output.
|
||||
//
|
||||
// Possible Input Scripts:
|
||||
// * To revoke an HTLC output that has been transitioned to the claim+delay
|
||||
// state:
|
||||
// * <revoke sig> 1
|
||||
//
|
||||
// * To claim and HTLC output, either with a pre-image or due to a timeout:
|
||||
// * <delay sig> 0
|
||||
//
|
||||
// OP_IF
|
||||
// <revoke key>
|
||||
// OP_ELSE
|
||||
// <delay in blocks>
|
||||
// OP_CHECKSEQUENCEVERIFY
|
||||
// OP_DROP
|
||||
// <delay key>
|
||||
// OP_ENDIF
|
||||
// OP_CHECKSIG
|
||||
//
|
||||
// TODO(roasbeef): possible renames for second-level
|
||||
// * transition?
|
||||
// * covenant output
|
||||
func SecondLevelHtlcScript(revocationKey, delayKey *btcec.PublicKey,
|
||||
csvDelay uint32) ([]byte, error) {
|
||||
|
||||
builder := txscript.NewScriptBuilder()
|
||||
|
||||
// If this is the revocation clause for this script is to be executed,
|
||||
// the spender will push a 1, forcing us to hit the true clause of this
|
||||
// if statement.
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
|
||||
// If this this is the revocation case, then we'll push the revocation
|
||||
// public key on the stack.
|
||||
builder.AddData(revocationKey.SerializeCompressed())
|
||||
|
||||
// Otherwise, this is either the sender or receiver of the HTLC
|
||||
// attempting to claim the HTLC output.
|
||||
builder.AddOp(txscript.OP_ELSE)
|
||||
|
||||
// In order to give the other party time to execute the revocation
|
||||
// clause above, we require a relative timeout to pass before the
|
||||
// output can be spent.
|
||||
builder.AddInt64(int64(csvDelay))
|
||||
builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY)
|
||||
builder.AddOp(txscript.OP_DROP)
|
||||
|
||||
// If the relative timelock passes, then we'll add the delay key to the
|
||||
// stack to ensure that we properly authenticate the spending party.
|
||||
builder.AddData(delayKey.SerializeCompressed())
|
||||
|
||||
// Close out the if statement.
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
|
||||
// In either case, we'll ensure that only either the party possessing
|
||||
// the revocation private key, or the delay private key is able to
|
||||
// spend this output.
|
||||
builder.AddOp(txscript.OP_CHECKSIG)
|
||||
|
||||
return builder.Script()
|
||||
}
|
||||
|
||||
// HtlcSpendSuccess spends a second-level HTLC output. This function is to be
|
||||
// used by the sender of an HTLC to claim the output after a relative timeout
|
||||
// or the receiver of the HTLC to claim on-chain with the pre-image.
|
||||
func HtlcSpendSuccess(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx, csvDelay uint32) (wire.TxWitness, error) {
|
||||
|
||||
// We're required to wait a relative period of time before we can sweep
|
||||
// the output in order to allow the other party to contest our claim of
|
||||
// validity to this version of the commitment transaction.
|
||||
sweepTx.TxIn[0].Sequence = LockTimeToSequence(false, csvDelay)
|
||||
|
||||
// Finally, OP_CSV requires that the version of the transaction
|
||||
// spending a pkscript with OP_CSV within it *must* be >= 2.
|
||||
sweepTx.Version = 2
|
||||
|
||||
// As we mutated the transaction, we'll re-calculate the sighashes for
|
||||
// this instance.
|
||||
signDesc.SigHashes = txscript.NewTxSigHashes(sweepTx)
|
||||
|
||||
// With the proper sequence and version set, we'll now sign the timeout
|
||||
// transaction using the passed signed descriptor. In order to generate
|
||||
// a valid signature, then signDesc should be using the base delay
|
||||
// public key, and the proper single tweak bytes.
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We set a zero as the first element the witness stack (ignoring the
|
||||
// witness script), in order to force execution to the second portion
|
||||
// of the if clause.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = nil
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// HtlcSpendRevoke spends a second-level HTLC output. This function is to be
|
||||
// used by the sender or receiver of an HTLC to claim the HTLC after a revoked
|
||||
// commitment transaction was broadcast.
|
||||
func HtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
||||
revokeTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
// We don't need any spacial modifications to the transaction as this
|
||||
// is just sweeping a revoked HTLC output. So we'll generate a regular
|
||||
// witness signature.
|
||||
sweepSig, err := signer.SignOutputRaw(revokeTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We set a one as the first element the witness stack (ignoring the
|
||||
// witness script), in order to force execution to the revocation
|
||||
// clause in the second level HTLC script.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = []byte{1}
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// HtlcSecondLevelSpend exposes the public witness generation function for
|
||||
// spending an HTLC success transaction, either due to an expiring time lock or
|
||||
// having had the payment preimage. This method is able to spend any
|
||||
// second-level HTLC transaction, assuming the caller sets the locktime or
|
||||
// seqno properly.
|
||||
//
|
||||
// NOTE: The caller MUST set the txn version, sequence number, and sign
|
||||
// descriptor's sig hash cache before invocation.
|
||||
func HtlcSecondLevelSpend(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
// With the proper sequence and version set, we'll now sign the timeout
|
||||
// transaction using the passed signed descriptor. In order to generate
|
||||
// a valid signature, then signDesc should be using the base delay
|
||||
// public key, and the proper single tweak bytes.
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We set a zero as the first element the witness stack (ignoring the
|
||||
// witness script), in order to force execution to the second portion
|
||||
// of the if clause.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll))
|
||||
witnessStack[1] = nil
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// LockTimeToSequence converts the passed relative locktime to a sequence
|
||||
// number in accordance to BIP-68.
|
||||
// See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
|
||||
// * (Compatibility)
|
||||
func LockTimeToSequence(isSeconds bool, locktime uint32) uint32 {
|
||||
if !isSeconds {
|
||||
// The locktime is to be expressed in confirmations.
|
||||
return locktime
|
||||
}
|
||||
|
||||
// Set the 22nd bit which indicates the lock time is in seconds, then
|
||||
// shift the locktime over by 9 since the time granularity is in
|
||||
// 512-second intervals (2^9). This results in a max lock-time of
|
||||
// 33,554,431 seconds, or 1.06 years.
|
||||
return SequenceLockTimeSeconds | (locktime >> 9)
|
||||
}
|
||||
|
||||
// CommitScriptToSelf constructs the public key script for the output on the
|
||||
// commitment transaction paying to the "owner" of said commitment transaction.
|
||||
// If the other party learns of the preimage to the revocation hash, then they
|
||||
// can claim all the settled funds in the channel, plus the unsettled funds.
|
||||
//
|
||||
// Possible Input Scripts:
|
||||
// REVOKE: <sig> 1
|
||||
// SENDRSWEEP: <sig> <emptyvector>
|
||||
//
|
||||
// Output Script:
|
||||
// OP_IF
|
||||
// <revokeKey>
|
||||
// OP_ELSE
|
||||
// <numRelativeBlocks> OP_CHECKSEQUENCEVERIFY OP_DROP
|
||||
// <timeKey>
|
||||
// OP_ENDIF
|
||||
// OP_CHECKSIG
|
||||
func CommitScriptToSelf(csvTimeout uint32, selfKey, revokeKey *btcec.PublicKey) ([]byte, error) {
|
||||
// This script is spendable under two conditions: either the
|
||||
// 'csvTimeout' has passed and we can redeem our funds, or they can
|
||||
// produce a valid signature with the revocation public key. The
|
||||
// revocation public key will *only* be known to the other party if we
|
||||
// have divulged the revocation hash, allowing them to homomorphically
|
||||
// derive the proper private key which corresponds to the revoke public
|
||||
// key.
|
||||
builder := txscript.NewScriptBuilder()
|
||||
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
|
||||
// If a valid signature using the revocation key is presented, then
|
||||
// allow an immediate spend provided the proper signature.
|
||||
builder.AddData(revokeKey.SerializeCompressed())
|
||||
|
||||
builder.AddOp(txscript.OP_ELSE)
|
||||
|
||||
// Otherwise, we can re-claim our funds after a CSV delay of
|
||||
// 'csvTimeout' timeout blocks, and a valid signature.
|
||||
builder.AddInt64(int64(csvTimeout))
|
||||
builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY)
|
||||
builder.AddOp(txscript.OP_DROP)
|
||||
builder.AddData(selfKey.SerializeCompressed())
|
||||
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
|
||||
// Finally, we'll validate the signature against the public key that's
|
||||
// left on the top of the stack.
|
||||
builder.AddOp(txscript.OP_CHECKSIG)
|
||||
|
||||
return builder.Script()
|
||||
}
|
||||
|
||||
// CommitScriptUnencumbered constructs the public key script on the commitment
|
||||
// transaction paying to the "other" party. The constructed output is a normal
|
||||
// p2wkh output spendable immediately, requiring no contestation period.
|
||||
func CommitScriptUnencumbered(key *btcec.PublicKey) ([]byte, error) {
|
||||
// This script goes to the "other" party, and it spendable immediately.
|
||||
builder := txscript.NewScriptBuilder()
|
||||
builder.AddOp(txscript.OP_0)
|
||||
builder.AddData(btcutil.Hash160(key.SerializeCompressed()))
|
||||
|
||||
return builder.Script()
|
||||
}
|
||||
|
||||
// CommitSpendTimeout constructs a valid witness allowing the owner of a
|
||||
// particular commitment transaction to spend the output returning settled
|
||||
// funds back to themselves after a relative block timeout. In order to
|
||||
// properly spend the transaction, the target input's sequence number should be
|
||||
// set accordingly based off of the target relative block timeout within the
|
||||
// redeem script. Additionally, OP_CSV requires that the version of the
|
||||
// transaction spending a pkscript with OP_CSV within it *must* be >= 2.
|
||||
func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
// Ensure the transaction version supports the validation of sequence
|
||||
// locks and CSV semantics.
|
||||
if sweepTx.Version < 2 {
|
||||
return nil, fmt.Errorf("version of passed transaction MUST "+
|
||||
"be >= 2, not %v", sweepTx.Version)
|
||||
}
|
||||
|
||||
// With the sequence number in place, we're now able to properly sign
|
||||
// off on the sweep transaction.
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Place an empty byte as the first item in the evaluated witness stack
|
||||
// to force script execution to the timeout spend clause. We need to
|
||||
// place an empty byte in order to ensure our script is still valid
|
||||
// from the PoV of nodes that are enforcing minimal OP_IF/OP_NOTIF.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = nil
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// CommitSpendRevoke constructs a valid witness allowing a node to sweep the
|
||||
// settled output of a malicious counterparty who broadcasts a revoked
|
||||
// commitment transaction.
|
||||
//
|
||||
// NOTE: The passed SignDescriptor should include the raw (untweaked)
|
||||
// revocation base public key of the receiver and also the proper double tweak
|
||||
// value based on the commitment secret of the revoked commitment.
|
||||
func CommitSpendRevoke(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Place a 1 as the first item in the evaluated witness stack to
|
||||
// force script execution to the revocation clause.
|
||||
witnessStack := wire.TxWitness(make([][]byte, 3))
|
||||
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witnessStack[1] = []byte{1}
|
||||
witnessStack[2] = signDesc.WitnessScript
|
||||
|
||||
return witnessStack, nil
|
||||
}
|
||||
|
||||
// CommitSpendNoDelay constructs a valid witness allowing a node to spend their
|
||||
// settled no-delay output on the counterparty's commitment transaction.
|
||||
//
|
||||
// NOTE: The passed SignDescriptor should include the raw (untweaked) public
|
||||
// key of the receiver and also the proper single tweak value based on the
|
||||
// current commitment point.
|
||||
func CommitSpendNoDelay(signer Signer, signDesc *SignDescriptor,
|
||||
sweepTx *wire.MsgTx) (wire.TxWitness, error) {
|
||||
|
||||
if signDesc.KeyDesc.PubKey == nil {
|
||||
return nil, fmt.Errorf("cannot generate witness with nil " +
|
||||
"KeyDesc pubkey")
|
||||
}
|
||||
|
||||
// This is just a regular p2wkh spend which looks something like:
|
||||
// * witness: <sig> <pubkey>
|
||||
sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Finally, we'll manually craft the witness. The witness here is the
|
||||
// exact same as a regular p2wkh witness, but we'll need to ensure that
|
||||
// we use the tweaked public key as the last item in the witness stack
|
||||
// which was originally used to created the pkScript we're spending.
|
||||
witness := make([][]byte, 2)
|
||||
witness[0] = append(sweepSig, byte(signDesc.HashType))
|
||||
witness[1] = TweakPubKeyWithTweak(
|
||||
signDesc.KeyDesc.PubKey, signDesc.SingleTweak,
|
||||
).SerializeCompressed()
|
||||
|
||||
return witness, nil
|
||||
}
|
||||
|
||||
// SingleTweakBytes computes set of bytes we call the single tweak. The purpose
|
||||
// of the single tweak is to randomize all regular delay and payment base
|
||||
// points. To do this, we generate a hash that binds the commitment point to
|
||||
// the pay/delay base point. The end end results is that the basePoint is
|
||||
// tweaked as follows:
|
||||
//
|
||||
// * key = basePoint + sha256(commitPoint || basePoint)*G
|
||||
func SingleTweakBytes(commitPoint, basePoint *btcec.PublicKey) []byte {
|
||||
h := sha256.New()
|
||||
h.Write(commitPoint.SerializeCompressed())
|
||||
h.Write(basePoint.SerializeCompressed())
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
// TweakPubKey tweaks a public base point given a per commitment point. The per
|
||||
// commitment point is a unique point on our target curve for each commitment
|
||||
// transaction. When tweaking a local base point for use in a remote commitment
|
||||
// transaction, the remote party's current per commitment point is to be used.
|
||||
// The opposite applies for when tweaking remote keys. Precisely, the following
|
||||
// operation is used to "tweak" public keys:
|
||||
//
|
||||
// tweakPub := basePoint + sha256(commitPoint || basePoint) * G
|
||||
// := G*k + sha256(commitPoint || basePoint)*G
|
||||
// := G*(k + sha256(commitPoint || basePoint))
|
||||
//
|
||||
// Therefore, if a party possess the value k, the private key of the base
|
||||
// point, then they are able to derive the private key by computing: compute
|
||||
// the proper private key for the revokeKey by computing:
|
||||
//
|
||||
// revokePriv := k + sha256(commitPoint || basePoint) mod N
|
||||
//
|
||||
// Where N is the order of the sub-group.
|
||||
//
|
||||
// The rationale for tweaking all public keys used within the commitment
|
||||
// contracts is to ensure that all keys are properly delinearized to avoid any
|
||||
// funny business when jointly collaborating to compute public and private
|
||||
// keys. Additionally, the use of the per commitment point ensures that each
|
||||
// commitment state houses a unique set of keys which is useful when creating
|
||||
// blinded channel outsourcing protocols.
|
||||
//
|
||||
// TODO(roasbeef): should be using double-scalar mult here
|
||||
func TweakPubKey(basePoint, commitPoint *btcec.PublicKey) *btcec.PublicKey {
|
||||
tweakBytes := SingleTweakBytes(commitPoint, basePoint)
|
||||
return TweakPubKeyWithTweak(basePoint, tweakBytes)
|
||||
}
|
||||
|
||||
// TweakPubKeyWithTweak is the exact same as the TweakPubKey function, however
|
||||
// it accepts the raw tweak bytes directly rather than the commitment point.
|
||||
func TweakPubKeyWithTweak(pubKey *btcec.PublicKey, tweakBytes []byte) *btcec.PublicKey {
|
||||
curve := btcec.S256()
|
||||
tweakX, tweakY := curve.ScalarBaseMult(tweakBytes)
|
||||
|
||||
// TODO(roasbeef): check that both passed on curve?
|
||||
x, y := curve.Add(pubKey.X, pubKey.Y, tweakX, tweakY)
|
||||
return &btcec.PublicKey{
|
||||
X: x,
|
||||
Y: y,
|
||||
Curve: curve,
|
||||
}
|
||||
}
|
||||
|
||||
// TweakPrivKey tweaks the private key of a public base point given a per
|
||||
// commitment point. The per commitment secret is the revealed revocation
|
||||
// secret for the commitment state in question. This private key will only need
|
||||
// to be generated in the case that a channel counter party broadcasts a
|
||||
// revoked state. Precisely, the following operation is used to derive a
|
||||
// tweaked private key:
|
||||
//
|
||||
// * tweakPriv := basePriv + sha256(commitment || basePub) mod N
|
||||
//
|
||||
// Where N is the order of the sub-group.
|
||||
func TweakPrivKey(basePriv *btcec.PrivateKey, commitTweak []byte) *btcec.PrivateKey {
|
||||
// tweakInt := sha256(commitPoint || basePub)
|
||||
tweakInt := new(big.Int).SetBytes(commitTweak)
|
||||
|
||||
tweakInt = tweakInt.Add(tweakInt, basePriv.D)
|
||||
tweakInt = tweakInt.Mod(tweakInt, btcec.S256().N)
|
||||
|
||||
tweakPriv, _ := btcec.PrivKeyFromBytes(btcec.S256(), tweakInt.Bytes())
|
||||
return tweakPriv
|
||||
}
|
||||
|
||||
// DeriveRevocationPubkey derives the revocation public key given the
|
||||
// counterparty's commitment key, and revocation preimage derived via a
|
||||
// pseudo-random-function. In the event that we (for some reason) broadcast a
|
||||
// revoked commitment transaction, then if the other party knows the revocation
|
||||
// preimage, then they'll be able to derive the corresponding private key to
|
||||
// this private key by exploiting the homomorphism in the elliptic curve group:
|
||||
// * https://en.wikipedia.org/wiki/Group_homomorphism#Homomorphisms_of_abelian_groups
|
||||
//
|
||||
// The derivation is performed as follows:
|
||||
//
|
||||
// revokeKey := revokeBase * sha256(revocationBase || commitPoint) +
|
||||
// commitPoint * sha256(commitPoint || revocationBase)
|
||||
//
|
||||
// := G*(revokeBasePriv * sha256(revocationBase || commitPoint)) +
|
||||
// G*(commitSecret * sha256(commitPoint || revocationBase))
|
||||
//
|
||||
// := G*(revokeBasePriv * sha256(revocationBase || commitPoint) +
|
||||
// commitSecret * sha256(commitPoint || revocationBase))
|
||||
//
|
||||
// Therefore, once we divulge the revocation secret, the remote peer is able to
|
||||
// compute the proper private key for the revokeKey by computing:
|
||||
//
|
||||
// revokePriv := (revokeBasePriv * sha256(revocationBase || commitPoint)) +
|
||||
// (commitSecret * sha256(commitPoint || revocationBase)) mod N
|
||||
//
|
||||
// Where N is the order of the sub-group.
|
||||
func DeriveRevocationPubkey(revokeBase, commitPoint *btcec.PublicKey) *btcec.PublicKey {
|
||||
|
||||
// R = revokeBase * sha256(revocationBase || commitPoint)
|
||||
revokeTweakBytes := SingleTweakBytes(revokeBase, commitPoint)
|
||||
rX, rY := btcec.S256().ScalarMult(revokeBase.X, revokeBase.Y,
|
||||
revokeTweakBytes)
|
||||
|
||||
// C = commitPoint * sha256(commitPoint || revocationBase)
|
||||
commitTweakBytes := SingleTweakBytes(commitPoint, revokeBase)
|
||||
cX, cY := btcec.S256().ScalarMult(commitPoint.X, commitPoint.Y,
|
||||
commitTweakBytes)
|
||||
|
||||
// Now that we have the revocation point, we add this to their commitment
|
||||
// public key in order to obtain the revocation public key.
|
||||
//
|
||||
// P = R + C
|
||||
revX, revY := btcec.S256().Add(rX, rY, cX, cY)
|
||||
return &btcec.PublicKey{
|
||||
X: revX,
|
||||
Y: revY,
|
||||
Curve: btcec.S256(),
|
||||
}
|
||||
}
|
||||
|
||||
// DeriveRevocationPrivKey derives the revocation private key given a node's
|
||||
// commitment private key, and the preimage to a previously seen revocation
|
||||
// hash. Using this derived private key, a node is able to claim the output
|
||||
// within the commitment transaction of a node in the case that they broadcast
|
||||
// a previously revoked commitment transaction.
|
||||
//
|
||||
// The private key is derived as follows:
|
||||
// revokePriv := (revokeBasePriv * sha256(revocationBase || commitPoint)) +
|
||||
// (commitSecret * sha256(commitPoint || revocationBase)) mod N
|
||||
//
|
||||
// Where N is the order of the sub-group.
|
||||
func DeriveRevocationPrivKey(revokeBasePriv *btcec.PrivateKey,
|
||||
commitSecret *btcec.PrivateKey) *btcec.PrivateKey {
|
||||
|
||||
// r = sha256(revokeBasePub || commitPoint)
|
||||
revokeTweakBytes := SingleTweakBytes(revokeBasePriv.PubKey(),
|
||||
commitSecret.PubKey())
|
||||
revokeTweakInt := new(big.Int).SetBytes(revokeTweakBytes)
|
||||
|
||||
// c = sha256(commitPoint || revokeBasePub)
|
||||
commitTweakBytes := SingleTweakBytes(commitSecret.PubKey(),
|
||||
revokeBasePriv.PubKey())
|
||||
commitTweakInt := new(big.Int).SetBytes(commitTweakBytes)
|
||||
|
||||
// Finally to derive the revocation secret key we'll perform the
|
||||
// following operation:
|
||||
//
|
||||
// k = (revocationPriv * r) + (commitSecret * c) mod N
|
||||
//
|
||||
// This works since:
|
||||
// P = (G*a)*b + (G*c)*d
|
||||
// P = G*(a*b) + G*(c*d)
|
||||
// P = G*(a*b + c*d)
|
||||
revokeHalfPriv := revokeTweakInt.Mul(revokeTweakInt, revokeBasePriv.D)
|
||||
commitHalfPriv := commitTweakInt.Mul(commitTweakInt, commitSecret.D)
|
||||
|
||||
revocationPriv := revokeHalfPriv.Add(revokeHalfPriv, commitHalfPriv)
|
||||
revocationPriv = revocationPriv.Mod(revocationPriv, btcec.S256().N)
|
||||
|
||||
priv, _ := btcec.PrivKeyFromBytes(btcec.S256(), revocationPriv.Bytes())
|
||||
return priv
|
||||
}
|
||||
|
||||
// ComputeCommitmentPoint generates a commitment point given a commitment
|
||||
// secret. The commitment point for each state is used to randomize each key in
|
||||
// the key-ring and also to used as a tweak to derive new public+private keys
|
||||
// for the state.
|
||||
func ComputeCommitmentPoint(commitSecret []byte) *btcec.PublicKey {
|
||||
x, y := btcec.S256().ScalarBaseMult(commitSecret)
|
||||
|
||||
return &btcec.PublicKey{
|
||||
X: x,
|
||||
Y: y,
|
||||
Curve: btcec.S256(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,917 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
t.Parallel()
|
||||
|
||||
// First, we'll generate a commitment point, and a commitment secret.
|
||||
// These will be used to derive the ultimate revocation keys.
|
||||
revocationPreimage := testHdSeed.CloneBytes()
|
||||
commitSecret, commitPoint := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
revocationPreimage)
|
||||
|
||||
// With the commitment secrets generated, we'll now create the base
|
||||
// keys we'll use to derive the revocation key from.
|
||||
basePriv, basePub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
testWalletPrivKey)
|
||||
|
||||
// With the point and key obtained, we can now derive the revocation
|
||||
// key itself.
|
||||
revocationPub := DeriveRevocationPubkey(basePub, commitPoint)
|
||||
|
||||
// The revocation public key derived from the original public key, and
|
||||
// the one derived from the private key should be identical.
|
||||
revocationPriv := DeriveRevocationPrivKey(basePriv, commitSecret)
|
||||
if !revocationPub.IsEqual(revocationPriv.PubKey()) {
|
||||
t.Fatalf("derived public keys don't match!")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTweakKeyDerivation tests that given a public key, and commitment tweak,
|
||||
// then we're able to properly derive a tweaked private key that corresponds to
|
||||
// the computed tweak public key. This scenario ensure that our key derivation
|
||||
// for any of the non revocation keys on the commitment transaction is correct.
|
||||
func TestTweakKeyDerivation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// First, we'll generate a base public key that we'll be "tweaking".
|
||||
baseSecret := testHdSeed.CloneBytes()
|
||||
basePriv, basePub := btcec.PrivKeyFromBytes(btcec.S256(), baseSecret)
|
||||
|
||||
// With the base key create, we'll now create a commitment point, and
|
||||
// from that derive the bytes we'll used to tweak the base public key.
|
||||
commitPoint := ComputeCommitmentPoint(bobsPrivKey)
|
||||
commitTweak := SingleTweakBytes(commitPoint, basePub)
|
||||
|
||||
// Next, we'll modify the public key. When we apply the same operation
|
||||
// to the private key we should get a key that matches.
|
||||
tweakedPub := TweakPubKey(basePub, commitPoint)
|
||||
|
||||
// Finally, attempt to re-generate the private key that matches the
|
||||
// tweaked public key. The derived key should match exactly.
|
||||
derivedPriv := TweakPrivKey(basePriv, commitTweak)
|
||||
if !derivedPriv.PubKey().IsEqual(tweakedPub) {
|
||||
t.Fatalf("pub keys don't match")
|
||||
}
|
||||
}
|
||||
|
||||
// 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 fatally.
|
||||
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:
|
||||
// * receiver spends
|
||||
// * revoke w/ sig
|
||||
// * HTLC with invalid preimage size
|
||||
// * HTLC with valid preimage size + sig
|
||||
// * sender spends
|
||||
// * invalid lock-time for CLTV
|
||||
// * invalid sequence for CSV
|
||||
// * valid lock-time+sequence, valid sig
|
||||
func TestHTLCSenderSpendValidation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// We generate a fake output, and the corresponding txin. This output
|
||||
// doesn't need to exist, as we'll only be validating spending from the
|
||||
// transaction that references this.
|
||||
txid, err := chainhash.NewHash(testHdSeed.CloneBytes())
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create txid: %v", err)
|
||||
}
|
||||
fundingOut := &wire.OutPoint{
|
||||
Hash: *txid,
|
||||
Index: 50,
|
||||
}
|
||||
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
||||
|
||||
// Next we'll the commitment secret for our commitment tx and also the
|
||||
// revocation key that we'll use as well.
|
||||
revokePreimage := testHdSeed.CloneBytes()
|
||||
commitSecret, commitPoint := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
revokePreimage)
|
||||
|
||||
// Generate a payment preimage to be used below.
|
||||
paymentPreimage := revokePreimage
|
||||
paymentPreimage[0] ^= 1
|
||||
paymentHash := sha256.Sum256(paymentPreimage[:])
|
||||
|
||||
// We'll also need some tests keys for alice and bob, and metadata of
|
||||
// the HTLC output.
|
||||
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
testWalletPrivKey)
|
||||
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
bobsPrivKey)
|
||||
paymentAmt := btcutil.Amount(1 * 10e8)
|
||||
|
||||
aliceLocalKey := TweakPubKey(aliceKeyPub, commitPoint)
|
||||
bobLocalKey := TweakPubKey(bobKeyPub, commitPoint)
|
||||
|
||||
// As we'll be modeling spends from Alice's commitment transaction,
|
||||
// we'll be using Bob's base point for the revocation key.
|
||||
revocationKey := DeriveRevocationPubkey(bobKeyPub, commitPoint)
|
||||
|
||||
// Generate the raw HTLC redemption scripts, and its p2wsh counterpart.
|
||||
htlcWitnessScript, err := SenderHTLCScript(aliceLocalKey, bobLocalKey,
|
||||
revocationKey, paymentHash[:])
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create htlc sender script: %v", err)
|
||||
}
|
||||
htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
|
||||
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 path to (could be Bob, could
|
||||
// be multiple hops down, it doesn't really matter).
|
||||
htlcOutput := &wire.TxOut{
|
||||
Value: int64(paymentAmt),
|
||||
PkScript: htlcPkScript,
|
||||
}
|
||||
senderCommitTx := wire.NewMsgTx(2)
|
||||
senderCommitTx.AddTxIn(fakeFundingTxIn)
|
||||
senderCommitTx.AddTxOut(htlcOutput)
|
||||
|
||||
prevOut := &wire.OutPoint{
|
||||
Hash: senderCommitTx.TxHash(),
|
||||
Index: 0,
|
||||
}
|
||||
|
||||
sweepTx := wire.NewMsgTx(2)
|
||||
sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil))
|
||||
sweepTx.AddTxOut(
|
||||
&wire.TxOut{
|
||||
PkScript: []byte("doesn't matter"),
|
||||
Value: 1 * 10e8,
|
||||
},
|
||||
)
|
||||
sweepTxSigHashes := txscript.NewTxSigHashes(sweepTx)
|
||||
|
||||
bobCommitTweak := SingleTweakBytes(commitPoint, bobKeyPub)
|
||||
aliceCommitTweak := SingleTweakBytes(commitPoint, aliceKeyPub)
|
||||
|
||||
// Finally, we'll create mock signers for both of them based on their
|
||||
// private keys. This test simplifies a bit and uses the same key as
|
||||
// the base point for all scripts and derivations.
|
||||
bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}}
|
||||
aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}}
|
||||
|
||||
// We'll also generate a signature on the sweep transaction above
|
||||
// that will act as Bob's signature to Alice for the second level HTLC
|
||||
// transaction.
|
||||
bobSignDesc := SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: bobCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
bobRecvrSig, err := bobSigner.SignOutputRaw(sweepTx, &bobSignDesc)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate alice signature: %v", err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
witness func() wire.TxWitness
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
// revoke w/ sig
|
||||
// TODO(roasbeef): test invalid revoke
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
DoubleTweak: commitSecret,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return SenderHtlcSpendRevokeWithKey(bobSigner, signDesc,
|
||||
revocationKey, sweepTx)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
{
|
||||
// HTLC with invalid preimage size
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: bobCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return SenderHtlcSpendRedeem(bobSigner, signDesc,
|
||||
sweepTx,
|
||||
// Invalid preimage length
|
||||
bytes.Repeat([]byte{1}, 45))
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
// HTLC with valid preimage size + sig
|
||||
// TODO(roasbeef): invalid preimage
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: bobCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return SenderHtlcSpendRedeem(bobSigner, signDesc,
|
||||
sweepTx, paymentPreimage)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
{
|
||||
// valid spend to the transition the state of the HTLC
|
||||
// output with the second level HTLC timeout
|
||||
// transaction.
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
SingleTweak: aliceCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return SenderHtlcSpendTimeout(bobRecvrSig, aliceSigner,
|
||||
signDesc, sweepTx)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO(roasbeef): set of cases to ensure able to sign w/ keypath and
|
||||
// not
|
||||
|
||||
for i, testCase := range testCases {
|
||||
sweepTx.TxIn[0].Witness = testCase.witness()
|
||||
|
||||
vm, err := txscript.NewEngine(htlcPkScript,
|
||||
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
||||
nil, int64(paymentAmt))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create engine: %v", err)
|
||||
}
|
||||
|
||||
// This buffer will trace execution of the Script, only dumping
|
||||
// out to stdout in the case that a test fails.
|
||||
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)
|
||||
}
|
||||
|
||||
debugBuf.WriteString(fmt.Sprintf("Stack: %v", vm.GetStack()))
|
||||
debugBuf.WriteString(fmt.Sprintf("AltStack: %v", vm.GetAltStack()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestHTLCReceiverSpendValidation tests all possible valid+invalid redemption
|
||||
// paths in the script used within the receiver's commitment transaction for an
|
||||
// incoming HTLC.
|
||||
//
|
||||
// The following cases are exercised by this test:
|
||||
// * receiver spends
|
||||
// * 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) {
|
||||
t.Parallel()
|
||||
|
||||
// We generate a fake output, and the corresponding txin. This output
|
||||
// doesn't need to exist, as we'll only be validating spending from the
|
||||
// transaction that references this.
|
||||
txid, err := chainhash.NewHash(testHdSeed.CloneBytes())
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create txid: %v", err)
|
||||
}
|
||||
fundingOut := &wire.OutPoint{
|
||||
Hash: *txid,
|
||||
Index: 50,
|
||||
}
|
||||
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
||||
|
||||
// Next we'll the commitment secret for our commitment tx and also the
|
||||
// revocation key that we'll use as well.
|
||||
revokePreimage := testHdSeed.CloneBytes()
|
||||
commitSecret, commitPoint := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
revokePreimage)
|
||||
|
||||
// Generate a payment preimage to be used below.
|
||||
paymentPreimage := revokePreimage
|
||||
paymentPreimage[0] ^= 1
|
||||
paymentHash := sha256.Sum256(paymentPreimage[:])
|
||||
|
||||
// We'll also need some tests keys for alice and bob, and metadata of
|
||||
// the HTLC output.
|
||||
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
testWalletPrivKey)
|
||||
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
bobsPrivKey)
|
||||
paymentAmt := btcutil.Amount(1 * 10e8)
|
||||
cltvTimeout := uint32(8)
|
||||
|
||||
aliceLocalKey := TweakPubKey(aliceKeyPub, commitPoint)
|
||||
bobLocalKey := TweakPubKey(bobKeyPub, commitPoint)
|
||||
|
||||
// As we'll be modeling spends from Bob's commitment transaction, we'll
|
||||
// be using Alice's base point for the revocation key.
|
||||
revocationKey := DeriveRevocationPubkey(aliceKeyPub, commitPoint)
|
||||
|
||||
// Generate the raw HTLC redemption scripts, and its p2wsh counterpart.
|
||||
htlcWitnessScript, err := ReceiverHTLCScript(cltvTimeout, aliceLocalKey,
|
||||
bobLocalKey, revocationKey, paymentHash[:])
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create htlc sender script: %v", err)
|
||||
}
|
||||
htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
|
||||
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 path to (could be Bob, could be
|
||||
// multiple hops down, it doesn't really matter).
|
||||
htlcOutput := &wire.TxOut{
|
||||
Value: int64(paymentAmt),
|
||||
PkScript: htlcWitnessScript,
|
||||
}
|
||||
|
||||
receiverCommitTx := wire.NewMsgTx(2)
|
||||
receiverCommitTx.AddTxIn(fakeFundingTxIn)
|
||||
receiverCommitTx.AddTxOut(htlcOutput)
|
||||
|
||||
prevOut := &wire.OutPoint{
|
||||
Hash: receiverCommitTx.TxHash(),
|
||||
Index: 0,
|
||||
}
|
||||
|
||||
sweepTx := wire.NewMsgTx(2)
|
||||
sweepTx.AddTxIn(&wire.TxIn{
|
||||
PreviousOutPoint: *prevOut,
|
||||
})
|
||||
sweepTx.AddTxOut(
|
||||
&wire.TxOut{
|
||||
PkScript: []byte("doesn't matter"),
|
||||
Value: 1 * 10e8,
|
||||
},
|
||||
)
|
||||
sweepTxSigHashes := txscript.NewTxSigHashes(sweepTx)
|
||||
|
||||
bobCommitTweak := SingleTweakBytes(commitPoint, bobKeyPub)
|
||||
aliceCommitTweak := SingleTweakBytes(commitPoint, aliceKeyPub)
|
||||
|
||||
// Finally, we'll create mock signers for both of them based on their
|
||||
// private keys. This test simplifies a bit and uses the same key as
|
||||
// the base point for all scripts and derivations.
|
||||
bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}}
|
||||
aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}}
|
||||
|
||||
// We'll also generate a signature on the sweep transaction above
|
||||
// that will act as Alice's signature to Bob for the second level HTLC
|
||||
// transaction.
|
||||
aliceSignDesc := SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
SingleTweak: aliceCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
aliceSenderSig, err := aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate alice signature: %v", err)
|
||||
}
|
||||
|
||||
// TODO(roasbeef): modify valid to check precise script errors?
|
||||
testCases := []struct {
|
||||
witness func() wire.TxWitness
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
// HTLC redemption w/ invalid preimage size
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: bobCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return ReceiverHtlcSpendRedeem(aliceSenderSig,
|
||||
bytes.Repeat([]byte{1}, 45), bobSigner,
|
||||
signDesc, sweepTx)
|
||||
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
// HTLC redemption w/ valid preimage size
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: bobCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return ReceiverHtlcSpendRedeem(aliceSenderSig,
|
||||
paymentPreimage[:], bobSigner,
|
||||
signDesc, sweepTx)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
{
|
||||
// revoke w/ sig
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
DoubleTweak: commitSecret,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return ReceiverHtlcSpendRevokeWithKey(aliceSigner,
|
||||
signDesc, revocationKey, sweepTx)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
{
|
||||
// refund w/ invalid lock time
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
SingleTweak: aliceCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return ReceiverHtlcSpendTimeout(aliceSigner, signDesc,
|
||||
sweepTx, int32(cltvTimeout-2))
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
// refund w/ valid lock time
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
SingleTweak: aliceCommitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return ReceiverHtlcSpendTimeout(aliceSigner, signDesc,
|
||||
sweepTx, int32(cltvTimeout))
|
||||
}),
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
sweepTx.TxIn[0].Witness = testCase.witness()
|
||||
|
||||
vm, err := txscript.NewEngine(htlcPkScript,
|
||||
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
||||
nil, int64(paymentAmt))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create engine: %v", err)
|
||||
}
|
||||
|
||||
// This buffer will trace execution of the Script, only dumping
|
||||
// out to stdout in the case that a test fails.
|
||||
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)
|
||||
}
|
||||
|
||||
debugBuf.WriteString(fmt.Sprintf("Stack: %v", vm.GetStack()))
|
||||
debugBuf.WriteString(fmt.Sprintf("AltStack: %v", vm.GetAltStack()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSecondLevelHtlcSpends tests all the possible redemption clauses from the
|
||||
// HTLC success and timeout covenant transactions.
|
||||
func TestSecondLevelHtlcSpends(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// We'll start be creating a creating a 2BTC HTLC.
|
||||
const htlcAmt = btcutil.Amount(2 * 10e8)
|
||||
|
||||
// In all of our scenarios, the CSV timeout to claim a self output will
|
||||
// be 5 blocks.
|
||||
const claimDelay = 5
|
||||
|
||||
// First we'll set up some initial key state for Alice and Bob that
|
||||
// will be used in the scripts we created below.
|
||||
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
testWalletPrivKey)
|
||||
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
||||
bobsPrivKey)
|
||||
|
||||
revokePreimage := testHdSeed.CloneBytes()
|
||||
commitSecret, commitPoint := btcec.PrivKeyFromBytes(
|
||||
btcec.S256(), revokePreimage)
|
||||
|
||||
// As we're modeling this as Bob sweeping the HTLC on-chain from his
|
||||
// commitment transaction after a period of time, we'll be using a
|
||||
// revocation key derived from Alice's base point and his secret.
|
||||
revocationKey := DeriveRevocationPubkey(aliceKeyPub, commitPoint)
|
||||
|
||||
// Next, craft a fake HTLC outpoint that we'll use to generate the
|
||||
// sweeping transaction using.
|
||||
txid, err := chainhash.NewHash(testHdSeed.CloneBytes())
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create txid: %v", err)
|
||||
}
|
||||
htlcOutPoint := &wire.OutPoint{
|
||||
Hash: *txid,
|
||||
Index: 0,
|
||||
}
|
||||
sweepTx := wire.NewMsgTx(2)
|
||||
sweepTx.AddTxIn(wire.NewTxIn(htlcOutPoint, nil, nil))
|
||||
sweepTx.AddTxOut(
|
||||
&wire.TxOut{
|
||||
PkScript: []byte("doesn't matter"),
|
||||
Value: 1 * 10e8,
|
||||
},
|
||||
)
|
||||
sweepTxSigHashes := txscript.NewTxSigHashes(sweepTx)
|
||||
|
||||
// The delay key will be crafted using Bob's public key as the output
|
||||
// we created will be spending from Alice's commitment transaction.
|
||||
delayKey := TweakPubKey(bobKeyPub, commitPoint)
|
||||
|
||||
// The commit tweak will be required in order for Bob to derive the
|
||||
// proper key need to spend the output.
|
||||
commitTweak := SingleTweakBytes(commitPoint, bobKeyPub)
|
||||
|
||||
// Finally we'll generate the HTLC script itself that we'll be spending
|
||||
// from. The revocation clause can be claimed by Alice, while Bob can
|
||||
// sweep the output after a particular delay.
|
||||
htlcWitnessScript, err := SecondLevelHtlcScript(revocationKey,
|
||||
delayKey, claimDelay)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create htlc script: %v", err)
|
||||
}
|
||||
htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create htlc output: %v", err)
|
||||
}
|
||||
|
||||
htlcOutput := &wire.TxOut{
|
||||
PkScript: htlcPkScript,
|
||||
Value: int64(htlcAmt),
|
||||
}
|
||||
|
||||
// TODO(roasbeef): make actually use timeout/success txns?
|
||||
|
||||
// Finally, we'll create mock signers for both of them based on their
|
||||
// private keys. This test simplifies a bit and uses the same key as
|
||||
// the base point for all scripts and derivations.
|
||||
bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}}
|
||||
aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}}
|
||||
|
||||
testCases := []struct {
|
||||
witness func() wire.TxWitness
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
// Sender of the HTLC attempts to activate the
|
||||
// revocation clause, but uses the wrong key (fails to
|
||||
// use the double tweak in this case).
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return HtlcSpendRevoke(aliceSigner, signDesc,
|
||||
sweepTx)
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
// Sender of HTLC activates the revocation clause.
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: aliceKeyPub,
|
||||
},
|
||||
DoubleTweak: commitSecret,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return HtlcSpendRevoke(aliceSigner, signDesc,
|
||||
sweepTx)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
{
|
||||
// Receiver of the HTLC attempts to sweep, but tries to
|
||||
// do so pre-maturely with a smaller CSV delay (2
|
||||
// blocks instead of 5 blocks).
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: commitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return HtlcSpendSuccess(bobSigner, signDesc,
|
||||
sweepTx, claimDelay-3)
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
// Receiver of the HTLC sweeps with the proper CSV
|
||||
// delay, but uses the wrong key (leaves off the single
|
||||
// tweak).
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return HtlcSpendSuccess(bobSigner, signDesc,
|
||||
sweepTx, claimDelay)
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
// Receiver of the HTLC sweeps with the proper CSV
|
||||
// delay, and the correct key.
|
||||
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
|
||||
signDesc := &SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: bobKeyPub,
|
||||
},
|
||||
SingleTweak: commitTweak,
|
||||
WitnessScript: htlcWitnessScript,
|
||||
Output: htlcOutput,
|
||||
HashType: txscript.SigHashAll,
|
||||
SigHashes: sweepTxSigHashes,
|
||||
InputIndex: 0,
|
||||
}
|
||||
|
||||
return HtlcSpendSuccess(bobSigner, signDesc,
|
||||
sweepTx, claimDelay)
|
||||
}),
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
sweepTx.TxIn[0].Witness = testCase.witness()
|
||||
|
||||
vm, err := txscript.NewEngine(htlcPkScript,
|
||||
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
||||
nil, int64(htlcAmt))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create engine: %v", err)
|
||||
}
|
||||
|
||||
// This buffer will trace execution of the Script, only dumping
|
||||
// out to stdout in the case that a test fails.
|
||||
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)
|
||||
}
|
||||
|
||||
debugBuf.WriteString(fmt.Sprintf("Stack: %v", vm.GetStack()))
|
||||
debugBuf.WriteString(fmt.Sprintf("AltStack: %v", vm.GetAltStack()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSpecificationKeyDerivation implements the test vectors provided in
|
||||
// BOLT-03, Appendix E.
|
||||
func TestSpecificationKeyDerivation(t *testing.T) {
|
||||
const (
|
||||
baseSecretHex = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
|
||||
perCommitmentSecretHex = "1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100"
|
||||
basePointHex = "036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2"
|
||||
perCommitmentPointHex = "025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486"
|
||||
)
|
||||
|
||||
baseSecret, err := privkeyFromHex(baseSecretHex)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse serialized privkey: %v", err)
|
||||
}
|
||||
perCommitmentSecret, err := privkeyFromHex(perCommitmentSecretHex)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse serialized privkey: %v", err)
|
||||
}
|
||||
basePoint, err := pubkeyFromHex(basePointHex)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse serialized pubkey: %v", err)
|
||||
}
|
||||
perCommitmentPoint, err := pubkeyFromHex(perCommitmentPointHex)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse serialized pubkey: %v", err)
|
||||
}
|
||||
|
||||
// name: derivation of key from basepoint and per_commitment_point
|
||||
const expectedLocalKeyHex = "0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5"
|
||||
actualLocalKey := TweakPubKey(basePoint, perCommitmentPoint)
|
||||
actualLocalKeyHex := pubkeyToHex(actualLocalKey)
|
||||
if actualLocalKeyHex != expectedLocalKeyHex {
|
||||
t.Errorf("Incorrect derivation of local public key: "+
|
||||
"expected %v, got %v", expectedLocalKeyHex, actualLocalKeyHex)
|
||||
}
|
||||
|
||||
// name: derivation of secret key from basepoint secret and per_commitment_secret
|
||||
const expectedLocalPrivKeyHex = "cbced912d3b21bf196a766651e436aff192362621ce317704ea2f75d87e7be0f"
|
||||
tweak := SingleTweakBytes(perCommitmentPoint, basePoint)
|
||||
actualLocalPrivKey := TweakPrivKey(baseSecret, tweak)
|
||||
actualLocalPrivKeyHex := privkeyToHex(actualLocalPrivKey)
|
||||
if actualLocalPrivKeyHex != expectedLocalPrivKeyHex {
|
||||
t.Errorf("Incorrect derivation of local private key: "+
|
||||
"expected %v, got %v, %v", expectedLocalPrivKeyHex,
|
||||
actualLocalPrivKeyHex, hex.EncodeToString(tweak))
|
||||
}
|
||||
|
||||
// name: derivation of revocation key from basepoint and per_commitment_point
|
||||
const expectedRevocationKeyHex = "02916e326636d19c33f13e8c0c3a03dd157f332f3e99c317c141dd865eb01f8ff0"
|
||||
actualRevocationKey := DeriveRevocationPubkey(basePoint, perCommitmentPoint)
|
||||
actualRevocationKeyHex := pubkeyToHex(actualRevocationKey)
|
||||
if actualRevocationKeyHex != expectedRevocationKeyHex {
|
||||
t.Errorf("Incorrect derivation of revocation public key: "+
|
||||
"expected %v, got %v", expectedRevocationKeyHex,
|
||||
actualRevocationKeyHex)
|
||||
}
|
||||
|
||||
// name: derivation of revocation secret from basepoint_secret and per_commitment_secret
|
||||
const expectedRevocationPrivKeyHex = "d09ffff62ddb2297ab000cc85bcb4283fdeb6aa052affbc9dddcf33b61078110"
|
||||
actualRevocationPrivKey := DeriveRevocationPrivKey(baseSecret,
|
||||
perCommitmentSecret)
|
||||
actualRevocationPrivKeyHex := privkeyToHex(actualRevocationPrivKey)
|
||||
if actualRevocationPrivKeyHex != expectedRevocationPrivKeyHex {
|
||||
t.Errorf("Incorrect derivation of revocation private key: "+
|
||||
"expected %v, got %v", expectedRevocationPrivKeyHex,
|
||||
actualRevocationPrivKeyHex)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrTweakOverdose signals a SignDescriptor is invalid because both of its
|
||||
// SingleTweak and DoubleTweak are non-nil.
|
||||
ErrTweakOverdose = errors.New("sign descriptor should only have one tweak")
|
||||
)
|
||||
|
||||
// SignDescriptor houses the necessary information required to successfully sign
|
||||
// a given output. This struct is used by the Signer interface in order to gain
|
||||
// access to critical data needed to generate a valid signature.
|
||||
type SignDescriptor struct {
|
||||
// KeyDesc is a descriptor that precisely describes *which* key to use
|
||||
// for signing. This may provide the raw public key directly, or
|
||||
// require the Signer to re-derive the key according to the populated
|
||||
// derivation path.
|
||||
KeyDesc keychain.KeyDescriptor
|
||||
|
||||
// SingleTweak is a scalar value that will be added to the private key
|
||||
// corresponding to the above public key to obtain the private key to
|
||||
// be used to sign this input. This value is typically derived via the
|
||||
// following computation:
|
||||
//
|
||||
// * derivedKey = privkey + sha256(perCommitmentPoint || pubKey) mod N
|
||||
//
|
||||
// NOTE: If this value is nil, then the input can be signed using only
|
||||
// the above public key. Either a SingleTweak should be set or a
|
||||
// DoubleTweak, not both.
|
||||
SingleTweak []byte
|
||||
|
||||
// DoubleTweak is a private key that will be used in combination with
|
||||
// its corresponding private key to derive the private key that is to
|
||||
// be used to sign the target input. Within the Lightning protocol,
|
||||
// this value is typically the commitment secret from a previously
|
||||
// revoked commitment transaction. This value is in combination with
|
||||
// two hash values, and the original private key to derive the private
|
||||
// key to be used when signing.
|
||||
//
|
||||
// * k = (privKey*sha256(pubKey || tweakPub) +
|
||||
// tweakPriv*sha256(tweakPub || pubKey)) mod N
|
||||
//
|
||||
// NOTE: If this value is nil, then the input can be signed using only
|
||||
// the above public key. Either a SingleTweak should be set or a
|
||||
// DoubleTweak, not both.
|
||||
DoubleTweak *btcec.PrivateKey
|
||||
|
||||
// WitnessScript is the full script required to properly redeem the
|
||||
// output. This field will only be populated if a p2wsh or a p2sh
|
||||
// output is being signed.
|
||||
WitnessScript []byte
|
||||
|
||||
// Output is the target output which should be signed. The PkScript and
|
||||
// Value fields within the output should be properly populated,
|
||||
// otherwise an invalid signature may be generated.
|
||||
Output *wire.TxOut
|
||||
|
||||
// HashType is the target sighash type that should be used when
|
||||
// generating the final sighash, and signature.
|
||||
HashType txscript.SigHashType
|
||||
|
||||
// SigHashes is the pre-computed sighash midstate to be used when
|
||||
// generating the final sighash for signing.
|
||||
SigHashes *txscript.TxSigHashes
|
||||
|
||||
// InputIndex is the target input within the transaction that should be
|
||||
// signed.
|
||||
InputIndex int
|
||||
}
|
||||
|
||||
// WriteSignDescriptor serializes a SignDescriptor struct into the passed
|
||||
// io.Writer stream.
|
||||
//
|
||||
// NOTE: We assume the SigHashes and InputIndex fields haven't been assigned
|
||||
// yet, since that is usually done just before broadcast by the witness
|
||||
// generator.
|
||||
func WriteSignDescriptor(w io.Writer, sd *SignDescriptor) error {
|
||||
err := binary.Write(w, binary.BigEndian, sd.KeyDesc.Family)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = binary.Write(w, binary.BigEndian, sd.KeyDesc.Index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = binary.Write(w, binary.BigEndian, sd.KeyDesc.PubKey != nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sd.KeyDesc.PubKey != nil {
|
||||
serializedPubKey := sd.KeyDesc.PubKey.SerializeCompressed()
|
||||
if err := wire.WriteVarBytes(w, 0, serializedPubKey); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := wire.WriteVarBytes(w, 0, sd.SingleTweak); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var doubleTweakBytes []byte
|
||||
if sd.DoubleTweak != nil {
|
||||
doubleTweakBytes = sd.DoubleTweak.Serialize()
|
||||
}
|
||||
if err := wire.WriteVarBytes(w, 0, doubleTweakBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := wire.WriteVarBytes(w, 0, sd.WitnessScript); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writeTxOut(w, sd.Output); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var scratch [4]byte
|
||||
binary.BigEndian.PutUint32(scratch[:], uint32(sd.HashType))
|
||||
if _, err := w.Write(scratch[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadSignDescriptor deserializes a SignDescriptor struct from the passed
|
||||
// io.Reader stream.
|
||||
func ReadSignDescriptor(r io.Reader, sd *SignDescriptor) error {
|
||||
err := binary.Read(r, binary.BigEndian, &sd.KeyDesc.Family)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = binary.Read(r, binary.BigEndian, &sd.KeyDesc.Index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var hasKey bool
|
||||
err = binary.Read(r, binary.BigEndian, &hasKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if hasKey {
|
||||
pubKeyBytes, err := wire.ReadVarBytes(r, 0, 34, "pubkey")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sd.KeyDesc.PubKey, err = btcec.ParsePubKey(
|
||||
pubKeyBytes, btcec.S256(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
singleTweak, err := wire.ReadVarBytes(r, 0, 32, "singleTweak")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Serializing a SignDescriptor with a nil-valued SingleTweak results
|
||||
// in deserializing a zero-length slice. Since a nil-valued SingleTweak
|
||||
// has special meaning and a zero-length slice for a SingleTweak is
|
||||
// invalid, we can use the zero-length slice as the flag for a
|
||||
// nil-valued SingleTweak.
|
||||
if len(singleTweak) == 0 {
|
||||
sd.SingleTweak = nil
|
||||
} else {
|
||||
sd.SingleTweak = singleTweak
|
||||
}
|
||||
|
||||
doubleTweakBytes, err := wire.ReadVarBytes(r, 0, 32, "doubleTweak")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Serializing a SignDescriptor with a nil-valued DoubleTweak results
|
||||
// in deserializing a zero-length slice. Since a nil-valued DoubleTweak
|
||||
// has special meaning and a zero-length slice for a DoubleTweak is
|
||||
// invalid, we can use the zero-length slice as the flag for a
|
||||
// nil-valued DoubleTweak.
|
||||
if len(doubleTweakBytes) == 0 {
|
||||
sd.DoubleTweak = nil
|
||||
} else {
|
||||
sd.DoubleTweak, _ = btcec.PrivKeyFromBytes(btcec.S256(), doubleTweakBytes)
|
||||
}
|
||||
|
||||
// Only one tweak should ever be set, fail if both are present.
|
||||
if sd.SingleTweak != nil && sd.DoubleTweak != nil {
|
||||
return ErrTweakOverdose
|
||||
}
|
||||
|
||||
witnessScript, err := wire.ReadVarBytes(r, 0, 500, "witnessScript")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sd.WitnessScript = witnessScript
|
||||
|
||||
txOut := &wire.TxOut{}
|
||||
if err := readTxOut(r, txOut); err != nil {
|
||||
return err
|
||||
}
|
||||
sd.Output = txOut
|
||||
|
||||
var hashType [4]byte
|
||||
if _, err := io.ReadFull(r, hashType[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
sd.HashType = txscript.SigHashType(binary.BigEndian.Uint32(hashType[:]))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
)
|
||||
|
||||
func TestSignDescriptorSerialization(t *testing.T) {
|
||||
keys := [][]byte{
|
||||
{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||
0xb4, 0x12, 0xa3,
|
||||
},
|
||||
{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||
0xb4, 0x12, 0xa3,
|
||||
},
|
||||
}
|
||||
|
||||
signDescriptors := []SignDescriptor{
|
||||
{
|
||||
SingleTweak: []byte{
|
||||
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
},
|
||||
WitnessScript: []byte{
|
||||
0x00, 0x14, 0xee, 0x91, 0x41, 0x7e, 0x85, 0x6c, 0xde,
|
||||
0x10, 0xa2, 0x91, 0x1e, 0xdc, 0xbd, 0xbd, 0x69, 0xe2,
|
||||
0xef, 0xb5, 0x71, 0x48,
|
||||
},
|
||||
Output: &wire.TxOut{
|
||||
Value: 5000000000,
|
||||
PkScript: []byte{
|
||||
0x41, // OP_DATA_65
|
||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
||||
0xa6, // 65-byte signature
|
||||
0xac, // OP_CHECKSIG
|
||||
},
|
||||
},
|
||||
HashType: txscript.SigHashAll,
|
||||
},
|
||||
|
||||
// Test serializing a SignDescriptor with a nil-valued PrivateTweak
|
||||
{
|
||||
SingleTweak: nil,
|
||||
WitnessScript: []byte{
|
||||
0x00, 0x14, 0xee, 0x91, 0x41, 0x7e, 0x85, 0x6c, 0xde,
|
||||
0x10, 0xa2, 0x91, 0x1e, 0xdc, 0xbd, 0xbd, 0x69, 0xe2,
|
||||
0xef, 0xb5, 0x71, 0x48,
|
||||
},
|
||||
Output: &wire.TxOut{
|
||||
Value: 5000000000,
|
||||
PkScript: []byte{
|
||||
0x41, // OP_DATA_65
|
||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
||||
0xa6, // 65-byte signature
|
||||
0xac, // OP_CHECKSIG
|
||||
},
|
||||
},
|
||||
HashType: txscript.SigHashAll,
|
||||
},
|
||||
}
|
||||
|
||||
for i := 0; i < len(signDescriptors); i++ {
|
||||
// Parse pubkeys for each sign descriptor.
|
||||
sd := &signDescriptors[i]
|
||||
pubkey, err := btcec.ParsePubKey(keys[i], btcec.S256())
|
||||
if err != nil {
|
||||
t.Fatalf("unable to parse pubkey: %v", err)
|
||||
}
|
||||
sd.KeyDesc = keychain.KeyDescriptor{
|
||||
KeyLocator: keychain.KeyLocator{
|
||||
Family: 50,
|
||||
Index: 99,
|
||||
},
|
||||
PubKey: pubkey,
|
||||
}
|
||||
|
||||
// Test that serialize -> deserialize yields same result as original.
|
||||
var buf bytes.Buffer
|
||||
if err := WriteSignDescriptor(&buf, sd); err != nil {
|
||||
t.Fatalf("unable to serialize sign descriptor[%v]: %v", i, sd)
|
||||
}
|
||||
|
||||
desSd := &SignDescriptor{}
|
||||
if err := ReadSignDescriptor(&buf, desSd); err != nil {
|
||||
t.Fatalf("unable to deserialize sign descriptor[%v]: %v", i, sd)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(sd, desSd) {
|
||||
t.Fatalf("original and deserialized sign descriptors not equal:\n"+
|
||||
"original : %+v\n"+
|
||||
"deserialized : %+v\n",
|
||||
sd, desSd)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// Signer represents an abstract object capable of generating raw signatures as
|
||||
// well as full complete input scripts given a valid SignDescriptor and
|
||||
// transaction. This interface fully abstracts away signing paving the way for
|
||||
// Signer implementations such as hardware wallets, hardware tokens, HSM's, or
|
||||
// simply a regular wallet.
|
||||
type Signer interface {
|
||||
// SignOutputRaw generates a signature for the passed transaction
|
||||
// according to the data within the passed SignDescriptor.
|
||||
//
|
||||
// NOTE: The resulting signature should be void of a sighash byte.
|
||||
SignOutputRaw(tx *wire.MsgTx, signDesc *SignDescriptor) ([]byte, error)
|
||||
|
||||
// ComputeInputScript generates a complete InputIndex for the passed
|
||||
// transaction with the signature as defined within the passed
|
||||
// SignDescriptor. This method should be capable of generating the
|
||||
// proper input script for both regular p2wkh output and p2wkh outputs
|
||||
// nested within a regular p2sh output.
|
||||
//
|
||||
// NOTE: This method will ignore any tweak parameters set within the
|
||||
// passed SignDescriptor as it assumes a set of typical script
|
||||
// templates (p2wkh, np2wkh, etc).
|
||||
ComputeInputScript(tx *wire.MsgTx, signDesc *SignDescriptor) (*Script, error)
|
||||
}
|
||||
|
||||
// Script represents any script inputs required to redeem a previous
|
||||
// output. This struct is used rather than just a witness, or scripSig in order
|
||||
// to accommodate nested p2sh which utilizes both types of input scripts.
|
||||
type Script struct {
|
||||
// Witness is the full witness stack required to unlock this output.
|
||||
Witness wire.TxWitness
|
||||
|
||||
// SigScript will only be populated if this is an input script sweeping
|
||||
// a nested p2sh output.
|
||||
SigScript []byte
|
||||
}
|
||||
+497
@@ -0,0 +1,497 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcd/blockchain"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
const (
|
||||
// CommitWeight is the weight of the base commitment transaction which
|
||||
// includes: one p2wsh input, out p2wkh output, and one p2wsh output.
|
||||
CommitWeight int64 = 724
|
||||
|
||||
// HtlcWeight is the weight of an HTLC output.
|
||||
HtlcWeight int64 = 172
|
||||
)
|
||||
|
||||
const (
|
||||
// witnessScaleFactor determines the level of "discount" witness data
|
||||
// receives compared to "base" data. A scale factor of 4, denotes that
|
||||
// witness data is 1/4 as cheap as regular non-witness data. Value copied
|
||||
// here for convenience.
|
||||
witnessScaleFactor = blockchain.WitnessScaleFactor
|
||||
|
||||
// The weight(weight), which is different from the !size! (see BIP-141),
|
||||
// is calculated as:
|
||||
// Weight = 4 * BaseSize + WitnessSize (weight).
|
||||
// BaseSize - size of the transaction without witness data (bytes).
|
||||
// WitnessSize - witness size (bytes).
|
||||
// Weight - the metric for determining the weight of the transaction.
|
||||
|
||||
// P2WPKHSize 22 bytes
|
||||
// - OP_0: 1 byte
|
||||
// - OP_DATA: 1 byte (PublicKeyHASH160 length)
|
||||
// - PublicKeyHASH160: 20 bytes
|
||||
P2WPKHSize = 1 + 1 + 20
|
||||
|
||||
// P2WSHSize 34 bytes
|
||||
// - OP_0: 1 byte
|
||||
// - OP_DATA: 1 byte (WitnessScriptSHA256 length)
|
||||
// - WitnessScriptSHA256: 32 bytes
|
||||
P2WSHSize = 1 + 1 + 32
|
||||
|
||||
// P2PKHOutputSize 34 bytes
|
||||
// - value: 8 bytes
|
||||
// - var_int: 1 byte (pkscript_length)
|
||||
// - pkscript (p2pkh): 25 bytes
|
||||
P2PKHOutputSize = 8 + 1 + 25
|
||||
|
||||
// P2WKHOutputSize 31 bytes
|
||||
// - value: 8 bytes
|
||||
// - var_int: 1 byte (pkscript_length)
|
||||
// - pkscript (p2wpkh): 22 bytes
|
||||
P2WKHOutputSize = 8 + 1 + P2WPKHSize
|
||||
|
||||
// P2WSHOutputSize 43 bytes
|
||||
// - value: 8 bytes
|
||||
// - var_int: 1 byte (pkscript_length)
|
||||
// - pkscript (p2wsh): 34 bytes
|
||||
P2WSHOutputSize = 8 + 1 + P2WSHSize
|
||||
|
||||
// P2SHOutputSize 32 bytes
|
||||
// - value: 8 bytes
|
||||
// - var_int: 1 byte (pkscript_length)
|
||||
// - pkscript (p2sh): 23 bytes
|
||||
P2SHOutputSize = 8 + 1 + 23
|
||||
|
||||
// P2PKHScriptSigSize 108 bytes
|
||||
// - OP_DATA: 1 byte (signature length)
|
||||
// - signature
|
||||
// - OP_DATA: 1 byte (pubkey length)
|
||||
// - pubkey
|
||||
P2PKHScriptSigSize = 1 + 73 + 1 + 33
|
||||
|
||||
// P2WKHWitnessSize 109 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - signature_length: 1 byte
|
||||
// - signature
|
||||
// - pubkey_length: 1 byte
|
||||
// - pubkey
|
||||
P2WKHWitnessSize = 1 + 1 + 73 + 1 + 33
|
||||
|
||||
// MultiSigSize 71 bytes
|
||||
// - OP_2: 1 byte
|
||||
// - OP_DATA: 1 byte (pubKeyAlice length)
|
||||
// - pubKeyAlice: 33 bytes
|
||||
// - OP_DATA: 1 byte (pubKeyBob length)
|
||||
// - pubKeyBob: 33 bytes
|
||||
// - OP_2: 1 byte
|
||||
// - OP_CHECKMULTISIG: 1 byte
|
||||
MultiSigSize = 1 + 1 + 33 + 1 + 33 + 1 + 1
|
||||
|
||||
// WitnessSize 222 bytes
|
||||
// - NumberOfWitnessElements: 1 byte
|
||||
// - NilLength: 1 byte
|
||||
// - sigAliceLength: 1 byte
|
||||
// - sigAlice: 73 bytes
|
||||
// - sigBobLength: 1 byte
|
||||
// - sigBob: 73 bytes
|
||||
// - WitnessScriptLength: 1 byte
|
||||
// - WitnessScript (MultiSig)
|
||||
WitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + MultiSigSize
|
||||
|
||||
// InputSize 41 bytes
|
||||
// - PreviousOutPoint:
|
||||
// - Hash: 32 bytes
|
||||
// - Index: 4 bytes
|
||||
// - OP_DATA: 1 byte (ScriptSigLength)
|
||||
// - ScriptSig: 0 bytes
|
||||
// - Witness <---- we use "Witness" instead of "ScriptSig" for
|
||||
// transaction validation, but "Witness" is stored
|
||||
// separately and weight for it size is smaller. So
|
||||
// we separate the calculation of ordinary data
|
||||
// from witness data.
|
||||
// - Sequence: 4 bytes
|
||||
InputSize = 32 + 4 + 1 + 4
|
||||
|
||||
// FundingInputSize represents the size of an input to a funding
|
||||
// transaction, and is equivalent to the size of a standard segwit input
|
||||
// as calculated above.
|
||||
FundingInputSize = InputSize
|
||||
|
||||
// CommitmentDelayOutput 43 bytes
|
||||
// - Value: 8 bytes
|
||||
// - VarInt: 1 byte (PkScript length)
|
||||
// - PkScript (P2WSH)
|
||||
CommitmentDelayOutput = 8 + 1 + P2WSHSize
|
||||
|
||||
// CommitmentKeyHashOutput 31 bytes
|
||||
// - Value: 8 bytes
|
||||
// - VarInt: 1 byte (PkScript length)
|
||||
// - PkScript (P2WPKH)
|
||||
CommitmentKeyHashOutput = 8 + 1 + P2WPKHSize
|
||||
|
||||
// HTLCSize 43 bytes
|
||||
// - Value: 8 bytes
|
||||
// - VarInt: 1 byte (PkScript length)
|
||||
// - PkScript (PW2SH)
|
||||
HTLCSize = 8 + 1 + P2WSHSize
|
||||
|
||||
// WitnessHeaderSize 2 bytes
|
||||
// - Flag: 1 byte
|
||||
// - Marker: 1 byte
|
||||
WitnessHeaderSize = 1 + 1
|
||||
|
||||
// BaseTxSize 8 bytes
|
||||
// - Version: 4 bytes
|
||||
// - LockTime: 4 bytes
|
||||
BaseTxSize = 4 + 4
|
||||
|
||||
// BaseCommitmentTxSize 125 + 43 * num-htlc-outputs bytes
|
||||
// - Version: 4 bytes
|
||||
// - WitnessHeader <---- part of the witness data
|
||||
// - CountTxIn: 1 byte
|
||||
// - TxIn: 41 bytes
|
||||
// FundingInput
|
||||
// - CountTxOut: 1 byte
|
||||
// - TxOut: 74 + 43 * num-htlc-outputs bytes
|
||||
// OutputPayingToThem,
|
||||
// OutputPayingToUs,
|
||||
// ....HTLCOutputs...
|
||||
// - LockTime: 4 bytes
|
||||
BaseCommitmentTxSize = 4 + 1 + FundingInputSize + 1 +
|
||||
CommitmentDelayOutput + CommitmentKeyHashOutput + 4
|
||||
|
||||
// BaseCommitmentTxWeight 500 weight
|
||||
BaseCommitmentTxWeight = witnessScaleFactor * BaseCommitmentTxSize
|
||||
|
||||
// WitnessCommitmentTxWeight 224 weight
|
||||
WitnessCommitmentTxWeight = WitnessHeaderSize + WitnessSize
|
||||
|
||||
// HTLCWeight 172 weight
|
||||
HTLCWeight = witnessScaleFactor * HTLCSize
|
||||
|
||||
// HtlcTimeoutWeight is the weight of the HTLC timeout transaction
|
||||
// which will transition an outgoing HTLC to the delay-and-claim state.
|
||||
HtlcTimeoutWeight = 663
|
||||
|
||||
// HtlcSuccessWeight is the weight of the HTLC success transaction
|
||||
// which will transition an incoming HTLC to the delay-and-claim state.
|
||||
HtlcSuccessWeight = 703
|
||||
|
||||
// MaxHTLCNumber is the maximum number HTLCs which can be included in a
|
||||
// commitment transaction. This limit was chosen such that, in the case
|
||||
// of a contract breach, the punishment transaction is able to sweep
|
||||
// all the HTLC's yet still remain below the widely used standard
|
||||
// weight limits.
|
||||
MaxHTLCNumber = 966
|
||||
|
||||
// ToLocalScriptSize 79 bytes
|
||||
// - OP_IF: 1 byte
|
||||
// - OP_DATA: 1 byte
|
||||
// - revoke_key: 33 bytes
|
||||
// - OP_ELSE: 1 byte
|
||||
// - OP_DATA: 1 byte
|
||||
// - csv_delay: 4 bytes
|
||||
// - OP_CHECKSEQUENCEVERIFY: 1 byte
|
||||
// - OP_DROP: 1 byte
|
||||
// - OP_DATA: 1 byte
|
||||
// - delay_key: 33 bytes
|
||||
// - OP_ENDIF: 1 byte
|
||||
// - OP_CHECKSIG: 1 byte
|
||||
ToLocalScriptSize = 1 + 1 + 33 + 1 + 1 + 4 + 1 + 1 + 1 + 33 + 1 + 1
|
||||
|
||||
// ToLocalTimeoutWitnessSize 156 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - local_delay_sig_length: 1 byte
|
||||
// - local_delay_sig: 73 bytes
|
||||
// - zero_length: 1 byte
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (to_local_script)
|
||||
ToLocalTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize
|
||||
|
||||
// ToLocalPenaltyWitnessSize 156 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - revocation_sig_length: 1 byte
|
||||
// - revocation_sig: 73 bytes
|
||||
// - OP_TRUE: 1 byte
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (to_local_script)
|
||||
ToLocalPenaltyWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize
|
||||
|
||||
// AcceptedHtlcScriptSize 139 bytes
|
||||
// - OP_DUP: 1 byte
|
||||
// - OP_HASH160: 1 byte
|
||||
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
|
||||
// - RIPEMD160(SHA256(revocationkey)): 20 bytes
|
||||
// - OP_EQUAL: 1 byte
|
||||
// - OP_IF: 1 byte
|
||||
// - OP_CHECKSIG: 1 byte
|
||||
// - OP_ELSE: 1 byte
|
||||
// - OP_DATA: 1 byte (remotekey length)
|
||||
// - remotekey: 33 bytes
|
||||
// - OP_SWAP: 1 byte
|
||||
// - OP_SIZE: 1 byte
|
||||
// - 32: 1 byte
|
||||
// - OP_EQUAL: 1 byte
|
||||
// - OP_IF: 1 byte
|
||||
// - OP_HASH160: 1 byte
|
||||
// - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
|
||||
// - RIPEMD160(payment_hash): 20 bytes
|
||||
// - OP_EQUALVERIFY: 1 byte
|
||||
// - 2: 1 byte
|
||||
// - OP_SWAP: 1 byte
|
||||
// - OP_DATA: 1 byte (localkey length)
|
||||
// - localkey: 33 bytes
|
||||
// - 2: 1 byte
|
||||
// - OP_CHECKMULTISIG: 1 byte
|
||||
// - OP_ELSE: 1 byte
|
||||
// - OP_DROP: 1 byte
|
||||
// - OP_DATA: 1 byte (cltv_expiry length)
|
||||
// - cltv_expiry: 4 bytes
|
||||
// - OP_CHECKLOCKTIMEVERIFY: 1 byte
|
||||
// - OP_DROP: 1 byte
|
||||
// - OP_CHECKSIG: 1 byte
|
||||
// - OP_ENDIF: 1 byte
|
||||
// - OP_ENDIF: 1 byte
|
||||
AcceptedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 7*1 + 20 + 4*1 +
|
||||
33 + 5*1 + 4 + 5*1
|
||||
|
||||
// AcceptedHtlcTimeoutWitnessSize 216
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - sender_sig_length: 1 byte
|
||||
// - sender_sig: 73 bytes
|
||||
// - nil_length: 1 byte
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script: (accepted_htlc_script)
|
||||
AcceptedHtlcTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + AcceptedHtlcScriptSize
|
||||
|
||||
// AcceptedHtlcSuccessWitnessSize 322 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - nil_length: 1 byte
|
||||
// - sig_alice_length: 1 byte
|
||||
// - sig_alice: 73 bytes
|
||||
// - sig_bob_length: 1 byte
|
||||
// - sig_bob: 73 bytes
|
||||
// - preimage_length: 1 byte
|
||||
// - preimage: 32 bytes
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (accepted_htlc_script)
|
||||
AcceptedHtlcSuccessWitnessSize = 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 + AcceptedHtlcScriptSize
|
||||
|
||||
// AcceptedHtlcPenaltyWitnessSize 249 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - revocation_sig_length: 1 byte
|
||||
// - revocation_sig: 73 bytes
|
||||
// - revocation_key_length: 1 byte
|
||||
// - revocation_key: 33 bytes
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (accepted_htlc_script)
|
||||
AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + AcceptedHtlcScriptSize
|
||||
|
||||
// OfferedHtlcScriptSize 133 bytes
|
||||
// - OP_DUP: 1 byte
|
||||
// - OP_HASH160: 1 byte
|
||||
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
|
||||
// - RIPEMD160(SHA256(revocationkey)): 20 bytes
|
||||
// - OP_EQUAL: 1 byte
|
||||
// - OP_IF: 1 byte
|
||||
// - OP_CHECKSIG: 1 byte
|
||||
// - OP_ELSE: 1 byte
|
||||
// - OP_DATA: 1 byte (remotekey length)
|
||||
// - remotekey: 33 bytes
|
||||
// - OP_SWAP: 1 byte
|
||||
// - OP_SIZE: 1 byte
|
||||
// - OP_DATA: 1 byte (32 length)
|
||||
// - 32: 1 byte
|
||||
// - OP_EQUAL: 1 byte
|
||||
// - OP_NOTIF: 1 byte
|
||||
// - OP_DROP: 1 byte
|
||||
// - 2: 1 byte
|
||||
// - OP_SWAP: 1 byte
|
||||
// - OP_DATA: 1 byte (localkey length)
|
||||
// - localkey: 33 bytes
|
||||
// - 2: 1 byte
|
||||
// - OP_CHECKMULTISIG: 1 byte
|
||||
// - OP_ELSE: 1 byte
|
||||
// - OP_HASH160: 1 byte
|
||||
// - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
|
||||
// - RIPEMD160(payment_hash): 20 bytes
|
||||
// - OP_EQUALVERIFY: 1 byte
|
||||
// - OP_CHECKSIG: 1 byte
|
||||
// - OP_ENDIF: 1 byte
|
||||
// - OP_ENDIF: 1 byte
|
||||
OfferedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 10*1 + 33 + 5*1 + 20 + 4*1
|
||||
|
||||
// OfferedHtlcTimeoutWitnessSize 285 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - nil_length: 1 byte
|
||||
// - sig_alice_length: 1 byte
|
||||
// - sig_alice: 73 bytes
|
||||
// - sig_bob_length: 1 byte
|
||||
// - sig_bob: 73 bytes
|
||||
// - nil_length: 1 byte
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (offered_htlc_script)
|
||||
OfferedHtlcTimeoutWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 + OfferedHtlcScriptSize
|
||||
|
||||
// OfferedHtlcSuccessWitnessSize 317 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - nil_length: 1 byte
|
||||
// - receiver_sig_length: 1 byte
|
||||
// - receiver_sig: 73 bytes
|
||||
// - sender_sig_length: 1 byte
|
||||
// - sender_sig: 73 bytes
|
||||
// - payment_preimage_length: 1 byte
|
||||
// - payment_preimage: 32 bytes
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (offered_htlc_script)
|
||||
OfferedHtlcSuccessWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 + OfferedHtlcScriptSize
|
||||
|
||||
// OfferedHtlcPenaltyWitnessSize 243 bytes
|
||||
// - number_of_witness_elements: 1 byte
|
||||
// - revocation_sig_length: 1 byte
|
||||
// - revocation_sig: 73 bytes
|
||||
// - revocation_key_length: 1 byte
|
||||
// - revocation_key: 33 bytes
|
||||
// - witness_script_length: 1 byte
|
||||
// - witness_script (offered_htlc_script)
|
||||
OfferedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize
|
||||
)
|
||||
|
||||
// EstimateCommitTxWeight estimate commitment transaction weight depending on
|
||||
// the precalculated weight of base transaction, witness data, which is needed
|
||||
// for paying for funding tx, and htlc weight multiplied by their count.
|
||||
func EstimateCommitTxWeight(count int, prediction bool) int64 {
|
||||
// Make prediction about the size of commitment transaction with
|
||||
// additional HTLC.
|
||||
if prediction {
|
||||
count++
|
||||
}
|
||||
|
||||
htlcWeight := int64(count * HTLCWeight)
|
||||
baseWeight := int64(BaseCommitmentTxWeight)
|
||||
witnessWeight := int64(WitnessCommitmentTxWeight)
|
||||
|
||||
return htlcWeight + baseWeight + witnessWeight
|
||||
}
|
||||
|
||||
// TxWeightEstimator is able to calculate weight estimates for transactions
|
||||
// based on the input and output types. For purposes of estimation, all
|
||||
// signatures are assumed to be of the maximum possible size, 73 bytes. Each
|
||||
// method of the estimator returns an instance with the estimate applied. This
|
||||
// allows callers to chain each of the methods
|
||||
type TxWeightEstimator struct {
|
||||
hasWitness bool
|
||||
inputCount uint32
|
||||
outputCount uint32
|
||||
inputSize int
|
||||
inputWitnessSize int
|
||||
outputSize int
|
||||
}
|
||||
|
||||
// AddP2PKHInput updates the weight estimate to account for an additional input
|
||||
// spending a P2PKH output.
|
||||
func (twe *TxWeightEstimator) AddP2PKHInput() *TxWeightEstimator {
|
||||
twe.inputSize += InputSize + P2PKHScriptSigSize
|
||||
twe.inputWitnessSize++
|
||||
twe.inputCount++
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddP2WKHInput updates the weight estimate to account for an additional input
|
||||
// spending a native P2PWKH output.
|
||||
func (twe *TxWeightEstimator) AddP2WKHInput() *TxWeightEstimator {
|
||||
twe.AddWitnessInput(P2WKHWitnessSize)
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddWitnessInput updates the weight estimate to account for an additional
|
||||
// input spending a native pay-to-witness output. This accepts the total size
|
||||
// of the witness as a parameter.
|
||||
func (twe *TxWeightEstimator) AddWitnessInput(witnessSize int) *TxWeightEstimator {
|
||||
twe.inputSize += InputSize
|
||||
twe.inputWitnessSize += witnessSize
|
||||
twe.inputCount++
|
||||
twe.hasWitness = true
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddNestedP2WKHInput updates the weight estimate to account for an additional
|
||||
// input spending a P2SH output with a nested P2WKH redeem script.
|
||||
func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
|
||||
twe.inputSize += InputSize + P2WPKHSize
|
||||
twe.inputWitnessSize += P2WKHWitnessSize
|
||||
twe.inputSize++
|
||||
twe.hasWitness = true
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddNestedP2WSHInput updates the weight estimate to account for an additional
|
||||
// input spending a P2SH output with a nested P2WSH redeem script.
|
||||
func (twe *TxWeightEstimator) AddNestedP2WSHInput(witnessSize int) *TxWeightEstimator {
|
||||
twe.inputSize += InputSize + P2WSHSize
|
||||
twe.inputWitnessSize += witnessSize
|
||||
twe.inputSize++
|
||||
twe.hasWitness = true
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddP2PKHOutput updates the weight estimate to account for an additional P2PKH
|
||||
// output.
|
||||
func (twe *TxWeightEstimator) AddP2PKHOutput() *TxWeightEstimator {
|
||||
twe.outputSize += P2PKHOutputSize
|
||||
twe.outputCount++
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddP2WKHOutput updates the weight estimate to account for an additional
|
||||
// native P2WKH output.
|
||||
func (twe *TxWeightEstimator) AddP2WKHOutput() *TxWeightEstimator {
|
||||
twe.outputSize += P2WKHOutputSize
|
||||
twe.outputCount++
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddP2WSHOutput updates the weight estimate to account for an additional
|
||||
// native P2WSH output.
|
||||
func (twe *TxWeightEstimator) AddP2WSHOutput() *TxWeightEstimator {
|
||||
twe.outputSize += P2WSHOutputSize
|
||||
twe.outputCount++
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// AddP2SHOutput updates the weight estimate to account for an additional P2SH
|
||||
// output.
|
||||
func (twe *TxWeightEstimator) AddP2SHOutput() *TxWeightEstimator {
|
||||
twe.outputSize += P2SHOutputSize
|
||||
twe.outputCount++
|
||||
|
||||
return twe
|
||||
}
|
||||
|
||||
// Weight gets the estimated weight of the transaction.
|
||||
func (twe *TxWeightEstimator) Weight() int {
|
||||
txSizeStripped := BaseTxSize +
|
||||
wire.VarIntSerializeSize(uint64(twe.inputCount)) + twe.inputSize +
|
||||
wire.VarIntSerializeSize(uint64(twe.outputCount)) + twe.outputSize
|
||||
weight := txSizeStripped * witnessScaleFactor
|
||||
if twe.hasWitness {
|
||||
weight += WitnessHeaderSize + twe.inputWitnessSize
|
||||
}
|
||||
return weight
|
||||
}
|
||||
|
||||
// VSize gets the estimated virtual size of the transactions, in vbytes.
|
||||
func (twe *TxWeightEstimator) VSize() int {
|
||||
// A tx's vsize is 1/4 of the weight, rounded up.
|
||||
return (twe.Weight() + witnessScaleFactor - 1) / witnessScaleFactor
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
// For simplicity a single priv key controls all of our test outputs.
|
||||
testWalletPrivKey = []byte{
|
||||
0x2b, 0xd8, 0x06, 0xc9, 0x7f, 0x0e, 0x00, 0xaf,
|
||||
0x1a, 0x1f, 0xc3, 0x32, 0x8f, 0xa7, 0x63, 0xa9,
|
||||
0x26, 0x97, 0x23, 0xc8, 0xdb, 0x8f, 0xac, 0x4f,
|
||||
0x93, 0xaf, 0x71, 0xdb, 0x18, 0x6d, 0x6e, 0x90,
|
||||
}
|
||||
|
||||
// We're alice :)
|
||||
bobsPrivKey = []byte{
|
||||
0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
|
||||
0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
|
||||
0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
|
||||
0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9,
|
||||
}
|
||||
|
||||
// Use a hard-coded HD seed.
|
||||
testHdSeed = chainhash.Hash{
|
||||
0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
|
||||
0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
|
||||
0x4f, 0x2f, 0x6f, 0x25, 0x88, 0xa3, 0xef, 0xb9,
|
||||
0x6a, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
|
||||
}
|
||||
)
|
||||
|
||||
// MockSigner is a simple implementation of the Signer interface. Each one has
|
||||
// a set of private keys in a slice and can sign messages using the appropriate
|
||||
// one.
|
||||
type MockSigner struct {
|
||||
Privkeys []*btcec.PrivateKey
|
||||
NetParams *chaincfg.Params
|
||||
}
|
||||
|
||||
// SignOutputRaw generates a signature for the passed transaction according to
|
||||
// the data within the passed SignDescriptor.
|
||||
func (m *MockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *SignDescriptor) ([]byte, error) {
|
||||
pubkey := signDesc.KeyDesc.PubKey
|
||||
switch {
|
||||
case signDesc.SingleTweak != nil:
|
||||
pubkey = TweakPubKeyWithTweak(pubkey, signDesc.SingleTweak)
|
||||
case signDesc.DoubleTweak != nil:
|
||||
pubkey = DeriveRevocationPubkey(pubkey, signDesc.DoubleTweak.PubKey())
|
||||
}
|
||||
|
||||
hash160 := btcutil.Hash160(pubkey.SerializeCompressed())
|
||||
privKey := m.findKey(hash160, signDesc.SingleTweak, signDesc.DoubleTweak)
|
||||
if privKey == nil {
|
||||
return nil, fmt.Errorf("Mock signer does not have key")
|
||||
}
|
||||
|
||||
sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
|
||||
signDesc.InputIndex, signDesc.Output.Value, signDesc.WitnessScript,
|
||||
txscript.SigHashAll, privKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sig[:len(sig)-1], nil
|
||||
}
|
||||
|
||||
// ComputeInputScript generates a complete InputIndex for the passed transaction
|
||||
// with the signature as defined within the passed SignDescriptor. This method
|
||||
// should be capable of generating the proper input script for both regular
|
||||
// p2wkh output and p2wkh outputs nested within a regular p2sh output.
|
||||
func (m *MockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *SignDescriptor) (*Script, error) {
|
||||
scriptType, addresses, _, err := txscript.ExtractPkScriptAddrs(
|
||||
signDesc.Output.PkScript, m.NetParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch scriptType {
|
||||
case txscript.PubKeyHashTy:
|
||||
privKey := m.findKey(addresses[0].ScriptAddress(), signDesc.SingleTweak,
|
||||
signDesc.DoubleTweak)
|
||||
if privKey == nil {
|
||||
return nil, fmt.Errorf("Mock signer does not have key for "+
|
||||
"address %v", addresses[0])
|
||||
}
|
||||
|
||||
sigScript, err := txscript.SignatureScript(
|
||||
tx, signDesc.InputIndex, signDesc.Output.PkScript,
|
||||
txscript.SigHashAll, privKey, true,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{SigScript: sigScript}, nil
|
||||
|
||||
case txscript.WitnessV0PubKeyHashTy:
|
||||
privKey := m.findKey(addresses[0].ScriptAddress(), signDesc.SingleTweak,
|
||||
signDesc.DoubleTweak)
|
||||
if privKey == nil {
|
||||
return nil, fmt.Errorf("Mock signer does not have key for "+
|
||||
"address %v", addresses[0])
|
||||
}
|
||||
|
||||
witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes,
|
||||
signDesc.InputIndex, signDesc.Output.Value,
|
||||
signDesc.Output.PkScript, txscript.SigHashAll, privKey, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{Witness: witnessScript}, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("Unexpected script type: %v", scriptType)
|
||||
}
|
||||
}
|
||||
|
||||
// findKey searches through all stored private keys and returns one
|
||||
// corresponding to the hashed pubkey if it can be found. The public key may
|
||||
// either correspond directly to the private key or to the private key with a
|
||||
// tweak applied.
|
||||
func (m *MockSigner) findKey(needleHash160 []byte, singleTweak []byte,
|
||||
doubleTweak *btcec.PrivateKey) *btcec.PrivateKey {
|
||||
|
||||
for _, privkey := range m.Privkeys {
|
||||
// First check whether public key is directly derived from private key.
|
||||
hash160 := btcutil.Hash160(privkey.PubKey().SerializeCompressed())
|
||||
if bytes.Equal(hash160, needleHash160) {
|
||||
return privkey
|
||||
}
|
||||
|
||||
// Otherwise check if public key is derived from tweaked private key.
|
||||
switch {
|
||||
case singleTweak != nil:
|
||||
privkey = TweakPrivKey(privkey, singleTweak)
|
||||
case doubleTweak != nil:
|
||||
privkey = DeriveRevocationPrivKey(privkey, doubleTweak)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
hash160 = btcutil.Hash160(privkey.PubKey().SerializeCompressed())
|
||||
if bytes.Equal(hash160, needleHash160) {
|
||||
return privkey
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// pubkeyFromHex parses a Bitcoin public key from a hex encoded string.
|
||||
func pubkeyFromHex(keyHex string) (*btcec.PublicKey, error) {
|
||||
bytes, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return btcec.ParsePubKey(bytes, btcec.S256())
|
||||
}
|
||||
|
||||
// privkeyFromHex parses a Bitcoin private key from a hex encoded string.
|
||||
func privkeyFromHex(keyHex string) (*btcec.PrivateKey, error) {
|
||||
bytes, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key, _ := btcec.PrivKeyFromBytes(btcec.S256(), bytes)
|
||||
return key, nil
|
||||
|
||||
}
|
||||
|
||||
// pubkeyToHex serializes a Bitcoin public key to a hex encoded string.
|
||||
func pubkeyToHex(key *btcec.PublicKey) string {
|
||||
return hex.EncodeToString(key.SerializeCompressed())
|
||||
}
|
||||
|
||||
// privkeyFromHex serializes a Bitcoin private key to a hex encoded string.
|
||||
func privkeyToHex(key *btcec.PrivateKey) string {
|
||||
return hex.EncodeToString(key.Serialize())
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// writeTxOut serializes a wire.TxOut struct into the passed io.Writer stream.
|
||||
func writeTxOut(w io.Writer, txo *wire.TxOut) error {
|
||||
var scratch [8]byte
|
||||
|
||||
binary.BigEndian.PutUint64(scratch[:], uint64(txo.Value))
|
||||
if _, err := w.Write(scratch[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := wire.WriteVarBytes(w, 0, txo.PkScript); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// readTxOut deserializes a wire.TxOut struct from the passed io.Reader stream.
|
||||
func readTxOut(r io.Reader, txo *wire.TxOut) error {
|
||||
var scratch [8]byte
|
||||
|
||||
if _, err := io.ReadFull(r, scratch[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
value := int64(binary.BigEndian.Uint64(scratch[:]))
|
||||
|
||||
pkScript, err := wire.ReadVarBytes(r, 0, 80, "pkScript")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*txo = wire.TxOut{
|
||||
Value: value,
|
||||
PkScript: pkScript,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
func TestTxOutSerialization(t *testing.T) {
|
||||
txo := wire.TxOut{
|
||||
Value: 1e7,
|
||||
PkScript: []byte{
|
||||
0x41, // OP_DATA_65
|
||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
||||
0xa6, // 65-byte signature
|
||||
0xac, // OP_CHECKSIG
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := writeTxOut(&buf, &txo); err != nil {
|
||||
t.Fatalf("unable to serialize txout: %v", err)
|
||||
}
|
||||
|
||||
var deserializedTxo wire.TxOut
|
||||
if err := readTxOut(&buf, &deserializedTxo); err != nil {
|
||||
t.Fatalf("unable to deserialize txout: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(txo, deserializedTxo) {
|
||||
t.Fatalf("original and deserialized txouts are different:\n"+
|
||||
"original : %+v\n"+
|
||||
"deserialized : %+v\n",
|
||||
txo, deserializedTxo)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// WitnessType determines how an output's witness will be generated. The
|
||||
// default commitmentTimeLock type will generate a witness that will allow
|
||||
// spending of a time-locked transaction enforced by CheckSequenceVerify.
|
||||
type WitnessType uint16
|
||||
|
||||
const (
|
||||
// CommitmentTimeLock is a witness that allows us to spend the output of
|
||||
// a commitment transaction after a relative lock-time lockout.
|
||||
CommitmentTimeLock WitnessType = 0
|
||||
|
||||
// CommitmentNoDelay is a witness that allows us to spend a settled
|
||||
// no-delay output immediately on a counterparty's commitment
|
||||
// transaction.
|
||||
CommitmentNoDelay WitnessType = 1
|
||||
|
||||
// CommitmentRevoke is a witness that allows us to sweep the settled
|
||||
// output of a malicious counterparty's who broadcasts a revoked
|
||||
// commitment transaction.
|
||||
CommitmentRevoke WitnessType = 2
|
||||
|
||||
// HtlcOfferedRevoke is a witness that allows us to sweep an HTLC which
|
||||
// we offered to the remote party in the case that they broadcast a
|
||||
// revoked commitment state.
|
||||
HtlcOfferedRevoke WitnessType = 3
|
||||
|
||||
// HtlcAcceptedRevoke is a witness that allows us to sweep an HTLC
|
||||
// output sent to us in the case that the remote party broadcasts a
|
||||
// revoked commitment state.
|
||||
HtlcAcceptedRevoke WitnessType = 4
|
||||
|
||||
// HtlcOfferedTimeoutSecondLevel is a witness that allows us to sweep
|
||||
// an HTLC output that we extended to a party, but was never fulfilled.
|
||||
// This HTLC output isn't directly on the commitment transaction, but
|
||||
// is the result of a confirmed second-level HTLC transaction. As a
|
||||
// result, we can only spend this after a CSV delay.
|
||||
HtlcOfferedTimeoutSecondLevel WitnessType = 5
|
||||
|
||||
// HtlcAcceptedSuccessSecondLevel is a witness that allows us to sweep
|
||||
// an HTLC output that was offered to us, and for which we have a
|
||||
// payment preimage. This HTLC output isn't directly on our commitment
|
||||
// transaction, but is the result of confirmed second-level HTLC
|
||||
// transaction. As a result, we can only spend this after a CSV delay.
|
||||
HtlcAcceptedSuccessSecondLevel WitnessType = 6
|
||||
|
||||
// HtlcOfferedRemoteTimeout is a witness that allows us to sweep an
|
||||
// HTLC that we offered to the remote party which lies in the
|
||||
// commitment transaction of the remote party. We can spend this output
|
||||
// after the absolute CLTV timeout of the HTLC as passed.
|
||||
HtlcOfferedRemoteTimeout WitnessType = 7
|
||||
|
||||
// HtlcAcceptedRemoteSuccess is a witness that allows us to sweep an
|
||||
// HTLC that was offered to us by the remote party. We use this witness
|
||||
// in the case that the remote party goes to chain, and we know the
|
||||
// pre-image to the HTLC. We can sweep this without any additional
|
||||
// timeout.
|
||||
HtlcAcceptedRemoteSuccess WitnessType = 8
|
||||
|
||||
// HtlcSecondLevelRevoke is a witness that allows us to sweep an HTLC
|
||||
// from the remote party's commitment transaction in the case that the
|
||||
// broadcast a revoked commitment, but then also immediately attempt to
|
||||
// go to the second level to claim the HTLC.
|
||||
HtlcSecondLevelRevoke WitnessType = 9
|
||||
|
||||
// WitnessKeyHash is a witness type that allows us to spend a regular
|
||||
// p2wkh output that's sent to an output which is under complete
|
||||
// control of the backing wallet.
|
||||
WitnessKeyHash WitnessType = 10
|
||||
|
||||
// NestedWitnessKeyHash is a witness type that allows us to sweep an
|
||||
// output that sends to a nested P2SH script that pays to a key solely
|
||||
// under our control. The witness generated needs to include the
|
||||
NestedWitnessKeyHash WitnessType = 11
|
||||
)
|
||||
|
||||
// Stirng returns a human readable version of the target WitnessType.
|
||||
func (wt WitnessType) String() string {
|
||||
switch wt {
|
||||
case CommitmentTimeLock:
|
||||
return "CommitmentTimeLock"
|
||||
|
||||
case CommitmentNoDelay:
|
||||
return "CommitmentNoDelay"
|
||||
|
||||
case CommitmentRevoke:
|
||||
return "CommitmentRevoke"
|
||||
|
||||
case HtlcOfferedRevoke:
|
||||
return "HtlcOfferedRevoke"
|
||||
|
||||
case HtlcAcceptedRevoke:
|
||||
return "HtlcAcceptedRevoke"
|
||||
|
||||
case HtlcOfferedTimeoutSecondLevel:
|
||||
return "HtlcOfferedTimeoutSecondLevel"
|
||||
|
||||
case HtlcAcceptedSuccessSecondLevel:
|
||||
return "HtlcAcceptedSuccessSecondLevel"
|
||||
|
||||
case HtlcOfferedRemoteTimeout:
|
||||
return "HtlcOfferedRemoteTimeout"
|
||||
|
||||
case HtlcAcceptedRemoteSuccess:
|
||||
return "HtlcAcceptedRemoteSuccess"
|
||||
|
||||
case HtlcSecondLevelRevoke:
|
||||
return "HtlcSecondLevelRevoke"
|
||||
|
||||
default:
|
||||
return fmt.Sprintf("Unknown WitnessType: %v", uint32(wt))
|
||||
}
|
||||
}
|
||||
|
||||
// WitnessGenerator represents a function which is able to generate the final
|
||||
// witness for a particular public key script. Additionally, if required, this
|
||||
// function will also return the sigScript for spending nested P2SH witness
|
||||
// outputs. This function acts as an abstraction layer, hiding the details of
|
||||
// the underlying script.
|
||||
type WitnessGenerator func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
|
||||
inputIndex int) (*Script, error)
|
||||
|
||||
// GenWitnessFunc will return a WitnessGenerator function that an output uses
|
||||
// to generate the witness and optionally the sigScript for a sweep
|
||||
// transaction. The sigScript will be generated if the witness type warrants
|
||||
// one for spending, such as the NestedWitnessKeyHash witness type.
|
||||
func (wt WitnessType) GenWitnessFunc(signer Signer,
|
||||
descriptor *SignDescriptor) WitnessGenerator {
|
||||
|
||||
return func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
|
||||
inputIndex int) (*Script, error) {
|
||||
|
||||
desc := descriptor
|
||||
desc.SigHashes = hc
|
||||
desc.InputIndex = inputIndex
|
||||
|
||||
switch wt {
|
||||
case CommitmentTimeLock:
|
||||
witness, err := CommitSpendTimeout(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case CommitmentNoDelay:
|
||||
witness, err := CommitSpendNoDelay(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case CommitmentRevoke:
|
||||
witness, err := CommitSpendRevoke(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case HtlcOfferedRevoke:
|
||||
witness, err := ReceiverHtlcSpendRevoke(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case HtlcAcceptedRevoke:
|
||||
witness, err := SenderHtlcSpendRevoke(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case HtlcOfferedTimeoutSecondLevel:
|
||||
witness, err := HtlcSecondLevelSpend(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case HtlcAcceptedSuccessSecondLevel:
|
||||
witness, err := HtlcSecondLevelSpend(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case HtlcOfferedRemoteTimeout:
|
||||
// We pass in a value of -1 for the timeout, as we
|
||||
// expect the caller to have already set the lock time
|
||||
// value.
|
||||
witness, err := ReceiverHtlcSpendTimeout(signer, desc, tx, -1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case HtlcSecondLevelRevoke:
|
||||
witness, err := HtlcSpendRevoke(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case WitnessKeyHash:
|
||||
fallthrough
|
||||
case NestedWitnessKeyHash:
|
||||
return signer.ComputeInputScript(tx, desc)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown witness type: %v", wt)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user