diff --git a/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs b/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs index 0ed39d5..923a382 100644 --- a/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs +++ b/Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs @@ -1,22 +1,15 @@ using System; using System.Collections; -using System.Collections.Generic; -using System.Linq; using System.Runtime.InteropServices; -using System.Threading.Tasks; using Blocto.Sdk.Core.Model; using Blocto.Sdk.Core.Utility; using Blocto.Sdk.Flow.Utility; -using Flow.FCL.Extensions; using Flow.FCL.Models; using Flow.FCL.Models.Authz; +using Flow.FCL.Utility; using Flow.FCL.WalletProvider; -using Flow.Net.Sdk.Core; using Flow.Net.Sdk.Core.Client; -using Flow.Net.Sdk.Core.Models; using Flow.Net.SDK.Extensions; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using UnityEngine; using UnityEngine.Networking; @@ -47,7 +40,7 @@ public class BloctoWalletProvider : MonoBehaviour, IBloctoWalletProvider, IWalle private WebRequestUtility _webRequestUtility; - private ResolveUtility _resolveUtility; + private IResolveUtility _resolveUtility; private IFlowClient _flowClient; @@ -55,10 +48,6 @@ public class BloctoWalletProvider : MonoBehaviour, IBloctoWalletProvider, IWalle private Guid _bloctoAppIdentifier; - private Dictionary, FlowTransaction>> _keyModeProcessMapper; - - private Dictionary> _noncustodialProcessMapper; - /// /// Create blocto wallet provider instance /// @@ -66,7 +55,7 @@ public class BloctoWalletProvider : MonoBehaviour, IBloctoWalletProvider, IWalle /// Blocto initial func /// Blocto sdk appId /// BloctoWalletProvider - public static BloctoWalletProvider CreateBloctoWalletProvider(Func, BloctoWalletProvider> initialFun, Guid bloctoAppIdentifier) + public static BloctoWalletProvider CreateBloctoWalletProvider(Func, BloctoWalletProvider> initialFun, Guid bloctoAppIdentifier) { var bloctoWalletProvider = initialFun.Invoke((gameObject, flowClient, resolveUtility) => { var provider = gameObject.AddComponent(); @@ -79,7 +68,7 @@ public static BloctoWalletProvider CreateBloctoWalletProvider(Func, FlowTransaction>> - { - {true, NonCustodialProcess}, - {false, CustodialProcess} - }; - - _noncustodialProcessMapper = new Dictionary> - { - {"cosigner", Cosigner}, - {"non-custodial", Noncustodual} - }; - - } - /// /// User connect wallet get account /// @@ -129,146 +102,6 @@ public void Authz(string iframeUrl, Uri pollingUri, Action StartCoroutine(GetService(pollingUri, internalCallback)); } - public virtual FlowTransaction SendTransaction(string preAuthzUrl, FlowTransaction tx, Action internalCallback, Action callback = null) - { - $"PreAuth url: {preAuthzUrl}".ToLog(); - var preSignableJObj = _resolveUtility.ResolvePreSignable(ref tx); - - $"PreSignable: {JsonConvert.SerializeObject(preSignableJObj)}".ToLog(); - var preAuthzResponse = _webRequestUtility.GetResponse(preAuthzUrl, "POST", "application/json", preSignableJObj); - - $"PreAuhtzResponse: {JsonConvert.SerializeObject(preAuthzResponse)}".ToLog(); - var isNonCustodial = preAuthzResponse.AuthorizerData.Authorizations.Any(p => p.Endpoint.ToLower().Contains("cosigner") || p.Endpoint.ToLower().Contains("non-custodial")); - var tmpAccount = GetAccount(preAuthzResponse.AuthorizerData.Proposer.Identity.Address).ConfigureAwait(false).GetAwaiter().GetResult(); - tx.ProposalKey = GetProposerKey(tmpAccount, preAuthzResponse.AuthorizerData.Proposer.Identity.KeyId); - - var signableJObj = default(JObject); - var endpoint = default((string IframeUrl, Uri PollingUrl)); - var process = _keyModeProcessMapper[isNonCustodial]; - tx = process.Invoke(tx, preAuthzResponse, callback); - - return tx; - } - - private FlowTransaction NonCustodialProcess(FlowTransaction tx, PreAuthzAdapterResponse preAuthzResponse, Action callback) - { - var signableJObj = default(JObject); - var endpoint = default((string IframeUrl, Uri PollingUrl)); - 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(); - _noncustodialProcessMapper[path].Invoke(postUrl, signableJObj, tx, endpoint); - $"Tx payload signatures: {JsonConvert.SerializeObject(tx.PayloadSignatures)}".ToLog(); - - // 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; - // } - } - - 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 - { - //// wait frontend fix bug - Address = new FlowAddress(authorization.Identity.Address), - 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; - } - - private FlowTransaction CustodialProcess(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; - } - - private void Cosigner(string postUrl, JObject signableJObj, FlowTransaction tx, (string IframeUrl, Uri PollingUrl) endpoint) - { - 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) - }); - } - - private void Noncustodual(string postUrl, JObject signableJObj, FlowTransaction tx, (string IframeUrl, Uri PollingUrl) endpoint) - { - var authzResponse = _webRequestUtility.GetResponse(postUrl, "POST", "application/json", signableJObj); - endpoint = authzResponse.AuthzEndpoint(); - } - /// /// SignMessage /// @@ -325,23 +158,6 @@ protected virtual IEnumerator GetService(Uri pollingUri, Action GetAccount(string address) - { - var account = _flowClient.GetAccountAtLatestBlockAsync(address); - return await account; - } - - private FlowProposalKey GetProposerKey(FlowAccount account, uint keyId) - { - var proposalKey = account.Keys.First(p => p.Index == keyId); - return new FlowProposalKey - { - Address = account.Address, - KeyId = keyId, - SequenceNumber = proposalKey.SequenceNumber - }; - } - /// /// Open webview ///