Skip to content

Commit

Permalink
Merge pull request #851 from Portkey-Wallet/feature/transacion_email_opt
Browse files Browse the repository at this point in the history
Transaction Email Optimization
  • Loading branch information
Edward-BXS authored Sep 30, 2024
2 parents 2857518 + fe8557a commit da0e458
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ namespace CAServer.CAAccount;
public interface IAppleZkProvider
{
public Task<string> SaveGuardianUserBeforeZkLoginAsync(VerifiedZkLoginRequestDto requestDto);

public Task<AppleUserExtraInfo> GetAppleUserExtraInfo(string accessToken);
}
17 changes: 16 additions & 1 deletion src/CAServer.Application/CAAccount/Provider/AppleZkProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<string> SaveGuardianUserBeforeZkLoginAsync(VerifiedZkLoginRequ
CaHash = requestDto.CaHash,
TargetChainId = requestDto.TargetChainId
};
var guardianIdentifier = userInfo.Email.IsNullOrEmpty() ? userInfo.Id : userInfo.Email;
var guardianIdentifier = userInfo.Email.IsNullOrEmpty() ? string.Empty : userInfo.Email;
await _guardianUserProvider.AppendSecondaryEmailInfo(verifyTokenRequestDto, hashInfo.Item1, guardianIdentifier, GuardianIdentifierType.Apple);
var response =
await _verifierServerClient.VerifyAppleTokenAsync(verifyTokenRequestDto, hashInfo.Item1, hashInfo.Item2);
Expand Down Expand Up @@ -202,4 +202,19 @@ private static AppleUserExtraInfo GetUserInfoFromToken(SecurityToken validatedTo

return userInfo;
}

