Skip to content

Commit

Permalink
Fix Frontend.Console build
Browse files Browse the repository at this point in the history
  • Loading branch information
knocte committed Jun 1, 2021
1 parent f764614 commit a23c58e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
4 changes: 1 addition & 3 deletions src/GWallet.Backend.Tests.End2End/LN.fs
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,9 @@ type LN() =
let pendingChannel = UnwrapResult pendingChannelRes "OpenChannel failed"
let minimumDepth = (pendingChannel :> IChannelToBeOpened).ConfirmationsRequired
let transactionHex =
UtxoCoin.Account.SignTransactionForDestination
pendingChannel.SignTransactionForDestination
(walletInstance.Account :?> NormalUtxoAccount)
metadata
pendingChannel.FundingDestination
pendingChannel.TransferAmount
walletInstance.Password
let! acceptRes = pendingChannel.Accept transactionHex
let (channelId, _fundingTxId) = UnwrapResult acceptRes "pendingChannel.Accept failed"
Expand Down
4 changes: 1 addition & 3 deletions src/GWallet.Backend.Tests.End2End/WalletInstance.fs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,9 @@ type ClientWalletInstance private (wallet: WalletInstance, nodeClient: NodeClien
let pendingChannel = UnwrapResult pendingChannelRes "OpenChannel failed"
let minimumDepth = (pendingChannel :> IChannelToBeOpened).ConfirmationsRequired
let transactionHex =
UtxoCoin.Account.SignTransactionForDestination
pendingChannel.SignTransactionForDestination
(self.Account :?> NormalUtxoAccount)
metadata
pendingChannel.FundingDestination
pendingChannel.TransferAmount
self.Password
let! acceptRes = pendingChannel.Accept transactionHex

Expand Down
2 changes: 2 additions & 0 deletions src/GWallet.Backend/PublicKey.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ type PublicKey private (currencyOpt: Option<Currency>, pubKey: string) =
static member internal Parse (currency: Currency) (pubKey: string): PublicKey =
PublicKey (Some currency, pubKey)

member internal self.ToPubKey() =
PubKey pubKey
16 changes: 6 additions & 10 deletions src/GWallet.Backend/UtxoCoin/Lightning/ChannelManagement.fs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type ChannelInfo =
| _ -> ChannelStatus.Broken
}

module public CryptoUtil =
module internal CryptoUtil =
let internal AccountPrivateKeyToNodeMasterPrivKey (accountKey: Key): NodeMasterPrivKey =
let privateKeyBytesLength = 32
let bytes: array<byte> = Array.zeroCreate privateKeyBytesLength
Expand All @@ -137,17 +137,12 @@ module public CryptoUtil =
let hashed = NBitcoin.Crypto.Hashes.DoubleSHA256 bytes
NodeMasterPrivKey <| NBitcoin.ExtKey (hashed.ToString())

let public NodeIdAsPubKeyFromAccountPrivKey (accountPrivKey: Key): PubKey =
let nodeMasterPrivKey = AccountPrivateKeyToNodeMasterPrivKey accountPrivKey
nodeMasterPrivKey.NodeId().Value

type ChannelStore(account: IUtxoAccount) =
static member ChannelFilePrefix = "chan-"
static member ChannelFileEnding = ".json"

member val Account = account
member val Currency = (account :> IAccount).Currency
member val Network = UtxoCoin.Account.GetNetwork (account :> IAccount).Currency

member self.AccountDir: DirectoryInfo =
Config.GetConfigDir self.Currency AccountKind.Normal
Expand Down Expand Up @@ -195,7 +190,7 @@ type ChannelStore(account: IUtxoAccount) =
(password: string)
: ChannelStore =
let channelStore = ChannelStore account
let network = channelStore.Network
let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (account :> IAccount).Currency
let nodeMasterPrivKey =
ExtKey.Parse (nodeMasterPrivKeyString, network)
|> NodeMasterPrivKey
Expand All @@ -218,6 +213,7 @@ type ChannelStore(account: IUtxoAccount) =
channelStore

