@xyo-network/react-wallet 2.64.0 → 2.64.3
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/MaxAccounts/MaxAccountsTableRow.js +41 -11
- package/dist/browser/components/MaxAccounts/MaxAccountsTableRow.js.map +1 -1
- package/dist/browser/components/MaxAccounts/OutOfBoundsSnackBar.js +3 -2
- package/dist/browser/components/MaxAccounts/OutOfBoundsSnackBar.js.map +1 -1
- package/dist/browser/components/MaxAccounts/index.js +88 -1
- package/dist/browser/components/MaxAccounts/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/DefaultSeedPhrase.js +3 -2
- package/dist/browser/components/SeedPhrase/DefaultSeedPhrase.js.map +1 -1
- package/dist/browser/components/SeedPhrase/_shared/SeedPhraseIconButton.js +3 -2
- package/dist/browser/components/SeedPhrase/_shared/SeedPhraseIconButton.js.map +1 -1
- package/dist/browser/components/SeedPhrase/_shared/index.js +45 -1
- package/dist/browser/components/SeedPhrase/_shared/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/SeedPhraseDialog.js +295 -16
- package/dist/browser/components/SeedPhrase/dialog/SeedPhraseDialog.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/DialogActionButtons.js +13 -3
- package/dist/browser/components/SeedPhrase/dialog/components/DialogActionButtons.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/OverwriteWarning.js +13 -3
- package/dist/browser/components/SeedPhrase/dialog/components/OverwriteWarning.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/NewPhraseTextField.js +74 -12
- package/dist/browser/components/SeedPhrase/dialog/components/fields/NewPhraseTextField.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/SavedPhraseTextField.js +60 -10
- package/dist/browser/components/SeedPhrase/dialog/components/fields/SavedPhraseTextField.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/index.js +125 -2
- package/dist/browser/components/SeedPhrase/dialog/components/fields/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/InvalidPhrase.js +3 -2
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/InvalidPhrase.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/PhraseHeaderBox.js +3 -2
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/PhraseHeaderBox.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/colorParser.js +2 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/colorParser.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/index.js +57 -3
- package/dist/browser/components/SeedPhrase/dialog/components/fields/validation-messages/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/components/index.js +164 -3
- package/dist/browser/components/SeedPhrase/dialog/components/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/dialog/index.js +315 -1
- package/dist/browser/components/SeedPhrase/dialog/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/index.js +373 -4
- package/dist/browser/components/SeedPhrase/index.js.map +1 -1
- package/dist/browser/components/SeedPhrase/settings/SeedPhraseTableRow.js +325 -12
- package/dist/browser/components/SeedPhrase/settings/SeedPhraseTableRow.js.map +1 -1
- package/dist/browser/components/SeedPhrase/settings/index.js +339 -1
- package/dist/browser/components/SeedPhrase/settings/index.js.map +1 -1
- package/dist/browser/components/WalletAccountDetails/WalletAccountDetails.js +62 -3
- package/dist/browser/components/WalletAccountDetails/WalletAccountDetails.js.map +1 -1
- package/dist/browser/components/WalletAccountDetails/index.js +83 -1
- package/dist/browser/components/WalletAccountDetails/index.js.map +1 -1
- package/dist/browser/components/WalletAccountSelect/Select.js +70 -5
- package/dist/browser/components/WalletAccountSelect/Select.js.map +1 -1
- package/dist/browser/components/WalletAccountSelect/SelectBar.js +150 -4
- package/dist/browser/components/WalletAccountSelect/SelectBar.js.map +1 -1
- package/dist/browser/components/WalletAccountSelect/WalletInfo.js +116 -10
- package/dist/browser/components/WalletAccountSelect/WalletInfo.js.map +1 -1
- package/dist/browser/components/WalletAccountSelect/index.js +175 -2
- package/dist/browser/components/WalletAccountSelect/index.js.map +1 -1
- package/dist/browser/components/index.js +694 -4
- package/dist/browser/components/index.js.map +1 -1
- package/dist/browser/contexts/SeedPhrase/Context.js +2 -1
- package/dist/browser/contexts/SeedPhrase/Context.js.map +1 -1
- package/dist/browser/contexts/SeedPhrase/Provider.js +9 -3
- package/dist/browser/contexts/SeedPhrase/Provider.js.map +1 -1
- package/dist/browser/contexts/SeedPhrase/index.js +91 -4
- package/dist/browser/contexts/SeedPhrase/index.js.map +1 -1
- package/dist/browser/contexts/SeedPhrase/use.js +8 -2
- package/dist/browser/contexts/SeedPhrase/use.js.map +1 -1
- package/dist/browser/contexts/Wallet/Context.js +2 -1
- package/dist/browser/contexts/Wallet/Context.js.map +1 -1
- package/dist/browser/contexts/Wallet/Provider.js +90 -7
- package/dist/browser/contexts/Wallet/Provider.js.map +1 -1
- package/dist/browser/contexts/Wallet/index.js +183 -5
- package/dist/browser/contexts/Wallet/index.js.map +1 -1
- package/dist/browser/contexts/Wallet/lib/WalletPath.js +4 -3
- package/dist/browser/contexts/Wallet/lib/WalletPath.js.map +1 -1
- package/dist/browser/contexts/Wallet/lib/index.js +21 -1
- package/dist/browser/contexts/Wallet/lib/index.js.map +1 -1
- package/dist/browser/contexts/Wallet/use.js +13 -7
- package/dist/browser/contexts/Wallet/use.js.map +1 -1
- package/dist/browser/contexts/Wallet/usePromise.js +2 -1
- package/dist/browser/contexts/Wallet/usePromise.js.map +1 -1
- package/dist/browser/contexts/index.js +273 -2
- package/dist/browser/contexts/index.js.map +1 -1
- package/dist/browser/hooks/index.js +147 -4
- package/dist/browser/hooks/index.js.map +1 -1
- package/dist/browser/hooks/useAccount.js +24 -4
- package/dist/browser/hooks/useAccount.js.map +1 -1
- package/dist/browser/hooks/useWallet.js +38 -4
- package/dist/browser/hooks/useWallet.js.map +1 -1
- package/dist/browser/hooks/useWallets.js +69 -4
- package/dist/browser/hooks/useWallets.js.map +1 -1
- package/dist/browser/hooks/useWrapperAccount.js +4 -3
- package/dist/browser/hooks/useWrapperAccount.js.map +1 -1
- package/dist/browser/index.js +890 -3
- package/dist/browser/index.js.map +1 -1
- package/dist/docs.json +147 -147
- package/package.json +22 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/contexts/index.ts"],"sourcesContent":["export * from './SeedPhrase'\nexport * from './Wallet'\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/contexts/SeedPhrase/Context.ts","../../../src/contexts/SeedPhrase/Provider.tsx","../../../src/contexts/SeedPhrase/use.tsx","../../../src/contexts/Wallet/Context.ts","../../../src/contexts/Wallet/lib/WalletPath.ts","../../../src/contexts/Wallet/Provider.tsx","../../../src/contexts/Wallet/usePromise.ts","../../../src/contexts/Wallet/use.ts"],"sourcesContent":["import { createContextEx } from '@xyo-network/react-shared'\n\nimport { SeedPhraseContextState } from './State'\n\nexport const SeedPhraseContext = createContextEx<SeedPhraseContextState>()\n","import { WithChildren } from '@xylabs/react-shared'\nimport { generateMnemonic, validateMnemonic, wordlists } from '@xyo-network/bip39'\nimport { useEffect, useMemo, useState } from 'react'\n\nimport { SeedPhraseContext } from './Context'\n\nexport interface SeedPhraseProviderProps extends WithChildren {\n defaultPhrase?: string\n handleChangeSeedPhrase?: (phrase: string) => void\n open?: boolean\n saveCallback?: () => void\n seedPhrase?: string\n}\n\nexport const SeedPhraseProvider: React.FC<SeedPhraseProviderProps> = ({\n children,\n defaultPhrase,\n handleChangeSeedPhrase,\n saveCallback,\n seedPhrase,\n open,\n}) => {\n const [phrase, setPhrase] = useState<string | undefined>()\n const [overwriteWarning, setOverwriteWarning] = useState(false)\n\n useEffect(() => {\n setPhrase(defaultPhrase)\n }, [defaultPhrase])\n\n useEffect(() => {\n if (!open) {\n handleCancelOverwrite()\n }\n }, [open])\n\n useEffect(() => {\n if (seedPhrase || open) {\n setPhrase?.(seedPhrase ?? '')\n }\n }, [seedPhrase, open, setPhrase])\n\n const handleGenerate = () => {\n const mnemonic = generateMnemonic(wordlists.english, 256)\n setPhrase?.(mnemonic)\n setOverwriteWarning?.(false)\n }\n\n const handleCancelOverwrite = () => {\n setOverwriteWarning?.(false)\n }\n\n const handleClear = () => {\n setPhrase?.('')\n setOverwriteWarning?.(false)\n }\n\n const handleSave = () => {\n if (!overwriteWarning && seedPhrase && seedPhrase !== phrase) {\n setOverwriteWarning?.(true)\n } else {\n handleChangeSeedPhrase?.(phrase ?? '')\n saveCallback?.()\n }\n }\n\n const validate = (passedPhrase?: string) => {\n if (!passedPhrase) {\n return null\n }\n return validateMnemonic(passedPhrase, wordlists.english)\n }\n\n const validSeedPhrase = useMemo(() => validate?.(seedPhrase), [seedPhrase])\n const validPhrase = useMemo(() => validate?.(phrase), [phrase])\n\n return (\n <SeedPhraseContext.Provider\n value={{\n handleCancelOverwrite,\n handleChangeSeedPhrase,\n handleClear,\n handleGenerate,\n handleSave,\n overwriteWarning,\n phrase,\n provided: true,\n seedPhrase,\n setOverwriteWarning,\n setPhrase,\n validPhrase,\n validSeedPhrase,\n validate,\n }}\n >\n {children}\n </SeedPhraseContext.Provider>\n )\n}\n","import { useContextEx } from '@xyo-network/react-shared'\n\nimport { SeedPhraseContext } from './Context'\n\nexport const useSeedPhrase = () => useContextEx(SeedPhraseContext, 'SeedPhrase', true)\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { WalletContextState } from './State'\n\nexport const WalletContext = createContextEx<WalletContextState>()\n","export const Bip44 = {\n base: 'm',\n coin_type: {\n /* https://github.com/satoshilabs/slips/blob/master/slip-0044.md */\n bitcoin: \"0'\",\n bitcoinTestnet: \"1'\",\n ether: \"60'\",\n etherClassic: \"61'\",\n },\n purpose: \"44'\",\n}\n\nexport const WalletRootPath = `${Bip44.base}/${Bip44.purpose}/${Bip44.coin_type.ether}`\n\nexport const ethereumAccountPath = (index: number, hardened = true) => {\n return `${WalletRootPath}/${index}${hardened ? \"'\" : ''}/0`\n}\n","import { WithChildren } from '@xylabs/react-shared'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useEffect, useState } from 'react'\n\nimport { WalletContext } from './Context'\nimport { WalletRootPath } from './lib'\nimport { usePromise } from './usePromise'\n\nexport interface WalletProviderProps {\n basePath?: string\n defaultActiveAccountIndex?: number\n rootWallet?: WalletInstance | null\n}\n\nexport const WalletProvider: React.FC<WithChildren<WalletProviderProps>> = ({\n basePath = WalletRootPath,\n children,\n defaultActiveAccountIndex = 0,\n rootWallet = null,\n ...props\n}) => {\n const [activeAccountIndex, setActiveAccountIndex] = useState(defaultActiveAccountIndex)\n\n useEffect(() => {\n if (defaultActiveAccountIndex !== undefined) {\n setActiveAccountIndex(defaultActiveAccountIndex)\n }\n }, [defaultActiveAccountIndex])\n\n const [coinTypeWallet = null] = usePromise(async () => {\n // ensure the wallet has the proper base\n if (rootWallet) {\n if (!rootWallet?.path.includes(basePath)) {\n try {\n return await rootWallet?.derivePath(basePath)\n } catch (e) {\n console.error('Error setting proper wallet base path', e)\n }\n }\n } else {\n return undefined\n }\n }, [basePath, rootWallet])\n\n const [activeAccount = null] = usePromise(\n async () => await coinTypeWallet?.derivePath(activeAccountIndex.toString()),\n [coinTypeWallet, activeAccountIndex],\n )\n\n return (\n <WalletContext.Provider\n value={{\n activeAccount,\n activeAccountIndex,\n basePath,\n coinTypeWallet,\n provided: true,\n rootWallet,\n setActiveAccountIndex,\n }}\n {...props}\n >\n {children}\n </WalletContext.Provider>\n )\n}\n","// Inspired from https://github.com/bsonntag/react-use-promise\n\nimport { Mutex } from 'async-mutex'\nimport { DependencyList, useEffect, useMemo, useState } from 'react'\n\nexport enum State {\n pending = 'pending',\n rejected = 'rejected',\n resolved = 'resolved',\n}\n\n/**\n * usePromise -\n */\nexport const usePromise = <TResult>(\n promise: () => Promise<TResult | undefined>,\n dependencies: DependencyList,\n debug: string | undefined = undefined,\n): [TResult | undefined, Error | undefined, State | undefined] => {\n const [result, setResult] = useState<TResult>()\n const [error, setError] = useState<Error>()\n const [state, setState] = useState<State>(State.pending)\n const mutex = useMemo(() => {\n return new Mutex()\n }, [])\n\n if (debug) console.log(`usePromise [${debug}]: started [${typeof promise}]`)\n\n const promiseMemo: Promise<TResult | undefined> | undefined = useMemo(() => {\n try {\n if (debug) console.log(`usePromise [${debug}]: re-memo [${typeof promise}]`)\n setState(State.pending)\n return promise?.()\n } catch (e) {\n if (debug) console.log(`usePromise [${debug}]: useMemo rejection [${typeof promise}]`)\n setResult(undefined)\n setError(e as Error)\n setState(State.rejected)\n }\n }, dependencies)\n\n if (debug) console.log(`usePromise [${debug}] Main Function`)\n\n useEffect(() => {\n if (debug) console.log(`usePromise [${debug}] useEffect`)\n mutex\n ?.acquire()\n .then(() => {\n promiseMemo\n ?.then((payload) => {\n if (debug) console.log(`usePromise [${debug}] then`)\n setResult(payload)\n setError(undefined)\n setState(State.resolved)\n mutex?.release()\n })\n .catch((e) => {\n const error = e as Error\n console.error(`usePromise: ${error.message}`)\n setResult(undefined)\n setError(error)\n setState(State.rejected)\n mutex?.release()\n })\n })\n .catch((e) => {\n const error = e as Error\n console.error(`usePromise: ${error.message}`)\n setResult(undefined)\n setError(error)\n setState(State.rejected)\n mutex?.release()\n })\n return () => {\n if (debug) console.log(`usePromise [${debug}] useEffect callback`)\n }\n }, [...dependencies, promiseMemo])\n if (debug) console.log(`usePromise [${debug}] returning ${JSON.stringify([result, error, state], null, 2)}`)\n return [result, error, state]\n}\n","import { usePromise } from '@xylabs/react-promise'\nimport { useContextEx, useProvided } from '@xyo-network/react-shared'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { WalletContext } from './Context'\n\nexport const useWalletContext = (required = true) => {\n return useContextEx(WalletContext, 'Wallet', required)\n}\n\nexport const useWalletProvided = () => {\n return useProvided(WalletContext)\n}\n\nexport const useCoinTypeWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { coinTypeWallet } = useWalletContext(required)\n return [coinTypeWallet, undefined]\n}\n\nexport const useRootWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { rootWallet } = useWalletContext(required)\n return [rootWallet, undefined]\n}\n\nexport const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const [coinTypeWallet] = useCoinTypeWallet(required)\n const [wallet] = usePromise(async () => (await coinTypeWallet?.derivePath(index.toString())) ?? coinTypeWallet, [coinTypeWallet, index])\n return [wallet, undefined]\n}\n\nexport const useSelectedWalletAccount = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { activeAccountIndex } = useWalletContext(required)\n //we pass in 0 as default since we can not call the hook optionally,\n //we resolve this false result by checking whether activeAccountIndex is defined before returning it\n const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required)\n if (activeAccountIndex === undefined && account === null) {\n return [null, undefined]\n } else {\n return [account, undefined]\n }\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAIzB,IAAM,oBAAoB,gBAAwC;;;ACHzE,SAAS,kBAAkB,kBAAkB,iBAAiB;AAC9D,SAAS,WAAW,SAAS,gBAAgB;AA0EzC;AA9DG,IAAM,qBAAwD,CAAC;AAAA,EACpE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAAM;AACJ,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA6B;AACzD,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,SAAS,KAAK;AAE9D,YAAU,MAAM;AACd,cAAU,aAAa;AAAA,EACzB,GAAG,CAAC,aAAa,CAAC;AAElB,YAAU,MAAM;AACd,QAAI,CAAC,MAAM;AACT,4BAAsB;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,IAAI,CAAC;AAET,YAAU,MAAM;AACd,QAAI,cAAc,MAAM;AACtB,kBAAY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;AACnD,QAAM,CAAC,MAAM,IAAIC,YAAW,YAAa,MAAM,gBAAgB,WAAW,MAAM,SAAS,CAAC,KAAM,gBAAgB,CAAC,gBAAgB,KAAK,CAAC;AACvI,SAAO,CAAC,QAAQ,MAAS;AAC3B;AAEO,IAAM,2BAA2B,CAAC,WAAW,SAAiE;AACnH,QAAM,EAAE,mBAAmB,IAAI,iBAAiB,QAAQ;AAGxD,QAAM,CAAC,OAAO,IAAI,4BAA4B,sBAAsB,GAAG,QAAQ;AAC/E,MAAI,uBAAuB,UAAa,YAAY,MAAM;AACxD,WAAO,CAAC,MAAM,MAAS;AAAA,EACzB,OAAO;AACL,WAAO,CAAC,SAAS,MAAS;AAAA,EAC5B;AACF;","names":["createContextEx","useEffect","useState","useEffect","useMemo","useState","useState","useMemo","useEffect","error","jsx","useState","useEffect","usePromise","useContextEx","useContextEx","usePromise"]}
|
|
@@ -1,5 +1,148 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/hooks/useAccount.ts
|
|
2
|
+
import { usePromise as usePromise2 } from "@xylabs/react-promise";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
// src/contexts/Wallet/Context.ts
|
|
6
|
+
import { createContextEx } from "@xyo-network/react-shared";
|
|
7
|
+
var WalletContext = createContextEx();
|
|
8
|
+
|
|
9
|
+
// src/contexts/Wallet/use.ts
|
|
10
|
+
import { usePromise } from "@xylabs/react-promise";
|
|
11
|
+
import { useContextEx, useProvided } from "@xyo-network/react-shared";
|
|
12
|
+
var useWalletContext = (required = true) => {
|
|
13
|
+
return useContextEx(WalletContext, "Wallet", required);
|
|
14
|
+
};
|
|
15
|
+
var useWalletProvided = () => {
|
|
16
|
+
return useProvided(WalletContext);
|
|
17
|
+
};
|
|
18
|
+
var useCoinTypeWallet = (required = true) => {
|
|
19
|
+
const { coinTypeWallet } = useWalletContext(required);
|
|
20
|
+
return [coinTypeWallet, void 0];
|
|
21
|
+
};
|
|
22
|
+
var useIndexedWalletFromContext = (index, required = true) => {
|
|
23
|
+
const [coinTypeWallet] = useCoinTypeWallet(required);
|
|
24
|
+
const [wallet] = usePromise(async () => await coinTypeWallet?.derivePath(index.toString()) ?? coinTypeWallet, [coinTypeWallet, index]);
|
|
25
|
+
return [wallet, void 0];
|
|
26
|
+
};
|
|
27
|
+
var useSelectedWalletAccount = (required = true) => {
|
|
28
|
+
const { activeAccountIndex } = useWalletContext(required);
|
|
29
|
+
const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required);
|
|
30
|
+
if (activeAccountIndex === void 0 && account === null) {
|
|
31
|
+
return [null, void 0];
|
|
32
|
+
} else {
|
|
33
|
+
return [account, void 0];
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/hooks/useAccount.ts
|
|
38
|
+
var useAccount = ({ wallet, account, index, required = false } = {}) => {
|
|
39
|
+
const walletContextProvided = useWalletProvided();
|
|
40
|
+
const [validationError, setValidationError] = useState();
|
|
41
|
+
if (wallet && account && !validationError) {
|
|
42
|
+
setValidationError(Error("useAccount can not have both a wallet and an account in the parameters"));
|
|
43
|
+
}
|
|
44
|
+
if (index && account && !validationError) {
|
|
45
|
+
setValidationError(Error("useAccount can not have both a index and an account in the parameters"));
|
|
46
|
+
}
|
|
47
|
+
const [error, setError] = useState();
|
|
48
|
+
const [coinTypeWallet] = useCoinTypeWallet(!wallet && required);
|
|
49
|
+
const { activeAccountIndex } = useWalletContext(false);
|
|
50
|
+
const [activeAccount] = usePromise2(async () => {
|
|
51
|
+
try {
|
|
52
|
+
if (!validationError) {
|
|
53
|
+
if (wallet) {
|
|
54
|
+
return await wallet?.derivePath?.(`${index ?? 0}'\0`);
|
|
55
|
+
} else if (coinTypeWallet) {
|
|
56
|
+
return await coinTypeWallet?.derivePath?.(`${index ?? activeAccountIndex ?? 0}'\0`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch (ex) {
|
|
60
|
+
const error2 = ex;
|
|
61
|
+
console.error(error2.message);
|
|
62
|
+
setError(error2);
|
|
63
|
+
}
|
|
64
|
+
}, [index, wallet, coinTypeWallet, activeAccountIndex, validationError]);
|
|
65
|
+
if (validationError && !error) {
|
|
66
|
+
console.error(validationError.message);
|
|
67
|
+
setError(validationError);
|
|
68
|
+
}
|
|
69
|
+
return [error ? void 0 : account ?? activeAccount ?? walletContextProvided ? null : void 0, error];
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/hooks/useWallet.ts
|
|
73
|
+
import { usePromise as usePromise3 } from "@xylabs/react-promise";
|
|
74
|
+
import { HDWallet } from "@xyo-network/account";
|
|
75
|
+
import { useState as useState2 } from "react";
|
|
76
|
+
var useWallet = ({ mnemonic, wallet, path, required = false, seed } = {}) => {
|
|
77
|
+
const walletContextProvided = useWalletProvided();
|
|
78
|
+
const [error, setError] = useState2();
|
|
79
|
+
const [contextAccount] = useSelectedWalletAccount(!wallet && required);
|
|
80
|
+
const [activeAccount] = usePromise3(async () => {
|
|
81
|
+
try {
|
|
82
|
+
const newAccount = await (() => {
|
|
83
|
+
if (wallet) {
|
|
84
|
+
return wallet;
|
|
85
|
+
} else if (mnemonic) {
|
|
86
|
+
return HDWallet.fromMnemonic(mnemonic);
|
|
87
|
+
} else if (seed) {
|
|
88
|
+
return HDWallet.fromSeed(seed);
|
|
89
|
+
}
|
|
90
|
+
return contextAccount;
|
|
91
|
+
})();
|
|
92
|
+
if (path) {
|
|
93
|
+
return newAccount?.derivePath?.(path);
|
|
94
|
+
} else {
|
|
95
|
+
return newAccount ?? wallet;
|
|
96
|
+
}
|
|
97
|
+
} catch (ex) {
|
|
98
|
+
setError(ex);
|
|
99
|
+
}
|
|
100
|
+
}, [mnemonic, contextAccount, seed, path, wallet]);
|
|
101
|
+
return [activeAccount ?? (walletContextProvided ? null : activeAccount), error];
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/hooks/useWallets.ts
|
|
105
|
+
import { usePromise as usePromise4 } from "@xylabs/react-promise";
|
|
106
|
+
var useWallets = ({ wallet, paths }) => {
|
|
107
|
+
const walletContextProvided = useWalletProvided();
|
|
108
|
+
const [foundWallet] = useWallet({ wallet });
|
|
109
|
+
const [wallets, error] = usePromise4(
|
|
110
|
+
async () => foundWallet ? await Promise.all(paths.map((path) => foundWallet.derivePath(path))) : void 0,
|
|
111
|
+
[foundWallet, paths]
|
|
112
|
+
);
|
|
113
|
+
return [wallets ?? (walletContextProvided ? null : wallets), error];
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// src/hooks/useWrapperAccount.ts
|
|
117
|
+
import { usePromise as usePromise5 } from "@xylabs/react-promise";
|
|
118
|
+
import { HDWallet as HDWallet2 } from "@xyo-network/account";
|
|
119
|
+
import { Mutex } from "async-mutex";
|
|
120
|
+
var globalWrapperWallet = void 0;
|
|
121
|
+
var globalWrapperWalletMutex = new Mutex();
|
|
122
|
+
var useWrapperAccount = (account) => {
|
|
123
|
+
return usePromise5(async () => {
|
|
124
|
+
return await globalWrapperWalletMutex.runExclusive(async () => {
|
|
125
|
+
if (account !== void 0) {
|
|
126
|
+
return account;
|
|
127
|
+
}
|
|
128
|
+
if (globalWrapperWallet) {
|
|
129
|
+
return globalWrapperWallet;
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
globalWrapperWallet = await HDWallet2.random();
|
|
133
|
+
console.log(`Global Wrapper Wallet Creation Success: ${globalWrapperWallet.address}`);
|
|
134
|
+
} catch (ex) {
|
|
135
|
+
const error = ex;
|
|
136
|
+
console.error(`Global Wrapper Wallet Creation Failed: ${error.message}`);
|
|
137
|
+
}
|
|
138
|
+
return globalWrapperWallet;
|
|
139
|
+
});
|
|
140
|
+
}, [account]);
|
|
141
|
+
};
|
|
142
|
+
export {
|
|
143
|
+
useAccount,
|
|
144
|
+
useWallet,
|
|
145
|
+
useWallets,
|
|
146
|
+
useWrapperAccount
|
|
147
|
+
};
|
|
5
148
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/index.ts"],"sourcesContent":["export * from './useAccount'\nexport * from './useWallet'\nexport * from './useWallets'\nexport * from './useWrapperAccount'\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/useAccount.ts","../../../src/contexts/Wallet/Context.ts","../../../src/contexts/Wallet/use.ts","../../../src/hooks/useWallet.ts","../../../src/hooks/useWallets.ts","../../../src/hooks/useWrapperAccount.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { AccountInstance } from '@xyo-network/account-model'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useCoinTypeWallet, useWalletContext, useWalletProvided } from '../contexts'\n\nexport interface AccountHookParams {\n account?: AccountInstance\n index?: number\n required?: boolean\n wallet?: WalletInstance\n}\n\nexport const useAccount = ({ wallet, account, index, required = false }: AccountHookParams = {}): [\n AccountInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [validationError, setValidationError] = useState<Error>()\n if (wallet && account && !validationError) {\n setValidationError(Error('useAccount can not have both a wallet and an account in the parameters'))\n }\n\n if (index && account && !validationError) {\n setValidationError(Error('useAccount can not have both a index and an account in the parameters'))\n }\n\n const [error, setError] = useState<Error>()\n const [coinTypeWallet] = useCoinTypeWallet(!wallet && required)\n const { activeAccountIndex } = useWalletContext(false)\n const [activeAccount] = usePromise(async () => {\n try {\n if (!validationError) {\n if (wallet) {\n return await wallet?.derivePath?.(`${index ?? 0}'\\0`)\n } else if (coinTypeWallet) {\n return await coinTypeWallet?.derivePath?.(`${index ?? activeAccountIndex ?? 0}'\\0`)\n }\n }\n } catch (ex) {\n const error = ex as Error\n console.error(error.message)\n setError(error)\n }\n }, [index, wallet, coinTypeWallet, activeAccountIndex, validationError])\n if (validationError && !error) {\n console.error(validationError.message)\n setError(validationError)\n }\n\n return [error ? undefined : account ?? activeAccount ?? walletContextProvided ? null : undefined, error]\n}\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { WalletContextState } from './State'\n\nexport const WalletContext = createContextEx<WalletContextState>()\n","import { usePromise } from '@xylabs/react-promise'\nimport { useContextEx, useProvided } from '@xyo-network/react-shared'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { WalletContext } from './Context'\n\nexport const useWalletContext = (required = true) => {\n return useContextEx(WalletContext, 'Wallet', required)\n}\n\nexport const useWalletProvided = () => {\n return useProvided(WalletContext)\n}\n\nexport const useCoinTypeWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { coinTypeWallet } = useWalletContext(required)\n return [coinTypeWallet, undefined]\n}\n\nexport const useRootWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { rootWallet } = useWalletContext(required)\n return [rootWallet, undefined]\n}\n\nexport const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const [coinTypeWallet] = useCoinTypeWallet(required)\n const [wallet] = usePromise(async () => (await coinTypeWallet?.derivePath(index.toString())) ?? coinTypeWallet, [coinTypeWallet, index])\n return [wallet, undefined]\n}\n\nexport const useSelectedWalletAccount = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { activeAccountIndex } = useWalletContext(required)\n //we pass in 0 as default since we can not call the hook optionally,\n //we resolve this false result by checking whether activeAccountIndex is defined before returning it\n const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required)\n if (activeAccountIndex === undefined && account === null) {\n return [null, undefined]\n } else {\n return [account, undefined]\n }\n}\n","import { usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { DataLike } from '@xyo-network/core'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useSelectedWalletAccount, useWalletProvided } from '../contexts'\n\nexport interface WalletHookParams {\n mnemonic?: string\n path?: string\n required?: boolean\n seed?: DataLike\n wallet?: WalletInstance | null\n}\n\nexport const useWallet = ({ mnemonic, wallet, path, required = false, seed }: WalletHookParams = {}): [\n WalletInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [error, setError] = useState<Error>()\n const [contextAccount] = useSelectedWalletAccount(!wallet && required)\n const [activeAccount] = usePromise(async () => {\n try {\n const newAccount = await (() => {\n if (wallet) {\n return wallet\n } else if (mnemonic) {\n return HDWallet.fromMnemonic(mnemonic as string)\n } else if (seed) {\n return HDWallet.fromSeed(seed)\n }\n return contextAccount\n })()\n\n if (path) {\n return newAccount?.derivePath?.(path)\n } else {\n return newAccount ?? wallet\n }\n } catch (ex) {\n setError(ex as Error)\n }\n }, [mnemonic, contextAccount, seed, path, wallet])\n return [activeAccount ?? (walletContextProvided ? null : activeAccount), error]\n}\n","import { usePromise } from '@xylabs/react-promise'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { useWalletProvided } from '../contexts'\nimport { useWallet } from './useWallet'\n\nexport interface WalletsHookParams {\n paths: string[]\n wallet?: WalletInstance | null\n}\n\nexport const useWallets = ({ wallet, paths }: WalletsHookParams): [WalletInstance[] | null | undefined, Error | undefined] => {\n const walletContextProvided = useWalletProvided()\n const [foundWallet] = useWallet({ wallet })\n const [wallets, error] = usePromise(\n async () => (foundWallet ? await Promise.all(paths.map((path) => foundWallet.derivePath(path))) : undefined),\n [foundWallet, paths],\n )\n return [wallets ?? (walletContextProvided ? null : wallets), error]\n}\n","import { State as PromiseState, usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { AccountInstance } from '@xyo-network/account-model'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { Mutex } from 'async-mutex'\n\nlet globalWrapperWallet: WalletInstance | undefined = undefined\nconst globalWrapperWalletMutex = new Mutex()\n\nexport const useWrapperAccount = (\n account?: AccountInstance | null,\n): [AccountInstance | null | undefined, Error | undefined, PromiseState | undefined] => {\n return usePromise(async () => {\n return await globalWrapperWalletMutex.runExclusive(async () => {\n //if we are expecting to receive a wallet or did receive on, return the override account\n if (account !== undefined) {\n return account\n }\n\n if (globalWrapperWallet) {\n return globalWrapperWallet\n }\n\n try {\n globalWrapperWallet = await HDWallet.random()\n console.log(`Global Wrapper Wallet Creation Success: ${globalWrapperWallet.address}`)\n } catch (ex) {\n const error = ex as Error\n console.error(`Global Wrapper Wallet Creation Failed: ${error.message}`)\n }\n return globalWrapperWallet\n })\n }, [account])\n}\n"],"mappings":";AAAA,SAAS,cAAAA,mBAAkB;AAG3B,SAAS,gBAAgB;;;ACHzB,SAAS,uBAAuB;AAIzB,IAAM,gBAAgB,gBAAoC;;;ACJjE,SAAS,kBAAkB;AAC3B,SAAS,cAAc,mBAAmB;AAKnC,IAAM,mBAAmB,CAAC,WAAW,SAAS;AACnD,SAAO,aAAa,eAAe,UAAU,QAAQ;AACvD;AAEO,IAAM,oBAAoB,MAAM;AACrC,SAAO,YAAY,aAAa;AAClC;AAEO,IAAM,oBAAoB,CAAC,WAAW,SAAiE;AAC5G,QAAM,EAAE,eAAe,IAAI,iBAAiB,QAAQ;AACpD,SAAO,CAAC,gBAAgB,MAAS;AACnC;AAOO,IAAM,8BAA8B,CAAC,OAAe,WAAW,SAAiE;AACrI,QAAM,CAAC,cAAc,IAAI,kBAAkB,QAAQ;AACnD,QAAM,CAAC,MAAM,IAAI,WAAW,YAAa,MAAM,gBAAgB,WAAW,MAAM,SAAS,CAAC,KAAM,gBAAgB,CAAC,gBAAgB,KAAK,CAAC;AACvI,SAAO,CAAC,QAAQ,MAAS;AAC3B;AAEO,IAAM,2BAA2B,CAAC,WAAW,SAAiE;AACnH,QAAM,EAAE,mBAAmB,IAAI,iBAAiB,QAAQ;AAGxD,QAAM,CAAC,OAAO,IAAI,4BAA4B,sBAAsB,GAAG,QAAQ;AAC/E,MAAI,uBAAuB,UAAa,YAAY,MAAM;AACxD,WAAO,CAAC,MAAM,MAAS;AAAA,EACzB,OAAO;AACL,WAA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Aa;AAEtB,IAAI,sBAAkD;AACtD,IAAM,2BAA2B,IAAI,MAAM;AAEpC,IAAM,oBAAoB,CAC/B,YACsF;AACtF,SAAOD,YAAW,YAAY;AAC5B,WAAO,MAAM,yBAAyB,aAAa,YAAY;AAE7D,UAAI,YAAY,QAAW;AACzB,eAAO;AAAA,MACT;AAEA,UAAI,qBAAqB;AACvB,eAAO;AAAA,MACT;AAEA,UAAI;AACF,8BAAsB,MAAMC,UAAS,OAAO;AAC5C,gBAAQ,IAAI,2CAA2C,oBAAoB,OAAO,EAAE;AAAA,MACtF,SAAS,IAAI;AACX,cAAM,QAAQ;AACd,gBAAQ,MAAM,0CAA0C,MAAM,OAAO,EAAE;AAAA,MACzE;AACA,aAAO;AAAA,IACT,CAAC;AAAA,EACH,GAAG,CAAC,OAAO,CAAC;AACd;","names":["usePromise","usePromise","error","usePromise","useState","useState","usePromise","usePromise","usePromise","usePromise","HDWallet"]}
|
|
@@ -1,7 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
// src/hooks/useAccount.ts
|
|
2
|
+
import { usePromise as usePromise2 } from "@xylabs/react-promise";
|
|
2
3
|
import { useState } from "react";
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
// src/contexts/Wallet/Context.ts
|
|
6
|
+
import { createContextEx } from "@xyo-network/react-shared";
|
|
7
|
+
var WalletContext = createContextEx();
|
|
8
|
+
|
|
9
|
+
// src/contexts/Wallet/use.ts
|
|
10
|
+
import { usePromise } from "@xylabs/react-promise";
|
|
11
|
+
import { useContextEx, useProvided } from "@xyo-network/react-shared";
|
|
12
|
+
var useWalletContext = (required = true) => {
|
|
13
|
+
return useContextEx(WalletContext, "Wallet", required);
|
|
14
|
+
};
|
|
15
|
+
var useWalletProvided = () => {
|
|
16
|
+
return useProvided(WalletContext);
|
|
17
|
+
};
|
|
18
|
+
var useCoinTypeWallet = (required = true) => {
|
|
19
|
+
const { coinTypeWallet } = useWalletContext(required);
|
|
20
|
+
return [coinTypeWallet, void 0];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/hooks/useAccount.ts
|
|
24
|
+
var useAccount = ({ wallet, account, index, required = false } = {}) => {
|
|
5
25
|
const walletContextProvided = useWalletProvided();
|
|
6
26
|
const [validationError, setValidationError] = useState();
|
|
7
27
|
if (wallet && account && !validationError) {
|
|
@@ -13,7 +33,7 @@ const useAccount = ({ wallet, account, index, required = false } = {}) => {
|
|
|
13
33
|
const [error, setError] = useState();
|
|
14
34
|
const [coinTypeWallet] = useCoinTypeWallet(!wallet && required);
|
|
15
35
|
const { activeAccountIndex } = useWalletContext(false);
|
|
16
|
-
const [activeAccount] =
|
|
36
|
+
const [activeAccount] = usePromise2(async () => {
|
|
17
37
|
try {
|
|
18
38
|
if (!validationError) {
|
|
19
39
|
if (wallet) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useAccount.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { AccountInstance } from '@xyo-network/account-model'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useCoinTypeWallet, useWalletContext, useWalletProvided } from '../contexts'\n\nexport interface AccountHookParams {\n account?: AccountInstance\n index?: number\n required?: boolean\n wallet?: WalletInstance\n}\n\nexport const useAccount = ({ wallet, account, index, required = false }: AccountHookParams = {}): [\n AccountInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [validationError, setValidationError] = useState<Error>()\n if (wallet && account && !validationError) {\n setValidationError(Error('useAccount can not have both a wallet and an account in the parameters'))\n }\n\n if (index && account && !validationError) {\n setValidationError(Error('useAccount can not have both a index and an account in the parameters'))\n }\n\n const [error, setError] = useState<Error>()\n const [coinTypeWallet] = useCoinTypeWallet(!wallet && required)\n const { activeAccountIndex } = useWalletContext(false)\n const [activeAccount] = usePromise(async () => {\n try {\n if (!validationError) {\n if (wallet) {\n return await wallet?.derivePath?.(`${index ?? 0}'\\0`)\n } else if (coinTypeWallet) {\n return await coinTypeWallet?.derivePath?.(`${index ?? activeAccountIndex ?? 0}'\\0`)\n }\n }\n } catch (ex) {\n const error = ex as Error\n console.error(error.message)\n setError(error)\n }\n }, [index, wallet, coinTypeWallet, activeAccountIndex, validationError])\n if (validationError && !error) {\n console.error(validationError.message)\n setError(validationError)\n }\n\n return [error ? undefined : account ?? activeAccount ?? walletContextProvided ? null : undefined, error]\n}\n"],"mappings":"AAAA,SAAS,
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/useAccount.ts","../../../src/contexts/Wallet/Context.ts","../../../src/contexts/Wallet/use.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { AccountInstance } from '@xyo-network/account-model'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useCoinTypeWallet, useWalletContext, useWalletProvided } from '../contexts'\n\nexport interface AccountHookParams {\n account?: AccountInstance\n index?: number\n required?: boolean\n wallet?: WalletInstance\n}\n\nexport const useAccount = ({ wallet, account, index, required = false }: AccountHookParams = {}): [\n AccountInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [validationError, setValidationError] = useState<Error>()\n if (wallet && account && !validationError) {\n setValidationError(Error('useAccount can not have both a wallet and an account in the parameters'))\n }\n\n if (index && account && !validationError) {\n setValidationError(Error('useAccount can not have both a index and an account in the parameters'))\n }\n\n const [error, setError] = useState<Error>()\n const [coinTypeWallet] = useCoinTypeWallet(!wallet && required)\n const { activeAccountIndex } = useWalletContext(false)\n const [activeAccount] = usePromise(async () => {\n try {\n if (!validationError) {\n if (wallet) {\n return await wallet?.derivePath?.(`${index ?? 0}'\\0`)\n } else if (coinTypeWallet) {\n return await coinTypeWallet?.derivePath?.(`${index ?? activeAccountIndex ?? 0}'\\0`)\n }\n }\n } catch (ex) {\n const error = ex as Error\n console.error(error.message)\n setError(error)\n }\n }, [index, wallet, coinTypeWallet, activeAccountIndex, validationError])\n if (validationError && !error) {\n console.error(validationError.message)\n setError(validationError)\n }\n\n return [error ? undefined : account ?? activeAccount ?? walletContextProvided ? null : undefined, error]\n}\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { WalletContextState } from './State'\n\nexport const WalletContext = createContextEx<WalletContextState>()\n","import { usePromise } from '@xylabs/react-promise'\nimport { useContextEx, useProvided } from '@xyo-network/react-shared'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { WalletContext } from './Context'\n\nexport const useWalletContext = (required = true) => {\n return useContextEx(WalletContext, 'Wallet', required)\n}\n\nexport const useWalletProvided = () => {\n return useProvided(WalletContext)\n}\n\nexport const useCoinTypeWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { coinTypeWallet } = useWalletContext(required)\n return [coinTypeWallet, undefined]\n}\n\nexport const useRootWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { rootWallet } = useWalletContext(required)\n return [rootWallet, undefined]\n}\n\nexport const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const [coinTypeWallet] = useCoinTypeWallet(required)\n const [wallet] = usePromise(async () => (await coinTypeWallet?.derivePath(index.toString())) ?? coinTypeWallet, [coinTypeWallet, index])\n return [wallet, undefined]\n}\n\nexport const useSelectedWalletAccount = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { activeAccountIndex } = useWalletContext(required)\n //we pass in 0 as default since we can not call the hook optionally,\n //we resolve this false result by checking whether activeAccountIndex is defined before returning it\n const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required)\n if (activeAccountIndex === undefined && account === null) {\n return [null, undefined]\n } else {\n return [account, undefined]\n }\n}\n"],"mappings":";AAAA,SAAS,cAAAA,mBAAkB;AAG3B,SAAS,gBAAgB;;;ACHzB,SAAS,uBAAuB;AAIzB,IAAM,gBAAgB,gBAAoC;;;ACJjE,SAAS,kBAAkB;AAC3B,SAAS,cAAc,mBAAmB;AAKnC,IAAM,mBAAmB,CA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;AACrC,aAAS,eAAe;AAAA,EAC1B;AAEA,SAAO,CAAC,QAAQ,SAAY,WAAW,iBAAiB,wBAAwB,OAAO,QAAW,KAAK;AACzG;","names":["usePromise","usePromise","error"]}
|
|
@@ -1,12 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
// src/hooks/useWallet.ts
|
|
2
|
+
import { usePromise as usePromise2 } from "@xylabs/react-promise";
|
|
2
3
|
import { HDWallet } from "@xyo-network/account";
|
|
3
4
|
import { useState } from "react";
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
// src/contexts/Wallet/Context.ts
|
|
7
|
+
import { createContextEx } from "@xyo-network/react-shared";
|
|
8
|
+
var WalletContext = createContextEx();
|
|
9
|
+
|
|
10
|
+
// src/contexts/Wallet/use.ts
|
|
11
|
+
import { usePromise } from "@xylabs/react-promise";
|
|
12
|
+
import { useContextEx, useProvided } from "@xyo-network/react-shared";
|
|
13
|
+
var useWalletContext = (required = true) => {
|
|
14
|
+
return useContextEx(WalletContext, "Wallet", required);
|
|
15
|
+
};
|
|
16
|
+
var useWalletProvided = () => {
|
|
17
|
+
return useProvided(WalletContext);
|
|
18
|
+
};
|
|
19
|
+
var useCoinTypeWallet = (required = true) => {
|
|
20
|
+
const { coinTypeWallet } = useWalletContext(required);
|
|
21
|
+
return [coinTypeWallet, void 0];
|
|
22
|
+
};
|
|
23
|
+
var useIndexedWalletFromContext = (index, required = true) => {
|
|
24
|
+
const [coinTypeWallet] = useCoinTypeWallet(required);
|
|
25
|
+
const [wallet] = usePromise(async () => await coinTypeWallet?.derivePath(index.toString()) ?? coinTypeWallet, [coinTypeWallet, index]);
|
|
26
|
+
return [wallet, void 0];
|
|
27
|
+
};
|
|
28
|
+
var useSelectedWalletAccount = (required = true) => {
|
|
29
|
+
const { activeAccountIndex } = useWalletContext(required);
|
|
30
|
+
const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required);
|
|
31
|
+
if (activeAccountIndex === void 0 && account === null) {
|
|
32
|
+
return [null, void 0];
|
|
33
|
+
} else {
|
|
34
|
+
return [account, void 0];
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/hooks/useWallet.ts
|
|
39
|
+
var useWallet = ({ mnemonic, wallet, path, required = false, seed } = {}) => {
|
|
6
40
|
const walletContextProvided = useWalletProvided();
|
|
7
41
|
const [error, setError] = useState();
|
|
8
42
|
const [contextAccount] = useSelectedWalletAccount(!wallet && required);
|
|
9
|
-
const [activeAccount] =
|
|
43
|
+
const [activeAccount] = usePromise2(async () => {
|
|
10
44
|
try {
|
|
11
45
|
const newAccount = await (() => {
|
|
12
46
|
if (wallet) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useWallet.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { DataLike } from '@xyo-network/core'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useSelectedWalletAccount, useWalletProvided } from '../contexts'\n\nexport interface WalletHookParams {\n mnemonic?: string\n path?: string\n required?: boolean\n seed?: DataLike\n wallet?: WalletInstance | null\n}\n\nexport const useWallet = ({ mnemonic, wallet, path, required = false, seed }: WalletHookParams = {}): [\n WalletInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [error, setError] = useState<Error>()\n const [contextAccount] = useSelectedWalletAccount(!wallet && required)\n const [activeAccount] = usePromise(async () => {\n try {\n const newAccount = await (() => {\n if (wallet) {\n return wallet\n } else if (mnemonic) {\n return HDWallet.fromMnemonic(mnemonic as string)\n } else if (seed) {\n return HDWallet.fromSeed(seed)\n }\n return contextAccount\n })()\n\n if (path) {\n return newAccount?.derivePath?.(path)\n } else {\n return newAccount ?? wallet\n }\n } catch (ex) {\n setError(ex as Error)\n }\n }, [mnemonic, contextAccount, seed, path, wallet])\n return [activeAccount ?? (walletContextProvided ? null : activeAccount), error]\n}\n"],"mappings":"AAAA,SAAS,
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/useWallet.ts","../../../src/contexts/Wallet/Context.ts","../../../src/contexts/Wallet/use.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { DataLike } from '@xyo-network/core'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useSelectedWalletAccount, useWalletProvided } from '../contexts'\n\nexport interface WalletHookParams {\n mnemonic?: string\n path?: string\n required?: boolean\n seed?: DataLike\n wallet?: WalletInstance | null\n}\n\nexport const useWallet = ({ mnemonic, wallet, path, required = false, seed }: WalletHookParams = {}): [\n WalletInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [error, setError] = useState<Error>()\n const [contextAccount] = useSelectedWalletAccount(!wallet && required)\n const [activeAccount] = usePromise(async () => {\n try {\n const newAccount = await (() => {\n if (wallet) {\n return wallet\n } else if (mnemonic) {\n return HDWallet.fromMnemonic(mnemonic as string)\n } else if (seed) {\n return HDWallet.fromSeed(seed)\n }\n return contextAccount\n })()\n\n if (path) {\n return newAccount?.derivePath?.(path)\n } else {\n return newAccount ?? wallet\n }\n } catch (ex) {\n setError(ex as Error)\n }\n }, [mnemonic, contextAccount, seed, path, wallet])\n return [activeAccount ?? (walletContextProvided ? null : activeAccount), error]\n}\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { WalletContextState } from './State'\n\nexport const WalletContext = createContextEx<WalletContextState>()\n","import { usePromise } from '@xylabs/react-promise'\nimport { useContextEx, useProvided } from '@xyo-network/react-shared'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { WalletContext } from './Context'\n\nexport const useWalletContext = (required = true) => {\n return useContextEx(WalletContext, 'Wallet', required)\n}\n\nexport const useWalletProvided = () => {\n return useProvided(WalletContext)\n}\n\nexport const useCoinTypeWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { coinTypeWallet } = useWalletContext(required)\n return [coinTypeWallet, undefined]\n}\n\nexport const useRootWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { rootWallet } = useWalletContext(required)\n return [rootWallet, undefined]\n}\n\nexport const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const [coinTypeWallet] = useCoinTypeWallet(required)\n const [wallet] = usePromise(async () => (await coinTypeWallet?.derivePath(index.toString())) ?? coinTypeWallet, [coinTypeWallet, index])\n return [wallet, undefined]\n}\n\nexport const useSelectedWalletAccount = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { activeAccountIndex } = useWalletContext(required)\n //we pass in 0 as default since we can not call the hook optionally,\n //we resolve this false result by checking whether activeAccountIndex is defined before returning it\n const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required)\n if (activeAccountIndex === undefined && account === null) {\n return [null, undefined]\n } else {\n return [account, undefined]\n }\n}\n"],"mappings":";AAAA,SAAS,cAAAA,mBAAkB;AAC3B,SAAS,gBAAgB;AAGzB,SAAS,gBAAgB;;;ACJzB,SAAS,uBAAuB;AAIzB,IAAM,gBAAgB,gBAAoC;;;ACJjE,SAAS,kBAAkB;AAC3B,SAAS,cAAc,mBAAmB;AAKnC,IAAM,mBAAmB,CAAC,WAAW,SAAS;AACnD,SAAO,aAAa,eAAe,UAAU,QAAQ;AACvD;AAEO,IAAM,oBAAoB,MAAM;AACrC,SAAO,YAAY,aAAa;AAClC;AAEO,IAAM,oBAAoB,CAAC,WAAW,SAAiE;AAC5G,QAAM,EAAE,eAAe,IAAI,iBAAiB,QAAQ;AACpD,SAAO,CAAC,gBAAgB,MAAS;AACnC;AAOO,IAAM,8BAA8B,CAAC,OAAe,WAAW,SAAiE;AACrI,QAAM,CAAC,cAAc,IAAI,kBAAkB,QAAQ;AACnD,QAAM,CAAC,MAAM,IAAI,WAAW,YAAa,MAAM,gBAAgB,WAAW,MAAM,SAAS,CAAC,KAAM,gBAAgB,CAAC,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|
+
|
|
4
|
+
// src/contexts/Wallet/Context.ts
|
|
5
|
+
import { createContextEx } from "@xyo-network/react-shared";
|
|
6
|
+
var WalletContext = createContextEx();
|
|
7
|
+
|
|
8
|
+
// src/contexts/Wallet/use.ts
|
|
1
9
|
import { usePromise } from "@xylabs/react-promise";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
10
|
+
import { useContextEx, useProvided } from "@xyo-network/react-shared";
|
|
11
|
+
var useWalletContext = (required = true) => {
|
|
12
|
+
return useContextEx(WalletContext, "Wallet", required);
|
|
13
|
+
};
|
|
14
|
+
var useWalletProvided = () => {
|
|
15
|
+
return useProvided(WalletContext);
|
|
16
|
+
};
|
|
17
|
+
var useCoinTypeWallet = (required = true) => {
|
|
18
|
+
const { coinTypeWallet } = useWalletContext(required);
|
|
19
|
+
return [coinTypeWallet, void 0];
|
|
20
|
+
};
|
|
21
|
+
var useIndexedWalletFromContext = (index, required = true) => {
|
|
22
|
+
const [coinTypeWallet] = useCoinTypeWallet(required);
|
|
23
|
+
const [wallet] = usePromise(async () => await coinTypeWallet?.derivePath(index.toString()) ?? coinTypeWallet, [coinTypeWallet, index]);
|
|
24
|
+
return [wallet, void 0];
|
|
25
|
+
};
|
|
26
|
+
var useSelectedWalletAccount = (required = true) => {
|
|
27
|
+
const { activeAccountIndex } = useWalletContext(required);
|
|
28
|
+
const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required);
|
|
29
|
+
if (activeAccountIndex === void 0 && account === null) {
|
|
30
|
+
return [null, void 0];
|
|
31
|
+
} else {
|
|
32
|
+
return [account, void 0];
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// src/hooks/useWallet.ts
|
|
37
|
+
import { usePromise as usePromise2 } from "@xylabs/react-promise";
|
|
38
|
+
import { HDWallet } from "@xyo-network/account";
|
|
39
|
+
import { useState } from "react";
|
|
40
|
+
var useWallet = ({ mnemonic, wallet, path, required = false, seed } = {}) => {
|
|
41
|
+
const walletContextProvided = useWalletProvided();
|
|
42
|
+
const [error, setError] = useState();
|
|
43
|
+
const [contextAccount] = useSelectedWalletAccount(!wallet && required);
|
|
44
|
+
const [activeAccount] = usePromise2(async () => {
|
|
45
|
+
try {
|
|
46
|
+
const newAccount = await (() => {
|
|
47
|
+
if (wallet) {
|
|
48
|
+
return wallet;
|
|
49
|
+
} else if (mnemonic) {
|
|
50
|
+
return HDWallet.fromMnemonic(mnemonic);
|
|
51
|
+
} else if (seed) {
|
|
52
|
+
return HDWallet.fromSeed(seed);
|
|
53
|
+
}
|
|
54
|
+
return contextAccount;
|
|
55
|
+
})();
|
|
56
|
+
if (path) {
|
|
57
|
+
return newAccount?.derivePath?.(path);
|
|
58
|
+
} else {
|
|
59
|
+
return newAccount ?? wallet;
|
|
60
|
+
}
|
|
61
|
+
} catch (ex) {
|
|
62
|
+
setError(ex);
|
|
63
|
+
}
|
|
64
|
+
}, [mnemonic, contextAccount, seed, path, wallet]);
|
|
65
|
+
return [activeAccount ?? (walletContextProvided ? null : activeAccount), error];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/hooks/useWallets.ts
|
|
69
|
+
var useWallets = ({ wallet, paths }) => {
|
|
5
70
|
const walletContextProvided = useWalletProvided();
|
|
6
71
|
const [foundWallet] = useWallet({ wallet });
|
|
7
|
-
const [wallets, error] =
|
|
72
|
+
const [wallets, error] = usePromise3(
|
|
8
73
|
async () => foundWallet ? await Promise.all(paths.map((path) => foundWallet.derivePath(path))) : void 0,
|
|
9
74
|
[foundWallet, paths]
|
|
10
75
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useWallets.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { useWalletProvided } from '../contexts'\nimport { useWallet } from './useWallet'\n\nexport interface WalletsHookParams {\n paths: string[]\n wallet?: WalletInstance | null\n}\n\nexport const useWallets = ({ wallet, paths }: WalletsHookParams): [WalletInstance[] | null | undefined, Error | undefined] => {\n const walletContextProvided = useWalletProvided()\n const [foundWallet] = useWallet({ wallet })\n const [wallets, error] = usePromise(\n async () => (foundWallet ? await Promise.all(paths.map((path) => foundWallet.derivePath(path))) : undefined),\n [foundWallet, paths],\n )\n return [wallets ?? (walletContextProvided ? null : wallets), error]\n}\n"],"mappings":"AAAA,SAAS,kBAAkB;
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/useWallets.ts","../../../src/contexts/Wallet/Context.ts","../../../src/contexts/Wallet/use.ts","../../../src/hooks/useWallet.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { useWalletProvided } from '../contexts'\nimport { useWallet } from './useWallet'\n\nexport interface WalletsHookParams {\n paths: string[]\n wallet?: WalletInstance | null\n}\n\nexport const useWallets = ({ wallet, paths }: WalletsHookParams): [WalletInstance[] | null | undefined, Error | undefined] => {\n const walletContextProvided = useWalletProvided()\n const [foundWallet] = useWallet({ wallet })\n const [wallets, error] = usePromise(\n async () => (foundWallet ? await Promise.all(paths.map((path) => foundWallet.derivePath(path))) : undefined),\n [foundWallet, paths],\n )\n return [wallets ?? (walletContextProvided ? null : wallets), error]\n}\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { WalletContextState } from './State'\n\nexport const WalletContext = createContextEx<WalletContextState>()\n","import { usePromise } from '@xylabs/react-promise'\nimport { useContextEx, useProvided } from '@xyo-network/react-shared'\nimport { WalletInstance } from '@xyo-network/wallet-model'\n\nimport { WalletContext } from './Context'\n\nexport const useWalletContext = (required = true) => {\n return useContextEx(WalletContext, 'Wallet', required)\n}\n\nexport const useWalletProvided = () => {\n return useProvided(WalletContext)\n}\n\nexport const useCoinTypeWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { coinTypeWallet } = useWalletContext(required)\n return [coinTypeWallet, undefined]\n}\n\nexport const useRootWallet = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { rootWallet } = useWalletContext(required)\n return [rootWallet, undefined]\n}\n\nexport const useIndexedWalletFromContext = (index: number, required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const [coinTypeWallet] = useCoinTypeWallet(required)\n const [wallet] = usePromise(async () => (await coinTypeWallet?.derivePath(index.toString())) ?? coinTypeWallet, [coinTypeWallet, index])\n return [wallet, undefined]\n}\n\nexport const useSelectedWalletAccount = (required = true): [WalletInstance | null | undefined, Error | undefined] => {\n const { activeAccountIndex } = useWalletContext(required)\n //we pass in 0 as default since we can not call the hook optionally,\n //we resolve this false result by checking whether activeAccountIndex is defined before returning it\n const [account] = useIndexedWalletFromContext(activeAccountIndex ?? 0, required)\n if (activeAccountIndex === undefined && account === null) {\n return [null, undefined]\n } else {\n return [account, undefined]\n }\n}\n","import { usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { DataLike } from '@xyo-network/core'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { useState } from 'react'\n\nimport { useSelectedWalletAccount, useWalletProvided } from '../contexts'\n\nexport interface WalletHookParams {\n mnemonic?: string\n path?: string\n required?: boolean\n seed?: DataLike\n wallet?: WalletInstance | null\n}\n\nexport const useWallet = ({ mnemonic, wallet, path, required = false, seed }: WalletHookParams = {}): [\n WalletInstance | null | undefined,\n Error | undefined,\n] => {\n const walletContextProvided = useWalletProvided()\n const [error, setError] = useState<Error>()\n const [contextAccount] = useSelectedWalletAccount(!wallet && required)\n const [activeAccount] = usePromise(async () => {\n try {\n const newAccount = await (() => {\n if (wallet) {\n return wallet\n } else if (mnemonic) {\n return HDWallet.fromMnemonic(mnemonic as string)\n } else if (seed) {\n return HDWallet.fromSeed(seed)\n }\n return contextAccount\n })()\n\n if (path) {\n return newAccount?.derivePath?.(path)\n } else {\n return newAccount ?? wallet\n }\n } catch (ex) {\n setError(ex as Error)\n }\n }, [mnemonic, contextAccount, seed, path, wallet])\n return [activeAccount ?? (walletContextProvided ? null :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B,CAAC,UAAU,QAAQ;AACrE,QAAM,CAAC,aAAa,IAAIC,YAAW,YAAY;AAC7C,QAAI;AACF,YAAM,aAAa,OAAO,MAAM;AAC9B,YAAI,QAAQ;AACV,iBAAO;AAAA,QACT,WAAW,UAAU;AACnB,iBAAO,SAAS,aAAa,QAAkB;AAAA,QACjD,WAAW,MAAM;AACf,iBAAO,SAAS,SAAS,IAAI;AAAA,QAC/B;AACA,eAAO;AAAA,MACT,GAAG;AAEH,UAAI,MAAM;AACR,eAAO,YAAY,aAAa,IAAI;AAAA,MACtC,OAAO;AACL,eAAO,cAAc;AAAA,MACvB;AAAA,IACF,SAAS,IAAI;AACX,eAAS,EAAW;AAAA,IACtB;AAAA,EACF,GAAG,CAAC,UAAU,gBAAgB,MAAM,MAAM,MAAM,CAAC;AACjD,SAAO,CAAC,kBAAkB,wBAAwB,OAAO,gBAAgB,KAAK;AAChF;;;AHnCO,IAAM,aAAa,CAAC,EAAE,QAAQ,MAAM,MAAmF;AAC5H,QAAM,wBAAwB,kBAAkB;AAChD,QAAM,CAAC,WAAW,IAAI,UAAU,EAAE,OAAO,CAAC;AAC1C,QAAM,CAAC,SAAS,KAAK,IAAIC;AAAA,IACvB,YAAa,cAAc,MAAM,QAAQ,IAAI,MAAM,IAAI,CAAC,SAAS,YAAY,WAAW,IAAI,CAAC,CAAC,IAAI;AAAA,IAClG,CAAC,aAAa,KAAK;AAAA,EACrB;AACA,SAAO,CAAC,YAAY,wBAAwB,OAAO,UAAU,KAAK;AACpE;","names":["usePromise","usePromise","usePromise","usePromise"]}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
// src/hooks/useWrapperAccount.ts
|
|
1
2
|
import { usePromise } from "@xylabs/react-promise";
|
|
2
3
|
import { HDWallet } from "@xyo-network/account";
|
|
3
4
|
import { Mutex } from "async-mutex";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
var globalWrapperWallet = void 0;
|
|
6
|
+
var globalWrapperWalletMutex = new Mutex();
|
|
7
|
+
var useWrapperAccount = (account) => {
|
|
7
8
|
return usePromise(async () => {
|
|
8
9
|
return await globalWrapperWalletMutex.runExclusive(async () => {
|
|
9
10
|
if (account !== void 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useWrapperAccount.ts"],"sourcesContent":["import { State as PromiseState, usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { AccountInstance } from '@xyo-network/account-model'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { Mutex } from 'async-mutex'\n\nlet globalWrapperWallet: WalletInstance | undefined = undefined\nconst globalWrapperWalletMutex = new Mutex()\n\nexport const useWrapperAccount = (\n account?: AccountInstance | null,\n): [AccountInstance | null | undefined, Error | undefined, PromiseState | undefined] => {\n return usePromise(async () => {\n return await globalWrapperWalletMutex.runExclusive(async () => {\n //if we are expecting to receive a wallet or did receive on, return the override account\n if (account !== undefined) {\n return account\n }\n\n if (globalWrapperWallet) {\n return globalWrapperWallet\n }\n\n try {\n globalWrapperWallet = await HDWallet.random()\n console.log(`Global Wrapper Wallet Creation Success: ${globalWrapperWallet.address}`)\n } catch (ex) {\n const error = ex as Error\n console.error(`Global Wrapper Wallet Creation Failed: ${error.message}`)\n }\n return globalWrapperWallet\n })\n }, [account])\n}\n"],"mappings":"AAAA,SAAgC,kBAAkB;AAClD,SAAS,gBAAgB;AAGzB,SAAS,aAAa;AAEtB,IAAI,sBAAkD;AACtD,
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/useWrapperAccount.ts"],"sourcesContent":["import { State as PromiseState, usePromise } from '@xylabs/react-promise'\nimport { HDWallet } from '@xyo-network/account'\nimport { AccountInstance } from '@xyo-network/account-model'\nimport { WalletInstance } from '@xyo-network/wallet-model'\nimport { Mutex } from 'async-mutex'\n\nlet globalWrapperWallet: WalletInstance | undefined = undefined\nconst globalWrapperWalletMutex = new Mutex()\n\nexport const useWrapperAccount = (\n account?: AccountInstance | null,\n): [AccountInstance | null | undefined, Error | undefined, PromiseState | undefined] => {\n return usePromise(async () => {\n return await globalWrapperWalletMutex.runExclusive(async () => {\n //if we are expecting to receive a wallet or did receive on, return the override account\n if (account !== undefined) {\n return account\n }\n\n if (globalWrapperWallet) {\n return globalWrapperWallet\n }\n\n try {\n globalWrapperWallet = await HDWallet.random()\n console.log(`Global Wrapper Wallet Creation Success: ${globalWrapperWallet.address}`)\n } catch (ex) {\n const error = ex as Error\n console.error(`Global Wrapper Wallet Creation Failed: ${error.message}`)\n }\n return globalWrapperWallet\n })\n }, [account])\n}\n"],"mappings":";AAAA,SAAgC,kBAAkB;AAClD,SAAS,gBAAgB;AAGzB,SAAS,aAAa;AAEtB,IAAI,sBAAkD;AACtD,IAAM,2BAA2B,IAAI,MAAM;AAEpC,IAAM,oBAAoB,CAC/B,YACsF;AACtF,SAAO,WAAW,YAAY;AAC5B,WAAO,MAAM,yBAAyB,aAAa,YAAY;AAE7D,UAAI,YAAY,QAAW;AACzB,eAAO;AAAA,MACT;AAEA,UAAI,qBAAqB;AACvB,eAAO;AAAA,MACT;AAEA,UAAI;AACF,8BAAsB,MAAM,SAAS,OAAO;AAC5C,gBAAQ,IAAI,2CAA2C,oBAAoB,OAAO,EAAE;AAAA,MACtF,SAAS,IAAI;AACX,cAAM,QAAQ;AACd,gBAAQ,MAAM,0CAA0C,MAAM,OAAO,EAAE;AAAA,MACzE;AACA,aAAO;AAAA,IACT,CAAC;AAAA,EACH,GAAG,CAAC,OAAO,CAAC;AACd;","names":[]}
|