Ejemplo n.º 1
0
        public async Task ShouldBeAbleToDeployAContractLoadingEncryptedPrivateKey()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var password      = "******";
            //this is your wallet key file which can be found on

            //Linux: ~/.ethereum/keystore
            //Mac: /Library/Ethereum/keystore
            //Windows: %APPDATA%/Ethereum

            var keyStoreEncryptedJson =
                @"{""crypto"":{""cipher"":""aes-128-ctr"",""ciphertext"":""b4f42e48903879b16239cd5508bc5278e5d3e02307deccbec25b3f5638b85f91"",""cipherparams"":{""iv"":""dc3f37d304047997aa4ef85f044feb45""},""kdf"":""scrypt"",""mac"":""ada930e08702b89c852759bac80533bd71fc4c1ef502291e802232b74bd0081a"",""kdfparams"":{""n"":65536,""r"":1,""p"":8,""dklen"":32,""salt"":""2c39648840b3a59903352b20386f8c41d5146ab88627eaed7c0f2cc8d5d95bd4""}},""id"":""19883438-6d67-4ab8-84b9-76a846ce544b"",""address"":""12890d2cce102216644c59dae5baed380d84830c"",""version"":3}";

            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            //if not using portable or netstandard (^net45) you can use LoadFromKeyStoreFile to load the file from the file system.

            var keyStoreService = new KeyStore.KeyStoreService();
            var key             = keyStoreService.DecryptKeyStoreFromJson(password, keyStoreEncryptedJson);


            var web3 = new Web3.Web3(new Account(key));

            var transactionPolling = new TransactionReceiptPollingService(web3);

            //assumed client is mining already
            var contractAddress = await
                                  transactionPolling.DeployContractAndGetAddressAsync(
                () =>
                web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000),
                                                         multiplier)
                );

            var contract = web3.Eth.GetContract(abi, contractAddress);

            var multiplyFunction = contract.GetFunction("multiply");

            var result = await multiplyFunction.CallAsync <int>(7);

            Assert.Equal(49, result);
        }
        public async Task <Decimal> CalculateTotalBalanceAccountsInFolderAsync(string rpcAddress, string folder)
        {
            var addresses = new List <string>();

            foreach (var file in Directory.GetFiles(folder))
            {
                var service = new KeyStore.KeyStoreService();
                using (var jsonFile = File.OpenText(file))
                {
                    var json    = jsonFile.ReadToEnd();
                    var address = service.GetAddressFromKeyStore(json);
                    addresses.Add(address);
                }
            }

            return(await CalculateTotalBalanceAccountsAsync(rpcAddress, addresses).ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        public async Task ShouldBeAbleToTransferBetweenAccountsLoadingEncryptedPrivateKey()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var addressTo     = "0x13f022d72158410433cbd66f5dd8bf6d2d129924";
            var password      = "******";

            var keyStoreEncryptedJson =
                @"{""crypto"":{""cipher"":""aes-128-ctr"",""ciphertext"":""b4f42e48903879b16239cd5508bc5278e5d3e02307deccbec25b3f5638b85f91"",""cipherparams"":{""iv"":""dc3f37d304047997aa4ef85f044feb45""},""kdf"":""scrypt"",""mac"":""ada930e08702b89c852759bac80533bd71fc4c1ef502291e802232b74bd0081a"",""kdfparams"":{""n"":65536,""r"":1,""p"":8,""dklen"":32,""salt"":""2c39648840b3a59903352b20386f8c41d5146ab88627eaed7c0f2cc8d5d95bd4""}},""id"":""19883438-6d67-4ab8-84b9-76a846ce544b"",""address"":""12890d2cce102216644c59dae5baed380d84830c"",""version"":3}";

            //this is your wallet key  file which can be found on

            //Linux: ~/.ethereum/keystore
            //Mac: /Library/Ethereum/keystore
            //Windows: %APPDATA%/Ethereum


            //if not using portable or netstandard (^net45) you can use LoadFromKeyStoreFile to load the file from the file system.

            var keyStoreService = new KeyStore.KeyStoreService();
            var key             = keyStoreService.DecryptKeyStoreFromJson(password, keyStoreEncryptedJson);

            var account = new Account(key);
            var web3    = new Web3.Web3(account);

            //The transaction receipt polling service is a simple utility service to poll for receipts until mined
            var transactionPolling = new TransactionReceiptPollingService(web3);

            var currentBalance = await web3.Eth.GetBalance.SendRequestAsync(addressTo);

            //assumed client is mining already
            //when sending a transaction using an Account, a raw transaction is signed and send using the private key
            var transactionReceipt = await transactionPolling.SendRequestAsync(() =>
                                                                               web3.TransactionManager.SendTransactionAsync(account.Address, addressTo, new HexBigInteger(20))
                                                                               );

            var newBalance = await web3.Eth.GetBalance.SendRequestAsync(addressTo);

            Assert.Equal(currentBalance.Value + 20, newBalance.Value);
        }
Ejemplo n.º 4
0
        private int RunCommand()
        {
            var sourceFolder = _sourceFolder.Value();

            if (string.IsNullOrWhiteSpace(sourceFolder))
            {
                System.Console.WriteLine("The source folder was not specified");
                return(1);
            }

            var rpcAddress = _rpcAddress.Value();

            if (string.IsNullOrWhiteSpace(rpcAddress))
            {
                System.Console.WriteLine("The rpcAddress was not specified");
                return(1);
            }

            BigInteger balance = 0;

            Web3.Web3 web3 = new Web3.Web3(rpcAddress);
            foreach (var file in Directory.GetFiles(sourceFolder))
            {
                var service = new KeyStore.KeyStoreService();
                using (var jsonFile = File.OpenText(file))
                {
                    var json    = jsonFile.ReadToEnd();
                    var address = service.GetAddressFromKeyStore(json);
                    balance = balance + web3.Eth.GetBalance.SendRequestAsync(address).Result;
                }
            }

            System.Console.WriteLine("Total Balance: " + Web3.Web3.Convert.FromWei(balance));

            return(1);
        }