diff --git a/src/cosmoscodec.ts b/src/cosmoscodec.ts index 920211a9ce..ebfffabeb8 100644 --- a/src/cosmoscodec.ts +++ b/src/cosmoscodec.ts @@ -9,6 +9,7 @@ import { SignableBytes, SignedTransaction, SigningJob, + TokenTicker, TransactionId, TxCodec, UnsignedTransaction, @@ -17,10 +18,11 @@ import { Sha256 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import { marshalTx, unmarshalTx } from "@tendermint/amino-js"; -import { isValidAddress, pubkeyToAddress } from "./address"; +import { isValidAddress, pubkeyToAddress, CosmosBech32Prefix } from "./address"; import { Caip5 } from "./caip5"; import { parseTx } from "./decode"; import { buildSignedTx, buildUnsignedTx } from "./encode"; +import { TokenInfos } from "./types"; const { toHex, toUtf8 } = Encoding; @@ -43,6 +45,14 @@ function sortJson(json: any): any { } export class CosmosCodec implements TxCodec { + private readonly prefix: CosmosBech32Prefix; + private readonly tokens: TokenInfos; + + public constructor(prefix: CosmosBech32Prefix, tokens: TokenInfos) { + this.prefix = prefix; + this.tokens = tokens; + } + public bytesToSign(unsigned: UnsignedTransaction, nonce: Nonce): SigningJob { const accountNumber = 0; const memo = (unsigned as any).memo; @@ -86,8 +96,7 @@ export class CosmosCodec implements TxCodec { } public identityToAddress(identity: Identity): Address { - const prefix = "cosmos"; - return pubkeyToAddress(identity.pubkey, prefix); + return pubkeyToAddress(identity.pubkey, this.prefix); } public isValidAddress(address: string): boolean { @@ -95,4 +104,15 @@ export class CosmosCodec implements TxCodec { } } -export const cosmosCodec = new CosmosCodec(); +const defaultPrefix = "cosmos" as CosmosBech32Prefix; + +const defaultTokens: TokenInfos = [ + { + fractionalDigits: 6, + tokenName: "Atom (Cosmos Hub)", + tokenTicker: "ATOM" as TokenTicker, + denom: "uatom", + }, +]; + +export const cosmosCodec = new CosmosCodec(defaultPrefix, defaultTokens); diff --git a/src/decode.ts b/src/decode.ts index 46f00d0b57..46c60b1434 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -45,9 +45,9 @@ export function decodeFullSignature(signature: amino.StdSignature, nonce: number }; } - // TODO: this needs access to token list - we need something more like amountToCoin and coinToAmount here - // and wire that info all the way from both connection and codec. - export function decodeAmount(amount: amino.Coin): Amount { +// TODO: this needs access to token list - we need something more like amountToCoin and coinToAmount here +// and wire that info all the way from both connection and codec. +export function decodeAmount(amount: amino.Coin): Amount { // TODO: more uglyness here (breaks unit tests) if (amount.denom !== "uatom") { throw new Error("Only ATOM amounts are supported"); diff --git a/src/types.ts b/src/types.ts index 415c37edd6..dabdc85154 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,6 +14,8 @@ export interface TokenInfo extends Token { readonly denom: string; } +export type TokenInfos = ReadonlyArray; + // TODO: alias amino types export function amountToCoin(lookup: ReadonlyArray, amount: Amount): amino.Coin { const match = lookup.find(({ tokenTicker }) => tokenTicker === amount.tokenTicker);