@reef-knot/core-react 1.8.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_tslib.js +31 -0
- package/dist/components/AutoConnect.d.ts +10 -0
- package/dist/components/AutoConnect.d.ts.map +1 -0
- package/dist/components/AutoConnect.js +12 -0
- package/dist/context/acceptTermsModal.d.ts +9 -8
- package/dist/context/acceptTermsModal.d.ts.map +1 -1
- package/dist/context/reefKnot.d.ts +1 -0
- package/dist/context/reefKnot.d.ts.map +1 -1
- package/dist/context/reefKnot.js +7 -1
- package/dist/helpers/checkTermsAccepted.d.ts +2 -0
- package/dist/helpers/checkTermsAccepted.d.ts.map +1 -0
- package/dist/helpers/checkTermsAccepted.js +11 -0
- package/dist/helpers/getUnsupportedChainError.d.ts +3 -0
- package/dist/helpers/getUnsupportedChainError.d.ts.map +1 -0
- package/dist/helpers/getUnsupportedChainError.js +18 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/userAgents.d.ts +8 -0
- package/dist/helpers/userAgents.d.ts.map +1 -0
- package/dist/helpers/userAgents.js +11 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/useAutoConnect.d.ts +2 -0
- package/dist/hooks/useAutoConnect.d.ts.map +1 -0
- package/dist/hooks/useAutoConnect.js +38 -0
- package/dist/hooks/useConnectorInfo.d.ts +11 -0
- package/dist/hooks/useConnectorInfo.d.ts.map +1 -0
- package/dist/hooks/useConnectorInfo.js +31 -0
- package/dist/hooks/useDisconnect.d.ts +7 -0
- package/dist/hooks/useDisconnect.d.ts.map +1 -0
- package/dist/hooks/useDisconnect.js +39 -0
- package/dist/hooks/useEagerConnect.d.ts +15 -0
- package/dist/hooks/useEagerConnect.d.ts.map +1 -0
- package/dist/hooks/useEagerConnect.js +101 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/package.json +9 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
|
|
16
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
27
|
+
var e = new Error(message);
|
|
28
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { __awaiter };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Chain } from 'wagmi';
|
|
3
|
+
import type { WalletAdapterData } from '@reef-knot/types';
|
|
4
|
+
export declare const AutoConnect: ({ children, autoConnect, }: {
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
autoConnect: boolean;
|
|
7
|
+
walletDataList: WalletAdapterData[];
|
|
8
|
+
chains: Chain[];
|
|
9
|
+
}) => React.JSX.Element;
|
|
10
|
+
//# sourceMappingURL=AutoConnect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AutoConnect.d.ts","sourceRoot":"","sources":["../../src/components/AutoConnect.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D,eAAO,MAAM,WAAW;cAIZ,MAAM,SAAS;iBACZ,OAAO;oBACJ,iBAAiB,EAAE;YAC3B,KAAK,EAAE;uBAKhB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useAutoConnect } from '../hooks/useAutoConnect.js';
|
|
3
|
+
|
|
4
|
+
const AutoConnect = ({
|
|
5
|
+
children,
|
|
6
|
+
autoConnect
|
|
7
|
+
}) => {
|
|
8
|
+
useAutoConnect(autoConnect);
|
|
9
|
+
return React.createElement(React.Fragment, null, children);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { AutoConnect };
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
export type AcceptTermsModal = {
|
|
3
|
+
isVisible: boolean;
|
|
4
|
+
setVisible: React.Dispatch<React.SetStateAction<boolean>>;
|
|
5
|
+
onContinue: () => void;
|
|
6
|
+
setOnContinue: React.Dispatch<React.SetStateAction<() => void>>;
|
|
7
|
+
error?: Error;
|
|
8
|
+
setError: React.Dispatch<React.SetStateAction<Error | undefined>>;
|
|
9
|
+
};
|
|
2
10
|
export type AcceptTermsModalContextValue = {
|
|
3
|
-
acceptTermsModal:
|
|
4
|
-
isVisible: boolean;
|
|
5
|
-
setVisible: React.Dispatch<React.SetStateAction<boolean>>;
|
|
6
|
-
onContinue: () => void;
|
|
7
|
-
setOnContinue: React.Dispatch<React.SetStateAction<() => void>>;
|
|
8
|
-
error?: Error;
|
|
9
|
-
setError: React.Dispatch<React.SetStateAction<Error | undefined>>;
|
|
10
|
-
};
|
|
11
|
+
acceptTermsModal: AcceptTermsModal;
|
|
11
12
|
};
|
|
12
13
|
export declare const AcceptTermsModalContext: React.Context<AcceptTermsModalContextValue>;
|
|
13
14
|
export declare const AcceptTermsModalContextProvider: ({ children, }: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"acceptTermsModal.d.ts","sourceRoot":"","sources":["../../src/context/acceptTermsModal.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA2C,MAAM,OAAO,CAAC;AAEhE,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"acceptTermsModal.d.ts","sourceRoot":"","sources":["../../src/context/acceptTermsModal.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA2C,MAAM,OAAO,CAAC;AAEhE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1D,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,aAAa,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC;IAChE,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC;CACnE,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,gBAAgB,EAAE,gBAAgB,CAAC;CACpC,CAAC;AAKF,eAAO,MAAM,uBAAuB,6CAKF,CAAC;AAEnC,eAAO,MAAM,+BAA+B;cAGhC,MAAM,SAAS;uBA+B1B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reefKnot.d.ts","sourceRoot":"","sources":["../../src/context/reefKnot.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAiB,EAAE,EAAE,SAAS,EAAW,MAAM,OAAO,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"reefKnot.d.ts","sourceRoot":"","sources":["../../src/context/reefKnot.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAiB,EAAE,EAAE,SAAS,EAAW,MAAM,OAAO,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAMrC,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5B,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,YAAY,EAAE,KAAK,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5B,cAAc,EAAE,iBAAiB,EAAE,CAAC;IACpC,MAAM,EAAE,KAAK,EAAE,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,eAAe,qCAA4C,CAAC;AAEzE,eAAO,MAAM,QAAQ,EAAE,EAAE,CAAC,oBAAoB,CAuC7C,CAAC"}
|
package/dist/context/reefKnot.js
CHANGED
|
@@ -2,6 +2,7 @@ import React, { createContext, useMemo } from 'react';
|
|
|
2
2
|
import { WCWarnBannerContextProvider } from '@reef-knot/ui-react';
|
|
3
3
|
import { getWalletDataList } from '../walletData/index.js';
|
|
4
4
|
import { AcceptTermsModalContextProvider } from './acceptTermsModal.js';
|
|
5
|
+
import { AutoConnect } from '../components/AutoConnect.js';
|
|
5
6
|
|
|
6
7
|
const ReefKnotContext = createContext({});
|
|
7
8
|
const ReefKnot = ({
|
|
@@ -9,6 +10,7 @@ const ReefKnot = ({
|
|
|
9
10
|
walletconnectProjectId,
|
|
10
11
|
chains,
|
|
11
12
|
defaultChain,
|
|
13
|
+
autoConnect = true,
|
|
12
14
|
children
|
|
13
15
|
}) => {
|
|
14
16
|
const walletDataList = getWalletDataList({
|
|
@@ -24,7 +26,11 @@ const ReefKnot = ({
|
|
|
24
26
|
}), [rpc, walletDataList, chains]);
|
|
25
27
|
return React.createElement(ReefKnotContext.Provider, {
|
|
26
28
|
value: contextValue
|
|
27
|
-
}, React.createElement(AcceptTermsModalContextProvider, null, React.createElement(WCWarnBannerContextProvider, null,
|
|
29
|
+
}, React.createElement(AcceptTermsModalContextProvider, null, React.createElement(WCWarnBannerContextProvider, null, React.createElement(AutoConnect, {
|
|
30
|
+
autoConnect: autoConnect,
|
|
31
|
+
walletDataList: walletDataList,
|
|
32
|
+
chains: chains
|
|
33
|
+
}, children))));
|
|
28
34
|
};
|
|
29
35
|
|
|
30
36
|
export { ReefKnot, ReefKnotContext };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkTermsAccepted.d.ts","sourceRoot":"","sources":["../../src/helpers/checkTermsAccepted.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,kBAAkB,eAK9B,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { LS_KEY_TERMS_ACCEPTANCE } from '../constants/localStorage.js';
|
|
2
|
+
|
|
3
|
+
const checkTermsAccepted = () => {
|
|
4
|
+
var _a;
|
|
5
|
+
if (typeof window !== 'undefined') {
|
|
6
|
+
return ((_a = window.localStorage) === null || _a === void 0 ? void 0 : _a.getItem(LS_KEY_TERMS_ACCEPTANCE)) === 'true';
|
|
7
|
+
}
|
|
8
|
+
return false;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { checkTermsAccepted };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getUnsupportedChainError.d.ts","sourceRoot":"","sources":["../../src/helpers/getUnsupportedChainError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,eAAO,MAAM,wBAAwB,oBAAqB,KAAK,EAAE,UAkBhE,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const getUnsupportedChainError = supportedChains => {
|
|
2
|
+
// Get names of supported chains to suggest them in case of "unsupported network" error
|
|
3
|
+
const supportedChainsNames = (() => {
|
|
4
|
+
const chains = supportedChains
|
|
5
|
+
// On Lido widgets the Polygon Mumbai network was added as a temporary workaround for the wagmi and walletconnect bug,
|
|
6
|
+
// when some wallets are failing to connect if there are only one supported network, so we need at least 2 of them.
|
|
7
|
+
// But we don't want to mention it in the error as a suggested supported network, because it is not really true, so temporary filtering it out.
|
|
8
|
+
// TODO: the issue is fixed in wagmi v1+, remove the filter after updating wagmi to v1+
|
|
9
|
+
.filter(chain => chain.id !== 80001).map(({
|
|
10
|
+
name
|
|
11
|
+
}) => name).filter(chainName => chainName !== 'unknown');
|
|
12
|
+
const lastChain = chains.pop();
|
|
13
|
+
return [chains.join(', '), lastChain].filter(chain => chain).join(' or ');
|
|
14
|
+
})();
|
|
15
|
+
return new Error(`Unsupported chain. Please switch to ${supportedChainsNames} in your wallet.`);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { getUnsupportedChainError };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sBAAsB,CAAC;AACrC,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="ua-parser-js" />
|
|
2
|
+
export declare const device: import("ua-parser-js").IDevice;
|
|
3
|
+
export declare const browser: import("ua-parser-js").IBrowser;
|
|
4
|
+
export declare const os: import("ua-parser-js").IOS;
|
|
5
|
+
export declare const isMobile: boolean;
|
|
6
|
+
export declare const isTablet: boolean;
|
|
7
|
+
export declare const isMobileOrTablet: boolean;
|
|
8
|
+
//# sourceMappingURL=userAgents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"userAgents.d.ts","sourceRoot":"","sources":["../../src/helpers/userAgents.ts"],"names":[],"mappings":";AAIA,eAAO,MAAM,MAAM,gCAAqB,CAAC;AACzC,eAAO,MAAM,OAAO,iCAAsB,CAAC;AAC3C,eAAO,MAAM,EAAE,4BAAiB,CAAC;AAEjC,eAAO,MAAM,QAAQ,SAA2B,CAAC;AACjD,eAAO,MAAM,QAAQ,SAA2B,CAAC;AACjD,eAAO,MAAM,gBAAgB,SAAuB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UAParser } from 'ua-parser-js';
|
|
2
|
+
|
|
3
|
+
const parser = new UAParser();
|
|
4
|
+
const device = parser.getDevice();
|
|
5
|
+
const browser = parser.getBrowser();
|
|
6
|
+
const os = parser.getOS();
|
|
7
|
+
const isMobile = device.type === 'mobile';
|
|
8
|
+
const isTablet = device.type === 'tablet';
|
|
9
|
+
const isMobileOrTablet = isMobile || isTablet;
|
|
10
|
+
|
|
11
|
+
export { browser, device, isMobile, isMobileOrTablet, isTablet, os };
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAutoConnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useAutoConnect.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,cAAc,uBAAwB,OAAO,SA4BzD,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
+
import { useClient, useAccount } from 'wagmi';
|
|
3
|
+
import { useRef, useCallback, useEffect } from 'react';
|
|
4
|
+
import { checkTermsAccepted } from '../helpers/checkTermsAccepted.js';
|
|
5
|
+
import { useEagerConnect } from './useEagerConnect.js';
|
|
6
|
+
|
|
7
|
+
const useAutoConnect = autoConnectEnabled => {
|
|
8
|
+
const isAutoConnectCalled = useRef(false);
|
|
9
|
+
const client = useClient();
|
|
10
|
+
const {
|
|
11
|
+
isConnected
|
|
12
|
+
} = useAccount();
|
|
13
|
+
const {
|
|
14
|
+
eagerConnect
|
|
15
|
+
} = useEagerConnect();
|
|
16
|
+
const autoConnect = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
// Don't auto-connect if already connected or if the auto-connect feature is disabled or if already tried to auto-connect.
|
|
18
|
+
if (isConnected || !autoConnectEnabled || isAutoConnectCalled.current) return;
|
|
19
|
+
// The current logic is to try auto-connect only once, even if an error happened and connection was not successful.
|
|
20
|
+
isAutoConnectCalled.current = true;
|
|
21
|
+
// Try to eagerly connect wallets that are meant to be used only with auto-connection.
|
|
22
|
+
// For example, wallets with dApp browsers, or using iframes to open dApps.
|
|
23
|
+
const {
|
|
24
|
+
connectResult,
|
|
25
|
+
connectError
|
|
26
|
+
} = yield eagerConnect();
|
|
27
|
+
// If still not connected and there were no errors and the terms of service are accepted,
|
|
28
|
+
// call the default wagmi autoConnect method, which attempts to connect to the last used connector.
|
|
29
|
+
if (!connectResult && !connectError && checkTermsAccepted()) {
|
|
30
|
+
yield client.autoConnect();
|
|
31
|
+
}
|
|
32
|
+
}), [autoConnectEnabled, client, eagerConnect, isConnected]);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
void autoConnect();
|
|
35
|
+
}, [autoConnect]);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { useAutoConnect };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type ConnectorInfo = {
|
|
2
|
+
connectorName?: string;
|
|
3
|
+
isGnosis: boolean;
|
|
4
|
+
isLedger: boolean;
|
|
5
|
+
isLedgerLive: boolean;
|
|
6
|
+
isDappBrowser: boolean;
|
|
7
|
+
isAutoConnectionSuitable: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare const useConnectorInfo: () => ConnectorInfo;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=useConnectorInfo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConnectorInfo.d.ts","sourceRoot":"","sources":["../../src/hooks/useConnectorInfo.ts"],"names":[],"mappings":"AAQA,KAAK,aAAa,GAAG;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,aAAa,EAAE,OAAO,CAAC;IACvB,wBAAwB,EAAE,OAAO,CAAC;CACnC,CAAC;AAEF,eAAO,MAAM,gBAAgB,QAAO,aAwBnC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useAccount } from 'wagmi';
|
|
2
|
+
import { SafeConnector } from 'wagmi/connectors/safe';
|
|
3
|
+
import { LedgerHIDConnector, LedgerLiveConnector } from '@reef-knot/ledger-connector';
|
|
4
|
+
import { isMobileOrTablet } from '../helpers/userAgents.js';
|
|
5
|
+
|
|
6
|
+
const useConnectorInfo = () => {
|
|
7
|
+
var _a;
|
|
8
|
+
const {
|
|
9
|
+
connector
|
|
10
|
+
} = useAccount();
|
|
11
|
+
const isLedger = connector instanceof LedgerHIDConnector;
|
|
12
|
+
const isLedgerLive = connector instanceof LedgerLiveConnector;
|
|
13
|
+
const isGnosis = connector instanceof SafeConnector;
|
|
14
|
+
const isDappBrowser = !!((_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.ethereum) && isMobileOrTablet;
|
|
15
|
+
const isAutoConnectionSuitable = isLedgerLive || isGnosis || isDappBrowser;
|
|
16
|
+
let connectorName = connector === null || connector === void 0 ? void 0 : connector.name;
|
|
17
|
+
// Do not set connector's name if the app is opened in a mobile wallet dapp browser,
|
|
18
|
+
// because we use a generic injected connector for this case and proper detection is hard.
|
|
19
|
+
// Also, it will be easy for a user to understand which wallet app is being used for connection.
|
|
20
|
+
if (isDappBrowser) connectorName = undefined;
|
|
21
|
+
return {
|
|
22
|
+
connectorName,
|
|
23
|
+
isGnosis,
|
|
24
|
+
isLedger,
|
|
25
|
+
isLedgerLive,
|
|
26
|
+
isDappBrowser,
|
|
27
|
+
isAutoConnectionSuitable
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { useConnectorInfo };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDisconnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useDisconnect.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,kBAAkB;;CAY9B,CAAC;AAEF,eAAO,MAAM,aAAa;wBACL,IAAI;CAWxB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useContext, useCallback } from 'react';
|
|
2
|
+
import { useDisconnect as useDisconnect$1, useAccount } from 'wagmi';
|
|
3
|
+
import { useConnectorInfo } from './useConnectorInfo.js';
|
|
4
|
+
import { AcceptTermsModalContext } from '../context/acceptTermsModal.js';
|
|
5
|
+
|
|
6
|
+
const useForceDisconnect = () => {
|
|
7
|
+
const {
|
|
8
|
+
disconnect
|
|
9
|
+
} = useDisconnect$1();
|
|
10
|
+
const {
|
|
11
|
+
acceptTermsModal: {
|
|
12
|
+
setVisible
|
|
13
|
+
}
|
|
14
|
+
} = useContext(AcceptTermsModalContext);
|
|
15
|
+
const forceDisconnect = useCallback(() => {
|
|
16
|
+
disconnect();
|
|
17
|
+
setVisible(false);
|
|
18
|
+
}, [disconnect, setVisible]);
|
|
19
|
+
return {
|
|
20
|
+
forceDisconnect
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
const useDisconnect = () => {
|
|
24
|
+
const {
|
|
25
|
+
isConnected
|
|
26
|
+
} = useAccount();
|
|
27
|
+
const {
|
|
28
|
+
disconnect
|
|
29
|
+
} = useDisconnect$1();
|
|
30
|
+
const {
|
|
31
|
+
isAutoConnectionSuitable
|
|
32
|
+
} = useConnectorInfo();
|
|
33
|
+
const available = isConnected && !isAutoConnectionSuitable;
|
|
34
|
+
return {
|
|
35
|
+
disconnect: available ? disconnect : undefined
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { useDisconnect, useForceDisconnect };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Chain } from 'wagmi';
|
|
2
|
+
import type { ConnectResult } from '@wagmi/core';
|
|
3
|
+
import { WalletAdapterData } from '@reef-knot/types';
|
|
4
|
+
import { AcceptTermsModal } from '../context/acceptTermsModal';
|
|
5
|
+
export declare const connectEagerly: (adapters: WalletAdapterData[], acceptTermsModal: AcceptTermsModal, supportedChains: Chain[]) => Promise<{
|
|
6
|
+
connectResult: ConnectResult | null;
|
|
7
|
+
connectError?: Error | undefined;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const useEagerConnect: () => {
|
|
10
|
+
eagerConnect: () => Promise<{
|
|
11
|
+
connectResult: ConnectResult | null;
|
|
12
|
+
connectError?: Error | undefined;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=useEagerConnect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEagerConnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useEagerConnect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,OAAO,KAAK,EAAE,aAAa,EAAa,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EACL,gBAAgB,EAEjB,MAAM,6BAA6B,CAAC;AAqDrC,eAAO,MAAM,cAAc,aACf,iBAAiB,EAAE,oBACX,gBAAgB,mBACjB,KAAK,EAAE;mBAET,aAAa,GAAG,IAAI;;EAgCpC,CAAC;AAEF,eAAO,MAAM,eAAe;;uBAlCX,aAAa,GAAG,IAAI;;;CA8CpC,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
+
import { connect, disconnect } from 'wagmi/actions';
|
|
3
|
+
import { useContext, useCallback } from 'react';
|
|
4
|
+
import { AcceptTermsModalContext } from '../context/acceptTermsModal.js';
|
|
5
|
+
import { getUnsupportedChainError } from '../helpers/getUnsupportedChainError.js';
|
|
6
|
+
import { checkTermsAccepted } from '../helpers/checkTermsAccepted.js';
|
|
7
|
+
import { useReefKnotContext } from './useReefKnotContext.js';
|
|
8
|
+
|
|
9
|
+
const connectAndHandleErrors = (connector, supportedChains, acceptTermsModal) => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
+
var _a, _b, _c, _d;
|
|
11
|
+
let connectResult = null;
|
|
12
|
+
let connectError;
|
|
13
|
+
try {
|
|
14
|
+
connectResult = yield connect({
|
|
15
|
+
connector
|
|
16
|
+
});
|
|
17
|
+
} catch (e) {
|
|
18
|
+
connectResult = null; // ensure that connectResult is empty in case of an error
|
|
19
|
+
connectError = e;
|
|
20
|
+
}
|
|
21
|
+
if (connectResult === null || connectResult === void 0 ? void 0 : connectResult.chain.unsupported) {
|
|
22
|
+
// No errors during connection, but the chain is unsupported.
|
|
23
|
+
// This case is considered as error for now, and we explicitly call disconnect() here.
|
|
24
|
+
// This logic comes from previously used web3-react connection logic, which wasn't reworked yet after web3-react removal.
|
|
25
|
+
// web3-react logic was: if a chain is unsupported – break the connection, throw an error
|
|
26
|
+
// wagmi logic is: if a chain is unsupported – connect anyway, without errors, set `chain.unsupported` flag to true.
|
|
27
|
+
// So, here we are trying to mimic the legacy logic, because we are not ready to rework it yet.
|
|
28
|
+
connectResult = null;
|
|
29
|
+
connectError = getUnsupportedChainError(supportedChains);
|
|
30
|
+
yield disconnect();
|
|
31
|
+
// A user may change a chain in a wallet app, prepare for that event.
|
|
32
|
+
// There is a strong recommendation in the MetaMask documentation
|
|
33
|
+
// to reload the page upon chain changes, unless there is a good reason not to.
|
|
34
|
+
// This looks like a good general approach.
|
|
35
|
+
const provider = yield connector.getProvider();
|
|
36
|
+
provider.once('chainChanged', () => {
|
|
37
|
+
var _a;
|
|
38
|
+
return (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.location.reload();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (connectError) {
|
|
42
|
+
(_a = acceptTermsModal.setError) === null || _a === void 0 ? void 0 : _a.call(acceptTermsModal, connectError);
|
|
43
|
+
(_b = acceptTermsModal.setVisible) === null || _b === void 0 ? void 0 : _b.call(acceptTermsModal, true);
|
|
44
|
+
} else {
|
|
45
|
+
(_c = acceptTermsModal.setVisible) === null || _c === void 0 ? void 0 : _c.call(acceptTermsModal, false);
|
|
46
|
+
(_d = acceptTermsModal.setError) === null || _d === void 0 ? void 0 : _d.call(acceptTermsModal, undefined);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
connectResult,
|
|
50
|
+
connectError
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
const connectEagerly = (adapters, acceptTermsModal, supportedChains) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
+
var _e, _f, _g;
|
|
55
|
+
const isTermsAccepted = checkTermsAccepted();
|
|
56
|
+
let connectResult = null;
|
|
57
|
+
let connectError;
|
|
58
|
+
const continueConnection = connector => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
({
|
|
60
|
+
connectResult,
|
|
61
|
+
connectError
|
|
62
|
+
} = yield connectAndHandleErrors(connector, supportedChains, acceptTermsModal));
|
|
63
|
+
});
|
|
64
|
+
for (const adapter of adapters) {
|
|
65
|
+
if ((_e = adapter.detector) === null || _e === void 0 ? void 0 : _e.call(adapter)) {
|
|
66
|
+
// wallet is detected
|
|
67
|
+
if (!isTermsAccepted) {
|
|
68
|
+
// Terms of service were not accepted previously.
|
|
69
|
+
// So, for legal reasons, we must ask a user to accept the terms before connecting.
|
|
70
|
+
const onContinue = () => void continueConnection(adapter.connector);
|
|
71
|
+
(_f = acceptTermsModal.setOnContinue) === null || _f === void 0 ? void 0 : _f.call(acceptTermsModal, () => onContinue);
|
|
72
|
+
(_g = acceptTermsModal.setVisible) === null || _g === void 0 ? void 0 : _g.call(acceptTermsModal, true);
|
|
73
|
+
} else {
|
|
74
|
+
yield continueConnection(adapter.connector);
|
|
75
|
+
}
|
|
76
|
+
break; // no need to iterate over all other adapters if at least one was detected
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
connectResult,
|
|
81
|
+
connectError
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
const useEagerConnect = () => {
|
|
85
|
+
const {
|
|
86
|
+
acceptTermsModal
|
|
87
|
+
} = useContext(AcceptTermsModalContext);
|
|
88
|
+
const {
|
|
89
|
+
walletDataList,
|
|
90
|
+
chains
|
|
91
|
+
} = useReefKnotContext();
|
|
92
|
+
const autoConnectOnlyAdapters = walletDataList.filter(adapter => adapter.autoConnectOnly);
|
|
93
|
+
const eagerConnect = useCallback(() => {
|
|
94
|
+
return connectEagerly(autoConnectOnlyAdapters, acceptTermsModal, chains);
|
|
95
|
+
}, [acceptTermsModal, autoConnectOnlyAdapters, chains]);
|
|
96
|
+
return {
|
|
97
|
+
eagerConnect
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { connectEagerly, useEagerConnect };
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
export { getConnectors, getWalletDataList } from './walletData/index.js';
|
|
2
2
|
export { useReefKnotContext } from './hooks/useReefKnotContext.js';
|
|
3
|
+
export { useAutoConnect } from './hooks/useAutoConnect.js';
|
|
4
|
+
export { connectEagerly, useEagerConnect } from './hooks/useEagerConnect.js';
|
|
5
|
+
export { useDisconnect, useForceDisconnect } from './hooks/useDisconnect.js';
|
|
6
|
+
export { useConnectorInfo } from './hooks/useConnectorInfo.js';
|
|
3
7
|
export { ReefKnot, ReefKnotContext } from './context/reefKnot.js';
|
|
4
8
|
export { AcceptTermsModalContext, AcceptTermsModalContextProvider } from './context/acceptTermsModal.js';
|
|
5
9
|
export { LS_KEY_TERMS_ACCEPTANCE } from './constants/localStorage.js';
|
|
6
10
|
export { holesky } from './chains/holesky.js';
|
|
11
|
+
export { getUnsupportedChainError } from './helpers/getUnsupportedChainError.js';
|
|
12
|
+
export { checkTermsAccepted } from './helpers/checkTermsAccepted.js';
|
|
13
|
+
export { browser, device, isMobile, isMobileOrTablet, isTablet, os } from './helpers/userAgents.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reef-knot/core-react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -36,15 +36,21 @@
|
|
|
36
36
|
"dev": "dev=on rollup -c -w",
|
|
37
37
|
"lint": "eslint --ext ts,tsx,js,mjs ."
|
|
38
38
|
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"ua-parser-js": "1.0.33"
|
|
41
|
+
},
|
|
39
42
|
"devDependencies": {
|
|
40
|
-
"@reef-knot/
|
|
41
|
-
"@reef-knot/
|
|
43
|
+
"@reef-knot/ledger-connector": "^3.0.0",
|
|
44
|
+
"@reef-knot/wallets-list": "^1.12.0",
|
|
45
|
+
"@reef-knot/types": "^1.5.0",
|
|
42
46
|
"@reef-knot/ui-react": "^1.0.8",
|
|
47
|
+
"@types/ua-parser-js": "^0.7.36",
|
|
43
48
|
"eslint-config-custom": "*",
|
|
44
49
|
"react": "18.2.0",
|
|
45
50
|
"wagmi": "^0.12.19"
|
|
46
51
|
},
|
|
47
52
|
"peerDependencies": {
|
|
53
|
+
"@reef-knot/ledger-connector": "^3.0.0",
|
|
48
54
|
"@reef-knot/wallets-list": "^1.4.1",
|
|
49
55
|
"@reef-knot/types": "^1.2.1",
|
|
50
56
|
"@reef-knot/ui-react": "^1.0.4",
|