Skip to content

Commit d0b91b7

Browse files
committed
nit fixes
1 parent b54a9bc commit d0b91b7

File tree

9 files changed

+61
-56
lines changed

9 files changed

+61
-56
lines changed

src/DotNetLightning.Core/Channel/CommitmentsModule.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module internal Commitments =
137137
match cm.GetHTLCCrossSigned(Direction.Out, msg.HTLCId) with
138138
| Some htlc when htlc.PaymentHash = msg.PaymentPreimage.Hash ->
139139
let commitments = cm.AddRemoteProposal(msg)
140-
let origin = cm.OriginChannels |> Map.tryFind(msg.HTLCId)
140+
let origin = cm.OriginChannels |> Map.tryFind msg.HTLCId
141141
[WeAcceptedFulfillHTLC(msg, origin, htlc, commitments)] |> Ok
142142
| Some htlc ->
143143
(htlc.PaymentHash, msg.PaymentPreimage)
@@ -345,10 +345,10 @@ module internal Commitments =
345345
Transactions.checkTxFinalized signedCommitTx localCommitTx.WhichInput sigPair
346346
|> expectTransactionError
347347
let! finalizedCommitTx = tmp
348-
let sortedHTLCTXs = Helpers.sortBothHTLCs htlcTimeoutTxs htlcSuccessTxs
348+
let sortedHTLCTXs = Helpers.sortBothHTLCs htlcTimeoutTxs htlcSuccessTxs
349349
do! checkSignatureCountMismatch sortedHTLCTXs msg
350350

