From 8034ae7bc6a659ea8fc74ee367118284e0868008 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 4 Feb 2020 01:16:28 +0100 Subject: [PATCH] Move sortJson into @cosmwasm/sdk --- packages/bcp/src/cosmwasmcodec.ts | 20 +------------------- packages/sdk/src/encoding.ts | 18 ++++++++++++++++++ packages/sdk/src/index.ts | 2 +- packages/sdk/types/encoding.d.ts | 1 + packages/sdk/types/index.d.ts | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/bcp/src/cosmwasmcodec.ts b/packages/bcp/src/cosmwasmcodec.ts index caf46ad7..7602830f 100644 --- a/packages/bcp/src/cosmwasmcodec.ts +++ b/packages/bcp/src/cosmwasmcodec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { marshalTx, unmarshalTx } from "@cosmwasm/sdk"; +import { marshalTx, sortJson, unmarshalTx } from "@cosmwasm/sdk"; import { Address, ChainId, @@ -24,24 +24,6 @@ import { nonceToAccountNumber, nonceToSequence, TokenInfos } from "./types"; const { toUtf8 } = Encoding; -function sortJson(json: any): any { - if (typeof json !== "object" || json === null) { - return json; - } - if (Array.isArray(json)) { - return json.map(sortJson); - } - const sortedKeys = Object.keys(json).sort(); - const result = sortedKeys.reduce( - (accumulator, key) => ({ - ...accumulator, - [key]: sortJson(json[key]), - }), - {}, - ); - return result; -} - export class CosmWasmCodec implements TxCodec { private readonly prefix: CosmosBech32Prefix; private readonly tokens: TokenInfos; diff --git a/packages/sdk/src/encoding.ts b/packages/sdk/src/encoding.ts index 6f6d9be3..3cee29af 100644 --- a/packages/sdk/src/encoding.ts +++ b/packages/sdk/src/encoding.ts @@ -2,6 +2,24 @@ import { Encoding } from "@iov/encoding"; import { StdTx } from "./types"; +export function sortJson(json: any): any { + if (typeof json !== "object" || json === null) { + return json; + } + if (Array.isArray(json)) { + return json.map(sortJson); + } + const sortedKeys = Object.keys(json).sort(); + const result = sortedKeys.reduce( + (accumulator, key) => ({ + ...accumulator, + [key]: sortJson(json[key]), + }), + {}, + ); + return result; +} + export function marshalTx(tx: StdTx): Uint8Array { const json = JSON.stringify(tx); return Encoding.toUtf8(json); diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index d84968e5..23e94da1 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -1,6 +1,6 @@ import * as types from "./types"; export { unmarshalTx } from "./decoding"; -export { marshalTx } from "./encoding"; +export { marshalTx, sortJson } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; export { types }; diff --git a/packages/sdk/types/encoding.d.ts b/packages/sdk/types/encoding.d.ts index 7a33940a..591bdbb4 100644 --- a/packages/sdk/types/encoding.d.ts +++ b/packages/sdk/types/encoding.d.ts @@ -1,2 +1,3 @@ import { StdTx } from "./types"; +export declare function sortJson(json: any): any; export declare function marshalTx(tx: StdTx): Uint8Array; diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index 4420e59b..0c6dcc71 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -1,5 +1,5 @@ import * as types from "./types"; export { unmarshalTx } from "./decoding"; -export { marshalTx } from "./encoding"; +export { marshalTx, sortJson } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; export { types };