private void SetSecretPhrase(NxtAccount account) { var localPasswordGenerator = new LocalPasswordGenerator(); var secretPhrase = localPasswordGenerator.GenerateDetermenisticPassword(MasterKey, account.Id, 256); account.SecretPhrase = secretPhrase; }
public async Task BackupAccount(NxtAccount account) { var decryptedAccountData = JsonConvert.SerializeObject(account); var encryptedMessage = nxtConnector.Encrypt(decryptedAccountData, MainAccount, secretPhrase); var encryptedJson = JsonConvert.SerializeObject(encryptedMessage); await nxtConnector.UploadTaggedData(encryptedJson, tagName, tags, "slack tipbot data storage", secretPhrase); }
private void SetAccountRs(NxtAccount account) { var accountWithPublicKey = GetAccountFromSecretPhrase(account.SecretPhrase); account.NxtPublicKey = accountWithPublicKey.PublicKey.ToHexString(); account.NxtAccountRs = accountWithPublicKey.AccountRs; }
public async Task UpdateAccount(NxtAccount account) { using (var context = new WalletContext()) { context.NxtAccounts.Attach(account); context.Entry(account).State = EntityState.Modified; await context.SaveChangesAsync(); } }
private async Task <NxtAccount> CreateNxtAccount(string slackUserId) { NxtAccount account = new NxtAccount { SlackId = slackUserId }; await walletRepository.AddAccount(account); nxtConnector.SetNxtProperties(account); await walletRepository.UpdateAccount(account); return(account); }
public void SetNxtProperties(NxtAccount account) { if (string.IsNullOrEmpty(account.SecretPhrase)) { SetSecretPhrase(account); SetAccountRs(account); } else if (string.IsNullOrEmpty(account.NxtAccountRs)) { SetAccountRs(account); } }
public async Task <NxtAccount> AddAccount(NxtAccount account) { using (var context = new WalletContext()) { context.NxtAccounts.Add(account); await context.SaveChangesAsync(); if (doBlockchainBackup) { await blockchainStore.BackupAccount(account); } return(account); } }
public async Task <ulong> Transfer(NxtAccount senderAccount, string addressRs, NxtTransferable transferable, decimal amount, string message, string recipientPublicKey = "") { SetNxtProperties(senderAccount); var parameters = new CreateTransactionBySecretPhrase(true, 1440, Amount.OneNxt, senderAccount.SecretPhrase); parameters.RecipientPublicKey = recipientPublicKey; parameters.Message = new CreateTransactionParameters.UnencryptedMessage(message, true); var quantity = (long)(amount * (long)Math.Pow(10, transferable.Decimals)); switch (transferable.Type) { case NxtTransferableType.Nxt: return(await SendMoney(parameters, addressRs, quantity)); case NxtTransferableType.Asset: return(await TransferAsset(addressRs, transferable.Id, quantity, parameters)); case NxtTransferableType.Currency: return(await TransferCurrency(addressRs, transferable.Id, quantity, parameters)); default: throw new ArgumentException($"Unsupported NxtTransferableType: {transferable.Type}", nameof(transferable)); } }
private async Task SendTransferableRecipientMessage(SlackUser slackUser, string recipientUserId, NxtTransferable transferable, NxtAccount recipientAccount, decimal amount) { if (transferable.HasRecipientMessage()) { var balance = await nxtConnector.GetBalance(transferable, recipientAccount.NxtAccountRs); if (balance == 0) { var imId = await SlackConnector.GetInstantMessageId(recipientUserId); var message = transferable.RecipientMessage.Replace("{amount}", $"{amount}").Replace("{sender}", $"<@{slackUser.Id}>"); await SlackConnector.SendMessage(imId, message); } } }
private async Task SendTransferableSenderMessage(SlackUser slackUser, string recipient, NxtTransferable transferable, NxtAccount recipientAccount) { if (transferable != Nxt.Singleton) { var balance = await nxtConnector.GetBalance(Nxt.Singleton, recipientAccount.NxtAccountRs); if (balance < 1) { var imId = await SlackConnector.GetInstantMessageId(slackUser.Id); var message = MessageConstants.RecipientDoesNotHaveAnyNxtHint(recipientAccount.SlackId, transferable.Name); await SlackConnector.SendMessage(imId, message); } } }
private async Task Tip(SlackUser slackUser, SlackChannelSession channelSession, string recipient, List <string> slackUserIds, decimal amountToTip, string unit, string comment, NxtTransferable transferable, NxtAccount account) { var isRecipientSlackUser = slackUserIds.Any(); var recipientCount = isRecipientSlackUser ? slackUserIds.Count : 1; if (slackUserIds.Contains(SlackConnector.SelfId)) { await SlackConnector.SendMessage(channelSession.Id, MessageConstants.CantTipBotChannel); return; } if (slackUserIds.Contains(slackUser.Id)) { await SlackConnector.SendMessage(channelSession.Id, MessageConstants.CantTipYourselfChannel); return; } if (!isRecipientSlackUser && !nxtConnector.IsValidAddressRs(recipient)) { await SlackConnector.SendMessage(channelSession.Id, MessageConstants.InvalidAddress); return; } if (account == null) { await SlackConnector.SendMessage(channelSession.Id, MessageConstants.NoAccountChannel); return; } if (!string.IsNullOrEmpty(comment) && comment.Length > 512) { await SlackConnector.SendMessage(channelSession.Id, MessageConstants.CommentTooLongChannel); return; } if (!(await VerifyParameters(transferable, unit, account, channelSession.Id, amountToTip, recipientCount))) { return; } if (isRecipientSlackUser) { foreach (var slackUserId in slackUserIds) { var recipientAccount = await walletRepository.GetAccount(slackUserId); var recipientPublicKey = ""; if (recipientAccount == null) { recipientAccount = await SendTipRecievedInstantMessage(slackUser, slackUserId); recipientPublicKey = recipientAccount.NxtPublicKey; } try { var recipientUserName = SlackConnector.GetUser(slackUserId).Name; var txMessage = MessageConstants.NxtTipTransactionMessage(slackUser.Name, recipientUserName, comment); var txId = await nxtConnector.Transfer(account, recipientAccount.NxtAccountRs, transferable, amountToTip, txMessage, recipientPublicKey); if (recipientCount == 1) { var reply = MessageConstants.TipSentChannel(slackUser.Id, slackUserId, amountToTip, transferable.Name, txId, comment); await SlackConnector.SendMessage(channelSession.Id, reply, false); } else { var imId = await SlackConnector.GetInstantMessageId(slackUserId); var reply = MessageConstants.TipSentDirectMessage(slackUser.Id, amountToTip, transferable.Name, txId); await SlackConnector.SendMessage(imId, reply); } await SendTransferableRecipientMessage(slackUser, slackUserId, transferable, recipientAccount, amountToTip); await SendTransferableSenderMessage(slackUser, slackUserId, transferable, recipientAccount); } catch (NxtException e) { logger.LogError(0, e, e.Message); throw; } } if (recipientCount > 1) { var reply = MessageConstants.MultitipSentChannel(slackUser.Id, recipient, amountToTip, transferable.Name, comment); await SlackConnector.SendMessage(channelSession.Id, reply); } } else { try { var txMessage = MessageConstants.NxtTipTransactionMessage(slackUser.Name, "", comment); var txId = await nxtConnector.Transfer(account, recipient, transferable, amountToTip, txMessage, ""); var reply = MessageConstants.TipToAddressRsSentChannel(slackUser.Id, recipient, amountToTip, transferable.Name, txId, comment); await SlackConnector.SendMessage(channelSession.Id, reply, false); } catch (NxtException e) { logger.LogError(0, e, e.Message); throw; } } }
private async Task <bool> VerifyParameters(NxtTransferable transferable, string unit, NxtAccount account, string slackSessionId, decimal amount, int recipientCount = 1) { if (transferable == null) { await SlackConnector.SendMessage(slackSessionId, MessageConstants.UnknownUnit(unit)); return(false); } amount *= recipientCount; var nxtBalance = await nxtConnector.GetBalance(Nxt.Singleton, account.NxtAccountRs); if (transferable == Nxt.Singleton) { if (nxtBalance >= amount && nxtBalance < amount + 1 && recipientCount == 1) { await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFundsNeedFee(nxtBalance)); return(false); } if (nxtBalance < amount + recipientCount) { await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFunds(nxtBalance, transferable.Name)); return(false); } } else { if (nxtBalance < recipientCount) { await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFundsNeedFee(nxtBalance, recipientCount)); return(false); } var balance = await nxtConnector.GetBalance(transferable, account.NxtAccountRs); if (balance < amount) { await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFunds(balance, transferable.Name)); return(false); } } return(true); }
private void SetupNxtAccount(NxtAccount nxtAccount, decimal balance) { walletRepositoryMock.Setup(r => r.GetAccount(It.Is <string>(slackId => slackId == nxtAccount.SlackId))).ReturnsAsync(nxtAccount); nxtConnectorMock.Setup(c => c.GetBalance(It.Is <NxtTransferable>(t => t == Nxt.Singleton), It.Is <string>(rs => rs == nxtAccount.NxtAccountRs))).ReturnsAsync(balance); SetupTransferable(Nxt.Singleton, balance, nxtAccount.NxtAccountRs); }