@xyo-network/react-wallet 7.5.8 → 7.5.12
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/browser/components/WalletAccountDetails/WalletAccountDetails.d.ts.map +1 -1
- package/dist/browser/contexts/SeedPhrase/Provider.d.ts.map +1 -1
- package/dist/browser/index.mjs +656 -566
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +112 -46
- package/src/components/MaxAccounts/MaxAccountsTableRow.tsx +0 -75
- package/src/components/MaxAccounts/OutOfBoundsSnackBar.tsx +0 -41
- package/src/components/MaxAccounts/index.ts +0 -1
- package/src/components/SeedPhrase/DefaultSeedPhrase.stories.tsx +0 -27
- package/src/components/SeedPhrase/DefaultSeedPhrase.tsx +0 -52
- package/src/components/SeedPhrase/_shared/SeedPhraseIconButton.tsx +0 -53
- package/src/components/SeedPhrase/_shared/index.ts +0 -1
- package/src/components/SeedPhrase/dialog/SeedPhraseDialog.stories.tsx +0 -33
- package/src/components/SeedPhrase/dialog/SeedPhraseDialog.tsx +0 -84
- package/src/components/SeedPhrase/dialog/components/DialogActionButtons.tsx +0 -33
- package/src/components/SeedPhrase/dialog/components/OverwriteWarning.tsx +0 -28
- package/src/components/SeedPhrase/dialog/components/fields/NewPhraseTextField.tsx +0 -45
- package/src/components/SeedPhrase/dialog/components/fields/PhraseDialogActions.stories.tsx +0 -29
- package/src/components/SeedPhrase/dialog/components/fields/PhraseDialogActions.tsx +0 -38
- package/src/components/SeedPhrase/dialog/components/fields/SavedPhraseTextField.tsx +0 -113
- package/src/components/SeedPhrase/dialog/components/fields/index.ts +0 -4
- package/src/components/SeedPhrase/dialog/components/fields/validation-messages/InvalidPhrase.tsx +0 -14
- package/src/components/SeedPhrase/dialog/components/fields/validation-messages/PhraseHeaderBox.tsx +0 -50
- package/src/components/SeedPhrase/dialog/components/fields/validation-messages/colorParser.ts +0 -14
- package/src/components/SeedPhrase/dialog/components/fields/validation-messages/index.ts +0 -3
- package/src/components/SeedPhrase/dialog/components/index.ts +0 -3
- package/src/components/SeedPhrase/dialog/index.ts +0 -2
- package/src/components/SeedPhrase/index.ts +0 -4
- package/src/components/SeedPhrase/settings/SeedPhraseTableRow.stories.tsx +0 -31
- package/src/components/SeedPhrase/settings/SeedPhraseTableRow.tsx +0 -48
- package/src/components/SeedPhrase/settings/index.ts +0 -1
- package/src/components/WalletAccountDetails/WalletAccountDetails.stories.tsx +0 -27
- package/src/components/WalletAccountDetails/WalletAccountDetails.tsx +0 -37
- package/src/components/WalletAccountDetails/WalletAccountDetailsWithProvider.stories.tsx +0 -33
- package/src/components/WalletAccountDetails/index.ts +0 -1
- package/src/components/WalletAccountSelect/RenderedMenuItem.tsx +0 -52
- package/src/components/WalletAccountSelect/Select.tsx +0 -84
- package/src/components/WalletAccountSelect/SelectBar.tsx +0 -44
- package/src/components/WalletAccountSelect/WalletAccountSelect.stories.tsx +0 -45
- package/src/components/WalletAccountSelect/WalletAccountSelectBar.stories.tsx +0 -61
- package/src/components/WalletAccountSelect/WalletAccountSelectWithProvider.stories.tsx +0 -43
- package/src/components/WalletAccountSelect/WalletInfo.tsx +0 -121
- package/src/components/WalletAccountSelect/index.ts +0 -2
- package/src/components/WalletAccountSelect/stories/WalletProviderDecorator.tsx +0 -31
- package/src/components/WalletAccountSelect/stories/index.ts +0 -1
- package/src/components/index.ts +0 -4
- package/src/contexts/SeedPhrase/Context.ts +0 -6
- package/src/contexts/SeedPhrase/Provider.tsx +0 -103
- package/src/contexts/SeedPhrase/State.ts +0 -19
- package/src/contexts/SeedPhrase/index.ts +0 -4
- package/src/contexts/SeedPhrase/use.ts +0 -6
- package/src/contexts/Wallet/Context.ts +0 -6
- package/src/contexts/Wallet/Provider.tsx +0 -46
- package/src/contexts/Wallet/State.ts +0 -15
- package/src/contexts/Wallet/index.ts +0 -4
- package/src/contexts/Wallet/use.ts +0 -37
- package/src/contexts/index.ts +0 -2
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useAccount.ts +0 -68
- package/src/hooks/useWallet.ts +0 -46
- package/src/hooks/useWallets.ts +0 -22
- package/src/hooks/useWrapperAccount.ts +0 -36
- package/src/index.ts +0 -3
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '@storybook/react-vite'
|
|
2
|
-
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
3
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
-
import React, { useEffect, useState } from 'react'
|
|
5
|
-
|
|
6
|
-
import { WalletProvider } from '../../../contexts/index.ts'
|
|
7
|
-
import { useWallet } from '../../../hooks/index.ts'
|
|
8
|
-
|
|
9
|
-
export const WalletProviderDecorator: Decorator = (Story, context) => {
|
|
10
|
-
const [rootWallet] = useWallet({ mnemonic: DefaultSeedPhrase })
|
|
11
|
-
const [wallet, setWallet] = useState<WalletInstance>()
|
|
12
|
-
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
let timeout: ReturnType<typeof setTimeout>
|
|
15
|
-
if (rootWallet) {
|
|
16
|
-
timeout = setTimeout(() => {
|
|
17
|
-
setWallet(rootWallet)
|
|
18
|
-
}, 1000)
|
|
19
|
-
}
|
|
20
|
-
return () => {
|
|
21
|
-
if (timeout) {
|
|
22
|
-
clearTimeout(timeout)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}, [rootWallet])
|
|
26
|
-
return (
|
|
27
|
-
<WalletProvider rootWallet={wallet}>
|
|
28
|
-
<Story {...context} />
|
|
29
|
-
</WalletProvider>
|
|
30
|
-
)
|
|
31
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './WalletProviderDecorator.tsx'
|
package/src/components/index.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { generateMnemonic, validateMnemonic } from '@scure/bip39'
|
|
2
|
-
import { wordlist } from '@scure/bip39/wordlists/english.js'
|
|
3
|
-
import { useResetState } from '@xylabs/react-hooks'
|
|
4
|
-
import type { PropsWithChildren } from 'react'
|
|
5
|
-
import React, {
|
|
6
|
-
useCallback, useEffect, useMemo, useState,
|
|
7
|
-
} from 'react'
|
|
8
|
-
|
|
9
|
-
import { SeedPhraseContext } from './Context.ts'
|
|
10
|
-
import type { SeedPhraseContextState } from './State.ts'
|
|
11
|
-
|
|
12
|
-
/** @public */
|
|
13
|
-
export interface SeedPhraseProviderProps extends PropsWithChildren {
|
|
14
|
-
defaultPhrase?: string
|
|
15
|
-
handleChangeSeedPhrase?: (phrase: string) => void
|
|
16
|
-
open?: boolean
|
|
17
|
-
saveCallback?: () => void
|
|
18
|
-
seedPhrase?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const validate = (passedPhrase?: string) => {
|
|
22
|
-
if (!passedPhrase) {
|
|
23
|
-
return null
|
|
24
|
-
}
|
|
25
|
-
return validateMnemonic(passedPhrase, wordlist)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** @public */
|
|
29
|
-
export const SeedPhraseProvider: React.FC<SeedPhraseProviderProps> = ({
|
|
30
|
-
children,
|
|
31
|
-
defaultPhrase,
|
|
32
|
-
handleChangeSeedPhrase,
|
|
33
|
-
saveCallback,
|
|
34
|
-
seedPhrase,
|
|
35
|
-
open,
|
|
36
|
-
}) => {
|
|
37
|
-
const [phrase, setPhrase] = useResetState<string | undefined>(defaultPhrase)
|
|
38
|
-
const [overwriteWarning, setOverwriteWarning] = useState(false)
|
|
39
|
-
|
|
40
|
-
useEffect(() => {
|
|
41
|
-
if (seedPhrase || open) {
|
|
42
|
-
setPhrase?.(seedPhrase ?? '')
|
|
43
|
-
}
|
|
44
|
-
}, [seedPhrase, open, setPhrase])
|
|
45
|
-
|
|
46
|
-
const handleGenerate = useCallback(() => {
|
|
47
|
-
const mnemonic = generateMnemonic(wordlist, 256)
|
|
48
|
-
setPhrase?.(mnemonic)
|
|
49
|
-
setOverwriteWarning?.(false)
|
|
50
|
-
}, [setPhrase])
|
|
51
|
-
|
|
52
|
-
const handleCancelOverwrite = useCallback(() => {
|
|
53
|
-
setOverwriteWarning(false)
|
|
54
|
-
}, [setOverwriteWarning])
|
|
55
|
-
|
|
56
|
-
const handleClear = useCallback(() => {
|
|
57
|
-
setPhrase('')
|
|
58
|
-
setOverwriteWarning(false)
|
|
59
|
-
}, [setPhrase, setOverwriteWarning])
|
|
60
|
-
|
|
61
|
-
const handleSave = useCallback(() => {
|
|
62
|
-
if (!overwriteWarning && seedPhrase && seedPhrase !== phrase) {
|
|
63
|
-
setOverwriteWarning?.(true)
|
|
64
|
-
} else {
|
|
65
|
-
handleChangeSeedPhrase?.(phrase ?? '')
|
|
66
|
-
saveCallback?.()
|
|
67
|
-
}
|
|
68
|
-
}, [handleChangeSeedPhrase, overwriteWarning, phrase, saveCallback, seedPhrase])
|
|
69
|
-
|
|
70
|
-
useEffect(() => {
|
|
71
|
-
if (!open) {
|
|
72
|
-
handleCancelOverwrite()
|
|
73
|
-
}
|
|
74
|
-
}, [handleCancelOverwrite, open])
|
|
75
|
-
|
|
76
|
-
const validSeedPhrase = useMemo(() => validate?.(seedPhrase), [seedPhrase])
|
|
77
|
-
const validPhrase = useMemo(() => validate?.(phrase), [phrase])
|
|
78
|
-
|
|
79
|
-
const value: SeedPhraseContextState = useMemo(() => ({
|
|
80
|
-
handleCancelOverwrite,
|
|
81
|
-
handleChangeSeedPhrase,
|
|
82
|
-
handleClear,
|
|
83
|
-
handleGenerate,
|
|
84
|
-
handleSave,
|
|
85
|
-
overwriteWarning,
|
|
86
|
-
phrase,
|
|
87
|
-
provided: true,
|
|
88
|
-
seedPhrase,
|
|
89
|
-
setOverwriteWarning,
|
|
90
|
-
setPhrase,
|
|
91
|
-
validPhrase,
|
|
92
|
-
validSeedPhrase,
|
|
93
|
-
validate,
|
|
94
|
-
}), [handleCancelOverwrite, handleChangeSeedPhrase, handleClear, handleGenerate, handleSave, overwriteWarning, phrase, seedPhrase, setOverwriteWarning, setPhrase, validPhrase, validSeedPhrase])
|
|
95
|
-
|
|
96
|
-
return (
|
|
97
|
-
<SeedPhraseContext
|
|
98
|
-
value={value}
|
|
99
|
-
>
|
|
100
|
-
{children}
|
|
101
|
-
</SeedPhraseContext>
|
|
102
|
-
)
|
|
103
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { ContextExState } from '@xylabs/react-shared'
|
|
2
|
-
import type { Dispatch, SetStateAction } from 'react'
|
|
3
|
-
|
|
4
|
-
/** @public */
|
|
5
|
-
export type SeedPhraseContextState = ContextExState<{
|
|
6
|
-
handleCancelOverwrite?: () => void
|
|
7
|
-
handleChangeSeedPhrase?: (value: string) => void
|
|
8
|
-
handleClear?: () => void
|
|
9
|
-
handleGenerate?: () => void
|
|
10
|
-
handleSave?: () => void
|
|
11
|
-
overwriteWarning?: boolean
|
|
12
|
-
phrase?: string
|
|
13
|
-
seedPhrase?: string
|
|
14
|
-
setOverwriteWarning?: Dispatch<SetStateAction<boolean>>
|
|
15
|
-
setPhrase?: Dispatch<SetStateAction<string | undefined>>
|
|
16
|
-
validPhrase?: boolean | null
|
|
17
|
-
validSeedPhrase?: boolean | null
|
|
18
|
-
validate?: (passedPhrase?: string) => boolean | null
|
|
19
|
-
}>
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { useResetState } from '@xylabs/react-hooks'
|
|
2
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
3
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
-
import type { PropsWithChildren } from 'react'
|
|
5
|
-
import React, { useMemo } from 'react'
|
|
6
|
-
|
|
7
|
-
import { WalletContext } from './Context.ts'
|
|
8
|
-
import type { WalletContextState } from './State.ts'
|
|
9
|
-
|
|
10
|
-
/** @public */
|
|
11
|
-
export interface WalletProviderProps {
|
|
12
|
-
defaultActiveAccountIndex?: number
|
|
13
|
-
rootWallet?: WalletInstance | null
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** @public */
|
|
17
|
-
export const WalletProvider: React.FC<PropsWithChildren<WalletProviderProps>> = ({
|
|
18
|
-
children,
|
|
19
|
-
defaultActiveAccountIndex = 0,
|
|
20
|
-
rootWallet = null,
|
|
21
|
-
...props
|
|
22
|
-
}) => {
|
|
23
|
-
const [activeAccountIndex, setActiveAccountIndex] = useResetState(defaultActiveAccountIndex)
|
|
24
|
-
|
|
25
|
-
const [activeAccount = null] = usePromise(async () => await rootWallet?.derivePath(activeAccountIndex.toString()), [activeAccountIndex, rootWallet])
|
|
26
|
-
|
|
27
|
-
const value: WalletContextState = useMemo(() => ({
|
|
28
|
-
activeAccount,
|
|
29
|
-
activeAccountIndex,
|
|
30
|
-
provided: true,
|
|
31
|
-
rootWallet,
|
|
32
|
-
setActiveAccountIndex,
|
|
33
|
-
}), [activeAccount,
|
|
34
|
-
activeAccountIndex,
|
|
35
|
-
rootWallet,
|
|
36
|
-
setActiveAccountIndex])
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<WalletContext
|
|
40
|
-
value={value}
|
|
41
|
-
{...props}
|
|
42
|
-
>
|
|
43
|
-
{children}
|
|
44
|
-
</WalletContext>
|
|
45
|
-
)
|
|
46
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ContextExState } from '@xylabs/react-shared'
|
|
2
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
|
-
import type { Dispatch } from 'react'
|
|
4
|
-
|
|
5
|
-
/** @public */
|
|
6
|
-
export type WalletContextState = ContextExState<{
|
|
7
|
-
/** Currently selected account */
|
|
8
|
-
activeAccount?: WalletInstance | null
|
|
9
|
-
/** Currently selected index */
|
|
10
|
-
activeAccountIndex?: number
|
|
11
|
-
/** The root wallet being used */
|
|
12
|
-
rootWallet?: WalletInstance | null
|
|
13
|
-
/** Set currently selected index */
|
|
14
|
-
setActiveAccountIndex?: Dispatch<number>
|
|
15
|
-
}>
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import { useContextEx, useProvided } from '@xylabs/react-shared'
|
|
3
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
-
|
|
5
|
-
import { WalletContext } from './Context.ts'
|
|
6
|
-
|
|
7
|
-
/** @public */
|
|
8
|
-
export const useWalletContext = (required = true) => {
|
|
9
|
-
return useContextEx(WalletContext, 'Wallet', required)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/** @public */
|
|
13
|
-
export const useWalletProvided = () => {
|
|
14
|
-
return useProvided(WalletContext)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** @public */
|
|
18
|
-
export const useRootWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {
|
|
19
|
-
const { rootWallet } = useWalletContext(required)
|
|
20
|
-
return [rootWallet, undefined]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** @public */
|
|
24
|
-
export const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | null | undefined, Error | undefined] => {
|
|
25
|
-
const { rootWallet } = useWalletContext(required)
|
|
26
|
-
const [wallet] = usePromise(async () => (await rootWallet?.derivePath(index.toString())) ?? rootWallet, [rootWallet, index])
|
|
27
|
-
return [wallet, undefined]
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** @public */
|
|
31
|
-
export const useSelectedWalletAccount = (required = true): [WalletInstance | null | undefined, Error | undefined] => {
|
|
32
|
-
const { activeAccountIndex } = useWalletContext(required)
|
|
33
|
-
// we pass in 0 as default since we can not call the hook optionally,
|
|
34
|
-
// we resolve this false result by checking whether activeAccountIndex is defined before returning it
|
|
35
|
-
const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required)
|
|
36
|
-
return activeAccountIndex === undefined && account === null ? [null, undefined] : [account, undefined]
|
|
37
|
-
}
|
package/src/contexts/index.ts
DELETED
package/src/hooks/index.ts
DELETED
package/src/hooks/useAccount.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { AccountInstance } from '@xyo-network/account-model'
|
|
3
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
-
import { useState } from 'react'
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
useRootWallet, useWalletContext, useWalletProvided,
|
|
8
|
-
} from '../contexts/index.ts'
|
|
9
|
-
|
|
10
|
-
/** @public */
|
|
11
|
-
export interface AccountHookParams {
|
|
12
|
-
account?: AccountInstance
|
|
13
|
-
index?: number
|
|
14
|
-
required?: boolean
|
|
15
|
-
wallet?: WalletInstance
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** @public */
|
|
19
|
-
export const useAccount = ({
|
|
20
|
-
wallet, account, index, required = false,
|
|
21
|
-
}: AccountHookParams = {}): [
|
|
22
|
-
AccountInstance | null | undefined,
|
|
23
|
-
Error | undefined,
|
|
24
|
-
] => {
|
|
25
|
-
const walletContextProvided = useWalletProvided()
|
|
26
|
-
const [validationError, setValidationError] = useState<Error>()
|
|
27
|
-
if (wallet && account && !validationError) {
|
|
28
|
-
setValidationError(new Error('useAccount can not have both a wallet and an account in the parameters'))
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (index && account && !validationError) {
|
|
32
|
-
setValidationError(new Error('useAccount can not have both a index and an account in the parameters'))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const [error, setError] = useState<Error>()
|
|
36
|
-
const [rootWallet] = useRootWallet(!wallet && required)
|
|
37
|
-
const { activeAccountIndex } = useWalletContext(false)
|
|
38
|
-
const [activeAccount] = usePromise(async () => {
|
|
39
|
-
try {
|
|
40
|
-
if (!validationError) {
|
|
41
|
-
if (wallet) {
|
|
42
|
-
const path = `${index ?? 0}'\0`
|
|
43
|
-
return await wallet?.derivePath?.(path)
|
|
44
|
-
} else if (rootWallet) {
|
|
45
|
-
const path = `${index ?? activeAccountIndex ?? 0}'\/0`
|
|
46
|
-
return await rootWallet?.derivePath?.(path)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
} catch (ex) {
|
|
50
|
-
const error = ex as Error
|
|
51
|
-
console.error(error.message)
|
|
52
|
-
setError(error)
|
|
53
|
-
}
|
|
54
|
-
}, [index, wallet, rootWallet, activeAccountIndex, validationError])
|
|
55
|
-
if (validationError && !error) {
|
|
56
|
-
console.error(validationError.message)
|
|
57
|
-
setError(validationError)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return [
|
|
61
|
-
error
|
|
62
|
-
? undefined
|
|
63
|
-
: (account ?? activeAccount ?? walletContextProvided)
|
|
64
|
-
? null
|
|
65
|
-
: undefined,
|
|
66
|
-
error,
|
|
67
|
-
]
|
|
68
|
-
}
|
package/src/hooks/useWallet.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import { HDWallet } from '@xyo-network/wallet'
|
|
3
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
-
import { useState } from 'react'
|
|
5
|
-
|
|
6
|
-
import { useSelectedWalletAccount, useWalletContext } from '../contexts/index.ts'
|
|
7
|
-
|
|
8
|
-
/** @public */
|
|
9
|
-
export interface WalletHookParams {
|
|
10
|
-
mnemonic?: string
|
|
11
|
-
path?: string
|
|
12
|
-
required?: boolean
|
|
13
|
-
seed?: ArrayBufferLike | string
|
|
14
|
-
wallet?: WalletInstance | null
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** @public */
|
|
18
|
-
export const useWallet = ({
|
|
19
|
-
mnemonic, wallet, path, required = false, seed,
|
|
20
|
-
}: WalletHookParams = {}): [
|
|
21
|
-
WalletInstance | null | undefined,
|
|
22
|
-
Error | undefined,
|
|
23
|
-
] => {
|
|
24
|
-
const walletContextProvided = useWalletContext(false)
|
|
25
|
-
const [error, setError] = useState<Error>()
|
|
26
|
-
const [contextAccount] = useSelectedWalletAccount(!wallet && required)
|
|
27
|
-
const [activeAccount] = usePromise(async () => {
|
|
28
|
-
try {
|
|
29
|
-
const newAccount = await (() => {
|
|
30
|
-
if (wallet) {
|
|
31
|
-
return wallet
|
|
32
|
-
} else if (mnemonic) {
|
|
33
|
-
return HDWallet.fromPhrase(mnemonic)
|
|
34
|
-
} else if (seed) {
|
|
35
|
-
return HDWallet.fromSeed(seed)
|
|
36
|
-
}
|
|
37
|
-
return contextAccount
|
|
38
|
-
})()
|
|
39
|
-
|
|
40
|
-
return path ? newAccount?.derivePath?.(path) : (newAccount ?? wallet)
|
|
41
|
-
} catch (ex) {
|
|
42
|
-
setError(ex as Error)
|
|
43
|
-
}
|
|
44
|
-
}, [mnemonic, contextAccount, seed, path, wallet])
|
|
45
|
-
return [activeAccount ?? (walletContextProvided ? null : activeAccount), error]
|
|
46
|
-
}
|
package/src/hooks/useWallets.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
3
|
-
|
|
4
|
-
import { useWalletContext } from '../contexts/index.ts'
|
|
5
|
-
import { useWallet } from './useWallet.ts'
|
|
6
|
-
|
|
7
|
-
/** @public */
|
|
8
|
-
export interface WalletsHookParams {
|
|
9
|
-
paths: string[]
|
|
10
|
-
wallet?: WalletInstance | null
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** @public */
|
|
14
|
-
export const useWallets = ({ wallet, paths }: WalletsHookParams): [WalletInstance[] | null | undefined, Error | undefined] => {
|
|
15
|
-
const walletContextProvided = useWalletContext(false)
|
|
16
|
-
const [foundWallet] = useWallet({ wallet })
|
|
17
|
-
const [wallets, error] = usePromise(
|
|
18
|
-
async () => (foundWallet ? await Promise.all(paths.map(path => foundWallet.derivePath(path))) : undefined),
|
|
19
|
-
[foundWallet, paths],
|
|
20
|
-
)
|
|
21
|
-
return [wallets ?? (walletContextProvided ? null : wallets), error]
|
|
22
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { UsePromiseState } from '@xylabs/react-promise'
|
|
2
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
3
|
-
import type { AccountInstance } from '@xyo-network/account-model'
|
|
4
|
-
import { HDWallet } from '@xyo-network/wallet'
|
|
5
|
-
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
6
|
-
import { Mutex } from 'async-mutex'
|
|
7
|
-
|
|
8
|
-
let globalWrapperWallet: WalletInstance | undefined
|
|
9
|
-
const globalWrapperWalletMutex = new Mutex()
|
|
10
|
-
|
|
11
|
-
/** @public */
|
|
12
|
-
export const useWrapperAccount = (
|
|
13
|
-
account?: AccountInstance | null,
|
|
14
|
-
): [AccountInstance | null | undefined, Error | undefined, UsePromiseState | undefined] => {
|
|
15
|
-
return usePromise(async () => {
|
|
16
|
-
return await globalWrapperWalletMutex.runExclusive(async () => {
|
|
17
|
-
// if we are expecting to receive a wallet or did receive on, return the override account
|
|
18
|
-
if (account !== undefined) {
|
|
19
|
-
return account
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (globalWrapperWallet) {
|
|
23
|
-
return globalWrapperWallet
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
globalWrapperWallet = await HDWallet.random()
|
|
28
|
-
console.log(`Global Wrapper Wallet Creation Success: ${globalWrapperWallet.address}`)
|
|
29
|
-
} catch (ex) {
|
|
30
|
-
const error = ex as Error
|
|
31
|
-
console.error(`Global Wrapper Wallet Creation Failed: ${error.message}`)
|
|
32
|
-
}
|
|
33
|
-
return globalWrapperWallet
|
|
34
|
-
})
|
|
35
|
-
}, [account])
|
|
36
|
-
}
|
package/src/index.ts
DELETED