Skip to content

Commit

Permalink
Backend,Frontend.Console: rename JSON property
Browse files Browse the repository at this point in the history
This way, if a version of geewallet that is too old to have
native segwit support (or disabled by default) tries to
deserialize a transaction proposal, it will error out
complaining about version mismatch instead of crashing with a
weird error.

This commit also tweaks the version comparison, to make sure
the VersionMismatch exception is actually raised, and caught
by the Frontend (Console, for now).
  • Loading branch information
knocte committed Feb 12, 2024
1 parent 3504da4 commit edfdfa0
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/GWallet.Backend.Tests/Deserialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Deserialization() =
Assert.That(deserializedUnsignedTrans.Proposal.Amount.Currency, Is.EqualTo Currency.BTC)
Assert.That(deserializedUnsignedTrans.Proposal.DestinationAddress,
Is.EqualTo("13jxHQDxGto46QhjFiMb78dZdys9ZD8vW5"))
Assert.That(deserializedUnsignedTrans.Proposal.OriginAddress,
Assert.That(deserializedUnsignedTrans.Proposal.OriginMainAddress,
Is.EqualTo("16pKBjGGZkUXo1afyBNf5ttFvV9hauS1kR"))

let btcTxMetadata = deserializedUnsignedTrans.Metadata :?> UtxoCoin.TransactionMetadata
Expand Down Expand Up @@ -84,7 +84,7 @@ type Deserialization() =
Assert.That(deserializedUnsignedTrans.Proposal.Amount.Currency, Is.EqualTo Currency.ETC)
Assert.That(deserializedUnsignedTrans.Proposal.DestinationAddress,
Is.EqualTo("0xf3j4m0rjxdddud9403j"))
Assert.That(deserializedUnsignedTrans.Proposal.OriginAddress,
Assert.That(deserializedUnsignedTrans.Proposal.OriginMainAddress,
Is.EqualTo("0xf3j4m0rjx94sushh03j"))

let etherTxMetadata = deserializedUnsignedTrans.Metadata :?> Ether.TransactionMetadata
Expand Down Expand Up @@ -122,7 +122,7 @@ type Deserialization() =
Is.EqualTo(Currency.BTC))
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.DestinationAddress,
Is.EqualTo("13jxHQDxGto46QhjFiMb78dZdys9ZD8vW5"))
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.OriginAddress,
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.OriginMainAddress,
Is.EqualTo("16pKBjGGZkUXo1afyBNf5ttFvV9hauS1kR"))

let btcTxMetadata = deserializedSignedTrans.TransactionInfo.Metadata :?> UtxoCoin.TransactionMetadata
Expand Down Expand Up @@ -161,7 +161,7 @@ type Deserialization() =
Is.EqualTo(Currency.ETC))
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.DestinationAddress,
Is.EqualTo("0xf3j4m0rjxdddud9403j"))
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.OriginAddress,
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.OriginMainAddress,
Is.EqualTo("0xf3j4m0rjx94sushh03j"))

let etherTxMetadata = deserializedSignedTrans.TransactionInfo.Metadata :?> Ether.TransactionMetadata
Expand Down Expand Up @@ -195,7 +195,7 @@ type Deserialization() =
Assert.That(deserializedUnsignedTrans.Proposal.Amount.Currency, Is.EqualTo Currency.SAI)
Assert.That(deserializedUnsignedTrans.Proposal.DestinationAddress,
Is.EqualTo("0xDb0381B1a380d8db2724A9Ca2d33E0C6C044bE3b"))
Assert.That(deserializedUnsignedTrans.Proposal.OriginAddress,
Assert.That(deserializedUnsignedTrans.Proposal.OriginMainAddress,
Is.EqualTo("0xba766d6d13E2Cc921Bf6e896319D32502af9e37E"))

let saiTxMetadata = deserializedUnsignedTrans.Metadata :?> Ether.TransactionMetadata
Expand Down Expand Up @@ -233,7 +233,7 @@ type Deserialization() =
Is.EqualTo Currency.SAI)
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.DestinationAddress,
Is.EqualTo("0xDb0381B1a380d8db2724A9Ca2d33E0C6C044bE3b"))
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.OriginAddress,
Assert.That(deserializedSignedTrans.TransactionInfo.Proposal.OriginMainAddress,
Is.EqualTo("0xba766d6d13E2Cc921Bf6e896319D32502af9e37E"))

let etherTxMetadata = deserializedSignedTrans.TransactionInfo.Metadata :?> Ether.TransactionMetadata
Expand Down
6 changes: 3 additions & 3 deletions src/GWallet.Backend.Tests/MarshallingData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module MarshallingData =

let private someUnsignedEtherTransactionProposal =
{
OriginAddress = "0xf3j4m0rjx94sushh03j";
OriginMainAddress = "0xf3j4m0rjx94sushh03j";
Amount = TransferAmount(10.01m, 12.02m, Currency.ETC);
DestinationAddress = "0xf3j4m0rjxdddud9403j";
}
Expand Down Expand Up @@ -180,7 +180,7 @@ module MarshallingData =