member internal self.GetNodeMasterPrivKey (password: string): string =
let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (self.Account :> IAccount).Currency
let nodeMasterPrivKey =
match self.Account with
| :? NormalUtxoAccount as normalAccount ->
Expand All @@ -232,15 +228,15 @@ type ChannelStore(account: IUtxoAccount) =
encryptedSecretString, publicExtKeyString
| _ -> failwith "malformed node master key file"
let encryptedSecret =
BitcoinEncryptedSecretNoEC(encryptedSecretString, self.Network)
BitcoinEncryptedSecretNoEC(encryptedSecretString, network)
let privateKey = encryptedSecret.GetKey password
let publicExtKey = ExtPubKey.Parse(publicExtKeyString, self.Network)
let publicExtKey = ExtPubKey.Parse(publicExtKeyString, network)
let privateExtKey = ExtKey(publicExtKey, privateKey)
NodeMasterPrivKey privateExtKey
| _ -> failwith "invalid account type"
nodeMasterPrivKey
.RawExtKey()
.ToString self.Network
.ToString network

member internal self.LoadChannel (channelId: ChannelIdentifier): SerializedChannel =
let fileName = self.ChannelFileName channelId
Expand Down
23 changes: 18 additions & 5 deletions src/GWallet.Backend/UtxoCoin/Lightning/Node.fs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,23 @@ type PendingChannel internal (outgoingUnfundedChannel: OutgoingUnfundedChannel)
member internal self.OutgoingUnfundedChannel = outgoingUnfundedChannel
member self.Currency: Currency =
(outgoingUnfundedChannel.ConnectedChannel.Account :> IAccount).Currency
member self.Network: Network = UtxoCoin.Account.GetNetwork self.Currency
member self.FundingDestination: IDestination = outgoingUnfundedChannel.FundingDestination
member self.FundingDestinationString: string =
(self.FundingDestination.ScriptPubKey.GetDestinationAddress self.Network).ToString()
let network = UtxoCoin.Account.GetNetwork self.Currency
(outgoingUnfundedChannel.FundingDestination.ScriptPubKey.GetDestinationAddress network).ToString()
member self.TransferAmount: TransferAmount = outgoingUnfundedChannel.TransferAmount

member self.SignTransactionForDestination (account: NormalUtxoAccount)
(txMetadata: TransactionMetadata)
(password: string)
: string =

GWallet.Backend.UtxoCoin.Account.SignTransactionForDestination
account
txMetadata
outgoingUnfundedChannel.FundingDestination
self.TransferAmount
password

