/// <summary> /// Create Sub Account Address and Private Key /// The Account Private key is encrypted with use of main account private key /// </summary> /// <param name="mainSecret">Main Account Private Key</param> /// <param name="name">Name of the Sub Account</param> /// <returns></returns> public async Task <(bool, string)> CreateAddress(BitcoinSecret mainSecret, string name) { if (!string.IsNullOrEmpty(Address)) { return(false, "Account already contains address."); } try { Key privateKey = new Key(); // generate a random private key PubKey publicKey = privateKey.PubKey; BitcoinSecret privateKeyFromNetwork = privateKey.GetBitcoinSecret(NeblioTransactionHelpers.Network); var address = publicKey.GetAddress(ScriptPubKeyType.Legacy, NeblioTransactionHelpers.Network); Address = address.ToString(); Secret = privateKeyFromNetwork; // todo load already encrypted key AccountKey = new Security.EncryptionKey(privateKeyFromNetwork.ToString()); AccountKey.PublicKey = Address; //ESKey = SymetricProvider.EncryptString(SecurityUtils.ComputeSha256Hash(mainSecret.PrivateKey.ToString()), privateKeyFromNetwork.ToString()); EKey = ECDSAProvider.EncryptStringWithPublicKey(privateKeyFromNetwork.ToString(), mainSecret.PubKey);// TODO: some preprocessor directive for run just in old version under .NETStandard2.1 //EKey = mainSecret.PubKey.Encrypt(privateKeyFromNetwork.ToString());// TODO: some preprocessor directive for run just in old version under .NETStandard2.1 Name = name; return(true, Address); } catch (Exception ex) { //todo return(false, ex.Message); } }
public override string LoadAccountKey(string wallet, string address, string key, IDbConnectorService dbservice, string pubkey = "", string password = "", string name = "", bool storeInDb = true, bool isItMainAccountKey = false, bool alreadyEncrypted = false, EncryptionKeyType type = EncryptionKeyType.BasicSecurity) { try { if (EconomyMainContext.Wallets.TryGetValue(wallet, out var w)) { if (w.Accounts.TryGetValue(address, out var account)) { if (isItMainAccountKey) { // todo load already encrypted key account.AccountKey = new Security.EncryptionKey(key, password); account.AccountKey.RelatedItemId = account.Id; account.AccountKey.Type = Security.EncryptionKeyType.AccountKey; account.AccountKeyId = account.AccountKey.Id; account.AccountKey.PublicKey = account.Address; if (!string.IsNullOrEmpty(password)) { account.AccountKey.PasswordHash = Security.SecurityUtil.HashPassword(password); } account.AccountKey.Name = name; if (EconomyMainContext.WorkWithDb) { dbservice.SaveKey(account.AccountKey); } account.AccountKeyId = account.AccountKey.Id; if (EconomyMainContext.WorkWithDb) { dbservice.SaveAccount(account); } return("OK"); } else { EncryptionKey k = null; if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(pubkey)) { // validate the key pair if it is correct combination of RSA keys try { if (type != EncryptionKeyType.AccountKey) { if (alreadyEncrypted) { var kd = SymetricProvider.DecryptString(password, key); if (kd != null) { key = kd; } } var m = AsymmetricProvider.EncryptString("test", pubkey); var r = AsymmetricProvider.DecryptString(m, key); if (r != "test") { throw new Exception("Key pair is not valid RSA key pair!"); } } k = new EncryptionKey(key, password); } catch (Exception ex) { throw new Exception("Key pair is not valid RSA key pair!"); } } else if (!string.IsNullOrEmpty(key) && string.IsNullOrEmpty(pubkey)) { if (alreadyEncrypted) { var kd = SymetricProvider.DecryptString(password, key); if (kd != null) { k = new Security.EncryptionKey(kd, password); } } else { k = new Security.EncryptionKey(key, password); k.LoadNewKey(key, fromDb: true); } } else if (!string.IsNullOrEmpty(pubkey) && string.IsNullOrEmpty(key)) { // create enc object k = new Security.EncryptionKey("passtest", password, true); // this can be used for testing the password k.PublicKey = pubkey; } else if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(pubkey)) { // obtain new RSA key pair var keypair = Security.AsymmetricProvider.GenerateNewKeyPair(); // create enc object k = new EncryptionKey(keypair.PrivateKey, password); k.PublicKey = keypair.PublicKey; } else { throw new Exception("Strange input!"); } k.RelatedItemId = account.Id; k.Type = Security.EncryptionKeyType.BasicSecurity; if (!string.IsNullOrEmpty(password)) { k.PasswordHash = Security.SecurityUtil.HashPassword(password); } k.Name = name; account.AccountKeys.Add(k); if (EconomyMainContext.WorkWithDb) { dbservice.SaveKey(k); } if (isItMainAccountKey) { account.AccountKeyId = account.AccountKey.Id; if (EconomyMainContext.WorkWithDb) { dbservice.SaveAccount(account); } } return("OK"); } } } } catch (Exception ex) { log.Error("Cannot load key to the account!", ex); } return("Load Account Key - ERROR"); }