let private someUnsignedBtcTransactionProposal =
{
OriginAddress = "16pKBjGGZkUXo1afyBNf5ttFvV9hauS1kR";
OriginMainAddress = "16pKBjGGZkUXo1afyBNf5ttFvV9hauS1kR";
Amount = TransferAmount(10.01m, 12.02m, Currency.BTC);
DestinationAddress = "13jxHQDxGto46QhjFiMb78dZdys9ZD8vW5";
}
Expand Down Expand Up @@ -264,7 +264,7 @@ module MarshallingData =
}
let private someUnsignedSaiTransactionProposal =
{
OriginAddress = "0xba766d6d13E2Cc921Bf6e896319D32502af9e37E";
OriginMainAddress = "0xba766d6d13E2Cc921Bf6e896319D32502af9e37E";
Amount = TransferAmount(1m, 7.08m, Currency.SAI)
DestinationAddress = "0xDb0381B1a380d8db2724A9Ca2d33E0C6C044bE3b";
}
Expand Down
4 changes: 2 additions & 2 deletions src/GWallet.Backend/Account.fs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ module Account =
else
transactionProposal.Amount.ValueToSend + fee.FeeValue
Caching.Instance.StoreOutgoingTransaction
transactionProposal.OriginAddress
transactionProposal.OriginMainAddress
transactionProposal.Amount.Currency
fee.Currency
txId
Expand Down Expand Up @@ -445,7 +445,7 @@ module Account =

let transactionProposal =
{
OriginAddress = baseAccount.PublicAddress
OriginMainAddress = baseAccount.PublicAddress
Amount = amount
DestinationAddress = destination
}
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/Ether/EtherAccount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ module internal Account =

