@xyo-network/react-wallet 2.54.0 → 2.55.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/dist/cjs/components/WalletAccountDetails/WalletAccountDetails.js +4 -4
- package/dist/cjs/components/WalletAccountDetails/WalletAccountDetails.js.map +1 -1
- package/dist/cjs/components/WalletAccountSelect/Select.js +10 -12
- package/dist/cjs/components/WalletAccountSelect/Select.js.map +1 -1
- package/dist/cjs/contexts/Account/MemoryProvider.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 +9 -4
- package/dist/cjs/contexts/Wallet/Provider.js.map +1 -1
- package/dist/cjs/hooks/index.js +6 -0
- package/dist/cjs/hooks/index.js.map +1 -0
- package/dist/cjs/hooks/useAccount.js +14 -0
- package/dist/cjs/hooks/useAccount.js.map +1 -0
- package/dist/cjs/hooks/useAccounts.js +13 -0
- package/dist/cjs/hooks/useAccounts.js.map +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/docs.json +16803 -5427
- package/dist/esm/components/WalletAccountDetails/WalletAccountDetails.js +3 -3
- package/dist/esm/components/WalletAccountDetails/WalletAccountDetails.js.map +1 -1
- package/dist/esm/components/WalletAccountSelect/Select.js +9 -12
- package/dist/esm/components/WalletAccountSelect/Select.js.map +1 -1
- package/dist/esm/contexts/Account/MemoryProvider.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 +8 -3
- package/dist/esm/contexts/Wallet/Provider.js.map +1 -1
- package/dist/esm/hooks/index.js +3 -0
- package/dist/esm/hooks/index.js.map +1 -0
- package/dist/esm/hooks/useAccount.js +9 -0
- package/dist/esm/hooks/useAccount.js.map +1 -0
- package/dist/esm/hooks/useAccounts.js +8 -0
- package/dist/esm/hooks/useAccounts.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/types/components/WalletAccountSelect/Select.d.ts +10 -0
- package/dist/types/components/WalletAccountSelect/Select.d.ts.map +1 -1
- package/dist/types/contexts/Account/MemoryProvider.d.ts +2 -2
- package/dist/types/contexts/Account/MemoryProvider.d.ts.map +1 -1
- package/dist/types/contexts/Account/State.d.ts +3 -3
- package/dist/types/contexts/Account/State.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 +2 -2
- package/dist/types/contexts/Wallet/Provider.d.ts.map +1 -1
- package/dist/types/hooks/index.d.ts +3 -0
- package/dist/types/hooks/index.d.ts.map +1 -0
- package/dist/types/hooks/useAccount.d.ts +11 -0
- package/dist/types/hooks/useAccount.d.ts.map +1 -0
- package/dist/types/hooks/useAccounts.d.ts +8 -0
- package/dist/types/hooks/useAccounts.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +12 -10
- package/src/components/WalletAccountDetails/WalletAccountDetails.tsx +3 -3
- package/src/components/WalletAccountDetails/WalletAccountDetailsWithProvider.stories.tsx +3 -2
- package/src/components/WalletAccountSelect/Select.tsx +51 -35
- package/src/components/WalletAccountSelect/WalletAccountSelect.stories.tsx +3 -2
- package/src/components/WalletAccountSelect/WalletAccountSelectBar.stories.tsx +31 -15
- package/src/components/WalletAccountSelect/WalletAccountSelectWithProvider.stories.tsx +3 -1
- package/src/contexts/Account/MemoryProvider.tsx +3 -3
- package/src/contexts/Account/State.ts +3 -3
- package/src/contexts/Account/use.ts +1 -1
- package/src/contexts/Wallet/Provider.tsx +24 -15
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useAccount.tsx +23 -0
- package/src/hooks/useAccounts.tsx +16 -0
- package/src/index.ts +1 -0
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { Meta, StoryFn } from '@storybook/react'
|
|
2
2
|
import { HDWallet } from '@xyo-network/account'
|
|
3
|
+
import { usePromise } from '@xyo-network/react-shared'
|
|
3
4
|
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
5
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
6
|
|
|
5
|
-
import { WalletProvider
|
|
7
|
+
import { WalletProvider } from '../../contexts'
|
|
8
|
+
import { useAccounts } from '../../hooks'
|
|
6
9
|
import { WalletAccountSelectBar } from './SelectBar'
|
|
7
10
|
|
|
8
|
-
const defaultWallet = HDWallet.fromMnemonic(DefaultSeedPhrase)
|
|
9
|
-
|
|
10
11
|
const StorybookEntry = {
|
|
11
12
|
argTypes: {},
|
|
12
13
|
component: WalletAccountSelectBar,
|
|
@@ -23,6 +24,7 @@ const Template: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
const WithWalletTemplate: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
27
|
+
const [defaultWallet] = usePromise(() => HDWallet.fromMnemonic(DefaultSeedPhrase))
|
|
26
28
|
return (
|
|
27
29
|
<WalletProvider defaultWallet={defaultWallet}>
|
|
28
30
|
<WalletAccountSelectBar {...args} />
|
|
@@ -30,6 +32,31 @@ const WithWalletTemplate: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
|
30
32
|
)
|
|
31
33
|
}
|
|
32
34
|
|
|
35
|
+
const WithFavoritesTemplate: StoryFn<typeof WalletAccountSelectBar> = (args) => {
|
|
36
|
+
const [defaultWallet] = usePromise(() => HDWallet.fromMnemonic(DefaultSeedPhrase))
|
|
37
|
+
const [wallets] = useAccounts({ account: defaultWallet, paths: ['0', '3', '5'] })
|
|
38
|
+
const castWallets = wallets as WalletInstance[]
|
|
39
|
+
return (
|
|
40
|
+
<WalletProvider defaultWallet={defaultWallet}>
|
|
41
|
+
<WalletAccountSelectBar
|
|
42
|
+
addressNames={
|
|
43
|
+
wallets
|
|
44
|
+
? {
|
|
45
|
+
[castWallets[0]?.address]: 'first address',
|
|
46
|
+
[castWallets[1]?.address]: undefined,
|
|
47
|
+
[castWallets[2]?.address]: 'sixth address',
|
|
48
|
+
}
|
|
49
|
+
: {}
|
|
50
|
+
}
|
|
51
|
+
icons={true}
|
|
52
|
+
maxAccounts={10}
|
|
53
|
+
showFavorite
|
|
54
|
+
{...args}
|
|
55
|
+
/>
|
|
56
|
+
</WalletProvider>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
33
60
|
const Default = Template.bind({})
|
|
34
61
|
Default.args = {}
|
|
35
62
|
|
|
@@ -42,18 +69,7 @@ WithWalletIcon.args = { icons: true }
|
|
|
42
69
|
const WithAdditionalAccounts = WithWalletTemplate.bind({})
|
|
43
70
|
WithAdditionalAccounts.args = { icons: true, maxAccounts: 10 }
|
|
44
71
|
|
|
45
|
-
const WithAccountFavorites =
|
|
46
|
-
const defaultWalletAtIndex = defaultWallet.derivePath(WalletRootPath)
|
|
47
|
-
WithAccountFavorites.args = {
|
|
48
|
-
addressNames: {
|
|
49
|
-
[defaultWalletAtIndex.derivePath('0').addressValue.hex]: 'first address',
|
|
50
|
-
[defaultWalletAtIndex.derivePath('3').addressValue.hex]: undefined,
|
|
51
|
-
[defaultWalletAtIndex.derivePath('5').addressValue.hex]: 'sixth address',
|
|
52
|
-
},
|
|
53
|
-
icons: true,
|
|
54
|
-
maxAccounts: 10,
|
|
55
|
-
showFavorite: true,
|
|
56
|
-
}
|
|
72
|
+
const WithAccountFavorites = WithFavoritesTemplate.bind({})
|
|
57
73
|
|
|
58
74
|
export { Default, WithAccountFavorites, WithAdditionalAccounts, WithWallet, WithWalletIcon }
|
|
59
75
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Meta, StoryFn } from '@storybook/react'
|
|
2
2
|
import { HDWallet } from '@xyo-network/account'
|
|
3
|
+
import { usePromise } from '@xyo-network/react-shared'
|
|
3
4
|
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
4
5
|
|
|
5
6
|
import { WalletProvider } from '../../contexts'
|
|
@@ -17,8 +18,9 @@ const StorybookEntry = {
|
|
|
17
18
|
} as Meta<typeof WalletAccountSelect>
|
|
18
19
|
|
|
19
20
|
const Template: StoryFn<typeof WalletAccountSelect> = (args) => {
|
|
21
|
+
const [wallet] = usePromise(() => HDWallet.fromMnemonic(DefaultSeedPhrase) as Promise<HDWallet>)
|
|
20
22
|
return (
|
|
21
|
-
<WalletProvider defaultWallet={
|
|
23
|
+
<WalletProvider defaultWallet={wallet}>
|
|
22
24
|
<WalletAccountSelect {...args}></WalletAccountSelect>
|
|
23
25
|
</WalletProvider>
|
|
24
26
|
)
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { WithChildren } from '@xylabs/react-shared'
|
|
2
|
-
import {
|
|
2
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
3
|
import { useState } from 'react'
|
|
4
4
|
|
|
5
5
|
import { AccountContext } from './Context'
|
|
6
6
|
|
|
7
7
|
export interface AccountMemoryProviderProps {
|
|
8
|
-
defaultAccount?:
|
|
8
|
+
defaultAccount?: WalletInstance
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export const AccountMemoryProvider: React.FC<WithChildren<AccountMemoryProviderProps>> = ({ defaultAccount, ...props }) => {
|
|
12
|
-
const [account, setAccount] = useState<
|
|
12
|
+
const [account, setAccount] = useState<WalletInstance | undefined>(defaultAccount)
|
|
13
13
|
|
|
14
14
|
return <AccountContext.Provider value={{ account: account, provided: true, setAccount }} {...props} />
|
|
15
15
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AccountInstance } from '@xyo-network/account-model'
|
|
2
1
|
import { ContextExState } from '@xyo-network/react-shared'
|
|
2
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
3
|
import { Dispatch } from 'react'
|
|
4
4
|
|
|
5
5
|
export interface AccountContextState extends ContextExState {
|
|
6
|
-
account?:
|
|
7
|
-
setAccount?: Dispatch<
|
|
6
|
+
account?: WalletInstance
|
|
7
|
+
setAccount?: Dispatch<WalletInstance>
|
|
8
8
|
}
|
|
@@ -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 useAccountFromContext = (required = false) => {
|
|
6
6
|
const { account } = useContextEx(AccountContext, 'Account', required)
|
|
7
7
|
return [account]
|
|
8
8
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
1
2
|
import { WithChildren } from '@xylabs/react-shared'
|
|
2
3
|
import { HDWallet } from '@xyo-network/account'
|
|
4
|
+
import { usePromise } from '@xyo-network/react-shared'
|
|
5
|
+
import { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
6
|
import { useEffect, useState } from 'react'
|
|
4
7
|
|
|
5
8
|
import { AccountContext } from '../Account'
|
|
@@ -10,13 +13,15 @@ import { useWallet } from './use'
|
|
|
10
13
|
export interface WalletProviderProps {
|
|
11
14
|
basePath?: string
|
|
12
15
|
defaultActiveAccountIndex?: number
|
|
13
|
-
defaultWallet?:
|
|
16
|
+
defaultWallet?: WalletInstance
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
const AccountWalletProvider: React.FC<WithChildren> = (props) => {
|
|
17
20
|
const { wallet, activeAccountIndex = 0 } = useWallet()
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
const [account] = usePromise(() => wallet?.derivePath(activeAccountIndex.toString()))
|
|
23
|
+
|
|
24
|
+
return <AccountContext.Provider value={{ account, provided: true }} {...props} />
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
export const WalletProvider: React.FC<WithChildren<WalletProviderProps>> = ({
|
|
@@ -35,21 +40,25 @@ export const WalletProvider: React.FC<WithChildren<WalletProviderProps>> = ({
|
|
|
35
40
|
}
|
|
36
41
|
}, [defaultActiveAccountIndex])
|
|
37
42
|
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
useAsyncEffect(
|
|
44
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
45
|
+
async () => {
|
|
46
|
+
// ensure the wallet has the proper base
|
|
47
|
+
if (defaultWallet) {
|
|
48
|
+
if (!wallet?.path.includes(basePath)) {
|
|
49
|
+
try {
|
|
50
|
+
const walletWithBasePath = (await defaultWallet?.derivePath(basePath)) as HDWallet
|
|
51
|
+
setWallet(walletWithBasePath)
|
|
52
|
+
} catch (e) {
|
|
53
|
+
console.error('Error setting proper wallet base path', e)
|
|
54
|
+
}
|
|
47
55
|
}
|
|
56
|
+
} else {
|
|
57
|
+
throw Error('WalletProvider requires a default HDWallet')
|
|
48
58
|
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}, [basePath, defaultWallet, wallet?.path])
|
|
59
|
+
},
|
|
60
|
+
[basePath, defaultWallet, wallet?.path],
|
|
61
|
+
)
|
|
53
62
|
|
|
54
63
|
return (
|
|
55
64
|
<WalletContext.Provider
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
6
|
+
import { useAccountFromContext } from '../contexts'
|
|
7
|
+
|
|
8
|
+
export interface AccountHookParams {
|
|
9
|
+
account?: WalletInstance
|
|
10
|
+
mnemonic?: string
|
|
11
|
+
path?: string
|
|
12
|
+
required?: boolean
|
|
13
|
+
seed?: DataLike
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const useAccount = ({ mnemonic, account, path, required, seed }: AccountHookParams = {}) => {
|
|
17
|
+
const [contextAccount] = useAccountFromContext(!account && required)
|
|
18
|
+
const [activeAccount] = usePromise(
|
|
19
|
+
async () => (account ?? mnemonic ? await HDWallet.fromMnemonic(mnemonic as string) : seed ? await HDWallet.fromSeed(seed) : contextAccount),
|
|
20
|
+
[account, mnemonic, contextAccount, seed],
|
|
21
|
+
)
|
|
22
|
+
return usePromise(async () => (path ? await activeAccount?.derivePath?.(path) : activeAccount))
|
|
23
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
package/src/index.ts
CHANGED