WalletConnect / WalletConnectSharp

A C# implementation of the WalletConnect client
Apache License 2.0
139 stars 60 forks source link

Getting "User disapproved requested chains" without user ever being prompted for the chains #174

Closed minichris closed 5 months ago

minichris commented 5 months ago

I'm developing a dApp and want to send a request to the user's wallet for them to send a transaction. I'm using WalletConnectSharp 2.2.0. I'm trying to connect with MetaMask v7.14.0 (1242) on Android.

using WalletConnectSharp.Core.Models;
using WalletConnectSharp.Core;
using WalletConnectSharp.Web3Wallet;
using WalletConnectSharp.Sign.Models;
using WalletConnectSharp.Sign.Models.Engine.Events;
using WalletConnectSharp.Sign.Models.Engine;
using WalletConnectSharp.Sign;
using WalletConnectSharp.Sign.Controllers;
using WalletConnectSharp.Network.Models;
using System.Transactions;
using Nethereum.Model;
using Newtonsoft.Json;
using WalletConnectSharp.Common.Utils;
using static System.Runtime.InteropServices.JavaScript.JSType;
using WalletConnectSharp.Storage;

internal class Program
{

    public class Transaction
    {
        [JsonProperty("from")] public string From { get; set; }

        [JsonProperty("to")] public string To { get; set; }

        [JsonProperty("gas", NullValueHandling = NullValueHandling.Ignore)]
        public string Gas { get; set; }

        [JsonProperty("gasPrice", NullValueHandling = NullValueHandling.Ignore)]
        public string GasPrice { get; set; }

        [JsonProperty("value")] public string Value { get; set; }

        [JsonProperty("data", NullValueHandling = NullValueHandling.Ignore)]
        public string Data { get; set; } = "0x";
    };

    [RpcMethod("eth_sendTransaction")]
    [RpcRequestOptions(Clock.ONE_MINUTE, 99997)]
    public class EthSendTransaction : List<Transaction>
    {
        public EthSendTransaction()
        {
        }

        public EthSendTransaction(params Transaction[] transactions) : base(transactions)
        {
        }
    }

    private static async Task Main(string[] args)
    {
        Console.WriteLine("Hello, World!");

        var dappOptions = new SignClientOptions()
        {
            ProjectId = "39f3dc0a2c604ec9885799f9fc5feb7c",
            Metadata = new Metadata()
            {
                Description = "An example dapp to showcase WalletConnectSharpv2",
                Icons = new[] { "https://walletconnect.com/meta/favicon.ico" },
                Name = "WalletConnectSharpv2 Dapp Example",
                Url = "https://walletconnect.com"
            },
            // Uncomment to disable persistant storage
            Storage = new InMemoryStorage()
        };
        var dappClient = await WalletConnectSignClient.Init(dappOptions);

        var dappConnectOptions = new ConnectOptions()
        {
            RequiredNamespaces = {{ //Changing this to OptionalNamespaces will also cause it to fail with the same error
                    "eip155", new ProposedNamespace()
                    {
                        Methods = new[]
                        {
                            "eth_sendTransaction",
                        },
                        Chains = new[]
                        {
                            "eip155:5"
                        }
                    }
                }
            }
        };

        var connectData = await dappClient.Connect(dappConnectOptions);
        Console.WriteLine(connectData.Uri);
        SessionStruct sessionData = await connectData.Approval;

        var CurrentAddress = dappClient.AddressProvider.CurrentAddress();
        Console.WriteLine(CurrentAddress.Address);

        Transaction transaction = new Transaction {
            From = CurrentAddress.Address,
            To = "0xC9571AaF9EbCa8C08EC37D962310d0Ab9F8A5Dd2",
            Value = "0",
            Data = "0x3d20db25",
            Gas = null,
            GasPrice = null
        };

        EthSendTransaction request = new EthSendTransaction(new Transaction());
        var result = await dappClient.Request<EthSendTransaction, string>(sessionData.Topic, request);

    }
}

When the code gets to sending the transaction at the end, it will throw the error "User disapproved requested chains", even though the user is never prompted to approve or disapprove the chain. This happens for both having the ProposedNamespace be a RequiredNamespace or an OptionalNamespace.

skibitsky commented 5 months ago

Hello @minichris,

There is a mistake in your code:

...

// New Transaction object with data
Transaction transaction = new Transaction {
     ....
};

// New _empty_ Transaction object used instead of the one from above
EthSendTransaction request = new EthSendTransaction(new Transaction()); 
minichris commented 5 months ago

Ah, thank you, that was it!