Exemple #1
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);
        }
Exemple #2
0
        public async Task <KeyValuePair <long, Task <object> > > SendRequestAsync(string method, List <object> parameters, string peerId = null, int timeoutSec = 0)
        {
            if (!isConnected && method != "wc_sessionRequest")
            {
                await Connect();
            }
            long id      = DateTime.UtcNow.ToUnixTime() * 1000; // in ms
            var  jsonRpc = new JsonRpc()
            {
                id = id, method = method, parameters = parameters
            };
            string ivHex     = RandomBytes(16).ToHexString();
            var    wcRequest = WCEncrypt(jsonRpc.ToJson(), keyHex, ivHex);

            WCDecrypt(wcRequest.data, keyHex, wcRequest.iv, wcRequest.hmac);
            var wcRequestPub = new WCPubSub()
            {
                topic = peerId ?? theirPeerId, payload = wcRequest.ToJson(), silent = true, type = "pub"
            };
            var completer = new TaskCompletionSource <dynamic>();

            if (method != "wc_sessionUpdate")
            {
                outstandingRpc[id] = new KeyValuePair <JsonRpc, TaskCompletionSource <dynamic> >(jsonRpc, completer);
            }
            await wsClient.SendMessageAsync(wcRequestPub.ToJson());

            var requestResult = completer.Task.TimeoutAfter(timeoutSec);

            return(new KeyValuePair <long, Task <object> >(id, requestResult));
        }
Exemple #3
0
        public static JsonRpc DecodePubSubMessage(string message, string keyHex)
        {
            var pubsub    = WCPubSub.FromString(message);
            var payload   = WCPayload.FromString(pubsub.payload);
            var wcRequest = WCDecrypt(payload.data, keyHex, payload.iv, payload.hmac);
            var jsonRpc   = JsonRpc.FromString(wcRequest);

            return(jsonRpc);
        }
Exemple #4
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)));
        }
Exemple #5
0
        public async Task <WCSession> Connect(string topic = null, Action <WCSession, JsonRpc> sessionRequestHandler = null)
        {
            try
            {
                var wsUrl        = new Regex(@"^http").Replace(bridgeUrl, "ws");
                var subTopic     = topic ?? ourPeerId;
                var wcSessionSub = new WCPubSub()
                {
                    topic = subTopic, payload = "{}", type = "sub", silent = true
                };
                wsClient = new WsClient();
                await wsClient.ConnectAsync(wsUrl);

                StartListener(sessionRequestHandler);
                // listen on our channel for message
                await wsClient.SendMessageAsync(wcSessionSub.ToJson());

                return(this);
            }
            catch
            {
                throw;
            }
        }