public static async Task TestTransactionSigning()
        {
            using (var trezorManager = await TrezorFactory.GetWindowsConnectedLedgerManagerAsync(GetPin))
            {
                await trezorManager.InitializeAsync();

                var signer = new TrezorExternalSigner(trezorManager, 0);

                var account = new ExternalAccount(signer);
                await account.InitialiseAsync();

                account.InitialiseDefaultTransactionManager(new RpcClient(new Uri("http://localhost:8545")));
                var tx = new TransactionInput()
                {
                    Nonce    = new HexBigInteger(10),
                    GasPrice = new HexBigInteger(10),
                    Gas      = new HexBigInteger(21000),
                    To       = "0x689c56aef474df92d44a1b70850f808488f9769c",
                    Value    = new HexBigInteger(BigInteger.Parse("10000000000000000000")),
                    From     = "0x6A1D4583b83E5ef91EeA1E591aD333BD04853399"
                };
                var signature = await account.TransactionManager.SignTransactionAsync(tx);

                var accountNethereum = new Account("0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29");
                accountNethereum.TransactionManager.Client = new RpcClient(new Uri("http://localhost:8545"));
                var signatureNethereum = await accountNethereum.TransactionManager.SignTransactionAsync(tx);

                System.Console.WriteLine("Trezor: " + signature);
                System.Console.WriteLine("Nethereum: " + signatureNethereum);
            }
        }
        public static async Task TestTransferTokenSigning()
        {
            using (var trezorManager = await TrezorFactory.GetWindowsConnectedLedgerManagerAsync(GetPin))
            {
                await trezorManager.InitializeAsync();

                var signer = new TrezorExternalSigner(trezorManager, 0);

                var account = new ExternalAccount(signer);
                await account.InitialiseAsync();

                var rpcClient = new RpcClient(new Uri("http://localhost:8545"));
                account.InitialiseDefaultTransactionManager(rpcClient);
                var web3 = new Web3.Web3(account, rpcClient);
                var tx   = new TransferFunction()
                {
                    Nonce       = 10,
                    GasPrice    = 10,
                    Gas         = 21000,
                    To          = "0x689c56aef474df92d44a1b70850f808488f9769c",
                    Value       = 100,
                    FromAddress = "0x6A1D4583b83E5ef91EeA1E591aD333BD04853399"
                };

                var signature = await web3.Eth.GetContractTransactionHandler <TransferFunction>().SignTransactionAsync("0x6810e776880c02933d47db1b9fc05908e5386b96", tx);

                var web32 = new Web3.Web3(new Account("0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29"));
                var signatureNethereum = await web32.Eth.GetContractTransactionHandler <TransferFunction>()
                                         .SignTransactionAsync("0x6810e776880c02933d47db1b9fc05908e5386b96", tx);

                System.Console.WriteLine("Trezor: " + signature);
                System.Console.WriteLine("Nethereum: " + signatureNethereum);
            }
        }
