Skip to content

Commit

Permalink
flexibility improvements for signing transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
celloman committed Nov 7, 2023
1 parent 34ae492 commit 5b9309e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ subprojects {
configurations.forEach { it.exclude("org.slf4j", "slf4j-api") }
val artifactName = "pb-grpc-$name-kotlin"

tasks.withType<PublishToMavenLocal> {
signing.isRequired = false
}

configure<PublishingExtension> {
publications {
create<MavenPublication>("maven") {
Expand Down
14 changes: 12 additions & 2 deletions client-common/src/main/kotlin/io/provenance/client/grpc/BaseReq.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ interface Signer {
fun sign(data: ByteArray): ByteArray
}

typealias CustomUnpackAccount = (Any.() -> Auth.BaseAccount)
typealias CustomizeSignerInfo = (SignerInfo.() -> SignerInfo)
data class BaseReqSigner(
val signer: Signer,
val sequenceOffset: Int = 0,
val account: Auth.BaseAccount? = null
val account: Auth.BaseAccount? = null,
private val customizeSignerInfo: CustomizeSignerInfo? = null,
val unpackAccount: CustomUnpackAccount? = null,
) {

fun buildSignerInfo(): SignerInfo =
SignerInfo.newBuilder()
.setPublicKey(Any.pack(this.signer.pubKey(), ""))
Expand All @@ -34,7 +39,12 @@ data class BaseReqSigner(
.setSingle(Single.newBuilder().setModeValue(SignMode.SIGN_MODE_DIRECT_VALUE))
)
.setSequence(this.account!!.sequence + this.sequenceOffset)
.build()
.build().let {
customizeSignerInfo?.invoke(it) ?: it
}

fun withCustomizeSignerInfo(customizeSignerInfo: CustomizeSignerInfo) = copy(customizeSignerInfo = customizeSignerInfo)
fun withUnpackAccount(unpackAccount: CustomUnpackAccount) = copy(unpackAccount = unpackAccount)
}

data class BaseReq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.provenance.client.coroutines

import com.google.protobuf.Any
import com.google.protobuf.ByteString
import cosmos.auth.v1beta1.Auth.BaseAccount
import cosmos.auth.v1beta1.QueryGrpcKt
import cosmos.auth.v1beta1.QueryOuterClass
import cosmos.base.tendermint.v1beta1.getLatestBlockRequest
Expand Down Expand Up @@ -82,10 +84,8 @@ open class PbCoroutinesClient(
feePayer: String? = null,
): BaseReq =
signers.map {
BaseReqSigner(
signer = it.signer,
sequenceOffset = it.sequenceOffset,
account = it.account ?: this.authClient.getBaseAccount(it.signer.address())
it.copy(
account = it.account ?: this.authClient.getBaseAccount(it.signer.address(), unpackAccount = it.unpackAccount)
)
}.let {
BaseReq(
Expand Down Expand Up @@ -173,7 +173,7 @@ open class PbCoroutinesClient(
.setMode(actualMode)
.build()
).let { res ->
if (simulateBlock) {
if (simulateBlock && res.txResponse.code == 0) {
val timeoutHeight = providedTimeoutHeight.takeIf { it > 0 } ?: (latestHeight() + 10) // default to 10 block timeout for polling if no height set
val txHash = res.txResponse.txhash
do {
Expand Down Expand Up @@ -204,12 +204,13 @@ open class PbCoroutinesClient(
* 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.
* @return [cosmos.auth.v1beta1.Auth.BaseAccount] or throw [IllegalArgumentException] if the account type is not supported.
* @return [BaseAccount] or throw [IllegalArgumentException] if the account type is not supported.
*/
suspend fun QueryGrpcKt.QueryCoroutineStub.getBaseAccount(bech32Address: String): cosmos.auth.v1beta1.Auth.BaseAccount =
suspend fun QueryGrpcKt.QueryCoroutineStub.getBaseAccount(bech32Address: String, unpackAccount: (Any.() -> BaseAccount)? = null): BaseAccount =
account(QueryOuterClass.QueryAccountRequest.newBuilder().setAddress(bech32Address).build()).account.run {
when {
this.`is`(cosmos.auth.v1beta1.Auth.BaseAccount::class.java) -> unpack(cosmos.auth.v1beta1.Auth.BaseAccount::class.java)
unpackAccount != null -> unpackAccount()
this.`is`(BaseAccount::class.java) -> unpack(BaseAccount::class.java)
else -> throw IllegalArgumentException("Account type not handled:$typeUrl")
}
}
Expand Down

0 comments on commit 5b9309e

Please sign in to comment.