From b1ce8ab74952fc4ab350517937d0445faac35a52 Mon Sep 17 00:00:00 2001 From: channa-figure <73041609+channa-figure@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:01:13 -0600 Subject: [PATCH] Add domain extension to get escrow account address (#121) --- CHANGELOG.md | 1 + .../explorer/grpc/extensions/Domain.kt | 11 +++++++++- .../explorer/domain/ExtensionsTest.kt | 22 ++++++++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6964b8a9..c0dcff0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features * IBC denom API #97 * `/ibc/all` +* Add get escrow account address method for ibc #119 ### Improvements * Removed hash conversion #66 diff --git a/service/src/main/kotlin/io/provenance/explorer/grpc/extensions/Domain.kt b/service/src/main/kotlin/io/provenance/explorer/grpc/extensions/Domain.kt index ccfb3e56..a20b4a48 100644 --- a/service/src/main/kotlin/io/provenance/explorer/grpc/extensions/Domain.kt +++ b/service/src/main/kotlin/io/provenance/explorer/grpc/extensions/Domain.kt @@ -1,14 +1,17 @@ package io.provenance.explorer.grpc.extensions +import com.google.common.io.BaseEncoding import com.google.protobuf.Any import com.google.protobuf.ByteString import cosmos.auth.v1beta1.Auth import cosmos.base.query.v1beta1.Pagination import cosmos.crypto.multisig.Keys import io.provenance.explorer.domain.core.logger +import io.provenance.explorer.domain.core.toBech32Data import io.provenance.explorer.domain.extensions.edPubKeyToBech32 import io.provenance.explorer.domain.extensions.secpPubKeyToBech32 import io.provenance.explorer.domain.extensions.toBase64 +import io.provenance.explorer.domain.extensions.toSha256 import io.provenance.explorer.service.prettyRole import io.provenance.marker.v1.Access import io.provenance.marker.v1.MarkerAccount @@ -80,7 +83,13 @@ fun Any.toAddress(hrpPrefix: String) = else -> null.also { logger().error("This typeUrl is not supported as a consensus address: $typeUrl") } } - +//TODO: Once cosmos-sdk implements a grpc endpoint for this we can replace this with grpc Issue: https://github.com/cosmos/cosmos-sdk/issues/9437 +fun getEscrowAccountAddress(portId: String, channelId: String, hrpPrefix: String) : String { + val contents = "${portId}/${channelId}".toByteArray() + var preImage = "ics20-1".encodeToByteArray() + 0x0.toByte() + contents + val hash = preImage.toSha256().copyOfRange(0, 20) + return hash.toBech32Data(hrpPrefix).address +} fun getPaginationBuilder(offset: Int, limit: Int) = diff --git a/service/src/test/kotlin/io/provenance/explorer/domain/ExtensionsTest.kt b/service/src/test/kotlin/io/provenance/explorer/domain/ExtensionsTest.kt index 951f631b..9d5eaabe 100644 --- a/service/src/test/kotlin/io/provenance/explorer/domain/ExtensionsTest.kt +++ b/service/src/test/kotlin/io/provenance/explorer/domain/ExtensionsTest.kt @@ -1,6 +1,16 @@ -//package io.provenance.explorer.domain -// -//class ExtensionsTest { -// -// -//} +package io.provenance.explorer.domain + +import io.provenance.explorer.grpc.extensions.getEscrowAccountAddress +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Tag +import org.junit.jupiter.api.Test + +class ExtensionsTest { + + @Test + @Tag("junit-jupiter") + fun `should return escrow account address with given portId channelId and prefix`() { + val result = getEscrowAccountAddress("transfer", "channel", "cosmos") + assertEquals("cosmos1dm7fargcm8km25nxe6xldj0y0j2dawg8h5s03l", result) + } +}