コード例 #1
0
        /// <summary>
        /// Get the KMS Keyvalue
        /// </summary>
        /// <param name="keyName">Key Name</param>
        /// <returns>Persisted Value</returns>
        private async Task <string> GetKMSKeyValue(string keyName)
        {
            Web3 web3    = GetWeb3();
            var  account = new Nethereum.Web3.Accounts.Account(privateKey);

            /** Function: getItem**/
            var getItemRequest = new GetItemFunction
            {
                Key         = keyName,
                FromAddress = account.Address,
                GasPrice    = Web3.Convert.ToWei(25, UnitConversion.EthUnit.Gwei)
            };

            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

            var keyQueryHandler = web3.Eth.GetContractQueryHandler <GetItemFunction>();

            var getItemResponse = await keyQueryHandler
                                  .QueryDeserializingToObjectAsync <GetItemOutputDTO>(
                getItemRequest, txtKMSContractAddress.Text.Trim());

            if (getItemResponse != null)
            {
                return(getItemResponse.ReturnValue1);
            }

            return(string.Empty);
        }
コード例 #2
0
        public Task <GetItemOutputDTO> GetItemQueryAsync(uint id, BlockParameter blockParameter = null)
        {
            var getItemFunction = new GetItemFunction();

            getItemFunction.Id = id;

            return(ContractHandler.QueryDeserializingToObjectAsync <GetItemFunction, GetItemOutputDTO>(getItemFunction, blockParameter));
        }
コード例 #3
0
        public async Task <Asset> GetItem(int index)
        {
            var web3 = _web3ProviderService.GetWeb3();

            var getItemHandler = web3.Eth.GetContractQueryHandler <GetItemFunction>();

            var getItem = new GetItemFunction
            {
                Index = index
            };

            var item = await getItemHandler.QueryDeserializingToObjectAsync <Asset>(getItem, ContractAddress);

            return(item);
        }