let txDetails =
{
OriginAddress = signer.GetSenderAddress signedTransaction.RawTransaction
OriginMainAddress = signer.GetSenderAddress signedTransaction.RawTransaction
Amount = UnitConversion.Convert.FromWei (IntTypeDecoder().DecodeBigInteger tx.Value)
Currency = getTransactionCurrency tx
DestinationAddress = destAddress
Expand Down
10 changes: 4 additions & 6 deletions src/GWallet.Backend/Marshalling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,12 @@ module Marshalling =
JsonConvert.DeserializeObject<MarshallingWrapper<'T>>(json, settings)
with
| ex ->
let versionJsonTag = "\"Version\":\""
let versionJsonTag = "\"Version\":"
if (json.Contains(versionJsonTag)) then
let jsonSinceVersion = json.Substring(json.IndexOf(versionJsonTag) + versionJsonTag.Length)
let endVersionIndex = jsonSinceVersion.IndexOf("\"")
let version = jsonSinceVersion.Substring(0, endVersionIndex)
if (version <> currentVersion) then
let wrapper = ExtractWrapper json
if (wrapper.Version <> currentVersion) then
let msg = SPrintF2 "Incompatible marshalling version found (%s vs. current %s) while trying to deserialize JSON"
version currentVersion
wrapper.Version currentVersion
raise <| VersionMismatchDuringDeserializationException(msg, ex)

let targetTypeName = typeof<'T>.FullName
Expand Down
20 changes: 15 additions & 5 deletions src/GWallet.Backend/Transaction.fs
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
namespace GWallet.Backend

open Newtonsoft.Json

type ITransactionDetails =
abstract member OriginAddress: string
abstract member OriginMainAddress: string
abstract member Amount: decimal
abstract member Currency: Currency
abstract member DestinationAddress: string

type internal SignedTransactionDetails =
{
OriginAddress: string
#if !NATIVE_SEGWIT
[<JsonProperty(PropertyName = "OriginAddress")>]
#endif
OriginMainAddress: string

Amount: decimal
Currency: Currency
DestinationAddress: string
}
interface ITransactionDetails with
member self.OriginAddress = self.OriginAddress
member self.OriginMainAddress = self.OriginMainAddress
member self.Amount = self.Amount
member self.Currency = self.Currency
member self.DestinationAddress = self.DestinationAddress

type UnsignedTransactionProposal =
{
OriginAddress: string;
#if !NATIVE_SEGWIT
[<JsonProperty(PropertyName = "OriginAddress")>]
#endif
OriginMainAddress: string

Amount: TransferAmount;
DestinationAddress: string;
}
interface ITransactionDetails with
member self.OriginAddress = self.OriginAddress
member self.OriginMainAddress = self.OriginMainAddress
member self.Amount = self.Amount.ValueToSend
member self.Currency = self.Amount.Currency
member self.DestinationAddress = self.DestinationAddress
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ module Account =
failwith "expected a single destination address"

{
OriginAddress = (account :> IAccount).PublicAddress
OriginMainAddress = (account :> IAccount).PublicAddress
DestinationAddress = destinationAddress.ToString()
Amount = value.ToDecimal MoneyUnit.BTC
Currency = currency
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Frontend.Console/Presentation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module Presentation =
| NotFresh(NotAvailable) -> None

Console.WriteLine("Transaction data:")
Console.WriteLine("Sender: " + trans.OriginAddress)
Console.WriteLine("Sender: " + trans.OriginMainAddress)
Console.WriteLine("Recipient: " + trans.DestinationAddress)
let fiatAmount =
match maybeEstimatedAmountInUsd with
Expand Down
18 changes: 14 additions & 4 deletions src/GWallet.Frontend.Console/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ let SignOffPayment() =
let unsTx = Account.LoadUnsignedTransactionFromFile fileToReadFrom.FullName
let accountsWithSameAddress =
Account.GetAllActiveAccounts().Where(
fun acc -> acc.PublicAddress = unsTx.Proposal.OriginAddress
fun acc -> acc.PublicAddress = unsTx.Proposal.OriginMainAddress
)
Some (unsTx, accountsWithSameAddress)
with
Expand Down Expand Up @@ -178,6 +178,16 @@ let SignOffPayment() =
| _ ->
failwith "Account type not supported. Please report this issue."

let TryConsumeJson func =
try
func()
with
| :? VersionMismatchDuringDeserializationException ->
Console.WriteLine()
Console.Error.WriteLine "There was an error (caused by a version mismatch) when trying to consume JSON;
it is recommended to upgrade (both the hotwallet and the offline coldwallet)"
UserInteraction.PressAnyKeyToContinue()

let SendPaymentOfSpecificAmount (account: IAccount)
(amount: TransferAmount)
(transactionMetadata: IBlockchainFeeInfo)
Expand All @@ -188,7 +198,7 @@ let SendPaymentOfSpecificAmount (account: IAccount)
Console.Write("Introduce a file name to save the unsigned transaction: ")
let filePath = Console.ReadLine()
let proposal = {
OriginAddress = account.PublicAddress;
OriginMainAddress = account.PublicAddress;
Amount = amount;
DestinationAddress = destination;
}
Expand Down Expand Up @@ -393,9 +403,9 @@ let rec PerformOperation (numActiveAccounts: uint32) (numHotAccounts: uint32) =
|> Async.RunSynchronously
| None -> ()
| Operations.SignOffPayment ->
SignOffPayment()
TryConsumeJson SignOffPayment
| Operations.BroadcastPayment ->
BroadcastPayment()
TryConsumeJson BroadcastPayment
| Operations.ArchiveAccount ->
ArchiveAccount()
| Operations.PairToWatchWallet ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GWallet.Frontend.XF\GWallet.Frontend.XF.fsproj">
<Project>{2C7A1A49-CAD1-4961-866A-D32868AE7D18}</Project>
<Project>{85236682-6463-4209-B66C-E0643EF12B46}</Project>
<Name>GWallet.Frontend.XF</Name>
</ProjectReference>
<ProjectReference Include="..\GWallet.Backend\GWallet.Backend.fsproj">
Expand Down
6 changes: 3 additions & 3 deletions src/GWallet.Frontend.XF/SendPage.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ type SendPage(account: IAccount, receivePage: Page, newReceivePageFunc: unit->Pa
unsignedTransaction.Proposal.Amount.Currency account.Currency
self.DisplayAlert("Alert", err, "OK") |> FrontendHelpers.DoubleCheckCompletionNonGeneric
)
elif account.PublicAddress <> unsignedTransaction.Proposal.OriginAddress then
elif account.PublicAddress <> unsignedTransaction.Proposal.OriginMainAddress then
MainThread.BeginInvokeOnMainThread(fun _ ->
transactionEntry.TextColor <- Color.Red
let err = "Transaction proposal's sender address doesn't match with this currency's account"
Expand Down Expand Up @@ -447,7 +447,7 @@ type SendPage(account: IAccount, receivePage: Page, newReceivePageFunc: unit->Pa
signedTransaction.TransactionInfo.Proposal.Amount.Currency account.Currency
self.DisplayAlert("Alert", err, "OK") |> FrontendHelpers.DoubleCheckCompletionNonGeneric
)
elif account.PublicAddress <> signedTransaction.TransactionInfo.Proposal.OriginAddress then
elif account.PublicAddress <> signedTransaction.TransactionInfo.Proposal.OriginMainAddress then
MainThread.BeginInvokeOnMainThread(fun _ ->
transactionEntry.TextColor <- Color.Red
let err = "Transaction's sender address doesn't match with this currency's account"
Expand Down Expand Up @@ -638,7 +638,7 @@ type SendPage(account: IAccount, receivePage: Page, newReceivePageFunc: unit->Pa

| :? ReadOnlyAccount as _readOnlyAccount ->
let proposal = {
OriginAddress = account.PublicAddress;
OriginMainAddress = account.PublicAddress
Amount = txInfo.Amount;
DestinationAddress = txInfo.Destination;
}
Expand Down

0 comments on commit edfdfa0

Please sign in to comment.