Example #3
0
        public async Task Test(string addressFrom, string privateKey, bool legacy)
        {
            var transfer = new TransferFunction();

            transfer.To          = "0x12890d2cce102216644c59daE5baed380d848301";
            transfer.FromAddress = addressFrom;
            transfer.Value       = 1;
            transfer.Nonce       = 1;
            transfer.GasPrice    = 100;
            transfer.Gas         = 1000;
            var rpcClient        = new HttpClient(new Uri("http://localhost:8545"), defaultTimeOutForTests);
            var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c");

            var account = new Account(privateKey, Chain.MainNet);

            account.TransactionManager.Client = rpcClient;
            var signature = await account.TransactionManager.SignTransactionAsync(transactionInput);

            var ledgerManager = await LedgerFactory.GetWindowsConnectedLedgerManager();

            var externalAccount = new ExternalAccount(new LedgerExternalSigner(ledgerManager, 0, legacy), 1);
            await externalAccount.InitialiseAsync();

            externalAccount.InitialiseDefaultTransactionManager(rpcClient);
            //Ensure contract data is enable in the settings of ledger nano
            var signature2 = await externalAccount.TransactionManager.SignTransactionAsync(transactionInput);

            Assert.Equal(signature, signature2);

            //Signing just transfer
            var signature3 = await externalAccount.TransactionManager.SignTransactionAsync(new TransactionInput()
            {
                From = addressFrom,
                GasPrice
                      = new HexBigInteger(Transaction.DEFAULT_GAS_PRICE),
                Gas   = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT),
                Nonce = new HexBigInteger(1),
                To    = "0x12890d2cce102216644c59daE5baed380d848301",
                Value = new HexBigInteger(100)
            });
        }
        public async Task TestSignature()
        {
            var transfer = new TransferFunction();

            transfer.To          = "0x12890d2cce102216644c59daE5baed380d848301";
            transfer.FromAddress = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D";
            transfer.Value       = 1;
            transfer.Nonce       = 1;
            transfer.GasPrice    = 100;
            transfer.Gas         = 1000;
            var rpcClient        = new RpcClient(new Uri("http://localhost:8545"));
            var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c");

            var account = new Account("0x128c6818917d98a3b933de1d400e777963424ce71f0a58755b092d1b670394eb", Chain.MainNet);

            account.TransactionManager.Client = rpcClient;
            var signature = await account.TransactionManager.SignTransactionAsync(transactionInput);


            var externalAccount = new ExternalAccount(new EthECKeyExternalSigner(new LedgerExternalSigner()), 1);
            await externalAccount.InitialiseAsync();

            externalAccount.InitialiseDefaultTransactionManager(rpcClient);
            //Ensure contract data is enable in the settings of ledger nano
            var signature2 = await externalAccount.TransactionManager.SignTransactionAsync(transactionInput);

            Assert.Equal(signature, signature2);

            //Signing just transfer
            var signature3 = await externalAccount.TransactionManager.SignTransactionAsync(new TransactionInput()
            {
                From = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D",
                GasPrice
                      = new HexBigInteger(Transaction.DEFAULT_GAS_PRICE),
                Gas   = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT),
                Nonce = new HexBigInteger(1),
                To    = "0x12890d2cce102216644c59daE5baed380d848301",
                Value = new HexBigInteger(100)
            });
        }
Example #5
0
        static void Main(string[] args)
        {
            KeyVaultClient kvc       = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
            var            signer    = new AzureKeyVaultExternalSigner(kvc, URI);
            var            publicKey = signer.GetPublicKeyAsync().Result;

            System.Console.WriteLine(publicKey.ToHex());

            var msgHash           = new Util.Sha3Keccack().CalculateHash("Hello").HexToByteArray();
            var ethExternalSigner = new EthECKeyExternalSigner(signer);

            var signature          = ethExternalSigner.SignAndCalculateVAsync(msgHash).Result;
            var publicKeyRecovered = EthECKey.RecoverFromSignature(signature, msgHash);

            System.Console.WriteLine(publicKeyRecovered.GetPubKey().ToHex());

            var transfer = new TransferFunction();

            transfer.To          = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D";
            transfer.FromAddress = publicKeyRecovered.GetPublicAddress();
            transfer.Value       = 1;
            transfer.Nonce       = 1;
            transfer.GasPrice    = 100;
            transfer.Gas         = 1000;

            var rpcClient        = new RpcClient(new Uri("http://localhost:8545"));
            var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c");

            var externalAccount = new ExternalAccount(ethExternalSigner, 1);

            externalAccount.InitialiseAsync().Wait();
            externalAccount.InitialiseDefaultTransactionManager(rpcClient);
            var signature2          = externalAccount.TransactionManager.SignTransactionAsync(transactionInput).Result;
            var transactionSigner   = new TransactionSigner();
            var publicKeyRecovered2 = transactionSigner.GetPublicKey(signature2, 1);

            System.Console.WriteLine(publicKeyRecovered2.ToHex());
            System.Console.ReadLine();
        }
