Skip to content

Commit

Permalink
fix(kmd): fix KmdAccount not renewing wallet handle tokens correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonboukheir committed Jul 14, 2022
1 parent be125d3 commit 1b0479d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
61 changes: 20 additions & 41 deletions Runtime/CareBoo.AlgoSdk/NodeServices/Kmd/KmdAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,71 +24,50 @@ public struct KmdAccount
[SerializeField]
Address address;

FixedString128Bytes walletHandleToken;

public KmdAccount(
KmdClient client,
FixedString128Bytes walletId,
FixedString128Bytes walletPassword,
Address address,
FixedString128Bytes walletHandleToken = default
Address address
)
{
this.client = client;
this.walletId = walletId;
this.walletPassword = walletPassword;
this.address = address;

this.walletHandleToken = walletHandleToken;
}

/// <inheritdoc />
public Address Address => address;

public async UniTask<FixedString128Bytes> RefreshWalletHandleToken(CancellationToken cancellationToken = default)
{
if (walletHandleToken.Length > 0)
{
var (renewErr, renewResponse) = await client.RenewWalletHandleToken(walletHandleToken).WithCancellation(cancellationToken);
if (renewErr)
{
Debug.LogError(renewErr);
}
}
else
{
var (initErr, initResponse) = await client.InitWalletHandleToken(walletId, walletPassword).WithCancellation(cancellationToken);
if (initErr)
{
Debug.LogError(initErr);
}
walletHandleToken = initResponse.WalletHandleToken;
}
return walletHandleToken;
}

/// <inheritdoc />
public async UniTask<SignedTxn<T>[]> SignTxnsAsync<T>(T[] txns, TxnIndices txnsToSign, CancellationToken cancellationToken = default)
where T : ITransaction, IEquatable<T>
{
var result = new SignedTxn<T>[txns.Length];
var indexEnum = txnsToSign.GetEnumerator();
ErrorResponse signErr;
SignTransactionResponse signResp;
while (indexEnum.MoveNext())
var (initErr, initResponse) = await client.InitWalletHandleToken(walletId, walletPassword)
.WithCancellation(cancellationToken);
initErr.ThrowIfError();
var walletHandleToken = initResponse.WalletHandleToken;
try
{
var i = indexEnum.Current;
(signErr, signResp) = await client.SignTransaction(Address, AlgoApiSerializer.SerializeMessagePack(txns[i]), walletHandleToken, walletPassword);
if (signErr)
{
Debug.LogError(signErr);
}
else
var result = new SignedTxn<T>[txns.Length];
var indexEnum = txnsToSign.GetEnumerator();
ErrorResponse signErr;
SignTransactionResponse signResp;
while (indexEnum.MoveNext())
{
var i = indexEnum.Current;
var serializedTxn = AlgoApiSerializer.SerializeMessagePack(txns[i]);
(signErr, signResp) = await client.SignTransaction(Address, serializedTxn, walletHandleToken, walletPassword);
signErr.ThrowIfError();
result[i] = AlgoApiSerializer.DeserializeMessagePack<SignedTxn<T>>(signResp.SignedTransaction);
}
return result;
}
finally
{
await client.ReleaseWalletHandleToken(walletHandleToken);
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected async UniTask InitWalletHandleToken()
var (keysErr, keysResponse) = await AlgoApiClientSettings.Kmd.ListKeys(walletHandleToken);
AssertOkay(keysErr);
PublicKey = keysResponse.Addresses.First();
kmdAccount = new KmdAccount(AlgoApiClientSettings.Kmd, wallet.Id, WalletPassword, PublicKey, walletHandleToken);
kmdAccount = new KmdAccount(AlgoApiClientSettings.Kmd, wallet.Id, WalletPassword, PublicKey);
}

protected async UniTask ReleaseWalletHandleToken()
Expand Down Expand Up @@ -77,7 +77,7 @@ protected async UniTask<PostTransactionsResponse> MakePaymentTransaction(ulong a

protected async UniTask<byte[]> Sign<T>(T txn) where T : ITransaction, IEquatable<T>
{
var kmdAccount = new KmdAccount(AlgoApiClientSettings.Kmd, wallet.Id, WalletPassword, PublicKey, walletHandleToken);
var kmdAccount = new KmdAccount(AlgoApiClientSettings.Kmd, wallet.Id, WalletPassword, PublicKey);
return await txn.SignWithAsync(kmdAccount);
}

Expand Down

0 comments on commit 1b0479d

Please sign in to comment.