Skip to content

Commit

Permalink
multi: return input.Signature from SignOutputRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Apr 10, 2020
1 parent 37dffb2 commit 0f94b8d
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 110 deletions.
2 changes: 1 addition & 1 deletion chancloser.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (c *channelCloser) proposeCloseSigned(fee btcutil.Amount) (*lnwire.ClosingS
// party responds we'll be able to decide if we've agreed on fees or
// not.
c.lastFeeProposal = fee
parsedSig, err := lnwire.NewSigFromRawSignature(rawSig)
parsedSig, err := lnwire.NewSigFromSignature(rawSig)
if err != nil {
return nil, err
}
Expand Down
25 changes: 18 additions & 7 deletions contractcourt/htlc_timeout_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
Expand All @@ -15,12 +16,22 @@ import (
"github.com/lightningnetwork/lnd/lnwallet"
)

type dummySignature struct{}

func (s *dummySignature) Serialize() []byte {
return []byte{}
}

func (s *dummySignature) Verify(_ []byte, _ *btcec.PublicKey) bool {
return true
}

type mockSigner struct {
}

func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx,
signDesc *input.SignDescriptor) ([]byte, error) {
return nil, nil
signDesc *input.SignDescriptor) (input.Signature, error) {
return &dummySignature{}, nil
}

