Skip to content

Commit

Permalink
Remove fix msg. Use hex salt
Browse files Browse the repository at this point in the history
  • Loading branch information
abefernan committed Jul 26, 2023
1 parent f62fa05 commit d6c1686
Showing 1 changed file with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MsgInstantiateContract2EncodeObject } from "@cosmjs/cosmwasm-stargate";
import { toUtf8 } from "@cosmjs/encoding";
import { fromHex, toUtf8 } from "@cosmjs/encoding";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { MsgGetter } from "..";
Expand Down Expand Up @@ -42,7 +42,6 @@ const MsgInstantiateContract2Form = ({
const [codeId, setCodeId] = useState("");
const [label, setLabel] = useState("");
const [adminAddress, setAdminAddress] = useState("");
const [fixMsg, setFixMsg] = useState(false);
const [salt, setSalt] = useState("");
const [msgContent, setMsgContent] = useState("{}");
const [selectedDenom, setSelectedDenom] = useState(denomOptions[0]);
Expand All @@ -52,13 +51,15 @@ const MsgInstantiateContract2Form = ({
const [codeIdError, setCodeIdError] = useState("");
const [labelError, setLabelError] = useState("");
const [adminAddressError, setAdminAddressError] = useState("");
const [saltError, setSaltError] = useState("");
const [customDenomError, setCustomDenomError] = useState("");
const [amountError, setAmountError] = useState("");

useEffect(() => {
setCodeIdError("");
setLabelError("");
setAdminAddressError("");
setSaltError("");
setCustomDenomError("");
setAmountError("");

Expand All @@ -79,6 +80,13 @@ const MsgInstantiateContract2Form = ({
return false;
}

try {
fromHex(salt);
} catch (e) {
setSaltError(e instanceof Error ? e.message : "Salt needs to be an hexadecimal string");
return false;
}

if (selectedDenom.value === customDenomOption.value && !customDenom) {
setCustomDenomError("Custom denom must be set because of selection above");
return false;
Expand Down Expand Up @@ -108,12 +116,20 @@ const MsgInstantiateContract2Form = ({
}
})();

const hexSalt = (() => {
try {
return fromHex(salt);
} catch {
return undefined;
}
})();

const msgContentUtf8Array = (() => {
try {
// The JsonEditor does not escape \n or remove whitespaces, so we need to parse + stringify
return toUtf8(JSON.stringify(JSON.parse(msgContent)));
} catch {
return Uint8Array.from([]);
return undefined;
}
})();

Expand All @@ -122,8 +138,8 @@ const MsgInstantiateContract2Form = ({
codeId: codeId || 0,
label,
admin: adminAddress,
fixMsg,
salt: toUtf8(salt),
fixMsg: false,
salt: hexSalt,
msg: msgContentUtf8Array,
funds: [microCoin],
});
Expand All @@ -142,7 +158,6 @@ const MsgInstantiateContract2Form = ({
chain.chainId,
codeId,
customDenom,
fixMsg,
fromAddress,
label,
msgContent,
Expand Down Expand Up @@ -187,20 +202,12 @@ const MsgInstantiateContract2Form = ({
</div>
<div className="form-item">
<Input
type="checkbox"
label="Fix msg"
name="fix-msg"
checked={fixMsg}
value={String(fixMsg)}
onChange={({ target }) => setFixMsg(target.checked)}
/>
</div>
<div className="form-item">
<Input
label="Salt"
label="Salt (hex encoded)"
name="salt"
placeholder="E.g. 1bac68"
value={salt}
onChange={({ target }) => setSalt(target.value)}
error={saltError}
/>
</div>
<div className="form-item">
Expand Down

0 comments on commit d6c1686

Please sign in to comment.