-
Notifications
You must be signed in to change notification settings - Fork 17
feat(ng): ledger BTC wallet policies #476
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
Open
meeh0w
wants to merge
2
commits into
main
Choose a base branch
from
feat/ng-ledger-btc-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
...ext/src/components/ledger/LedgerRegisterBtcWalletPolicy/LedgerRegisterBtcWalletPolicy.tsx
This file contains hidden or 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,87 @@ | ||
| import { FC } from 'react'; | ||
| import { Button } from '@avalabs/k2-alpine'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { Page } from '@/components/Page'; | ||
| import { SlideUpDialog } from '@/components/Dialog'; | ||
|
|
||
| import { PolicyRegistrationState } from './types'; | ||
| import { usePolicyRegistrationState } from './hooks'; | ||
| import { ConfirmPublicKey, SetupWalletPolicy } from './components'; | ||
|
|
||
| const pageProps = { | ||
| descriptionProps: { | ||
| maxWidth: '90%', | ||
| }, | ||
| contentProps: { | ||
| width: '100%', | ||
| justifyContent: 'space-between', | ||
| }, | ||
| }; | ||
|
|
||
| export const LedgerRegisterBtcWalletPolicy: FC = () => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| const { status, xpub, dismiss, policyName, policyDerivationPath } = | ||
| usePolicyRegistrationState(); | ||
|
|
||
| const isPubkeyPhase = status.startsWith('pubkey:'); | ||
| const phase = isPubkeyPhase ? 'pubkey' : 'policy'; | ||
|
|
||
| const Content = ComponentByPhase[phase]; | ||
|
|
||
| console.log('DEBUG status', status, policyName); | ||
|
||
|
|
||
| return ( | ||
| <SlideUpDialog open={status !== 'idle' && status !== 'dismissed'}> | ||
| <Page | ||
| withBackButton | ||
| onBack={dismiss} | ||
| title={ | ||
| isPubkeyPhase | ||
| ? t('Please confirm the public key displayed on your Ledger') | ||
| : t('Set up a wallet policy in the Bitcoin app') | ||
| } | ||
| description={ | ||
| isPubkeyPhase | ||
| ? '' | ||
| : t( | ||
| 'Ledger requires you to set up a wallet policy in the Bitcoin app. Please approve or reject this action on your Ledger device.', | ||
| ) | ||
| } | ||
| {...pageProps} | ||
| > | ||
| <Content | ||
| status={status} | ||
| xpub={xpub} | ||
| policyName={policyName} | ||
| policyDerivationPath={policyDerivationPath} | ||
| dismiss={dismiss} | ||
| /> | ||
| <Button | ||
| variant="contained" | ||
| color={status === 'policy:success' ? 'primary' : 'secondary'} | ||
| size="extension" | ||
| fullWidth | ||
| onClick={dismiss} | ||
| loading={status.endsWith(':pending')} | ||
| disabled={status.endsWith(':pending')} | ||
| > | ||
| {status === 'policy:success' | ||
| ? t('Done') | ||
| : status.endsWith(':pending') | ||
| ? t('Waiting for approval...') | ||
| : t('Dismiss')} | ||
| </Button> | ||
| </Page> | ||
| </SlideUpDialog> | ||
| ); | ||
| }; | ||
|
|
||
| const ComponentByPhase: Record< | ||
| 'pubkey' | 'policy', | ||
| FC<PolicyRegistrationState> | ||
| > = { | ||
| pubkey: ConfirmPublicKey, | ||
| policy: SetupWalletPolicy, | ||
| }; | ||
25 changes: 25 additions & 0 deletions
25
.../next/src/components/ledger/LedgerRegisterBtcWalletPolicy/components/ConfirmPublicKey.tsx
This file contains hidden or 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,25 @@ | ||
| import { FC } from 'react'; | ||
| import { Stack } from '@avalabs/k2-alpine'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { Card } from '@/components/Card'; | ||
|
|
||
| import { PolicyRegistrationState } from '../types'; | ||
| import { DetailRow } from './DetailRow'; | ||
| import { StatusCard } from './StatusCard'; | ||
|
|
||
| export const ConfirmPublicKey: FC<PolicyRegistrationState> = ({ | ||
| policyDerivationPath, | ||
| status, | ||
| }) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| return ( | ||
| <Stack gap={1} width="100%"> | ||
| <Card> | ||
| <DetailRow label={t('Path')} value={policyDerivationPath ?? ''} /> | ||
| </Card> | ||
| <StatusCard status={status} /> | ||
| </Stack> | ||
| ); | ||
| }; |
31 changes: 31 additions & 0 deletions
31
apps/next/src/components/ledger/LedgerRegisterBtcWalletPolicy/components/DetailRow.tsx
This file contains hidden or 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,31 @@ | ||
| import { Typography } from '@avalabs/k2-alpine'; | ||
| import { Stack } from '@avalabs/k2-alpine'; | ||
| import { FC } from 'react'; | ||
|
|
||
| type Props = { | ||
| label: string; | ||
| value: string | React.ReactElement; | ||
| }; | ||
|
|
||
| export const DetailRow: FC<Props> = ({ label, value }) => ( | ||
| <Stack | ||
| direction="row" | ||
| gap={1} | ||
| px={2} | ||
| py={0.5} | ||
| minHeight={36} | ||
| alignItems="center" | ||
| justifyContent="space-between" | ||
| > | ||
| <Stack direction="row" alignItems="center" gap={0.5}> | ||
| <Typography variant="body3" sx={{ whiteSpace: 'nowrap' }}> | ||
| {label} | ||
| </Typography> | ||
| </Stack> | ||
| {typeof value === 'string' ? ( | ||
| <Typography variant="body3">{value}</Typography> | ||
| ) : ( | ||
| value | ||
| )} | ||
| </Stack> | ||
| ); |
30 changes: 30 additions & 0 deletions
30
...next/src/components/ledger/LedgerRegisterBtcWalletPolicy/components/SetupWalletPolicy.tsx
This file contains hidden or 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,30 @@ | ||
| import { FC } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { Divider, Stack } from '@avalabs/k2-alpine'; | ||
|
|
||
| import { Card } from '@/components/Card'; | ||
|
|
||
| import { PolicyRegistrationState } from '../types'; | ||
| import { DetailRow } from './DetailRow'; | ||
| import { StatusCard } from './StatusCard'; | ||
|
|
||
| const POLICY_DESCRIPTOR = 'wpkh(@0/**)'; | ||
|
|
||
| export const SetupWalletPolicy: FC<PolicyRegistrationState> = ({ | ||
| policyName, | ||
| status, | ||
| }) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| return ( | ||
| <Stack gap={1} width="100%"> | ||
| <Card> | ||
| <Stack divider={<Divider />}> | ||
| <DetailRow label={t('Name')} value={policyName ?? ''} /> | ||
| <DetailRow label={t('Policy')} value={POLICY_DESCRIPTOR} /> | ||
| </Stack> | ||
| </Card> | ||
| <StatusCard status={status} /> | ||
| </Stack> | ||
| ); | ||
| }; |
61 changes: 61 additions & 0 deletions
61
apps/next/src/components/ledger/LedgerRegisterBtcWalletPolicy/components/StatusCard.tsx
This file contains hidden or 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,61 @@ | ||
| import { FC } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { CircularProgress, Stack, Typography } from '@avalabs/k2-alpine'; | ||
|
|
||
| import { Card } from '@/components/Card'; | ||
|
|
||
| import { PhaseStatus, PolicyRegistrationState, Status } from '../types'; | ||
| import { DetailRow } from './DetailRow'; | ||
|
|
||
| export const StatusCard: FC<Pick<PolicyRegistrationState, 'status'>> = ({ | ||
| status, | ||
| }) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| return ( | ||
| <Card> | ||
| <DetailRow | ||
| label={t('Status')} | ||
| value={<PhaseStatusIndicator phaseStatus={getPhaseStatus(status)} />} | ||
| /> | ||
| </Card> | ||
| ); | ||
| }; | ||
|
|
||
| const PhaseStatusIndicator: FC<{ phaseStatus: PhaseStatus }> = ({ | ||
| phaseStatus, | ||
| }) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| if (phaseStatus === 'error') { | ||
| return ( | ||
| <Typography variant="body3" color="error.main"> | ||
| {t('An error occurred, please try again later')} | ||
| </Typography> | ||
| ); | ||
| } | ||
| if (phaseStatus === 'incorrect-device') { | ||
| return ( | ||
| <Typography variant="body3" color="error.main"> | ||
| {t('This Ledger was not used to create this wallet')} | ||
| </Typography> | ||
| ); | ||
| } | ||
|
|
||
| if (phaseStatus === 'pending') { | ||
| return ( | ||
| <Stack direction="row" alignItems="end"> | ||
| <CircularProgress size={16} /> | ||
| </Stack> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Typography variant="body3" color="success"> | ||
| {t('Success!')} | ||
| </Typography> | ||
| ); | ||
| }; | ||
|
|
||
| const getPhaseStatus = (status: Status): PhaseStatus => | ||
| status.split(':')[1] as PhaseStatus; |
2 changes: 2 additions & 0 deletions
2
apps/next/src/components/ledger/LedgerRegisterBtcWalletPolicy/components/index.ts
This file contains hidden or 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,2 @@ | ||
| export * from './ConfirmPublicKey'; | ||
| export * from './SetupWalletPolicy'; |
1 change: 1 addition & 0 deletions
1
apps/next/src/components/ledger/LedgerRegisterBtcWalletPolicy/hooks/index.ts
This file contains hidden or 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 @@ | ||
| export * from './usePolicyRegistrationState'; |
Oops, something went wrong.
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.
titleProps seems like it is not getting used in the Page component. Do we need this one still?
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.
Nope, good catch 👌