public async Task<AppleUserExtraInfo> GetAppleUserExtraInfo(string accessToken)
{
SecurityToken securityToken;
try
{
securityToken = await ValidateTokenAsync(accessToken);
}
catch (Exception e)
{
_logger.LogError(e, "GetAppleUserExtraInfo ValidateTokenAsync failed");
return null;
}
return GetUserInfoFromToken(securityToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task<string> SaveGuardianUserBeforeZkLoginAsync(VerifiedZkLoginRequ
CaHash = requestDto.CaHash,
TargetChainId = requestDto.TargetChainId
};
var guardianIdentifier = userInfo.Email.IsNullOrEmpty() ? userInfo.Id : userInfo.Email;
var guardianIdentifier = userInfo.Email.IsNullOrEmpty() ? string.Empty : userInfo.Email;
await _guardianUserProvider.AppendSecondaryEmailInfo(verifyTokenRequestDto, hashInfo.Item1, guardianIdentifier, GuardianIdentifierType.Google);
var response =
await _verifierServerClient.VerifyGoogleTokenAsync(verifyTokenRequestDto, hashInfo.Item1, hashInfo.Item2);
Expand Down
16 changes: 11 additions & 5 deletions src/CAServer.Application/Verifier/VerifierAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using AElf.Types;
using CAServer.AccountValidator;
using CAServer.AppleVerify;
using CAServer.CAAccount;
using CAServer.CAAccount.Dtos;
using CAServer.CAAccount.Provider;
using CAServer.Cache;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class VerifierAppService : CAServerAppService, IVerifierAppService
private readonly ICAAccountProvider _accountProvider;
private readonly SendVerifierCodeRequestLimitOptions _sendVerifierCodeRequestLimitOption;
private readonly IdentityUserManager _userManager;
private readonly IAppleZkProvider _appleZkProvider;

private const string SendVerifierCodeInterfaceRequestCountCacheKey =
"SendVerifierCodeInterfaceRequestCountCacheKey";
Expand All @@ -79,7 +81,8 @@ public VerifierAppService(IEnumerable<IAccountValidator> accountValidator, IObje
ICacheProvider cacheProvider, IContractProvider contractProvider, IHttpClientService httpClientService,
IDistributedCache<AppleKeys> distributedCache,
ICAAccountProvider accountProvider,
IdentityUserManager userManager)
IdentityUserManager userManager,
IAppleZkProvider appleZkProvider)
{
_accountValidator = accountValidator;
_objectMapper = objectMapper;
Expand All @@ -96,6 +99,7 @@ public VerifierAppService(IEnumerable<IAccountValidator> accountValidator, IObje
_distributedCache = distributedCache;
_accountProvider = accountProvider;
_userManager = userManager;
_appleZkProvider = appleZkProvider;
}

public async Task<VerifierServerResponse> SendVerificationRequestAsync(SendVerificationRequestInput input)
Expand Down Expand Up @@ -272,7 +276,9 @@ public async Task<VerificationCodeResponse> VerifyAppleTokenAsync(VerifyTokenReq
{
var userId = GetAppleUserId(requestDto.AccessToken);
var hashInfo = await GetSaltAndHashAsync(userId);
await AppendSecondaryEmailInfo(requestDto, hashInfo.Item1, userId, GuardianIdentifierType.Apple);
var userExtraInfo = await _appleZkProvider.GetAppleUserExtraInfo(requestDto.AccessToken);
var guardianIdentifier = userExtraInfo == null || userExtraInfo.Email.IsNullOrEmpty() ? string.Empty : userExtraInfo?.Email;
await AppendSecondaryEmailInfo(requestDto, hashInfo.Item1, guardianIdentifier, GuardianIdentifierType.Apple);
var response =
await _verifierServerClient.VerifyAppleTokenAsync(requestDto, hashInfo.Item1, hashInfo.Item2);
if (!response.Success)
Expand Down Expand Up @@ -320,7 +326,7 @@ public async Task<VerificationCodeResponse> VerifyTwitterTokenAsync(VerifyTokenR
await StatisticTwitterAsync(userId);

var hashInfo = await GetSaltAndHashAsync(userId);
await AppendSecondaryEmailInfo(requestDto, hashInfo.Item1, userId, GuardianIdentifierType.Twitter);
await AppendSecondaryEmailInfo(requestDto, hashInfo.Item1, string.Empty, GuardianIdentifierType.Twitter);
var response =
await _verifierServerClient.VerifyTwitterTokenAsync(requestDto, hashInfo.Item1, hashInfo.Item2);
if (!response.Success)
Expand Down Expand Up @@ -456,7 +462,7 @@ public async Task<VerificationCodeResponse> VerifyTelegramTokenAsync(VerifyToken
var userId = GetTelegramUserId(requestDto.AccessToken);
_logger.LogDebug("TeleGram userid is {uid}",userId);
var hashInfo = await GetSaltAndHashAsync(userId);
await AppendSecondaryEmailInfo(requestDto, hashInfo.Item1, userId, GuardianIdentifierType.Telegram);
await AppendSecondaryEmailInfo(requestDto, hashInfo.Item1, string.Empty, GuardianIdentifierType.Telegram);
var response =
await _verifierServerClient.VerifyTelegramTokenAsync(requestDto, hashInfo.Item1, hashInfo.Item2);
if (!response.Success)
Expand Down Expand Up @@ -502,7 +508,7 @@ public async Task<VerificationCodeResponse> VerifyFacebookTokenAsync(VerifyToken
{
var facebookUser = await GetFacebookUserInfoAsync(requestDto);
var userSaltAndHash = await GetSaltAndHashAsync(facebookUser.Id);
var guardianIdentifier = facebookUser.Email.IsNullOrEmpty() ? facebookUser.Id : facebookUser.Email;
var guardianIdentifier = facebookUser.Email.IsNullOrEmpty() ? string.Empty : facebookUser.Email;
await AppendSecondaryEmailInfo(requestDto, userSaltAndHash.Item1, guardianIdentifier, GuardianIdentifierType.Facebook);
requestDto.SecondaryEmail = facebookUser.Email.IsNullOrEmpty() ? requestDto.SecondaryEmail : facebookUser.Email;
var response =
Expand Down
65 changes: 64 additions & 1 deletion src/CAServer.Application/Verifier/VerifierServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
using CAServer.Options;
using CAServer.Security.Dtos;
using CAServer.Settings;
using CAServer.Tokens;
using CAServer.Tokens.Cache;
using CAServer.Tokens.Dtos;
using CAServer.Tokens.Provider;
using CAServer.Verifier.Dtos;
using CAVerifierServer.Account;
using Microsoft.Extensions.Logging;
Expand All @@ -23,6 +27,7 @@
using Portkey.Contracts.CA;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ObjectMapping;

namespace CAServer.Verifier;

Expand All @@ -36,13 +41,17 @@ public class VerifierServerClient : IDisposable, IVerifierServerClient, ISinglet
private readonly ChainOptions _chainOptions;
private readonly IIpInfoAppService _ipInfoAppService;
private readonly IContractProvider _contractProvider;
private readonly ITokenProvider _tokenProvider;
private readonly ITokenCacheProvider _tokenCacheProvider;
private readonly IObjectMapper _objectMapper;

public VerifierServerClient(IOptionsSnapshot<AdaptableVariableOptions> adaptableVariableOptions,
IGetVerifierServerProvider getVerifierServerProvider,
ILogger<VerifierServerClient> logger,
IHttpClientFactory httpClientFactory, IAccelerateManagerProvider accelerateManagerProvider,
IOptions<ChainOptions> chainOptions, IIpInfoAppService ipInfoAppService,
IContractProvider contractProvider)
IContractProvider contractProvider, ITokenProvider tokenProvider,
ITokenCacheProvider tokenCacheProvider, IObjectMapper objectMapper)
{
_getVerifierServerProvider = getVerifierServerProvider;
_logger = logger;
Expand All @@ -52,6 +61,9 @@ public VerifierServerClient(IOptionsSnapshot<AdaptableVariableOptions> adaptable
_ipInfoAppService = ipInfoAppService;
_chainOptions = chainOptions.Value;
_contractProvider = contractProvider;
_tokenProvider = tokenProvider;
_tokenCacheProvider = tokenCacheProvider;
_objectMapper = objectMapper;
}

private bool _disposed;
Expand Down Expand Up @@ -379,6 +391,9 @@ private async Task<ResponseResultDto<T>> GetResultAsync<T>(VerifyTokenRequestDto
SingleLimit = GetDetailDesc(input.OperationDetails, "singleLimit"),
DailyLimit = GetDetailDesc(input.OperationDetails, "dailyLimit")
};
await AmountHandler(showOperationDetails, input.OperationType, chainId:input.ChainId, symbol:showOperationDetails.Token,
amount:showOperationDetails.Amount, singleLimit:showOperationDetails.SingleLimit, dailyLimit:showOperationDetails.DailyLimit);
ToAddressHandler(showOperationDetails, showOperationDetails.ToAddress);
var showOperationDetailsJson = JsonConvert.SerializeObject(showOperationDetails);
var result = await GetResultFromVerifierAsync<T>(url, input.AccessToken, identifierHash, salt,
input.OperationType,
Expand All @@ -388,6 +403,54 @@ private async Task<ResponseResultDto<T>> GetResultAsync<T>(VerifyTokenRequestDto
return result;
}

private static void ToAddressHandler(ShowOperationDetailsDto showOperationDetails, string toAddress)
{
if (toAddress.IsNullOrEmpty())
{
return ;
}

var length = toAddress.Length / 2;
showOperationDetails.ToAddress = string.Concat(toAddress.AsSpan(0, length), "\n", toAddress.AsSpan(length));
}

private async Task AmountHandler(ShowOperationDetailsDto showOperationDetailsDto, OperationType operationType,
string chainId, string symbol, string amount, string singleLimit, string dailyLimit)
{
if (chainId.IsNullOrEmpty() || symbol.IsNullOrEmpty() || OperationType.GuardianApproveTransfer.Equals(operationType))
{
return ;
}

if (amount.IsNullOrEmpty() && singleLimit.IsNullOrEmpty() && dailyLimit.IsNullOrEmpty())
{
return;
}
var tokenInfoDto = await GetTokenInfoAsync(chainId, symbol);
showOperationDetailsDto.Amount = CalculationHelper.GetAmountInUsd(amount, tokenInfoDto.Decimals);
showOperationDetailsDto.SingleLimit = CalculationHelper.GetAmountInUsd(singleLimit, tokenInfoDto.Decimals);
showOperationDetailsDto.DailyLimit = CalculationHelper.GetAmountInUsd(dailyLimit, tokenInfoDto.Decimals);
}
private async Task<GetTokenInfoDto> GetTokenInfoAsync(string chainId, string symbol)
{
IndexerTokens dto = null;
try
{
dto = await _tokenProvider.GetTokenInfosAsync(chainId, symbol.Trim().ToUpper(), string.Empty, 0, 1);
}
catch (Exception e)
{
_logger.LogError(e, "_tokenProvider GetTokenInfosAsync failed");
}
var tokenInfo = dto?.TokenInfo?.FirstOrDefault();
if (tokenInfo == null)
{
return await _tokenCacheProvider.GetTokenInfoAsync(chainId, symbol, TokenType.Token);
}

return _objectMapper.Map<IndexerToken, GetTokenInfoDto>(tokenInfo);
}

private async Task<ResponseResultDto<T>> GetResultFromVerifierAsync<T>(string url,
string accessToken, string identifierHash, string salt,
OperationType verifierCodeOperationType, string chainId, string operationDetails, string secondaryEmail, string showOperationDetails)
Expand Down

0 comments on commit da0e458

Please sign in to comment.