Skip to content

Commit

Permalink
Attempt to register session with WaaS
Browse files Browse the repository at this point in the history
  • Loading branch information
BellringerQuinn committed Nov 16, 2023
1 parent f70c871 commit 0d1bac6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Assets/SequenceSDK/WaaS/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class HttpClient
NullValueHandling = NullValueHandling.Ignore
};

public HttpClient()
public HttpClient(string url)
{
_url = url;
this._defaultHeaders = new Dictionary<string, string>();
}

Expand Down
51 changes: 42 additions & 9 deletions Assets/SequenceSDK/WaaS/WaaSLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
using System.Threading.Tasks;
using Amazon.CognitoIdentity.Model;
using Sequence.Authentication;
using Sequence.Extensions;
using Sequence.Utils;
using Sequence.Wallet;
using UnityEngine;

namespace Sequence.WaaS
{
public class WaaSLogin : ILogin
{
public static readonly string WaaSLoginUrl = "https://d14tu8valot5m0.cloudfront.net/rpc/WaaSAuthenticator/";
public static readonly string WaaSLoginUrl = "https://d14tu8valot5m0.cloudfront.net/rpc/WaasAuthenticator";

private AWSConfig _awsConfig;
private string _waasProjectId;
Expand Down Expand Up @@ -88,18 +90,26 @@ public async Task ConnectToWaaS(string idToken)

EthWallet sessionWallet = new EthWallet();

string signedPayled = await PrepareSignedPayload(sessionWallet, idToken, dataKey);
string loginPayload = AssembleLoginPayloadJson(idToken, sessionWallet);
string payloadCiphertext = await PrepareEncryptedPayload(sessionWallet, idToken, dataKey, loginPayload);
string signedPayload = await sessionWallet.SignMessage(loginPayload);

// Todo register session with waas rpc
Debug.LogError("Signed payload: " + signedPayled);
try
{
(string sessionId, string dataWallet) = await RegisterSession(dataKey.Ciphertext.ByteArrayToHexStringWithPrefix(), payloadCiphertext, signedPayload);
Debug.LogError($"Session ID: {sessionId} | Data Wallet: {dataWallet}");
}
catch (Exception e)
{
OnLoginFailed?.Invoke("Error registering session: " + e.Message);
return;
}
}

private async Task<string> PrepareSignedPayload(Wallet.IWallet sessionWallet, string idToken, DataKey dataKey)
private async Task<string> PrepareEncryptedPayload(Wallet.IWallet sessionWallet, string idToken, DataKey dataKey, string loginPayload)
{
string loginPayload = AssembleLoginPayloadJson(idToken, sessionWallet);
byte[] encryptedPayload = Encryptor.AES256CBCEncryption(dataKey.Plaintext, loginPayload);
string signedPayled = await sessionWallet.SignMessage(encryptedPayload);
return signedPayled;
return encryptedPayload.ByteArrayToHexStringWithPrefix();
}

private string AssembleLoginPayloadJson(string idToken, Wallet.IWallet sessionWallet)
Expand All @@ -108,10 +118,18 @@ private string AssembleLoginPayloadJson(string idToken, Wallet.IWallet sessionWa
sessionWallet.GetAddress(), idToken);
string intentJson = JsonUtility.ToJson(intent);
WaaSLoginPayload payload = new WaaSLoginPayload(_waasProjectId, idToken, sessionWallet.GetAddress(),
"UserWallet", intentJson);
"FRIENDLY SESSION WALLET", intentJson);
string payloadJson = JsonUtility.ToJson(payload);
return payloadJson;
}

private async Task<(string, string)> RegisterSession(string encryptedPayloadKey, string payloadCiphertext, string signedPayload)
{
HttpClient client = new HttpClient(WaaSLoginUrl);
RegisterSessionPayload payload = new RegisterSessionPayload(encryptedPayloadKey, payloadCiphertext, signedPayload);
(string sessionId, string dataWallet) = await client.SendRequest<RegisterSessionPayload, (string, string)>("RegisterSession", payload);
return (sessionId, dataWallet);
}
}

[Serializable]
Expand Down Expand Up @@ -169,4 +187,19 @@ public WaaSLoginPayload(string projectId, string idToken, string sessionAddress,
intentJSON = intentJson;
}
}

[Serializable]
public class RegisterSessionPayload
{
public string encryptedPayloadKey;
public string payloadCiphertext;
public string payloadSig;

public RegisterSessionPayload(string encryptedPayloadKey, string payloadCiphertext, string payloadSig)
{
this.encryptedPayloadKey = encryptedPayloadKey;
this.payloadCiphertext = payloadCiphertext;
this.payloadSig = payloadSig;
}
}
}
2 changes: 1 addition & 1 deletion Assets/SequenceSDK/WaaS/WaaSWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class WaaSWallet : IWallet

public WaaSWallet(string jwt)
{
this._httpClient = new HttpClient();
this._httpClient = new HttpClient("https://next-api.sequence.app/rpc/Wallet");
this._address = JwtHelper.GetWalletAddressFromJwt(jwt);
this._httpClient.AddDefaultHeader("Authorization", $"Bearer {jwt}");
}
Expand Down

0 comments on commit 0d1bac6

Please sign in to comment.