Import from @iov/cosmos

This commit is contained in:
Ethan Frey
2020-01-22 09:41:48 +01:00
commit 0fc62d4aa8
49 changed files with 2490 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
import { Address, PubkeyBundle } from "@iov/bcp";
export declare type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
export declare type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
export declare type CosmosBech32Prefix = CosmosAddressBech32Prefix | CosmosPubkeyBech32Prefix;
export declare function decodeCosmosAddress(
address: Address,
): {
readonly prefix: CosmosAddressBech32Prefix;
readonly data: Uint8Array;
};
export declare function isValidAddress(address: string): boolean;
export declare function pubkeyToAddress(pubkey: PubkeyBundle, prefix: CosmosBech32Prefix): Address;
+13
View File
@@ -0,0 +1,13 @@
import { ChainId } from "@iov/bcp";
/**
* Conversion between native chain IDs and CAIP-5 format
*
* @see https://github.com/ChainAgnostic/CAIPs/pull/9
*/
export declare class Caip5 {
/**
* @param native The `chain_id` field from Tendermint's genesis file
*/
static encode(native: string): ChainId;
static decode(chainId: ChainId): string;
}
+21
View File
@@ -0,0 +1,21 @@
import {
Address,
ChainId,
Identity,
Nonce,
PostableBytes,
SignedTransaction,
SigningJob,
TransactionId,
TxCodec,
UnsignedTransaction,
} from "@iov/bcp";
export declare class CosmosCodec implements TxCodec {
bytesToSign(unsigned: UnsignedTransaction, nonce: Nonce): SigningJob;
bytesToPost(signed: SignedTransaction): PostableBytes;
identifier(signed: SignedTransaction): TransactionId;
parseBytes(bytes: PostableBytes, chainId: ChainId, nonce?: Nonce): SignedTransaction;
identityToAddress(identity: Identity): Address;
isValidAddress(address: string): boolean;
}
export declare const cosmosCodec: CosmosCodec;
+53
View File
@@ -0,0 +1,53 @@
import {
Account,
AccountQuery,
AddressQuery,
BlockchainConnection,
BlockHeader,
ChainId,
ConfirmedAndSignedTransaction,
ConfirmedTransaction,
FailedTransaction,
Fee,
Nonce,
PostableBytes,
PostTxResponse,
PubkeyQuery,
Token,
TokenTicker,
TransactionId,
TransactionQuery,
UnsignedTransaction,
} from "@iov/bcp";
import { Stream } from "xstream";
export declare class CosmosConnection implements BlockchainConnection {
static establish(url: string): Promise<CosmosConnection>;
private static initialize;
private readonly restClient;
private readonly chainData;
private readonly primaryToken;
private readonly supportedTokens;
private get prefix();
private constructor();
disconnect(): void;
chainId(): ChainId;
height(): Promise<number>;
getToken(searchTicker: TokenTicker): Promise<Token | undefined>;
getAllTokens(): Promise<readonly Token[]>;
getAccount(query: AccountQuery): Promise<Account | undefined>;
watchAccount(_account: AccountQuery): Stream<Account | undefined>;
getNonce(query: AddressQuery | PubkeyQuery): Promise<Nonce>;
getNonces(query: AddressQuery | PubkeyQuery, count: number): Promise<readonly Nonce[]>;
getBlockHeader(height: number): Promise<BlockHeader>;
watchBlockHeaders(): Stream<BlockHeader>;
getTx(id: TransactionId): Promise<ConfirmedAndSignedTransaction<UnsignedTransaction> | FailedTransaction>;
postTx(tx: PostableBytes): Promise<PostTxResponse>;
searchTx(
query: TransactionQuery,
): Promise<readonly (ConfirmedTransaction<UnsignedTransaction> | FailedTransaction)[]>;
listenTx(_query: TransactionQuery): Stream<ConfirmedTransaction<UnsignedTransaction> | FailedTransaction>;
liveTx(_query: TransactionQuery): Stream<ConfirmedTransaction<UnsignedTransaction> | FailedTransaction>;
getFeeQuote(tx: UnsignedTransaction): Promise<Fee>;
withDefaultFee<T extends UnsignedTransaction>(tx: T): Promise<T>;
private parseAndPopulateTxResponse;
}
+9
View File
@@ -0,0 +1,9 @@
import { ChainConnector, ChainId } from "@iov/bcp";
import { CosmosConnection } from "./cosmosconnection";
/**
* A helper to connect to a cosmos-based chain at a given url
*/
export declare function createCosmosConnector(
url: string,
expectedChainId?: ChainId,
): ChainConnector<CosmosConnection>;
+28
View File
@@ -0,0 +1,28 @@
import {
Amount,
ChainId,
ConfirmedAndSignedTransaction,
Fee,
FullSignature,
Nonce,
PubkeyBundle,
SendTransaction,
SignatureBytes,
SignedTransaction,
UnsignedTransaction,
} from "@iov/bcp";
import amino from "@tendermint/amino-js";
import { TxsResponse } from "./restclient";
export declare function decodePubkey(pubkey: amino.PubKey): PubkeyBundle;
export declare function decodeSignature(signature: string): SignatureBytes;
export declare function decodeFullSignature(signature: amino.StdSignature, nonce: number): FullSignature;
export declare function decodeAmount(amount: amino.Coin): Amount;
export declare function parseMsg(msg: amino.Msg, chainId: ChainId): SendTransaction;
export declare function parseFee(fee: amino.StdFee): Fee;
export declare function parseTx(tx: amino.Tx, chainId: ChainId, nonce: Nonce): SignedTransaction;
export declare function parseTxsResponse(
chainId: ChainId,
currentHeight: number,
nonce: Nonce,
response: TxsResponse,
): ConfirmedAndSignedTransaction<UnsignedTransaction>;
+9
View File
@@ -0,0 +1,9 @@
import { Amount, Fee, FullSignature, PubkeyBundle, SignedTransaction, UnsignedTransaction } from "@iov/bcp";
import amino from "@tendermint/amino-js";
import { AminoTx } from "./types";
export declare function encodePubkey(pubkey: PubkeyBundle): amino.PubKey;
export declare function encodeAmount(amount: Amount): amino.Coin;
export declare function encodeFee(fee: Fee): amino.StdFee;
export declare function encodeFullSignature(fullSignature: FullSignature): amino.StdSignature;
export declare function buildUnsignedTx(tx: UnsignedTransaction): AminoTx;
export declare function buildSignedTx(tx: SignedTransaction): AminoTx;
+3
View File
@@ -0,0 +1,3 @@
export { cosmosCodec, CosmosCodec } from "./cosmoscodec";
export { CosmosConnection } from "./cosmosconnection";
export { createCosmosConnector } from "./cosmosconnector";
+77
View File
@@ -0,0 +1,77 @@
import { Address, PostableBytes, TransactionId } from "@iov/bcp";
import amino from "@tendermint/amino-js";
import { AminoTx } from "./types";
interface NodeInfo {
readonly network: string;
}
interface NodeInfoResponse {
readonly node_info: NodeInfo;
}
interface BlockMeta {
readonly header: {
readonly height: number;
readonly time: string;
readonly num_txs: number;
};
readonly block_id: {
readonly hash: string;
};
}
interface Block {
readonly header: {
readonly height: number;
};
}
interface BlocksResponse {
readonly block_meta: BlockMeta;
readonly block: Block;
}
interface AuthAccountsResponse {
readonly result: {
readonly value: amino.BaseAccount;
};
}
export interface TxsResponse {
readonly height: string;
readonly txhash: string;
readonly raw_log: string;
readonly tx: AminoTx;
}
interface SearchTxsResponse {
readonly total_count: string;
readonly count: string;
readonly page_number: string;
readonly page_total: string;
readonly limit: string;
readonly txs: readonly TxsResponse[];
}
interface PostTxsParams {}
interface PostTxsResponse {
readonly height: string;
readonly txhash: string;
readonly code?: number;
readonly raw_log?: string;
}
declare type RestClientResponse =
| NodeInfoResponse
| BlocksResponse
| AuthAccountsResponse
| TxsResponse
| SearchTxsResponse
| PostTxsResponse;
declare type BroadcastMode = "block" | "sync" | "async";
export declare class RestClient {
private readonly client;
private readonly mode;
constructor(url: string, mode?: BroadcastMode);
get(path: string): Promise<RestClientResponse>;
post(path: string, params: PostTxsParams): Promise<RestClientResponse>;
nodeInfo(): Promise<NodeInfoResponse>;
blocksLatest(): Promise<BlocksResponse>;
blocks(height: number): Promise<BlocksResponse>;
authAccounts(address: Address, height?: string): Promise<AuthAccountsResponse>;
txs(query: string): Promise<SearchTxsResponse>;
txsById(id: TransactionId): Promise<TxsResponse>;
postTx(tx: PostableBytes): Promise<PostTxsResponse>;
}
export {};
+5
View File
@@ -0,0 +1,5 @@
import amino from "@tendermint/amino-js";
export declare type AminoTx = amino.Tx & {
readonly value: amino.StdTx;
};
export declare function isAminoStdTx(txValue: amino.TxValue): txValue is amino.StdTx;