Skip to content

Commit

Permalink
Refactor to re-use more code when sending messages to WaaS. Fix unmar…
Browse files Browse the repository at this point in the history
…shalling of DropSessionReturn
  • Loading branch information
BellringerQuinn committed Nov 23, 2023
1 parent 252c027 commit 204a034
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 40 deletions.
15 changes: 15 additions & 0 deletions Assets/SequenceSDK/WaaS/DataTypes/ReturnTypes/DropSessionReturn.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions Assets/SequenceSDK/WaaS/IntentSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,23 @@ public async Task<T> SendIntent<T, T2>(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<string, string> headers = new Dictionary<string, string>();
headers.Add("X-Sequence-Tenant", _waasProjectId.ToString());
if (typeof(T) == typeof(TransactionReturn))
{
var transactionReturn = await SendTransactionIntent(intent, headers);
return (T)(object)transactionReturn;
}

IntentReturn<T> result = await _httpClient.SendRequest<WaaSPayload, IntentReturn<T>>("SendIntent", intent, headers);
IntentReturn<T> result = await PostIntent<IntentReturn<T>>(sendIntentPayload, "SendIntent");
return result.data;
}

private async Task<TransactionReturn> SendTransactionIntent(WaaSPayload intent,
private async Task<IntentReturn<TransactionReturn>> SendTransactionIntent(WaaSPayload intent,
Dictionary<string, string> headers)
{
IntentReturn<JObject> result = await _httpClient.SendRequest<WaaSPayload, IntentReturn<JObject>>("SendIntent", intent, headers);
if (result.code == SuccessfulTransactionReturn.IdentifyingCode)
{
SuccessfulTransactionReturn successfulTransactionReturn = JsonConvert.DeserializeObject<SuccessfulTransactionReturn>(result.data.ToString());
return successfulTransactionReturn;
return new IntentReturn<TransactionReturn>(result.code, successfulTransactionReturn);
}
else if (result.code == FailedTransactionReturn.IdentifyingCode)
{
FailedTransactionReturn failedTransactionReturn = JsonConvert.DeserializeObject<FailedTransactionReturn>(result.data.ToString());
return failedTransactionReturn;
return new IntentReturn<TransactionReturn>(result.code, failedTransactionReturn);
}
else
{
Expand Down Expand Up @@ -106,13 +95,24 @@ public async Task<bool> DropSession(string dropSessionId)
{
DropSessionArgs args = new DropSessionArgs(SessionId, dropSessionId);
string payload = JsonConvert.SerializeObject(args);
var result = await PostIntent<DropSessionReturn>(payload, "DropSession");
return result.ok;
}

public async Task<T> PostIntent<T>(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<string, string> headers = new Dictionary<string, string>();
headers.Add("X-Sequence-Tenant", _waasProjectId.ToString());
IntentReturn<bool> result = await _httpClient.SendRequest<WaaSPayload, IntentReturn<bool>>("DropSession", intent, headers);
return result.data;
if (typeof(T) == typeof(IntentReturn<TransactionReturn>))
{
var transactionReturn = await SendTransactionIntent(intent, headers);
return (T)(object)transactionReturn;
}
T result = await _httpClient.SendRequest<WaaSPayload, T>(path, intent, headers);
return result;
}
}
}
30 changes: 9 additions & 21 deletions Assets/SequenceSDK/WaaS/WaaSLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RegisterSessionResponse>(loginPayload, "RegisterSession");
string sessionId = registerSessionResponse.session.id;
string walletAddress = registerSessionResponse.data.wallet;
OnLoginSuccess?.Invoke(sessionId, walletAddress);
Expand All @@ -135,12 +140,6 @@ public async Task ConnectToWaaS(string idToken)
}
}

private async Task<string> 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,
Expand All @@ -151,16 +150,5 @@ private string AssembleLoginPayloadJson(string idToken, Wallet.IWallet sessionWa
string payloadJson = JsonUtility.ToJson(payload);
return payloadJson;
}

private async Task<RegisterSessionResponse> RegisterSession(string encryptedPayloadKey, string payloadCiphertext, string signedPayload)
{
HttpClient client = new HttpClient(WaaSLoginUrl);
WaaSPayload payload = new WaaSPayload(encryptedPayloadKey, payloadCiphertext, signedPayload);
RegisterSessionResponse response = await client.SendRequest<WaaSPayload, RegisterSessionResponse>("RegisterSession", payload, new Dictionary<string, string>()
{
{"X-Sequence-Tenant", "9"},
});
return response;
}
}
}
12 changes: 10 additions & 2 deletions Assets/SequenceSDK/WaaS/WaaSWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Sequence.Authentication;
using Sequence.Wallet;
using UnityEngine;

namespace Sequence.WaaS
{
Expand All @@ -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<CreatePartnerReturn> CreatePartner(CreatePartnerArgs args, Dictionary<string, string> headers = null)
Expand Down Expand Up @@ -121,7 +122,14 @@ public async Task<TransactionReturn> SendTransaction(SendTransactionArgs args)
public async Task<bool> 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;
}

Expand Down

0 comments on commit 204a034

Please sign in to comment.