Skip to content

Commit

Permalink
🚧 --wip-- [skip ci]]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Jan 10, 2025
1 parent 4f01dfe commit 9af8aea
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export type DeviceActionProps<
debug?: boolean,
) => ExecuteDeviceActionReturnType<Output, Error, IntermediateValue>;
initialValues: Input;
InputValuesComponent?: React.FC<{
initialValues: Input;
onChange: (values: Input) => void;
valueSelector?: ValueSelector<FieldType>;
disabled?: boolean;
}>;
validateValues?: (args: Input) => boolean;
valueSelector?: ValueSelector<FieldType>;
deviceModelId: DeviceModelId;
Expand Down Expand Up @@ -94,6 +100,7 @@ export function DeviceActionTester<
executeDeviceAction,
valueSelector,
validateValues,
InputValuesComponent,
} = props;

const nonce = useRef(-1);
Expand Down Expand Up @@ -204,12 +211,21 @@ export function DeviceActionTester<
rowGap={3}
pointerEvents={loading ? "none" : "auto"}
>
<CommandForm
initialValues={values}
onChange={setValues}
valueSelector={valueSelector}
disabled={loading}
/>
{InputValuesComponent ? (
<InputValuesComponent
initialValues={values}
onChange={setValues}
valueSelector={valueSelector}
disabled={loading}
/>
) : (
<CommandForm
initialValues={values}
onChange={setValues}
valueSelector={valueSelector}
disabled={loading}
/>
)}
<Divider />
<Switch
checked={inspect}
Expand Down
65 changes: 57 additions & 8 deletions apps/sample/src/components/SignerBtcView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from "react";
import React, { useEffect, useMemo } from "react";
import {
DefaultDescriptorTemplate,
DefaultWallet,
Expand All @@ -13,13 +13,62 @@ import {
type SignPsbtDAIntermediateValue,
type SignPsbtDAOutput,
} from "@ledgerhq/device-signer-kit-bitcoin";
import { Flex, Input, SelectInput } from "@ledgerhq/react-ui";

import { DeviceActionsList } from "@/components/DeviceActionsView/DeviceActionsList";
import { type DeviceActionProps } from "@/components/DeviceActionsView/DeviceActionTester";
import { useForm } from "@/hooks/useForm";
import { useDmk } from "@/providers/DeviceManagementKitProvider";

const DEFAULT_DERIVATION_PATH = "84'/0'/0'";

type SignPsbtInputValuesType = {
psbt: string;
path: string;
descriptorTemplate: DefaultDescriptorTemplate;
};

const SignPsbtDAInputValues: React.FC<{
initialValues: SignPsbtInputValuesType;
onChange: (values: SignPsbtInputValuesType) => void;
disabled?: boolean;
}> = ({ initialValues, onChange, disabled }) => {
const { formValues, setFormValue } = useForm(initialValues);

useEffect(() => {
onChange(formValues);
}, [formValues, onChange]);

return (
<Flex flexDirection="column" flexWrap="wrap" rowGap={5} columnGap={5}>
<Flex
flexDirection="column"
alignItems="flex-start"
rowGap={3}
columnGap={3}
>
<Input
id="psbt"
value={formValues.psbt}
placeholder="psbt"
onChange={(newVal) => setFormValue("psbt", newVal)}
disabled={disabled}
data-testid={`input-text_psbt`}
/>
<SelectInput
options={Object.entries(DefaultDescriptorTemplate).map(
([key, value]) => ({
label: key,
value,
}),
)}
/>
<Input />
</Flex>
</Flex>
);
};

export const SignerBtcView: React.FC<{ sessionId: string }> = ({
sessionId,
}) => {
Expand Down Expand Up @@ -87,29 +136,29 @@ export const SignerBtcView: React.FC<{ sessionId: string }> = ({
title: "Sign psbt",
description:
"Perform all the actions necessary to sign a PSBT with the device",
executeDeviceAction: ({ derivationPath, psbt }) => {
executeDeviceAction: ({ descriptorTemplate, psbt, path }) => {
if (!signer) {
throw new Error("Signer not initialized");
}

return signer.signPsbt(
new DefaultWallet(
derivationPath,
DefaultDescriptorTemplate.NATIVE_SEGWIT,
),
new DefaultWallet(path, descriptorTemplate),
psbt,
);
},
InputValuesComponent: SignPsbtDAInputValues,
initialValues: {
derivationPath: DEFAULT_DERIVATION_PATH,
descriptorTemplate: DefaultDescriptorTemplate.NATIVE_SEGWIT,
psbt: "",
path: DEFAULT_DERIVATION_PATH,
},
deviceModelId,
} satisfies DeviceActionProps<
SignPsbtDAOutput,
{
psbt: string;
derivationPath: string;
path: string;
descriptorTemplate: DefaultDescriptorTemplate;
},
SignPsbtDAError,
SignPsbtDAIntermediateValue
Expand Down
2 changes: 1 addition & 1 deletion apps/sample/src/styles/globalstyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const GlobalStyle = createGlobalStyle`
background-color: #000000;
}
body {
user-select: none;
user-select: text;
}
.no-scrollbar {
&::-webkit-scrollbar {
Expand Down

0 comments on commit 9af8aea

Please sign in to comment.