-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💄 (smpl): Custom device action input form for signpsbt
- Loading branch information
1 parent
4f01dfe
commit cfdf8dd
Showing
5 changed files
with
132 additions
and
14 deletions.
There are no files selected for viewing
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
100 changes: 100 additions & 0 deletions
100
apps/sample/src/components/SignerBtcView/SignPsbtDAInputValusForm.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,100 @@ | ||
import React, { useCallback, useEffect } from "react"; | ||
import { DefaultDescriptorTemplate } from "@ledgerhq/device-signer-kit-bitcoin"; | ||
import { Flex, Input, SelectInput, Text } from "@ledgerhq/react-ui"; | ||
|
||
import { useForm } from "@/hooks/useForm"; | ||
|
||
type SignPsbtInputValuesType = { | ||
psbt: string; | ||
path: string; | ||
descriptorTemplate: DefaultDescriptorTemplate; | ||
}; | ||
|
||
const descriptorTemplateToDerivationPath: Record< | ||
DefaultDescriptorTemplate, | ||
string | ||
> = { | ||
[DefaultDescriptorTemplate.NATIVE_SEGWIT]: "86'/0'/0'", | ||
[DefaultDescriptorTemplate.TAPROOT]: "84'/0'/0'", | ||
[DefaultDescriptorTemplate.NESTED_SEGWIT]: "49'/0'/0'", | ||
[DefaultDescriptorTemplate.LEGACY]: "44'/0'/0'", | ||
}; | ||
|
||
const descriptorTemplateToLabel = { | ||
[DefaultDescriptorTemplate.NATIVE_SEGWIT]: "Native Segwit", | ||
[DefaultDescriptorTemplate.TAPROOT]: "Taproot", | ||
[DefaultDescriptorTemplate.NESTED_SEGWIT]: "Nested Segwit", | ||
[DefaultDescriptorTemplate.LEGACY]: "Legacy", | ||
}; | ||
|
||
export const SignPsbtDAInputValuesForm: React.FC<{ | ||
initialValues: SignPsbtInputValuesType; | ||
onChange: (values: SignPsbtInputValuesType) => void; | ||
disabled?: boolean; | ||
}> = ({ initialValues, onChange, disabled }) => { | ||
const { formValues, setFormValues, setFormValue } = useForm(initialValues); | ||
|
||
useEffect(() => { | ||
onChange(formValues); | ||
}, [formValues, onChange]); | ||
|
||
const onWalletDescriptorTemplateChange = useCallback( | ||
(value: DefaultDescriptorTemplate) => { | ||
const newValues = { | ||
path: descriptorTemplateToDerivationPath[value], | ||
descriptorTemplate: value, | ||
}; | ||
setFormValues((prev) => ({ ...prev, ...newValues })); | ||
}, | ||
[setFormValues], | ||
); | ||
|
||
return ( | ||
<Flex | ||
flexDirection="column" | ||
flex={1} | ||
rowGap={6} | ||
columnGap={6} | ||
flexWrap="wrap" | ||
> | ||
<Flex flexDirection="row" alignItems="center" mb={4}> | ||
<Text style={{ marginRight: 8 }}>Wallet address type</Text> | ||
<SelectInput | ||
options={Object.entries(DefaultDescriptorTemplate).map( | ||
([_key, value]) => ({ | ||
label: descriptorTemplateToLabel[value], | ||
value, | ||
}), | ||
)} | ||
value={{ | ||
label: descriptorTemplateToLabel[formValues.descriptorTemplate], | ||
value: formValues.descriptorTemplate, | ||
}} | ||
isMulti={false} | ||
isSearchable={false} | ||
onChange={(newVal) => | ||
newVal && onWalletDescriptorTemplateChange(newVal.value) | ||
} | ||
/> | ||
</Flex> | ||
|
||
<Input | ||
id="path" | ||
value={formValues.path} | ||
placeholder="path" | ||
onChange={(newVal) => setFormValue("path", newVal)} | ||
disabled={disabled} | ||
data-testid="input-text_path" | ||
/> | ||
|
||
<Input | ||
id="psbt" | ||
value={formValues.psbt} | ||
placeholder="psbt" | ||
onChange={(newVal) => setFormValue("psbt", newVal)} | ||
disabled={disabled} | ||
data-testid="input-text_psbt" | ||
/> | ||
</Flex> | ||
); | ||
}; |
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