Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add custom unpacking for accounts in blocking client #64

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.provenance.client.grpc

import com.google.protobuf.Any
import com.google.protobuf.ByteString
import cosmos.auth.v1beta1.Auth.BaseAccount
import cosmos.auth.v1beta1.QueryGrpc
import cosmos.auth.v1beta1.QueryOuterClass
import cosmos.base.tendermint.v1beta1.getLatestBlockRequest
import cosmos.tx.v1beta1.ServiceOuterClass.BroadcastMode
import cosmos.tx.v1beta1.ServiceOuterClass.BroadcastTxRequest
Expand All @@ -17,7 +21,6 @@ import io.grpc.stub.MetadataUtils
import io.provenance.client.common.exceptions.TransactionTimeoutException
import io.provenance.client.common.extensions.txHash
import io.provenance.client.common.gas.GasEstimate
import io.provenance.client.protobuf.extensions.getBaseAccount
import io.provenance.client.protobuf.extensions.getTx
import io.provenance.msgfees.v1.QueryParamsRequest
import java.io.Closeable
Expand Down Expand Up @@ -90,7 +93,7 @@ open class AbstractPbClient<T : ManagedChannelBuilder<T>>(
BaseReqSigner(
signer = it.signer,
sequenceOffset = it.sequenceOffset,
account = it.account ?: this.authClient.getBaseAccount(it.signer.address())
account = it.account ?: this.authClient.getBaseAccount(it.signer.address(), it.unpackAccount)
)
}.let {
BaseReq(
Expand Down Expand Up @@ -268,4 +271,22 @@ fun <S : AbstractStub<S>> S.addBlockHeight(blockHeight: String): S {
return withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata))
}

/**
* Given an address, get the base account associated with it.
*
* See [Accounts](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Accounts.kt#L18).
*
* @param bech32Address The bech32 address to fetch.
* @param unpackAccount A custom unpacking helper for circumstances that utilize non-BaseAccount account types.
* @return [BaseAccount] or throw [IllegalArgumentException] if the account type is not supported.
*/
fun QueryGrpc.QueryBlockingStub.getBaseAccount(bech32Address: String, unpackAccount: (Any.() -> BaseAccount)? = null): BaseAccount =
account(QueryOuterClass.QueryAccountRequest.newBuilder().setAddress(bech32Address).build()).account.run {
when {
unpackAccount != null -> unpackAccount()
this.`is`(BaseAccount::class.java) -> unpack(BaseAccount::class.java)
else -> throw IllegalArgumentException("Account type not handled:$typeUrl")
}
}

typealias PreBroadcastTxHashHandler = (String) -> Unit
Loading