func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx,
Expand Down Expand Up @@ -145,8 +156,8 @@ func TestHtlcTimeoutResolver(t *testing.T) {
timeout: true,
txToBroadcast: func() (*wire.MsgTx, error) {
witness, err := input.SenderHtlcSpendTimeout(
nil, txscript.SigHashAll, signer,
fakeSignDesc, sweepTx,
&dummySignature{}, txscript.SigHashAll,
signer, fakeSignDesc, sweepTx,
)
if err != nil {
return nil, err
Expand All @@ -165,9 +176,9 @@ func TestHtlcTimeoutResolver(t *testing.T) {
timeout: false,
txToBroadcast: func() (*wire.MsgTx, error) {
witness, err := input.ReceiverHtlcSpendRedeem(
nil, txscript.SigHashAll,
fakePreimageBytes, signer,
fakeSignDesc, sweepTx,
&dummySignature{}, txscript.SigHashAll,
fakePreimageBytes, signer, fakeSignDesc,
sweepTx,
)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion discovery/gossiper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -96,7 +97,7 @@ type mockSigner struct {
}

func (n *mockSigner) SignMessage(pubKey *btcec.PublicKey,
msg []byte) (*btcec.Signature, error) {
msg []byte) (input.Signature, error) {

if !pubKey.IsEqual(n.privKey.PubKey()) {
return nil, fmt.Errorf("unknown public key")
Expand Down
26 changes: 20 additions & 6 deletions fundingmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ type fundingConfig struct {
//
// TODO(roasbeef): should instead pass on this responsibility to a
// distinct sub-system?
SignMessage func(pubKey *btcec.PublicKey, msg []byte) (*btcec.Signature, error)
SignMessage func(pubKey *btcec.PublicKey,
msg []byte) (input.Signature, error)

// CurrentNodeAnnouncement should return the latest, fully signed node
// announcement from the backing Lightning Network node.
Expand Down Expand Up @@ -1726,7 +1727,7 @@ func (f *fundingManager) continueFundingAccept(resCtx *reservationWithCtx,
PendingChannelID: pendingChanID,
FundingPoint: *outPoint,
}
fundingCreated.CommitSig, err = lnwire.NewSigFromRawSignature(sig)
fundingCreated.CommitSig, err = lnwire.NewSigFromSignature(sig)
if err != nil {
fndgLog.Errorf("Unable to parse signature: %v", err)
f.failFundingFlow(resCtx.peer, pendingChanID, err)
Expand Down Expand Up @@ -1775,14 +1776,21 @@ func (f *fundingManager) handleFundingCreated(fmsg *fundingCreatedMsg) {
fndgLog.Infof("completing pending_id(%x) with ChannelPoint(%v)",
pendingChanID[:], fundingOut)

commitSig, err := fmsg.msg.CommitSig.ToSignature()
if err != nil {
fndgLog.Errorf("unable to parse signature: %v", err)
f.failFundingFlow(fmsg.peer, pendingChanID, err)
return
}

// With all the necessary data available, attempt to advance the
// funding workflow to the next stage. If this succeeds then the
// funding transaction will broadcast after our next message.
// CompleteReservationSingle will also mark the channel as 'IsPending'
// in the database.
commitSig := fmsg.msg.CommitSig.ToSignatureBytes()
completeChan, err := resCtx.reservation.CompleteReservationSingle(
&fundingOut, commitSig)
&fundingOut, commitSig,
)
if err != nil {
// TODO(roasbeef): better error logging: peerID, channelID, etc.
fndgLog.Errorf("unable to complete single reservation: %v", err)
Expand Down Expand Up @@ -1837,7 +1845,7 @@ func (f *fundingManager) handleFundingCreated(fmsg *fundingCreatedMsg) {
// With their signature for our version of the commitment transaction
// verified, we can now send over our signature to the remote peer.
_, sig := resCtx.reservation.OurSignatures()
ourCommitSig, err := lnwire.NewSigFromRawSignature(sig)
ourCommitSig, err := lnwire.NewSigFromSignature(sig)
if err != nil {
fndgLog.Errorf("unable to parse signature: %v", err)
f.failFundingFlow(fmsg.peer, pendingChanID, err)
Expand Down Expand Up @@ -1950,7 +1958,13 @@ func (f *fundingManager) handleFundingSigned(fmsg *fundingSignedMsg) {
// The remote peer has responded with a signature for our commitment
// transaction. We'll verify the signature for validity, then commit
// the state to disk as we can now open the channel.
commitSig := fmsg.msg.CommitSig.ToSignatureBytes()
commitSig, err := fmsg.msg.CommitSig.ToSignature()
if err != nil {
fndgLog.Errorf("Unable to parse signature: %v", err)
f.failFundingFlow(fmsg.peer, pendingChanID, err)
return
}

completeChan, err := resCtx.reservation.CompleteReservation(
nil, commitSig,
)
Expand Down
6 changes: 4 additions & 2 deletions fundingmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
Wallet: lnw,
Notifier: chainNotifier,
FeeEstimator: estimator,
SignMessage: func(pubKey *btcec.PublicKey, msg []byte) (*btcec.Signature, error) {
SignMessage: func(pubKey *btcec.PublicKey,
msg []byte) (input.Signature, error) {

return testSig, nil
},
SendAnnouncement: func(msg lnwire.Message,
Expand Down Expand Up @@ -474,7 +476,7 @@ func recreateAliceFundingManager(t *testing.T, alice *testNode) {
Notifier: oldCfg.Notifier,
FeeEstimator: oldCfg.FeeEstimator,
SignMessage: func(pubKey *btcec.PublicKey,
msg []byte) (*btcec.Signature, error) {
msg []byte) (input.Signature, error) {
return testSig, nil
},
SendAnnouncement: func(msg lnwire.Message,
Expand Down
6 changes: 4 additions & 2 deletions htlcswitch/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,9 @@ type mockSigner struct {
key *btcec.PrivateKey
}

func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *input.SignDescriptor) ([]byte, error) {
func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx,
signDesc *input.SignDescriptor) (input.Signature, error) {

amt := signDesc.Output.Value
witnessScript := signDesc.WitnessScript
privKey := m.key
Expand All @@ -877,7 +879,7 @@ func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *input.SignDescripto
return nil, err
}

return sig[:len(sig)-1], nil
return btcec.ParseDERSignature(sig[:len(sig)-1], btcec.S256())
}
func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *input.SignDescriptor) (*input.Script, error) {

Expand Down
32 changes: 18 additions & 14 deletions input/script_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var (
type Signature interface {
// Serialize returns a DER-encoded ECDSA signature.
Serialize() []byte

// Verify return true if the ECDSA signature is valid for the passed
// message digest under the provided public key.
Verify([]byte, *btcec.PublicKey) bool
}

// WitnessScriptHash generates a pay-to-witness-script-hash public key script
Expand Down Expand Up @@ -290,7 +294,7 @@ func SenderHtlcSpendRevokeWithKey(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = revokeKey.SerializeCompressed()
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -339,7 +343,7 @@ func SenderHtlcSpendRedeem(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = paymentPreimage
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -367,7 +371,7 @@ func SenderHtlcSpendTimeout(receiverSig Signature,
witnessStack := wire.TxWitness(make([][]byte, 5))
witnessStack[0] = nil
witnessStack[1] = append(receiverSig.Serialize(), byte(receiverSigHash))
witnessStack[2] = append(sweepSig, byte(signDesc.HashType))
witnessStack[2] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[3] = nil
witnessStack[4] = signDesc.WitnessScript

Expand Down Expand Up @@ -535,7 +539,7 @@ func ReceiverHtlcSpendRedeem(senderSig Signature,
witnessStack := wire.TxWitness(make([][]byte, 5))
witnessStack[0] = nil
witnessStack[1] = append(senderSig.Serialize(), byte(senderSigHash))
witnessStack[2] = append(sweepSig, byte(signDesc.HashType))
witnessStack[2] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[3] = paymentPreimage
witnessStack[4] = signDesc.WitnessScript

Expand All @@ -562,7 +566,7 @@ func ReceiverHtlcSpendRevokeWithKey(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = revokeKey.SerializeCompressed()
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -627,7 +631,7 @@ func ReceiverHtlcSpendTimeout(signer Signer, signDesc *SignDescriptor,
}

witnessStack := wire.TxWitness(make([][]byte, 3))
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
witnessStack[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = nil
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -732,7 +736,7 @@ func HtlcSpendSuccess(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = nil
witnessStack[2] = signDesc.WitnessScript

Expand All @@ -757,7 +761,7 @@ func HtlcSpendRevoke(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = []byte{1}
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -788,7 +792,7 @@ func HtlcSecondLevelSpend(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(txscript.SigHashAll))
witnessStack[1] = nil
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -892,7 +896,7 @@ func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = nil
witnessStack[2] = signDesc.WitnessScript

Expand All @@ -917,7 +921,7 @@ func CommitSpendRevoke(signer Signer, signDesc *SignDescriptor,
// 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[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = []byte{1}
witnessStack[2] = signDesc.WitnessScript

Expand Down Expand Up @@ -951,7 +955,7 @@ func CommitSpendNoDelay(signer Signer, signDesc *SignDescriptor,
// exact same as a regular p2wkh witness, depending on the value of the
// tweakless bool.
witness := make([][]byte, 2)
witness[0] = append(sweepSig, byte(signDesc.HashType))
witness[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))

switch tweakless {
// If we're tweaking the key, then we use the tweaked public key as the
Expand Down Expand Up @@ -1028,7 +1032,7 @@ func CommitSpendToRemoteConfirmed(signer Signer, signDesc *SignDescriptor,
// Finally, we'll manually craft the witness. The witness here is the
// signature and the redeem script.
witnessStack := make([][]byte, 2)
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
witnessStack[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = signDesc.WitnessScript

return witnessStack, nil
Expand Down Expand Up @@ -1084,7 +1088,7 @@ func CommitSpendAnchor(signer Signer, signDesc *SignDescriptor,

// The witness here is just a signature and the redeem script.
witnessStack := make([][]byte, 2)
witnessStack[0] = append(sweepSig, byte(signDesc.HashType))
witnessStack[0] = append(sweepSig.Serialize(), byte(signDesc.HashType))
witnessStack[1] = signDesc.WitnessScript

return witnessStack, nil
Expand Down
12 changes: 8 additions & 4 deletions input/script_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
bobSigBytes, err := bobSigner.SignOutputRaw(sweepTx, &bobSignDesc)
bobSig, err := bobSigner.SignOutputRaw(sweepTx, &bobSignDesc)
if err != nil {
t.Fatalf("unable to generate alice signature: %v", err)
}

bobRecvrSig, err = btcec.ParseDERSignature(bobSigBytes, btcec.S256())
bobRecvrSig, err = btcec.ParseDERSignature(
bobSig.Serialize(), btcec.S256(),
)
if err != nil {
t.Fatalf("unable to parse signature: %v", err)
}
Expand Down Expand Up @@ -700,12 +702,14 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
aliceSigBytes, err := aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc)
aliceSig, err := aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc)
if err != nil {
t.Fatalf("unable to generate alice signature: %v", err)
}

aliceSenderSig, err = btcec.ParseDERSignature(aliceSigBytes, btcec.S256())
aliceSenderSig, err = btcec.ParseDERSignature(
aliceSig.Serialize(), btcec.S256(),
)
if err != nil {
t.Fatalf("unable to parse signature: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion input/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type Signer interface {
// 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)
SignOutputRaw(tx *wire.MsgTx,
signDesc *SignDescriptor) (Signature, error)

// ComputeInputScript generates a complete InputIndex for the passed
// transaction with the signature as defined within the passed
Expand Down
Loading

0 comments on commit 0f94b8d

Please sign in to comment.