Remove encoding classes Base64 and Hex

This commit is contained in:
Simon Warta 2020-12-21 00:05:50 +01:00
parent 96b82562f6
commit 1cdfc35fe1
4 changed files with 23 additions and 53 deletions

View File

@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { toHex } from "@cosmjs/encoding";
import { toBase64, toHex } from "@cosmjs/encoding";
import { JsonRpcRequest } from "@cosmjs/json-rpc";
import { assertNotEmpty, Base64, Integer, may } from "../../encodings";
import { assertNotEmpty, Integer, may } from "../../encodings";
import { createJsonRpcRequest } from "../../jsonrpc";
import * as requests from "../../requests";
@ -53,7 +53,7 @@ interface RpcBroadcastTxParams {
}
function encodeBroadcastTxParams(params: requests.BroadcastTxParams): RpcBroadcastTxParams {
return {
tx: Base64.encode(assertNotEmpty(params.tx)),
tx: toBase64(assertNotEmpty(params.tx)),
};
}
@ -64,7 +64,7 @@ interface RpcTxParams {
}
function encodeTxParams(params: requests.TxParams): RpcTxParams {
return {
hash: Base64.encode(assertNotEmpty(params.hash)),
hash: toBase64(assertNotEmpty(params.hash)),
prove: params.prove,
};
}

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromHex } from "@cosmjs/encoding";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import {
@ -10,10 +10,8 @@ import {
assertObject,
assertSet,
assertString,
Base64,
DateTime,
dictionaryToStringMap,
Hex,
Integer,
may,
optional,
@ -38,7 +36,7 @@ function decodeAbciInfo(data: RpcAbciInfoResponse): responses.AbciInfoResponse {
return {
data: data.data,
lastBlockHeight: may(Integer.parse, data.last_block_height),
lastBlockAppHash: may(Base64.decode, data.last_block_app_hash),
lastBlockAppHash: may(fromBase64, data.last_block_app_hash),
};
}
@ -62,8 +60,8 @@ function decodeQueryProof(data: RpcQueryProof): responses.QueryProof {
return {
ops: data.ops.map((op) => ({
type: op.type,
key: Base64.decode(op.key),
data: Base64.decode(op.data),
key: fromBase64(op.key),
data: fromBase64(op.data),
})),
};
}
@ -82,8 +80,8 @@ interface RpcAbciQueryResponse {
function decodeAbciQuery(data: RpcAbciQueryResponse): responses.AbciQueryResponse {
return {
key: Base64.decode(optional(data.key, "")),
value: Base64.decode(optional(data.value, "")),
key: fromBase64(optional(data.key, "")),
value: fromBase64(optional(data.value, "")),
proof: may(decodeQueryProof, data.proof),
height: may(Integer.parse, data.height),
code: may(Integer.parse, data.code),
@ -101,8 +99,8 @@ interface RpcAttribute {
function decodeAttribute(attribute: RpcAttribute): responses.Attribute {
return {
key: Base64.decode(assertNotEmpty(attribute.key)),
value: Base64.decode(assertNotEmpty(attribute.value)),
key: fromBase64(assertNotEmpty(attribute.key)),
value: fromBase64(assertNotEmpty(attribute.value)),
};
}
@ -136,7 +134,7 @@ interface RpcTxData {
function decodeTxData(data: RpcTxData): responses.TxData {
return {
data: may(Base64.decode, data.data),
data: may(fromBase64, data.data),
log: data.log,
code: Integer.parse(assertNumber(optional<number>(data.code, 0))),
events: decodeEvents(data.events),
@ -155,7 +153,7 @@ function decodePubkey(data: RpcPubkey): ValidatorPubkey {
// go-amino special code
return {
algorithm: "ed25519",
data: Base64.decode(assertNotEmpty(data.value)),
data: fromBase64(assertNotEmpty(data.value)),
};
}
throw new Error(`unknown pubkey type: ${data.type}`);
@ -173,7 +171,7 @@ function decodeValidatorUpdate(data: RpcValidatorUpdate): responses.Validator {
return {
pubkey: decodePubkey(assertObject(data.pub_key)),
votingPower: Integer.parse(assertNotEmpty(data.voting_power)),
address: Hex.decode(assertNotEmpty(data.address)),
address: fromHex(assertNotEmpty(data.address)),
};
}
@ -410,7 +408,7 @@ type RpcSignature = {
function decodeSignature(data: RpcSignature): ValidatorSignature {
return {
algorithm: "ed25519",
data: Base64.decode(assertNotEmpty(data.signature)),
data: fromBase64(assertNotEmpty(data.signature)),
};
}
@ -603,13 +601,13 @@ interface RpcTxProof {
function decodeTxProof(data: RpcTxProof): responses.TxProof {
return {
data: Base64.decode(assertNotEmpty(data.data)),
data: fromBase64(assertNotEmpty(data.data)),
rootHash: fromHex(assertNotEmpty(data.root_hash)),
proof: {
total: Integer.parse(assertNotEmpty(data.proof.total)),
index: Integer.parse(assertNotEmpty(data.proof.index)),
leafHash: Base64.decode(assertNotEmpty(data.proof.leaf_hash)),
aunts: assertArray(data.proof.aunts).map(Base64.decode),
leafHash: fromBase64(assertNotEmpty(data.proof.leaf_hash)),
aunts: assertArray(data.proof.aunts).map(fromBase64),
},
};
}
@ -627,7 +625,7 @@ interface RpcTxResponse {
function decodeTxResponse(data: RpcTxResponse): responses.TxResponse {
return {
tx: Base64.decode(assertNotEmpty(data.tx)) as TxBytes,
tx: fromBase64(assertNotEmpty(data.tx)) as TxBytes,
result: decodeTxData(assertObject(data.tx_result)),
height: Integer.parse(assertNotEmpty(data.height)),
index: Integer.parse(assertNumber(data.index)),
@ -658,7 +656,7 @@ interface RpcTxEvent {
}
function decodeTxEvent(data: RpcTxEvent): responses.TxEvent {
const tx = Base64.decode(assertNotEmpty(data.tx)) as TxBytes;
const tx = fromBase64(assertNotEmpty(data.tx)) as TxBytes;
return {
tx: tx,
hash: hashTx(tx),
@ -730,7 +728,7 @@ function decodeBlock(data: RpcBlock): responses.Block {
return {
header: decodeHeader(assertObject(data.header)),
lastCommit: decodeCommit(assertObject(data.last_commit)),
txs: data.data.txs ? assertArray(data.data.txs).map(Base64.decode) : [],
txs: data.data.txs ? assertArray(data.data.txs).map(fromBase64) : [],
evidence: data.evidence && may(decodeEvidences, data.evidence.evidence),
};
}

View File

@ -1,4 +1,4 @@
import { fromBase64, fromHex, fromRfc3339, toBase64, toHex, toUtf8 } from "@cosmjs/encoding";
import { fromRfc3339, toUtf8 } from "@cosmjs/encoding";
import { Int53 } from "@cosmjs/math";
import { BlockId, ReadonlyDateWithNanoseconds, Version } from "./responses";
@ -155,16 +155,6 @@ export class Integer {
}
}
export class Base64 {
public static encode(data: Uint8Array): string {
return toBase64(data);
}
public static decode(base64String: string): Uint8Array {
return fromBase64(base64String);
}
}
export class DateTime {
public static decode(dateTimeString: string): ReadonlyDateWithNanoseconds {
const readonlyDate = fromRfc3339(dateTimeString);
@ -181,16 +171,6 @@ export class DateTime {
}
}
export class Hex {
public static encode(data: Uint8Array): string {
return toHex(data);
}
public static decode(hexString: string): Uint8Array {
return fromHex(hexString);
}
}
// Encodings needed for hashing block headers
// Several of these functions are inspired by https://github.com/nomic-io/js-tendermint/blob/tendermint-0.30/src/

View File

@ -57,18 +57,10 @@ export declare class Integer {
static parse(input: string | number): number;
static encode(num: number): string;
}
export declare class Base64 {
static encode(data: Uint8Array): string;
static decode(base64String: string): Uint8Array;
}
export declare class DateTime {
static decode(dateTimeString: string): ReadonlyDateWithNanoseconds;
static encode(dateTime: ReadonlyDateWithNanoseconds): string;
}
export declare class Hex {
static encode(data: Uint8Array): string;
static decode(hexString: string): Uint8Array;
}
export declare function encodeString(s: string): Uint8Array;
export declare function encodeInt(n: number): Uint8Array;
export declare function encodeTime(time: ReadonlyDateWithNanoseconds): Uint8Array;