Example #6
0
        public async Task <TransactionReturnInfo> CreateTransactionWithExternalAccountAsync(string ContractAddress, ContractInfo contractInfo, string FunctionName, ExternalAccount externalAccount, object[] inputParams = null, List <string> PrivateFor = null)
        {
            if (web3 == null)
            {
                throw new Exception("web3 handler has not been set - please call SetWeb3Handler First");
            }

            if (PrivateFor != null && PrivateFor?.Count != 0)
            {
                web3.ClearPrivateForRequestParameters();
                web3.SetPrivateRequestParameters(PrivateFor);
            }

            var contract = web3.Eth.GetContract(contractInfo.ContractABI, ContractAddress);

            if (contract == null)
            {
                throw new Exception("Could not find contract with ABI at specified address");
            }

            var contractFunction = contract.GetFunction(FunctionName);

            if (contractFunction == null)
            {
                throw new Exception("Could not find function with name " + FunctionName);
            }

            try
            {
                await externalAccount.InitialiseAsync();

                externalAccount.InitialiseDefaultTransactionManager(web3.Client);

                //default RealGas
                var realGas = new HexBigInteger(500000);

                var txInput = contractFunction.CreateTransactionInput(externalAccount.Address, realGas, new HexBigInteger(0), new HexBigInteger(0), inputParams);

                //var txCountNonce = await externalAccount.NonceService.GetNextNonceAsync();

                //--- set nonce? ---//
                //txInput.Nonce = txCountNonce;

                //var supposedNextNonce = await account.NonceService.GetNextNonceAsync();
                //log.LogInformation("Nonce txInput Value: " + txCountNonce.Value );
                //Console.WriteLine("Nonce txInput Value: " + txCountNonce.Value );

                var transactionReceipt = await externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync(txInput, null);

                if (transactionReceipt != null)
                {
                    Console.WriteLine($"Processed Transaction - txHash: {transactionReceipt.TransactionHash}");

                    return(new TransactionReturnInfo
                    {
                        TransactionHash = transactionReceipt.TransactionHash,
                        BlockHash = transactionReceipt.BlockHash,
                        BlockNumber = transactionReceipt.BlockNumber.Value,
                        ContractAddress = ContractAddress
                    });
                }
                return(null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Example #7
0
        public async Task <TransactionReturnInfo> CreateContractWithExternalAccountAsync(ContractInfo contractInfo, ExternalAccount externalAccount, object[] inputParams = null, List <string> PrivateFor = null)
        {
            if (web3 == null)
            {
                throw new Exception("web3 handler has not been set - please call SetWeb3Handler First");
            }

            if (PrivateFor != null && PrivateFor?.Count != 0)
            {
                web3.ClearPrivateForRequestParameters();
                web3.SetPrivateRequestParameters(PrivateFor);
            }


            await externalAccount.InitialiseAsync();

            Console.WriteLine("externalAccount InitializeAsync() completed");
            externalAccount.InitialiseDefaultTransactionManager(web3.Client);
            Console.WriteLine("externalAccount.InitialiseDefaultTransactionManager() completed");
            //--- get transaction count to set nonce ---//
            var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(externalAccount.Address, BlockParameter.CreatePending());

            Console.WriteLine("web3.Eth.Transactions.GetTransactionCount.SendRequestAsync() completed");
            try
            {
                var gasDeploy = await web3.Eth.DeployContract.EstimateGasAsync(
                    abi : contractInfo.ContractABI,
                    contractByteCode : contractInfo.ContractByteCode,
                    from : externalAccount.Address,
                    values : inputParams == null?new object[] {} : inputParams);

                Console.WriteLine("Creating new contract and waiting for address");

                // Gas estimate is usually low - with private quorum we don't need to worry about gas so lets just multiply it by 5.
                var realGas = new HexBigInteger(gasDeploy.Value * 5);

                Console.WriteLine("About to call web3.Eth.DeployContract.GetData");
                var txData = web3.Eth.DeployContract.GetData(
                    contractInfo.ContractByteCode,
                    contractInfo.ContractABI,
                    inputParams
                    );
                Console.WriteLine("Returned from web3.Eth.DeployContract.GetData with the returned data of: {0}", txData);

                var txInput = new TransactionInput(
                    txData,
                    externalAccount.Address,
                    realGas,
                    new HexBigInteger(0)
                    );

                Console.WriteLine("About to call externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync");
                var transactionReceipt = await externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync(txInput, null);

                Console.WriteLine("Returned from externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync");
                Console.WriteLine(transactionReceipt.ContractAddress);

                return(new TransactionReturnInfo
                {
                    TransactionHash = transactionReceipt.TransactionHash,
                    BlockHash = transactionReceipt.BlockHash,
                    BlockNumber = transactionReceipt.BlockNumber.Value,
                    ContractAddress = transactionReceipt.ContractAddress
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Example #8
0
        static void Main(string[] args)
        {
            //var credential = new DefaultAzureCredential(
            //    new DefaultAzureCredentialOptions
            //    {
            //        //VisualStudioTenantId = TENANT_ID,
            //        //VisualStudioCodeTenantId = TENANT_ID,
            //    });
            var credential = new ClientSecretCredential(TENANT_ID, APP_ID, APP_PASSWORD);
            var signer     = new AzureKeyVaultExternalSigner(KEY_NAME, URI, credential);
            var address    = signer.GetAddressAsync().Result;

            System.Console.WriteLine(address);

            var msgHash = new Util.Sha3Keccack().CalculateHash("Hello").HexToByteArray();

            var signature          = signer.SignAsync(msgHash).Result;
            var publicKeyRecovered = EthECKey.RecoverFromSignature(signature, msgHash);

            System.Console.WriteLine(publicKeyRecovered.GetPubKey().ToHex());

            var transfer = new TransferFunction();

            transfer.To          = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D";
            transfer.FromAddress = publicKeyRecovered.GetPublicAddress();
            transfer.Value       = 1;
            transfer.Nonce       = 1;
            transfer.GasPrice    = 100;
            transfer.Gas         = 1000;

            var rpcClient        = new RpcClient(new Uri("http://localhost:8545"));
            var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c");

            var externalAccount = new ExternalAccount(signer, 1);

            externalAccount.InitialiseAsync().Wait();
            externalAccount.InitialiseDefaultTransactionManager(rpcClient);


            var signature2               = externalAccount.TransactionManager.SignTransactionAsync(transactionInput).Result;
            var publicKeyRecovered2      = TransactionVerificationAndRecovery.GetPublicKey(signature2);
            var transactionFromSignature = TransactionFactory.CreateTransaction(signature2);

            System.Console.WriteLine("Recovered public key");
            System.Console.WriteLine(publicKeyRecovered2.ToHex());
            System.Console.WriteLine("Recovered transaction Type");
            System.Console.WriteLine(transactionFromSignature.TransactionType.ToString());


            System.Console.WriteLine("Signing EIP1559");


            var transferEip1559 = new TransferFunction();

            transferEip1559.To                   = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D";
            transferEip1559.FromAddress          = publicKeyRecovered.GetPublicAddress();
            transferEip1559.Value                = 1;
            transferEip1559.Nonce                = 1;
            transferEip1559.MaxFeePerGas         = 1000;
            transferEip1559.MaxPriorityFeePerGas = 999;
            transferEip1559.Gas                  = 1000;

            var transactionInputEip1559 = transferEip1559.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c");
            var signature3                      = externalAccount.TransactionManager.SignTransactionAsync(transactionInputEip1559).Result;
            var publicKeyRecovered3             = TransactionVerificationAndRecovery.GetPublicKey(signature3);
            var transactionFromSignatureEIP1559 = TransactionFactory.CreateTransaction(signature3);

            System.Console.WriteLine("Recovered public key");
            System.Console.WriteLine(publicKeyRecovered3.ToHex());
            System.Console.WriteLine("Recovered transaction Type");
            System.Console.WriteLine(transactionFromSignatureEIP1559.TransactionType.ToString());
            System.Console.ReadLine();
        }