Skip to content

Commit

Permalink
Merge pull request #57 from piercetrey-figure/piercetrey-figure/sc-26…
Browse files Browse the repository at this point in the history
…8894/signer-flexibility-improvements

flexibility improvements for signing transactions
  • Loading branch information
celloman authored Nov 17, 2023
2 parents 7c600a7 + 5b9309e commit 9cdbb35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 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 @@ -81,10 +83,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 @@ -203,12 +203,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 9cdbb35

Please sign in to comment.