-
Notifications
You must be signed in to change notification settings - Fork 143
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
feat: add rpc-get-xpub method #5203
Open
kyranjamie
wants to merge
1
commit into
dev
Choose a base branch
from
feat/rpc-get-xpub
base: dev
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 all commits
Commits
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 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,54 @@ | ||
import { Box, Flex, styled } from 'leather-styles/jsx'; | ||
|
||
import { RequesterFlag } from '@app/components/requester-flag'; | ||
import { Button } from '@app/ui/components/button/button'; | ||
import { LettermarkIcon } from '@app/ui/icons'; | ||
import { LogomarkIcon } from '@app/ui/icons/logomark-icon'; | ||
|
||
interface GetXpubLayoutProps { | ||
requester: string; | ||
onUserApproveGetXpub(): void; | ||
} | ||
export function GetXpubLayout(props: GetXpubLayoutProps) { | ||
const { requester, onUserApproveGetXpub } = props; | ||
|
||
return ( | ||
<Flex flexDirection="column" height="100vh" width="100%"> | ||
<Flex | ||
flex={1} | ||
flexDirection="column" | ||
textAlign="center" | ||
alignItems="center" | ||
p="space.06" | ||
gap="space.06" | ||
> | ||
<Box mb="space.08" mt="space.11"> | ||
<LogomarkIcon width="248px" height="58px" /> | ||
</Box> | ||
<styled.p textStyle="heading.03">Connect your account to</styled.p> | ||
|
||
<RequesterFlag requester={requester} /> | ||
<Box width="100%" display="flex"> | ||
<Button onClick={() => onUserApproveGetXpub()} fullWidth> | ||
<Flex justifyContent="center" alignItems="center"> | ||
<LettermarkIcon mr="space.02" /> | ||
<styled.span textStyle="label.02">Connect Leather</styled.span> | ||
</Flex> | ||
</Button> | ||
</Box> | ||
</Flex> | ||
<Flex | ||
px="space.05" | ||
py="space.03" | ||
lineHeight="20px" | ||
textAlign="center" | ||
alignSelf="bottom" | ||
bg="accent.background-secondary" | ||
> | ||
<styled.p textStyle="caption.02"> | ||
By connecting you give permission to {requester} to view Xpub of this account | ||
</styled.p> | ||
</Flex> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { closeWindow } from '@shared/utils'; | ||
|
||
import { GetXpubLayout } from './components/get-xpub.layout'; | ||
import { useGetXpub } from './use-request-accounts'; | ||
|
||
export function RpcGetXpub() { | ||
const { origin, onUserApproveGetXpub } = useGetXpub(); | ||
|
||
if (origin === null) { | ||
closeWindow(); | ||
throw new Error('Origin is null'); | ||
} | ||
|
||
const requester = new URL(origin).host; | ||
|
||
return <GetXpubLayout requester={requester} onUserApproveGetXpub={onUserApproveGetXpub} />; | ||
} |
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,58 @@ | ||
import { deriveNativeSegwitAccountFromRootKeychain } from '@shared/crypto/bitcoin/p2wpkh-address-gen'; | ||
import { logger } from '@shared/logger'; | ||
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods'; | ||
import { closeWindow } from '@shared/utils'; | ||
|
||
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; | ||
import { mnemonicToRootNode } from '@app/common/keychain/keychain'; | ||
import { useRpcRequestParams } from '@app/common/rpc-helpers'; | ||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useAppPermissions } from '@app/store/app-permissions/app-permissions.slice'; | ||
import { useDefaultWalletSecretKey } from '@app/store/in-memory-key/in-memory-key.selectors'; | ||
|
||
export function useGetXpub() { | ||
const analytics = useAnalytics(); | ||
|
||
const permissions = useAppPermissions(); | ||
const { tabId, origin, requestId } = useRpcRequestParams(); | ||
|
||
const currentAccountIndex = useCurrentAccountIndex(); | ||
const secretKey = useDefaultWalletSecretKey(); | ||
const rootKey = secretKey ? mnemonicToRootNode(secretKey) : null; | ||
|
||
return { | ||
origin, | ||
onUserApproveGetXpub() { | ||
if (!tabId || !origin) { | ||
logger.error('Cannot give app accounts: missing tabId, origin'); | ||
return; | ||
} | ||
|
||
const keysToIncludeInResponse = []; | ||
|
||
if (rootKey) { | ||
const createBitcoinAccount = deriveNativeSegwitAccountFromRootKeychain(rootKey, 'mainnet'); | ||
const currentBitcoinAccount = createBitcoinAccount(currentAccountIndex); | ||
|
||
const nativeSegwitXpubResponse: any = { | ||
symbol: 'BTC', | ||
type: 'p2wpkh', | ||
xpub: currentBitcoinAccount.keychain.publicExtendedKey, | ||
}; | ||
|
||
keysToIncludeInResponse.push(nativeSegwitXpubResponse); | ||
} | ||
|
||
void analytics.track('user_approved_get_xpub', { origin }); | ||
permissions.hasRequestedAccounts(origin); | ||
chrome.tabs.sendMessage( | ||
tabId, | ||
makeRpcSuccessResponse('getXpub', { | ||
id: requestId, | ||
result: { xpubs: keysToIncludeInResponse as any }, | ||
}) | ||
); | ||
closeWindow(); | ||
}, | ||
}; | ||
} |
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,30 @@ | ||
import { RpcErrorCode } from '@btckit/types'; | ||
|
||
import { RouteUrls } from '@shared/route-urls'; | ||
import { GetXpubRequest } from '@shared/rpc/methods/get-xpub'; | ||
import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods'; | ||
|
||
import { | ||
listenForPopupClose, | ||
makeSearchParamsWithDefaults, | ||
triggerRequestWindowOpen, | ||
} from '../messaging-utils'; | ||
|
||
export async function rpcGetXpub(message: GetXpubRequest, port: chrome.runtime.Port) { | ||
const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]); | ||
const { id } = await triggerRequestWindowOpen(RouteUrls.RpcGetXpub, urlParams); | ||
listenForPopupClose({ | ||
tabId, | ||
id, | ||
response: { | ||
id: message.id, | ||
result: makeRpcErrorResponse('getXpub', { | ||
id: message.id, | ||
error: { | ||
code: RpcErrorCode.USER_REJECTION, | ||
message: 'User rejected request to get xpub', | ||
}, | ||
}), | ||
}, | ||
}); | ||
} |
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,7 @@ | ||
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types'; | ||
|
||
export type GetXpubRequest = RpcRequest<'getXpub'>; | ||
|
||
type GetXpubResponse = RpcResponse<{ xpubs: [] }>; | ||
|
||
export type GetXpub = DefineRpcMethod<GetXpubRequest, GetXpubResponse>; |
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,43 @@ | ||
import { BrowserContext, Page } from '@playwright/test'; | ||
import { TEST_ACCOUNT_1_XPUB } from '@tests/mocks/constants'; | ||
|
||
import { test } from '../../fixtures/fixtures'; | ||
|
||
test.describe('RPC get Xpub', () => { | ||
test.beforeEach(async ({ extensionId, globalPage, onboardingPage, page }) => { | ||
await globalPage.setupAndUseApiCalls(extensionId); | ||
await onboardingPage.signInWithTestAccount(extensionId); | ||
await page.goto('localhost:3000', { waitUntil: 'networkidle' }); | ||
}); | ||
|
||
function checkVisibleContent(context: BrowserContext) { | ||
return async (buttonToPress: 'Cancel' | 'Confirm') => { | ||
const popup = await context.waitForEvent('page'); | ||
await popup.waitForTimeout(500); | ||
const btn = popup.locator('text="Connect Leather"'); | ||
|
||
if (buttonToPress === 'Confirm') { | ||
await btn.click(); | ||
} else { | ||
await popup.close(); | ||
} | ||
}; | ||
} | ||
|
||
function initiateRPCGetXpub(page: Page) { | ||
return async () => | ||
page.evaluate(async () => | ||
(window as any).LeatherProvider.request('getXpub').catch((e: unknown) => e) | ||
); | ||
} | ||
|
||
test('that xpub is correct', async ({ page, context }) => { | ||
const xpub = TEST_ACCOUNT_1_XPUB; | ||
const [result] = await Promise.all([ | ||
initiateRPCGetXpub(page)(), | ||
checkVisibleContent(context)('Confirm'), | ||
]); | ||
|
||
test.expect(result.result.xpubs[0].xpub).toEqual(xpub); | ||
}); | ||
}); |
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.
This logged error doesn't seem to have the right message, was this just copied?