From 851192aa4464417734aa9268ebe6a91864ee2cfb Mon Sep 17 00:00:00 2001 From: willclarktech Date: Thu, 15 Oct 2020 12:46:27 +0200 Subject: [PATCH] stargate: Use proto-signing pubkey encoding helpers --- packages/stargate/src/queries/auth.spec.ts | 25 +++++-------------- .../src/stargateclient.searchtx.spec.ts | 23 +++++++++-------- packages/stargate/src/stargateclient.spec.ts | 23 +++++++++-------- packages/stargate/src/stargateclient.ts | 19 ++------------ 4 files changed, 34 insertions(+), 56 deletions(-) diff --git a/packages/stargate/src/queries/auth.spec.ts b/packages/stargate/src/queries/auth.spec.ts index ad9ad3d6..046c723b 100644 --- a/packages/stargate/src/queries/auth.spec.ts +++ b/packages/stargate/src/queries/auth.spec.ts @@ -1,15 +1,14 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { fromBase64 } from "@cosmjs/encoding"; +import { encodePubkey } from "@cosmjs/proto-signing"; import { Client as TendermintClient } from "@cosmjs/tendermint-rpc"; import { assert } from "@cosmjs/utils"; import Long from "long"; -import { cosmos, google } from "../codec"; +import { google } from "../codec"; import { nonExistentAddress, pendingWithoutSimapp, simapp, unused, validator } from "../testutils.spec"; import { AuthExtension, setupAuthExtension } from "./auth"; import { QueryClient } from "./queryclient"; -const { PubKey } = cosmos.crypto.secp256k1; const { Any } = google.protobuf; async function makeClientWithAuth(rpcUrl: string): Promise<[QueryClient & AuthExtension, TendermintClient]> { @@ -41,16 +40,10 @@ describe("AuthExtension", () => { const account = await client.auth.account(validator.address); assert(account); - const pubkey = PubKey.create({ - key: fromBase64(validator.pubkey.value), - }); - const pubkeyAny = Any.create({ - type_url: "/cosmos.crypto.secp256k1.PubKey", - value: Uint8Array.from(PubKey.encode(pubkey).finish()), - }); + const pubkey = encodePubkey(validator.pubkey); expect(account).toEqual({ address: validator.address, - pubKey: pubkeyAny, + pubKey: Any.create(pubkey), // accountNumber not set sequence: Long.fromNumber(validator.sequence, true), }); @@ -93,16 +86,10 @@ describe("AuthExtension", () => { const account = await client.auth.unverified.account(validator.address); assert(account); - const pubkey = PubKey.create({ - key: fromBase64(validator.pubkey.value), - }); - const pubkeyAny = Any.create({ - type_url: "/cosmos.crypto.secp256k1.PubKey", - value: Uint8Array.from(PubKey.encode(pubkey).finish()), - }); + const pubkey = encodePubkey(validator.pubkey); expect(account).toEqual({ address: validator.address, - pubKey: pubkeyAny, + pubKey: Any.create(pubkey), // accountNumber not set sequence: Long.fromNumber(validator.sequence, true), }); diff --git a/packages/stargate/src/stargateclient.searchtx.spec.ts b/packages/stargate/src/stargateclient.searchtx.spec.ts index 97dca3a4..db42cd89 100644 --- a/packages/stargate/src/stargateclient.searchtx.spec.ts +++ b/packages/stargate/src/stargateclient.searchtx.spec.ts @@ -1,10 +1,16 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { fromBase64 } from "@cosmjs/encoding"; +import { fromBase64, toBase64 } from "@cosmjs/encoding"; import { Coin, coins } from "@cosmjs/launchpad"; -import { DirectSecp256k1Wallet, makeAuthInfo, makeSignBytes, Registry } from "@cosmjs/proto-signing"; +import { + DirectSecp256k1Wallet, + encodePubkey, + makeAuthInfo, + makeSignBytes, + Registry, +} from "@cosmjs/proto-signing"; import { assert, sleep } from "@cosmjs/utils"; -import { cosmos, google } from "./codec"; +import { cosmos } from "./codec"; import { BroadcastTxResponse, isBroadcastTxFailure, @@ -14,8 +20,6 @@ import { import { faucet, makeRandomAddress, pendingWithoutSimapp, simapp, simappEnabled } from "./testutils.spec"; const { TxRaw } = cosmos.tx.v1beta1; -const { PubKey } = cosmos.crypto.secp256k1; -const { Any } = google.protobuf; interface TestTxSend { readonly sender: string; @@ -37,10 +41,9 @@ async function sendTokens( readonly tx: Uint8Array; }> { const [{ address: walletAddress, pubkey: pubkeyBytes }] = await wallet.getAccounts(); - const pubkey = PubKey.create({ key: pubkeyBytes }); - const pubkeyAny = Any.create({ - type_url: "/cosmos.crypto.secp256k1.PubKey", - value: PubKey.encode(pubkey).finish(), + const pubkey = encodePubkey({ + type: "tendermint/PubKeySecp256k1", + value: toBase64(pubkeyBytes), }); const txBodyFields = { typeUrl: "/cosmos.tx.v1beta1.TxBody", @@ -67,7 +70,7 @@ async function sendTokens( }, ]; const gasLimit = 200000; - const authInfoBytes = makeAuthInfo([pubkeyAny], feeAmount, gasLimit, sequence); + const authInfoBytes = makeAuthInfo([pubkey], feeAmount, gasLimit, sequence); const chainId = await client.getChainId(); const signDocBytes = makeSignBytes(txBodyBytes, authInfoBytes, chainId, accountNumber); diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index f3cf719c..b6863423 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -1,10 +1,16 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { fromBase64 } from "@cosmjs/encoding"; -import { DirectSecp256k1Wallet, makeAuthInfo, makeSignBytes, Registry } from "@cosmjs/proto-signing"; +import { fromBase64, toBase64 } from "@cosmjs/encoding"; +import { + DirectSecp256k1Wallet, + encodePubkey, + makeAuthInfo, + makeSignBytes, + Registry, +} from "@cosmjs/proto-signing"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; -import { cosmos, google } from "./codec"; +import { cosmos } from "./codec"; import { assertIsBroadcastTxSuccess, PrivateStargateClient, StargateClient } from "./stargateclient"; import { faucet, @@ -18,8 +24,6 @@ import { } from "./testutils.spec"; const { TxRaw } = cosmos.tx.v1beta1; -const { PubKey } = cosmos.crypto.secp256k1; -const { Any } = google.protobuf; describe("StargateClient", () => { describe("connect", () => { @@ -254,10 +258,9 @@ describe("StargateClient", () => { const client = await StargateClient.connect(simapp.tendermintUrl); const wallet = await DirectSecp256k1Wallet.fromMnemonic(faucet.mnemonic); const [{ address, pubkey: pubkeyBytes }] = await wallet.getAccounts(); - const pubkey = PubKey.create({ key: pubkeyBytes }); - const pubkeyAny = Any.create({ - type_url: "/cosmos.crypto.secp256k1.PubKey", - value: PubKey.encode(pubkey).finish(), + const pubkey = encodePubkey({ + type: "tendermint/PubKeySecp256k1", + value: toBase64(pubkeyBytes), }); const registry = new Registry(); const txBodyFields = { @@ -289,7 +292,7 @@ describe("StargateClient", () => { }, ]; const gasLimit = 200000; - const authInfoBytes = makeAuthInfo([pubkeyAny], feeAmount, gasLimit, sequence); + const authInfoBytes = makeAuthInfo([pubkey], feeAmount, gasLimit, sequence); const chainId = await client.getChainId(); const signDocBytes = makeSignBytes(txBodyBytes, authInfoBytes, chainId, accountNumber); diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index b55360c7..2defde96 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -3,7 +3,6 @@ import { toHex } from "@cosmjs/encoding"; import { Block, Coin, - encodeSecp256k1Pubkey, isSearchByHeightQuery, isSearchByIdQuery, PubKey, @@ -11,11 +10,12 @@ import { SearchTxQuery, } from "@cosmjs/launchpad"; import { Uint53, Uint64 } from "@cosmjs/math"; +import { decodePubkey } from "@cosmjs/proto-signing"; import { broadcastTxCommitSuccess, Client as TendermintClient, QueryString } from "@cosmjs/tendermint-rpc"; import { assert, assertDefined } from "@cosmjs/utils"; import Long from "long"; -import { cosmos, google } from "./codec"; +import { cosmos } from "./codec"; import { AuthExtension, BankExtension, QueryClient, setupAuthExtension, setupBankExtension } from "./queries"; /** A transaction that is indexed as part of the transaction history */ @@ -85,21 +85,6 @@ function uint64FromProto(input: number | Long | null | undefined): Uint64 { return Uint64.fromString(input.toString()); } -function decodePubkey(pubkey?: google.protobuf.IAny | null): PubKey | null { - if (!pubkey || !pubkey.value) { - return null; - } - - switch (pubkey.type_url) { - case "/cosmos.crypto.secp256k1.PubKey": { - const { key } = cosmos.crypto.secp256k1.PubKey.decode(pubkey.value); - return encodeSecp256k1Pubkey(key); - } - default: - throw new Error("Unknown pubkey type"); - } -} - function accountFromProto(input: cosmos.auth.v1beta1.IBaseAccount): Account { const { address, pubKey, accountNumber, sequence } = input; const pubkey = decodePubkey(pubKey);