예제 #1
0
        public async Task <KeyValuePair <long, Task <object> > > SendResponseAsync(long id, string method, dynamic result = null, dynamic error = null)
        {
            if (!isConnected && method != "wc_sessionRequest")
            {
                await Connect();
            }
            var jsonRpc = new JsonRpc()
            {
                id = id, result = result, error = error
            };
            string ivHex         = RandomBytes(16).ToHexString();
            var    wcResponse    = WCEncrypt(jsonRpc.ToJson(), keyHex, ivHex);
            var    wcResponsePub = new WCPubSub()
            {
                topic = theirPeerId, payload = wcResponse.ToJson(), silent = true, type = "pub"
            };
            await wsClient.SendMessageAsync(wcResponsePub.ToJson());

            return(new KeyValuePair <long, Task <object> >(id, Task.FromResult((object)true)));
        }
예제 #2
0
        private void ProcessMessage(JsonRpc jsonRpc)
        {
            var       id            = jsonRpc.id;
            var       method        = jsonRpc.method;
            var       parameters    = jsonRpc.parameters;
            var       result        = jsonRpc.result;
            var       error         = jsonRpc.error;
            bool      handled       = false;
            Exception internalError = null;
            bool      hasHandler    = false;

            if (method != null)
            {
                if (eventHandler != null && eventHandler.ContainsKey(method))
                {
                    var handlers = eventHandler[method] ?? new List <Func <WCSession, JsonRpc, JsonRpc> >();
                    foreach (var handler in handlers)
                    {
                        try
                        {
                            hasHandler = true;
                            handler(this, jsonRpc);
                            handled = true;
                        }
                        catch (Exception ex)
                        {
                            internalError = ex;
                        }
                    }
                }
                if (!hasHandler && internalError != null && eventHandler.ContainsKey("_"))
                {
                    var handlers = eventHandler["_"] ?? new List <Func <WCSession, JsonRpc, JsonRpc> >();
                    foreach (var handler in handlers)
                    {
                        try
                        {
                            handler(this, jsonRpc);
                            handled = true;
                        }
                        catch (Exception ex)
                        {
                            internalError = ex;
                        }
                    }
                }
                if (!handled || internalError != null)
                {
                    dynamic errorresponse = new
                    {
                        id    = jsonRpc.id,
                        error = new
                        {
                            code    = internalError != null ? -32063 : -32601,
                            message = internalError != null ? internalError.Message : "method not found"
                        }
                    };
                    try
                    {
                        // intended run and not wait
                        if (id != null)
                        {
                            _ = SendResponseAsync(id.Value, method, error = errorresponse);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            else if (result != null || error != null)
            {
                if (id != null && outstandingRpc.ContainsKey(id.Value))
                {
                    var request         = outstandingRpc[id.Value];
                    var requestedMethod = request.Key.method;
                    var completer       = request.Value;
                    outstandingRpc.Remove(id.Value);
                    if (result != null)
                    {
                        if (requestedMethod == "wc_sessionRequest")
                        {
                            theirMeta     = result["peerMeta"];
                            theirPeerId   = result["peerId"] as string;
                            theirChainId  = result["chainId"] as int?;
                            theirRpcUrl   = result["rpcUrl"] as string;
                            theirAccounts = result["accounts"] as List <string> ?? new List <string>();
                            isActive      = (bool)result["approved"];
                            isConnected   = isActive;
                        }
                        completer.SetResult(result);
                    }
                    else
                    {
                        completer.SetException(new Exception(JsonConvert.SerializeObject(error)));
                    }
                }
            }
        }
예제 #3
0
        public async Task <KeyValuePair <long, Task <object> > > SendSessionRequestResponse(JsonRpc sessionRequest, dynamic myMeta
                                                                                            , List <string> accounts = null, bool approved = true
                                                                                            , string rpcUrl          = null, int?chainId   = null, bool ssl = true)
        {
            var wcSessionRequestResult = new {
                approved  = approved,
                accounts  = accounts ?? new List <string>(),
                rpcUrl    = rpcUrl,
                ssl       = ssl,
                networkId = chainId,
                peerId    = ourPeerId,
                peerMeta  = myMeta,
                chainId   = chainId
            };
            var response = SendResponseAsync(sessionRequest.id.Value, sessionRequest.method, wcSessionRequestResult);

            if (approved)
            {
                isConnected = true;
                isActive    = true;
                var wcSub = new WCPubSub()
                {
                    topic = ourPeerId, payload = "{}", type = "sub", silent = true
                };
                // listen on our channel for message
                await wsClient.SendMessageAsync(wcSub.ToJson());
            }
            return(await response);
        }