chore: restore gaia functionality

This commit is contained in:
kyranjamie
2023-07-03 11:08:55 +02:00
committed by kyranjamie
parent 51b7da0a84
commit bf136dcc7b
3 changed files with 67 additions and 7 deletions

View File

@@ -1,7 +1,12 @@
import { useCallback } from 'react';
import { bytesToHex } from '@stacks/common';
import { makeAuthResponse } from '@stacks/wallet-sdk';
import {
createWalletGaiaConfig,
getOrCreateWalletConfig,
makeAuthResponse,
updateWalletConfigWithApp,
} from '@stacks/wallet-sdk';
import { finalizeAuthResponse } from '@shared/actions/finalize-auth-response';
import { gaiaUrl } from '@shared/constants';
@@ -13,16 +18,21 @@ import { useKeyActions } from '@app/common/hooks/use-key-actions';
import { useWalletType } from '@app/common/use-wallet-type';
import { useNativeSegwitNetworkSigners } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useTaprootNetworkSigners } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
import { useStacksAccounts } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import {
useLegacyStacksWallet,
useStacksAccounts,
} from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
export function useFinishAuthRequest() {
const { decodedAuthRequest, authRequest } = useOnboardingState();
const { decodedAuthRequest, authRequest, appIcon, appName } = useOnboardingState();
const keyActions = useKeyActions();
const stacksAccounts = useStacksAccounts();
const { walletType } = useWalletType();
const accounts = useStacksAccounts();
const { origin, tabId } = useAuthRequestParams();
const wallet = useLegacyStacksWallet();
// TODO: It would be good to separate out finishing auth by the wallet vs an app
// so that the additional data we provide apps can be removed from our onboarding.
// Currently, these create errors unless early returns are used in the keychain code.
@@ -43,6 +53,28 @@ export function useFinishAuthRequest() {
// We can't perform any of this logic for non-software wallets
// as they require the key to be available in the JS context
if (walletType === 'software' && account.type === 'software') {
if (!wallet) return;
const gaiaHubConfig = await createWalletGaiaConfig({ gaiaHubUrl: gaiaUrl, wallet });
const walletConfig = await getOrCreateWalletConfig({
wallet,
gaiaHubConfig,
skipUpload: true,
});
await updateWalletConfigWithApp({
wallet,
walletConfig,
gaiaHubConfig,
account,
app: {
origin: appURL.origin,
lastLoginAt: new Date().getTime(),
scopes: decodedAuthRequest.scopes,
appIcon: appIcon as string,
name: appName as string,
},
});
const taprootAccount = deriveAllTaprootNetworkSigners(accountIndex);
const nativeSegwitAccount = deriveAllNativeSegWitNetworkSigners(accountIndex);
@@ -82,12 +114,15 @@ export function useFinishAuthRequest() {
},
[
accounts,
stacksAccounts,
decodedAuthRequest,
authRequest,
stacksAccounts,
origin,
tabId,
walletType,
wallet,
appIcon,
appName,
deriveAllTaprootNetworkSigners,
deriveAllNativeSegWitNetworkSigners,
keyActions,

View File

@@ -2,7 +2,10 @@ import { useMemo } from 'react';
import { useAtomValue } from 'jotai';
import { stacksAccountState } from '@app/store/accounts/blockchain/stacks/stacks-accounts';
import {
legacyStackWallet,
stacksAccountState,
} from '@app/store/accounts/blockchain/stacks/stacks-accounts';
import { useSignatureRequestAccountIndex } from '@app/store/signatures/requests.hooks';
import { useTransactionRequestState } from '@app/store/transactions/requests.hooks';
import { transactionNetworkVersionState } from '@app/store/transactions/transaction';
@@ -54,3 +57,12 @@ export function useTransactionAccountIndex() {
export function useTransactionNetworkVersion() {
return useAtomValue(transactionNetworkVersionState);
}
/**
* @deprecated
* This exists only to serve the remaining Gaia functionality.
* Do not perpetuate its use.
*/
export function useLegacyStacksWallet() {
return useAtomValue(legacyStackWallet);
}

View File

@@ -9,7 +9,7 @@ import {
pubKeyfromPrivKey,
publicKeyToAddress,
} from '@stacks/transactions';
import { deriveStxPrivateKey } from '@stacks/wallet-sdk';
import { deriveStxPrivateKey, generateWallet } from '@stacks/wallet-sdk';
import { atom } from 'jotai';
import { DATA_DERIVATION_PATH, deriveStacksSalt } from '@shared/crypto/stacks/stacks-address-gen';
@@ -18,7 +18,10 @@ import { derivePublicKey } from '@app/common/keychain/keychain';
import { createNullArrayOfLength } from '@app/common/utils';
import { storeAtom } from '@app/store';
import { selectStacksChain } from '@app/store/chains/stx-chain.selectors';
import { selectRootKeychain } from '@app/store/in-memory-key/in-memory-key.selectors';
import {
selectDefaultWalletKey,
selectRootKeychain,
} from '@app/store/in-memory-key/in-memory-key.selectors';
import { selectLedgerKey } from '@app/store/keys/key.selectors';
import { addressNetworkVersionState } from '@app/store/transactions/transaction';
@@ -104,3 +107,13 @@ export const stacksAccountState = atom<StacksAccount[] | undefined>(get => {
const softwareAccounts = get(softwareAccountsState);
return ledgerAccounts ?? softwareAccounts;
});
export const legacyStackWallet = atom(async get => {
const store = get(storeAtom);
const secretKey = selectDefaultWalletKey(store);
const accounts = get(softwareAccountsState);
if (!secretKey) return;
const wallet = await generateWallet({ secretKey, password: '' });
wallet.accounts = accounts ?? [];
return wallet;
});