Skip to content

Commit

Permalink
Use ton api config to enable v5
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovNikita committed Jul 12, 2024
1 parent 9087b6d commit dac5ce4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
58 changes: 41 additions & 17 deletions packages/core/src/service/walletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ import { Network } from '../entries/network';
import { AuthState } from '../entries/password';
import { WalletAddress, WalletState, WalletVersion, WalletVersions } from '../entries/wallet';
import { WalletApi } from '../tonApiV2';
import { TonendpointConfig } from '../tonkeeperApi/tonendpoint';
import { encrypt } from './cryptoService';
import { walletContract } from './wallet/contractService';
import { getFallbackWalletEmoji, getWalletStateOrDie, setWalletState } from './wallet/storeService';

export const createNewWalletState = async (api: APIConfig, mnemonic: string[], name?: string) => {
export const createNewWalletState = async (
api: APIConfig,
mnemonic: string[],
config: TonendpointConfig,
name?: string
) => {
const keyPair = await mnemonicToPrivateKey(mnemonic);

const publicKey = keyPair.publicKey.toString('hex');

const active = await findWalletAddress(api, publicKey);
const active = await findWalletAddress(api, publicKey, config.flags?.disable_v5r1 ?? true);

const state: WalletState = {
publicKey,
Expand Down Expand Up @@ -60,7 +66,7 @@ const findWalletVersion = (interfaces?: string[]): WalletVersion => {
throw new Error('Unexpected wallet version');
};

const findWalletAddress = async (api: APIConfig, publicKey: string) => {
const findWalletAddress = async (api: APIConfig, publicKey: string, disable_v5r1: boolean) => {
try {
const result = await new WalletApi(api.tonApiV2).getWalletsByPublicKey({
publicKey: publicKey
Expand Down Expand Up @@ -89,17 +95,30 @@ const findWalletAddress = async (api: APIConfig, publicKey: string) => {
console.warn(e);
}

const contact = WalletContractV5R1.create({
workChain: 0,
publicKey: Buffer.from(publicKey, 'hex')
});
const wallet: WalletAddress = {
rawAddress: contact.address.toRawString(),
friendlyAddress: contact.address.toString(),
version: WalletVersion.V5R1
};
if (disable_v5r1) {
const contact = WalletContractV4.create({
workchain: 0,
publicKey: Buffer.from(publicKey, 'hex')
});
const wallet: WalletAddress = {
rawAddress: contact.address.toRawString(),
friendlyAddress: contact.address.toString(),
version: WalletVersion.V4R2
};
return wallet;
} else {
const contact = WalletContractV5R1.create({
workChain: 0,
publicKey: Buffer.from(publicKey, 'hex')
});
const wallet: WalletAddress = {
rawAddress: contact.address.toRawString(),
friendlyAddress: contact.address.toString(),
version: WalletVersion.V5R1
};

return wallet;
return wallet;
}
};

export const getWalletAddress = (
Expand Down Expand Up @@ -171,7 +190,11 @@ export const updateWalletProperty = async (
await setWalletState(storage, updated);
};

export const walletStateFromSignerQr = async (api: APIConfig, qrCode: string) => {
export const walletStateFromSignerQr = async (
api: APIConfig,
qrCode: string,
config: TonendpointConfig
) => {
if (!qrCode.startsWith('tonkeeper://signer')) {
throw new Error('Unexpected QR code');
}
Expand All @@ -189,7 +212,7 @@ export const walletStateFromSignerQr = async (api: APIConfig, qrCode: string) =>

const publicKey = pk;

const active = await findWalletAddress(api, publicKey);
const active = await findWalletAddress(api, publicKey, config.flags?.disable_v5r1 ?? true);

const state: WalletState = {
publicKey,
Expand All @@ -206,9 +229,10 @@ export const walletStateFromSignerQr = async (api: APIConfig, qrCode: string) =>
export const walletStateFromSignerDeepLink = async (
api: APIConfig,
publicKey: string,
name: string | null
name: string | null,
config: TonendpointConfig
) => {
const active = await findWalletAddress(api, publicKey);
const active = await findWalletAddress(api, publicKey, config.flags?.disable_v5r1 ?? true);

const state: WalletState = {
publicKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tonkeeperApi/tonendpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface BootOptions {
type TonendpointResponse<Data> = { success: false } | { success: true; data: Data };

export interface TonendpointConfig {
flags?: { [key: string]: boolean };
flags?: { disable_v5r1: boolean; [key: string]: boolean };
tonendpoint: string;

tonApiKey?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/uikit/src/pages/import/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
GearLottieIcon,
WriteLottieIcon
} from '../../components/lottie/LottieIcons';
import { useAppContext } from '../../hooks/appContext';
import { useAppSdk } from '../../hooks/appSdk';
import { useTranslation } from '../../hooks/translation';
import { useActiveWallet } from '../../state/wallet';
Expand All @@ -21,6 +22,7 @@ import { Subscribe } from './Subscribe';
const Create: FC<{ listOfAuth: AuthState['kind'][] }> = ({ listOfAuth }) => {
const sdk = useAppSdk();
const { t } = useTranslation();
const { config } = useAppContext();
const {
mutateAsync: checkPasswordAndCreateWalletAsync,
isLoading: isConfirmLoading,
Expand Down
15 changes: 9 additions & 6 deletions packages/uikit/src/pages/import/Password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createNewWalletState,
encryptWalletMnemonic
} from '@tonkeeper/core/dist/service/walletService';
import { TonendpointConfig } from '@tonkeeper/core/dist/tonkeeperApi/tonendpoint';
import { useEffect, useState } from 'react';
import styled from 'styled-components';
import { IconPage } from '../../components/Layout';
Expand All @@ -26,6 +27,7 @@ import { getPasswordByNotification } from '../../state/mnemonic';

const createWalletWithKeychain = async (
client: QueryClient,
config: TonendpointConfig,
api: APIConfig,
sdk: IAppSdk,
mnemonic: string[]
Expand All @@ -34,7 +36,7 @@ const createWalletWithKeychain = async (
throw new Error('Keychain is not define');
}

const state = await createNewWalletState(api, mnemonic);
const state = await createNewWalletState(api, mnemonic, config);
state.auth = { kind: 'keychain' };

await sdk.keychain.setPassword(state.publicKey, mnemonic.join(' '));
Expand All @@ -47,6 +49,7 @@ const createWalletWithKeychain = async (

const createWallet = async (
client: QueryClient,
config: TonendpointConfig,
api: APIConfig,
sdk: IAppSdk,
mnemonic: string[],
Expand All @@ -58,7 +61,7 @@ const createWallet = async (
throw new Error('Missing encrypt password key');
}

const state = await createNewWalletState(api, mnemonic);
const state = await createNewWalletState(api, mnemonic, config);
const encryptedMnemonic = await encryptWalletMnemonic(mnemonic, key);
await addWalletWithGlobalAuthState(sdk.storage, state, auth, encryptedMnemonic);

Expand All @@ -68,7 +71,7 @@ const createWallet = async (

export const useAddWalletMutation = () => {
const sdk = useAppSdk();
const { api } = useAppContext();
const { api, config } = useAppContext();
const client = useQueryClient();

return useMutation<
Expand All @@ -82,7 +85,7 @@ export const useAddWalletMutation = () => {
}

if (listOfAuth && listOfAuth.length === 1 && listOfAuth[0] == 'keychain') {
return createWalletWithKeychain(client, api, sdk, mnemonic);
return createWalletWithKeychain(client, config, api, sdk, mnemonic);
}

const auth = await sdk.storage.get<AuthState>(AppKey.GLOBAL_AUTH_STATE);
Expand All @@ -95,14 +98,14 @@ export const useAddWalletMutation = () => {
}

if (auth.kind === 'none') {
return createWallet(client, api, sdk, mnemonic, auth);
return createWallet(client, config, api, sdk, mnemonic, auth);
}

if (!password) {
password = await getPasswordByNotification(sdk, auth);
}

return createWallet(client, api, sdk, mnemonic, auth, password);
return createWallet(client, config, api, sdk, mnemonic, auth, password);
});
};

Expand Down
4 changes: 2 additions & 2 deletions packages/uikit/src/pages/signer/LinkPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { AppRoute } from '../../libs/routes';
const useAddWalletMutation = () => {
const sdk = useAppSdk();
const client = useQueryClient();
const { api } = useAppContext();
const { api, config } = useAppContext();
const navigate = useNavigate();

return useMutation<void, Error, { publicKey: string | null; name: string | null }>(
async ({ publicKey, name }) => {
if (publicKey === null) {
sdk.topMessage('Missing public key');
} else {
const state = await walletStateFromSignerDeepLink(api, publicKey, name);
const state = await walletStateFromSignerDeepLink(api, publicKey, name, config);
await addWalletWithCustomAuthState(sdk.storage, state);
await client.invalidateQueries([QueryKey.account]);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/uikit/src/state/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { AppRoute } from '../libs/routes';

export const usePairSignerMutation = () => {
const sdk = useAppSdk();
const { api } = useAppContext();
const { api, config } = useAppContext();
const client = useQueryClient();
const navigate = useNavigate();
return useMutation<void, Error, string>(async qrCode => {
try {
const state = await walletStateFromSignerQr(api, qrCode);
const state = await walletStateFromSignerQr(api, qrCode, config);

await addWalletWithCustomAuthState(sdk.storage, state);

Expand Down

0 comments on commit dac5ce4

Please sign in to comment.