stargate: Use proto-signing pubkey encoding helpers

This commit is contained in:
willclarktech 2020-10-15 12:46:27 +02:00
parent 56bbc15491
commit 851192aa44
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 34 additions and 56 deletions

View File

@ -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),
});

View File

@ -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);

View File

@ -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);

View File

@ -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);