|
| 1 | +package lnwallet |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/btcsuite/btcd/wire" |
| 5 | + "github.com/lightningnetwork/lnd/channeldb" |
| 6 | + "github.com/lightningnetwork/lnd/fn" |
| 7 | + "github.com/lightningnetwork/lnd/input" |
| 8 | + "github.com/lightningnetwork/lnd/tlv" |
| 9 | +) |
| 10 | + |
| 11 | +// BaseAuxJob is a struct that contains the common fields that are shared among |
| 12 | +// the aux sign/verify jobs. |
| 13 | +type BaseAuxJob struct { |
| 14 | + // OutputIndex is the output index of the HTLC on the commitment |
| 15 | + // transaction being signed. |
| 16 | + // |
| 17 | + // NOTE: If the output is dust from the PoV of the commitment chain, |
| 18 | + // then this value will be -1. |
| 19 | + OutputIndex int32 |
| 20 | + |
| 21 | + // KeyRing is the commitment key ring that contains the keys needed to |
| 22 | + // generate the second level HTLC signatures. |
| 23 | + KeyRing CommitmentKeyRing |
| 24 | + |
| 25 | + // HTLC is the HTLC that is being signed or verified. |
| 26 | + HTLC PaymentDescriptor |
| 27 | + |
| 28 | + // Incoming is a boolean that indicates if the HTLC is incoming or |
| 29 | + // outgoing. |
| 30 | + Incoming bool |
| 31 | + |
| 32 | + // CommitBlob is the commitment transaction blob that contains the aux |
| 33 | + // information for this channel. |
| 34 | + CommitBlob fn.Option[tlv.Blob] |
| 35 | + |
| 36 | + // HtlcLeaf is the aux tap leaf that corresponds to the HTLC being |
| 37 | + // signed/verified. |
| 38 | + HtlcLeaf input.AuxTapLeaf |
| 39 | +} |
| 40 | + |
| 41 | +// AuxSigJob is a struct that contains all the information needed to sign an |
| 42 | +// HTLC for custom channels. |
| 43 | +type AuxSigJob struct { |
| 44 | + // SignDesc is the sign desc for this HTLC. |
| 45 | + SignDesc input.SignDescriptor |
| 46 | + |
| 47 | + BaseAuxJob |
| 48 | + |
| 49 | + // Resp is a channel that will be used to send the result of the sign |
| 50 | + // job. |
| 51 | + Resp chan AuxSigJobResp |
| 52 | + |
| 53 | + // Cancel is a channel that should be closed if the caller wishes to |
| 54 | + // abandon all pending sign jobs part of a single batch. |
| 55 | + Cancel chan struct{} |
| 56 | +} |
| 57 | + |
| 58 | +// NewAuxSigJob creates a new AuxSigJob. |
| 59 | +func NewAuxSigJob(sigJob SignJob, keyRing CommitmentKeyRing, incoming bool, |
| 60 | + htlc PaymentDescriptor, commitBlob fn.Option[tlv.Blob], |
| 61 | + htlcLeaf input.AuxTapLeaf, cancelChan chan struct{}) AuxSigJob { |
| 62 | + |
| 63 | + return AuxSigJob{ |
| 64 | + SignDesc: sigJob.SignDesc, |
| 65 | + BaseAuxJob: BaseAuxJob{ |
| 66 | + OutputIndex: sigJob.OutputIndex, |
| 67 | + KeyRing: keyRing, |
| 68 | + HTLC: htlc, |
| 69 | + Incoming: incoming, |
| 70 | + CommitBlob: commitBlob, |
| 71 | + HtlcLeaf: htlcLeaf, |
| 72 | + }, |
| 73 | + Resp: make(chan AuxSigJobResp, 1), |
| 74 | + Cancel: cancelChan, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// AuxSigJobResp is a struct that contains the result of a sign job. |
| 79 | +type AuxSigJobResp struct { |
| 80 | + // SigBlob is the signature blob that was generated for the HTLC. This |
| 81 | + // is an opaque TLV field that may contain the signature and other data. |
| 82 | + SigBlob fn.Option[tlv.Blob] |
| 83 | + |
| 84 | + // HtlcIndex is the index of the HTLC that was signed. |
| 85 | + HtlcIndex uint64 |
| 86 | + |
| 87 | + // Err is the error that occurred when executing the specified |
| 88 | + // signature job. In the case that no error occurred, this value will |
| 89 | + // be nil. |
| 90 | + Err error |
| 91 | +} |
| 92 | + |
| 93 | +// AuxVerifyJob is a struct that contains all the information needed to verify |
| 94 | +// an HTLC for custom channels. |
| 95 | +type AuxVerifyJob struct { |
| 96 | + // SigBlob is the signature blob that was generated for the HTLC. This |
| 97 | + // is an opaque TLV field that may contain the signature and other data. |
| 98 | + SigBlob fn.Option[tlv.Blob] |
| 99 | + |
| 100 | + BaseAuxJob |
| 101 | + |
| 102 | + // Cancel is a channel that should be closed if the caller wishes to |
| 103 | + // abandon the job. |
| 104 | + Cancel chan struct{} |
| 105 | + |
| 106 | + // ErrResp is a channel that will be used to send the result of the |
| 107 | + // verify job. |
| 108 | + ErrResp chan error |
| 109 | +} |
| 110 | + |
| 111 | +// NewAuxVerifyJob creates a new AuxVerifyJob. |
| 112 | +func NewAuxVerifyJob(sig fn.Option[tlv.Blob], keyRing CommitmentKeyRing, |
| 113 | + incoming bool, htlc PaymentDescriptor, commitBlob fn.Option[tlv.Blob], |
| 114 | + htlcLeaf input.AuxTapLeaf) AuxVerifyJob { |
| 115 | + |
| 116 | + return AuxVerifyJob{ |
| 117 | + SigBlob: sig, |
| 118 | + BaseAuxJob: BaseAuxJob{ |
| 119 | + KeyRing: keyRing, |
| 120 | + HTLC: htlc, |
| 121 | + Incoming: incoming, |
| 122 | + CommitBlob: commitBlob, |
| 123 | + HtlcLeaf: htlcLeaf, |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// AuxSigner is an interface that is used to sign and verify HTLCs for custom |
| 129 | +// channels. It is similar to the existing SigPool, but uses opaque blobs to |
| 130 | +// shuffle around signature information and other metadata. |
| 131 | +type AuxSigner interface { |
| 132 | + // SubmitSecondLevelSigBatch takes a batch of aux sign jobs and |
| 133 | + // processes them asynchronously. |
| 134 | + SubmitSecondLevelSigBatch(chanState *channeldb.OpenChannel, |
| 135 | + commitTx *wire.MsgTx, sigJob []AuxSigJob) error |
| 136 | + |
| 137 | + // PackSigs takes a series of aux signatures and packs them into a |
| 138 | + // single blob that can be sent alongside the CommitSig messages. |
| 139 | + PackSigs([]fn.Option[tlv.Blob]) (fn.Option[tlv.Blob], error) |
| 140 | + |
| 141 | + // UnpackSigs takes a packed blob of signatures and returns the |
| 142 | + // original signatures for each HTLC, keyed by HTLC index. |
| 143 | + UnpackSigs(fn.Option[tlv.Blob]) ([]fn.Option[tlv.Blob], error) |
| 144 | + |
| 145 | + // VerifySecondLevelSigs attempts to synchronously verify a batch of aux |
| 146 | + // sig jobs. |
| 147 | + VerifySecondLevelSigs(chanState *channeldb.OpenChannel, |
| 148 | + commitTx *wire.MsgTx, verifyJob []AuxVerifyJob) error |
| 149 | +} |
0 commit comments