Skip to content

Commit

Permalink
fixed decoding invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Apr 17, 2024
1 parent 6b23d32 commit 3df328c
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const InnerComponent: ActionComponent<undefined, GovernanceDepositData> = (
? govProposalsSelector({
status: ProposalStatus.PROPOSAL_STATUS_DEPOSIT_PERIOD,
chainId,
limit: 100,
})
: constSelector(undefined)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const Component: ActionComponent<undefined, GovernanceVoteData> = (props) => {
? govProposalsSelector({
status: ProposalStatus.PROPOSAL_STATUS_VOTING_PERIOD,
chainId,
limit: 100,
})
: constSelector(undefined)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const useDecodedCosmosMsg: UseDecodedCosmosMsg<SetUpApproverData> = (
}
}

const parsedMsg = decodeJsonFromBase64(info.msg)
const parsedMsg = decodeJsonFromBase64(info.msg, true)
if (
!info.label.endsWith(`${DaoProposalSingleAdapterId}_approver`) ||
!objectMatchesStructure(parsedMsg, {
Expand All @@ -104,7 +104,8 @@ const useDecodedCosmosMsg: UseDecodedCosmosMsg<SetUpApproverData> = (
}

const parsedPreProposeMsg = decodeJsonFromBase64(
parsedMsg.pre_propose_info.module_may_propose.info.msg
parsedMsg.pre_propose_info.module_may_propose.info.msg,
true
)
if (
!objectMatchesStructure(parsedPreProposeMsg, {
Expand Down
2 changes: 1 addition & 1 deletion packages/stateful/actions/core/nfts/TransferNft/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const useDecodedCosmosMsg: UseDecodedCosmosMsg<TransferNftData> = (

executeSmartContract: true,
smartContractMsg: JSON.stringify(
decodeJsonFromBase64(msg.wasm.execute.msg.send_nft.msg),
decodeJsonFromBase64(msg.wasm.execute.msg.send_nft.msg, true),
null,
2
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const useDecodedCosmosMsg: UseDecodedCosmosMsg<ExecuteData> = (
: msg.wasm.execute.contract_addr,
message: JSON.stringify(
isCw20
? decodeJsonFromBase64(msg.wasm.execute.msg.send.msg)
? decodeJsonFromBase64(msg.wasm.execute.msg.send.msg, true)
: msg.wasm.execute.msg,
undefined,
2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const makeInstantiate2Action: ActionMaker<Instantiate2Data> = (
admin: msg.stargate.value.admin,
code_id: Number(msg.stargate.value.codeId),
label: msg.stargate.value.label,
msg: decodeJsonFromBase64(toBase64(msg.stargate.value.msg)),
msg: decodeJsonFromBase64(toBase64(msg.stargate.value.msg), true),
funds: msg.stargate.value.funds,
fix_msg: msg.stargate.value.fixMsg,
salt: fromUtf8(msg.stargate.value.salt),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ export const makeManageVestingAction: ActionMaker<ManageVestingData> = (
},
}) &&
objectMatchesStructure(
decodeJsonFromBase64(msg.wasm.execute.msg.send.msg),
decodeJsonFromBase64(msg.wasm.execute.msg.send.msg, true),
{
instantiate_payroll_contract: instantiateStructure,
}
Expand Down Expand Up @@ -934,8 +934,10 @@ export const makeManageVestingAction: ActionMaker<ManageVestingData> = (
// isCw20Begin
else {
// Extract instantiate message from cw20 send message.
instantiateMsg = decodeJsonFromBase64(msg.wasm.execute.msg.send.msg)
.instantiate_payroll_contract.instantiate_msg as VestingInstantiateMsg
instantiateMsg = decodeJsonFromBase64(
msg.wasm.execute.msg.send.msg,
true
).instantiate_payroll_contract?.instantiate_msg as VestingInstantiateMsg
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ const useDecodedCosmosMsg: UseDecodedCosmosMsg<PerformTokenSwapData> = (
}) &&
// Use common key to identify CW20s being sent to token swaps from this
// DAO DAO action.
CW20_SEND_MSG_KEY in decodeJsonFromBase64(msg.wasm.execute.msg.send.msg)
CW20_SEND_MSG_KEY in
decodeJsonFromBase64(msg.wasm.execute.msg.send.msg, true)
) {
return {
match: true,
Expand Down
19 changes: 14 additions & 5 deletions packages/stateless/components/dao/create/pages/CreateDaoReview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'

import { CreateDaoContext, DaoInfoCard } from '@dao-dao/types'
import { decodeJsonFromBase64, processError } from '@dao-dao/utils'
import {
decodeJsonFromBase64,
objectMatchesStructure,
processError,
} from '@dao-dao/utils'

import { CosmosMessageDisplay } from '../../../CosmosMessageDisplay'
import { Checkbox } from '../../../inputs/Checkbox'
Expand Down Expand Up @@ -47,10 +51,15 @@ export const CreateDaoReview = ({

// Convert encoded pre_propose_info message back to readable JSON.
if (
'pre_propose_info' in msg &&
'module_may_propose' in msg.pre_propose_info &&
'info' in msg.pre_propose_info.module_may_propose &&
'msg' in msg.pre_propose_info.module_may_propose.info
objectMatchesStructure(msg, {
pre_propose_info: {
module_may_propose: {
info: {
msg: {},
},
},
},
})
) {
msg.pre_propose_info.module_may_propose.info.msg =
decodeJsonFromBase64(
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/messages/cw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const decodeMessage = (msg: CosmosMsgFor_Empty): Record<string, any> => {
if (msgType && isBinaryType(msgType)) {
const base64MsgContainer = (msg.wasm as any)[msgType]
if (base64MsgContainer && 'msg' in base64MsgContainer) {
const parsedMsg = decodeJsonFromBase64(base64MsgContainer.msg)
const parsedMsg = decodeJsonFromBase64(base64MsgContainer.msg, true)
if (parsedMsg) {
return {
...msg,
Expand Down
20 changes: 16 additions & 4 deletions packages/utils/messages/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ export const encodeJsonToBase64 = (object: any) =>
toBase64(toUtf8(JSON.stringify(object)))

/**
* Decode base64 string into JSON object.
* Decode base64 string into JSON object. If `fallbackToString` is true and
* JSON parse fails, decoded string is returned.
*/
export const decodeJsonFromBase64 = (base64String?: string) => {
export const decodeJsonFromBase64 = (
base64String?: string,
fallbackToString = false
) => {
if (base64String) {
const jsonMessage = fromUtf8(fromBase64(base64String))
if (jsonMessage) {
return JSON.parse(jsonMessage)
try {
if (jsonMessage) {
return JSON.parse(jsonMessage)
}
} catch (err) {
if (fallbackToString) {
return jsonMessage
}

throw err
}
}
}

0 comments on commit 3df328c

Please sign in to comment.