Skip to content

Commit 0389f6a

Browse files
aaraniknocte
authored andcommitted
Fix HTLC signing process
1 parent 80089f0 commit 0389f6a

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-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
@@ -1245,7 +1246,7 @@ and Channel = {
12451246
let htlcSigs =
12461247
sortedHTLCTXs
12471248
|> List.map(
1248-
(fun htlc -> channelPrivKeys.SignHtlcTx htlc.Value remoteNextPerCommitmentPoint)
1249+
(fun htlc -> signHtlcTx htlc channelPrivKeys remoteNextPerCommitmentPoint)
12491250
>> fst
12501251
>> (fun txSig -> txSig.Signature)
12511252
)
@@ -1326,29 +1327,30 @@ and Channel = {
13261327
let sortedHTLCTXs = Commitments.Helpers.sortBothHTLCs htlcTimeoutTxs htlcSuccessTxs
13271328
do! Commitments.checkSignatureCountMismatch sortedHTLCTXs msg
13281329

1329-
let _localHTLCSigs, sortedHTLCTXs =
1330-
let localHtlcSigsAndHTLCTxs =
1331-
sortedHTLCTXs |> List.map(fun htlc ->
1332-
channelPrivKeys.SignHtlcTx htlc.Value localPerCommitmentPoint
1333-
)
1334-
localHtlcSigsAndHTLCTxs |> List.map(fst), localHtlcSigsAndHTLCTxs |> List.map(snd) |> Seq.cast<IHTLCTx> |> List.ofSeq
1330+
let htlcTxsAndSignatures =
1331+
sortedHTLCTXs
1332+
|> List.zip (msg.HTLCSignatures)
1333+
|> List.map(fun (remoteSig, htlc) ->
1334+
htlc, signHtlcTx htlc channelPrivKeys localPerCommitmentPoint |> fst, remoteSig
1335+
)
13351336

13361337
let remoteHTLCPubKey = localPerCommitmentPoint.DeriveHtlcPubKey remoteChannelKeys.HtlcBasepoint
13371338

1338-
let checkHTLCSig (htlc: IHTLCTx, remoteECDSASig: LNECDSASignature): Result<_, _> =
1339-
let remoteS = TransactionSignature(remoteECDSASig.Value, SigHash.All)
1339+
let checkHTLCSig (htlc: IHTLCTx, localSignature: TransactionSignature, remoteECDSASig: LNECDSASignature): Result<_, _> =
1340+
let remoteSignature = TransactionSignature(remoteECDSASig.Value, SigHash.All)
13401341
match htlc with
13411342
| :? HTLCTimeoutTx ->
1342-
(Transactions.checkTxFinalized (htlc.Value) (0) (seq [(remoteHTLCPubKey.RawPubKey(), remoteS)]))
1343+
(Transactions.checkTxFinalized (htlc.Value) (0) (seq [(remoteHTLCPubKey.RawPubKey(), remoteSignature)]))
13431344
|> Result.map(box)
13441345
// we cannot check that htlc-success tx are spendable because we need the payment preimage; thus we only check the remote sig
13451346
| :? HTLCSuccessTx ->
1346-
(Transactions.checkSigAndAdd (htlc) (remoteS) (remoteHTLCPubKey.RawPubKey()))
1347+
(htlc :?> HTLCTimeoutTx).Finalize(localSignature, remoteSignature)
1348+
|> Result.map(FinalizedTx)
13471349
|> Result.map(box)
13481350
| _ -> failwith "Unreachable!"
13491351

13501352
let! txList =
1351-
List.zip sortedHTLCTXs msg.HTLCSignatures
1353+
htlcTxsAndSignatures
13521354
|> List.map(checkHTLCSig)
13531355
|> List.sequenceResultA
13541356
|> expectTransactionErrors

src/DotNetLightning.Core/Crypto/KeyExtensions.fs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ open DotNetLightning.Core.Utils.Extensions
99

1010
open ResultUtils
1111
open ResultUtils.Portability
12+
open NBitcoin.BuilderExtensions
1213

1314
[<AutoOpen>]
1415
module NBitcoinArithmethicExtensions =
@@ -280,19 +281,6 @@ module KeyExtensions =
280281
| Some signature -> (signature, psbt)
281282
| None -> failwithf "Failed to get signature for %A with funding pub key (%A). This should never happen" psbt fundingPubKey
282283

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-
296284
/// This is the node-wide master key which is also used for
297285
/// transport-level encryption. The channel's keys are derived from
298286
/// 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)