stargate: Rearrange auth queries

This commit is contained in:
willclarktech
2021-03-30 15:40:39 +02:00
parent 7e94bb54fc
commit 7d038cd216
4 changed files with 36 additions and 34 deletions
+9 -9
View File
@@ -53,23 +53,24 @@ describe("AuthExtension", () => {
tmClient.disconnect();
});
it("returns null for non-existent address", async () => {
it("rejects for non-existent address", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.account(nonExistentAddress);
expect(account).toBeNull();
await expectAsync(client.auth.account(nonExistentAddress)).toBeRejectedWithError(
/account cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd not found/i,
);
tmClient.disconnect();
});
});
describe("unverified", () => {
describe("verified", () => {
describe("account", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.unverified.account(unused.address);
const account = await client.auth.verified.account(unused.address);
assert(account);
expect(account.typeUrl).toEqual("/cosmos.auth.v1beta1.BaseAccount");
@@ -86,7 +87,7 @@ describe("AuthExtension", () => {
it("works for account with pubkey and non-zero sequence", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.unverified.account(validator.delegatorAddress);
const account = await client.auth.verified.account(validator.delegatorAddress);
assert(account);
expect(account.typeUrl).toEqual("/cosmos.auth.v1beta1.BaseAccount");
@@ -103,10 +104,9 @@ describe("AuthExtension", () => {
it("returns null for non-existent address", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.verified.account(nonExistentAddress);
await expectAsync(client.auth.unverified.account(nonExistentAddress)).toBeRejectedWithError(
/account cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd not found/i,
);
expect(account).toBeNull();
tmClient.disconnect();
});
+9 -9
View File
@@ -13,7 +13,7 @@ export interface AuthExtension {
* `typeUrl` and decode the `value` using its own type decoder.
*/
readonly account: (address: string) => Promise<Any | null>;
readonly unverified: {
readonly verified: {
/**
* Returns an account if it exists and `null` otherwise.
*
@@ -35,16 +35,16 @@ export function setupAuthExtension(base: QueryClient): AuthExtension {
return {
auth: {
account: async (address: string) => {
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32
const key = Uint8Array.from([0x01, ...toAccAddress(address)]);
const responseData = await base.queryVerified("acc", key);
if (responseData.length === 0) return null;
return Any.decode(responseData);
const { account } = await queryService.Account({ address: address });
return account ?? null;
},
unverified: {
verified: {
account: async (address: string) => {
const { account } = await queryService.Account({ address: address });
return account ?? null;
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32
const key = Uint8Array.from([0x01, ...toAccAddress(address)]);
const responseData = await base.queryVerified("acc", key);
if (responseData.length === 0) return null;
return Any.decode(responseData);
},
},
},
@@ -276,7 +276,7 @@ export class SigningStargateClient extends StargateClient {
if (explicitSignerData) {
signerData = explicitSignerData;
} else {
const accountFromChain = await this.getAccountUnverified(signerAddress);
const accountFromChain = await this.getAccount(signerAddress);
if (!accountFromChain) {
throw new Error("Account not found");
}
+17 -15
View File
@@ -160,29 +160,31 @@ export class StargateClient {
return status.syncInfo.latestBlockHeight;
}
// this is nice to display data to the user, but is slower
public async getAccount(searchAddress: string): Promise<Account | null> {
const account = await this.forceGetQueryClient().auth.account(searchAddress);
return account ? accountFromAny(account) : null;
try {
const account = await this.forceGetQueryClient().auth.account(searchAddress);
return account ? accountFromAny(account) : null;
} catch (error) {
if (/rpc error: code = NotFound/i.test(error)) {
return null;
}
throw error;
}
}
// if we just need to get the sequence for signing a transaction, let's make this faster
// (no need to wait a block before submitting)
public async getAccountUnverified(searchAddress: string): Promise<Account | null> {
const account = await this.forceGetQueryClient().auth.unverified.account(searchAddress);
public async getAccountVerified(searchAddress: string): Promise<Account | null> {
const account = await this.forceGetQueryClient().auth.verified.account(searchAddress);
return account ? accountFromAny(account) : null;
}
public async getSequence(address: string): Promise<SequenceResponse | null> {
const account = await this.getAccount(address);
if (account) {
return {
accountNumber: account.accountNumber,
sequence: account.sequence,
};
} else {
return null;
}
return account
? {
accountNumber: account.accountNumber,
sequence: account.sequence,
}
: null;
}
public async getBlock(height?: number): Promise<Block> {