WalletConnect / WalletConnectSharp

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

C# Wallet SessionRequestEvents thow error #191

Closed AphobiaCat closed 3 weeks ago

AphobiaCat commented 1 month ago

using System; using WalletConnectSharp.Core; using WalletConnectSharp.Sign.Models;

using WalletConnectSharp.Sign; using WalletConnectSharp.Storage;

using Nethereum.Web3.Accounts;

using WalletConnectSharp.Common.Utils; using WalletConnectSharp.Network.Models;

[RpcMethod("eth_signTypedData_v4")] [RpcRequestOptions(Clock.ONE_MINUTE, 99999)] public class EthSignTypedDataV4 : List { public EthSignTypedDataV4(string account, string data) : base(new[] { account, data }) { }

public EthSignTypedDataV4()
{
}

}

class Program { static string projectID = "a67f7428be992afdffa43f961029112a"; static string relayUrl = "wss://relay.walletconnect.com";

static async Task Main(string[] args)
{
    string signAddress = "public key";
    string privateKey = "private key";
    string pair_str = "QRCode Here";

    var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    var walletFilePath = Path.Combine(home, ".wc", "store_wallet_example.json");
    var walletOptions = new SignClientOptions()
    {
        ProjectId = projectID,
        Metadata = new Metadata()
        {
            Description = "Test",
            Icons = new[] { "https://walletconnect.com/meta/favicon.ico" },
            Name = "Wallet",
            Url = "https://walletconnect.com"
        },
        Storage = new FileSystemStorage(walletFilePath),
    };

    WalletConnectSignClient walletClient = await WalletConnectSignClient.Init(walletOptions);

    var proposal = await walletClient.Pair(pair_str);

    var approveData = await walletClient.Approve(proposal.ApproveProposal(signAddress));

    await approveData.Acknowledged();

    Account wallet__ = new Account(privateKey);

    walletClient.Engine.SessionRequestEvents<EthSignTypedDataV4, string>()
        .OnRequest += (requestData) =>
        {
            var message = requestData.Request.Params;
            var signature = wallet__.AccountSigningService.SignTypedDataV4.SendRequestAsync(message[1], requestData.Request.Id).Result;
            requestData.Response = signature;
            return Task.CompletedTask;
        };

    Console.WriteLine("Wait Auth");
    while (true)
    {

    }
}

}

AphobiaCat commented 1 month ago

image Here is the information to fill in the check-in wallet

image This error is encountered after execution

aliarbak commented 1 month ago

Hey @AphobiaCat

walletClient.Engine.SessionRequestEvents<EthSignTypedDataV4, string>() is not valid, you should pass a response model, like this:


[JsonConverter(typeof(EthSignTypedDataV4ResponseJsonConverter))]
[RpcMethod("eth_signTypedData_v4"), RpcResponseOptions(Clock.ONE_MINUTE, 99993)]
public class EthSignTypedDataV4Response
{
    public string Signature { get; set; }

    public EthSignTypedDataV4Response(string signature)
    {
        Signature = signature;
    }

    public EthSignTypedDataV4Response()
    {
    }

    public static implicit operator string(EthSignTypedDataV4Response d) {
        return d.Signature;
    }

    public override string ToString()
    {
        return Signature;
    }
}

public class EthSignTypedDataV4ResponseJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return new EthSignTypedDataV4Response(reader.Value?.ToString());
    }
}
AphobiaCat commented 1 month ago

Oh Thanks,I'll try it now

skibitsky commented 1 month ago

Hi @AphobiaCat, were you able to resolve the issue?