コード例 #4
0
        public GetItemFunctionTests()
        {
            _mockRepository = new Mock <IItemRepository>();
            _mockRepository.Setup(_ => _.GetById <Item>(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(new List <Item> {
                new Item {
                    Id = Guid.NewGuid().ToString()
                }
            });

            var serviceCollection = Startup.BuildContainer();

            serviceCollection.Replace(new ServiceDescriptor(typeof(IItemRepository), _ => _mockRepository.Object,
                                                            ServiceLifetime.Transient));

            _sut = new GetItemFunction(serviceCollection.BuildServiceProvider());
        }
コード例 #5
0
 public Task <GetItemOutputDTO> GetItemQueryAsync(GetItemFunction getItemFunction, BlockParameter blockParameter = null)
 {
     return(ContractHandler.QueryDeserializingToObjectAsync <GetItemFunction, GetItemOutputDTO>(getItemFunction, blockParameter));
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: ranjancse26/RSK-KMS
        /// <summary>
        /// The AES Key/Value with the Private Key for Contract is for demonstration purpose only
        /// Feel free to use it.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            string key   = "aesKey";
            string value = "testing";

            string rnsResolvedAddress = GetRnsResolvedAddress("ranjancse.rsk", true);

            string nodeUrl                = ConfigurationManager.AppSettings["RskTestnetNodeUrl"];
            var    privateKey             = ConfigurationManager.AppSettings["PrivateKey"];
            var    fromTransferPrivateKey = ConfigurationManager.AppSettings["FromTransferPrivateKey"];
            var    account                = new Nethereum.Web3.Accounts.Account(privateKey);

            IGasPriceService gasPriceService = new GasPriceService(nodeUrl);
            int gasPrice = gasPriceService.GetRskMinGasPrice();

            // Load some RBTC
            LoadSomeRBTC(nodeUrl, fromTransferPrivateKey,
                         account.Address, 0.001m, 0.06m);

            Stopwatch stopwatch = new Stopwatch();

            System.Console.WriteLine("Trying to pull the RSA certificate from the local store using the Thumbprint");

            // Get the certificate by Thumbprint
            string           thumbPrint   = ConfigurationManager.AppSettings["Thumbprint"].ToUpper();
            X509Certificate2 filteredCert = X509CertificateHelper.GetRSKCertificate(thumbPrint,
                                                                                    StoreLocation.LocalMachine);

            if (filteredCert == null)
            {
                System.Console.WriteLine($"Unable to find the RSK certificate by Thumbprint: " +
                                         $"{thumbPrint}");
                System.Console.ReadLine();
                return;
            }

            // Encrypt Text/Data
            var encryptedText = RSAEncryptionHelper.Encrypt(value, filteredCert);

            var  url  = ConfigurationManager.AppSettings["ContractDeploymentUrl"];
            Web3 web3 = new Web3(account, url);

            // Get the balance
            stopwatch.Start();
            var weiBalance  = AccountHelper.GetBalance(web3, account);
            var etherAmount = Web3.Convert.FromWei(weiBalance.Value);

            stopwatch.Stop();

            System.Console.WriteLine($"Account Balance: {etherAmount}");
            System.Console.WriteLine($"Time take to fetch the balance:" +
                                     $" {stopwatch.Elapsed.Seconds} seconds");

            // Gas estimated, in wei
            System.Console.WriteLine($"Estimated Gas Price: {gasPrice}");

            System.Console.WriteLine("Deploying the Iterable Mapping Library");
            stopwatch.Restart();

            // Deploy Iterable Mapping Library
            TransactionReceipt transactionReceiptDeployment;
            string             contractAddress;
            ContractHandler    contractHandler;

            RSKContractHelper.DeployIterableMappingContract(web3,
                                                            out transactionReceiptDeployment,
                                                            out contractAddress,
                                                            out contractHandler);
            stopwatch.Stop();

            System.Console.WriteLine($"Iterable Mapping Contarct Address: " +
                                     $"{contractAddress}");
            System.Console.WriteLine($"Time taken to deploy the Iterable mapping:" +
                                     $" {stopwatch.Elapsed.Seconds} seconds");

            System.Console.WriteLine("Deploying the RSK KMS Contract");

            // Deploy the RSK Contract
            stopwatch.Restart();
            contractHandler = RSKContractHelper.DeployRSKKeyManagmentContract(web3,
                                                                              transactionReceiptDeployment,
                                                                              out contractAddress);
            stopwatch.Stop();
            System.Console.WriteLine($"RSK Contract Address {contractAddress}");
            System.Console.WriteLine($"Time taken to deploy the RSK Contract: " +
                                     $"{stopwatch.Elapsed.Seconds} seconds");

            System.Console.WriteLine("Trying to set a value in RSK KMS Contract");

            /** Function: setItem**/
            var setItemRequest = new SetItemFunction
            {
                Key         = key,
                Value       = encryptedText,
                FromAddress = account.Address
            };

            setItemRequest.GasPrice = new BigInteger(gasPrice * 1.1);

            stopwatch.Restart();
            var setItemFunctionTxnReceipt = contractHandler
                                            .SendRequestAndWaitForReceiptAsync(setItemRequest)
                                            .ConfigureAwait(false)
                                            .GetAwaiter()
                                            .GetResult();

            stopwatch.Stop();
            System.Console.WriteLine($"Time taken to set the KMS Key Item: " +
                                     $"{stopwatch.Elapsed.Seconds} seconds");

            System.Console.WriteLine("Trying to get a value from the RSK KMS Contract");

            /** Function: getItem**/
            var getItemRequest = new GetItemFunction
            {
                Key         = key,
                FromAddress = account.Address
            };

            stopwatch.Restart();
            var getItemResponse = contractHandler
                                  .QueryAsync <GetItemFunction, string>(getItemRequest)
                                  .ConfigureAwait(false)
                                  .GetAwaiter()
                                  .GetResult();

            stopwatch.Stop();
            System.Console.WriteLine($"Time taken to get the KMS Key Item: " +
                                     $"{stopwatch.Elapsed.Seconds} seconds");

            if (!string.IsNullOrEmpty(getItemResponse))
            {
                var decryptedText = RSAEncryptionHelper.Decrypt(getItemResponse, filteredCert);
                System.Console.WriteLine($"Decrypted Text: {decryptedText}");
            }
            else
            {
                System.Console.WriteLine("The KMS Response as empty");
            }

            System.Console.WriteLine("Press any key to exit");
            System.Console.ReadLine();
        }