diff --git a/Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs b/Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs new file mode 100644 index 00000000..be59c17b --- /dev/null +++ b/Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs @@ -0,0 +1,15 @@ +using System; + +namespace Sequence.WaaS +{ + [Serializable] + public class DropSessionReturn + { + public bool ok { get; private set; } + + public DropSessionReturn(bool ok) + { + this.ok = ok; + } + } +} \ No newline at end of file diff --git a/Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs.meta b/Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs.meta new file mode 100644 index 00000000..0ad38813 --- /dev/null +++ b/Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d461c58bac1144fd91b1b4f727c1020f +timeCreated: 1700767062 \ No newline at end of file diff --git a/Assets/SequenceSDK/WaaS/IntentSender.cs b/Assets/SequenceSDK/WaaS/IntentSender.cs index 6dd01c80..748136b7 100644 --- a/Assets/SequenceSDK/WaaS/IntentSender.cs +++ b/Assets/SequenceSDK/WaaS/IntentSender.cs @@ -40,34 +40,23 @@ public async Task SendIntent(T2 args) Debug.Log($"Intent Payload: {intentPayload}"); string sendIntentPayload = AssembleSendIntentPayload(intentPayload); Debug.Log($"Send intent payload: {sendIntentPayload}"); - string payloadCiphertext = await PrepareEncryptedPayload(_dataKey, sendIntentPayload); - string signedPayload = await _sessionWallet.SignMessage(sendIntentPayload); - WaaSPayload intent = new WaaSPayload(_dataKey.Ciphertext.ByteArrayToHexStringWithPrefix(), payloadCiphertext, signedPayload); - Dictionary headers = new Dictionary(); - headers.Add("X-Sequence-Tenant", _waasProjectId.ToString()); - if (typeof(T) == typeof(TransactionReturn)) - { - var transactionReturn = await SendTransactionIntent(intent, headers); - return (T)(object)transactionReturn; - } - - IntentReturn result = await _httpClient.SendRequest>("SendIntent", intent, headers); + IntentReturn result = await PostIntent>(sendIntentPayload, "SendIntent"); return result.data; } - private async Task SendTransactionIntent(WaaSPayload intent, + private async Task> SendTransactionIntent(WaaSPayload intent, Dictionary headers) { IntentReturn result = await _httpClient.SendRequest>("SendIntent", intent, headers); if (result.code == SuccessfulTransactionReturn.IdentifyingCode) { SuccessfulTransactionReturn successfulTransactionReturn = JsonConvert.DeserializeObject(result.data.ToString()); - return successfulTransactionReturn; + return new IntentReturn(result.code, successfulTransactionReturn); } else if (result.code == FailedTransactionReturn.IdentifyingCode) { FailedTransactionReturn failedTransactionReturn = JsonConvert.DeserializeObject(result.data.ToString()); - return failedTransactionReturn; + return new IntentReturn(result.code, failedTransactionReturn); } else { @@ -106,13 +95,24 @@ public async Task DropSession(string dropSessionId) { DropSessionArgs args = new DropSessionArgs(SessionId, dropSessionId); string payload = JsonConvert.SerializeObject(args); + var result = await PostIntent(payload, "DropSession"); + return result.ok; + } + + public async Task PostIntent(string payload, string path) + { string payloadCiphertext = await PrepareEncryptedPayload(_dataKey, payload); string signedPayload = await _sessionWallet.SignMessage(payload); WaaSPayload intent = new WaaSPayload(_dataKey.Ciphertext.ByteArrayToHexStringWithPrefix(), payloadCiphertext, signedPayload); Dictionary headers = new Dictionary(); headers.Add("X-Sequence-Tenant", _waasProjectId.ToString()); - IntentReturn result = await _httpClient.SendRequest>("DropSession", intent, headers); - return result.data; + if (typeof(T) == typeof(IntentReturn)) + { + var transactionReturn = await SendTransactionIntent(intent, headers); + return (T)(object)transactionReturn; + } + T result = await _httpClient.SendRequest(path, intent, headers); + return result; } } } \ No newline at end of file diff --git a/Assets/SequenceSDK/WaaS/WaaSLogin.cs b/Assets/SequenceSDK/WaaS/WaaSLogin.cs index e1adfb79..8242e8cd 100644 --- a/Assets/SequenceSDK/WaaS/WaaSLogin.cs +++ b/Assets/SequenceSDK/WaaS/WaaSLogin.cs @@ -14,7 +14,7 @@ namespace Sequence.WaaS { public class WaaSLogin : ILogin { - public static readonly string WaaSLoginUrl = "https://d14tu8valot5m0.cloudfront.net/rpc/WaasAuthenticator"; + public const string WaaSWithAuthUrl = "https://d14tu8valot5m0.cloudfront.net/rpc/WaasAuthenticator"; private AWSConfig _awsConfig; private int _waasProjectId; @@ -115,13 +115,18 @@ public async Task ConnectToWaaS(string idToken) EthWallet sessionWallet = new EthWallet(); + IntentSender sender = new IntentSender( + new HttpClient(WaaSWithAuthUrl), + dataKey, + sessionWallet, + "Unknown", + _waasProjectId, + _waasVersion); string loginPayload = AssembleLoginPayloadJson(idToken, sessionWallet); - string payloadCiphertext = await PrepareEncryptedPayload(dataKey, loginPayload); - string signedPayload = await sessionWallet.SignMessage(loginPayload); try { - RegisterSessionResponse registerSessionResponse = await RegisterSession(dataKey.Ciphertext.ByteArrayToHexStringWithPrefix(), payloadCiphertext, signedPayload); + RegisterSessionResponse registerSessionResponse = await sender.PostIntent(loginPayload, "RegisterSession"); string sessionId = registerSessionResponse.session.id; string walletAddress = registerSessionResponse.data.wallet; OnLoginSuccess?.Invoke(sessionId, walletAddress); @@ -135,12 +140,6 @@ public async Task ConnectToWaaS(string idToken) } } - private async Task PrepareEncryptedPayload(DataKey dataKey, string loginPayload) - { - byte[] encryptedPayload = Encryptor.AES256CBCEncryption(dataKey.Plaintext, loginPayload); - return encryptedPayload.ByteArrayToHexStringWithPrefix(); - } - private string AssembleLoginPayloadJson(string idToken, Wallet.IWallet sessionWallet) { WaaSLoginIntent intent = new WaaSLoginIntent(_waasVersion, WaaSLoginIntent.Packet.OpenSessionCode, @@ -151,16 +150,5 @@ private string AssembleLoginPayloadJson(string idToken, Wallet.IWallet sessionWa string payloadJson = JsonUtility.ToJson(payload); return payloadJson; } - - private async Task RegisterSession(string encryptedPayloadKey, string payloadCiphertext, string signedPayload) - { - HttpClient client = new HttpClient(WaaSLoginUrl); - WaaSPayload payload = new WaaSPayload(encryptedPayloadKey, payloadCiphertext, signedPayload); - RegisterSessionResponse response = await client.SendRequest("RegisterSession", payload, new Dictionary() - { - {"X-Sequence-Tenant", "9"}, - }); - return response; - } } } \ No newline at end of file diff --git a/Assets/SequenceSDK/WaaS/WaaSWallet.cs b/Assets/SequenceSDK/WaaS/WaaSWallet.cs index a615dcdb..1270d785 100644 --- a/Assets/SequenceSDK/WaaS/WaaSWallet.cs +++ b/Assets/SequenceSDK/WaaS/WaaSWallet.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Sequence.Authentication; using Sequence.Wallet; +using UnityEngine; namespace Sequence.WaaS { @@ -16,7 +17,7 @@ public WaaSWallet(Address address, string sessionId, EthWallet sessionWallet, Da { _address = address; _httpClient = new HttpClient("https://d14tu8valot5m0.cloudfront.net/rpc/WaasWallet"); - _intentSender = new IntentSender(new HttpClient("https://d14tu8valot5m0.cloudfront.net/rpc/WaasAuthenticator"), awsDataKey, sessionWallet, sessionId, waasProjectId, waasVersion); + _intentSender = new IntentSender(new HttpClient(WaaSLogin.WaaSWithAuthUrl), awsDataKey, sessionWallet, sessionId, waasProjectId, waasVersion); } public Task CreatePartner(CreatePartnerArgs args, Dictionary headers = null) @@ -121,7 +122,14 @@ public async Task SendTransaction(SendTransactionArgs args) public async Task DropSession(string dropSessionId) { var result = await _intentSender.DropSession(dropSessionId); - OnDropSessionComplete?.Invoke(dropSessionId); + if (result) + { + OnDropSessionComplete?.Invoke(dropSessionId); + } + else + { + Debug.LogError("Failed to drop session: " + dropSessionId); + } return result; }