member public self.Accept (fundingTransactionHex: string)
: Async<Result<ChannelIdentifier * TransactionIdentifier, IErrorMsg>> = async {
let! fundedChannelRes =
Expand Down Expand Up @@ -792,17 +803,19 @@ module public Connection =
let public StartClient (channelStore: ChannelStore)
(password: string)
: NodeClient =
let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (channelStore.Account :> IAccount).Currency
let nodeMasterPrivKey =
ExtKey.Parse(channelStore.GetNodeMasterPrivKey password, channelStore.Network)
ExtKey.Parse(channelStore.GetNodeMasterPrivKey password, network)
|> NodeMasterPrivKey
NodeClient (channelStore, nodeMasterPrivKey)

let public StartServer (channelStore: ChannelStore)
(password: string)
(bindAddress: IPEndPoint)
: NodeServer =
let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (channelStore.Account :> IAccount).Currency
let nodeMasterPrivKey =
ExtKey.Parse(channelStore.GetNodeMasterPrivKey password, channelStore.Network)
ExtKey.Parse(channelStore.GetNodeMasterPrivKey password, network)
|> NodeMasterPrivKey
let transportListener = TransportListener.Bind nodeMasterPrivKey bindAddress
new NodeServer (channelStore, transportListener)
Expand Down
4 changes: 4 additions & 0 deletions src/GWallet.Backend/UtxoCoin/Lightning/PublicHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

// the only reason this Helpers file exists is because calling these methods directly would cause the frontend to need to
// reference DotNetLightning or NBitcoin directly, so: TODO: report this bug against the F# compiler
// (this is the easiest way to reproduce: 1. checkout repo at same commit hash where the commit that adds this comment,
// 2. add `member internal val Network = UtxoCoin.Account.GetNetwork (account :> IAccount).Currency` to the type
// ChannelStore), 3. compile: see failing errors about wanting to link NBitcoin from Frontend.Console, even for an
// internal element of the API!
// (related: https://stackoverflow.com/questions/62274013/fs0074-the-type-referenced-through-c-crecord-is-defined-in-an-assembly-that-i)

open System
Expand Down
12 changes: 6 additions & 6 deletions src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type internal TransactionOutpoint =
type IUtxoAccount =
inherit IAccount

abstract member PublicKey: PubKey with get
abstract member PublicKey: PublicKey with get


type NormalUtxoAccount(currency: Currency, accountFile: FileRepresentation,
Expand All @@ -32,23 +32,23 @@ type NormalUtxoAccount(currency: Currency, accountFile: FileRepresentation,
inherit GWallet.Backend.NormalAccount(currency, accountFile, fromAccountFileToPublicAddress)

interface IUtxoAccount with
member val PublicKey = fromAccountFileToPublicKey accountFile with get
member val PublicKey = PublicKey(fromAccountFileToPublicKey accountFile) with get

type ReadOnlyUtxoAccount(currency: Currency, accountFile: FileRepresentation,
fromAccountFileToPublicAddress: FileRepresentation -> string,
fromAccountFileToPublicKey: FileRepresentation -> PubKey) =
inherit GWallet.Backend.ReadOnlyAccount(currency, accountFile, fromAccountFileToPublicAddress)

interface IUtxoAccount with
member val PublicKey = fromAccountFileToPublicKey accountFile with get
member val PublicKey = PublicKey(fromAccountFileToPublicKey accountFile) with get

type ArchivedUtxoAccount(currency: Currency, accountFile: FileRepresentation,
fromAccountFileToPublicAddress: FileRepresentation -> string,
fromAccountFileToPublicKey: FileRepresentation -> PubKey) =
inherit GWallet.Backend.ArchivedAccount(currency, accountFile, fromAccountFileToPublicAddress)

interface IUtxoAccount with
member val PublicKey = fromAccountFileToPublicKey accountFile with get
member val PublicKey = PublicKey(fromAccountFileToPublicKey accountFile) with get

module Account =

Expand Down Expand Up @@ -174,7 +174,7 @@ module Account =
let scriptPubKey = Script(scriptPubKeyInBytes)
let coin =
Coin(txHash, uint32 inputOutpointInfo.OutputIndex, Money(inputOutpointInfo.ValueInSatoshis), scriptPubKey)
coin.ToScriptCoin account.PublicKey.WitHash.ScriptPubKey :> ICoin
coin.ToScriptCoin (account.PublicKey.ToPubKey().WitHash.ScriptPubKey) :> ICoin

let private CreateTransactionAndCoinsToBeSigned (account: IUtxoAccount)
(transactionInputs: List<TransactionInputOutpointInfo>)
Expand Down Expand Up @@ -396,7 +396,7 @@ module Account =
| :? SecurityException ->
raise (InvalidPassword)

let SignTransactionForDestination (account: NormalUtxoAccount)
let internal SignTransactionForDestination (account: NormalUtxoAccount)
(txMetadata: TransactionMetadata)
(destination: IDestination)
(amount: TransferAmount)
Expand Down
4 changes: 1 addition & 3 deletions src/GWallet.Frontend.Console/LayerTwo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ module LayerTwo =
: string =
match account with
| :? UtxoCoin.NormalUtxoAccount as normalAccount ->
UtxoCoin.Account.SignTransactionForDestination
pendingChannel.SignTransactionForDestination
normalAccount
metadata
pendingChannel.FundingDestination
pendingChannel.TransferAmount
password
| :? UtxoCoin.ReadOnlyUtxoAccount ->
Console.WriteLine("To open a channel from a read-only account the paired wallet must \
Expand Down

0 comments on commit a23c58e

Please sign in to comment.