@xyo-network/react-wallet 2.54.1 → 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.
Files changed (68) hide show
  1. package/dist/cjs/components/WalletAccountDetails/WalletAccountDetails.js +4 -4
  2. package/dist/cjs/components/WalletAccountDetails/WalletAccountDetails.js.map +1 -1
  3. package/dist/cjs/components/WalletAccountSelect/Select.js +10 -12
  4. package/dist/cjs/components/WalletAccountSelect/Select.js.map +1 -1
  5. package/dist/cjs/contexts/Account/MemoryProvider.js.map +1 -1
  6. package/dist/cjs/contexts/Account/use.js +3 -3
  7. package/dist/cjs/contexts/Account/use.js.map +1 -1
  8. package/dist/cjs/contexts/Wallet/Provider.js +9 -4
  9. package/dist/cjs/contexts/Wallet/Provider.js.map +1 -1
  10. package/dist/cjs/hooks/index.js +6 -0
  11. package/dist/cjs/hooks/index.js.map +1 -0
  12. package/dist/cjs/hooks/useAccount.js +14 -0
  13. package/dist/cjs/hooks/useAccount.js.map +1 -0
  14. package/dist/cjs/hooks/useAccounts.js +13 -0
  15. package/dist/cjs/hooks/useAccounts.js.map +1 -0
  16. package/dist/cjs/index.js +1 -0
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/docs.json +1920 -1047
  19. package/dist/esm/components/WalletAccountDetails/WalletAccountDetails.js +3 -3
  20. package/dist/esm/components/WalletAccountDetails/WalletAccountDetails.js.map +1 -1
  21. package/dist/esm/components/WalletAccountSelect/Select.js +9 -12
  22. package/dist/esm/components/WalletAccountSelect/Select.js.map +1 -1
  23. package/dist/esm/contexts/Account/MemoryProvider.js.map +1 -1
  24. package/dist/esm/contexts/Account/use.js +1 -1
  25. package/dist/esm/contexts/Account/use.js.map +1 -1
  26. package/dist/esm/contexts/Wallet/Provider.js +8 -3
  27. package/dist/esm/contexts/Wallet/Provider.js.map +1 -1
  28. package/dist/esm/hooks/index.js +3 -0
  29. package/dist/esm/hooks/index.js.map +1 -0
  30. package/dist/esm/hooks/useAccount.js +9 -0
  31. package/dist/esm/hooks/useAccount.js.map +1 -0
  32. package/dist/esm/hooks/useAccounts.js +8 -0
  33. package/dist/esm/hooks/useAccounts.js.map +1 -0
  34. package/dist/esm/index.js +1 -0
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/types/components/WalletAccountSelect/Select.d.ts +10 -0
  37. package/dist/types/components/WalletAccountSelect/Select.d.ts.map +1 -1
  38. package/dist/types/contexts/Account/MemoryProvider.d.ts +2 -2
  39. package/dist/types/contexts/Account/MemoryProvider.d.ts.map +1 -1
  40. package/dist/types/contexts/Account/State.d.ts +3 -3
  41. package/dist/types/contexts/Account/State.d.ts.map +1 -1
  42. package/dist/types/contexts/Account/use.d.ts +1 -1
  43. package/dist/types/contexts/Account/use.d.ts.map +1 -1
  44. package/dist/types/contexts/Wallet/Provider.d.ts +2 -2
  45. package/dist/types/contexts/Wallet/Provider.d.ts.map +1 -1
  46. package/dist/types/hooks/index.d.ts +3 -0
  47. package/dist/types/hooks/index.d.ts.map +1 -0
  48. package/dist/types/hooks/useAccount.d.ts +11 -0
  49. package/dist/types/hooks/useAccount.d.ts.map +1 -0
  50. package/dist/types/hooks/useAccounts.d.ts +8 -0
  51. package/dist/types/hooks/useAccounts.d.ts.map +1 -0
  52. package/dist/types/index.d.ts +1 -0
  53. package/dist/types/index.d.ts.map +1 -1
  54. package/package.json +12 -10
  55. package/src/components/WalletAccountDetails/WalletAccountDetails.tsx +3 -3
  56. package/src/components/WalletAccountDetails/WalletAccountDetailsWithProvider.stories.tsx +3 -2
  57. package/src/components/WalletAccountSelect/Select.tsx +51 -35
  58. package/src/components/WalletAccountSelect/WalletAccountSelect.stories.tsx +3 -2
  59. package/src/components/WalletAccountSelect/WalletAccountSelectBar.stories.tsx +31 -15
  60. package/src/components/WalletAccountSelect/WalletAccountSelectWithProvider.stories.tsx +3 -1
  61. package/src/contexts/Account/MemoryProvider.tsx +3 -3
  62. package/src/contexts/Account/State.ts +3 -3
  63. package/src/contexts/Account/use.ts +1 -1
  64. package/src/contexts/Wallet/Provider.tsx +24 -15
  65. package/src/hooks/index.ts +2 -0
  66. package/src/hooks/useAccount.tsx +23 -0
  67. package/src/hooks/useAccounts.tsx +16 -0
  68. 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, WalletRootPath } from '../../contexts'
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 = WithWalletTemplate.bind({})
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={HDWallet.fromMnemonic(DefaultSeedPhrase)}>
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 { AccountInstance } from '@xyo-network/account-model'
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?: AccountInstance
8
+ defaultAccount?: WalletInstance
9
9
  }
10
10
 
11
11
  export const AccountMemoryProvider: React.FC<WithChildren<AccountMemoryProviderProps>> = ({ defaultAccount, ...props }) => {
12
- const [account, setAccount] = useState<AccountInstance | undefined>(defaultAccount)
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?: AccountInstance
7
- setAccount?: Dispatch<AccountInstance>
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 useAccount = (required = false) => {
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?: HDWallet
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
- return <AccountContext.Provider value={{ account: wallet?.derivePath(activeAccountIndex.toString()), provided: true }} {...props} />
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
- useEffect(() => {
39
- // ensure the wallet has the proper base
40
- if (defaultWallet) {
41
- if (!wallet?.path.includes(basePath)) {
42
- try {
43
- const walletWithBasePath = defaultWallet?.derivePath(basePath)
44
- setWallet(walletWithBasePath)
45
- } catch (e) {
46
- console.error('Error setting proper wallet base path', e)
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
- } else {
50
- throw Error('WalletProvider requires a default HDWallet')
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,2 @@
1
+ export * from './useAccount'
2
+ export * from './useAccounts'
@@ -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
@@ -1,2 +1,3 @@
1
1
  export * from './components'
2
2
  export * from './contexts'
3
+ export * from './hooks'