Create higher level Account type

This commit is contained in:
Simon Warta 2020-03-02 14:21:06 +01:00
parent 911c9f0649
commit ed79897cde
15 changed files with 74 additions and 48 deletions

View File

@ -164,7 +164,7 @@ export class CosmWasmConnection implements BlockchainConnection {
const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.addressPrefix) : query.address;
const bankAccount = await this.cosmWasmClient.getAccount(address);
const supportedBankCoins = (bankAccount?.coins || []).filter(({ denom }) =>
const supportedBankCoins = (bankAccount?.balance || []).filter(({ denom }) =>
this.bankTokens.find(token => token.denom === denom),
);
const erc20Amounts = await Promise.all(
@ -191,7 +191,7 @@ export class CosmWasmConnection implements BlockchainConnection {
...supportedBankCoins.map(coin => decodeAmount(this.bankTokens, coin)),
...nonZeroErc20Amounts,
].sort((a, b) => a.tokenTicker.localeCompare(b.tokenTicker));
const pubkey = bankAccount?.public_key ? decodeCosmosPubkey(bankAccount.public_key) : undefined;
const pubkey = bankAccount?.pubkey ? decodeCosmosPubkey(bankAccount.pubkey) : undefined;
return {
address: address,
balance: balance,

View File

@ -83,10 +83,10 @@ describe("CosmWasmClient", () => {
const client = new CosmWasmClient(wasmdEndpoint);
expect(await client.getAccount(unused.address)).toEqual({
address: unused.address,
account_number: 5,
accountNumber: 5,
sequence: 0,
public_key: "",
coins: [
pubkey: undefined,
balance: [
{ denom: "ucosm", amount: "1000000000" },
{ denom: "ustake", amount: "1000000000" },
],

View File

@ -3,13 +3,23 @@ import { Encoding } from "@iov/encoding";
import { Log, parseLogs } from "./logs";
import { BroadcastMode, RestClient } from "./restclient";
import { CosmosSdkAccount, CosmosSdkTx, StdTx } from "./types";
import { Coin, CosmosSdkTx, StdTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
readonly sequence: number;
}
export interface Account {
/** Bech32 account address */
readonly address: string;
readonly balance: ReadonlyArray<Coin>;
/** Bech32 encoded pubkey */
readonly pubkey: string | undefined;
readonly accountNumber: number;
readonly sequence: number;
}
export interface PostTxResult {
readonly logs: readonly Log[];
readonly rawLog: string;
@ -162,15 +172,23 @@ export class CosmWasmClient {
);
}
return {
accountNumber: account.account_number,
accountNumber: account.accountNumber,
sequence: account.sequence,
};
}
public async getAccount(address: string): Promise<CosmosSdkAccount | undefined> {
public async getAccount(address: string): Promise<Account | undefined> {
const account = await this.restClient.authAccounts(address);
const value = account.result.value;
return value.address === "" ? undefined : value;
return value.address === ""
? undefined
: {
address: value.address,
balance: value.coins,
pubkey: value.public_key || undefined,
accountNumber: value.account_number,
sequence: value.sequence,
};
}
/**

View File

@ -7,6 +7,7 @@ export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding";
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export {
Account,
Block,
BlockHeader,
Code,

View File

@ -1,7 +1,7 @@
import { Bech32, Encoding } from "@iov/encoding";
import equal from "fast-deep-equal";
import { Bech32PubKey, PubKey, pubkeyType } from "./types";
import { PubKey, pubkeyType } from "./types";
export function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey {
if (pubkey.length !== 33 || (pubkey[0] !== 0x02 && pubkey[0] !== 0x03)) {
@ -28,8 +28,8 @@ const pubkeyAminoPrefixEd25519 = Encoding.fromHex("1624de6420");
const pubkeyAminoPrefixSr25519 = Encoding.fromHex("0dfb1005");
const pubkeyAminoPrefixLength = pubkeyAminoPrefixSecp256k1.length;
export function decodeBech32Pubkey(bech: Bech32PubKey): PubKey {
const { prefix, data } = Bech32.decode(bech);
export function decodeBech32Pubkey(bechEncoded: string): PubKey {
const { prefix, data } = Bech32.decode(bechEncoded);
if (!isCosmosPubkeyBech32Prefix(prefix)) {
throw new Error(`Invalid bech32 prefix. Must be one of ${validPubkeyPrefixes.join(", ")}.`);
}
@ -65,7 +65,7 @@ export function decodeBech32Pubkey(bech: Bech32PubKey): PubKey {
}
}
export function encodeBech32Pubkey(pubkey: PubKey, prefix: CosmosPubkeyBech32Prefix): Bech32PubKey {
export function encodeBech32Pubkey(pubkey: PubKey, prefix: CosmosPubkeyBech32Prefix): string {
let aminoPrefix: Uint8Array;
switch (pubkey.type) {
// Note: please don't add cases here without writing additional unit tests

View File

@ -1,10 +1,20 @@
import { Encoding } from "@iov/encoding";
import axios, { AxiosError, AxiosInstance } from "axios";
import { CosmosSdkAccount, CosmosSdkTx, Model, parseWasmData, StdTx, WasmData } from "./types";
import { Coin, CosmosSdkTx, Model, parseWasmData, StdTx, WasmData } from "./types";
const { fromBase64, toHex, toUtf8 } = Encoding;
export interface CosmosSdkAccount {
/** Bech32 account address */
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
/** Bech32 encoded pubkey */
readonly public_key: string;
readonly account_number: number;
readonly sequence: number;
}
interface NodeInfo {
readonly network: string;
}

View File

@ -205,7 +205,7 @@ describe("SigningCosmWasmClient", () => {
// got tokens
const after = await client.getAccount(beneficiaryAddress);
assert(after);
expect(after.coins).toEqual(transferAmount);
expect(after.balance).toEqual(transferAmount);
});
});
});

View File

@ -3,13 +3,12 @@ import { Encoding } from "@iov/encoding";
import pako from "pako";
import { isValidBuilder } from "./builder";
import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { Account, CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { makeSignBytes } from "./encoding";
import { findAttribute, Log } from "./logs";
import { BroadcastMode } from "./restclient";
import {
Coin,
CosmosSdkAccount,
MsgExecuteContract,
MsgInstantiateContract,
MsgSend,
@ -121,7 +120,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
return super.getNonce(address || this.senderAddress);
}
public async getAccount(address?: string): Promise<CosmosSdkAccount | undefined> {
public async getAccount(address?: string): Promise<Account | undefined> {
return super.getAccount(address || this.senderAddress);
}

View File

@ -150,18 +150,6 @@ export const pubkeyType = {
export const pubkeyTypes: readonly string[] = [pubkeyType.secp256k1, pubkeyType.ed25519, pubkeyType.sr25519];
// bech32-encoded amino-binary encoded PubKey interface. oof.
export type Bech32PubKey = string;
export interface CosmosSdkAccount {
/** Bech32 account address */
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
readonly public_key: Bech32PubKey;
readonly account_number: number;
readonly sequence: number;
}
export interface WasmData {
// key is hex-encoded
readonly key: string;

View File

@ -1,10 +1,19 @@
import { Log } from "./logs";
import { BroadcastMode, RestClient } from "./restclient";
import { CosmosSdkAccount, CosmosSdkTx, StdTx } from "./types";
import { Coin, CosmosSdkTx, StdTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
readonly sequence: number;
}
export interface Account {
/** Bech32 account address */
readonly address: string;
readonly balance: ReadonlyArray<Coin>;
/** Bech32 encoded pubkey */
readonly pubkey: string | undefined;
readonly accountNumber: number;
readonly sequence: number;
}
export interface PostTxResult {
readonly logs: readonly Log[];
readonly rawLog: string;
@ -109,7 +118,7 @@ export declare class CosmWasmClient {
* @param address returns data for this address. When unset, the client's sender adddress is used.
*/
getNonce(address: string): Promise<GetNonceResult>;
getAccount(address: string): Promise<CosmosSdkAccount | undefined>;
getAccount(address: string): Promise<Account | undefined>;
/**
* Gets block header and meta
*

View File

@ -6,6 +6,7 @@ export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding";
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export {
Account,
Block,
BlockHeader,
Code,

View File

@ -1,5 +1,5 @@
import { Bech32PubKey, PubKey } from "./types";
import { PubKey } from "./types";
export declare function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey;
export declare type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
export declare function decodeBech32Pubkey(bech: Bech32PubKey): PubKey;
export declare function encodeBech32Pubkey(pubkey: PubKey, prefix: CosmosPubkeyBech32Prefix): Bech32PubKey;
export declare function decodeBech32Pubkey(bechEncoded: string): PubKey;
export declare function encodeBech32Pubkey(pubkey: PubKey, prefix: CosmosPubkeyBech32Prefix): string;

View File

@ -1,4 +1,13 @@
import { CosmosSdkAccount, CosmosSdkTx, Model, StdTx } from "./types";
import { Coin, CosmosSdkTx, Model, StdTx } from "./types";
export interface CosmosSdkAccount {
/** Bech32 account address */
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
/** Bech32 encoded pubkey */
readonly public_key: string;
readonly account_number: number;
readonly sequence: number;
}
interface NodeInfo {
readonly network: string;
}

View File

@ -1,7 +1,7 @@
import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { Account, CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { Log } from "./logs";
import { BroadcastMode } from "./restclient";
import { Coin, CosmosSdkAccount, StdFee, StdSignature } from "./types";
import { Coin, StdFee, StdSignature } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
}
@ -56,7 +56,7 @@ export declare class SigningCosmWasmClient extends CosmWasmClient {
broadcastMode?: BroadcastMode,
);
getNonce(address?: string): Promise<GetNonceResult>;
getAccount(address?: string): Promise<CosmosSdkAccount | undefined>;
getAccount(address?: string): Promise<Account | undefined>;
/** Uploads code and returns a receipt, including the code ID */
upload(wasmCode: Uint8Array, meta?: UploadMeta, memo?: string): Promise<UploadResult>;
instantiate(

View File

@ -109,15 +109,6 @@ export declare const pubkeyType: {
sr25519: "tendermint/PubKeySr25519";
};
export declare const pubkeyTypes: readonly string[];
export declare type Bech32PubKey = string;
export interface CosmosSdkAccount {
/** Bech32 account address */
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
readonly public_key: Bech32PubKey;
readonly account_number: number;
readonly sequence: number;
}
export interface WasmData {
readonly key: string;
readonly val: string;