Skip to content

Commit

Permalink
Backend: update Nethereum to latest version
Browse files Browse the repository at this point in the history
This commit updates Nethereum to latest version to support
extracting token transfer amount from the raw tx created when
sending (hot-storage) or signing (cold-storage).
  • Loading branch information
aarani committed Oct 9, 2023
1 parent ae83bae commit 71fd73d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 45 deletions.
12 changes: 11 additions & 1 deletion src/GWallet.Backend.Tests/GWallet.Backend.Tests-legacy.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<HintPath>..\..\packages\NBitcoin.Altcoins.3.0.8\lib\net452\NBitcoin.Altcoins.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions">
<HintPath>..\..\packages\Microsoft.Extensions.Logging.Abstractions.1.0.2\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Extensions.Logging.Abstractions.6.0.0\lib\net461\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
<Reference Include="NBitcoin">
<HintPath>..\..\packages\NBitcoin.6.0.17\lib\net461\NBitcoin.dll</HintPath>
Expand All @@ -152,10 +152,20 @@
<Reference Include="System.Runtime.Extensions">
<HintPath>..\..\packages\System.Runtime.Extensions.4.3.0\lib\net462\System.Runtime.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Numerics.Vectors">
<HintPath>..\..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe">
<HintPath>..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Memory">
<HintPath>..\..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
</ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="AfterBuild">
</Target>
-->
<Import Project="..\..\packages\Microsoft.Extensions.Logging.Abstractions.6.0.0\build\Microsoft.Extensions.Logging.Abstractions.targets" Condition="Exists('..\..\packages\Microsoft.Extensions.Logging.Abstractions.6.0.0\build\Microsoft.Extensions.Logging.Abstractions.targets')" />
</Project>
5 changes: 4 additions & 1 deletion src/GWallet.Backend.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="FSharp.Core" version="4.7.0" targetFramework="net461" />
<package id="Fsdk" version="0.6.0--date20230530-1155.git-3bb8d08" targetFramework="net46" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="1.0.2" targetFramework="net46" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="6.0.0" targetFramework="net471" />
<package id="NBitcoin" version="6.0.17" targetFramework="net471" />
<package id="NBitcoin.Altcoins" version="3.0.8" targetFramework="net452" />
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net46" />
Expand All @@ -15,10 +15,13 @@
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net471" />
<package id="System.Globalization" version="4.3.0" targetFramework="net471" />
<package id="System.Linq" version="4.3.0" targetFramework="net471" />
<package id="System.Memory" version="4.5.4" targetFramework="net471" />
<package id="System.Net.Requests" version="4.3.0" targetFramework="net46" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net471" />
<package id="System.Reflection" version="4.3.0" targetFramework="net471" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net471" />
<package id="System.Runtime" version="4.3.0" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net471" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net471" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net471" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net46" />
Expand Down
16 changes: 8 additions & 8 deletions src/GWallet.Backend/Ether/EtherAccount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open System.Numerics
open System.Threading.Tasks

open Nethereum.ABI.Decoders
open Nethereum.Model
open Nethereum.Signer
open Nethereum.KeyStore
open Nethereum.Util
Expand All @@ -20,7 +21,7 @@ open GWallet.Backend.FSharpUtil.UwpHacks
module internal Account =

let private addressUtil = AddressUtil()
let private signer = TransactionSigner()
let private signer = LegacyTransactionSigner()

let private KeyStoreService = KeyStoreService()

Expand Down Expand Up @@ -332,8 +333,7 @@ module internal Account =
else
failwith <| SPrintF1 "Assertion failed: Ether currency %A not supported?" account.Currency

let chain = GetNetwork account.Currency
if not (signer.VerifyTransaction(trans, chain)) then
if not (TransactionVerificationAndRecovery.VerifyTransaction trans) then
failwith "Transaction could not be verified?"
trans

Expand Down Expand Up @@ -426,7 +426,7 @@ module internal Account =
signedTransaction.TransactionInfo.Proposal :> ITransactionDetails

