Use AuthExtension in StargateClient
This commit is contained in:
parent
4003f5eb65
commit
98b81f8fa8
@ -1,13 +0,0 @@
|
||||
import { google } from "./generated/codecimpl";
|
||||
|
||||
/**
|
||||
* Decodes a serialized [google.protobuf.Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto)
|
||||
* and returns the components.
|
||||
*/
|
||||
export function decodeAny(serialized: Uint8Array): { readonly typeUrl: string; readonly value: Uint8Array } {
|
||||
const envelope = google.protobuf.Any.decode(serialized);
|
||||
return {
|
||||
typeUrl: envelope.type_url,
|
||||
value: envelope.value,
|
||||
};
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
export { omitDefaults } from "./adr27";
|
||||
export { decodeAny } from "./any";
|
||||
export { Coin } from "./msgs";
|
||||
export { cosmosField } from "./decorator";
|
||||
export { Registry } from "./registry";
|
||||
|
||||
10
packages/proto-signing/types/any.d.ts
vendored
10
packages/proto-signing/types/any.d.ts
vendored
@ -1,10 +0,0 @@
|
||||
/**
|
||||
* Decodes a serialized [google.protobuf.Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto)
|
||||
* and returns the components.
|
||||
*/
|
||||
export declare function decodeAny(
|
||||
serialized: Uint8Array,
|
||||
): {
|
||||
readonly typeUrl: string;
|
||||
readonly value: Uint8Array;
|
||||
};
|
||||
1
packages/proto-signing/types/index.d.ts
vendored
1
packages/proto-signing/types/index.d.ts
vendored
@ -1,5 +1,4 @@
|
||||
export { omitDefaults } from "./adr27";
|
||||
export { decodeAny } from "./any";
|
||||
export { Coin } from "./msgs";
|
||||
export { cosmosField } from "./decorator";
|
||||
export { Registry } from "./registry";
|
||||
|
||||
@ -11,13 +11,12 @@ import {
|
||||
SearchTxQuery,
|
||||
} from "@cosmjs/launchpad";
|
||||
import { Uint53, Uint64 } from "@cosmjs/math";
|
||||
import { decodeAny } 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 } from "./generated/codecimpl";
|
||||
import { BankExtension, QueryClient, setupBankExtension } from "./queries";
|
||||
import { AuthExtension, BankExtension, QueryClient, setupAuthExtension, setupBankExtension } from "./queries";
|
||||
|
||||
/** A transaction that is indexed as part of the transaction history */
|
||||
export interface IndexedTx {
|
||||
@ -81,14 +80,16 @@ export function assertIsBroadcastTxSuccess(
|
||||
}
|
||||
}
|
||||
|
||||
function uint64FromProto(input: number | Long): Uint64 {
|
||||
function uint64FromProto(input: number | Long | null | undefined): Uint64 {
|
||||
if (!input) return Uint64.fromNumber(0);
|
||||
return Uint64.fromString(input.toString());
|
||||
}
|
||||
|
||||
function decodeBaseAccount(data: Uint8Array, prefix: string): Account {
|
||||
const { address, pubKey, accountNumber, sequence } = cosmos.auth.BaseAccount.decode(data);
|
||||
function accountFromProto(input: cosmos.auth.IBaseAccount, prefix: string): Account {
|
||||
const { address, pubKey, accountNumber, sequence } = input;
|
||||
// Pubkey is still Amino-encoded in BaseAccount (https://github.com/cosmos/cosmos-sdk/issues/6886)
|
||||
const pubkey = pubKey.length ? decodeAminoPubkey(pubKey) : null;
|
||||
const pubkey = pubKey && pubKey.length ? decodeAminoPubkey(pubKey) : null;
|
||||
assert(address);
|
||||
return {
|
||||
address: Bech32.encode(prefix, address),
|
||||
pubkey: pubkey,
|
||||
@ -115,7 +116,7 @@ export interface PrivateStargateClient {
|
||||
|
||||
export class StargateClient {
|
||||
private readonly tmClient: TendermintClient;
|
||||
private readonly queryClient: QueryClient & BankExtension;
|
||||
private readonly queryClient: QueryClient & AuthExtension & BankExtension;
|
||||
private chainId: string | undefined;
|
||||
|
||||
public static async connect(endpoint: string): Promise<StargateClient> {
|
||||
@ -125,7 +126,7 @@ export class StargateClient {
|
||||
|
||||
private constructor(tmClient: TendermintClient) {
|
||||
this.tmClient = tmClient;
|
||||
this.queryClient = QueryClient.withExtensions(tmClient, setupBankExtension);
|
||||
this.queryClient = QueryClient.withExtensions(tmClient, setupAuthExtension, setupBankExtension);
|
||||
}
|
||||
|
||||
public async getChainId(): Promise<string> {
|
||||
@ -145,21 +146,10 @@ export class StargateClient {
|
||||
}
|
||||
|
||||
public async getAccount(searchAddress: string): Promise<Account | null> {
|
||||
const { prefix, data: binAddress } = Bech32.decode(searchAddress);
|
||||
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32
|
||||
const accountKey = Uint8Array.from([0x01, ...binAddress]);
|
||||
const responseData = await this.queryClient.queryVerified("acc", accountKey);
|
||||
const { prefix } = Bech32.decode(searchAddress);
|
||||
|
||||
if (responseData.length === 0) return null;
|
||||
|
||||
const { typeUrl, value } = decodeAny(responseData);
|
||||
switch (typeUrl) {
|
||||
case "/cosmos.auth.BaseAccount": {
|
||||
return decodeBaseAccount(value, prefix);
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unsupported type: '${typeUrl}'`);
|
||||
}
|
||||
const account = await this.queryClient.auth.account(searchAddress);
|
||||
return account ? accountFromProto(account, prefix) : null;
|
||||
}
|
||||
|
||||
public async getSequence(address: string): Promise<SequenceResponse | null> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user