Skip to content
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: introduce getXpub RPC method #4958

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/app/pages/rpc-get-xpub/components/get-xpub.layout.tsx
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 { LeatherIcon } from '@app/ui/components/icons/leather-icon';
import { LeatherLIcon } from '@app/ui/components/icons/leather-l-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">
<LeatherIcon 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">
<LeatherLIcon 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>
);
}
22 changes: 22 additions & 0 deletions src/app/pages/rpc-get-xpub/rpc-get-xpub.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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}
/>
);
}
58 changes: 58 additions & 0 deletions src/app/pages/rpc-get-xpub/use-request-accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 {useRpcRequestParams} from '@app/common/rpc-helpers';
import {useAppPermissions} from '@app/store/app-permissions/app-permissions.slice';
import {useCurrentAccountIndex} from "@app/store/accounts/account";
import {useDefaultWalletSecretKey} from "@app/store/in-memory-key/in-memory-key.selectors";
import {mnemonicToRootNode} from "@app/common/keychain/keychain";
import {deriveNativeSegwitAccountFromRootKeychain} from "@shared/crypto/bitcoin/p2wpkh-address-gen";


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();
},
};
}
9 changes: 9 additions & 0 deletions src/app/routes/rpc-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RouteUrls } from '@shared/route-urls';
import { ledgerBitcoinTxSigningRoutes } from '@app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container';
import { ledgerStacksMessageSigningRoutes } from '@app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg.routes';
import { RpcGetAddresses } from '@app/pages/rpc-get-addresses/rpc-get-addresses';
import { RpcGetXpub } from '@app/pages/rpc-get-xpub/rpc-get-xpub';
import { rpcSendTransferRoutes } from '@app/pages/rpc-send-transfer/rpc-send-transfer.routes';
import { RpcSignPsbt } from '@app/pages/rpc-sign-psbt/rpc-sign-psbt';
import { RpcSignPsbtSummary } from '@app/pages/rpc-sign-psbt/rpc-sign-psbt-summary';
Expand All @@ -24,6 +25,14 @@ export const rpcRequestRoutes = (
</AccountGate>
}
/>
<Route
path={RouteUrls.RpcGetXpub}
element={
<AccountGate>
<RpcGetXpub />
</AccountGate>
}
/>
{rpcSendTransferRoutes}
<Route
path={RouteUrls.RpcSignBip322Message}
Expand Down
6 changes: 6 additions & 0 deletions src/background/messaging/rpc-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { rpcSignMessage } from './rpc-methods/sign-message';
import { rpcSignPsbt } from './rpc-methods/sign-psbt';
import { rpcSignStacksMessage } from './rpc-methods/sign-stacks-message';
import { rpcSupportedMethods } from './rpc-methods/supported-methods';
import {rpcGetXpub} from "@background/messaging/rpc-methods/get-xpub";

export async function rpcMessageHandler(message: WalletRequests, port: chrome.runtime.Port) {
switch (message.method) {
Expand All @@ -20,6 +21,11 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru
break;
}

case 'getXpub': {
await rpcGetXpub(message, port);
break;
}

case 'signMessage': {
await rpcSignMessage(message, port);
break;
Expand Down
30 changes: 30 additions & 0 deletions src/background/messaging/rpc-methods/get-xpub.ts
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 { makeRpcErrorResponse } from '@shared/rpc/rpc-methods';

import {
listenForPopupClose,
makeSearchParamsWithDefaults,
triggerRequestWindowOpen,
} from '../messaging-utils';
import {GetXpubRequest} from "@shared/rpc/methods/get-xpub";

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',
},
}),
},
});
}
1 change: 1 addition & 0 deletions src/shared/route-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export enum RouteUrls {

// Request routes bitcoin
RpcGetAddresses = '/get-addresses',
RpcGetXpub = '/get-xpub',
RpcSignPsbt = '/sign-psbt',
RpcSignPsbtSummary = '/sign-psbt/summary',
RpcSendTransfer = '/send-transfer',
Expand Down
8 changes: 8 additions & 0 deletions src/shared/rpc/methods/get-xpub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';


export type GetXpubRequest = RpcRequest<'getXpub'>;

type GetXpubResponse = RpcResponse<{ xpubs: [] }>;

export type GetXpub = DefineRpcMethod<GetXpubRequest, GetXpubResponse>;
4 changes: 3 additions & 1 deletion src/shared/rpc/rpc-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import { AcceptBitcoinContract } from './methods/accept-bitcoin-contract';
import { SignPsbt } from './methods/sign-psbt';
import { SignStacksMessage } from './methods/sign-stacks-message';
import { SupportedMethods } from './methods/supported-methods';
import {GetXpub} from "@shared/rpc/methods/get-xpub";

// Supports BtcKit methods, as well as custom Leather methods
export type WalletMethodMap = BtcKitMethodMap &
SupportedMethods &
SignPsbt &
AcceptBitcoinContract &
SignStacksTransaction &
SignStacksMessage;
SignStacksMessage &
GetXpub;

export type WalletRequests = ValueOf<WalletMethodMap>['request'];
export type WalletResponses = ValueOf<WalletMethodMap>['response'];
Expand Down