Skip to content

Commit

Permalink
fix: build error
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis-portto committed Sep 22, 2022
1 parent 0bdc0ed commit ccafa97
Showing 1 changed file with 4 additions and 188 deletions.
192 changes: 4 additions & 188 deletions Assets/Plugins/Blocto.Sdk/Flow/BloctoWalletProvider.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -47,26 +40,22 @@ public class BloctoWalletProvider : MonoBehaviour, IBloctoWalletProvider, IWalle

private WebRequestUtility _webRequestUtility;

private ResolveUtility _resolveUtility;
private IResolveUtility _resolveUtility;

private IFlowClient _flowClient;

private bool _isCancelRequest;

private Guid _bloctoAppIdentifier;

private Dictionary<bool, Func<FlowTransaction, PreAuthzAdapterResponse, Action<string>, FlowTransaction>> _keyModeProcessMapper;

private Dictionary<string, Action<string, JObject, FlowTransaction, (string IframeUrl, Uri PollingUrl)>> _noncustodialProcessMapper;

/// <summary>
/// Create blocto wallet provider instance
/// </summary>
/// <param name="gameObject">Main gameobject</param>
/// <param name="initialFun">Blocto initial func</param>
/// <param name="bloctoAppIdentifier">Blocto sdk appId</param>
/// <returns>BloctoWalletProvider</returns>
public static BloctoWalletProvider CreateBloctoWalletProvider(Func<Func<GameObject, IFlowClient, ResolveUtility, BloctoWalletProvider>, BloctoWalletProvider> initialFun, Guid bloctoAppIdentifier)
public static BloctoWalletProvider CreateBloctoWalletProvider(Func<Func<GameObject, IFlowClient, IResolveUtility, BloctoWalletProvider>, BloctoWalletProvider> initialFun, Guid bloctoAppIdentifier)
{
var bloctoWalletProvider = initialFun.Invoke((gameObject, flowClient, resolveUtility) => {
var provider = gameObject.AddComponent<BloctoWalletProvider>();
Expand All @@ -79,7 +68,7 @@ public static BloctoWalletProvider CreateBloctoWalletProvider(Func<Func<GameObje

bloctoWalletProvider.gameObject.name = "bloctowalletprovider";
bloctoWalletProvider._isCancelRequest = false;
bloctoWalletProvider._webRequestUtility.BloctoAppId = bloctoAppIdentifier.ToString();
bloctoWalletProvider._bloctoAppIdentifier = bloctoAppIdentifier;

if(Application.platform == RuntimePlatform.Android)
{
Expand All @@ -89,22 +78,6 @@ public static BloctoWalletProvider CreateBloctoWalletProvider(Func<Func<GameObje
return bloctoWalletProvider;
}

public void Start()
{
_keyModeProcessMapper = new Dictionary<bool, Func<FlowTransaction, PreAuthzAdapterResponse, Action<string>, FlowTransaction>>
{
{true, NonCustodialProcess},
{false, CustodialProcess}
};

_noncustodialProcessMapper = new Dictionary<string, Action<string, JObject, FlowTransaction, (string IframeUrl, Uri PollingUrl)>>
{
{"cosigner", Cosigner},
{"non-custodial", Noncustodual}
};

}

/// <summary>
/// User connect wallet get account
/// </summary>
Expand All @@ -129,146 +102,6 @@ public void Authz<TResponse>(string iframeUrl, Uri pollingUri, Action<TResponse>
StartCoroutine(GetService<TResponse>(pollingUri, internalCallback));
}

public virtual FlowTransaction SendTransaction(string preAuthzUrl, FlowTransaction tx, Action internalCallback, Action<string> callback = null)
{
$"PreAuth url: {preAuthzUrl}".ToLog();
var preSignableJObj = _resolveUtility.ResolvePreSignable(ref tx);

$"PreSignable: {JsonConvert.SerializeObject(preSignableJObj)}".ToLog();
var preAuthzResponse = _webRequestUtility.GetResponse<PreAuthzAdapterResponse>(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<string> 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<SignatureResponse>(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<NonCustodialAuthzResponse>(postUrl, "POST", "application/json", signableJObj);
// endpoint = authzResponse.AuthzEndpoint();
// break;
// }
}

Authz<SignatureResponse>(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<SignatureResponse>(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<string> 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<AuthzAdapterResponse>(postUrl, "POST", "application/json", signableJObj);
var endpoint = authzResponse.AuthzEndpoint();

Authz<AuthzAdapterResponse>(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<SignatureResponse>(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<SignatureResponse>(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<NonCustodialAuthzResponse>(postUrl, "POST", "application/json", signableJObj);
endpoint = authzResponse.AuthzEndpoint();
}

/// <summary>
/// SignMessage
/// </summary>
Expand Down Expand Up @@ -325,23 +158,6 @@ protected virtual IEnumerator GetService<TResponse>(Uri pollingUri, Action<TResp
internalCallback.Invoke(response);
}

public async Task<FlowAccount> 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
};
}

/// <summary>
/// Open webview
/// </summary>
Expand Down

0 comments on commit ccafa97

Please sign in to comment.