WalletConnectSharp icon indicating copy to clipboard operation
WalletConnectSharp copied to clipboard

C# Wallet SessionRequestEvents thow error

Open AphobiaCat opened this issue 1 year ago • 3 comments

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 avatar May 07 '24 08:05 AphobiaCat

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

image This error is encountered after execution

AphobiaCat avatar May 07 '24 09:05 AphobiaCat

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());
    }
}

aliarbak avatar May 07 '24 09:05 aliarbak

Oh Thanks,I'll try it now

AphobiaCat avatar May 07 '24 09:05 AphobiaCat

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

skibitsky avatar May 16 '24 09:05 skibitsky