diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 6c5b16c9..c57510b2 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -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, diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 88d65dc8..660c9e23 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -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" }, ], diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index 26b9e590..79d5b4cb 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -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; + /** 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 { + public async getAccount(address: string): Promise { 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, + }; } /** diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index bf7704fd..0b5e47b2 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -7,6 +7,7 @@ export { unmarshalTx } from "./decoding"; export { makeSignBytes, marshalTx } from "./encoding"; export { BroadcastMode, RestClient, TxsResponse } from "./restclient"; export { + Account, Block, BlockHeader, Code, diff --git a/packages/sdk/src/pubkey.ts b/packages/sdk/src/pubkey.ts index 5da2e0a5..b9c8b1ba 100644 --- a/packages/sdk/src/pubkey.ts +++ b/packages/sdk/src/pubkey.ts @@ -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 diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 4e064fdc..ac5fd3cb 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -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; + /** Bech32 encoded pubkey */ + readonly public_key: string; + readonly account_number: number; + readonly sequence: number; +} + interface NodeInfo { readonly network: string; } diff --git a/packages/sdk/src/signingcosmwasmclient.spec.ts b/packages/sdk/src/signingcosmwasmclient.spec.ts index 096008d0..79c74fc1 100644 --- a/packages/sdk/src/signingcosmwasmclient.spec.ts +++ b/packages/sdk/src/signingcosmwasmclient.spec.ts @@ -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); }); }); }); diff --git a/packages/sdk/src/signingcosmwasmclient.ts b/packages/sdk/src/signingcosmwasmclient.ts index 67d38752..db5933f1 100644 --- a/packages/sdk/src/signingcosmwasmclient.ts +++ b/packages/sdk/src/signingcosmwasmclient.ts @@ -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 { + public async getAccount(address?: string): Promise { return super.getAccount(address || this.senderAddress); } diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index c902cc4b..1a354ade 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -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; - readonly public_key: Bech32PubKey; - readonly account_number: number; - readonly sequence: number; -} - export interface WasmData { // key is hex-encoded readonly key: string; diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index 54fa2749..89f5f45e 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -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; + /** 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; - getAccount(address: string): Promise; + getAccount(address: string): Promise; /** * Gets block header and meta * diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index 54934860..f77304d1 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -6,6 +6,7 @@ export { unmarshalTx } from "./decoding"; export { makeSignBytes, marshalTx } from "./encoding"; export { BroadcastMode, RestClient, TxsResponse } from "./restclient"; export { + Account, Block, BlockHeader, Code, diff --git a/packages/sdk/types/pubkey.d.ts b/packages/sdk/types/pubkey.d.ts index a7dddae8..8715588b 100644 --- a/packages/sdk/types/pubkey.d.ts +++ b/packages/sdk/types/pubkey.d.ts @@ -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; diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index eaa59c0f..3e8a00d1 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -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; + /** Bech32 encoded pubkey */ + readonly public_key: string; + readonly account_number: number; + readonly sequence: number; +} interface NodeInfo { readonly network: string; } diff --git a/packages/sdk/types/signingcosmwasmclient.d.ts b/packages/sdk/types/signingcosmwasmclient.d.ts index 8b169172..9c8f5b0a 100644 --- a/packages/sdk/types/signingcosmwasmclient.d.ts +++ b/packages/sdk/types/signingcosmwasmclient.d.ts @@ -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; } @@ -56,7 +56,7 @@ export declare class SigningCosmWasmClient extends CosmWasmClient { broadcastMode?: BroadcastMode, ); getNonce(address?: string): Promise; - getAccount(address?: string): Promise; + getAccount(address?: string): Promise; /** Uploads code and returns a receipt, including the code ID */ upload(wasmCode: Uint8Array, meta?: UploadMeta, memo?: string): Promise; instantiate( diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index d7f1bdf8..1a62733a 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -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; - readonly public_key: Bech32PubKey; - readonly account_number: number; - readonly sequence: number; -} export interface WasmData { readonly key: string; readonly val: string;