mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Add prefix and tokens into Codec
This commit is contained in:
parent
fa40ff54a5
commit
17468f5a75
@ -9,6 +9,7 @@ import {
|
|||||||
SignableBytes,
|
SignableBytes,
|
||||||
SignedTransaction,
|
SignedTransaction,
|
||||||
SigningJob,
|
SigningJob,
|
||||||
|
TokenTicker,
|
||||||
TransactionId,
|
TransactionId,
|
||||||
TxCodec,
|
TxCodec,
|
||||||
UnsignedTransaction,
|
UnsignedTransaction,
|
||||||
@ -17,10 +18,11 @@ import { Sha256 } from "@iov/crypto";
|
|||||||
import { Encoding } from "@iov/encoding";
|
import { Encoding } from "@iov/encoding";
|
||||||
import { marshalTx, unmarshalTx } from "@tendermint/amino-js";
|
import { marshalTx, unmarshalTx } from "@tendermint/amino-js";
|
||||||
|
|
||||||
import { isValidAddress, pubkeyToAddress } from "./address";
|
import { isValidAddress, pubkeyToAddress, CosmosBech32Prefix } from "./address";
|
||||||
import { Caip5 } from "./caip5";
|
import { Caip5 } from "./caip5";
|
||||||
import { parseTx } from "./decode";
|
import { parseTx } from "./decode";
|
||||||
import { buildSignedTx, buildUnsignedTx } from "./encode";
|
import { buildSignedTx, buildUnsignedTx } from "./encode";
|
||||||
|
import { TokenInfos } from "./types";
|
||||||
|
|
||||||
const { toHex, toUtf8 } = Encoding;
|
const { toHex, toUtf8 } = Encoding;
|
||||||
|
|
||||||
@ -43,6 +45,14 @@ function sortJson(json: any): any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class CosmosCodec implements TxCodec {
|
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 {
|
public bytesToSign(unsigned: UnsignedTransaction, nonce: Nonce): SigningJob {
|
||||||
const accountNumber = 0;
|
const accountNumber = 0;
|
||||||
const memo = (unsigned as any).memo;
|
const memo = (unsigned as any).memo;
|
||||||
@ -86,8 +96,7 @@ export class CosmosCodec implements TxCodec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public identityToAddress(identity: Identity): Address {
|
public identityToAddress(identity: Identity): Address {
|
||||||
const prefix = "cosmos";
|
return pubkeyToAddress(identity.pubkey, this.prefix);
|
||||||
return pubkeyToAddress(identity.pubkey, prefix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public isValidAddress(address: string): boolean {
|
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);
|
||||||
|
@ -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
|
// 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.
|
// and wire that info all the way from both connection and codec.
|
||||||
export function decodeAmount(amount: amino.Coin): Amount {
|
export function decodeAmount(amount: amino.Coin): Amount {
|
||||||
// TODO: more uglyness here (breaks unit tests)
|
// TODO: more uglyness here (breaks unit tests)
|
||||||
if (amount.denom !== "uatom") {
|
if (amount.denom !== "uatom") {
|
||||||
throw new Error("Only ATOM amounts are supported");
|
throw new Error("Only ATOM amounts are supported");
|
||||||
|
@ -14,6 +14,8 @@ export interface TokenInfo extends Token {
|
|||||||
readonly denom: string;
|
readonly denom: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TokenInfos = ReadonlyArray<TokenInfo>;
|
||||||
|
|
||||||
// TODO: alias amino types
|
// TODO: alias amino types
|
||||||
export function amountToCoin(lookup: ReadonlyArray<TokenInfo>, amount: Amount): amino.Coin {
|
export function amountToCoin(lookup: ReadonlyArray<TokenInfo>, amount: Amount): amino.Coin {
|
||||||
const match = lookup.find(({ tokenTicker }) => tokenTicker === amount.tokenTicker);
|
const match = lookup.find(({ tokenTicker }) => tokenTicker === amount.tokenTicker);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user