예제 #1
0
        public async Task <JToken> SendApiRequest(Request request)
        {
            if (request.Type == "identityFromPermissions" && !Paired)
            {
                return(false);
            }

            await Pair();

            if (!Paired)
            {
                throw new Exception("The user did not allow this app to connect to their Scatter");
            }

            var tcs = new TaskCompletionSource <JToken>();

            request.Id     = UtilsHelper.RandomNumber(24);
            request.Appkey = StorageProvider.GetAppkey();
            request.Nonce  = StorageProvider.GetNonce() ?? "";

            var nextNonce = UtilsHelper.RandomNumberBytes();

            request.NextNonce = UtilsHelper.ByteArrayToHexString(Sha256Manager.GetHash(nextNonce));
            StorageProvider.SetNonce(UtilsHelper.ByteArrayToHexString(nextNonce));

            OpenTasks.Add(request.Id, tcs);
            OpenTaskTimes.Add(request.Id, DateTime.Now);

            await Send("api", new { data = request, plugin = AppName });

            return(await tcs.Task);
        }
예제 #2
0
        /// <summary>
        /// Sign bytes using the signature provider
        /// </summary>
        /// <param name="chainId">EOSIO Chain id</param>
        /// <param name="requiredKeys">required public keys for signing this bytes</param>
        /// <param name="signBytes">signature bytes</param>
        /// <param name="abiNames">abi contract names to get abi information from</param>
        /// <returns>List of signatures per required keys</returns>
        public async Task <IEnumerable <string> > Sign(string chainId, IEnumerable <string> requiredKeys, byte[] signBytes, IEnumerable <string> abiNames = null)
        {
            IEnumerable <AbiRequest> abis = null;

            if (abiNames != null)
            {
                abis = abiNames.Select(a => new AbiRequest()
                {
                    account_name = a
                });
            }
            else
            {
                abis = new List <AbiRequest>();
            }

            var result = await Scatter.RequestSignature(new SignatureRequest()
            {
                network        = Scatter.GetNetwork(),
                blockchain     = ScatterConstants.Blockchains.EOSIO,
                requiredFields = new List <object>(), //TODO create concrete object for requiredFields
                transaction    = new Transaction()
                {
                    abis = abis,
                    serializedTransaction = UtilsHelper.ByteArrayToHexString(signBytes),
                    chainId = chainId
                },
                origin = Scatter.GetAppName()
            });

            return(result.signatures);
        }
예제 #3
0
        public async Task <IEnumerable <string> > Sign(string chainId, IEnumerable <string> requiredKeys, byte[] signBytes, IEnumerable <string> abiNames = null)
        {
            IEnumerable <object> abis = null;

            if (abiNames != null)
            {
                abis = abiNames.Select(a => new { account_name = a });
            }
            else
            {
                abis = new List <object>();
            }

            var result = await Scatter.RequestSignature(new
            {
                network        = Scatter.Network,
                blockchain     = Scatter.Blockchains.EOSIO,
                requiredFields = new List <object>(),
                transaction    = new
                {
                    abis,
                    serializedTransaction = UtilsHelper.ByteArrayToHexString(signBytes),
                    chainId
                },
                origin = Scatter.AppName
            });

            return(result.Signatures);
        }
        protected void HandlePairedResponse(bool?paired)
        {
            Paired = paired.GetValueOrDefault();

            if (Paired)
            {
                var storedAppKey = StorageProvider.GetAppkey();

                string hashed = storedAppKey.StartsWith("appkey:") ?
                                UtilsHelper.ByteArrayToHexString(Sha256Manager.GetHash(Encoding.UTF8.GetBytes(storedAppKey))) :
                                storedAppKey;

                if (string.IsNullOrWhiteSpace(storedAppKey) ||
                    storedAppKey != hashed)
                {
                    StorageProvider.SetAppkey(hashed);
                }
            }

            OpenTask openTask;

            if (!OpenTasks.TryGetValue(PairOpenId, out openTask))
            {
                return;
            }

            openTask.PromiseTask.SetResult(Paired);
            openTask.TaskTimeoutMS = int.MaxValue;
        }
        public ScatterUnitTests()
        {
            var storageProvider = new MemoryStorageProvider();

            storageProvider.SetAppkey(UtilsHelper.ByteArrayToHexString(Sha256Manager.GetHash(Encoding.UTF8.GetBytes("appkey:0a182c0d054b6fd9f9361c82fcd040b46c41a6f61952a3ea"))));

            var scatter = new Scatter(new ScatterConfigurator()
            {
                AppName         = "SCATTER-SHARP",
                Network         = network,
                StorageProvider = storageProvider
            });

            ScatterUnitTestCases = new ScatterUnitTestCases(scatter, network);
        }
        /// <summary>
        /// Send api request to scatter
        /// </summary>
        /// <typeparam name="TRequest">Request type param</typeparam>
        /// <typeparam name="TReturn">Return type param</typeparam>
        /// <param name="request">Request object</param>
        /// <param name="timeout">set response timeout that overrides the default one</param>
        /// <returns></returns>
        public async Task <TReturn> SendApiRequest <TRequest, TReturn>(Request <TRequest> request, int?timeout = null)
        {
            if (request.type == "identityFromPermissions" && !Paired)
            {
                return(default(TReturn));
            }

            await Pair();

            if (!Paired)
            {
                throw new Exception("The user did not allow this app to connect to their Scatter");
            }

            var tcs = new TaskCompletionSource <object>();

            do
            {
                request.id = UtilsHelper.RandomNumber(24);
            }while (OpenTasks.ContainsKey(request.id));

            request.appkey = StorageProvider.GetAppkey();
            request.nonce  = StorageProvider.GetNonce() ?? "";

            var nextNonce = UtilsHelper.RandomNumberBytes();

            request.nextNonce = UtilsHelper.ByteArrayToHexString(Sha256Manager.GetHash(nextNonce));
            StorageProvider.SetNonce(UtilsHelper.ByteArrayToHexString(nextNonce));

            OpenTasks.Add(request.id, new OpenTask()
            {
                PromiseTask     = tcs,
                TaskRequestTime = DateTime.Now,
                TaskTimeoutMS   = timeout.HasValue ? timeout.Value : TimeoutMS
            });

            await SockIO.EmitAsync("api", new RequestWrapper()
            {
                data   = request,
                plugin = AppName
            });

            return(BuildApiResponse <TReturn>(await tcs.Task));
        }
        protected void HandlePairedResponse(bool?paired)
        {
            Paired = paired.GetValueOrDefault();

            if (Paired)
            {
                var storedAppKey = StorageProvider.GetAppkey();

                string hashed = storedAppKey.StartsWith("appkey:") ?
                                UtilsHelper.ByteArrayToHexString(Sha256Manager.GetHash(Encoding.UTF8.GetBytes(storedAppKey))) :
                                storedAppKey;

                if (string.IsNullOrWhiteSpace(storedAppKey) ||
                    storedAppKey != hashed)
                {
                    StorageProvider.SetAppkey(hashed);
                }
            }

            if (PairOpenTask != null)
            {
                PairOpenTask.SetResult(Paired);
            }
        }