@xyo-network/react-wallet 2.55.5 → 2.55.6
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/dist/cjs/components/WalletAccountSelect/Select.js +21 -12
- package/dist/cjs/components/WalletAccountSelect/Select.js.map +1 -1
- package/dist/cjs/contexts/Account/use.js +3 -3
- package/dist/cjs/contexts/Account/use.js.map +1 -1
- package/dist/cjs/contexts/Wallet/Provider.js +14 -21
- package/dist/cjs/contexts/Wallet/Provider.js.map +1 -1
- package/dist/cjs/contexts/Wallet/use.js +23 -4
- package/dist/cjs/contexts/Wallet/use.js.map +1 -1
- package/dist/cjs/hooks/index.js +2 -1
- package/dist/cjs/hooks/index.js.map +1 -1
- package/dist/cjs/hooks/useAccount.js +24 -20
- package/dist/cjs/hooks/useAccount.js.map +1 -1
- package/dist/cjs/hooks/useWallet.js +42 -0
- package/dist/cjs/hooks/useWallet.js.map +1 -0
- package/dist/cjs/hooks/useWallets.js +12 -0
- package/dist/cjs/hooks/useWallets.js.map +1 -0
- package/dist/docs.json +1887 -1741
- package/dist/esm/components/WalletAccountSelect/Select.js +22 -12
- package/dist/esm/components/WalletAccountSelect/Select.js.map +1 -1
- package/dist/esm/contexts/Account/use.js +1 -1
- package/dist/esm/contexts/Account/use.js.map +1 -1
- package/dist/esm/contexts/Wallet/Provider.js +14 -21
- package/dist/esm/contexts/Wallet/Provider.js.map +1 -1
- package/dist/esm/contexts/Wallet/use.js +19 -3
- package/dist/esm/contexts/Wallet/use.js.map +1 -1
- package/dist/esm/hooks/index.js +2 -1
- package/dist/esm/hooks/index.js.map +1 -1
- package/dist/esm/hooks/useAccount.js +25 -21
- package/dist/esm/hooks/useAccount.js.map +1 -1
- package/dist/esm/hooks/useWallet.js +36 -0
- package/dist/esm/hooks/useWallet.js.map +1 -0
- package/dist/esm/hooks/useWallets.js +8 -0
- package/dist/esm/hooks/useWallets.js.map +1 -0
- package/dist/types/components/WalletAccountSelect/Select.d.ts +0 -10
- package/dist/types/components/WalletAccountSelect/Select.d.ts.map +1 -1
- package/dist/types/contexts/Account/use.d.ts +1 -1
- package/dist/types/contexts/Account/use.d.ts.map +1 -1
- package/dist/types/contexts/Wallet/Provider.d.ts +1 -1
- package/dist/types/contexts/Wallet/Provider.d.ts.map +1 -1
- package/dist/types/contexts/Wallet/State.d.ts +10 -4
- package/dist/types/contexts/Wallet/State.d.ts.map +1 -1
- package/dist/types/contexts/Wallet/use.d.ts +5 -1
- package/dist/types/contexts/Wallet/use.d.ts.map +1 -1
- package/dist/types/hooks/index.d.ts +2 -1
- package/dist/types/hooks/index.d.ts.map +1 -1
- package/dist/types/hooks/useAccount.d.ts +4 -5
- package/dist/types/hooks/useAccount.d.ts.map +1 -1
- package/dist/types/hooks/useWallet.d.ts +14 -0
- package/dist/types/hooks/useWallet.d.ts.map +1 -0
- package/dist/types/hooks/useWallets.d.ts +7 -0
- package/dist/types/hooks/useWallets.d.ts.map +1 -0
- package/package.json +12 -12
- package/src/components/WalletAccountDetails/WalletAccountDetailsWithProvider.stories.tsx +3 -3
- package/src/components/WalletAccountSelect/Select.tsx +47 -55
- package/src/components/WalletAccountSelect/WalletAccountSelect.stories.tsx +3 -3
- package/src/components/WalletAccountSelect/WalletAccountSelectBar.stories.tsx +6 -8
- package/src/components/WalletAccountSelect/WalletAccountSelectWithProvider.stories.tsx +3 -4
- package/src/contexts/Account/use.ts +1 -1
- package/src/contexts/Wallet/Provider.tsx +20 -33
- package/src/contexts/Wallet/State.ts +10 -4
- package/src/contexts/Wallet/use.ts +23 -3
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useAccount.tsx +28 -23
- package/src/hooks/useWallet.tsx +47 -0
- package/src/hooks/useWallets.tsx +18 -0
- package/dist/cjs/hooks/useAccounts.js +0 -13
- package/dist/cjs/hooks/useAccounts.js.map +0 -1
- package/dist/esm/hooks/useAccounts.js +0 -8
- package/dist/esm/hooks/useAccounts.js.map +0 -1
- package/dist/types/hooks/useAccounts.d.ts +0 -8
- package/dist/types/hooks/useAccounts.d.ts.map +0 -1
- package/src/hooks/useAccounts.tsx +0 -16
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { CircularProgress, SelectProps } from '@mui/material'
|
|
1
|
+
import { CircularProgress, MenuItem, SelectProps } from '@mui/material'
|
|
2
2
|
import { SelectEx } from '@xylabs/react-select'
|
|
3
|
-
import { HDWallet } from '@xyo-network/account'
|
|
4
3
|
import { AddressRenderRowBox, AddressRenderRowBoxPropsBase } from '@xyo-network/react-address-render'
|
|
5
|
-
import { usePromise } from '@xyo-network/react-shared'
|
|
6
4
|
|
|
7
|
-
import {
|
|
5
|
+
import { useWalletContext } from '../../contexts'
|
|
6
|
+
import { useAccount } from '../../hooks'
|
|
8
7
|
|
|
9
8
|
type SharedAddressRenderRowBoxProps = Pick<AddressRenderRowBoxPropsBase, 'iconOnly' | 'iconSize' | 'icons' | 'showFavorite'>
|
|
10
9
|
|
|
@@ -17,31 +16,6 @@ const arrayRange = (length: number, start = 0) => {
|
|
|
17
16
|
return Array.from(Array(length).keys()).map((x) => x + start)
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
export const RenderValue: React.FC<{
|
|
21
|
-
addressNames: Record<string, string | undefined>
|
|
22
|
-
iconOnly?: boolean
|
|
23
|
-
iconSize: number
|
|
24
|
-
icons?: boolean
|
|
25
|
-
selected: number
|
|
26
|
-
showFavorite?: boolean
|
|
27
|
-
wallet: HDWallet
|
|
28
|
-
}> = ({ iconOnly, iconSize, showFavorite, icons, addressNames, selected, wallet }) => {
|
|
29
|
-
const [account] = usePromise<HDWallet>(() => wallet.derivePath(selected.toString()) as Promise<HDWallet>)
|
|
30
|
-
const customName = account ? addressNames[account?.address] : undefined
|
|
31
|
-
const favorite = account ? account?.address in addressNames : undefined
|
|
32
|
-
return (
|
|
33
|
-
<AddressRenderRowBox
|
|
34
|
-
address={account?.address}
|
|
35
|
-
iconOnly={iconOnly}
|
|
36
|
-
iconSize={iconSize}
|
|
37
|
-
icons={icons}
|
|
38
|
-
name={customName}
|
|
39
|
-
favorite={favorite}
|
|
40
|
-
showFavorite={showFavorite}
|
|
41
|
-
/>
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
19
|
export const WalletAccountSelect: React.FC<WalletAccountSelectProps> = ({
|
|
46
20
|
addressNames = {},
|
|
47
21
|
iconOnly,
|
|
@@ -52,43 +26,61 @@ export const WalletAccountSelect: React.FC<WalletAccountSelectProps> = ({
|
|
|
52
26
|
size,
|
|
53
27
|
...props
|
|
54
28
|
}) => {
|
|
55
|
-
const { activeAccountIndex = 0, setActiveAccountIndex,
|
|
56
|
-
const disabled = !
|
|
29
|
+
const { activeAccountIndex = 0, setActiveAccountIndex, derivedWallet } = useWalletContext()
|
|
30
|
+
const disabled = !derivedWallet || activeAccountIndex === undefined
|
|
57
31
|
|
|
58
32
|
return (
|
|
59
33
|
<>
|
|
60
|
-
{
|
|
34
|
+
{derivedWallet ? (
|
|
61
35
|
<SelectEx
|
|
62
36
|
disabled={disabled}
|
|
63
|
-
renderValue={(selected) =>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
37
|
+
renderValue={(selected) => {
|
|
38
|
+
const Item: React.FC = () => {
|
|
39
|
+
const [account] = useAccount({ path: selected.toString(), wallet: derivedWallet })
|
|
40
|
+
const customName = account ? addressNames[account.address] : undefined
|
|
41
|
+
const favorite = account && account.address in addressNames
|
|
42
|
+
return (
|
|
43
|
+
<AddressRenderRowBox
|
|
44
|
+
address={account?.address}
|
|
45
|
+
iconOnly={iconOnly}
|
|
46
|
+
iconSize={iconSize}
|
|
47
|
+
icons={icons}
|
|
48
|
+
name={customName}
|
|
49
|
+
favorite={favorite}
|
|
50
|
+
showFavorite={showFavorite}
|
|
51
|
+
/>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
return <Item />
|
|
55
|
+
}}
|
|
74
56
|
value={activeAccountIndex}
|
|
75
57
|
onChange={(event) => setActiveAccountIndex?.(parseInt(`${event.target.value}`))}
|
|
76
58
|
size={size}
|
|
77
59
|
variant="outlined"
|
|
78
60
|
{...props}
|
|
79
61
|
>
|
|
80
|
-
{arrayRange(maxAccounts).map((index) =>
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
62
|
+
{arrayRange(maxAccounts).map((index) => {
|
|
63
|
+
const Item: React.FC = () => {
|
|
64
|
+
const [account] = useAccount({ path: index.toString(), wallet: derivedWallet })
|
|
65
|
+
const customName = account ? addressNames[account.address] : undefined
|
|
66
|
+
const favorite = account && account.address in addressNames
|
|
67
|
+
return (
|
|
68
|
+
<MenuItem key={account?.address} value={index}>
|
|
69
|
+
<AddressRenderRowBox
|
|
70
|
+
address={account?.address}
|
|
71
|
+
favorite={favorite}
|
|
72
|
+
iconOnly={iconOnly}
|
|
73
|
+
iconSize={iconSize}
|
|
74
|
+
icons={icons}
|
|
75
|
+
name={customName}
|
|
76
|
+
showFavorite={showFavorite}
|
|
77
|
+
/>
|
|
78
|
+
</MenuItem>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return <Item key={index} />
|
|
83
|
+
})}
|
|
92
84
|
</SelectEx>
|
|
93
85
|
) : (
|
|
94
86
|
<CircularProgress size={24} />
|
|
@@ -2,7 +2,7 @@ import { Meta, StoryFn } from '@storybook/react'
|
|
|
2
2
|
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
3
3
|
|
|
4
4
|
import { WalletProvider } from '../../contexts'
|
|
5
|
-
import {
|
|
5
|
+
import { useWallet } from '../../hooks'
|
|
6
6
|
import { WalletAccountSelect } from './Select'
|
|
7
7
|
|
|
8
8
|
const StorybookEntry = {
|
|
@@ -21,9 +21,9 @@ const Template: StoryFn<typeof WalletAccountSelect> = (args) => {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
const WithWalletTemplate: StoryFn<typeof WalletAccountSelect> = (args) => {
|
|
24
|
-
const [
|
|
24
|
+
const [wallet] = useWallet({ mnemonic: DefaultSeedPhrase })
|
|
25
25
|
return (
|
|
26
|
-
<WalletProvider
|
|
26
|
+
<WalletProvider rootWallet={wallet}>
|
|
27
27
|
<WalletAccountSelect {...args} />
|
|
28
28
|
</WalletProvider>
|
|
29
29
|
)
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { Meta, StoryFn } from '@storybook/react'
|
|
2
|
-
import { HDWallet } from '@xyo-network/account'
|
|
3
|
-
import { usePromise } from '@xyo-network/react-shared'
|
|
4
2
|
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
5
3
|
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
6
4
|
|
|
7
5
|
import { WalletProvider } from '../../contexts'
|
|
8
|
-
import {
|
|
6
|
+
import { useWallet, useWallets } from '../../hooks'
|
|
9
7
|
import { WalletAccountSelectBar } from './SelectBar'
|
|
10
8
|
|
|
11
9
|
const StorybookEntry = {
|
|
@@ -24,20 +22,20 @@ const Template: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
|
24
22
|
}
|
|
25
23
|
|
|
26
24
|
const WithWalletTemplate: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
27
|
-
const [
|
|
25
|
+
const [rootWallet] = useWallet({ mnemonic: DefaultSeedPhrase })
|
|
28
26
|
return (
|
|
29
|
-
<WalletProvider
|
|
27
|
+
<WalletProvider rootWallet={rootWallet}>
|
|
30
28
|
<WalletAccountSelectBar {...args} />
|
|
31
29
|
</WalletProvider>
|
|
32
30
|
)
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
const WithFavoritesTemplate: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
36
|
-
const [
|
|
37
|
-
const [wallets] =
|
|
34
|
+
const [rootWallet] = useWallet({ mnemonic: DefaultSeedPhrase })
|
|
35
|
+
const [wallets] = useWallets({ paths: ['0', '3', '5'], wallet: rootWallet })
|
|
38
36
|
const castWallets = wallets as WalletInstance[] | undefined
|
|
39
37
|
return (
|
|
40
|
-
<WalletProvider
|
|
38
|
+
<WalletProvider rootWallet={rootWallet}>
|
|
41
39
|
<WalletAccountSelectBar
|
|
42
40
|
addressNames={
|
|
43
41
|
castWallets
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Meta, StoryFn } from '@storybook/react'
|
|
2
|
-
import { HDWallet } from '@xyo-network/account'
|
|
3
|
-
import { usePromise } from '@xyo-network/react-shared'
|
|
4
2
|
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
5
3
|
|
|
6
4
|
import { WalletProvider } from '../../contexts'
|
|
5
|
+
import { useWallet } from '../../hooks'
|
|
7
6
|
import { WalletAccountSelect } from './Select'
|
|
8
7
|
|
|
9
8
|
const StorybookEntry = {
|
|
@@ -18,9 +17,9 @@ const StorybookEntry = {
|
|
|
18
17
|
} as Meta<typeof WalletAccountSelect>
|
|
19
18
|
|
|
20
19
|
const Template: StoryFn<typeof WalletAccountSelect> = (args) => {
|
|
21
|
-
const [
|
|
20
|
+
const [rootWallet] = useWallet({ mnemonic: DefaultSeedPhrase })
|
|
22
21
|
return (
|
|
23
|
-
<WalletProvider
|
|
22
|
+
<WalletProvider rootWallet={rootWallet}>
|
|
24
23
|
<WalletAccountSelect {...args}></WalletAccountSelect>
|
|
25
24
|
</WalletProvider>
|
|
26
25
|
)
|
|
@@ -2,7 +2,7 @@ import { useContextEx } from '@xyo-network/react-shared'
|
|
|
2
2
|
|
|
3
3
|
import { AccountContext } from './Context'
|
|
4
4
|
|
|
5
|
-
export const
|
|
5
|
+
export const useContextAccount = (required = false) => {
|
|
6
6
|
const { account } = useContextEx(AccountContext, 'Account', required)
|
|
7
7
|
return [account]
|
|
8
8
|
}
|
|
@@ -1,37 +1,24 @@
|
|
|
1
|
-
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
2
1
|
import { WithChildren } from '@xylabs/react-shared'
|
|
3
|
-
import { HDWallet } from '@xyo-network/account'
|
|
4
2
|
import { usePromise } from '@xyo-network/react-shared'
|
|
5
3
|
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
6
4
|
import { useEffect, useState } from 'react'
|
|
7
5
|
|
|
8
|
-
import { AccountContext } from '../Account'
|
|
9
6
|
import { WalletContext } from './Context'
|
|
10
7
|
import { WalletRootPath } from './lib'
|
|
11
|
-
import { useWallet } from './use'
|
|
12
8
|
|
|
13
9
|
export interface WalletProviderProps {
|
|
14
10
|
basePath?: string
|
|
15
11
|
defaultActiveAccountIndex?: number
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const AccountWalletProvider: React.FC<WithChildren> = (props) => {
|
|
20
|
-
const { wallet, activeAccountIndex = 0 } = useWallet()
|
|
21
|
-
|
|
22
|
-
const [account] = usePromise(() => wallet?.derivePath(activeAccountIndex.toString()))
|
|
23
|
-
|
|
24
|
-
return <AccountContext.Provider value={{ account, provided: true }} {...props} />
|
|
12
|
+
rootWallet?: WalletInstance
|
|
25
13
|
}
|
|
26
14
|
|
|
27
15
|
export const WalletProvider: React.FC<WithChildren<WalletProviderProps>> = ({
|
|
28
16
|
basePath = WalletRootPath,
|
|
29
17
|
children,
|
|
30
18
|
defaultActiveAccountIndex = 0,
|
|
31
|
-
|
|
19
|
+
rootWallet,
|
|
32
20
|
...props
|
|
33
21
|
}) => {
|
|
34
|
-
const [wallet, setWallet] = useState<HDWallet | undefined>()
|
|
35
22
|
const [activeAccountIndex, setActiveAccountIndex] = useState(defaultActiveAccountIndex)
|
|
36
23
|
|
|
37
24
|
useEffect(() => {
|
|
@@ -40,37 +27,37 @@ export const WalletProvider: React.FC<WithChildren<WalletProviderProps>> = ({
|
|
|
40
27
|
}
|
|
41
28
|
}, [defaultActiveAccountIndex])
|
|
42
29
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
setWallet(walletWithBasePath)
|
|
52
|
-
} catch (e) {
|
|
53
|
-
console.error('Error setting proper wallet base path', e)
|
|
54
|
-
}
|
|
30
|
+
const [derivedWallet] = usePromise(async () => {
|
|
31
|
+
// ensure the wallet has the proper base
|
|
32
|
+
if (rootWallet) {
|
|
33
|
+
if (!rootWallet?.path.includes(basePath)) {
|
|
34
|
+
try {
|
|
35
|
+
return await rootWallet?.derivePath(basePath)
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.error('Error setting proper wallet base path', e)
|
|
55
38
|
}
|
|
56
39
|
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
40
|
+
} else {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}, [basePath, rootWallet])
|
|
44
|
+
|
|
45
|
+
const [activeAccount] = usePromise(() => derivedWallet?.derivePath(activeAccountIndex.toString()), [derivedWallet, activeAccountIndex])
|
|
60
46
|
|
|
61
47
|
return (
|
|
62
48
|
<WalletContext.Provider
|
|
63
49
|
value={{
|
|
50
|
+
activeAccount,
|
|
64
51
|
activeAccountIndex,
|
|
65
52
|
basePath,
|
|
53
|
+
derivedWallet,
|
|
66
54
|
provided: true,
|
|
55
|
+
rootWallet,
|
|
67
56
|
setActiveAccountIndex,
|
|
68
|
-
setWallet,
|
|
69
|
-
wallet,
|
|
70
57
|
}}
|
|
71
58
|
{...props}
|
|
72
59
|
>
|
|
73
|
-
|
|
60
|
+
{children}
|
|
74
61
|
</WalletContext.Provider>
|
|
75
62
|
)
|
|
76
63
|
}
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { HDWallet } from '@xyo-network/account'
|
|
2
1
|
import { ContextExState } from '@xyo-network/react-shared'
|
|
3
|
-
import {
|
|
2
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
|
+
import { Dispatch } from 'react'
|
|
4
4
|
|
|
5
5
|
export interface WalletContextState extends ContextExState {
|
|
6
|
+
/** Currently selected account */
|
|
7
|
+
activeAccount?: WalletInstance
|
|
8
|
+
/** Currently selected index */
|
|
6
9
|
activeAccountIndex?: number
|
|
7
10
|
/** Base path from which the wallet was derived */
|
|
8
11
|
basePath?: string
|
|
12
|
+
/** The derived wallet being used */
|
|
13
|
+
derivedWallet?: WalletInstance
|
|
14
|
+
/** The root wallet being used */
|
|
15
|
+
rootWallet?: WalletInstance
|
|
16
|
+
/** Set currently selected index */
|
|
9
17
|
setActiveAccountIndex?: Dispatch<number>
|
|
10
|
-
setWallet?: Dispatch<SetStateAction<HDWallet | undefined>>
|
|
11
|
-
wallet?: HDWallet
|
|
12
18
|
}
|
|
@@ -1,7 +1,27 @@
|
|
|
1
|
-
import { useContextEx } from '@xyo-network/react-shared'
|
|
1
|
+
import { useContextEx, usePromise } from '@xyo-network/react-shared'
|
|
2
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
2
3
|
|
|
3
4
|
import { WalletContext } from './Context'
|
|
4
5
|
|
|
5
|
-
export const
|
|
6
|
-
return useContextEx(WalletContext, '
|
|
6
|
+
export const useWalletContext = (required = true) => {
|
|
7
|
+
return useContextEx(WalletContext, 'WalletContext', required)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const useContextWallet = (required = true): [WalletInstance | undefined] => {
|
|
11
|
+
const { derivedWallet } = useWalletContext(required)
|
|
12
|
+
return [derivedWallet]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | undefined] => {
|
|
16
|
+
const [wallet] = useContextWallet(required)
|
|
17
|
+
const [account] = usePromise(() => wallet?.derivePath(index.toString()), [wallet, index])
|
|
18
|
+
return [account]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const useSelectedWalletAccount = (required = true): [WalletInstance | undefined] => {
|
|
22
|
+
const { activeAccountIndex } = useWalletContext(required)
|
|
23
|
+
//we pass in 0 as default since we can not call the hook optionally,
|
|
24
|
+
//we resolve this false result by checking whether activeAccountIndex is defined before returning it
|
|
25
|
+
const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0)
|
|
26
|
+
return [activeAccountIndex !== undefined ? account : undefined]
|
|
7
27
|
}
|
package/src/hooks/index.ts
CHANGED
package/src/hooks/useAccount.tsx
CHANGED
|
@@ -1,42 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DataLike } from '@xyo-network/core'
|
|
1
|
+
import { AccountInstance } from '@xyo-network/account-model'
|
|
3
2
|
import { usePromise } from '@xyo-network/react-shared'
|
|
4
3
|
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
5
4
|
import { useState } from 'react'
|
|
6
5
|
|
|
7
|
-
import {
|
|
6
|
+
import { useContextAccount } from '../contexts'
|
|
8
7
|
|
|
9
8
|
export interface AccountHookParams {
|
|
10
|
-
account?:
|
|
11
|
-
mnemonic?: string
|
|
9
|
+
account?: AccountInstance
|
|
12
10
|
path?: string
|
|
13
11
|
required?: boolean
|
|
14
|
-
|
|
12
|
+
wallet?: WalletInstance
|
|
15
13
|
}
|
|
16
14
|
|
|
17
|
-
export const useAccount = ({
|
|
15
|
+
export const useAccount = ({ wallet, account, path, required = false }: AccountHookParams = {}): [AccountInstance | undefined, Error | undefined] => {
|
|
16
|
+
let validationError: Error | undefined = undefined
|
|
17
|
+
if (wallet && account) {
|
|
18
|
+
validationError = Error('useAccount can not have both a wallet and an account in the parameters')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (path && account) {
|
|
22
|
+
validationError = Error('useAccount can not have both a path and an account in the parameters')
|
|
23
|
+
}
|
|
18
24
|
const [error, setError] = useState<Error>()
|
|
19
|
-
const [contextAccount] =
|
|
25
|
+
const [contextAccount] = useContextAccount(!(account || wallet) && required)
|
|
20
26
|
const [activeAccount] = usePromise(async () => {
|
|
21
27
|
try {
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
if (!validationError) {
|
|
29
|
+
if (wallet) {
|
|
30
|
+
if (path) {
|
|
31
|
+
return await wallet?.derivePath?.(path)
|
|
32
|
+
} else {
|
|
33
|
+
return wallet
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
return contextAccount
|
|
29
37
|
}
|
|
30
|
-
return contextAccount
|
|
31
|
-
})()
|
|
32
|
-
if (path) {
|
|
33
|
-
return newAccount?.derivePath?.(path)
|
|
34
|
-
} else {
|
|
35
|
-
return newAccount
|
|
36
38
|
}
|
|
37
39
|
} catch (ex) {
|
|
38
40
|
setError(ex as Error)
|
|
39
41
|
}
|
|
40
|
-
}, [
|
|
41
|
-
|
|
42
|
+
}, [path, wallet, contextAccount, validationError])
|
|
43
|
+
if (validationError && !error) {
|
|
44
|
+
setError(validationError)
|
|
45
|
+
}
|
|
46
|
+
return [error ? undefined : activeAccount, error]
|
|
42
47
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { HDWallet } from '@xyo-network/account'
|
|
2
|
+
import { DataLike } from '@xyo-network/core'
|
|
3
|
+
import { usePromise } from '@xyo-network/react-shared'
|
|
4
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
5
|
+
import { useState } from 'react'
|
|
6
|
+
|
|
7
|
+
import { useContextWallet } from '../contexts'
|
|
8
|
+
|
|
9
|
+
export interface WalletHookParams {
|
|
10
|
+
mnemonic?: string
|
|
11
|
+
path?: string
|
|
12
|
+
required?: boolean
|
|
13
|
+
seed?: DataLike
|
|
14
|
+
wallet?: WalletInstance
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const useWallet = ({ mnemonic, wallet, path, required = false, seed }: WalletHookParams = {}): [
|
|
18
|
+
WalletInstance | undefined,
|
|
19
|
+
Error | undefined,
|
|
20
|
+
] => {
|
|
21
|
+
const [error, setError] = useState<Error>()
|
|
22
|
+
const [contextAccount] = useContextWallet(!wallet && required)
|
|
23
|
+
const [activeAccount] = usePromise(async () => {
|
|
24
|
+
try {
|
|
25
|
+
const newAccount = await (() => {
|
|
26
|
+
if (wallet) {
|
|
27
|
+
return wallet
|
|
28
|
+
} else if (mnemonic) {
|
|
29
|
+
return HDWallet.fromMnemonic(mnemonic as string)
|
|
30
|
+
} else if (seed) {
|
|
31
|
+
return HDWallet.fromSeed(seed)
|
|
32
|
+
}
|
|
33
|
+
return contextAccount
|
|
34
|
+
})()
|
|
35
|
+
|
|
36
|
+
if (path) {
|
|
37
|
+
console.log(`path: ${path}`)
|
|
38
|
+
return newAccount?.derivePath?.(path)
|
|
39
|
+
} else {
|
|
40
|
+
return newAccount ?? wallet
|
|
41
|
+
}
|
|
42
|
+
} catch (ex) {
|
|
43
|
+
setError(ex as Error)
|
|
44
|
+
}
|
|
45
|
+
}, [mnemonic, contextAccount, seed, path, wallet])
|
|
46
|
+
return [activeAccount, error]
|
|
47
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { usePromise } from '@xyo-network/react-shared'
|
|
2
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
|
+
|
|
4
|
+
import { useWallet } from './useWallet'
|
|
5
|
+
|
|
6
|
+
export interface WalletsHookParams {
|
|
7
|
+
paths: string[]
|
|
8
|
+
wallet?: WalletInstance
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const useWallets = ({ wallet, paths }: WalletsHookParams): [WalletInstance[] | undefined, Error | undefined] => {
|
|
12
|
+
const [foundWallet] = useWallet({ wallet })
|
|
13
|
+
const [wallets, error] = usePromise(
|
|
14
|
+
() => (foundWallet ? Promise.all(paths.map((path) => foundWallet.derivePath(path))) : undefined),
|
|
15
|
+
[foundWallet, paths],
|
|
16
|
+
)
|
|
17
|
+
return [wallets, error]
|
|
18
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useAccounts = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const react_shared_1 = require("@xyo-network/react-shared");
|
|
6
|
-
const contexts_1 = require("../contexts");
|
|
7
|
-
const useAccounts = ({ account, paths, required } = {}) => {
|
|
8
|
-
const [contextAccount] = (0, contexts_1.useAccountFromContext)(!account && required);
|
|
9
|
-
const activeAccount = account !== null && account !== void 0 ? account : contextAccount;
|
|
10
|
-
return (0, react_shared_1.usePromise)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return (paths ? yield Promise.all(paths.map((path) => { var _a; return (_a = activeAccount === null || activeAccount === void 0 ? void 0 : activeAccount.derivePath) === null || _a === void 0 ? void 0 : _a.call(activeAccount, path); })) : activeAccount); }));
|
|
11
|
-
};
|
|
12
|
-
exports.useAccounts = useAccounts;
|
|
13
|
-
//# sourceMappingURL=useAccounts.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAccounts.js","sourceRoot":"","sources":["../../../src/hooks/useAccounts.tsx"],"names":[],"mappings":";;;;AAAA,4DAAsD;AAGtD,0CAAmD;AAQ5C,MAAM,WAAW,GAAG,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,KAAyB,EAAE,EAAE,EAAE;IACnF,MAAM,CAAC,cAAc,CAAC,GAAG,IAAA,gCAAqB,EAAC,CAAC,OAAO,IAAI,QAAQ,CAAC,CAAA;IACpE,MAAM,aAAa,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,cAAc,CAAA;IAC/C,OAAO,IAAA,yBAAU,EAAC,GAAS,EAAE,0DAAC,OAAA,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,WAAC,OAAA,MAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,UAAU,8DAAG,IAAI,CAAC,CAAA,EAAA,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAA,GAAA,CAAC,CAAA;AACpI,CAAC,CAAA;AAJY,QAAA,WAAW,eAIvB"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xyo-network/react-shared';
|
|
2
|
-
import { useAccountFromContext } from '../contexts';
|
|
3
|
-
export const useAccounts = ({ account, paths, required } = {}) => {
|
|
4
|
-
const [contextAccount] = useAccountFromContext(!account && required);
|
|
5
|
-
const activeAccount = account ?? contextAccount;
|
|
6
|
-
return usePromise(async () => (paths ? await Promise.all(paths.map((path) => activeAccount?.derivePath?.(path))) : activeAccount));
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=useAccounts.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAccounts.js","sourceRoot":"","sources":["../../../src/hooks/useAccounts.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAQnD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,KAAyB,EAAE,EAAE,EAAE;IACnF,MAAM,CAAC,cAAc,CAAC,GAAG,qBAAqB,CAAC,CAAC,OAAO,IAAI,QAAQ,CAAC,CAAA;IACpE,MAAM,aAAa,GAAG,OAAO,IAAI,cAAc,CAAA;IAC/C,OAAO,UAAU,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,EAAE,UAAU,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAA;AACpI,CAAC,CAAA"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { WalletInstance } from '@xyo-network/wallet-model';
|
|
2
|
-
export interface AccountsHookParams {
|
|
3
|
-
account?: WalletInstance;
|
|
4
|
-
paths?: string[];
|
|
5
|
-
required?: boolean;
|
|
6
|
-
}
|
|
7
|
-
export declare const useAccounts: ({ account, paths, required }?: AccountsHookParams) => [WalletInstance | (WalletInstance | undefined)[] | undefined, Error | undefined, import("@xyo-network/react-shared").State | undefined];
|
|
8
|
-
//# sourceMappingURL=useAccounts.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAccounts.d.ts","sourceRoot":"","sources":["../../../src/hooks/useAccounts.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAI1D,MAAM,WAAW,kBAAkB;IACjC,OAAO,CAAC,EAAE,cAAc,CAAA;IACxB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,eAAO,MAAM,WAAW,kCAAkC,kBAAkB,4IAI3E,CAAA"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xyo-network/react-shared'
|
|
2
|
-
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
|
-
|
|
4
|
-
import { useAccountFromContext } from '../contexts'
|
|
5
|
-
|
|
6
|
-
export interface AccountsHookParams {
|
|
7
|
-
account?: WalletInstance
|
|
8
|
-
paths?: string[]
|
|
9
|
-
required?: boolean
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const useAccounts = ({ account, paths, required }: AccountsHookParams = {}) => {
|
|
13
|
-
const [contextAccount] = useAccountFromContext(!account && required)
|
|
14
|
-
const activeAccount = account ?? contextAccount
|
|
15
|
-
return usePromise(async () => (paths ? await Promise.all(paths.map((path) => activeAccount?.derivePath?.(path))) : activeAccount))
|
|
16
|
-
}
|