| _ ->
let getTransactionChainId (tx: TransactionBase) =
let getTransactionChainId (tx: SignedLegacyTransactionBase) =
// the chain id can be deconstructed like so -
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
// into one of the following -
Expand All @@ -437,22 +437,22 @@ module internal Account =
let chainId = (v - BigInteger 35) / BigInteger 2
chainId

let getTransactionCurrency (tx: TransactionBase) =
let getTransactionCurrency (tx: SignedLegacyTransactionBase) =
match int (getTransactionChainId tx) with
| chainId when chainId = int Config.EthNet -> ETH
| chainId when chainId = int Config.EtcNet -> ETC
| other -> failwith <| SPrintF1 "Could not infer currency from transaction where chainId = %i." other

let tx = TransactionFactory.CreateTransaction signedTransaction.RawTransaction
let tx = TransactionFactory.CreateTransaction signedTransaction.RawTransaction :?> SignedLegacyTransaction

// HACK: I prefix 12 elements to the address due to AddressTypeDecoder expecting some sort of header...
let address = AddressTypeDecoder().Decode (Array.append (Array.zeroCreate 12) tx.ReceiveAddress)

let destAddress = addressUtil.ConvertToChecksumAddress address
let destAddress = address.ConvertToEthereumChecksumAddress()

let txDetails =
{
OriginAddress = signer.GetSenderAddress signedTransaction.RawTransaction
OriginAddress = TransactionVerificationAndRecovery.GetSenderAddress signedTransaction.RawTransaction
Amount = UnitConversion.Convert.FromWei (IntTypeDecoder().DecodeBigInteger tx.Value)
Currency = getTransactionCurrency tx
DestinationAddress = destAddress
Expand Down
32 changes: 13 additions & 19 deletions src/GWallet.Backend/Ether/EtherServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open System.Linq

open Nethereum.Util
open Nethereum.Hex.HexTypes
open Nethereum.JsonRpc.Client
open Nethereum.Web3
open Nethereum.RPC.Eth.DTOs
open Nethereum.StandardTokenEIP20.ContractDefinition
Expand All @@ -20,8 +21,8 @@ type BalanceType =
| Unconfirmed
| Confirmed

type SomeWeb3 (connectionTimeOut, url: string) =
inherit Web3 (connectionTimeOut, url)
type SomeWeb3 (url: string) =
inherit Web3 (url)

member val Url = url with get

Expand Down Expand Up @@ -67,7 +68,7 @@ module Web3ServerSeedList =

module Server =

let private Web3Server (connectionTimeOut, serverDetails: ServerDetails) =
let private Web3Server (serverDetails: ServerDetails) =
match serverDetails.ServerInfo.ConnectionType with
| { Protocol = Tcp _ ; Encrypted = _ } ->
failwith <| SPrintF1 "Ether server of TCP connection type?: %s" serverDetails.ServerInfo.NetworkPath
Expand All @@ -78,7 +79,7 @@ module Server =
else
"http"
let uri = SPrintF2 "%s://%s" protocol serverDetails.ServerInfo.NetworkPath
SomeWeb3 (connectionTimeOut, uri)
SomeWeb3 uri

let HttpRequestExceptionMatchesErrorCode (ex: Http.HttpRequestException) (errorCode: int): bool =
ex.Message.StartsWith(SPrintF1 "%i " errorCode) || ex.Message.Contains(SPrintF1 " %i " errorCode)
Expand Down Expand Up @@ -192,7 +193,7 @@ module Server =
]

let MaybeRethrowRpcResponseException (ex: Exception): unit =
let maybeRpcResponseEx = FSharpUtil.FindException<JsonRpcSharp.Client.RpcResponseException> ex
let maybeRpcResponseEx = FSharpUtil.FindException<RpcResponseException> ex
match maybeRpcResponseEx with
| Some rpcResponseEx ->
if not (isNull rpcResponseEx.RpcError) then
Expand Down Expand Up @@ -235,7 +236,7 @@ module Server =

