@unisat/wallet-state 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -5
- package/lib/index.d.mts +2012 -200
- package/lib/index.d.ts +2012 -200
- package/lib/index.js +6951 -570
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +6754 -517
- package/lib/index.mjs.map +1 -1
- package/lib/types/index.d.mts +1 -1
- package/lib/types/index.d.ts +1 -1
- package/package.json +19 -15
- package/src/.DS_Store +0 -0
- package/src/context/ApprovalContext.tsx +27 -0
- package/src/context/DeviceContext.tsx +36 -0
- package/src/context/I18nContext.tsx +14 -172
- package/src/context/NavigationContext.tsx +305 -0
- package/src/context/PriceContext.tsx +2 -2
- package/src/context/StorageContext.tsx +393 -0
- package/src/context/ToolsContext.tsx +50 -0
- package/src/context/WalletContext.tsx +119 -108
- package/src/context/index.ts +17 -2
- package/src/hooks/accounts.ts +11 -5
- package/src/hooks/browser.ts +11 -0
- package/src/hooks/global.ts +170 -7
- package/src/hooks/index.ts +1 -2
- package/src/hooks/settings.ts +28 -37
- package/src/hooks/transactions.ts +28 -155
- package/src/hooks/ui.ts +232 -36
- package/src/index.ts +33 -24
- package/src/reducers/accounts.ts +19 -2
- package/src/reducers/browser.ts +223 -0
- package/src/reducers/global.ts +67 -1
- package/src/reducers/index.ts +1 -0
- package/src/reducers/transactions.ts +0 -9
- package/src/reducers/ui.ts +127 -8
- package/src/types/index.ts +1 -1
- package/src/ui-hooks/index.ts +107 -0
- package/src/ui-hooks/useActionOverviewSectionLogic.ts +150 -0
- package/src/ui-hooks/useAddressTypeScreenLogic.ts +160 -0
- package/src/ui-hooks/useAlkanesBalanceCardLogic.ts +41 -0
- package/src/ui-hooks/useAlkanesCollectionListLogic.ts +68 -0
- package/src/ui-hooks/useAlkanesListLogic.ts +69 -0
- package/src/ui-hooks/useAlkanesNFTListLogic.ts +42 -0
- package/src/ui-hooks/useAlkanesNFTScreenLogic.ts +45 -0
- package/src/ui-hooks/useAlkanesTokenScreenLogic.ts +130 -0
- package/src/ui-hooks/useAmountInputLogic.ts +80 -0
- package/src/ui-hooks/useAnnouncementCardLogic.ts +91 -0
- package/src/ui-hooks/useBRC20BalanceCardLogic.ts +115 -0
- package/src/ui-hooks/useBRC20InscribeTransferLogic.ts +398 -0
- package/src/ui-hooks/useBRC20ListLogic.ts +75 -0
- package/src/ui-hooks/useBRC20ProgListLogic.ts +77 -0
- package/src/ui-hooks/useBRC20SendScreenLogic.ts +411 -0
- package/src/ui-hooks/useBRC20SingleStepScreenLogic.ts +208 -0
- package/src/ui-hooks/useBRC20TokenScreenLogic.ts +469 -0
- package/src/ui-hooks/useBalanceCardLogic.ts +164 -0
- package/src/ui-hooks/useBtcDisplayLogic.ts +16 -0
- package/src/ui-hooks/useCAT20BalanceCardLogic.ts +35 -0
- package/src/ui-hooks/useCAT20ListLogic.ts +83 -0
- package/src/ui-hooks/useCAT20TokenScreenLogic.ts +122 -0
- package/src/ui-hooks/useCAT721ListLogic.ts +68 -0
- package/src/ui-hooks/useCAT721NFTScreenLogic.ts +37 -0
- package/src/ui-hooks/useCreatePasswordScreenLogic.ts +92 -0
- package/src/ui-hooks/useCreateWalletLogicImportWordsStep.ts +299 -0
- package/src/ui-hooks/useEditAccountNameScreenLogic.ts +71 -0
- package/src/ui-hooks/useEditContactScreenLogic.ts +162 -0
- package/src/ui-hooks/useEditWalletNameScreenLogic.ts +58 -0
- package/src/ui-hooks/useExportMnemonicsScreenLogic.ts +75 -0
- package/src/ui-hooks/useExportPrivateKeyScreenLogic.ts +64 -0
- package/src/ui-hooks/useFeeRateBarLogic.ts +303 -0
- package/src/ui-hooks/useInfiniteList.ts +85 -0
- package/src/ui-hooks/useInscriptionListLogic.ts +68 -0
- package/src/ui-hooks/useLockTimePageLogic.ts +43 -0
- package/src/ui-hooks/useNotificationsLogic.ts +115 -0
- package/src/ui-hooks/useOrdinalsInscriptionScreenLogic.ts +130 -0
- package/src/ui-hooks/useRunesBalanceCardLogic.ts +44 -0
- package/src/ui-hooks/useRunesListLogic.ts +74 -0
- package/src/ui-hooks/useRunesTokenScreenLogic.ts +149 -0
- package/src/ui-hooks/useSecurityCardLogic.ts +0 -0
- package/src/ui-hooks/useSendAlkanesNFTScreenLogic.ts +138 -0
- package/src/ui-hooks/useSendAlkanesScreenLogic.ts +192 -0
- package/src/ui-hooks/useSendCAT20ScreenLogic.ts +297 -0
- package/src/ui-hooks/useSendCAT721ScreenLogic.ts +205 -0
- package/src/ui-hooks/useSendOrdinalsInscriptionScreenLogic.ts +137 -0
- package/src/ui-hooks/useSendRunesScreenLogic.ts +172 -0
- package/src/ui-hooks/useSettingsTabScreenLogic.ts +211 -0
- package/src/ui-hooks/useSignMessageLogic.ts +302 -0
- package/src/ui-hooks/useSignPsbtLogic.ts +517 -0
- package/src/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.ts +95 -0
- package/src/ui-hooks/useTxConfirmScreenLogic.ts +47 -0
- package/src/ui-hooks/useTxCreateScreenLogic.ts +161 -0
- package/src/ui-hooks/useTxFailScreenLogic.ts +26 -0
- package/src/ui-hooks/useTxSuccessScreenLogic.ts +33 -0
- package/src/updater/accounts.ts +11 -11
- package/src/utils/bitcoin-utils.ts +17 -8
- package/src/utils/eventBus.ts +2 -1
- package/src/utils/password-utils.ts +78 -0
- package/src/utils/ui-utils.ts +28 -0
- package/src/hooks/approval.ts +0 -72
- package/src/hooks/i18n.ts +0 -53
- package/src/utils/i18n.ts +0 -41
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import { CAT20Balance, CAT_VERSION, TickPriceItem } from '@unisat/wallet-shared'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
CATAssetTabKey,
|
|
7
|
+
getSupportedAssets,
|
|
8
|
+
useCATAssetTabKey,
|
|
9
|
+
useChainType,
|
|
10
|
+
useCurrentAccount,
|
|
11
|
+
useNavigation,
|
|
12
|
+
useWallet,
|
|
13
|
+
useWallTabFocusRefresh,
|
|
14
|
+
} from '..'
|
|
15
|
+
import { useInfiniteList } from './useInfiniteList'
|
|
16
|
+
|
|
17
|
+
export function useCAT20ListLogic(version: CAT_VERSION) {
|
|
18
|
+
const nav = useNavigation()
|
|
19
|
+
const wallet = useWallet()
|
|
20
|
+
const currentAccount = useCurrentAccount()
|
|
21
|
+
const chainType = useChainType()
|
|
22
|
+
const [priceMap, setPriceMap] = useState<{ [key: string]: TickPriceItem }>({})
|
|
23
|
+
|
|
24
|
+
const priceMapRef = useRef(priceMap)
|
|
25
|
+
const updatePrices = (res: { [tick: string]: TickPriceItem }) => {
|
|
26
|
+
const newPriceMap = { ...priceMapRef.current }
|
|
27
|
+
Object.keys(res).forEach(tick => {
|
|
28
|
+
newPriceMap[tick] = res[tick]
|
|
29
|
+
})
|
|
30
|
+
priceMapRef.current = newPriceMap
|
|
31
|
+
setPriceMap(newPriceMap)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const {
|
|
35
|
+
data: tokens,
|
|
36
|
+
total,
|
|
37
|
+
loading,
|
|
38
|
+
hasMore,
|
|
39
|
+
onRefresh,
|
|
40
|
+
onLoadMore,
|
|
41
|
+
} = useInfiniteList<CAT20Balance>({
|
|
42
|
+
fetcher: async (page, pageSize) => {
|
|
43
|
+
const supportedAssets = getSupportedAssets(chainType, currentAccount.address)
|
|
44
|
+
if (!supportedAssets.assets.CAT20 || currentAccount.address === '') {
|
|
45
|
+
return { list: [], total: 0 }
|
|
46
|
+
}
|
|
47
|
+
const { list, total } = await wallet.getCAT20List(
|
|
48
|
+
version,
|
|
49
|
+
currentAccount.address,
|
|
50
|
+
page,
|
|
51
|
+
pageSize
|
|
52
|
+
)
|
|
53
|
+
if (list.length > 0) {
|
|
54
|
+
wallet.getCAT20sPrice(list.map(item => item.tokenId)).then(updatePrices)
|
|
55
|
+
}
|
|
56
|
+
return { list, total }
|
|
57
|
+
},
|
|
58
|
+
dependencies: [currentAccount.address, version, chainType],
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const tabKey = useCATAssetTabKey()
|
|
62
|
+
const isFocus = tabKey === CATAssetTabKey.CAT20 || CATAssetTabKey.CAT20_V2
|
|
63
|
+
const lastRefreshTimeRef = useRef<number>(0)
|
|
64
|
+
const walletTabFocusRefresh = useWallTabFocusRefresh()
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!isFocus) return
|
|
67
|
+
|
|
68
|
+
// already refreshed → do nothing
|
|
69
|
+
const alreadyRefreshed = lastRefreshTimeRef.current === walletTabFocusRefresh
|
|
70
|
+
if (alreadyRefreshed) return
|
|
71
|
+
|
|
72
|
+
onRefresh()
|
|
73
|
+
|
|
74
|
+
// mark refreshed
|
|
75
|
+
lastRefreshTimeRef.current = walletTabFocusRefresh
|
|
76
|
+
}, [walletTabFocusRefresh, isFocus])
|
|
77
|
+
|
|
78
|
+
const onClickItem = (item: CAT20Balance) => {
|
|
79
|
+
nav.navigate('CAT20TokenScreen', { tokenId: item.tokenId, version })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return { tokens, total, loading, hasMore, onRefresh, onLoadMore, onClickItem, priceMap }
|
|
83
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { KeyringType } from '@unisat/keyring-service/types'
|
|
2
|
+
import { AddressCAT20TokenSummary, CAT_VERSION } from '@unisat/wallet-shared'
|
|
3
|
+
import { ChainType } from '@unisat/wallet-types'
|
|
4
|
+
import { useEffect, useMemo, useState } from 'react'
|
|
5
|
+
import { useI18n, useNavigation, useTools, useWallet } from 'src/context'
|
|
6
|
+
import {
|
|
7
|
+
useCAT20IconInfo,
|
|
8
|
+
useCAT20MarketPlaceWebsite,
|
|
9
|
+
useCAT20TokenInfoExplorerUrl,
|
|
10
|
+
useChainType,
|
|
11
|
+
useCurrentAccount,
|
|
12
|
+
useCurrentKeyring,
|
|
13
|
+
useResetTxState,
|
|
14
|
+
} from 'src/hooks'
|
|
15
|
+
|
|
16
|
+
export function useCAT20TokenScreenLogic() {
|
|
17
|
+
const nav = useNavigation()
|
|
18
|
+
const { tokenId, version } = nav.getRouteState<'CAT20TokenScreen'>()
|
|
19
|
+
const { t } = useI18n()
|
|
20
|
+
const [tokenSummary, setTokenSummary] = useState<AddressCAT20TokenSummary>({
|
|
21
|
+
cat20Balance: {
|
|
22
|
+
tokenId: '',
|
|
23
|
+
amount: '0',
|
|
24
|
+
decimals: 0,
|
|
25
|
+
symbol: '',
|
|
26
|
+
name: '',
|
|
27
|
+
},
|
|
28
|
+
cat20Info: {
|
|
29
|
+
tokenId: '',
|
|
30
|
+
name: '',
|
|
31
|
+
symbol: '',
|
|
32
|
+
max: '0',
|
|
33
|
+
premine: '0',
|
|
34
|
+
limit: 0,
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const wallet = useWallet()
|
|
39
|
+
|
|
40
|
+
const account = useCurrentAccount()
|
|
41
|
+
const keyring = useCurrentKeyring()
|
|
42
|
+
const [loading, setLoading] = useState(true)
|
|
43
|
+
const tools = useTools()
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
wallet.getAddressCAT20TokenSummary(version, account.address, tokenId).then(tokenSummary => {
|
|
46
|
+
setTokenSummary(tokenSummary)
|
|
47
|
+
setLoading(false)
|
|
48
|
+
})
|
|
49
|
+
}, [])
|
|
50
|
+
|
|
51
|
+
const tokenUrl = useCAT20TokenInfoExplorerUrl(version, tokenSummary.cat20Info.tokenId)
|
|
52
|
+
|
|
53
|
+
const enableTransfer = useMemo(() => {
|
|
54
|
+
let enable = false
|
|
55
|
+
if (tokenSummary.cat20Balance && tokenSummary.cat20Balance.amount !== '0') {
|
|
56
|
+
enable = true
|
|
57
|
+
}
|
|
58
|
+
return enable
|
|
59
|
+
}, [tokenSummary])
|
|
60
|
+
|
|
61
|
+
const chainType = useChainType()
|
|
62
|
+
const enableTrade = useMemo(() => {
|
|
63
|
+
if (chainType === ChainType.FRACTAL_BITCOIN_MAINNET && version === CAT_VERSION.V1) {
|
|
64
|
+
return true
|
|
65
|
+
} else {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
}, [chainType])
|
|
69
|
+
const marketPlaceUrl = useCAT20MarketPlaceWebsite(tokenId)
|
|
70
|
+
|
|
71
|
+
const resetTxState = useResetTxState()
|
|
72
|
+
|
|
73
|
+
const onClickBack = () => {
|
|
74
|
+
nav.goBack()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const onClickMerge = e => {
|
|
78
|
+
if (keyring.type === KeyringType.KeystoneKeyring) {
|
|
79
|
+
tools.toastError(t('merge_utxos_is_not_supported_for_keystone_yet'))
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
resetTxState()
|
|
83
|
+
nav.navigate('MergeCAT20Screen', {
|
|
84
|
+
version: version,
|
|
85
|
+
cat20Balance: tokenSummary.cat20Balance,
|
|
86
|
+
cat20Info: tokenSummary.cat20Info,
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const onClickSend = e => {
|
|
91
|
+
if (keyring.type === KeyringType.KeystoneKeyring) {
|
|
92
|
+
tools.toastError(t('send_cat20_is_not_supported_for_keystone_yet'))
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
resetTxState()
|
|
96
|
+
nav.navigate('SendCAT20Screen', {
|
|
97
|
+
version: version,
|
|
98
|
+
cat20Balance: tokenSummary.cat20Balance,
|
|
99
|
+
cat20Info: tokenSummary.cat20Info,
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const onClickTrade = e => {
|
|
104
|
+
nav.navToUrl(marketPlaceUrl)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const iconInfo = useCAT20IconInfo(tokenSummary.cat20Info.name, tokenSummary.cat20Info.tokenId)
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
tokenSummary,
|
|
111
|
+
loading,
|
|
112
|
+
tokenUrl,
|
|
113
|
+
enableTransfer,
|
|
114
|
+
enableTrade,
|
|
115
|
+
iconInfo,
|
|
116
|
+
onClickMerge,
|
|
117
|
+
onClickSend,
|
|
118
|
+
onClickTrade,
|
|
119
|
+
onClickBack,
|
|
120
|
+
t,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
import { CAT721Balance, CAT_VERSION } from '@unisat/wallet-shared'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
CATAssetTabKey,
|
|
7
|
+
getSupportedAssets,
|
|
8
|
+
useCATAssetTabKey,
|
|
9
|
+
useChainType,
|
|
10
|
+
useCurrentAccount,
|
|
11
|
+
useNavigation,
|
|
12
|
+
useWallet,
|
|
13
|
+
useWallTabFocusRefresh,
|
|
14
|
+
} from '..'
|
|
15
|
+
import { useInfiniteList } from './useInfiniteList'
|
|
16
|
+
|
|
17
|
+
export function useCAT721ListLogic(version: CAT_VERSION) {
|
|
18
|
+
const nav = useNavigation()
|
|
19
|
+
const wallet = useWallet()
|
|
20
|
+
const currentAccount = useCurrentAccount()
|
|
21
|
+
const chainType = useChainType()
|
|
22
|
+
const {
|
|
23
|
+
data: tokens,
|
|
24
|
+
total,
|
|
25
|
+
loading,
|
|
26
|
+
hasMore,
|
|
27
|
+
onRefresh,
|
|
28
|
+
onLoadMore,
|
|
29
|
+
} = useInfiniteList<CAT721Balance>({
|
|
30
|
+
fetcher: async (page, pageSize) => {
|
|
31
|
+
const supportedAssets = getSupportedAssets(chainType, currentAccount.address)
|
|
32
|
+
if (!supportedAssets.assets.CAT20 || currentAccount.address === '') {
|
|
33
|
+
return { list: [], total: 0 }
|
|
34
|
+
}
|
|
35
|
+
const { list, total } = await wallet.getCAT721List(
|
|
36
|
+
version,
|
|
37
|
+
currentAccount.address,
|
|
38
|
+
page,
|
|
39
|
+
pageSize
|
|
40
|
+
)
|
|
41
|
+
return { list, total }
|
|
42
|
+
},
|
|
43
|
+
dependencies: [currentAccount.address, version, chainType],
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const tabKey = useCATAssetTabKey()
|
|
47
|
+
const isFocus = tabKey === CATAssetTabKey.CAT721 || CATAssetTabKey.CAT721_V2
|
|
48
|
+
const lastRefreshTimeRef = useRef<number>(0)
|
|
49
|
+
const walletTabFocusRefresh = useWallTabFocusRefresh()
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!isFocus) return
|
|
52
|
+
|
|
53
|
+
// already refreshed → do nothing
|
|
54
|
+
const alreadyRefreshed = lastRefreshTimeRef.current === walletTabFocusRefresh
|
|
55
|
+
if (alreadyRefreshed) return
|
|
56
|
+
|
|
57
|
+
onRefresh()
|
|
58
|
+
|
|
59
|
+
// mark refreshed
|
|
60
|
+
lastRefreshTimeRef.current = walletTabFocusRefresh
|
|
61
|
+
}, [walletTabFocusRefresh, isFocus])
|
|
62
|
+
|
|
63
|
+
const onClickItem = (item: CAT721Balance) => {
|
|
64
|
+
nav.navigate('CAT721CollectionScreen', { collectionId: item.collectionId, version })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { tokens, total, loading, hasMore, onRefresh, onLoadMore, onClickItem }
|
|
68
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useI18n, useNavigation } from 'src/context'
|
|
2
|
+
import { useResetTxState } from 'src/hooks'
|
|
3
|
+
|
|
4
|
+
export function useCAT721NFTScreenLogic() {
|
|
5
|
+
const nav = useNavigation()
|
|
6
|
+
const { collectionInfo, localId, version } = nav.getRouteState<'CAT721NFTScreen'>()
|
|
7
|
+
|
|
8
|
+
const { t } = useI18n()
|
|
9
|
+
|
|
10
|
+
const resetTxState = useResetTxState()
|
|
11
|
+
|
|
12
|
+
const onClickBack = () => {
|
|
13
|
+
nav.goBack()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const onClickSend = () => {
|
|
17
|
+
resetTxState()
|
|
18
|
+
nav.navigate('SendCAT721Screen', {
|
|
19
|
+
version: version,
|
|
20
|
+
collectionInfo: collectionInfo,
|
|
21
|
+
localId: localId,
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
// info
|
|
27
|
+
collectionInfo,
|
|
28
|
+
localId,
|
|
29
|
+
version,
|
|
30
|
+
// i18n
|
|
31
|
+
t,
|
|
32
|
+
|
|
33
|
+
// actions
|
|
34
|
+
onClickBack,
|
|
35
|
+
onClickSend,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react'
|
|
2
|
+
import { useI18n, useNavigation, useTools, useWallet } from 'src/context'
|
|
3
|
+
import { getPasswordStrengthWord, MIN_PASSWORD_LENGTH } from 'src/utils/password-utils'
|
|
4
|
+
|
|
5
|
+
export function useCreatePasswordScreenLogic() {
|
|
6
|
+
const nav = useNavigation()
|
|
7
|
+
const { isNewAccount, isKeystone, fromColdWallet } = nav.getRouteState<'CreatePasswordScreen'>()
|
|
8
|
+
const [newPassword, setNewPassword] = useState('')
|
|
9
|
+
const { t } = useI18n()
|
|
10
|
+
|
|
11
|
+
const [confirmPassword, setConfirmPassword] = useState('')
|
|
12
|
+
|
|
13
|
+
const [disabled, setDisabled] = useState(true)
|
|
14
|
+
|
|
15
|
+
const wallet = useWallet()
|
|
16
|
+
|
|
17
|
+
const tools = useTools()
|
|
18
|
+
const bootWithPassword = (password: string) => {
|
|
19
|
+
wallet
|
|
20
|
+
.boot(password)
|
|
21
|
+
.then(() => {
|
|
22
|
+
if (fromColdWallet) {
|
|
23
|
+
nav.navigate('CreateColdWalletScreen', { fromUnlock: true })
|
|
24
|
+
} else if (isKeystone) {
|
|
25
|
+
nav.navigate('CreateKeystoneWalletScreen', { fromUnlock: true })
|
|
26
|
+
} else if (isNewAccount) {
|
|
27
|
+
nav.navigate('CreateHDWalletScreen', { isImport: false, fromUnlock: true })
|
|
28
|
+
} else {
|
|
29
|
+
nav.navigate('CreateHDWalletScreen', { isImport: true, fromUnlock: true })
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
.catch(err => {
|
|
33
|
+
tools.toastError(err)
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
setDisabled(true)
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
newPassword &&
|
|
42
|
+
newPassword.length >= MIN_PASSWORD_LENGTH &&
|
|
43
|
+
newPassword === confirmPassword
|
|
44
|
+
) {
|
|
45
|
+
setDisabled(false)
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
}, [newPassword, confirmPassword])
|
|
49
|
+
|
|
50
|
+
const strongTextRenderData = useMemo(() => {
|
|
51
|
+
if (!newPassword) {
|
|
52
|
+
return null
|
|
53
|
+
}
|
|
54
|
+
return getPasswordStrengthWord(newPassword, t)
|
|
55
|
+
}, [newPassword, t])
|
|
56
|
+
|
|
57
|
+
const matchTextRenderData = useMemo(() => {
|
|
58
|
+
if (!confirmPassword) {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (newPassword !== confirmPassword) {
|
|
63
|
+
return {
|
|
64
|
+
text: t('passwords_dont_match'),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null
|
|
68
|
+
}, [newPassword, confirmPassword])
|
|
69
|
+
|
|
70
|
+
const onClickConfirm = () => {
|
|
71
|
+
bootWithPassword(newPassword)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const onPasswordChange = (e: string | { target: { value: string } }) => {
|
|
75
|
+
const val = typeof e === 'string' ? e : e.target.value
|
|
76
|
+
setNewPassword(val)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const onConfirmPasswordChange = (e: string | { target: { value: string } }) => {
|
|
80
|
+
const val = typeof e === 'string' ? e : e.target.value
|
|
81
|
+
setConfirmPassword(val)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
disabled,
|
|
86
|
+
strongTextRenderData,
|
|
87
|
+
matchTextRenderData,
|
|
88
|
+
onClickConfirm,
|
|
89
|
+
onPasswordChange,
|
|
90
|
+
onConfirmPasswordChange,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { OW_HD_PATH, RESTORE_WALLETS, RestoreWalletType, WordsType } from '@unisat/wallet-shared'
|
|
2
|
+
import { AddressType } from '@unisat/wallet-types'
|
|
3
|
+
import { useEffect, useMemo, useState } from 'react'
|
|
4
|
+
import { useI18n, useNavigation, useTools, useWallet } from 'src/context'
|
|
5
|
+
import { useAppDispatch, useCreateAccountCallback } from 'src/hooks'
|
|
6
|
+
import { accountActions, keyringsActions } from 'src/reducers'
|
|
7
|
+
import { validateMnemonic } from 'src/utils/bitcoin-utils'
|
|
8
|
+
|
|
9
|
+
export enum TabType {
|
|
10
|
+
STEP1 = 'STEP1',
|
|
11
|
+
STEP2 = 'STEP2',
|
|
12
|
+
CHOOSE_ADDRESS_TYPE = 'CHOOSE_ADDRESS_TYPE',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalizeWhitespace(str: string) {
|
|
16
|
+
str = str.replace(/[\n\r\t]+/g, ' ')
|
|
17
|
+
str = str.replace(/\s+/g, ' ')
|
|
18
|
+
str = str.trim()
|
|
19
|
+
return str
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ContextData {
|
|
23
|
+
mnemonics: string
|
|
24
|
+
hdPath: string
|
|
25
|
+
passphrase: string
|
|
26
|
+
addressType: AddressType
|
|
27
|
+
step1Completed: boolean
|
|
28
|
+
tabType: TabType
|
|
29
|
+
restoreWalletType: RestoreWalletType
|
|
30
|
+
isRestore: boolean
|
|
31
|
+
isCustom: boolean
|
|
32
|
+
customHdPath: string
|
|
33
|
+
addressTypeIndex: number
|
|
34
|
+
wordsType: WordsType
|
|
35
|
+
walletName?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface UpdateContextDataParams {
|
|
39
|
+
mnemonics?: string
|
|
40
|
+
hdPath?: string
|
|
41
|
+
passphrase?: string
|
|
42
|
+
addressType?: AddressType
|
|
43
|
+
step1Completed?: boolean
|
|
44
|
+
tabType?: TabType
|
|
45
|
+
restoreWalletType?: RestoreWalletType
|
|
46
|
+
isCustom?: boolean
|
|
47
|
+
customHdPath?: string
|
|
48
|
+
addressTypeIndex?: number
|
|
49
|
+
wordsType?: WordsType
|
|
50
|
+
walletName?: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface WordsItem {
|
|
54
|
+
key: WordsType
|
|
55
|
+
label: string
|
|
56
|
+
count: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const getWords12Item = t => ({
|
|
60
|
+
key: WordsType.WORDS_12,
|
|
61
|
+
label: t('mnemonics_12_words'),
|
|
62
|
+
count: 12,
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const getWords24Item = t => ({
|
|
66
|
+
key: WordsType.WORDS_24,
|
|
67
|
+
label: t('mnemonics_24_words'),
|
|
68
|
+
count: 24,
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
interface CreateWalletLogicParams {
|
|
72
|
+
contextData: ContextData
|
|
73
|
+
updateContextData: (params: UpdateContextDataParams) => void
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function textToWordsArray(text: string) {
|
|
77
|
+
return text.split(' ').filter(v => v.trim() !== '')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function useCreateWalletLogicImportWordsStep(params: CreateWalletLogicParams) {
|
|
81
|
+
const { contextData, updateContextData } = params
|
|
82
|
+
const { t } = useI18n()
|
|
83
|
+
|
|
84
|
+
const walletTypeConfig = RESTORE_WALLETS.find(
|
|
85
|
+
item => item.value === contextData.restoreWalletType
|
|
86
|
+
)
|
|
87
|
+
const wordsItems: Array<WordsItem> = useMemo(() => {
|
|
88
|
+
const supportedWordsItems: WordsItem[] = []
|
|
89
|
+
if (walletTypeConfig) {
|
|
90
|
+
if (walletTypeConfig.wordsTypes.includes(WordsType.WORDS_12)) {
|
|
91
|
+
supportedWordsItems.push(getWords12Item(t))
|
|
92
|
+
}
|
|
93
|
+
if (walletTypeConfig.wordsTypes.includes(WordsType.WORDS_24)) {
|
|
94
|
+
supportedWordsItems.push(getWords24Item(t))
|
|
95
|
+
}
|
|
96
|
+
return supportedWordsItems
|
|
97
|
+
} else {
|
|
98
|
+
return [getWords12Item(t), getWords24Item(t)]
|
|
99
|
+
}
|
|
100
|
+
}, [contextData.restoreWalletType, t])
|
|
101
|
+
|
|
102
|
+
const wallet = useWallet()
|
|
103
|
+
|
|
104
|
+
const [disabled, setDisabled] = useState(true)
|
|
105
|
+
|
|
106
|
+
const [inputWords, setInputWords] = useState<Array<string>>(
|
|
107
|
+
new Array(wordsItems[contextData.wordsType].count).fill('')
|
|
108
|
+
)
|
|
109
|
+
const [inputWordsText, setInputWordsText] = useState('')
|
|
110
|
+
const [inputWordsError, setInputWordsError] = useState(false)
|
|
111
|
+
const tools = useTools()
|
|
112
|
+
|
|
113
|
+
const createAccount = useCreateAccountCallback()
|
|
114
|
+
const nav = useNavigation()
|
|
115
|
+
|
|
116
|
+
const dispatch = useAppDispatch()
|
|
117
|
+
const [loading, setLoading] = useState(false)
|
|
118
|
+
|
|
119
|
+
const [enteredWordsCount, setEnteredWordsCount] = useState(0)
|
|
120
|
+
|
|
121
|
+
const enablePhrase = walletTypeConfig?.phraseSupport
|
|
122
|
+
|
|
123
|
+
const maxWordsCount = wordsItems[contextData.wordsType].count
|
|
124
|
+
|
|
125
|
+
const initKey = async () => {
|
|
126
|
+
let totalKeyringCount = 0
|
|
127
|
+
try {
|
|
128
|
+
totalKeyringCount = await wallet.getTotalKeyringCount()
|
|
129
|
+
} catch (e) {
|
|
130
|
+
const error = e as Error
|
|
131
|
+
tools.toastError(error.message)
|
|
132
|
+
}
|
|
133
|
+
updateContextData({ walletName: `HD Wallet #${totalKeyringCount + 1}` })
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
initKey()
|
|
138
|
+
}, [])
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
setDisabled(true)
|
|
142
|
+
|
|
143
|
+
const words = textToWordsArray(inputWordsText)
|
|
144
|
+
if (words.length !== maxWordsCount) {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const mnemonic = words.join(' ')
|
|
149
|
+
if (!validateMnemonic(mnemonic)) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
setDisabled(false)
|
|
154
|
+
}, [inputWordsText])
|
|
155
|
+
|
|
156
|
+
const updateWords = (words: Array<string>, isEndedWithSpace?: boolean) => {
|
|
157
|
+
setInputWords(words)
|
|
158
|
+
let text = words.join(' ')
|
|
159
|
+
setInputWordsText(isEndedWithSpace ? text + ' ' : text)
|
|
160
|
+
|
|
161
|
+
const enteredWordsCount = words.filter(key => {
|
|
162
|
+
return key.trim() != ''
|
|
163
|
+
}).length
|
|
164
|
+
|
|
165
|
+
setEnteredWordsCount(enteredWordsCount)
|
|
166
|
+
|
|
167
|
+
if (!validateMnemonic(text) && enteredWordsCount >= maxWordsCount) {
|
|
168
|
+
setInputWordsError(true)
|
|
169
|
+
} else {
|
|
170
|
+
setInputWordsError(false)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const onInputWordsTextChange = (e: { target: { value: string } } | string) => {
|
|
175
|
+
let value = typeof e === 'string' ? e : e.target.value
|
|
176
|
+
let isEndedWithSpace = /\s$/.test(value)
|
|
177
|
+
value = normalizeWhitespace(value)
|
|
178
|
+
|
|
179
|
+
const wordsArr = textToWordsArray(value)
|
|
180
|
+
updateWords(wordsArr, isEndedWithSpace)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const onInputWordsChange = (e: { target: { value: string } } | string, index: number) => {
|
|
184
|
+
let value = typeof e === 'string' ? e : e.target.value
|
|
185
|
+
value = normalizeWhitespace(value)
|
|
186
|
+
|
|
187
|
+
const newKeys = [...inputWords]
|
|
188
|
+
newKeys.splice(index, 1, value)
|
|
189
|
+
updateWords(newKeys)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// extension
|
|
193
|
+
const onHandleEventPaste = (event, index: number) => {
|
|
194
|
+
const copyText = event.clipboardData?.getData('text/plain')
|
|
195
|
+
const textArr = normalizeWhitespace(copyText).split(' ')
|
|
196
|
+
const newKeys = [...inputWords]
|
|
197
|
+
if (textArr) {
|
|
198
|
+
for (let i = 0; i < inputWords.length - index; i++) {
|
|
199
|
+
if (textArr.length == i) {
|
|
200
|
+
break
|
|
201
|
+
}
|
|
202
|
+
newKeys[index + i] = textArr[i]
|
|
203
|
+
}
|
|
204
|
+
updateWords(newKeys)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
event.preventDefault()
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const onInputWalletNameChange = (e: { target: { value: string } } | string) => {
|
|
211
|
+
const value = typeof e === 'string' ? e : e.target.value
|
|
212
|
+
updateContextData({ walletName: value })
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const onClickNext = async () => {
|
|
216
|
+
try {
|
|
217
|
+
const mnemonics = inputWords.join(' ')
|
|
218
|
+
|
|
219
|
+
if (contextData.wordsType === WordsType.WORDS_12) {
|
|
220
|
+
if (inputWords.length !== 12) {
|
|
221
|
+
tools.toastError(t('ErrorPrompt_CreateHDwallet_StepImport_Not_12_words'))
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (contextData.wordsType === WordsType.WORDS_24) {
|
|
227
|
+
if (inputWords.length !== 24) {
|
|
228
|
+
tools.toastError(t('ErrorPrompt_CreateHDwallet_StepImport_Not_24_words'))
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!validateMnemonic(mnemonics)) {
|
|
234
|
+
tools.toastError(t('ErrorPrompt_CreateHDwallet_StepImport_InvalidMnemonic'))
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
setLoading(true)
|
|
239
|
+
|
|
240
|
+
if (contextData.restoreWalletType === RestoreWalletType.OW) {
|
|
241
|
+
await createAccount(mnemonics, OW_HD_PATH, '', AddressType.P2TR, 1)
|
|
242
|
+
const keyrings = await wallet.getKeyrings()
|
|
243
|
+
const keyring = keyrings[keyrings.length - 1]
|
|
244
|
+
if (contextData.walletName) {
|
|
245
|
+
keyring.alianName = contextData.walletName
|
|
246
|
+
await wallet.setKeyringAlianName(keyring, contextData.walletName)
|
|
247
|
+
}
|
|
248
|
+
await wallet.changeKeyring(keyring)
|
|
249
|
+
const _keyrings = await wallet.getKeyrings()
|
|
250
|
+
// @ts-ignore SAFE
|
|
251
|
+
dispatch(keyringsActions.setKeyrings(_keyrings))
|
|
252
|
+
// @ts-ignore SAFE
|
|
253
|
+
dispatch(keyringsActions.setCurrent(keyring))
|
|
254
|
+
// @ts-ignore SAFE
|
|
255
|
+
dispatch(accountActions.setCurrent(keyring?.accounts[0]))
|
|
256
|
+
|
|
257
|
+
nav.navToTab()
|
|
258
|
+
} else {
|
|
259
|
+
updateContextData({
|
|
260
|
+
walletName: contextData.walletName,
|
|
261
|
+
mnemonics,
|
|
262
|
+
tabType: TabType.CHOOSE_ADDRESS_TYPE,
|
|
263
|
+
})
|
|
264
|
+
}
|
|
265
|
+
} catch (e) {
|
|
266
|
+
tools.toastError((e as any).message)
|
|
267
|
+
} finally {
|
|
268
|
+
setLoading(false)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const onClickWordsItem = (wordsItem: WordsItem) => {
|
|
273
|
+
updateContextData({ wordsType: wordsItem.key })
|
|
274
|
+
setInputWordsText('')
|
|
275
|
+
setInputWords(new Array(wordsItem.count).fill(''))
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const shouldEnteredWordsCount = wordsItems[contextData.wordsType].count
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
wordsItems,
|
|
282
|
+
t,
|
|
283
|
+
disabled,
|
|
284
|
+
inputWords,
|
|
285
|
+
inputWordsText,
|
|
286
|
+
onHandleEventPaste,
|
|
287
|
+
onClickNext,
|
|
288
|
+
onClickWordsItem,
|
|
289
|
+
inputWordsError,
|
|
290
|
+
onInputWordsTextChange,
|
|
291
|
+
onInputWordsChange,
|
|
292
|
+
loading,
|
|
293
|
+
enablePhrase,
|
|
294
|
+
walletName: contextData.walletName,
|
|
295
|
+
onInputWalletNameChange,
|
|
296
|
+
enteredWordsCount,
|
|
297
|
+
shouldEnteredWordsCount,
|
|
298
|
+
}
|
|
299
|
+
}
|