Pull out queryVerified

This commit is contained in:
Simon Warta 2020-08-06 13:29:46 +02:00
parent 0a2f72f493
commit 620785cc3a
2 changed files with 28 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Bech32 } from "@cosmjs/encoding";
import { Bech32, toHex } from "@cosmjs/encoding";
import { Uint64 } from "@cosmjs/math";
import { BaseAccount, decodeAny } from "@cosmjs/proto-signing";
import { Client as TendermintClient } from "@cosmjs/tendermint-rpc";
@ -29,20 +29,11 @@ export class StargateClient {
public async getSequence(address: string): Promise<GetSequenceResult> {
const binAddress = Bech32.decode(address).data;
// 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.queryVerified("acc", accountKey);
const response = await this.tmClient.abciQuery({
// we need the StoreKey for the module, not the module name
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L12
path: "/store/acc/key",
data: accountKey,
prove: false,
});
const { typeUrl, value } = decodeAny(response.value);
const { typeUrl, value } = decodeAny(responseData);
switch (typeUrl) {
case "/cosmos.auth.BaseAccount": {
const { account_number, sequence } = BaseAccount.decode(value);
@ -53,7 +44,6 @@ export class StargateClient {
sequence: uint64FromProto(sequence).toNumber(),
};
}
default:
throw new Error(`Unsupported type: ${typeUrl}`);
}
@ -62,4 +52,28 @@ export class StargateClient {
public disconnect(): void {
this.tmClient.disconnect();
}
private async queryVerified(store: string, key: Uint8Array): Promise<Uint8Array> {
const response = await this.tmClient.abciQuery({
// we need the StoreKey for the module, not the module name
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L12
path: `/store/${store}/key`,
data: key,
prove: true,
});
if (response.code) {
throw new Error(`Query failed with (${response.code}): ${response.log}`);
}
// TODO: better way to compare?
if (toHex(response.key) !== toHex(key)) {
throw new Error(`Response key ${toHex(response.key)} doesn't match query key ${toHex(key)}`);
}
// TODO: implement proof verification
// https://github.com/CosmWasm/cosmjs/issues/347
return response.value;
}
}

View File

@ -8,4 +8,5 @@ export declare class StargateClient {
private constructor();
getSequence(address: string): Promise<GetSequenceResult>;
disconnect(): void;
private queryVerified;
}