import {z} from 'zod' export type CreateApiCosmosTxV1beta1DecodeArgs = {body: {tx_bytes?: string | undefined}}; export type CreateApiCosmosTxV1beta1DecodeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const cosmosTxV1beta1ModeInfoMulti = z.object({bitarray: z.object({extra_bits_stored: z.number().int().optional(), elems: z.string().optional()}).optional(), mode_infos: z.array(z.lazy(() => cosmosTxV1beta1ModeInfo)).optional()}); export const cosmosTxV1beta1ModeInfo: z.ZodType = z.object({single: z.object({mode: z.enum(["SIGN_MODE_UNSPECIFIED", "SIGN_MODE_DIRECT", "SIGN_MODE_TEXTUAL", "SIGN_MODE_DIRECT_AUX", "SIGN_MODE_LEGACY_AMINO_JSON", "SIGN_MODE_EIP_191"]).optional()}).optional(), multi: cosmosTxV1beta1ModeInfoMulti.optional()}); export const cosmosTxV1beta1SignerInfo = z.object({public_key: z.object({type_url: z.string().optional(), value: z.string().optional()}).optional(), mode_info: cosmosTxV1beta1ModeInfo.optional(), sequence: z.string().optional()}); export const cosmosTxV1beta1AuthInfo = z.object({signer_infos: z.array(cosmosTxV1beta1SignerInfo).optional(), fee: z.object({amount: z.array(z.object({denom: z.string().optional(), amount: z.string().optional()})).optional(), gas_limit: z.string().optional(), payer: z.string().optional(), granter: z.string().optional()}).optional(), tip: z.object({amount: z.array(z.object({denom: z.string().optional(), amount: z.string().optional()})).optional(), tipper: z.string().optional()}).optional()}); export const cosmosTxV1beta1Tx = z.object({body: z.object({messages: z.array(z.object({type_url: z.string().optional(), value: z.string().optional()})).optional(), memo: z.string().optional(), timeout_height: z.string().optional(), extension_options: z.array(z.object({type_url: z.string().optional(), value: z.string().optional()})).optional(), non_critical_extension_options: z.array(z.object({type_url: z.string().optional(), value: z.string().optional()})).optional()}).optional(), auth_info: cosmosTxV1beta1AuthInfo.optional(), signatures: z.array(z.string()).optional()}); export const cosmosTxV1beta1TxDecodeResponse = z.object({tx: cosmosTxV1beta1Tx.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * TxDecode decodes the transaction. * * POST /cosmos/tx/v1beta1/decode */ export const createApiCosmosTxV1beta1Decode = async (args: CreateApiCosmosTxV1beta1DecodeArgs, options: CreateApiCosmosTxV1beta1DecodeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/cosmos/tx/v1beta1/decode${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cosmosTxV1beta1TxDecodeResponse, await res.json()) };