public async void ShouldTransferWholeBalanceInEtherEIP1599() { var web3 = _ethereumClientIntegrationFixture.GetWeb3(); var privateKey = EthECKey.GenerateKey(); var newAccount = new Account(privateKey.GetPrivateKey(), EthereumClientIntegrationFixture.ChainId); var toAddress = newAccount.Address; var transferService = web3.Eth.GetEtherTransferService(); var fee = await transferService.SuggestFeeToTransferWholeBalanceInEtherAsync().ConfigureAwait(false); var receipt = await transferService .TransferEtherAndWaitForReceiptAsync(toAddress, 0.1m, maxPriorityFee : fee.MaxPriorityFeePerGas.Value, maxFeePerGas : fee.MaxFeePerGas.Value).ConfigureAwait(false); var balance = await web3.Eth.GetBalance.SendRequestAsync(toAddress).ConfigureAwait(false); Assert.Equal(0.1m, Web3.Web3.Convert.FromWei(balance)); var web32 = new Web3.Web3(newAccount, web3.Client); var feeWhole = await web32.Eth.GetEtherTransferService().SuggestFeeToTransferWholeBalanceInEtherAsync().ConfigureAwait(false); var amount = await web32.Eth.GetEtherTransferService() .CalculateTotalAmountToTransferWholeBalanceInEtherAsync(toAddress, maxFeePerGas: feeWhole.MaxFeePerGas.Value).ConfigureAwait(false); var receiptBack = await web32.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(EthereumClientIntegrationFixture.AccountAddress, amount, feeWhole.MaxPriorityFeePerGas.Value, feeWhole.MaxFeePerGas.Value).ConfigureAwait(false); var balanceFrom = await web32.Eth.GetBalance.SendRequestAsync(toAddress).ConfigureAwait(false); Assert.Equal(0, Web3.Web3.Convert.FromWei(balanceFrom)); }
public async void ShouldTransferWholeBalanceInEther() { var web3 = _ethereumClientIntegrationFixture.GetWeb3(); var privateKey = EthECKey.GenerateKey(); var newAccount = new Account(privateKey.GetPrivateKey()); var toAddress = newAccount.Address; var transferService = web3.Eth.GetEtherTransferService(); var receipt = await web3.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(toAddress, 1000, 2); var balance = await web3.Eth.GetBalance.SendRequestAsync(toAddress); Assert.Equal(1000, Web3.Web3.Convert.FromWei(balance)); var totalAmountBack = await transferService.CalculateTotalAmountToTransferWholeBalanceInEther(toAddress, 2); var web32 = new Web3.Web3(newAccount, web3.Client); var receiptBack = await web32.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(AccountFactory.Address, totalAmountBack, 2); var balanceFrom = await web32.Eth.GetBalance.SendRequestAsync(toAddress); Assert.Equal(0, Web3.Web3.Convert.FromWei(balanceFrom)); }
public async void ShouldTransferWholeBalanceInEther() { var web3 = _ethereumClientIntegrationFixture.GetWeb3(); var privateKey = EthECKey.GenerateKey(); var newAccount = new Account(privateKey.GetPrivateKey(), EthereumClientIntegrationFixture.ChainId); var toAddress = newAccount.Address; var transferService = web3.Eth.GetEtherTransferService(); var receipt = await web3.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(toAddress, 0.1m, gasPriceGwei: 2).ConfigureAwait(false); var balance = await web3.Eth.GetBalance.SendRequestAsync(toAddress).ConfigureAwait(false); Assert.Equal(0.1m, Web3.Web3.Convert.FromWei(balance)); var totalAmountBack = await transferService.CalculateTotalAmountToTransferWholeBalanceInEtherAsync(toAddress, 2m).ConfigureAwait(false); var web32 = new Web3.Web3(newAccount, web3.Client); var receiptBack = await web32.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(EthereumClientIntegrationFixture.AccountAddress, totalAmountBack, gasPriceGwei: 2).ConfigureAwait(false); var balanceFrom = await web32.Eth.GetBalance.SendRequestAsync(toAddress).ConfigureAwait(false); Assert.Equal(0, Web3.Web3.Convert.FromWei(balanceFrom)); }
private async Task <(string, byte[])> GenerateAndImportPrivateKeyAsync() { var ecKey = EthECKey.GenerateKey(); var address = ecKey.GetPublicAddress().ToLowerInvariant(); #region Logging _log.Debug ( $"A private key for {address} has been generated.", new { address } ); #endregion await _keyVaultClient.ImportKeyAsync ( vaultBaseUrl : _vaultBaseUrl, keyName : address, key : ecKey.ToJsonWebKey() ); #region Logging _log.Debug ( $"A private key for {address} has been imported to the key vault.", new { address } ); #endregion return(address, ecKey.GetPubKey()); }
public UserKeys() { var privKey = EthECKey.GenerateKey(); // Random private key PublicKey = privKey.GetPubKeyNoPrefix().ToHex(); PrivateKey = privKey.GetPrivateKey().Substring(2); }
public static Account Create() { var accountEcKey = EthECKey.GenerateKey(); var accountPrivateKey = accountEcKey.GetPrivateKeyAsBytes().ToHex(); return(new Account(accountPrivateKey)); }
private void Start() { //PlayerPrefs.DeleteAll(); SaveData = JsonUtility.FromJson <SaveDataModel>(PlayerPrefs.GetString("SaveData")); if (SaveData == null) { SaveData = new SaveDataModel(); } if (string.IsNullOrEmpty(SaveData.PrivateKey)) { EthECKey ecKey = EthECKey.GenerateKey(); SaveData.PrivateKey = ecKey.GetPrivateKey().Substring(2); SaveData.Address = ecKey.GetPublicAddress().ToLower().Trim(); } else { EthECKey ecKey = new EthECKey(SaveData.PrivateKey); SaveData.Address = ecKey.GetPublicAddress().ToLower().Trim(); } if (SaveData.Balance <= (decimal)1) { ETHManager.Instance.RopstenFauset(SaveData.Address, (arg0) => { }); } Save(); GetBalance(); }
private static List <List <EthECKey> > GenerateKeys(int count) { int taskCount = 8; var tasks = new List <Task <List <EthECKey> > >(); for (int t = 0; t < taskCount; t++) { int taskId = t; Task <List <EthECKey> > task = Task.Run(() => { var keys = new List <EthECKey>(); for (int i = 0; i < count / taskCount; i++) { keys.Add(EthECKey.GenerateKey()); } return(keys); }); tasks.Add(task); } var result = new List <List <EthECKey> >(); foreach (var task in tasks) { result.Add(task.Result); } return(result); }
public string GenerateNewPrivateKey() { var ecKey = EthECKey.GenerateKey(); var privateKey = ecKey.GetPrivateKey(); return(privateKey); }
public void ShouldCreateAKeyOf32Bytes() { for (int i = 0; i < 10000; i++) { var key = EthECKey.GenerateKey(); Assert.True(key.GetPrivateKeyAsBytes().Length == 32); } }
public void CreatePkey() { EthECKey ecKey = EthECKey.GenerateKey(); accountPrivateKey = ecKey.GetPrivateKey(); CopyMainPrivateKey(); AfterEnterPkey(); }
public void ShouldCreateDifferentKeysUsingRandomSeeds() { //this is using random so they will do anyway, but somebody thought that the seed was to create the same key, so keeping it here for reference var key = EthECKey.GenerateKey(Sha3Keccack.Current.CalculateHash("monkey").HexToByteArray()); var key2 = EthECKey.GenerateKey(Sha3Keccack.Current.CalculateHash("monkey2").HexToByteArray()); Assert.True(key.GetPrivateKey() != key2.GetPrivateKey()); }
public void ShouldGenerateECKey() { var ecKey = EthECKey.GenerateKey(); var key = ecKey.GetPrivateKeyAsBytes(); var regeneratedKey = new ECKey(key, true); Assert.Equal(key.ToHex(), regeneratedKey.GetPrivateKeyAsBytes().ToHex()); Assert.Equal(ecKey.GetPublicAddress(), regeneratedKey.GetPublicAddress()); }
// ------------------------------------------- /* * Display messages on screen and buttons on screen */ void OnGUI() { GUI.skin = SkinUI; float fontSize = 1.2f * 15; // BUTTON CLEAR LOG float yGlobalPosition = 10; if (GUI.Button(new Rect(new Vector2(10, yGlobalPosition), new Vector2(Screen.width - 20, 2 * fontSize)), "Clear Log")) { m_activateTextArea = false; m_displayMessages.Clear(); } yGlobalPosition += 2.2f * fontSize; // GENERATE NEW KEY if (GUI.Button(new Rect(new Vector2(10, yGlobalPosition), new Vector2(Screen.width - 20, 4 * fontSize)), "Create free new address on ++" + EthereumController.Instance.NetworkAPI + "++ Network")) { #if ENABLE_ETHEREUM EthECKey ecKey = EthECKey.GenerateKey(); string privateKey = ecKey.GetPrivateKey(); AddLog("+++GENERATING KEY FOR NETWORK[" + EthereumController.Instance.NetworkAPI + "]+++"); AddLog("++++KEY GENERATED++++"); AddLog("PRIVATE KEY:"); AddLog("" + privateKey); AddLog("PUBLIC KEY:"); AddLog("" + ecKey.GetPublicAddress()); #endif } yGlobalPosition += 4.2f * fontSize; // LOG DISPLAY GUI.Label(new Rect(0, yGlobalPosition, Screen.width - 20, fontSize), "**PROGRAM LOG**"); yGlobalPosition += 1.2f * fontSize; int linesTextArea = 10; if (m_activateTextArea) { linesTextArea = 10; } else { linesTextArea = 2; } float finalHeighArea = linesTextArea * fontSize; m_scrollPosition = GUI.BeginScrollView(new Rect(10, yGlobalPosition, Screen.width - 20, Screen.height - yGlobalPosition), m_scrollPosition, new Rect(0, 0, 200, m_displayMessages.Count * finalHeighArea)); float yPosition = 0; for (int i = 0; i < m_displayMessages.Count; i++) { string message = m_displayMessages[i]; GUI.TextArea(new Rect(0, yPosition, Screen.width, finalHeighArea), message); yPosition += finalHeighArea; } GUI.EndScrollView(); }
public static void Main(string[] args) { //Reading an existing file var address = "12890d2cce102216644c59dae5baed380d84830c"; var password = "******"; //UTC--2016-11-23T09-58-36Z--ca2137bc-e2a1-5c40-d60d-ab8cf5fb302c var file = File.OpenText("UTC--2016-11-23T09-58-36Z--ca2137bc-e2a1-5c40-d60d-ab8cf5fb302c"); // var file = File.OpenText("UTC--2015-11-25T05-05-03.116905600Z--12890d2cce102216644c59dae5baed380d84830c"); var json = file.ReadToEnd(); //using the simple key store service var service = new KeyStoreService(); //decrypt the private key var key = service.DecryptKeyStoreFromJson(password, json); //Generating a new file using the existing private key (key) //create new file with the standard UTC file name var fileName = service.GenerateUTCFileName(address); using (var newfile = File.CreateText(fileName)) { //generate the encrypted and key store content as json. (The default uses pbkdf2) var newJson = service.EncryptAndGenerateDefaultKeyStoreAsJson(password, key, address); newfile.Write(newJson); newfile.Flush(); } //Decrypting the new file created file = File.OpenText(fileName); json = file.ReadToEnd(); var newkey = service.DecryptKeyStoreFromJson(password, json); //Compare the keys System.Console.WriteLine("Original key: " + key.ToHex()); System.Console.WriteLine("New key: " + key.ToHex()); System.Console.ReadLine(); //We can use EthECKey to generate a new ECKey pair, this is using SecureRandom var ecKey = EthECKey.GenerateKey(); var privateKey = ecKey.GetPrivateKeyAsBytes(); var genAddress = ecKey.GetPublicAddress(); //instead of the default service we can use either //Scrypt var scryptService = new KeyStoreScryptService(); var scryptResult = scryptService.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress); //or pkbdf2 var pbkdf2Service = new KeyStorePbkdf2Service(); var pkbdf2Result = pbkdf2Service.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress); //Both services can be configured with a new IRandomBytesGenerator for the IV and Salt, currently uses SecureRandom for both. //also when encrypting we can pass custom KdfParameters }
public void SigTest() { var key = EthECKey.GenerateKey(); var privatekey1 = key.GetPrivateKey(); var publicKeyAddress = key.GetPublicAddress(); var str = "0a0277372208420acec80decf6fc40b8b9d59dd72e5a68080112640a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412330a1541b221f7d502a2120fe5eafd6c87507741955f168812154194c5e2d9527ddb212e4b69fb52e62b551ee39eee1880ade20470aaf7d19dd72e"; var signHex = EthECKey.Sign(str.HexToByteArray(), privatekey1); var verify = EthECKey.Verify(str.HexToByteArray(), signHex, publicKeyAddress); }
public IKeySecret Generate() { var key = EthECKey.GenerateKey(); return(new EthKeySecret() { PrivateKey = key.GetPrivateKey(), PublicAddress = key.GetPublicAddress() }); }
/// <summary> /// Creates blockchain account if needed and store the private key in Azure KeyVault /// </summary> /// <param name="identifier">key pair identifier.</param> /// <param name="privateKey">The given private key to store, if not supplied a new private key will be generated</param> public async Task CreateAccountAsync(string identifier, string privateKey = "") { if (string.IsNullOrEmpty(privateKey)) { var account = EthECKey.GenerateKey(); privateKey = account.GetPrivateKey(); } await StoreAccountAsync(identifier, privateKey); }
/// <summary> /// Create a Recent wallet /// </summary> /// <returns>Wallet data</returns> public WalletData createWallet() { var ecKey = EthECKey.GenerateKey(); _wallet = new WalletData { address = ecKey.GetPublicAddress(), PK = ecKey.GetPrivateKey() }; _web3 = new Web3(new Nethereum.Web3.Accounts.Account(_wallet.PK), NodeUrl); return(_wallet); }
public (string Address, string PrivateKey) CreateWallet() { var ethECKey = EthECKey.GenerateKey(); return ( ethECKey.GetPublicAddress(), ethECKey.GetPrivateKey() ); }
public static string GenerateNewNonce() { var currentChallenge = DateTime.Now.ToString("O") + "- Challenge"; //creating a random key, hashing, signing and hashing again to be truly random var key = EthECKey.GenerateKey(); var currentKey = key.GetPrivateKey(); // random key to sign message var signer = new MessageSigner(); return(Util.Sha3Keccack.Current.CalculateHash(signer.HashAndSign(currentChallenge, currentKey))); }
public EthAccount GenerateAddress() { var ecKey = EthECKey.GenerateKey(); return(new EthAccount { PrivateKey = ecKey.GetPrivateKey(), PublicAddress = ecKey.GetPublicAddress() }); }
public void CreateAccount() { invalidPrivateKey.SetActive(false); var ecKey = EthECKey.GenerateKey(); var privateKey = ecKey.GetPrivateKey(); PlayerPrefs.SetString("privateKey", privateKey); SheepContract.Instance.SwitchAccount(); switchAccountPanel.SetActive(false); }
public WalletCreationResponse CreateWallet() { var key = EthECKey.GenerateKey(); var address = AddressUtil.Current.ConvertToChecksumAddress(key.GetPublicAddress()); return(new WalletCreationResponse { PublicAddress = address, PrivateKey = key.GetPrivateKey() }); }
private static string GetAccount() { var ecKey = EthECKey.GenerateKey(); var privateKey = ecKey.GetPrivateKey(); Debug.Log(string.Format("private key {0}", privateKey)); // var privateKey = ecKey.GetPrivateKeyAsBytes().ToHex(); var account = new Account(ecKey); return(account.Address); }
public ContractHandler(string blockchainNetworkUrl) { this.blockchainNetworkUrl = blockchainNetworkUrl; var ecKey = EthECKey.GenerateKey(); var privateKey = ecKey.GetPrivateKeyAsBytes().ToHex(); this.account = new Account(privateKey); this.web3 = new Web3(account, this.blockchainNetworkUrl); web3.TransactionManager.DefaultGas = 6721975; web3.TransactionManager.DefaultGasPrice = 0; }
//从私钥创建账号 public void CreateAccountFromkey() { Console.WriteLine("create offline account from private key"); var keyPairs = EthECKey.GenerateKey(); var privateKey = keyPairs.GetPrivateKey(); var chainId = new BigInteger(1234); var account = new Account(privateKey, chainId); Console.WriteLine("address=>" + account.Address); Console.WriteLine("TransactionManager=>" + account.TransactionManager); }
/// <summary> /// Create a Recent wallet /// </summary> /// <returns>Wallet data</returns> public WalletData createWallet() { var ecKey = EthECKey.GenerateKey(); _wallet = new WalletData { address = ecKey.GetPublicAddress(), PK = ecKey.GetPrivateKey() }; _web3 = new Web3(new Nethereum.Web3.Accounts.Account(_wallet.PK), _nodeUrl); _paymentChannelsContract = _web3.Eth.GetContract(PaymentChannelsABI, _paymentChannelsContractAddress); _validatorsContract = _web3.Eth.GetContract(ValidatorsABI, _validatorsContractAddress); return(_wallet); }
public IActionResult TestKeys() { List <string> result = new List <string>(); for (int i = 0; i < 20; i++) { var key = EthECKey.GenerateKey(); result.Add(key.GetPublicAddress()); } return(Ok(result)); }
public void SharedSecretGenerationBasedOnEthKeyImportTest() { EthECKey ecKey1 = EthECKey.GenerateKey(); EthECKey ecKey2 = EthECKey.GenerateKey(); X25519Key alice = X25519Key.ImportKey(ecKey1.GetPrivateKey()); X25519Key bob = X25519Key.ImportKey(ecKey2.GetPrivateKey()); var shared1 = alice.GenerateSharedSecretHex(bob.PublicKey); var shared2 = bob.GenerateSharedSecretHex(alice.PublicKey); Assert.Equal(shared1, shared2); }