From 1b0479dd2b54b0c0c4820018fd2d50f2440021ea Mon Sep 17 00:00:00 2001 From: Jason Elie Bou Kheir <5115126+jasonboukheir@users.noreply.github.com> Date: Wed, 13 Jul 2022 22:43:26 -0700 Subject: [PATCH] fix(kmd): fix `KmdAccount` not renewing wallet handle tokens correctly --- .../NodeServices/Kmd/KmdAccount.cs | 61 ++++++------------- .../Kmd/KmdClientTestFixture.cs | 4 +- 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/Runtime/CareBoo.AlgoSdk/NodeServices/Kmd/KmdAccount.cs b/Runtime/CareBoo.AlgoSdk/NodeServices/Kmd/KmdAccount.cs index 2a155290e..cc0e98dae 100644 --- a/Runtime/CareBoo.AlgoSdk/NodeServices/Kmd/KmdAccount.cs +++ b/Runtime/CareBoo.AlgoSdk/NodeServices/Kmd/KmdAccount.cs @@ -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; } /// public Address Address => address; - public async UniTask 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; - } - /// public async UniTask[]> SignTxnsAsync(T[] txns, TxnIndices txnsToSign, CancellationToken cancellationToken = default) where T : ITransaction, IEquatable { - var result = new SignedTxn[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[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>(signResp.SignedTransaction); } + return result; + } + finally + { + await client.ReleaseWalletHandleToken(walletHandleToken); } - return result; } } } diff --git a/Tests/Runtime/CareBoo.AlgoSdk.IntegrationTests/Kmd/KmdClientTestFixture.cs b/Tests/Runtime/CareBoo.AlgoSdk.IntegrationTests/Kmd/KmdClientTestFixture.cs index a71c4a151..7e6a4f02c 100644 --- a/Tests/Runtime/CareBoo.AlgoSdk.IntegrationTests/Kmd/KmdClientTestFixture.cs +++ b/Tests/Runtime/CareBoo.AlgoSdk.IntegrationTests/Kmd/KmdClientTestFixture.cs @@ -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() @@ -77,7 +77,7 @@ protected async UniTask MakePaymentTransaction(ulong a protected async UniTask Sign(T txn) where T : ITransaction, IEquatable { - 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); }