Create higher level Account type
This commit is contained in:
parent
911c9f0649
commit
ed79897cde
@ -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,
|
||||
|
||||
@ -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" },
|
||||
],
|
||||
|
||||
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -7,6 +7,7 @@ export { unmarshalTx } from "./decoding";
|
||||
export { makeSignBytes, marshalTx } from "./encoding";
|
||||
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
|
||||
export {
|
||||
Account,
|
||||
Block,
|
||||
BlockHeader,
|
||||
Code,
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
13
packages/sdk/types/cosmwasmclient.d.ts
vendored
13
packages/sdk/types/cosmwasmclient.d.ts
vendored
@ -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
|
||||
*
|
||||
|
||||
1
packages/sdk/types/index.d.ts
vendored
1
packages/sdk/types/index.d.ts
vendored
@ -6,6 +6,7 @@ export { unmarshalTx } from "./decoding";
|
||||
export { makeSignBytes, marshalTx } from "./encoding";
|
||||
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
|
||||
export {
|
||||
Account,
|
||||
Block,
|
||||
BlockHeader,
|
||||
Code,
|
||||
|
||||
6
packages/sdk/types/pubkey.d.ts
vendored
6
packages/sdk/types/pubkey.d.ts
vendored
@ -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;
|
||||
|
||||
11
packages/sdk/types/restclient.d.ts
vendored
11
packages/sdk/types/restclient.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
||||
@ -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(
|
||||
|
||||
9
packages/sdk/types/types.d.ts
vendored
9
packages/sdk/types/types.d.ts
vendored
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user