WalletConnect / WalletConnectSharp

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

Cannot connect to a Dapp, always throws error #166

Closed tperalta82 closed 3 weeks ago

tperalta82 commented 6 months ago

Hey, i'm developing a Wallet and trying to use the Sign API to connect to a Dapp, sample code is as follows:

    SignClientOptions so = 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"
        },

        Storage = new InMemoryStorage()
    };

    var walletClient = await WalletConnectSignClient.Init(so);
    Console.WriteLine("Enter WC Dapp URI");
    var URI = Console.ReadLine();
    ProposalStruct proposal = await walletClient.Pair(URI);
    var walletAddress = "0xsssssss"; // wallet is incorrect here but just for the issue
    var approveData = await walletClient.Approve(proposal, walletAddress);
    await approveData.Acknowledged();
    var sessionData = approveData.Topic;

The walletClient.Approve is always throwing:

System.ArgumentException: 'An item with the same key has already been added. Key: eip155'

tried versions from 2.0.5 to 2.1.6, followed the docs to try and implement it, tried to use uniswap.

Any ideas?

skibitsky commented 5 months ago

Hello,

Sorry for the delayed response. You need to use Web3WalletClient. Here is the working code:

var options = new CoreOptions()
{
    ProjectId = "39f3dc0a2c604ec9885799f9fc5feb7c",
    Name = "My Wallet",
};

var core = new WalletConnectCore(options);

var metadata = new Metadata()
{
    Description = "An example wallet to showcase WalletConnectSharpv2",
    Icons = new[] { "https://walletconnect.com/meta/favicon.ico" },
    Name = $"wallet-csharp-test",
    Url = "https://walletconnect.com",
};

var walletClient = await Web3WalletClient.Init(core, metadata, metadata.Name);

walletClient.SessionProposed += async (sender, session) =>
{
    var proposal = session.Proposal;
    var requiredNamespaces = proposal.RequiredNamespaces;

    var approvedNamespaces = new Namespaces(requiredNamespaces);
    approvedNamespaces["eip155"].Accounts = new []{("eip155:1:0xab16.............")};

    var sessionData = await walletClient.ApproveSession(proposal.Id, approvedNamespaces);
    var sessionTopic = sessionData.Topic;

    Console.WriteLine("Session Data: " + sessionTopic);
};

Console.WriteLine("Enter WC Dapp URI");
var URI = Console.ReadLine();
await walletClient.Pair(URI);

// So that console doesn't close
await Task.Delay(TimeSpan.FromMinutes(5));

This approves only the required namespaces. Uniswap includes Ethereum in the required namespaces and all other networks in the optional namespaces. You would need to add more chains to approvedNamespaces to enable them.

Please refer to the Web3Wallet documentation and unit tests for more code samples.