-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support EncodeObj #129
Closed
Closed
Support EncodeObj #129
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
722544f
Add EncodeObject to MsgType and TxMsg
abefernan ddfb5f7
Add EncodeObject helpers and tweak signatures
abefernan d8a0e9c
Add TxEncodeObjectDetails
abefernan 0955961
Remove unneeded try/catch
abefernan c39bb16
Add EncodeObjectForm
abefernan cb39de1
Add custom EncodeObject button
abefernan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
components/dataViews/TransactionInfo/TxEncodeObjectDetails.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { EncodeObject } from "@cosmjs/proto-signing"; | ||
|
||
interface TxEncodeObjectDetailsProps { | ||
readonly msg: EncodeObject; | ||
} | ||
|
||
const TxEncodeObjectDetails = ({ msg }: TxEncodeObjectDetailsProps) => ( | ||
<> | ||
<li> | ||
<h3>Custom EncodeObject</h3> | ||
</li> | ||
<li> | ||
<label>Type URL:</label> | ||
<div>{msg.typeUrl}</div> | ||
</li> | ||
<li> | ||
<label>Value:</label> | ||
<div>{JSON.stringify(msg.value, null, 2)}</div> | ||
</li> | ||
<style jsx>{` | ||
li:not(:has(h3)) { | ||
background: rgba(255, 255, 255, 0.03); | ||
padding: 6px 10px; | ||
border-radius: 8px; | ||
display: flex; | ||
align-items: center; | ||
} | ||
li + li:nth-child(2) { | ||
margin-top: 25px; | ||
} | ||
li + li { | ||
margin-top: 10px; | ||
} | ||
li div { | ||
padding: 3px 6px; | ||
} | ||
label { | ||
font-size: 12px; | ||
background: rgba(255, 255, 255, 0.1); | ||
padding: 3px 6px; | ||
border-radius: 5px; | ||
display: block; | ||
} | ||
`}</style> | ||
</> | ||
); | ||
|
||
export default TxEncodeObjectDetails; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
components/forms/CreateTxForm/MsgForm/EncodeObjectForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { EncodeObject } from "@cosmjs/proto-signing"; | ||
import { assert } from "@cosmjs/utils"; | ||
import { useEffect, useState } from "react"; | ||
import { MsgGetter } from ".."; | ||
import { useAppContext } from "../../../../context/AppContext"; | ||
import { isEncodeObject } from "../../../../lib/txMsgHelpers"; | ||
import { TxMsg } from "../../../../types/txMsg"; | ||
import Input from "../../../inputs/Input"; | ||
import StackableContainer from "../../../layout/StackableContainer"; | ||
|
||
interface EncodeObjectFormProps { | ||
readonly setMsgGetter: (msgGetter: MsgGetter) => void; | ||
readonly deleteMsg: () => void; | ||
} | ||
|
||
const EncodeObjectForm = ({ setMsgGetter, deleteMsg }: EncodeObjectFormProps) => { | ||
const { state } = useAppContext(); | ||
assert(state.chain.addressPrefix, "addressPrefix missing"); | ||
|
||
const [msgTypeUrl, setMsgTypeUrl] = useState(""); | ||
const [msgValue, setMsgValue] = useState(""); | ||
|
||
const [msgTypeUrlError, setMsgTypeUrlError] = useState(""); | ||
const [msgValueError, setMsgValueError] = useState(""); | ||
|
||
useEffect(() => { | ||
setMsgTypeUrlError(""); | ||
setMsgValueError(""); | ||
|
||
const isMsgValid = (msg: TxMsg): msg is EncodeObject => { | ||
if (!msgTypeUrl) { | ||
setMsgTypeUrlError("Type URL is required"); | ||
return false; | ||
} | ||
|
||
if (!msgValue) { | ||
setMsgValueError("Value is required"); | ||
return false; | ||
} | ||
|
||
try { | ||
JSON.parse(msgValue); | ||
} catch { | ||
setMsgValueError("Value must be valid JSON"); | ||
return false; | ||
} | ||
|
||
return isEncodeObject(msg); | ||
}; | ||
|
||
const parsedMsgValue = (() => { | ||
try { | ||
return JSON.parse(msgValue); | ||
} catch { | ||
return {}; | ||
} | ||
})(); | ||
|
||
const msg: EncodeObject = { typeUrl: msgTypeUrl, value: parsedMsgValue }; | ||
|
||
setMsgGetter({ isMsgValid, msg }); | ||
}, [msgTypeUrl, msgValue, setMsgGetter]); | ||
|
||
return ( | ||
<StackableContainer lessPadding lessMargin> | ||
<button className="remove" onClick={() => deleteMsg()}> | ||
✕ | ||
</button> | ||
<h2>Custom EncodeObject</h2> | ||
<div className="form-item"> | ||
<Input | ||
label="Type URL" | ||
name="msg-type-url" | ||
value={msgTypeUrl} | ||
onChange={({ target }) => setMsgTypeUrl(target.value)} | ||
error={msgTypeUrlError} | ||
/> | ||
</div> | ||
<div className="form-item"> | ||
<Input | ||
label="Value" | ||
name="msg-value" | ||
value={msgValue} | ||
onChange={({ target }) => setMsgValue(target.value)} | ||
error={msgValueError} | ||
/> | ||
</div> | ||
<style jsx>{` | ||
.form-item { | ||
margin-top: 1.5em; | ||
} | ||
button.remove { | ||
background: rgba(255, 255, 255, 0.2); | ||
width: 30px; | ||
height: 30px; | ||
border-radius: 50%; | ||
border: none; | ||
color: white; | ||
position: absolute; | ||
right: 10px; | ||
top: 10px; | ||
} | ||
`}</style> | ||
</StackableContainer> | ||
); | ||
}; | ||
|
||
export default EncodeObjectForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
value: parsedMsgValue
here is the tricky part: here we need the telescope-specific format. E.g. 64bit integers need to be represented as aLong
type. Binary data needs to be Uint8Array. There is no standardized way to get this from JSON without message specific JSON -> JavaScript importers.