Skip to content

Commit

Permalink
fix(walletconnect): WalletConnectAccount.SignTxns can now be done w…
Browse files Browse the repository at this point in the history
…ithout calling `BeginSession`
  • Loading branch information
jasonboukheir committed Jun 12, 2022
1 parent 0d172ac commit 84bcba9
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions Runtime/CareBoo.AlgoSdk.WalletConnect/WalletConnectAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public class WalletConnectAccount

CancellationTokenSource sessionCancellation;

public WalletConnectAccount(SessionData sessionData)
{
this.sessionData = sessionData;
session = null;
sessionCancellation = null;
}
bool beginningSession;

/// <inheritdoc />
public Address Address => sessionData.Accounts?[0] ?? Address.Empty;
Expand All @@ -30,7 +25,11 @@ public WalletConnectAccount(SessionData sessionData)
public SessionStatus ConnectionStatus => session?.ConnectionStatus ?? default;

/// <inheritdoc />
public SessionData SessionData => sessionData;
public SessionData SessionData
{
get => sessionData;
set => sessionData = value;
}

/// <inheritdoc />
public string BridgeUrl
Expand All @@ -46,13 +45,32 @@ public ClientMeta DappMeta
set => sessionData.DappMeta = value;
}

~WalletConnectAccount()
{
EndSession();
}

/// <inheritdoc />
public async UniTask BeginSession()
{
session = new AlgorandWalletConnectSession(SessionData);
sessionCancellation = new CancellationTokenSource();
await session.Connect(sessionCancellation.Token);
sessionData = session.Save();
if (beginningSession)
{
await UniTask.WaitWhile(() => beginningSession);
return;
}
beginningSession = true;
try
{
sessionCancellation = new CancellationTokenSource();
session = new AlgorandWalletConnectSession(SessionData);
session.OnSessionDisconnect += OnSessionDisconnect;
await session.Connect(sessionCancellation.Token);
sessionData = session.Save();
}
finally
{
beginningSession = false;
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -110,16 +128,16 @@ public async UniTask<SignedTxn<T>[]> SignTxnsAsync<T>(
)
where T : ITransaction, IEquatable<T>
{
CheckSession();

var cancellation = CancellationTokenSource.CreateLinkedTokenSource(sessionCancellation.Token, cancellationToken);

if (txns == null)
throw new ArgumentNullException(nameof(txns));

if (txns.Length == 0 || txns.Length > 16)
throw new ArgumentException("Must have 1-16 transactions", nameof(txns));

if (ConnectionStatus == SessionStatus.None)
await BeginSession();

var cancellation = CancellationTokenSource.CreateLinkedTokenSource(sessionCancellation.Token, cancellationToken);
var walletTxns = new WalletTransaction[txns.Length];
for (var i = 0; i < txns.Length; i++)
{
Expand Down Expand Up @@ -147,5 +165,10 @@ void CheckSession()
if (session == null)
throw new NullReferenceException("Session has not been initialized! Please call " + nameof(BeginSession) + " first.");
}

void OnSessionDisconnect(string reason)
{
sessionData = session.Save();
}
}
}

0 comments on commit 84bcba9

Please sign in to comment.