let MaybeRethrowRpcClientTimeoutException (ex: Exception): unit =
let maybeRpcTimeoutException =
FSharpUtil.FindException<JsonRpcSharp.Client.RpcClientTimeoutException> ex
FSharpUtil.FindException<RpcClientTimeoutException> ex
match maybeRpcTimeoutException with
| Some rpcTimeoutEx ->
raise <| ServerTimedOutException(exMsg, rpcTimeoutEx)
Expand All @@ -252,7 +253,7 @@ module Server =

// this could be a Xamarin.Android bug (see https://gitlab.com/nblockchain/geewallet/issues/119)
let MaybeRethrowObjectDisposedException (ex: Exception): unit =
let maybeRpcUnknownEx = FSharpUtil.FindException<JsonRpcSharp.Client.RpcClientUnknownException> ex
let maybeRpcUnknownEx = FSharpUtil.FindException<RpcClientUnknownException> ex
match maybeRpcUnknownEx with
| Some _ ->
let maybeObjectDisposedEx = FSharpUtil.FindException<ObjectDisposedException> ex
Expand All @@ -266,12 +267,12 @@ module Server =
()

let MaybeRethrowInnerRpcException (ex: Exception): unit =
let maybeRpcUnknownEx = FSharpUtil.FindException<JsonRpcSharp.Client.RpcClientUnknownException> ex
let maybeRpcUnknownEx = FSharpUtil.FindException<RpcClientUnknownException> ex
match maybeRpcUnknownEx with
| Some rpcUnknownEx ->

let maybeDeSerializationEx =
FSharpUtil.FindException<JsonRpcSharp.Client.DeserializationException> rpcUnknownEx
FSharpUtil.FindException<DeserializationException> rpcUnknownEx
match maybeDeSerializationEx with
| None ->
()
Expand Down Expand Up @@ -389,7 +390,7 @@ module Server =

