From ee1c6e0dc01aab2d44e63938d81369037500f889 Mon Sep 17 00:00:00 2001 From: jamisliao Date: Mon, 3 Oct 2022 00:18:08 +0800 Subject: [PATCH] refactor: refactor transaction --- .gitignore | 4 +- .../Blocto.Sdk/Flow/BloctoWalletProvider.cs | 213 ++++++++++-------- .../UnityIosPlugin/Editor/SwiftPostProcess.cs | 23 +- ProjectSettings/ProjectSettings.asset | 6 +- 4 files changed, 146 insertions(+), 100 deletions(-) diff --git a/.gitignore b/.gitignore index 5e6e1ad..a8c189b 100644 --- a/.gitignore +++ b/.gitignore @@ -79,7 +79,7 @@ android install_BurstDebugInformation_DoNotShip ios install_BurstDebugInformation_DoNotShip UserSettings UserSettings -Assets/Plugins/Flow/*/obj -android project_BurstDebugInformation_DoNotShip ios project ios project_BurstDebugInformation_DoNotShip +Assets/Plugins/Flow/Flow.Net.Sdk.Core/obj +android project_BurstDebugInformation_DoNotShip diff --git a/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs b/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs index d48bff2..05744f9 100644 --- a/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs +++ b/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs @@ -97,6 +97,8 @@ public class BloctoWalletProvider : MonoBehaviour, IBloctoWalletProvider, IWalle private string _appSdkDomain = "https://staging.blocto.app/sdk?"; + private Dictionary, FlowTransaction>> _transactionModeMapper; + public bool _isInstalledApp = false; public static string UniversalLink = "default"; @@ -142,6 +144,11 @@ public static BloctoWalletProvider CreateBloctoWalletProvider(Func, FlowTransaction>> + { + { true, CustodialHandler}, + { false, NonCustodialHandler} + }; if(_isInstalledApp) { _authnReturnParsers = new List> @@ -332,6 +339,13 @@ public void Authenticate(string url, Dictionary parameters, Acti } } + /// + /// Send transacton + /// + /// pre authz url + /// flow transaction data + /// complete transaction internal callback + /// complete transaction callback public virtual void SendTransaction(string preAuthzUrl, FlowTransaction tx, Action internalCallback, Action callback = null) { var preSignableJObj = _resolveUtility.ResolvePreSignable(ref tx); @@ -340,101 +354,124 @@ public virtual void SendTransaction(string preAuthzUrl, FlowTransaction tx, Acti var tmpAccount = GetAccount(preAuthzResponse.AuthorizerData.Proposer.Identity.Address).ConfigureAwait(false).GetAwaiter().GetResult(); tx.ProposalKey = GetProposerKey(tmpAccount, preAuthzResponse.AuthorizerData.Proposer.Identity.KeyId); + var action = _transactionModeMapper[isNonCustodial]; + tx = action.Invoke(tx, preAuthzResponse, callback); + } + + /// + /// Handle custodial mode transaction + /// + /// transaction data + /// pre authz response data + /// + /// Flow transaction data + private FlowTransaction CustodialHandler(FlowTransaction tx, PreAuthzAdapterResponse preAuthzResponse, Action callback) + { + var authorization = preAuthzResponse.AuthorizerData.Authorizations.First(); + var postUrl = authorization.AuthzAdapterEndpoint(); + var authorize = authorization.ConvertToFlowAccount(); + var signableJObj = _resolveUtility.ResolveSignable(ref tx, preAuthzResponse.AuthorizerData, authorize).First(); + var authzResponse = WebRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); + var endpoint = authzResponse.AuthzEndpoint(); + + Authz(endpoint.IframeUrl, endpoint.PollingUrl, item => { + var response = item as AuthzAdapterResponse; + var signInfo = response.SignatureInfo(); + if (signInfo.Signature != null) + { + var payloadSignature = tx.PayloadSignatures.First(p => p.Address.Address == signInfo.Address?.ToString().RemoveHexPrefix()); + payloadSignature.Signature = signInfo.Signature?.ToString().StringToBytes().ToArray(); + } + + var payerEndpoint = preAuthzResponse.PayerEndpoint(); + var payerSignable = _resolveUtility.ResolvePayerSignable(ref tx, signableJObj); + var payerSignResponse = WebRequestUtility.GetResponse(payerEndpoint.AbsoluteUri, "POST", "application/json", payerSignable); + signInfo = payerSignResponse.SignatureInfo(); + if (signInfo.Signature != null && signInfo.Address != null) + { + var envelopeSignature = tx.EnvelopeSignatures.First(p => p.Address.Address == signInfo.Address.ToString().RemoveHexPrefix()); + envelopeSignature.Signature = signInfo.Signature?.ToString().StringToBytes().ToArray(); + } + + var txResponse = _flowClient.SendTransactionAsync(tx).ConfigureAwait(false).GetAwaiter().GetResult(); + $"TxId: {txResponse.Id}".ToLog(); + callback?.Invoke(txResponse.Id); + }); + + return tx; + } + + /// + /// Handle non-custodial mode transaction + /// + /// transaction data + /// pre authz response data + /// + /// Flow transaction data + private FlowTransaction NonCustodialHandler(FlowTransaction tx, PreAuthzAdapterResponse preAuthzResponse, Action callback) + { var signableJObj = default(JObject); var endpoint = default((string IframeUrl, Uri PollingUrl)); - if(isNonCustodial) + var authorization = preAuthzResponse.AuthorizerData.Authorizations.First(); + var authorize = authorization.ConvertToFlowAccount(); + var signableJObjs = _resolveUtility.ResolveSignable(ref tx, preAuthzResponse.AuthorizerData, authorize); + tx.PayloadSignatures.Clear(); + + for (var index = 0; index < preAuthzResponse.AuthorizerData.Authorizations.Count; index++) { - var authorization = preAuthzResponse.AuthorizerData.Authorizations.First(); - var authorize = authorization.ConvertToFlowAccount(); - var signableJObjs = _resolveUtility.ResolveSignable(ref tx, preAuthzResponse.AuthorizerData, authorize); - tx.PayloadSignatures.Clear(); - - for (var index = 0; index < preAuthzResponse.AuthorizerData.Authorizations.Count; index++) + signableJObj = signableJObjs[index]; + var postUrl = preAuthzResponse.AuthorizerData.Authorizations[index].AuthzAdapterEndpoint(); + var path = postUrl.Split("?").First().Split("/").Last(); + switch (path) { - signableJObj = signableJObjs[index]; - var postUrl = preAuthzResponse.AuthorizerData.Authorizations[index].AuthzAdapterEndpoint(); - var path = postUrl.Split("?").First().Split("/").Last(); - switch (path) - { - case "cosigner": - var cosigner = WebRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); - tx.PayloadSignatures.Add(new FlowSignature - { - Address = new FlowAddress(cosigner.SignatureInfo().Address.ToString()), - Signature = cosigner.SignatureInfo().Signature.ToString().StringToBytes().ToArray(), - KeyId = Convert.ToUInt32(cosigner.SignatureInfo().KeyId) - }); - break; - case "non-custodial": - var authzResponse = WebRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); - endpoint = authzResponse.AuthzEndpoint(); - break; - } + case "cosigner": + var cosigner = WebRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); + tx.PayloadSignatures.Add(new FlowSignature + { + Address = new FlowAddress(cosigner.SignatureInfo().Address.ToString()), + Signature = cosigner.SignatureInfo().Signature.ToString().StringToBytes().ToArray(), + KeyId = Convert.ToUInt32(cosigner.SignatureInfo().KeyId) + }); + + break; + case "non-custodial": + var authzResponse = WebRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); + endpoint = authzResponse.AuthzEndpoint(); + break; } - - Authz(endpoint.IframeUrl, endpoint.PollingUrl, response => { - var signInfo = response.SignatureInfo(); - if (signInfo.Signature != null) - { - $"Signature info keyId: {signInfo.KeyId}".ToLog(); - tx.PayloadSignatures.Add(new FlowSignature - { - Address = new FlowAddress(signInfo.Address.ToString()), - Signature = signInfo.Signature.ToString().StringToBytes().ToArray(), - KeyId = Convert.ToUInt32(signInfo.KeyId) - }); - } - - var payerEndpoint = preAuthzResponse.PayerEndpoint(); - var payerSignable = _resolveUtility.ResolvePayerSignable(ref tx, signableJObj); - var payerSignResponse = WebRequestUtility.GetResponse(payerEndpoint.AbsoluteUri, "POST", "application/json", payerSignable); - signInfo = payerSignResponse.SignatureInfo(); - if (signInfo.Signature != null && signInfo.Address != null) - { - var envelopeSignature = tx.EnvelopeSignatures.First(p => p.Address.Address == signInfo.Address.ToString().RemoveHexPrefix()); - envelopeSignature.Signature = signInfo.Signature?.ToString().StringToBytes().ToArray(); - } - - var txResponse = _flowClient.SendTransactionAsync(tx).ConfigureAwait(false).GetAwaiter().GetResult(); - $"TxId: {txResponse.Id}".ToLog(); - callback?.Invoke(txResponse.Id); - }); - } - else - { - var authorization = preAuthzResponse.AuthorizerData.Authorizations.First(); - var postUrl = authorization.AuthzAdapterEndpoint(); - var authorize = authorization.ConvertToFlowAccount(); - signableJObj = _resolveUtility.ResolveSignable(ref tx, preAuthzResponse.AuthorizerData, authorize).First(); - var authzResponse = WebRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); - endpoint = authzResponse.AuthzEndpoint(); - - Authz(endpoint.IframeUrl, endpoint.PollingUrl, item => { - var response = item as AuthzAdapterResponse; - var signInfo = response.SignatureInfo(); - if (signInfo.Signature != null) - { - var payloadSignature = tx.PayloadSignatures.First(p => p.Address.Address == signInfo.Address?.ToString().RemoveHexPrefix()); - payloadSignature.Signature = signInfo.Signature?.ToString().StringToBytes().ToArray(); - } - - var payerEndpoint = preAuthzResponse.PayerEndpoint(); - var payerSignable = _resolveUtility.ResolvePayerSignable(ref tx, signableJObj); - var payerSignResponse = WebRequestUtility.GetResponse(payerEndpoint.AbsoluteUri, "POST", "application/json", payerSignable); - signInfo = payerSignResponse.SignatureInfo(); - if (signInfo.Signature != null && signInfo.Address != null) - { - var envelopeSignature = tx.EnvelopeSignatures.First(p => p.Address.Address == signInfo.Address.ToString().RemoveHexPrefix()); - envelopeSignature.Signature = signInfo.Signature?.ToString().StringToBytes().ToArray(); - } - - var txResponse = _flowClient.SendTransactionAsync(tx).ConfigureAwait(false).GetAwaiter().GetResult(); - $"TxId: {txResponse.Id}".ToLog(); - callback?.Invoke(txResponse.Id); - }); } + + Authz(endpoint.IframeUrl, endpoint.PollingUrl, response => { + var signInfo = response.SignatureInfo(); + if (signInfo.Signature != null) + { + $"Signature info keyId: {signInfo.KeyId}".ToLog(); + tx.PayloadSignatures.Add(new FlowSignature + { + Address = new FlowAddress(signInfo.Address.ToString()), + Signature = signInfo.Signature.ToString().StringToBytes().ToArray(), + KeyId = Convert.ToUInt32(signInfo.KeyId) + }); + } + + var payerEndpoint = preAuthzResponse.PayerEndpoint(); + var payerSignable = _resolveUtility.ResolvePayerSignable(ref tx, signableJObj); + var payerSignResponse = WebRequestUtility.GetResponse(payerEndpoint.AbsoluteUri, "POST", "application/json", payerSignable); + signInfo = payerSignResponse.SignatureInfo(); + if (signInfo.Signature != null && signInfo.Address != null) + { + var envelopeSignature = tx.EnvelopeSignatures.First(p => p.Address.Address == signInfo.Address.ToString().RemoveHexPrefix()); + envelopeSignature.Signature = signInfo.Signature?.ToString().StringToBytes().ToArray(); + } + + var txResponse = _flowClient.SendTransactionAsync(tx).ConfigureAwait(false).GetAwaiter().GetResult(); + $"TxId: {txResponse.Id}".ToLog(); + callback?.Invoke(txResponse.Id); + }); + + return tx; } - + /// /// Get authorizer signature /// diff --git a/Assets/Plugins/iOS/UnityIosPlugin/Editor/SwiftPostProcess.cs b/Assets/Plugins/iOS/UnityIosPlugin/Editor/SwiftPostProcess.cs index f815dd4..c2a4b7a 100644 --- a/Assets/Plugins/iOS/UnityIosPlugin/Editor/SwiftPostProcess.cs +++ b/Assets/Plugins/iOS/UnityIosPlugin/Editor/SwiftPostProcess.cs @@ -20,13 +20,14 @@ public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath) var proj = new PBXProject(); proj.ReadFromFile(projPath); - var targetGuid = proj.TargetGuidByName(PBXProject.GetUnityTestTargetName()); + // string targetName = "Unity-iPhone"; + var targetName = proj.GetUnityFrameworkTargetGuid(); + // var targetGuid = proj.TargetGuidByName(); + var targetGuid = proj.GetUnityMainTargetGuid(); proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO"); - - proj.SetBuildProperty(targetGuid, "SWIFT_OBJC_BRIDGING_HEADER", "Libraries/Plugins/iOS/UnityIosPlugin/Source/UnityPlugin-Bridging-Header.h"); proj.SetBuildProperty(targetGuid, "SWIFT_OBJC_INTERFACE_HEADER_NAME", "UnityFramework-Swift.h"); @@ -65,7 +66,15 @@ public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath) queriesUrlTypeArray?.AddString("blocto-staging"); } - OnProstProcessBuildIOS(buildPath); + var entitlements = new ProjectCapabilityManager(projPath, "Unity-iPhone/Unity-iPhoneReleaseForProfiling.entitlements", "Unity-iPhone"); + entitlements.AddAssociatedDomains(new string[] { "applinks:657f-220-136-194-114.jp.ngrok.io?mode=developer" }); + + //Apply + entitlements.WriteToFile(); + proj.AddCapability(targetGuid, PBXCapabilityType.AssociatedDomains, "Unity-iPhone/iPhoneReleaseForProfiling.entitlements"); + proj.SetBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", "Unity-iPhoneReleaseForProfiling.entitlements"); + + //OnProstProcessBuildIOS(buildPath); plist.WriteToFile(plistPath); proj.WriteToFile(projPath); @@ -79,12 +88,12 @@ private static void OnProstProcessBuildIOS(string pathToBuiltProject) //Default target name. Yours might be different string targetName = "Unity-iPhone"; //Set the entitlements file name to what you want but make sure it has this extension - string entitlementsFileName = "my_app.entitlements"; + string entitlementsFileName = "Unity-iPhoneReleaseForRunning.entitlements"; var entitlements = new ProjectCapabilityManager(pathToBuiltProject + projectPath, entitlementsFileName, targetName); - entitlements.AddAssociatedDomains(new string[] { "applinks:fed9-114-36-191-176.jp.ngrok.io?mode=developer" }); + entitlements.AddAssociatedDomains(new string[] { "applinks:657f-220-136-194-114.jp.ngrok.io?mode=developer" }); //Apply - entitlements.WriteToFile(); + // entitlements.WriteToFile(); // string projectPath = PBXProject.GetPBXProjectPath(path); diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 6e7f05a..2412e3d 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -13,7 +13,7 @@ PlayerSettings: useOnDemandResources: 0 accelerometerFrequency: 60 companyName: Portto - productName: com.blocto.flow.demo + productName: com.blocto.flow.demoapp defaultCursor: {fileID: 0} cursorHotspot: {x: 0, y: 0} m_SplashScreenBackgroundColor: {r: 0.13725491, g: 0.12156863, b: 0.1254902, a: 1} @@ -178,7 +178,7 @@ PlayerSettings: keepLoadedShadersAlive: 0 StripUnusedMeshComponents: 0 VertexChannelCompressionMask: 4054 - iPhoneSdkVersion: 989 + iPhoneSdkVersion: 988 iOSTargetOSVersionString: 14.0 tvOSSdkVersion: 0 tvOSRequireExtendedGameController: 0 @@ -227,7 +227,7 @@ PlayerSettings: metalAPIValidation: 1 iOSRenderExtraFrameOnPause: 0 iosCopyPluginsCodeInsteadOfSymlink: 0 - appleDeveloperTeamID: + appleDeveloperTeamID: 6CLS73YQRQ iOSManualSigningProvisioningProfileID: tvOSManualSigningProvisioningProfileID: iOSManualSigningProvisioningProfileType: 0