Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .github/workflows/publish_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,5 @@ jobs:
run: |
cd $GITHUB_WORKSPACE/src/DotNetLightning.Core
dotnet pack . -p:Configuration=Release --version-suffix date`date +%Y%m%d-%H%M`-git-`echo $GITHUB_SHA | cut -c 1-7` -p:BouncyCastle=True
dotnet nuget push ./bin/Release/DotNetLightning.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push ./bin/Release/DotNetLightning.Kiss.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json

- name: Upload nuget packages (native)
run: |
cd $GITHUB_WORKSPACE/src/DotNetLightning.Core
dotnet pack . -p:Configuration=Release --version-suffix date`date +%Y%m%d-%H%M`-git-`echo $GITHUB_SHA | cut -c 1-7`-${{ matrix.RID }}
dotnet nuget push ./bin/Release/DotNetLightning.Core.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
6 changes: 2 additions & 4 deletions src/DotNetLightning.Core/Chain/KeysInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type ChannelKeys = {
PaymentBaseKey: Key
DelayedPaymentBaseKey: Key
HTLCBaseKey: Key
CommitmentSeed: uint256
CommitmentSeed: CommitmentSeed
}

with
Expand All @@ -65,7 +65,6 @@ type ChannelKeys = {
PaymentBasePubKey = this.PaymentBaseKey.PubKey
DelayedPaymentBasePubKey = this.DelayedPaymentBaseKey.PubKey
HTLCBasePubKey = this.HTLCBaseKey.PubKey
CommitmentSeed = this.CommitmentSeed
}

/// In usual operation we should not hold secrets on memory. So only hold pubkey
Expand All @@ -75,7 +74,6 @@ and ChannelPubKeys = {
PaymentBasePubKey: PubKey
DelayedPaymentBasePubKey: PubKey
HTLCBasePubKey: PubKey
CommitmentSeed: uint256
}


Expand Down Expand Up @@ -108,7 +106,7 @@ type DefaultKeyRepository(nodeSecret: ExtKey, channelIndex: int) =

let destinationKey = channelMasterKey.Derive(1, true).PrivateKey
let shutdownKey = channelMasterKey.Derive(2, true).PrivateKey
let commitmentSeed = channelMasterKey.Derive(3, true).PrivateKey.ToBytes() |> uint256
let commitmentSeed = channelMasterKey.Derive(3, true).PrivateKey |> CommitmentSeed

let fundingKey = channelMasterKey.Derive(4, true).PrivateKey
let fundingPubKey = fundingKey.PubKey
Expand Down
117 changes: 82 additions & 35 deletions src/DotNetLightning.Core/Channel/Channel.fs

Large diffs are not rendered by default.

