Skip to content

Commit 37fe5af

Browse files
aaraniknocte
authored andcommitted
Fix HTLC signing process
PSBT sign function needs transaction builder for any non-predefined script templates so we use helper function currently available in DNL to sign ITimeoutTxs.
1 parent 4241283 commit 37fe5af

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

src/DotNetLightning.Core/Channel/Channel.fs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open DotNetLightning.Utils.Aether
66
open DotNetLightning.Chain
77
open DotNetLightning.Crypto
88
open DotNetLightning.Transactions
9+
open DotNetLightning.Transactions.Transactions
910
open DotNetLightning.Serialization
1011
open DotNetLightning.Serialization.Msgs
1112
open NBitcoin
@@ -1230,7 +1231,7 @@ and Channel = {
12301231
let htlcSigs =
12311232
sortedHTLCTXs
12321233
|> List.map(
1233-
(fun htlc -> channelPrivKeys.SignHtlcTx htlc.Value remoteNextPerCommitmentPoint)
1234+
(fun htlc -> signHtlcTx htlc channelPrivKeys remoteNextPerCommitmentPoint)
12341235
>> fst
12351236
>> (fun txSig -> txSig.Signature)
12361237
)
@@ -1311,29 +1312,30 @@ and Channel = {
13111312
let sortedHTLCTXs = Commitments.Helpers.sortBothHTLCs htlcTimeoutTxs htlcSuccessTxs
13121313
do! Commitments.checkSignatureCountMismatch sortedHTLCTXs msg
13131314

1314-
let _localHTLCSigs, sortedHTLCTXs =
1315-
let localHtlcSigsAndHTLCTxs =
1316-
sortedHTLCTXs |> List.map(fun htlc ->
1317-
channelPrivKeys.SignHtlcTx htlc.Value localPerCommitmentPoint
1318-
)
1319-
localHtlcSigsAndHTLCTxs |> List.map(fst), localHtlcSigsAndHTLCTxs |> List.map(snd) |> Seq.cast<IHTLCTx> |> List.ofSeq
1315+
let htlcTxsAndSignatures =
1316+
sortedHTLCTXs
1317+
|> List.zip (msg.HTLCSignatures)
1318+
|> List.map(fun (remoteSig, htlc) ->
1319+
htlc, signHtlcTx htlc channelPrivKeys localPerCommitmentPoint |> fst, remoteSig
1320+
)
13201321

13211322
let remoteHTLCPubKey = localPerCommitmentPoint.DeriveHtlcPubKey remoteChannelKeys.HtlcBasepoint
13221323

1323-
let checkHTLCSig (htlc: IHTLCTx, remoteECDSASig: LNECDSASignature): Result<_, _> =
1324-
let remoteS = TransactionSignature(remoteECDSASig.Value, SigHash.All)
1324+
let checkHTLCSig (htlc: IHTLCTx, localSignature: TransactionSignature, remoteECDSASig: LNECDSASignature): Result<_, _> =
1325+
let remoteSignature = TransactionSignature(remoteECDSASig.Value, SigHash.All)
13251326
match htlc with
13261327
| :? HTLCTimeoutTx ->
1327-
(Transactions.checkTxFinalized (htlc.Value) (0) (seq [(remoteHTLCPubKey.RawPubKey(), remoteS)]))
1328+
(htlc :?> HTLCTimeoutTx).Finalize(localSignature, remoteSignature)
1329+
|> Result.map(FinalizedTx)
13281330
|> Result.map(box)
13291331
// we cannot check that htlc-success tx are spendable because we need the payment preimage; thus we only check the remote sig
13301332
| :? HTLCSuccessTx ->
1331-
(Transactions.checkSigAndAdd (htlc) (remoteS) (remoteHTLCPubKey.RawPubKey()))
1333+
Transactions.checkSigAndAdd htlc remoteSignature (remoteHTLCPubKey.RawPubKey())
13321334
|> Result.map(box)
13331335
| _ -> failwith "Unreachable!"
13341336

13351337
let! txList =
1336-
List.zip sortedHTLCTXs msg.HTLCSignatures
1338+
htlcTxsAndSignatures
13371339
|> List.map(checkHTLCSig)
13381340
|> List.sequenceResultA
13391341
|> expectTransactionErrors

src/DotNetLightning.Core/Crypto/KeyExtensions.fs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,6 @@ module KeyExtensions =
280280
| Some signature -> (signature, psbt)
281281
| None -> failwithf "Failed to get signature for %A with funding pub key (%A). This should never happen" psbt fundingPubKey
282282

283-
member this.SignHtlcTx (psbt: PSBT)
284-
(perCommitmentPoint: PerCommitmentPoint)
285-
: TransactionSignature * PSBT =
286-
let htlcPrivKey = perCommitmentPoint.DeriveHtlcPrivKey this.HtlcBasepointSecret
287-
let htlcPubKey = htlcPrivKey.HtlcPubKey()
288-
psbt.SignWithKeys(htlcPrivKey.RawKey()) |> ignore
289-
match psbt.GetMatchingSig(htlcPubKey.RawPubKey()) with
290-
| Some signature -> (signature, psbt)
291-
| None ->
292-
failwithf
293-
"failed to get htlc signature for %A. with htlc pubkey (%A) and perCommitmentPoint (%A)"
294-
psbt htlcPubKey perCommitmentPoint
295-
296283
/// This is the node-wide master key which is also used for
297284
/// transport-level encryption. The channel's keys are derived from
298285
/// this via BIP32 key derivation where `channelIndex` is the child

src/DotNetLightning.Core/Transactions/Transactions.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,13 @@ module Transactions =
519519
| Error e -> failwithf "%A" e
520520

521521
let sign(tx, key) = signCore(tx, key, true)
522+
let signHtlcTx (htlc: IHTLCTx) (channelPrivKeys: ChannelPrivKeys) (perCommitmentPoint: PerCommitmentPoint) =
523+
let htlcPrivKey =
524+
perCommitmentPoint.DeriveHtlcPrivKey
525+
channelPrivKeys.HtlcBasepointSecret
526+
527+
sign(htlc, htlcPrivKey.RawKey())
528+
522529
let makeHTLCTimeoutTx (commitTx: Transaction)
523530
(localDustLimit: Money)
524531
(localRevocationPubKey: RevocationPubKey)

0 commit comments

Comments
 (0)