Skip to content

Commit

Permalink
Merge pull request #137 from tonkeeper/feature/v5r1
Browse files Browse the repository at this point in the history
Support wallet v5r1
  • Loading branch information
KuznetsovNikita authored Jul 11, 2024
2 parents a4a8a0e + 256ac61 commit 9fe0ea9
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 135 deletions.
2 changes: 1 addition & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@testing-library/user-event": "^13.5.0",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.6",
"@types/node": "^20.10.3",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@types/react-is": "^18",
Expand Down
2 changes: 1 addition & 1 deletion apps/twa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@testing-library/user-event": "^13.5.0",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.6",
"@types/node": "^20.10.3",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@types/react-transition-group": "^4.4.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"@ledgerhq/hw-transport-webhid": "^6.28.6",
"@ledgerhq/hw-transport-webusb": "^6.28.6",
"@ton-community/ton-ledger": "^7.0.1",
"@ton/core": "0.54.0",
"@ton/core": "0.56.0",
"@ton/crypto": "3.2.0",
"@ton/ton": "https://github.com/tonkeeper/tonkeeper-ton#build9",
"@ton/ton": "https://github.com/tonkeeper/tonkeeper-ton#build10",
"bignumber.js": "^9.1.1",
"ethers": "^6.6.5",
"query-string": "^8.1.0",
Expand Down
30 changes: 12 additions & 18 deletions packages/core/src/entries/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ export enum WalletVersion {
V3R2 = 1,
V4R1 = 2,
V4R2 = 3,
W5 = 4
V5beta = 4,
V5R1 = 5
}

export const WalletVersions = [WalletVersion.V3R1, WalletVersion.V3R2, WalletVersion.V4R2];
export const WalletVersions = [
WalletVersion.V3R1,
WalletVersion.V3R2,
WalletVersion.V4R2,
WalletVersion.V5beta,
WalletVersion.V5R1
];

