49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
|
|
import { AminoMsg, StdFee, StdSignDoc } from "@cosmjs/amino";
|
|
import { toUtf8 } from "@cosmjs/encoding";
|
|
import { Uint53 } from "@cosmjs/math";
|
|
|
|
function sortedObject(obj: any): any {
|
|
if (typeof obj !== "object" || obj === null) {
|
|
return obj;
|
|
}
|
|
if (Array.isArray(obj)) {
|
|
return obj.map(sortedObject);
|
|
}
|
|
const sortedKeys = Object.keys(obj).sort();
|
|
const result: Record<string, any> = {};
|
|
// NOTE: Use forEach instead of reduce for performance with large objects eg Wasm code
|
|
sortedKeys.forEach((key) => {
|
|
result[key] = sortedObject(obj[key]);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
/** Returns a JSON string with objects sorted by key */
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export function sortedJsonStringify(obj: any): string {
|
|
return JSON.stringify(sortedObject(obj));
|
|
}
|
|
|
|
export function makeSignDoc(
|
|
msgs: readonly AminoMsg[],
|
|
fee: StdFee,
|
|
chainId: string,
|
|
memo: string | undefined,
|
|
accountNumber: number | string,
|
|
sequence: number | string,
|
|
): StdSignDoc {
|
|
return {
|
|
chain_id: chainId,
|
|
account_number: Uint53.fromString(accountNumber.toString()).toString(),
|
|
sequence: Uint53.fromString(sequence.toString()).toString(),
|
|
fee: fee,
|
|
msgs: msgs,
|
|
memo: memo || "",
|
|
};
|
|
}
|
|
|
|
export function serializeSignDoc(signDoc: StdSignDoc): Uint8Array {
|
|
return toUtf8(sortedJsonStringify(signDoc));
|
|
}
|