48 changes: 47 additions & 1 deletion src/DotNetLightning.Core/Channel/ChannelError.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ChannelError =
// --- case they sent unacceptable msg ---
| InvalidOpenChannel of InvalidOpenChannelError
| InvalidAcceptChannel of InvalidAcceptChannelError
| InvalidMonoHopUnidirectionalPayment of InvalidMonoHopUnidirectionalPaymentError
| InvalidUpdateAddHTLC of InvalidUpdateAddHTLCError
| InvalidRevokeAndACK of InvalidRevokeAndACKError
| InvalidUpdateFee of InvalidUpdateFeeError
Expand Down Expand Up @@ -69,6 +70,7 @@ type ChannelError =
| TheyCannotAffordFee (_, _, _) -> Close
| InvalidOpenChannel _ -> DistrustPeer
| InvalidAcceptChannel _ -> DistrustPeer
| InvalidMonoHopUnidirectionalPayment _ -> Close
| InvalidUpdateAddHTLC _ -> Close
| InvalidRevokeAndACK _ -> Close
| InvalidUpdateFee _ -> Close
Expand Down Expand Up @@ -179,6 +181,18 @@ and InvalidAcceptChannelError = {
member this.Message =
String.concat "; " this.Errors

and InvalidMonoHopUnidirectionalPaymentError = {
NetworkMsg: MonoHopUnidirectionalPaymentMsg
Errors: string list
}
with
static member Create msg e = {
NetworkMsg = msg
Errors = e
}
member this.Message =
String.concat "; " this.Errors

and InvalidUpdateAddHTLCError = {
Msg: UpdateAddHTLCMsg
Errors: string list
Expand Down Expand Up @@ -507,6 +521,22 @@ module internal AcceptChannelMsgValidation =

(check1 |> Validation.ofResult) *^> check2 *^> check3 *^> check4 *^> check5 *^> check6 *^> check7

module UpdateMonoHopUnidirectionalPaymentWithContext =
let internal checkWeHaveSufficientFunds (state: Commitments) (currentSpec) =
let fees =
if state.LocalParams.IsFunder then
Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
else
Money.Zero
let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
if missing < Money.Zero then
sprintf "We don't have sufficient funds to send mono-hop unidirectional payment. current to_remote amount is: %A. Remote Channel Reserve is: %A. and fee is %A"
(currentSpec.ToRemote.ToMoney())
state.RemoteParams.ChannelReserveSatoshis
fees
|> Error
else
Ok()

module UpdateAddHTLCValidation =
let internal checkExpiryIsNotPast (current: BlockHeight) (expiry) =
Expand All @@ -521,7 +551,23 @@ module UpdateAddHTLCValidation =
let internal checkAmountIsLargerThanMinimum (htlcMinimum: LNMoney) (amount) =
check (amount) (<) (htlcMinimum) "htlc value (%A) is too small. must be greater or equal to %A"


module internal MonoHopUnidirectionalPaymentValidationWithContext =
let checkWeHaveSufficientFunds (state: Commitments) (currentSpec) =
let fees =
if state.LocalParams.IsFunder then
Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
else
Money.Zero
let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
if missing < Money.Zero then
sprintf "We don't have sufficient funds to send mono-hop unidirectional payment. current to_remote amount is: %A. Remote Channel Reserve is: %A. and fee is %A"
(currentSpec.ToRemote.ToMoney())
state.RemoteParams.ChannelReserveSatoshis
fees
|> Error
else
Ok()

module internal UpdateAddHTLCValidationWithContext =
let checkLessThanHTLCValueInFlightLimit (currentSpec: CommitmentSpec) (limit) (add: UpdateAddHTLCMsg) =
let htlcValueInFlight = currentSpec.HTLCs |> Map.toSeq |> Seq.sumBy (fun (_, v) -> v.Add.Amount)
Expand Down
6 changes: 6 additions & 0 deletions src/DotNetLightning.Core/Channel/ChannelOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ open DotNetLightning.Serialize

open NBitcoin

type OperationMonoHopUnidirectionalPayment = {
Amount: LNMoney
}

type OperationAddHTLC = {
Amount: LNMoney
PaymentHash: PaymentHash
Expand Down Expand Up @@ -197,6 +201,8 @@ type ChannelCommand =
| CreateChannelReestablish

// normal
| MonoHopUnidirectionalPayment of OperationMonoHopUnidirectionalPayment
| ApplyMonoHopUnidirectionalPayment of msg: MonoHopUnidirectionalPaymentMsg
| AddHTLC of OperationAddHTLC
| ApplyUpdateAddHTLC of msg: UpdateAddHTLCMsg * currentHeight: BlockHeight
| FulfillHTLC of OperationFulfillHTLC
Expand Down
48 changes: 46 additions & 2 deletions src/DotNetLightning.Core/Channel/ChannelTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module Data =
FundingSatoshis: Money
PushMsat: LNMoney
InitialFeeRatePerKw: FeeRatePerKw
RemoteFirstPerCommitmentPoint: PubKey
RemoteFirstPerCommitmentPoint: CommitmentPubKey
LastSent: OpenChannelMsg
}
with interface IChannelStateData
Expand All @@ -88,7 +88,7 @@ module Data =
FundingSatoshis: Money
PushMSat: LNMoney
InitialFeeRatePerKw: FeeRatePerKw
RemoteFirstPerCommitmentPoint: PubKey
RemoteFirstPerCommitmentPoint: CommitmentPubKey
ChannelFlags: uint8
LastSent: AcceptChannelMsg
}
Expand Down Expand Up @@ -264,6 +264,9 @@ type ChannelEvent =
| BothFundingLocked of nextState: Data.NormalData

// -------- normal operation ------
| WeAcceptedOperationMonoHopUnidirectionalPayment of msg: MonoHopUnidirectionalPaymentMsg * newCommitments: Commitments
| WeAcceptedMonoHopUnidirectionalPayment of newCommitments: Commitments

| WeAcceptedOperationAddHTLC of msg: UpdateAddHTLCMsg * newCommitments: Commitments
| WeAcceptedUpdateAddHTLC of newCommitments: Commitments

Expand Down Expand Up @@ -355,6 +358,26 @@ type ChannelState =
(fun v cc -> match cc with
| Normal _ -> Normal v
| _ -> cc )
member this.ChannelId: Option<ChannelId> =
match this with
| WaitForInitInternal
| WaitForOpenChannel _
| WaitForAcceptChannel _
| WaitForFundingCreated _ -> None
| WaitForFundingSigned data -> Some data.ChannelId
| WaitForFundingConfirmed data -> Some data.ChannelId
| WaitForFundingLocked data -> Some data.ChannelId
| Normal data -> Some data.ChannelId
| Shutdown data -> Some data.ChannelId
| Negotiating data -> Some data.ChannelId
| Closing data -> Some data.ChannelId
| Closed _
| Offline _
| Syncing _
| ErrFundingLost _
| ErrFundingTimeOut _
| ErrInformationLeak _ -> None

member this.Phase =
match this with
| WaitForInitInternal
Expand All @@ -374,3 +397,24 @@ type ChannelState =
| ErrFundingLost _
| ErrFundingTimeOut _
| ErrInformationLeak _ -> Abnormal

member this.Commitments: Option<Commitments> =
match this with
| WaitForInitInternal
| WaitForOpenChannel _
| WaitForAcceptChannel _
| WaitForFundingCreated _
| WaitForFundingSigned _ -> None
| WaitForFundingConfirmed data -> Some (data :> IHasCommitments).Commitments
| WaitForFundingLocked data -> Some (data :> IHasCommitments).Commitments
| Normal data -> Some (data :> IHasCommitments).Commitments
| Shutdown data -> Some (data :> IHasCommitments).Commitments
| Negotiating data -> Some (data :> IHasCommitments).Commitments
| Closing data -> Some (data :> IHasCommitments).Commitments
| Closed _
| Offline _
| Syncing _
| ErrFundingLost _
| ErrFundingTimeOut _
| ErrInformationLeak _ -> None

20 changes: 0 additions & 20 deletions src/DotNetLightning.Core/Channel/ChannelUtils.fs

This file was deleted.

27 changes: 17 additions & 10 deletions src/DotNetLightning.Core/Channel/ChannelValidation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ module internal ChannelHelpers =
(initialFeeRatePerKw: FeeRatePerKw)
(fundingOutputIndex: TxOutIndex)
(fundingTxId: TxId)
(localPerCommitmentPoint: PubKey)
(remotePerCommitmentPoint: PubKey)
(localPerCommitmentPoint: CommitmentPubKey)
(remotePerCommitmentPoint: CommitmentPubKey)
(secpContext: ISecp256k1)
(n: Network): Result<CommitmentSpec * CommitTx * CommitmentSpec * CommitTx, ChannelError> =
let toLocal = if (localParams.IsFunder) then fundingSatoshis.ToLNMoney() - pushMSat else pushMSat
Expand All @@ -95,12 +95,12 @@ module internal ChannelHelpers =
fundingTxId
fundingOutputIndex
fundingSatoshis
let revPubKeyForLocal = Generators.revocationPubKey secpContext remoteParams.RevocationBasePoint localPerCommitmentPoint
let delayedPubKeyForLocal = Generators.derivePubKey secpContext localParams.ChannelPubKeys.DelayedPaymentBasePubKey localPerCommitmentPoint
let paymentPubKeyForLocal = Generators.derivePubKey secpContext remoteParams.PaymentBasePoint localPerCommitmentPoint
let revPubKeyForLocal = Generators.revocationPubKey secpContext remoteParams.RevocationBasePoint localPerCommitmentPoint.PubKey
let delayedPubKeyForLocal = Generators.derivePubKey secpContext localParams.ChannelPubKeys.DelayedPaymentBasePubKey localPerCommitmentPoint.PubKey
let paymentPubKeyForLocal = Generators.derivePubKey secpContext remoteParams.PaymentBasePoint localPerCommitmentPoint.PubKey
let localCommitTx =
Transactions.makeCommitTx scriptCoin
0UL
CommitmentNumber.FirstCommitment
localParams.ChannelPubKeys.PaymentBasePubKey
remoteParams.PaymentBasePoint
localParams.IsFunder
Expand All @@ -113,12 +113,12 @@ module internal ChannelHelpers =
(remoteParams.HTLCBasePoint)
localSpec
n
let revPubKeyForRemote = Generators.revocationPubKey secpContext localParams.ChannelPubKeys.RevocationBasePubKey remotePerCommitmentPoint
let delayedPubKeyForRemote = Generators.derivePubKey secpContext remoteParams.DelayedPaymentBasePoint remotePerCommitmentPoint
let paymentPubKeyForRemote = Generators.derivePubKey secpContext localParams.ChannelPubKeys.PaymentBasePubKey remotePerCommitmentPoint
let revPubKeyForRemote = Generators.revocationPubKey secpContext localParams.ChannelPubKeys.RevocationBasePubKey remotePerCommitmentPoint.PubKey
let delayedPubKeyForRemote = Generators.derivePubKey secpContext remoteParams.DelayedPaymentBasePoint remotePerCommitmentPoint.PubKey
let paymentPubKeyForRemote = Generators.derivePubKey secpContext localParams.ChannelPubKeys.PaymentBasePubKey remotePerCommitmentPoint.PubKey
let remoteCommitTx =
Transactions.makeCommitTx scriptCoin
0UL
CommitmentNumber.FirstCommitment
remoteParams.PaymentBasePoint
localParams.ChannelPubKeys.PaymentBasePubKey
(not localParams.IsFunder)
Expand Down Expand Up @@ -182,6 +182,13 @@ module internal Validation =
*> AcceptChannelMsgValidation.checkConfigPermits conf.PeerChannelConfigLimits msg
|> Result.mapError(InvalidAcceptChannelError.Create msg >> InvalidAcceptChannel)

let checkOurMonoHopUnidirectionalPaymentIsAcceptableWithCurrentSpec (currentSpec) (state: Commitments) (payment: MonoHopUnidirectionalPaymentMsg) =
Validation.ofResult(MonoHopUnidirectionalPaymentValidationWithContext.checkWeHaveSufficientFunds state currentSpec)
|> Result.mapError(fun errs -> InvalidMonoHopUnidirectionalPayment { NetworkMsg = payment; Errors = errs })

let checkTheirMonoHopUnidirectionalPaymentIsAcceptableWithCurrentSpec (currentSpec) (state: Commitments) (payment: MonoHopUnidirectionalPaymentMsg) =
Validation.ofResult(MonoHopUnidirectionalPaymentValidationWithContext.checkWeHaveSufficientFunds state currentSpec)
|> Result.mapError(fun errs -> InvalidMonoHopUnidirectionalPayment { NetworkMsg = payment; Errors = errs })

let checkOperationAddHTLC (state: NormalData) (op: OperationAddHTLC) =
Validation.ofResult(UpdateAddHTLCValidation.checkExpiryIsNotPast op.CurrentHeight op.Expiry)
Expand Down
Loading