export const walletVersionText = (version: WalletVersion) => {
switch (version) {
Expand All @@ -21,28 +28,15 @@ export const walletVersionText = (version: WalletVersion) => {
return 'v3R2';
case WalletVersion.V4R2:
return 'v4R2';
case WalletVersion.W5:
case WalletVersion.V5beta:
return 'W5 beta';
case WalletVersion.V5R1:
return 'W5';
default:
return String(version);
}
};

export const walletVersionFromText = (value: string) => {
switch (value) {
case 'v3R1':
return WalletVersion.V3R1;
case 'v3R2':
return WalletVersion.V3R2;
case 'v4R2':
return WalletVersion.V4R2;
case 'W5':
return WalletVersion.W5;
default:
throw new Error('Unsupported version');
}
};

export interface WalletAddress {
friendlyAddress: string;
rawAddress: string;
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/service/signerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const walletVersionText = (version: WalletVersion) => {
return 'v3r2';
case WalletVersion.V4R2:
return 'v4r2';
case WalletVersion.W5:
case WalletVersion.V5beta:
return 'v5beta';
case WalletVersion.V5R1:
return 'v5r1';
default:
return String(version);
Expand Down Expand Up @@ -77,7 +79,10 @@ export const publishSignerMessage = async (
const message = Cell.fromBase64(messageBase64).asBuilder();

const transfer = beginCell();
if (walletState.active.version === WalletVersion.W5) {
if (
walletState.active.version === WalletVersion.V5beta ||
walletState.active.version === WalletVersion.V5R1
) {
transfer.storeBuilder(message).storeBuffer(signature);
} else {
transfer.storeBuffer(signature).storeBuilder(message);
Expand Down
14 changes: 3 additions & 11 deletions packages/core/src/service/transfer/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import {
toNano
} from '@ton/core';
import { sign } from '@ton/crypto';
import { WalletContractV3R1 } from '@ton/ton/dist/wallets/WalletContractV3R1';
import { WalletContractV3R2 } from '@ton/ton/dist/wallets/WalletContractV3R2';
import { WalletContractV4 } from '@ton/ton/dist/wallets/WalletContractV4';
import { WalletContractV5 } from '@ton/ton/dist/wallets/WalletContractV5';
import BigNumber from 'bignumber.js';
import nacl from 'tweetnacl';
import { APIConfig } from '../../entries/apis';
import { TonRecipient, TransferEstimationEvent } from '../../entries/send';
import { BaseSigner } from '../../entries/signer';
import { WalletState } from '../../entries/wallet';
import { Account, AccountsApi, LiteServerApi, WalletApi } from '../../tonApiV2';
import { walletContractFromState } from '../wallet/contractService';
import { NotEnoughBalanceError } from '../../errors/NotEnoughBalanceError';
import { Account, AccountsApi, LiteServerApi, WalletApi } from '../../tonApiV2';
import { WalletContract, walletContractFromState } from '../wallet/contractService';

export enum SendMode {
CARRY_ALL_REMAINING_BALANCE = 128,
Expand All @@ -32,11 +28,7 @@ export enum SendMode {
NONE = 0
}

export const externalMessage = (
contract: WalletContractV3R1 | WalletContractV3R2 | WalletContractV4 | WalletContractV5,
seqno: number,
body: Cell
) => {
export const externalMessage = (contract: WalletContract, seqno: number, body: Cell) => {
return beginCell()
.storeWritable(
storeMessage(
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/service/transfer/multiSendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export type TransferMessage = {
};

export const MAX_ALLOWED_WALLET_MSGS = {
[WalletVersion.W5]: 255,
[WalletVersion.V5R1]: 255,
[WalletVersion.V5beta]: 255,
[WalletVersion.V4R2]: 4,
[WalletVersion.V4R1]: 4,
[WalletVersion.V3R2]: 4,
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/service/wallet/contractService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { beginCell, storeStateInit } from '@ton/core';
import { WalletContractV3R1 } from '@ton/ton/dist/wallets/WalletContractV3R1';
import { WalletContractV3R2 } from '@ton/ton/dist/wallets/WalletContractV3R2';
import { WalletContractV4 } from '@ton/ton/dist/wallets/WalletContractV4';
import { WalletContractV5 } from '@ton/ton/dist/wallets/WalletContractV5';
import { WalletContractV5Beta } from '@ton/ton/dist/wallets/WalletContractV5Beta';
import { WalletContractV5R1 } from '@ton/ton/dist/wallets/WalletContractV5R1';
import { Network } from '../../entries/network';
import { WalletState, WalletVersion } from '../../entries/wallet';

Expand All @@ -13,6 +14,8 @@ export const walletContractFromState = (wallet: WalletState) => {

const workchain = 0;

export type WalletContract = ReturnType<typeof walletContract>;

export const walletContract = (
publicKey: Buffer,
version: WalletVersion,
Expand All @@ -27,13 +30,15 @@ export const walletContract = (
throw new Error('Unsupported wallet contract version - v4R1');
case WalletVersion.V4R2:
return WalletContractV4.create({ workchain, publicKey });
case WalletVersion.W5:
return WalletContractV5.create({
case WalletVersion.V5beta:
return WalletContractV5Beta.create({
walletId: {
networkGlobalId: network
},
publicKey
});
case WalletVersion.V5R1:
return WalletContractV5R1.create({ workChain: workchain, publicKey });
}
};

Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/service/walletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseTonAccount } from '@keystonehq/keystone-sdk/dist/wallet/hdKey';
import { Address } from '@ton/core';
import { mnemonicToPrivateKey } from '@ton/crypto';
import { WalletContractV4 } from '@ton/ton/dist/wallets/WalletContractV4';
import { WalletContractV5R1 } from '@ton/ton/dist/wallets/WalletContractV5R1';
import queryString from 'query-string';
import { AppKey } from '../Keys';
import { IStorage } from '../Storage';
Expand Down Expand Up @@ -42,7 +43,9 @@ export const encryptWalletMnemonic = async (mnemonic: string[], password: string
const versionMap: Record<string, WalletVersion> = {
wallet_v3r1: WalletVersion.V3R1,
wallet_v3r2: WalletVersion.V3R2,
wallet_v4r2: WalletVersion.V4R2
wallet_v4r2: WalletVersion.V4R2,
wallet_v5_beta: WalletVersion.V5beta,
wallet_v5r1: WalletVersion.V5R1
};

const findWalletVersion = (interfaces?: string[]): WalletVersion => {
Expand Down Expand Up @@ -86,14 +89,14 @@ const findWalletAddress = async (api: APIConfig, publicKey: string) => {
console.warn(e);
}

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

return wallet;
Expand Down
4 changes: 1 addition & 3 deletions packages/uikit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@ngraveio/bc-ur": "^1.1.13",
"@polkadot/react-qr": "^3.1.1",
"@tanstack/react-query": "4.3.4",
"@ton/core": "0.53.0",
"@ton/core": "0.56.0",
"@ton/crypto": "3.2.0",
"@tonkeeper/core": "0.1.0",
"bignumber.js": "^9.1.1",
Expand Down Expand Up @@ -53,8 +53,6 @@
"@storybook/manager-webpack4": "^6.5.14",
"@storybook/react": "^6.5.14",
"@storybook/testing-library": "^0.0.13",
"@ton/core": "0.54.0",
"@ton/crypto": "3.2.0",
"@types/country-list-js": "^3.1.3",
"@types/react": "^18.0.26",
"@types/react-beautiful-dnd": "^13.1.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { WalletVersion } from '@tonkeeper/core/dist/entries/wallet';
import { arrayToCsvString } from '@tonkeeper/core/dist/service/parserService';
import { MAX_ALLOWED_WALLET_MSGS } from '@tonkeeper/core/dist/service/transfer/multiSendService';
import { shiftedDecimals } from '@tonkeeper/core/dist/utils/balance';
import { getDecimalSeparator } from '@tonkeeper/core/dist/utils/formatting';
import { removeGroupSeparator } from '@tonkeeper/core/dist/utils/send';
import BigNumber from 'bignumber.js';
import { FC, useEffect, useState } from 'react';
import { Controller, FormProvider, useFieldArray, useForm, useFormContext } from 'react-hook-form';
Expand All @@ -21,7 +23,6 @@ import {
} from '../../../hooks/useAsyncValidator';
import { useDisclosure } from '../../../hooks/useDisclosure';
import { AppRoute, WalletSettingsRoute } from '../../../libs/routes';
import { useEnableW5, useEnableW5Mutation } from '../../../state/experemental';
import { useAssets } from '../../../state/home';
import { useIsActiveWalletLedger } from '../../../state/ledger';
import {
Expand Down Expand Up @@ -50,8 +51,6 @@ import { SaveListNotification } from './SaveListNotification';
import { UpdateListNotification } from './UpdateListNotification';
import { ImportListNotification } from './import-list/ImportListNotification';
import { getWillBeMultiSendValue } from './utils';
import { removeGroupSeparator } from '@tonkeeper/core/dist/utils/send';
import { getDecimalSeparator } from '@tonkeeper/core/dist/utils/formatting';

const FormHeadingWrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -318,8 +317,6 @@ const MultiSendAddMore: FC<{
fieldsNumber: number;
}> = ({ onAdd, fieldsNumber }) => {
const { t } = useTranslation();
const { data } = useEnableW5();
const { mutate } = useEnableW5Mutation();

const wallet = useWalletContext();

Expand All @@ -342,23 +339,17 @@ const MultiSendAddMore: FC<{
);
}

const onActivateW5 = () => {
if (!data) {
mutate();
}
};

if (wallet.active.version !== WalletVersion.W5) {
if (
wallet.active.version !== WalletVersion.V5beta &&
wallet.active.version !== WalletVersion.V5R1
) {
return (
<MaximumReachedContainer>
<Body2>{t('multi_send_maximum_reached')}</Body2>
&nbsp;
<Dot>·</Dot>
&nbsp;
<LinkStyled
onClick={onActivateW5}
to={AppRoute.walletSettings + WalletSettingsRoute.version}
>
<LinkStyled to={AppRoute.walletSettings + WalletSettingsRoute.version}>
{t('multi_send_switch_to_w5')}
</LinkStyled>
&nbsp;
Expand Down
24 changes: 1 addition & 23 deletions packages/uikit/src/pages/settings/Dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,10 @@ import React, { useMemo } from 'react';
import { InnerBody } from '../../components/Body';
import { SubHeader } from '../../components/SubHeader';
import { SettingsItem, SettingsList } from '../../components/settings/SettingsList';
import { useAppContext, useWalletContext } from '../../hooks/appContext';
import { useWalletContext } from '../../hooks/appContext';
import { useTranslation } from '../../hooks/translation';
import { useEnableW5, useEnableW5Mutation } from '../../state/experemental';
import { useMutateWalletProperty } from '../../state/wallet';

const SettingsW5 = () => {
const { experimental } = useAppContext();
const { data } = useEnableW5();
const { mutate } = useEnableW5Mutation();

const items = useMemo<SettingsItem[]>(() => {
return [
{
name: 'Experimental W5',
icon: data ? 'Active' : 'Inactive',
action: () => mutate()
}
];
}, [data, mutate]);

if (data === undefined || experimental !== true) return null;

return <SettingsList items={items} />;
};

export const DevSettings = React.memo(() => {
const { t } = useTranslation();

Expand All @@ -52,7 +31,6 @@ export const DevSettings = React.memo(() => {
<SettingsList items={items} />
{/* TODO: ENABLE TRON */}
{/* <SettingsList items={items2} /> */}
<SettingsW5 />
</InnerBody>
</>
);
Expand Down
Loading

0 comments on commit 9fe0ea9

Please sign in to comment.