351-
let HTLCTxsAndSignatures =
351+
let htlcTxsAndSignatures =
352352
sortedHTLCTXs
353353
|> List.zip (msg.HTLCSignatures)
354354
|> List.map(fun (remoteSig, htlc) ->
@@ -371,7 +371,7 @@ module internal Commitments =
371371
| _ -> failwith "Unreachable!"
372372

373373
let! txList =
374-
HTLCTxsAndSignatures
374+
htlcTxsAndSignatures
375375
|> List.map(checkHTLCSig)
376376
|> List.sequenceResultA
377377
|> expectTransactionErrors

src/DotNetLightning.Core/Crypto/KeyExtensions.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ module KeyExtensions =
286286
: TransactionSignature * PSBT =
287287
let htlcPrivKey = perCommitmentPoint.DeriveHtlcPrivKey this.HtlcBasepointSecret
288288
let htlcPubKey = htlcPrivKey.HtlcPubKey()
289-
psbt.Settings.CustomBuilderExtensions <- ([new HTLCReceivedExtensions() :> BuilderExtension; new HTLCOfferedExtensions():> BuilderExtension] |> Seq.ofList)
289+
psbt.Settings.CustomBuilderExtensions <-
290+
[
291+
HtlcReceivedExtensions() :> BuilderExtension
292+
HtlcOfferedExtensions() :> BuilderExtension
293+
] |> Seq.ofList
290294
psbt.SignWithKeys(htlcPrivKey.RawKey()) |> ignore
291295
match psbt.GetMatchingSig(htlcPubKey.RawPubKey()) with
292296
| Some signature -> (signature, psbt)

src/DotNetLightning.Core/Crypto/Sphinx.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module Sphinx =
9191
computeEphemeralPublicKeysAndSharedSecretsCore
9292
(sessionKey) (pubKeys |> List.tail) ([ephemeralPK0]) ([blindingFactor0]) ([secret0])
9393

94-
let rec internal generateFiller (keyType: string) (payloads: byte[] list) (sharedSecrets: Key list) =
94+
let rec internal generateFiller (keyType: string) (payloads: list<array<byte>>) (sharedSecrets: list<Key>) =
9595
let filler_size =
9696
payloads.[1..] |>
9797
List.sumBy (fun payload -> payload.Length + MacLength)
@@ -106,7 +106,6 @@ module Sphinx =
106106
payloads.[..i-1] |>
107107
List.sumBy (fun payload -> payload.Length + MacLength)
108108

109-
110109
let filler_start = HopDataSize - filler_offset
111110
let filler_end = HopDataSize + payloads.[i].Length + MacLength
112111
let filler_len = filler_end - filler_start
@@ -197,14 +196,14 @@ module Sphinx =
197196
// set of filler bytes by using chacha20 with a key derived from the session
198197
// key.
199198
let DeterministicPacketFiller (sessionKey: Key) =
200-
generateStream(generateKey("pad",sessionKey.ToBytes()), 1300)
199+
generateStream(generateKey("pad", sessionKey.ToBytes()), HopDataSize)
201200

202201
// BlankPacketFiller is a packet filler that doesn't attempt to fill out the
203202
// packet at all. It should ONLY be used for generating test vectors or other
204203
// instances that required deterministic packet generation.
205204
[<Obsolete("BlankPacketFiller is obsolete, see here: https://github.com/lightningnetwork/lightning-rfc/commit/8dd0b75809c9a7498bb9031a6674e5f58db509f4", false)>]
206205
let BlankPacketFiller _=
207-
Array.zeroCreate 1300
206+
Array.zeroCreate HopDataSize
208207

209208
type PacketAndSecrets = {
210209
Packet: OnionPacket
@@ -213,7 +212,7 @@ module Sphinx =
213212
SharedSecrets: (Key * PubKey) list
214213
}
215214
with
216-
static member Create (sessionKey: Key, pubKeys: PubKey list, payloads: byte[] list, ad: byte[], initialPacketFiller: Key -> byte[]) =
215+
static member Create (sessionKey: Key, pubKeys: list<PubKey>, payloads: list<array<byte>>, ad: array<byte>, initialPacketFiller: Key -> array<byte>) =
217216
let (ephemeralPubKeys, sharedSecrets) = computeEphemeralPublicKeysAndSharedSecrets (sessionKey) (pubKeys)
218217
let filler = generateFiller "rho" payloads sharedSecrets
219218

src/DotNetLightning.Core/DotNetLightning.Core.fsproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
<Compile Include="Utils/Errors.fs" />
4747
<Compile Include="Utils/RouteType.fs" />
4848
<Compile Include="Utils/PriorityQueue.fs" />
49-
<Compile Include="Utils/HTLCReceivedExtensions.fs" />
50-
<Compile Include="Utils/HTLCOfferedExtensions.fs" />
49+
<Compile Include="Utils/HtlcReceivedExtensions.fs" />
50+
<Compile Include="Utils/HtlcOfferedExtensions.fs" />
5151
<Compile Include="Chain/ChainInterface.fs" />
5252
<Compile Include="Chain/KeysInterface.fs" />
5353
<Compile Include="Serialization\GenericTLV.fs" />

src/DotNetLightning.Core/Serialization/TLVs.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ type HopPayloadTLV =
108108
static member FromGenericTLV(tlv: GenericTLV) =
109109
match tlv.Type with
110110
| 2UL ->
111-
UInt64.FromTruncatedBytes(tlv.Value)
111+
UInt64.FromTruncatedBytes tlv.Value
112112
|> LNMoney.MilliSatoshis
113113
|> AmountToForward
114114
| 4UL ->
115-
UInt32.FromTruncatedBytes(tlv.Value)
115+
UInt32.FromTruncatedBytes tlv.Value
116116
|> OutgoingCLTV
117117
| 6UL ->
118-
ShortChannelId.From8Bytes(tlv.Value)
118+
ShortChannelId.From8Bytes tlv.Value
119119
|> ShortChannelId
120120
| 8UL ->
121121
let secret = tlv.Value.[0..PaymentSecret.LENGTH - 1] |> PaymentSecret.FromByteArray
122-
let totalMSat = UInt64.FromTruncatedBytes(tlv.Value.[PaymentSecret.LENGTH..]) |> LNMoney.MilliSatoshis
122+
let totalMSat = UInt64.FromTruncatedBytes tlv.Value.[PaymentSecret.LENGTH..] |> LNMoney.MilliSatoshis
123123
(secret, totalMSat) |> PaymentData
124124
| _ -> Unknown tlv
125125

src/DotNetLightning.Core/Transactions/Transactions.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ module Transactions =
524524
let dest = Scripts.toLocalDelayed localRevocationPubKey toLocalDelay localDelayedPaymentPubKey
525525
// we have already done dust limit check above
526526
txb.DustPrevention <- false
527-
txb.Extensions.Add (new HTLCOfferedExtensions())
527+
txb.Extensions.Add (HtlcOfferedExtensions())
528528
let tx = txb.AddCoins(scriptCoin)
529529
.Send(dest.WitHash, amount)
530530
.SendFees(fee)
@@ -567,7 +567,7 @@ module Transactions =
567567
ScriptCoin(coin, redeem)
568568
let dest = Scripts.toLocalDelayed localRevocationPubKey toLocalDelay localDelayedPaymentPubKey
569569
// we have already done dust limit check above
570-
txb.Extensions.Add (new HTLCReceivedExtensions())
570+
txb.Extensions.Add (HtlcReceivedExtensions())
571571
txb.DustPrevention <- false
572572
let tx = txb.AddCoins(scriptCoin)
573573
.Send(dest.WitHash, amount)

src/DotNetLightning.Core/Utils/Extensions.fs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,29 @@ type System.UInt64 with
6060
buf
6161

6262
member private x.GetLeadingZerosCount() =
63-
if (x = 0UL) then
63+
if x = 0UL then
6464
8
65-
else if (x &&& 0xffffffffffffff00UL = 0UL) then
65+
elif x &&& 0xffffffffffffff00UL = 0UL then
6666
7
67-
else if (x &&& 0xffffffffffff0000UL = 0UL) then
67+
elif x &&& 0xffffffffffff0000UL = 0UL then
6868
6
69-
else if (x &&& 0xffffffffff000000UL = 0UL) then
69+
elif x &&& 0xffffffffff000000UL = 0UL then
7070
5
71-
else if (x &&& 0xffffffff00000000UL = 0UL) then
71+
elif x &&& 0xffffffff00000000UL = 0UL then
7272
4
73-
else if (x &&& 0xffffff0000000000UL = 0UL) then
73+
elif x &&& 0xffffff0000000000UL = 0UL then
7474
3
75-
else if (x &&& 0xffff000000000000UL = 0UL) then
75+
elif x &&& 0xffff000000000000UL = 0UL then
7676
2
77-
else if (x &&& 0xff00000000000000UL = 0UL) then
77+
elif x &&& 0xff00000000000000UL = 0UL then
7878
1
7979
else
8080
0
8181

8282
member x.GetTruncatedBytes() =
8383
let numZeros =
8484
x.GetLeadingZerosCount()
85-
x.GetBytesBigEndian() |> Array.skip(numZeros)
85+
x.GetBytesBigEndian() |> Array.skip numZeros
8686

8787
//TODO: better error handling?
8888
static member FromTruncatedBytes(bytes: array<byte>): uint64 =
@@ -114,21 +114,21 @@ type System.UInt32 with
114114
BitConverter.ToUInt32(bytes, 0)
115115

116116
member private x.GetLeadingZerosCount() =
117-
if (x = 0u) then
117+
if x = 0u then
118118
4
119-
else if (x &&& 0xffffff00u = 0u) then
119+
elif x &&& 0xffffff00u = 0u then
120120
3
121-
else if (x &&& 0xffff0000u = 0u) then
121+
elif x &&& 0xffff0000u = 0u then
122122
2
123-
else if (x &&& 0xff000000u = 0u) then
123+
elif x &&& 0xff000000u = 0u then
124124
1
125125
else
126126
0
127127

128128
member x.GetTruncatedBytes() =
129129
let numZeros =
130130
x.GetLeadingZerosCount()
131-
x.GetBytesBigEndian() |> Array.skip(numZeros)
131+
x.GetBytesBigEndian() |> Array.skip numZeros
132132

133133
//TODO: better error handling?
134134
static member FromTruncatedBytes(bytes: array<byte>): uint32 =

src/DotNetLightning.Core/Utils/HTLCOfferedExtensions.fs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ open ResultUtils
99
open ResultUtils.Portability
1010
open Newtonsoft.Json
1111

12-
type HTLCOfferedParameters = {
12+
type HtlcOfferedParameters = {
1313
LocalHtlcPubKey: HtlcPubKey
1414
RemoteHtlcPubKey: HtlcPubKey
1515
}
1616
with
17-
static member TryExtractParameters (scriptPubKey: Script): Option<HTLCOfferedParameters> =
17+
static member TryExtractParameters (scriptPubKey: Script): Option<HtlcOfferedParameters> =
1818
let ops =
1919
scriptPubKey.ToOps()
2020
// we have to collect it into a list and convert back to a seq
@@ -23,7 +23,6 @@ type HTLCOfferedParameters = {
2323
|> List.ofSeq
2424
|> Seq.ofList
2525

26-
Console.WriteLine(JsonConvert.SerializeObject(ops))
2726
let checkOpCode(opcodeType: OpcodeType) = seqParser<Op> {
2827
let! op = SeqParser.next()
2928
if op.Code = opcodeType then
@@ -88,31 +87,33 @@ type HTLCOfferedParameters = {
8887
| Ok data -> Some data
8988
| Error _consumeAllError -> None
9089

91-
type internal HTLCOfferedExtensions() =
90+
type internal HtlcOfferedExtensions() =
9291
inherit BuilderExtension()
9392
override self.CanGenerateScriptSig (scriptPubKey: Script): bool =
94-
(HTLCOfferedParameters.TryExtractParameters scriptPubKey).IsSome
93+
(HtlcOfferedParameters.TryExtractParameters scriptPubKey).IsSome
9594

9695
override self.GenerateScriptSig(scriptPubKey: Script, keyRepo: IKeyRepository, signer: ISigner): Script =
9796
let parameters =
98-
match (HTLCOfferedParameters.TryExtractParameters scriptPubKey) with
97+
match (HtlcOfferedParameters.TryExtractParameters scriptPubKey) with
9998
| Some parameters -> parameters
10099
| None ->
101100
failwith
102101
"NBitcoin should not call this unless CanGenerateScriptSig returns true"
103-
let pubKey = keyRepo.FindKey scriptPubKey
102+
let pubKeyOpt =
103+
keyRepo.FindKey scriptPubKey
104+
|> Option.ofObj
104105
// FindKey will return null if it can't find a key for
105106
// scriptPubKey. If we can't find a valid key then this method
106107
// should return null, indicating to NBitcoin that the sigScript
107108
// could not be generated.
108-
match pubKey with
109-
| null -> null
110-
| _ when pubKey = parameters.LocalHtlcPubKey.RawPubKey() ->
109+
match pubKeyOpt with
110+
| None -> null
111+
| Some pubKey when pubKey = parameters.LocalHtlcPubKey.RawPubKey() ->
111112
let localHtlcSig = signer.Sign (parameters.LocalHtlcPubKey.RawPubKey())
112113
Script [
113114
Op.GetPushOp (localHtlcSig.ToBytes())
114115
]
115-
| _ when pubKey = parameters.RemoteHtlcPubKey.RawPubKey() ->
116+
| Some pubKey when pubKey = parameters.RemoteHtlcPubKey.RawPubKey() ->
116117
let remoteHtlcSig = signer.Sign (parameters.RemoteHtlcPubKey.RawPubKey())
117118
Script [
118119
Op.GetPushOp (remoteHtlcSig.ToBytes())
@@ -138,7 +139,7 @@ type internal HTLCOfferedExtensions() =
138139
raise <| NotSupportedException()
139140

140141
override self.IsCompatibleKey(pubKey: PubKey, scriptPubKey: Script): bool =
141-
match HTLCOfferedParameters.TryExtractParameters scriptPubKey with
142+
match HtlcOfferedParameters.TryExtractParameters scriptPubKey with
142143
| None -> false
143144
| Some parameters ->
144145
parameters.LocalHtlcPubKey.RawPubKey() = pubKey

src/DotNetLightning.Core/Utils/HTLCReceivedExtensions.fs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ open ResultUtils
99
open ResultUtils.Portability
1010
open Newtonsoft.Json
1111

12-
type HTLCReceivedParameters = {
12+
type HtlcReceivedParameters = {
1313
LocalHtlcPubKey: HtlcPubKey
1414
RemoteHtlcPubKey: HtlcPubKey
1515
}
1616
with
17-
static member TryExtractParameters (scriptPubKey: Script): Option<HTLCReceivedParameters> =
17+
static member TryExtractParameters (scriptPubKey: Script): Option<HtlcReceivedParameters> =
1818
let ops =
1919
scriptPubKey.ToOps()
2020
// we have to collect it into a list and convert back to a seq
@@ -23,7 +23,6 @@ type HTLCReceivedParameters = {
2323
|> List.ofSeq
2424
|> Seq.ofList
2525

26-
Console.WriteLine(JsonConvert.SerializeObject(ops))
2726
let checkOpCode(opcodeType: OpcodeType) = seqParser<Op> {
2827
let! op = SeqParser.next()
2928
if op.Code = opcodeType then
@@ -94,31 +93,33 @@ type HTLCReceivedParameters = {
9493
| Ok data -> Some data
9594
| Error _consumeAllError -> None
9695

97-
type internal HTLCReceivedExtensions() =
96+
type internal HtlcReceivedExtensions() =
9897
inherit BuilderExtension()
9998
override self.CanGenerateScriptSig (scriptPubKey: Script): bool =
100-
(HTLCReceivedParameters.TryExtractParameters scriptPubKey).IsSome
99+
(HtlcReceivedParameters.TryExtractParameters scriptPubKey).IsSome
101100

102101
override self.GenerateScriptSig(scriptPubKey: Script, keyRepo: IKeyRepository, signer: ISigner): Script =
103102
let parameters =
104-
match (HTLCReceivedParameters.TryExtractParameters scriptPubKey) with
103+
match (HtlcReceivedParameters.TryExtractParameters scriptPubKey) with
105104
| Some parameters -> parameters
106105
| None ->
107106
failwith
108107
"NBitcoin should not call this unless CanGenerateScriptSig returns true"
109-
let pubKey = keyRepo.FindKey scriptPubKey
108+
let pubKeyOpt =
109+
keyRepo.FindKey scriptPubKey
110+
|> Option.ofObj
110111
// FindKey will return null if it can't find a key for
111112
// scriptPubKey. If we can't find a valid key then this method
112113
// should return null, indicating to NBitcoin that the sigScript
113114
// could not be generated.
114-
match pubKey with
115-
| null -> null
116-
| _ when pubKey = parameters.LocalHtlcPubKey.RawPubKey() ->
115+
match pubKeyOpt with
116+
| None -> null
117+
| Some pubKey when pubKey = parameters.LocalHtlcPubKey.RawPubKey() ->
117118
let localHtlcSig = signer.Sign (parameters.LocalHtlcPubKey.RawPubKey())
118119
Script [
119120
Op.GetPushOp (localHtlcSig.ToBytes())
120121
]
121-
| _ when pubKey = parameters.RemoteHtlcPubKey.RawPubKey() ->
122+
| Some pubKey when pubKey = parameters.RemoteHtlcPubKey.RawPubKey() ->
122123
let remoteHtlcSig = signer.Sign (parameters.RemoteHtlcPubKey.RawPubKey())
123124
Script [
124125
Op.GetPushOp (remoteHtlcSig.ToBytes())
@@ -144,7 +145,7 @@ type internal HTLCReceivedExtensions() =
144145
raise <| NotSupportedException()
145146

146147
override self.IsCompatibleKey(pubKey: PubKey, scriptPubKey: Script): bool =
147-
match HTLCReceivedParameters.TryExtractParameters scriptPubKey with
148+
match HtlcReceivedParameters.TryExtractParameters scriptPubKey with
148149
| None -> false
149150
| Some parameters ->
150151
parameters.LocalHtlcPubKey.RawPubKey() = pubKey

0 commit comments

Comments
 (0)