diff --git a/src/codec-components/BinaryViewCodec/CEnum.tsx b/src/codec-components/BinaryViewCodec/CEnum.tsx new file mode 100644 index 0000000..d3fbefd --- /dev/null +++ b/src/codec-components/BinaryViewCodec/CEnum.tsx @@ -0,0 +1,36 @@ +import { EditEnum, NOTIN } from "@codec-components" +import { u8 } from "@polkadot-api/substrate-bindings" +import { useStateObservable } from "@react-rxjs/core" +import { isActive$ } from "../common/paths.state" +import { useSubtreeFocus } from "../common/SubtreeFocus" +import { + headerHighlight, + highlight, + MissingData, + toConcatHex, +} from "./codec-components" + +export const CEnum: EditEnum = ({ value, inner, path, shape }) => { + const isActive = useStateObservable(isActive$(path.join("."))) + const focus = useSubtreeFocus() + const sub = focus.getNextPath(path) + if (sub) { + return inner + } + + if (value === NOTIN) return + + const entry = shape.value[value.type] + if (!entry) { + console.log(shape.value, value.type) + throw new Error("Type not found??") + } + return ( + + + {toConcatHex(u8.enc(entry.idx))} + + {inner} + + ) +} diff --git a/src/codec-components/BinaryViewCodec/CStruct.tsx b/src/codec-components/BinaryViewCodec/CStruct.tsx new file mode 100644 index 0000000..d58361e --- /dev/null +++ b/src/codec-components/BinaryViewCodec/CStruct.tsx @@ -0,0 +1,28 @@ +import { EditStruct, NOTIN } from "@codec-components" +import { useStateObservable } from "@react-rxjs/core" +import React from "react" +import { isActive$ } from "../common/paths.state" +import { useSubtreeFocus } from "../common/SubtreeFocus" +import { highlight, MissingData } from "./codec-components" + +export const CStruct: EditStruct = ({ innerComponents, value, path }) => { + const isActive = useStateObservable(isActive$(path.join("."))) + const focus = useSubtreeFocus() + const sub = focus.getNextPath(path) + if (sub) { + const field = Object.entries(innerComponents).find(([key]) => key === sub) + return field?.[1] + } + + if (value === NOTIN) { + return + } + + return ( + + {Object.entries(innerComponents).map(([key, jsx]) => ( + {jsx} + ))} + + ) +} diff --git a/src/codec-components/BinaryViewCodec/ListComponents.tsx b/src/codec-components/BinaryViewCodec/ListComponents.tsx new file mode 100644 index 0000000..ea3ff9a --- /dev/null +++ b/src/codec-components/BinaryViewCodec/ListComponents.tsx @@ -0,0 +1,60 @@ +import { + EditArray, + EditComplexCodecComponentProps, + EditSequence, + EditTuple, + NOTIN, +} from "@codec-components" +import { compact } from "@polkadot-api/substrate-bindings" +import { useStateObservable } from "@react-rxjs/core" +import React, { ReactNode } from "react" +import { isActive$ } from "../common/paths.state" +import { useSubtreeFocus } from "../common/SubtreeFocus" +import { + headerHighlight, + highlight, + MissingData, + toConcatHex, +} from "./codec-components" + +export const CSequence: EditSequence = (props) => + +export const CArray: EditArray = (props) => ( + +) + +export const CTuple: EditTuple = (props) => ( + +) + +export const ListDisplay: React.FC< + EditComplexCodecComponentProps & { + innerComponents: ReactNode[] + fixedLength?: boolean + } +> = (props) => { + const { innerComponents, fixedLength, value, path } = props + const isActive = useStateObservable(isActive$(path.join("."))) + const focus = useSubtreeFocus() + const sub = focus.getNextPath(path) + if (sub) { + return innerComponents[Number(sub)] + } + + if (value === NOTIN) { + return + } + + return ( + + {fixedLength ? null : ( + + {toConcatHex(compact.enc(value.length))} + + )} + {innerComponents.map((jsx, idx) => ( + {jsx} + ))} + + ) +} diff --git a/src/codec-components/BinaryViewCodec/codec-components.tsx b/src/codec-components/BinaryViewCodec/codec-components.tsx new file mode 100644 index 0000000..526cf92 --- /dev/null +++ b/src/codec-components/BinaryViewCodec/codec-components.tsx @@ -0,0 +1,87 @@ +import { + EditAccountId, + EditBigNumber, + EditBool, + EditBytes, + EditEthAccount, + EditNumber, + EditOption, + EditPrimitiveComponentProps, + EditResult, + EditStr, + EditVoid, + NOTIN, +} from "@codec-components" +import { u8 } from "@polkadot-api/substrate-bindings" +import { toHex } from "@polkadot-api/utils" +import { useStateObservable } from "@react-rxjs/core" +import { FC } from "react" +import { isActive$ } from "../common/paths.state" +import { useSubtreeFocus } from "../common/SubtreeFocus" + +export const CVoid: EditVoid = () => null + +const CPrimitive: FC> = ({ + encodedValue, + path, +}) => { + const isActive = useStateObservable(isActive$(path.join("."))) + return encodedValue ? ( + + + {toConcatHex(encodedValue)} + + + ) : ( + + ) +} +export const CBool: EditBool = CPrimitive +export const CStr: EditStr = CPrimitive +export const CEthAccount: EditEthAccount = CPrimitive +export const CBigNumber: EditBigNumber = CPrimitive +export const CNumber: EditNumber = CPrimitive +export const CAccountId: EditAccountId = CPrimitive +export const CBytes: EditBytes = CPrimitive + +export const COption: EditOption = ({ path, value, inner }) => { + const focus = useSubtreeFocus() + const sub = focus.getNextPath(path) + if (sub) { + return inner + } + if (value === NOTIN) return + + // TODO Looking at scale-ts, Option seems to be 00, 01 or 02! (None, Some(true), Some(false)) + // but can't find it at the docs? https://docs.substrate.io/reference/scale-codec/ + return ( + + {toConcatHex(u8.enc(value ? 1 : 0))} + {inner} + + ) +} + +export const CResult: EditResult = ({ value, inner, path }) => { + const focus = useSubtreeFocus() + const sub = focus.getNextPath(path) + if (sub) return inner + + if (value === NOTIN) return + + return ( + + {toConcatHex(u8.enc(value.success ? 0 : 1))} + {inner} + + ) +} + +export const MissingData = () => ( + (…) +) +export const toConcatHex = (value: Uint8Array) => toHex(value).slice(2) +export const highlight = (isActive: boolean) => + isActive ? "text-polkadot-400 mx-1" : "" +export const headerHighlight = (isActive: boolean) => + isActive ? "text-polkadot-500" : "" diff --git a/src/codec-components/BinaryViewCodec/components.ts b/src/codec-components/BinaryViewCodec/components.ts new file mode 100644 index 0000000..cd2e42f --- /dev/null +++ b/src/codec-components/BinaryViewCodec/components.ts @@ -0,0 +1,4 @@ +export * from "./CEnum" +export * from "./CStruct" +export * from "./codec-components" +export * from "./ListComponents" diff --git a/src/codec-components/BinaryViewCodec/index.ts b/src/codec-components/BinaryViewCodec/index.ts new file mode 100644 index 0000000..94489ff --- /dev/null +++ b/src/codec-components/BinaryViewCodec/index.ts @@ -0,0 +1,5 @@ +import { getCodecComponent } from "@codec-components" + +import * as treeComponents from "./components" + +export const BinaryViewCodec = getCodecComponent(treeComponents) diff --git a/src/components/BinaryEditButton.tsx b/src/components/BinaryEditButton.tsx index b1db788..bd3877e 100644 --- a/src/components/BinaryEditButton.tsx +++ b/src/components/BinaryEditButton.tsx @@ -180,6 +180,7 @@ const BinaryEditModalContent: FC< !isValid && "opacity-50", )} onClick={submit} + disabled={!isValid} > OK diff --git a/src/components/Expand.tsx b/src/components/Expand.tsx index 9b32149..8dc131e 100644 --- a/src/components/Expand.tsx +++ b/src/components/Expand.tsx @@ -1,12 +1,24 @@ -import { FC } from "react" +import { ComponentProps, FC } from "react" import { ChevronRight } from "lucide-react" -import { twMerge as clsx } from "tailwind-merge" +import { twMerge } from "tailwind-merge" -export const ExpandBtn: FC<{ - expanded: boolean -}> = ({ expanded }) => ( +export const ExpandBtn: FC< + { + expanded: boolean + direction?: "horizontal" | "vertical" + } & ComponentProps +> = ({ expanded, direction = "horizontal", ...props }) => ( ) diff --git a/src/pages/Extrinsics/BinaryDisplay.tsx b/src/pages/Extrinsics/BinaryDisplay.tsx index b9f0651..024ad08 100644 --- a/src/pages/Extrinsics/BinaryDisplay.tsx +++ b/src/pages/Extrinsics/BinaryDisplay.tsx @@ -1,57 +1,91 @@ +import { BinaryViewCodec } from "@/codec-components/BinaryViewCodec" import { BinaryEditButton } from "@/components/BinaryEditButton" import { CopyText } from "@/components/Copy" -import { NOTIN } from "@codec-components" +import { ExpandBtn } from "@/components/Expand" +import { + CodecComponentType, + CodecComponentUpdate, + CodecComponentValue, + MetadataType, + NOTIN, +} from "@codec-components" import { Binary, HexString } from "@polkadot-api/substrate-bindings" -import { FC } from "react" +import { toHex } from "@polkadot-api/utils" +import { FC, useState } from "react" +import { twMerge } from "tailwind-merge" -const DISPLAY_MAX_LEN = 1024 -const COPY_MAX_LEN = 5 * 1024 * 1024 export const BinaryDisplay: FC<{ - value: Uint8Array | HexString | null - isEmpty: boolean - onValueChanged: (value: any | NOTIN) => boolean - decode: (value: Uint8Array | HexString) => any | NOTIN -}> = ({ value, isEmpty, onValueChanged, decode }) => { - const displayLength = DISPLAY_MAX_LEN * 2 + 2 - - const copyLength = COPY_MAX_LEN * 2 + 2 - const hex = value - ? typeof value === "string" - ? value.slice(0, copyLength) - : Binary.fromBytes(value.slice(0, COPY_MAX_LEN)).asHex() - : null - const exceedsDisplayLength = hex && hex.length > displayLength - const exceedsCopyLength = !!hex && hex.length >= COPY_MAX_LEN * 2 + 2 - - const copyDisabled = hex === null || exceedsCopyLength + codecType: number + metadata: MetadataType + value: CodecComponentValue + onUpdate: (value: CodecComponentUpdate) => void + codec: { + enc: (value: any | NOTIN) => Uint8Array + dec: (value: Uint8Array | HexString) => any | NOTIN + } +}> = ({ codecType, metadata, value, onUpdate, codec }) => { + const [wrap, setWrap] = useState(false) + const encoded = (() => { + if (value.type === CodecComponentType.Initial) { + if (!value.value) return null + return typeof value.value === "string" + ? Binary.fromHex(value.value).asBytes() + : value.value + } + if (value.value.empty || !value.value.encoded) return null + return value.value.encoded + })() + const hex = encoded ? toHex(encoded) : null + const isEmpty = + (value.type === CodecComponentType.Initial && !value.value) || + (value.type === CodecComponentType.Updated && value.value.empty) return (
-
- -
- {hex ? ( - hex.slice(0, displayLength) + (exceedsDisplayLength ? "…" : "") - ) : ( +
+ +
+ {isEmpty ? (
- {isEmpty - ? "Start by filling out your extrinsic, or enter a binary using the edit binary button at the end of this line." - : "Cannot display hex. Complete the required values."} + Start by filling out your extrinsic, or enter a binary using the + edit binary button at the end of this line.
+ ) : ( + <> + 0x + + )}
- +
+ setWrap((v) => !v)} + /> + { + const encoded = codec.enc(decoded) + onUpdate({ empty: false, decoded, encoded }) + return true + }} + decode={codec.dec} + iconProps={{ + size: 24, + }} + /> +
) diff --git a/src/pages/Extrinsics/Extrinsics.tsx b/src/pages/Extrinsics/Extrinsics.tsx index 2c10a9a..e0626e4 100644 --- a/src/pages/Extrinsics/Extrinsics.tsx +++ b/src/pages/Extrinsics/Extrinsics.tsx @@ -2,11 +2,7 @@ import { metadata$ } from "@/chain.state" import { ActionButton } from "@/components/ActionButton" import { ButtonGroup } from "@/components/ButtonGroup" import { withSubscribe } from "@/components/withSuspense" -import { - CodecComponentType, - CodecComponentValue, - NOTIN, -} from "@codec-components" +import { CodecComponentType, CodecComponentValue } from "@codec-components" import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders" import { Binary } from "@polkadot-api/substrate-bindings" import { state, useStateObservable } from "@react-rxjs/core" @@ -28,17 +24,16 @@ const extrinsicProps$ = state( })), ), ) -const extrinsicDecoder = extrinsicProps$.pipeState( - map( - ({ metadata, codecType }) => - getDynamicBuilder(getLookupFn(metadata)).buildDefinition(codecType).dec, +const extrinsicCodec = extrinsicProps$.pipeState( + map(({ metadata, codecType }) => + getDynamicBuilder(getLookupFn(metadata)).buildDefinition(codecType), ), ) export const Extrinsics = withSubscribe(() => { const [viewMode, setViewMode] = useState<"edit" | "json">("edit") const extrinsicProps = useStateObservable(extrinsicProps$) - const decoder = useStateObservable(extrinsicDecoder) + const codec = useStateObservable(extrinsicCodec) const [componentValue, setComponentValue] = useState({ type: CodecComponentType.Initial, @@ -57,25 +52,12 @@ export const Extrinsics = withSubscribe(() => {
Extrinsics
+ setComponentValue({ type: CodecComponentType.Updated, value }) } - decode={(value) => { - try { - return decoder(value) - } catch (_) { - return NOTIN - } - }} - onValueChanged={(decoded) => { - setComponentValue({ - type: CodecComponentType.Updated, - value: { empty: false, decoded }, - }) - return true - }} />
@@ -112,7 +94,7 @@ export const Extrinsics = withSubscribe(() => { ? Binary.fromHex(binaryValue).asBytes() : binaryValue } - decode={decoder} + decode={codec.dec} /> )}