Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lnwallet/script_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,34 @@ func htlcSpendSuccess(signer Signer, signDesc *SignDescriptor,
return witnessStack, nil
}

// HtlcSpendSuccess 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.
// NOTE: The caller MUST set the txn version, sequence number, and sign
// descriptor's sig hash cache before invocation.
func HtlcSpendSuccess(signer Signer, signDesc *SignDescriptor,
sweepTx *wire.MsgTx) (wire.TxWitness, error) {

// With the proper sequence an 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
}

// htlcTimeoutRevoke 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.
Expand Down
49 changes: 41 additions & 8 deletions lnwallet/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,29 @@ const (
// weight limits.
MaxHTLCNumber = 966

// ToLocalPenaltyScriptSize 83 bytes
// ToLocalScriptSize 83 bytes
// - OP_IF: 1 byte
// - OP_DATA: 1 byte (revocationkey length)
// - revocationkey: 33 bytes
// - OP_CHECKSIG: 1 byte
// - OP_ELSE: 1 byte
// - OP_DATA: 1 byte (localkey length)
// - localkey: 33 bytes
// - local_delay_key: 33 bytes
// - OP_CHECKSIG_VERIFY: 1 byte
// - OP_DATA: 1 byte (delay length)
// - delay: 8 bytes
// -OP_CHECKSEQUENCEVERIFY: 1 byte
// - OP_ENDIF: 1 byte
ToLocalPenaltyScriptSize = 1 + 1 + 33 + 1 + 1 + 1 + 33 + 1 + 1 + 8 + 1 + 1
ToLocalScriptSize = 1 + 1 + 33 + 1 + 1 + 1 + 33 + 1 + 1 + 8 + 1 + 1

// ToLocalTimeoutWitnessSize x 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 160 bytes
// - number_of_witness_elements: 1 byte
Expand All @@ -208,9 +217,9 @@ const (
// - one_length: 1 byte
// - witness_script_length: 1 byte
// - witness_script (to_local_script)
ToLocalPenaltyWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalPenaltyScriptSize
ToLocalPenaltyWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize

// AcceptedHtlcPenaltyScriptSize 139 bytes
// AcceptedHtlcScriptSize 139 bytes
// - OP_DUP: 1 byte
// - OP_HASH160: 1 byte
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
Expand Down Expand Up @@ -245,9 +254,22 @@ const (
// - OP_CHECKSIG: 1 byte
// - OP_ENDIF: 1 byte
// - OP_ENDIF: 1 byte
AcceptedHtlcPenaltyScriptSize = 3*1 + 20 + 5*1 + 33 + 7*1 + 20 + 4*1 +
AcceptedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 7*1 + 20 + 4*1 +
33 + 5*1 + 4 + 5*1

// AcceptedHtlcSuccessWitnessSize 325 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
Expand All @@ -256,8 +278,7 @@ const (
// - revocation_key: 33 bytes
// - witness_script_length: 1 byte
// - witness_script (accepted_htlc_script)
AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 +
AcceptedHtlcPenaltyScriptSize
AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + AcceptedHtlcScriptSize

// OfferedHtlcScriptSize 133 bytes
// - OP_DUP: 1 byte
Expand Down Expand Up @@ -303,6 +324,18 @@ const (
// - witness_script (offered_htlc_script)
OfferedHtlcWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize

// 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

// OfferedHtlcPenaltyWitnessSize 243 bytes
// - number_of_witness_elements: 1 byte
// - revocation_sig_length: 1 byte
Expand Down
26 changes: 19 additions & 7 deletions lnwallet/witnessgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
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 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 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 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
Expand All @@ -33,6 +34,15 @@ const (
// HtlcAcceptedRevoke is a witness that allows us to sweep an HTLC
// output that we accepted from the counterparty.
HtlcAcceptedRevoke WitnessType = 4

// HtlcOfferedTimeout is a witness that allows us to sweep an HTLC
// output that we extended to a party, but was never fulfilled.
HtlcOfferedTimeout WitnessType = 5

// HtlcAcceptedSuccess is a witness that allows us to sweep an HTLC
// output that was offered to us, and for which we have a payment
// preimage.
HtlcAcceptedSuccess WitnessType = 6
)

// WitnessGenerator represents a function which is able to generate the final
Expand Down Expand Up @@ -64,6 +74,8 @@ func (wt WitnessType) GenWitnessFunc(signer Signer,
return ReceiverHtlcSpendRevoke(signer, desc, tx)
case HtlcAcceptedRevoke:
return SenderHtlcSpendRevoke(signer, desc, tx)
case HtlcOfferedTimeout:
return HtlcSpendSuccess(signer, desc, tx)
default:
return nil, fmt.Errorf("unknown witness type: %v", wt)
}
Expand Down