Move sortJson into @cosmwasm/sdk
This commit is contained in:
parent
ee2c621fba
commit
8034ae7bc6
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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 };
|
||||
|
||||
1
packages/sdk/types/encoding.d.ts
vendored
1
packages/sdk/types/encoding.d.ts
vendored
@ -1,2 +1,3 @@
|
||||
import { StdTx } from "./types";
|
||||
export declare function sortJson(json: any): any;
|
||||
export declare function marshalTx(tx: StdTx): Uint8Array;
|
||||
|
||||
2
packages/sdk/types/index.d.ts
vendored
2
packages/sdk/types/index.d.ts
vendored
@ -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 };
|
||||
|
||||
Loading…
Reference in New Issue
Block a user