-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new `request` mechanism through which dApps can interact with the wallet with a JSON RPC API. Initially, this only supports: `stx_requestAccounts`, a method that requests public key information from the wallet. chore: tidy test app, adding request accounts chore: move request account logic to new file
- Loading branch information
1 parent
6a049e7
commit a0edbf1
Showing
42 changed files
with
3,187 additions
and
377 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
24 changes: 24 additions & 0 deletions
24
src/app/common/actions/send-request-account-response.spec.ts
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,24 @@ | ||
import { sendRequestAccountResponseToTab } from './send-request-account-response'; | ||
import { sendMessageToTab } from '@shared/messages'; | ||
|
||
jest.mock('@shared/messages', () => ({ | ||
sendMessageToTab: jest.fn().mockImplementation(() => null), | ||
})); | ||
|
||
describe(sendRequestAccountResponseToTab.name, () => { | ||
it('must only return to app with public keys', () => { | ||
sendRequestAccountResponseToTab({ | ||
tabId: '2', | ||
id: '1', | ||
account: { | ||
stxPublicKey: 'pubKey1', | ||
dataPublicKey: 'dataKey1', | ||
stxPrivateKey: 'lskdjfjsldf', | ||
} as any, | ||
}); | ||
expect(sendMessageToTab).toHaveBeenCalledTimes(1); | ||
expect(sendMessageToTab).toHaveBeenCalledWith(2, '1', { | ||
result: [{ dataPublicKey: 'dataKey1', stxPublicKey: 'pubKey1' }], | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { SoftwareWalletAccountWithAddress } from '@app/store/accounts/account.models'; | ||
import { sendMessageToTab } from '@shared/messages'; | ||
|
||
interface SendRequestAccountResponseToTabArgs { | ||
tabId: string; | ||
id: string; | ||
account: SoftwareWalletAccountWithAddress; | ||
} | ||
export function sendRequestAccountResponseToTab(args: SendRequestAccountResponseToTabArgs) { | ||
const { tabId, id, account } = args; | ||
|
||
const safeAccountKeys = { | ||
stxPublicKey: account.stxPublicKey, | ||
dataPublicKey: account.dataPublicKey, | ||
}; | ||
|
||
return sendMessageToTab(parseInt(tabId), id, { result: [safeAccountKeys] }); | ||
} | ||
|
||
export function sendUserDeniesAccountRequest({ tabId, id }: { tabId: string; id: string }) { | ||
return sendMessageToTab(parseInt(tabId), id, { error: { code: 4000, message: 'lskdjflksjdfl' } }); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { BoxProps } from '@stacks/ui'; | ||
|
||
import { SoftwareWalletAccountWithAddress } from '@app/store/accounts/account.models'; | ||
import { Title } from '../typography'; | ||
|
||
interface AccountTitlePlaceholderProps extends BoxProps { | ||
account: SoftwareWalletAccountWithAddress; | ||
} | ||
export function AccountTitlePlaceholder({ account, ...rest }: AccountTitlePlaceholderProps) { | ||
const name = `Account ${account?.index + 1}`; | ||
return ( | ||
<Title fontSize={2} lineHeight="1rem" fontWeight="400" {...rest}> | ||
{name} | ||
</Title> | ||
); | ||
} | ||
|
||
interface AccountTitleProps extends BoxProps { | ||
account: SoftwareWalletAccountWithAddress; | ||
name: string; | ||
} | ||
export function AccountTitle({ account, name, ...rest }: AccountTitleProps) { | ||
return ( | ||
<Title fontSize={2} lineHeight="1rem" fontWeight="400" {...rest}> | ||
{name} | ||
</Title> | ||
); | ||
} |
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,24 @@ | ||
import { Flex, Stack, Text } from '@stacks/ui'; | ||
|
||
import { AppIcon } from '@app/components/app-icon'; | ||
import { Title } from '@app/components/typography'; | ||
|
||
interface AccountPickerLayoutProps { | ||
appName?: string; | ||
children: React.ReactNode; | ||
} | ||
export function AccountPickerLayout(props: AccountPickerLayoutProps) { | ||
const { appName, children } = props; | ||
return ( | ||
<Flex flexDirection="column" px={['loose', 'unset']} width="100%"> | ||
<Stack spacing="loose" textAlign="center"> | ||
<AppIcon mt="extra-loose" mb="loose" size="72px" /> | ||
<Stack spacing="base"> | ||
<Title fontSize={4}>Choose an account</Title> | ||
<Text textStyle="caption">to connect to {appName}</Text> | ||
</Stack> | ||
</Stack> | ||
{children} | ||
</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
45 changes: 45 additions & 0 deletions
45
src/app/pages/account-authentication/account-authentication.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,45 @@ | ||
import { memo, useCallback, useEffect, useState } from 'react'; | ||
|
||
import { useRouteHeader } from '@app/common/hooks/use-route-header'; | ||
import { useWallet } from '@app/common/hooks/use-wallet'; | ||
import { useAppDetails } from '@app/common/hooks/auth/use-app-details'; | ||
import { Header } from '@app/components/header'; | ||
import { AccountPicker } from '@app/features/account-picker/accounts'; | ||
import { useOnboardingState } from '@app/common/hooks/auth/use-onboarding-state'; | ||
import { useAccounts } from '@app/store/accounts/account.hooks'; | ||
import { AccountPickerLayout } from '@app/features/account-picker/account-picker.layout'; | ||
|
||
export const AuthenticateAccount = memo(() => { | ||
const accounts = useAccounts(); | ||
const { name: appName } = useAppDetails(); | ||
const { decodedAuthRequest } = useOnboardingState(); | ||
const { cancelAuthentication, wallet, finishSignIn } = useWallet(); | ||
const [selectedAccountIndex, setSelectedAccountIndex] = useState<number | null>(null); | ||
|
||
useRouteHeader(<Header hideActions />); | ||
|
||
const signIntoAccount = async (index: number) => { | ||
setSelectedAccountIndex(index); | ||
await finishSignIn(index); | ||
}; | ||
|
||
const handleUnmount = useCallback(async () => { | ||
cancelAuthentication(); | ||
}, [cancelAuthentication]); | ||
|
||
useEffect(() => { | ||
window.addEventListener('beforeunload', handleUnmount); | ||
return () => window.removeEventListener('beforeunload', handleUnmount); | ||
}, [handleUnmount]); | ||
|
||
if (!wallet || !accounts || !decodedAuthRequest) return null; | ||
|
||
return ( | ||
<AccountPickerLayout appName={appName}> | ||
<AccountPicker | ||
onAccountSelected={index => signIntoAccount(index)} | ||
selectedAccountIndex={selectedAccountIndex} | ||
/> | ||
</AccountPickerLayout> | ||
); | ||
}); |
Oops, something went wrong.