From 6d8448b172fe0cfcb9dd236079ea7273b8c82ff9 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Tue, 1 Jun 2021 22:14:28 +0800 Subject: [PATCH] Cosmetic stuff & move some code around --- src/GWallet.Backend/Account.fs | 24 ++--------------- src/GWallet.Backend/PublicKey.fs | 2 -- .../UtxoCoin/Lightning/ChannelManagement.fs | 4 +-- .../UtxoCoin/Lightning/Node.fs | 26 +++++++++++++++---- .../UtxoCoin/UtxoCoinAccount.fs | 11 ++++---- src/GWallet.Frontend.Console/LayerTwo.fs | 12 ++++----- 6 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/GWallet.Backend/Account.fs b/src/GWallet.Backend/Account.fs index 6d267e70a..f5ce1c0cb 100644 --- a/src/GWallet.Backend/Account.fs +++ b/src/GWallet.Backend/Account.fs @@ -6,7 +6,6 @@ open System.IO open System.Threading.Tasks open GWallet.Backend.FSharpUtil.UwpHacks -open DotNetLightning.Utils module Account = @@ -74,25 +73,6 @@ module Account = yield GetAccountFromFile accountFile currency kind } - let internal GetLightningNodeMasterPrivKey (password: string) - (accountFile: FileRepresentation) - (currency: Currency) - : string = - let utxoAccount = - UtxoCoin.Account.GetAccountFromFile - accountFile - currency - AccountKind.Normal - let utxoAccountPrivateKey = - UtxoCoin.Account.GetPrivateKey (utxoAccount :?> NormalAccount) password - let nodeMasterPrivKey = - UtxoCoin.Lightning.CryptoUtil.AccountPrivateKeyToNodeMasterPrivKey - utxoAccountPrivateKey - let network = UtxoCoin.Account.GetNetwork currency - nodeMasterPrivKey - .RawExtKey() - .ToString network - let GetNormalAccountsPairingInfoForWatchWallet (password: string) : Option = let allCurrencies = Currency.GetAll() @@ -108,11 +88,11 @@ module Account = AccountKind.Normal match (utxoCurrencyFile, etherCurrencyFile) with - | (Some (firstUtxoAccountFile, utxoCurrency), Some (firstEtherAccountFile, _etherCurrency)) -> + | Some (firstUtxoAccountFile, utxoCurrency), Some (firstEtherAccountFile, _etherCurrency) -> let utxoCoinPublicKey = UtxoCoin.Account.GetPublicKeyFromNormalAccountFile firstUtxoAccountFile let etherPublicAddress = Ether.Account.GetPublicAddressFromNormalAccountFile firstEtherAccountFile let lightningNodeMasterPrivKey = - GetLightningNodeMasterPrivKey + UtxoCoin.Lightning.Node.GetMasterPrivateKey password firstUtxoAccountFile utxoCurrency diff --git a/src/GWallet.Backend/PublicKey.fs b/src/GWallet.Backend/PublicKey.fs index 135bba7ee..9756a2160 100644 --- a/src/GWallet.Backend/PublicKey.fs +++ b/src/GWallet.Backend/PublicKey.fs @@ -22,5 +22,3 @@ type PublicKey private (currencyOpt: Option, pubKey: string) = static member internal Parse (currency: Currency) (pubKey: string): PublicKey = PublicKey (Some currency, pubKey) - member internal self.ToPubKey() = - PubKey pubKey diff --git a/src/GWallet.Backend/UtxoCoin/Lightning/ChannelManagement.fs b/src/GWallet.Backend/UtxoCoin/Lightning/ChannelManagement.fs index 8ab73d20a..0ddcf90d8 100644 --- a/src/GWallet.Backend/UtxoCoin/Lightning/ChannelManagement.fs +++ b/src/GWallet.Backend/UtxoCoin/Lightning/ChannelManagement.fs @@ -190,7 +190,7 @@ type ChannelStore(account: IUtxoAccount) = (password: string) : ChannelStore = let channelStore = ChannelStore account - let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (account :> IAccount).Currency + let network = Account.GetNetwork (account :> IAccount).Currency let nodeMasterPrivKey = ExtKey.Parse (nodeMasterPrivKeyString, network) |> NodeMasterPrivKey @@ -213,7 +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 network = Account.GetNetwork (self.Account :> IAccount).Currency let nodeMasterPrivKey = match self.Account with | :? NormalUtxoAccount as normalAccount -> diff --git a/src/GWallet.Backend/UtxoCoin/Lightning/Node.fs b/src/GWallet.Backend/UtxoCoin/Lightning/Node.fs index 98ac1e22d..32205eb0b 100644 --- a/src/GWallet.Backend/UtxoCoin/Lightning/Node.fs +++ b/src/GWallet.Backend/UtxoCoin/Lightning/Node.fs @@ -166,19 +166,19 @@ type IncomingChannelEvent = type PendingChannel internal (outgoingUnfundedChannel: OutgoingUnfundedChannel) = member internal self.OutgoingUnfundedChannel = outgoingUnfundedChannel - member self.Currency: Currency = + member __.Currency: Currency = (outgoingUnfundedChannel.ConnectedChannel.Account :> IAccount).Currency member self.FundingDestinationString: string = let network = UtxoCoin.Account.GetNetwork self.Currency (outgoingUnfundedChannel.FundingDestination.ScriptPubKey.GetDestinationAddress network).ToString() - member self.TransferAmount: TransferAmount = outgoingUnfundedChannel.TransferAmount + member __.TransferAmount: TransferAmount = outgoingUnfundedChannel.TransferAmount member self.SignTransactionForDestination (account: NormalUtxoAccount) (txMetadata: TransactionMetadata) (password: string) : string = - GWallet.Backend.UtxoCoin.Account.SignTransactionForDestination + Account.SignTransactionForDestination account txMetadata outgoingUnfundedChannel.FundingDestination @@ -799,11 +799,27 @@ type Node = ChainWatcher.CheckForChannelFraudAndSendRevocationTx channelId self.ChannelStore + static member internal GetMasterPrivateKey (password: string) + (accountFile: FileRepresentation) + (currency: Currency) + : string = + let utxoAccount = Account.GetAccountFromFile accountFile currency AccountKind.Normal + let utxoAccountPrivateKey = Account.GetPrivateKey (utxoAccount :?> NormalAccount) password + let nodeMasterPrivKey = + UtxoCoin.Lightning.CryptoUtil.AccountPrivateKeyToNodeMasterPrivKey + utxoAccountPrivateKey + let network = UtxoCoin.Account.GetNetwork currency + nodeMasterPrivKey + .RawExtKey() + .ToString network + + module public Connection = + let public StartClient (channelStore: ChannelStore) (password: string) : NodeClient = - let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (channelStore.Account :> IAccount).Currency + let network = Account.GetNetwork (channelStore.Account :> IAccount).Currency let nodeMasterPrivKey = ExtKey.Parse(channelStore.GetNodeMasterPrivKey password, network) |> NodeMasterPrivKey @@ -813,7 +829,7 @@ module public Connection = (password: string) (bindAddress: IPEndPoint) : NodeServer = - let network = GWallet.Backend.UtxoCoin.Account.GetNetwork (channelStore.Account :> IAccount).Currency + let network = Account.GetNetwork (channelStore.Account :> IAccount).Currency let nodeMasterPrivKey = ExtKey.Parse(channelStore.GetNodeMasterPrivKey password, network) |> NodeMasterPrivKey diff --git a/src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs b/src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs index 2f5d03008..164dc1525 100644 --- a/src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs +++ b/src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs @@ -174,7 +174,8 @@ module Account = let scriptPubKey = Script(scriptPubKeyInBytes) let coin = Coin(txHash, uint32 inputOutpointInfo.OutputIndex, Money(inputOutpointInfo.ValueInSatoshis), scriptPubKey) - coin.ToScriptCoin (account.PublicKey.ToPubKey().WitHash.ScriptPubKey) :> ICoin + let pubKey = PubKey (account.PublicKey.ToString ()) + coin.ToScriptCoin (pubKey.WitHash.ScriptPubKey) :> ICoin let private CreateTransactionAndCoinsToBeSigned (account: IUtxoAccount) (transactionInputs: List) @@ -397,10 +398,10 @@ module Account = raise (InvalidPassword) let internal SignTransactionForDestination (account: NormalUtxoAccount) - (txMetadata: TransactionMetadata) - (destination: IDestination) - (amount: TransferAmount) - (password: string) = + (txMetadata: TransactionMetadata) + (destination: IDestination) + (amount: TransferAmount) + (password: string) = let privateKey = GetPrivateKey account password diff --git a/src/GWallet.Frontend.Console/LayerTwo.fs b/src/GWallet.Frontend.Console/LayerTwo.fs index 4de105dbc..b64b505a6 100644 --- a/src/GWallet.Frontend.Console/LayerTwo.fs +++ b/src/GWallet.Frontend.Console/LayerTwo.fs @@ -125,9 +125,8 @@ module LayerTwo = metadata password | :? UtxoCoin.ReadOnlyUtxoAccount -> - Console.WriteLine("To open a channel from a read-only account the paired wallet must \ - sign the funding transaction") - Console.Write("Introduce a file name to save the unsigned transaction: ") + Console.WriteLine "To open a channel from a read-only account the paired wallet must sign the funding transaction." + Console.Write "Introduce a file name to save the unsigned transaction: " let unsignedFilePath = Console.ReadLine() let proposal = { OriginAddress = account.PublicAddress @@ -135,14 +134,13 @@ module LayerTwo = DestinationAddress = pendingChannel.FundingDestinationString } Account.SaveUnsignedTransaction proposal metadata unsignedFilePath - Console.WriteLine("Transaction saved. Now copy it to the device with the private \ - key to sign it.") + Console.WriteLine "Transaction saved. Now copy it to the device with the private key to sign it." let rec getSignedTransaction() = let signedFilePath = - UserInteraction.AskFileNameToLoad("Enter file name to load the signed transaction: ") + UserInteraction.AskFileNameToLoad "Enter file name to load the signed transaction: " let signedTransaction = Account.LoadSignedTransactionFromFile signedFilePath.FullName if signedTransaction.TransactionInfo.Proposal.DestinationAddress <> proposal.DestinationAddress then - Console.WriteLine("Transaction data does not match. Incorrect file?") + Console.WriteLine "Transaction data does not match. Incorrect file?" getSignedTransaction() else signedTransaction.RawTransaction