let Web3ServerToRetrievalFunc (server: ServerDetails)
(web3ClientFunc: SomeWeb3->Async<'R>)
currency
_currency
: Async<'R> =

let HandlePossibleEtherFailures (job: Async<'R>): Async<'R> =
Expand All @@ -404,15 +405,8 @@ module Server =
return raise <| FSharpUtil.ReRaise ex
}

let connectionTimeout =
match currency with
| Currency.ETC when etcEcosystemIsMomentarilyCentralized ->
Config.DEFAULT_NETWORK_TIMEOUT + Config.DEFAULT_NETWORK_TIMEOUT
| _ ->
Config.DEFAULT_NETWORK_TIMEOUT

async {
let web3Server = Web3Server (connectionTimeout, server)
let web3Server = Web3Server (server)
try
return! HandlePossibleEtherFailures (web3ClientFunc web3Server)

Expand Down Expand Up @@ -694,7 +688,7 @@ module Server =
web3Funcs
with
| ex ->
match FSharpUtil.FindException<JsonRpcSharp.Client.RpcResponseException> ex with
match FSharpUtil.FindException<RpcResponseException> ex with
| None ->
return raise (FSharpUtil.ReRaise ex)
| Some rpcResponseException ->
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/Ether/TokenManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ module TokenManager =

// this is a dummy instance we need in order to pass it to base class of StandardTokenService, but not
// really used online; FIXME: propose "Web3-less" overload to Nethereum
let private dummyOfflineWeb3 = Web3 Config.DEFAULT_NETWORK_TIMEOUT
let private dummyOfflineWeb3 = Web3()
type OfflineTokenServiceWrapper(currency: Currency) =
inherit TokenServiceWrapper(dummyOfflineWeb3, currency)
46 changes: 34 additions & 12 deletions src/GWallet.Backend/GWallet.Backend-legacy.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
<HintPath>..\..\packages\Xamarin.Essentials.1.0.0\lib\netstandard1.0\Xamarin.Essentials.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions">
<HintPath>..\..\packages\Microsoft.Extensions.Logging.Abstractions.1.0.2\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Extensions.Logging.Abstractions.6.0.0\lib\net461\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
<Reference Include="JsonRpcSharp.TcpClient">
<HintPath>..\..\packages\JsonRpcSharp.0.98.0--date20230731-1252.git-6788e32\lib\netstandard2.0\JsonRpcSharp.TcpClient.dll</HintPath>
Expand All @@ -224,38 +224,59 @@
<Reference Include="JsonRpcSharp.WebSocketClient">
<HintPath>..\..\packages\JsonRpcSharp.0.98.0--date20230731-1252.git-6788e32\lib\netstandard2.0\JsonRpcSharp.WebSocketClient.dll</HintPath>
</Reference>
<Reference Include="ADRaffy.ENSNormalize">
<HintPath>..\..\packages\ADRaffy.ENSNormalize.0.1.5\lib\net461\ADRaffy.ENSNormalize.dll</HintPath>
</Reference>
<Reference Include="Nethereum.BlockchainProcessing">
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.BlockchainProcessing.dll</HintPath>
</Reference>
<Reference Include="Nethereum.JsonRpc.Client">
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.JsonRpc.Client.dll</HintPath>
</Reference>
<Reference Include="Nethereum.JsonRpc.RpcClient">
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.JsonRpc.RpcClient.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Merkle.Patricia">
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Merkle.Patricia.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Model">
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Model.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Signer.EIP712">
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Signer.EIP712.dll</HintPath>
</Reference>
<Reference Include="Nethereum.ABI">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.ABI.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.ABI.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Accounts">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.Accounts.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Accounts.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Contracts">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.Contracts.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Contracts.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Hex">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.Hex.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Hex.dll</HintPath>
</Reference>
<Reference Include="Nethereum.KeyStore">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.KeyStore.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.KeyStore.dll</HintPath>
</Reference>
<Reference Include="Nethereum.RLP">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.RLP.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.RLP.dll</HintPath>
</Reference>
<Reference Include="Nethereum.RPC">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.RPC.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.RPC.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Signer">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.Signer.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Signer.dll</HintPath>
</Reference>
<Reference Include="Nethereum.StandardTokenEIP20">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.StandardTokenEIP20.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.StandardTokenEIP20.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Util">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.Util.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Util.dll</HintPath>
</Reference>
<Reference Include="Nethereum.Web3">
<HintPath>..\..\packages\Nethereum.0.95.0--date20210125-0551.git-05a29e9\lib\netstandard2.0\Nethereum.Web3.dll</HintPath>
<HintPath>..\..\packages\Nethereum.3.99.0--date20231003-1147.git-b9a16cf\lib\netstandard2.0\Nethereum.Web3.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack">
<HintPath>..\..\packages\HtmlAgilityPack.1.11.24\lib\Net45\HtmlAgilityPack.dll</HintPath>
Expand All @@ -270,4 +291,5 @@
</Target>
-->
<Import Project="..\..\packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('..\..\packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets')" />
<Import Project="..\..\packages\Microsoft.Extensions.Logging.Abstractions.6.0.0\build\Microsoft.Extensions.Logging.Abstractions.targets" Condition="Exists('..\..\packages\Microsoft.Extensions.Logging.Abstractions.6.0.0\build\Microsoft.Extensions.Logging.Abstractions.targets')" />
</Project>
5 changes: 4 additions & 1 deletion src/GWallet.Backend/GWallet.Backend.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@
<PackageReference Include="NBitcoin.Altcoins" Version="3.0.8">
<GeneratePathProperty></GeneratePathProperty>
</PackageReference>
<PackageReference Include="Nethereum" Version="0.95.0--date20210125-0551.git-05a29e9">
<PackageReference Include="Nethereum" Version="3.99.0--date20231003-1147.git-b9a16cf">
<GeneratePathProperty></GeneratePathProperty>
</PackageReference>
<PackageReference Include="JsonRpcSharp" Version="0.98.0--date20230731-1252.git-6788e32">
<GeneratePathProperty></GeneratePathProperty>
</PackageReference>
<PackageReference Include="SharpRaven" Version="2.4.0">
Expand Down
Loading

0 comments on commit 71fd73d

Please sign in to comment.