Encoding
Platform-neutral JSON encoding utilities for CosmWasm message fields.
import { Json } from 'schemos'Json.toBytes
Encode a JSON-serializable object to UTF-8 bytes.
Json.toBytes(value: Record<string, unknown>): Uint8ArrayUse for MsgExecuteContract.msg and queryData fields in telescope SDKs:
import { Json } from 'schemos'
import { MsgExecuteContract } from 'osmojs/cosmwasm/wasm/v1/tx'
// Json is re-exported from the main schemos entrypoint
const envelope = cw20Msg('transfer', { amount: '1000', recipient: 'osmo1...' })
MsgExecuteContract.fromPartial({
sender,
contract,
msg: Json.toBytes(envelope),
funds: [],
})Json.toBase64
Encode a JSON-serializable object to a base64 string.
Json.toBase64(value: Record<string, unknown>): stringUse for CosmWasm Binary fields — e.g., the msg field in cw20 send (hook message to receiving contract):
import { Json } from 'schemos'
cw20Msg('send', {
amount: '1000',
contract: 'osmo1hook...',
msg: Json.toBase64({ execute_swap: { offer_asset: 'uosmo' } }),
})Json.fromBytes
Decode UTF-8 bytes to a JSON object.
Json.fromBytes(bytes: Uint8Array): unknownUse for decoding query responses from telescope RPC:
import { Json } from 'schemos'
const res = await rpc.cosmwasm.wasm.v1.smartContractState({ address, queryData })
const result = Json.fromBytes(res.data)Implementation Notes
- Uses
TextEncoder/TextDecoderfor UTF-8 (platform-neutral — works in Node.js and browsers) - Base64 encoding uses a lookup table approach adapted from ox (MIT, wevm LLC)
- No dependency on Node.js
Buffer