@btc-vision/walletconnect 1.4.1 → 1.5.1
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/.prettierrc.json +1 -1
- package/CHANGELOG.md +83 -0
- package/README.md +11 -11
- package/browser/consts.d.ts +2 -0
- package/browser/context/WalletConnectContext.d.ts +18 -0
- package/browser/hooks/WalletConnectHook.d.ts +2 -0
- package/browser/index.d.ts +4 -2
- package/browser/index.js +1 -1
- package/browser/index.js.LICENSE.txt +0 -2
- package/browser/provider/WalletConnectProvider.d.ts +9 -0
- package/browser/types.d.ts +11 -0
- package/browser/utils/accessibility/definitions.d.ts +2 -0
- package/browser/utils/accessibility/errorDecoder.d.ts +2 -0
- package/browser/utils/accessibility/patterns.d.ts +13 -0
- package/browser/wallets/controller.d.ts +29 -0
- package/browser/wallets/index.d.ts +4 -0
- package/browser/wallets/opwallet/controller.d.ts +26 -0
- package/browser/wallets/opwallet/interface.d.ts +5 -0
- package/browser/wallets/types.d.ts +34 -0
- package/browser/wallets/unisat/controller.d.ts +26 -0
- package/browser/wallets/unisat/interface.d.ts +5 -0
- package/build/consts.d.ts +2 -0
- package/build/consts.js +6 -0
- package/build/context/WalletConnectContext.d.ts +18 -0
- package/build/context/WalletConnectContext.js +2 -0
- package/build/hooks/WalletConnectHook.d.ts +2 -0
- package/build/hooks/WalletConnectHook.js +9 -0
- package/build/index.d.ts +4 -2
- package/build/index.js +4 -2
- package/build/provider/WalletConnectProvider.d.ts +9 -0
- package/build/provider/WalletConnectProvider.js +178 -0
- package/build/types.d.ts +11 -0
- package/build/types.js +1 -0
- package/build/utils/accessibility/definitions.d.ts +2 -0
- package/build/utils/accessibility/definitions.js +8 -0
- package/build/utils/accessibility/errorDecoder.d.ts +2 -0
- package/build/utils/accessibility/errorDecoder.js +73 -0
- package/build/utils/accessibility/patterns.d.ts +13 -0
- package/build/utils/accessibility/patterns.js +51 -0
- package/build/wallets/controller.d.ts +29 -0
- package/build/wallets/controller.js +205 -0
- package/build/wallets/index.d.ts +4 -0
- package/build/wallets/index.js +16 -0
- package/build/wallets/opwallet/controller.d.ts +26 -0
- package/build/wallets/opwallet/controller.js +132 -0
- package/build/wallets/opwallet/interface.d.ts +5 -0
- package/build/wallets/opwallet/interface.js +35 -0
- package/build/wallets/types.d.ts +34 -0
- package/build/wallets/types.js +1 -0
- package/build/wallets/unisat/controller.d.ts +26 -0
- package/build/wallets/unisat/controller.js +135 -0
- package/build/wallets/unisat/interface.d.ts +5 -0
- package/build/wallets/unisat/interface.js +285 -0
- package/eslint.config.js +23 -2
- package/package.json +15 -10
- package/src/README.md +24 -0
- package/src/consts.ts +8 -0
- package/src/context/WalletConnectContext.ts +21 -0
- package/src/hooks/WalletConnectHook.tsx +13 -0
- package/src/index.ts +5 -2
- package/src/provider/WalletConnectProvider.tsx +341 -0
- package/src/types.ts +13 -0
- package/src/utils/accessibility/definitions.ts +9 -0
- package/src/utils/accessibility/errorDecoder.ts +105 -0
- package/src/utils/accessibility/patterns.ts +86 -0
- package/src/utils/style.css +131 -0
- package/src/utils/theme.css +26 -0
- package/src/wallets/controller.ts +231 -0
- package/src/wallets/index.ts +22 -0
- package/src/wallets/opwallet/controller.ts +177 -0
- package/src/wallets/opwallet/interface.ts +42 -0
- package/src/wallets/types.ts +39 -0
- package/src/wallets/unisat/controller.ts +179 -0
- package/src/wallets/unisat/interface.ts +292 -0
- package/tsconfig.webpack.json +2 -6
- package/webpack.config.js +4 -0
- package/browser/WalletConnection.d.ts +0 -21
- package/browser/WalletProvider.d.ts +0 -25
- package/build/WalletConnection.d.ts +0 -21
- package/build/WalletConnection.js +0 -152
- package/build/WalletProvider.d.ts +0 -25
- package/build/WalletProvider.js +0 -178
- package/src/WalletConnection.ts +0 -210
- package/src/WalletProvider.tsx +0 -240
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React, { type ReactNode } from 'react';
|
|
2
|
+
import '../utils/style.css';
|
|
3
|
+
import '../utils/theme.css';
|
|
4
|
+
interface WalletConnectProviderProps {
|
|
5
|
+
theme?: 'light' | 'dark' | 'moto';
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
declare const WalletConnectProvider: React.FC<WalletConnectProviderProps>;
|
|
9
|
+
export default WalletConnectProvider;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Network } from '@btc-vision/bitcoin';
|
|
2
|
+
import { UnisatChainType } from '@btc-vision/transaction';
|
|
3
|
+
export interface WalletConnectNetwork extends Network {
|
|
4
|
+
chainType: UnisatChainType;
|
|
5
|
+
}
|
|
6
|
+
export interface WalletInformation {
|
|
7
|
+
name: string;
|
|
8
|
+
icon: string;
|
|
9
|
+
isInstalled: boolean;
|
|
10
|
+
isConnected: boolean;
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { errorDefinitions } from './definitions';
|
|
2
|
+
export type ErrorTranslations = {
|
|
3
|
+
en: ErrorMessage;
|
|
4
|
+
'en-US'?: ErrorMessage;
|
|
5
|
+
fr?: ErrorMessage;
|
|
6
|
+
'fr-CA'?: ErrorMessage;
|
|
7
|
+
};
|
|
8
|
+
type ErrorMessage = (typeof errorDefinitions)[keyof typeof errorDefinitions];
|
|
9
|
+
type ErrorMessages = ErrorMessage | ErrorTranslations;
|
|
10
|
+
export declare const patternMap: Array<[string, string, ErrorMessages]>;
|
|
11
|
+
export declare const escapeKey: (key: string) => string;
|
|
12
|
+
export declare const patternRegExp: Array<[string, RegExp, ErrorMessages]>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Unisat, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
2
|
+
import { WalletConnectNetwork } from '../types';
|
|
3
|
+
import { SupportedWallets } from './index';
|
|
4
|
+
import type { ControllerConnectAccounts, ControllerErrorResponse, ControllerResponse, WalletConnectWallet } from './types.ts';
|
|
5
|
+
declare class WalletController {
|
|
6
|
+
private static wallets;
|
|
7
|
+
private static currentWallet;
|
|
8
|
+
static getWallets: () => WalletConnectWallet[];
|
|
9
|
+
static isWalletInstalled(wallet: string): boolean;
|
|
10
|
+
static getWalletType(): SupportedWallets | null;
|
|
11
|
+
static getProvider(): Unisat | null;
|
|
12
|
+
static getSigner(): Promise<UnisatSigner | null>;
|
|
13
|
+
static convertChainTypeToNetwork(chainType: UnisatChainType): WalletConnectNetwork;
|
|
14
|
+
static getNetwork(): Promise<WalletConnectNetwork>;
|
|
15
|
+
static getPublicKey(): Promise<string | null>;
|
|
16
|
+
static canAutoConnect(walletName: string): Promise<boolean>;
|
|
17
|
+
static connect(walletName: string): Promise<ControllerResponse<ControllerConnectAccounts | ControllerErrorResponse>>;
|
|
18
|
+
static disconnectIfWalletChanged(newWallet: WalletConnectWallet): Promise<void>;
|
|
19
|
+
static disconnect(): Promise<void>;
|
|
20
|
+
static setAccountsChangedHook(fn: (accounts: string[]) => void): void;
|
|
21
|
+
static setDisconnectHook(fn: () => void): void;
|
|
22
|
+
static setChainChangedHook(fn: (network: WalletConnectNetwork) => void): void;
|
|
23
|
+
static removeAccountsChangedHook(): void;
|
|
24
|
+
static removeDisconnectHook(): void;
|
|
25
|
+
static removeChainChangedHook(): void;
|
|
26
|
+
static registerWallet: (wallet: WalletConnectWallet) => void;
|
|
27
|
+
static unbindHooks(): void;
|
|
28
|
+
}
|
|
29
|
+
export { WalletController };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Unisat, UnisatChainType } from '@btc-vision/transaction';
|
|
2
|
+
import type { WalletBase } from '../types.ts';
|
|
3
|
+
declare class OPWallet implements WalletBase {
|
|
4
|
+
private walletBase;
|
|
5
|
+
private accountsChangedHookWrapper?;
|
|
6
|
+
private chainChangedHookWrapper?;
|
|
7
|
+
private disconnectHookWrapper?;
|
|
8
|
+
private _isConnected;
|
|
9
|
+
isInstalled(): boolean;
|
|
10
|
+
isConnected(): boolean;
|
|
11
|
+
canAutoConnect(): Promise<boolean>;
|
|
12
|
+
getChainId(): void;
|
|
13
|
+
connect(): Promise<string[]>;
|
|
14
|
+
disconnect(): Promise<void>;
|
|
15
|
+
getProvider(): Unisat | null;
|
|
16
|
+
getSigner(): Promise<null>;
|
|
17
|
+
getPublicKey(): Promise<string>;
|
|
18
|
+
getNetwork(): Promise<UnisatChainType>;
|
|
19
|
+
setAccountsChangedHook(fn: (accounts: string[]) => void): void;
|
|
20
|
+
removeAccountsChangedHook(): void;
|
|
21
|
+
setDisconnectHook(fn: () => void): void;
|
|
22
|
+
removeDisconnectHook(): void;
|
|
23
|
+
setChainChangedHook(fn: (chainType: UnisatChainType) => void): void;
|
|
24
|
+
removeChainChangedHook(): void;
|
|
25
|
+
}
|
|
26
|
+
export default OPWallet;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Unisat, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
2
|
+
import { SupportedWallets } from './index';
|
|
3
|
+
export type { AbstractRpcProvider } from 'opnet';
|
|
4
|
+
export interface WalletBase {
|
|
5
|
+
isInstalled(): boolean;
|
|
6
|
+
isConnected(): boolean;
|
|
7
|
+
canAutoConnect(): Promise<boolean>;
|
|
8
|
+
getProvider(): Unisat | null;
|
|
9
|
+
getSigner(): Promise<UnisatSigner | null>;
|
|
10
|
+
connect(): Promise<string[] | undefined>;
|
|
11
|
+
disconnect(): Promise<void>;
|
|
12
|
+
getPublicKey(): Promise<string | null>;
|
|
13
|
+
getNetwork(): Promise<UnisatChainType>;
|
|
14
|
+
setAccountsChangedHook(fn: (accounts: string[]) => void): void;
|
|
15
|
+
removeAccountsChangedHook(): void;
|
|
16
|
+
setDisconnectHook(fn: () => void): void;
|
|
17
|
+
removeDisconnectHook(): void;
|
|
18
|
+
setChainChangedHook(fn: (network: UnisatChainType) => void): void;
|
|
19
|
+
removeChainChangedHook(): void;
|
|
20
|
+
getChainId(): void;
|
|
21
|
+
}
|
|
22
|
+
export interface WalletConnectWallet {
|
|
23
|
+
name: SupportedWallets;
|
|
24
|
+
icon: string;
|
|
25
|
+
controller: WalletBase;
|
|
26
|
+
}
|
|
27
|
+
export interface ControllerResponse<T> {
|
|
28
|
+
code: number;
|
|
29
|
+
data?: T;
|
|
30
|
+
}
|
|
31
|
+
export interface ControllerErrorResponse {
|
|
32
|
+
message: string;
|
|
33
|
+
}
|
|
34
|
+
export type ControllerConnectAccounts = string[];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Unisat, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
2
|
+
import type { WalletBase } from '../types.ts';
|
|
3
|
+
declare class UnisatWallet implements WalletBase {
|
|
4
|
+
private walletBase;
|
|
5
|
+
private accountsChangedHookWrapper?;
|
|
6
|
+
private chainChangedHookWrapper?;
|
|
7
|
+
private disconnectHookWrapper?;
|
|
8
|
+
private _isConnected;
|
|
9
|
+
isInstalled(): boolean;
|
|
10
|
+
isConnected(): boolean;
|
|
11
|
+
canAutoConnect(): Promise<boolean>;
|
|
12
|
+
getProvider(): Unisat | null;
|
|
13
|
+
getSigner(): Promise<UnisatSigner>;
|
|
14
|
+
getChainId(): void;
|
|
15
|
+
connect(): Promise<string[]>;
|
|
16
|
+
disconnect(): Promise<void>;
|
|
17
|
+
getPublicKey(): Promise<string>;
|
|
18
|
+
getNetwork(): Promise<UnisatChainType>;
|
|
19
|
+
setAccountsChangedHook(fn: (accounts: string[]) => void): void;
|
|
20
|
+
removeAccountsChangedHook(): void;
|
|
21
|
+
setDisconnectHook(fn: () => void): void;
|
|
22
|
+
removeDisconnectHook(): void;
|
|
23
|
+
setChainChangedHook(fn: (chainType: UnisatChainType) => void): void;
|
|
24
|
+
removeChainChangedHook(): void;
|
|
25
|
+
}
|
|
26
|
+
export default UnisatWallet;
|
package/build/consts.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Address, Unisat, UnisatSigner } from '@btc-vision/transaction';
|
|
2
|
+
import type { WalletConnectNetwork, WalletInformation } from '../types.ts';
|
|
3
|
+
import { SupportedWallets } from '../wallets';
|
|
4
|
+
export type WalletConnectContextType = {
|
|
5
|
+
allWallets: WalletInformation[];
|
|
6
|
+
walletType: string | null;
|
|
7
|
+
walletAddress: string | null;
|
|
8
|
+
network: WalletConnectNetwork;
|
|
9
|
+
publicKey: string | null;
|
|
10
|
+
address: Address | null;
|
|
11
|
+
openConnectModal: () => void;
|
|
12
|
+
connectToWallet: (wallet: SupportedWallets) => void;
|
|
13
|
+
connecting: boolean;
|
|
14
|
+
disconnect: () => void;
|
|
15
|
+
provider: Unisat | null;
|
|
16
|
+
signer: UnisatSigner | null;
|
|
17
|
+
};
|
|
18
|
+
export declare const WalletConnectContext: import("react").Context<WalletConnectContextType | undefined>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { WalletConnectContext, } from '../context/WalletConnectContext';
|
|
3
|
+
export const useWalletConnect = () => {
|
|
4
|
+
const context = useContext(WalletConnectContext);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error('useCore must be used within a UserProvider');
|
|
7
|
+
}
|
|
8
|
+
return context;
|
|
9
|
+
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { WalletConnectContext } from './context/WalletConnectContext';
|
|
2
|
+
export { useWalletConnect } from './hooks/WalletConnectHook';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export { default as WalletConnectProvider } from './provider/WalletConnectProvider';
|
package/build/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { WalletConnectContext } from './context/WalletConnectContext';
|
|
2
|
+
export { useWalletConnect } from './hooks/WalletConnectHook';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export { default as WalletConnectProvider } from './provider/WalletConnectProvider';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React, { type ReactNode } from 'react';
|
|
2
|
+
import '../utils/style.css';
|
|
3
|
+
import '../utils/theme.css';
|
|
4
|
+
interface WalletConnectProviderProps {
|
|
5
|
+
theme?: 'light' | 'dark' | 'moto';
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
declare const WalletConnectProvider: React.FC<WalletConnectProviderProps>;
|
|
9
|
+
export default WalletConnectProvider;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Address } from '@btc-vision/transaction';
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { DefaultWalletConnectNetwork } from '../consts';
|
|
5
|
+
import { WalletConnectContext } from '../context/WalletConnectContext';
|
|
6
|
+
import '../utils/style.css';
|
|
7
|
+
import '../utils/theme.css';
|
|
8
|
+
import { WalletController } from '../wallets';
|
|
9
|
+
const AUTO_RECONNECT_RETRIES = 5;
|
|
10
|
+
const WalletConnectProvider = ({ theme, children }) => {
|
|
11
|
+
const [connectError, setConnectError] = useState(undefined);
|
|
12
|
+
const timeoutRef = useRef(null);
|
|
13
|
+
const [network, setNetwork] = useState(DefaultWalletConnectNetwork);
|
|
14
|
+
const [supportedWallets] = useState(WalletController.getWallets);
|
|
15
|
+
const [selectedWallet, setSelectedWallet] = useState(() => localStorage.getItem('WC_SelectedWallet') || null);
|
|
16
|
+
const [connecting, setConnecting] = useState(false);
|
|
17
|
+
const [modalOpen, setModalOpen] = useState(false);
|
|
18
|
+
const [walletAddress, setWalletAddress] = useState(null);
|
|
19
|
+
const [publicKey, setPublicKey] = useState(null);
|
|
20
|
+
const [walletType, setWalletType] = useState(null);
|
|
21
|
+
const [provider, setProvider] = useState(null);
|
|
22
|
+
const [signer, setSigner] = useState(null);
|
|
23
|
+
const clearConnectError = useCallback(() => {
|
|
24
|
+
if (timeoutRef.current)
|
|
25
|
+
clearTimeout(timeoutRef.current);
|
|
26
|
+
timeoutRef.current = setTimeout(() => setConnectError(undefined), 5000);
|
|
27
|
+
}, []);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (connectError) {
|
|
30
|
+
clearConnectError();
|
|
31
|
+
}
|
|
32
|
+
}, [connectError, clearConnectError]);
|
|
33
|
+
const openConnectModal = () => {
|
|
34
|
+
setConnectError(undefined);
|
|
35
|
+
setModalOpen(true);
|
|
36
|
+
};
|
|
37
|
+
const closeConnectModal = () => {
|
|
38
|
+
setModalOpen(false);
|
|
39
|
+
setConnectError(undefined);
|
|
40
|
+
};
|
|
41
|
+
const disconnect = useCallback(async () => {
|
|
42
|
+
console.log('DISCONNECTING FROM WALLET');
|
|
43
|
+
localStorage.removeItem('WC_SelectedWallet');
|
|
44
|
+
setSelectedWallet(null);
|
|
45
|
+
setPublicKey(null);
|
|
46
|
+
setWalletAddress(null);
|
|
47
|
+
setConnecting(false);
|
|
48
|
+
WalletController.removeDisconnectHook();
|
|
49
|
+
WalletController.removeChainChangedHook();
|
|
50
|
+
WalletController.removeAccountsChangedHook();
|
|
51
|
+
await WalletController.disconnect();
|
|
52
|
+
setNetwork(DefaultWalletConnectNetwork);
|
|
53
|
+
}, []);
|
|
54
|
+
const connectToWallet = useCallback(async (wallet) => {
|
|
55
|
+
setConnecting(true);
|
|
56
|
+
try {
|
|
57
|
+
const response = await WalletController.connect(wallet);
|
|
58
|
+
if (response.code === 200 && Array.isArray(response.data)) {
|
|
59
|
+
if (!response.data || response.data.length === 0) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
setWalletAddress(response.data[0]);
|
|
63
|
+
const publicKey = await WalletController.getPublicKey();
|
|
64
|
+
setPublicKey(publicKey);
|
|
65
|
+
const network = await WalletController.getNetwork();
|
|
66
|
+
setNetwork(network);
|
|
67
|
+
WalletController.setAccountsChangedHook(accountsChanged);
|
|
68
|
+
WalletController.setChainChangedHook(chainChanged);
|
|
69
|
+
WalletController.setDisconnectHook(disconnect);
|
|
70
|
+
closeConnectModal();
|
|
71
|
+
setSelectedWallet(wallet);
|
|
72
|
+
localStorage.setItem('WC_SelectedWallet', wallet);
|
|
73
|
+
}
|
|
74
|
+
else if (response.data && 'message' in response.data) {
|
|
75
|
+
setConnectError(response.data.message);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
setConnectError('Unknown error');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
setConnectError(err.message || 'Unexpected error');
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
setConnecting(false);
|
|
86
|
+
}
|
|
87
|
+
}, [disconnect]);
|
|
88
|
+
const attemptReconnect = useCallback(async () => {
|
|
89
|
+
console.warn('Trying to reconnect...', selectedWallet, connecting);
|
|
90
|
+
if (!selectedWallet || connecting)
|
|
91
|
+
return;
|
|
92
|
+
const canAutoConnect = await WalletController.canAutoConnect(selectedWallet);
|
|
93
|
+
console.log('CanAutoConnect', canAutoConnect);
|
|
94
|
+
if (!canAutoConnect)
|
|
95
|
+
return;
|
|
96
|
+
let attempts = 0;
|
|
97
|
+
const reconnect = async () => {
|
|
98
|
+
attempts++;
|
|
99
|
+
const walletAvailable = WalletController.isWalletInstalled(selectedWallet);
|
|
100
|
+
if (walletAvailable) {
|
|
101
|
+
console.log(`Attempting to reconnect to ${selectedWallet} (Attempt ${attempts})`);
|
|
102
|
+
await connectToWallet(selectedWallet);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (attempts < AUTO_RECONNECT_RETRIES) {
|
|
106
|
+
setTimeout(reconnect, 1000 * attempts);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
await reconnect();
|
|
110
|
+
}, [selectedWallet, connectToWallet]);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
void attemptReconnect();
|
|
113
|
+
}, [attemptReconnect]);
|
|
114
|
+
const accountsChanged = useCallback(async (accounts) => {
|
|
115
|
+
console.log('Accounts', accounts);
|
|
116
|
+
if (selectedWallet) {
|
|
117
|
+
const account = accounts.length > 0 ? accounts[0] : null;
|
|
118
|
+
setWalletAddress(account);
|
|
119
|
+
const publicKey = account ? await WalletController.getPublicKey() : null;
|
|
120
|
+
setPublicKey(publicKey);
|
|
121
|
+
}
|
|
122
|
+
}, [selectedWallet, setWalletAddress, setPublicKey]);
|
|
123
|
+
const chainChanged = useCallback((network) => {
|
|
124
|
+
if (selectedWallet) {
|
|
125
|
+
setNetwork(network);
|
|
126
|
+
}
|
|
127
|
+
}, [selectedWallet, setNetwork]);
|
|
128
|
+
const allWallets = useMemo(() => {
|
|
129
|
+
return supportedWallets.map((wallet) => {
|
|
130
|
+
return {
|
|
131
|
+
name: wallet.name,
|
|
132
|
+
icon: wallet.icon,
|
|
133
|
+
isInstalled: wallet.controller.isInstalled(),
|
|
134
|
+
isConnected: wallet.controller.isConnected(),
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}, [supportedWallets, network]);
|
|
138
|
+
const availableWallets = useMemo(() => {
|
|
139
|
+
return supportedWallets.filter((wallet) => wallet.controller.isInstalled());
|
|
140
|
+
}, [supportedWallets, network]);
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
const walletType = walletAddress ? WalletController.getWalletType() : null;
|
|
143
|
+
setWalletType(walletType);
|
|
144
|
+
const provider = walletAddress ? WalletController.getProvider() : null;
|
|
145
|
+
setProvider(provider);
|
|
146
|
+
}, [walletAddress]);
|
|
147
|
+
useEffect(() => {
|
|
148
|
+
const updateSigner = async () => {
|
|
149
|
+
const signer = publicKey ? await WalletController.getSigner() : null;
|
|
150
|
+
setSigner(signer);
|
|
151
|
+
};
|
|
152
|
+
void updateSigner();
|
|
153
|
+
}, [network, publicKey]);
|
|
154
|
+
const currentTheme = useMemo(() => {
|
|
155
|
+
const currentTheme = theme || 'light';
|
|
156
|
+
return `wallet-connect-${currentTheme}-theme`;
|
|
157
|
+
}, [theme]);
|
|
158
|
+
const address = useMemo(() => {
|
|
159
|
+
return publicKey ? Address.fromString(publicKey) : null;
|
|
160
|
+
}, [publicKey]);
|
|
161
|
+
return (_jsxs(WalletConnectContext.Provider, { value: {
|
|
162
|
+
walletAddress,
|
|
163
|
+
publicKey,
|
|
164
|
+
address,
|
|
165
|
+
connecting,
|
|
166
|
+
connectToWallet,
|
|
167
|
+
disconnect,
|
|
168
|
+
openConnectModal,
|
|
169
|
+
network,
|
|
170
|
+
allWallets,
|
|
171
|
+
provider,
|
|
172
|
+
signer,
|
|
173
|
+
walletType,
|
|
174
|
+
}, children: [children, modalOpen && (_jsx("div", { className: `wallet-connect-modal-backdrop ${currentTheme}`, children: _jsxs("div", { className: "wallet-connect-modal", role: "dialog", "aria-modal": "true", "aria-labelledby": "wallet-connect-modal-title", children: [_jsxs("div", { className: "wallet-connect-header", children: [_jsx("span", { children: "Connect Wallet" }), _jsx("button", { className: "close", onClick: () => closeConnectModal(), children: _jsx("span", { className: "close-icon", children: _jsx("svg", { width: "30px", height: "30px", viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", children: _jsx("path", { className: "close-x-path", d: "M16 8L8 16M8.00001 8L16 16", stroke: "#fff", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) }) }) })] }), connectError && (_jsx("div", { className: "wallet-connect-error", children: _jsx("p", { className: "error-message", children: connectError }) })), availableWallets.length > 0 ? (_jsx("div", { className: "wallet-list", children: availableWallets.map((wallet) => (_jsxs("button", { onClick: () => connectToWallet(wallet.name), disabled: connecting || !wallet.controller.isInstalled(), className: `wallet-button ${wallet.controller.isInstalled()
|
|
175
|
+
? 'wallet-installed'
|
|
176
|
+
: 'wallet-not-installed'}`, children: [wallet.icon ? (_jsx("div", { className: "wallet-icon", title: wallet.name, children: _jsx("img", { src: wallet.icon, alt: wallet.name }) })) : (_jsx("div", { className: "wallet-name", children: wallet.name })), wallet.controller.isConnected() ? (_jsx("div", { className: "wallet-connected", children: "(Connected)" })) : (_jsx(_Fragment, {})), wallet.controller.isInstalled() ? (_jsx(_Fragment, {})) : (_jsx("div", { className: "wallet-not-installed", children: "(Not Installed)" }))] }, wallet.name))) })) : (_jsxs("div", { children: [_jsx("p", { children: "No wallets available" }), _jsx("p", { children: "Supporting the following wallets" }), _jsx("div", { className: "wallet-list", children: supportedWallets.map((wallet) => (_jsx("a", { href: `https://chromewebstore.google.com/search/${wallet.name}`, children: wallet.icon ? (_jsx("div", { className: "wallet-icon", title: wallet.name, children: _jsx("img", { src: wallet.icon, alt: wallet.name }) })) : (_jsx("div", { className: "wallet-name", children: wallet.name })) }))) })] }))] }) }))] }));
|
|
177
|
+
};
|
|
178
|
+
export default WalletConnectProvider;
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Network } from '@btc-vision/bitcoin';
|
|
2
|
+
import { UnisatChainType } from '@btc-vision/transaction';
|
|
3
|
+
export interface WalletConnectNetwork extends Network {
|
|
4
|
+
chainType: UnisatChainType;
|
|
5
|
+
}
|
|
6
|
+
export interface WalletInformation {
|
|
7
|
+
name: string;
|
|
8
|
+
icon: string;
|
|
9
|
+
isInstalled: boolean;
|
|
10
|
+
isConnected: boolean;
|
|
11
|
+
}
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const genericErrors = {
|
|
2
|
+
UserRejected: 'Wallet Dialog was closed by the user',
|
|
3
|
+
UnknownError: 'Unknown error occurred',
|
|
4
|
+
IndexingInProgress: 'Node is still indexing. Please try again shortly',
|
|
5
|
+
};
|
|
6
|
+
export const errorDefinitions = {
|
|
7
|
+
...genericErrors,
|
|
8
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { patternMap, patternRegExp } from './patterns';
|
|
2
|
+
const RE_EXTRACT_ERROR = new RegExp('^(.*?):\\s*(?:([^:]+):)?\\s*(.*?)\\s+at\\s(\\S+\\s\\()?\\S+\\d+:\\d+\\)?', 'm');
|
|
3
|
+
const _translation = (err, locale) => {
|
|
4
|
+
switch (locale) {
|
|
5
|
+
case 'en':
|
|
6
|
+
return err['en'] || '';
|
|
7
|
+
case 'en-us':
|
|
8
|
+
return err['en-US'] || '';
|
|
9
|
+
case 'fr':
|
|
10
|
+
return err['fr'] || '';
|
|
11
|
+
case 'fr-ca':
|
|
12
|
+
return err['fr-CA'] || '';
|
|
13
|
+
default:
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const _errors = (err, locales) => {
|
|
18
|
+
if (typeof err === 'string') {
|
|
19
|
+
return err;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
for (const locale of locales) {
|
|
23
|
+
const translation = _translation(err, locale);
|
|
24
|
+
if (translation)
|
|
25
|
+
return translation;
|
|
26
|
+
}
|
|
27
|
+
return err['en'];
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const _normalizeLocales = (locales) => {
|
|
31
|
+
const _locales = locales.map((l) => l.toLowerCase().trim());
|
|
32
|
+
for (const locale of _locales) {
|
|
33
|
+
const language = locale.split('-')[0];
|
|
34
|
+
if (!_locales.includes(language)) {
|
|
35
|
+
_locales.push(language);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return _locales;
|
|
39
|
+
};
|
|
40
|
+
export const _e = (err, locales = ['en']) => {
|
|
41
|
+
const [, , error] = _match_e(err.toString(), locales);
|
|
42
|
+
return error;
|
|
43
|
+
};
|
|
44
|
+
export const _match_e = (err, locales = ['en']) => {
|
|
45
|
+
const normalizedLocales = _normalizeLocales(locales);
|
|
46
|
+
const matches = RE_EXTRACT_ERROR.exec(err);
|
|
47
|
+
const packageName = matches ? matches[2] : '';
|
|
48
|
+
let msg = matches ? matches[3] : err;
|
|
49
|
+
const lookup = `${packageName}: ${msg}`.trim();
|
|
50
|
+
const lookupLower = lookup.toLowerCase();
|
|
51
|
+
for (const [orig, pattern, translation] of patternRegExp) {
|
|
52
|
+
const placeHolders = pattern.exec(lookup);
|
|
53
|
+
if (placeHolders) {
|
|
54
|
+
msg = _errors(translation, normalizedLocales) || msg;
|
|
55
|
+
for (let i = 0; i < placeHolders.length; i++) {
|
|
56
|
+
const regex = new RegExp(`\\$${i}`, 'g');
|
|
57
|
+
msg = msg.replace(regex, placeHolders[i] ?? '');
|
|
58
|
+
}
|
|
59
|
+
return [orig, pattern.source, msg];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (const [orig, pattern, translation] of patternMap) {
|
|
63
|
+
if (lookupLower == pattern) {
|
|
64
|
+
return [orig, pattern, _errors(translation, normalizedLocales) || msg];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const [orig, pattern, translation] of patternMap) {
|
|
68
|
+
if (lookupLower.includes(pattern)) {
|
|
69
|
+
return [orig, pattern, _errors(translation, normalizedLocales) || msg];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return ['', '', msg];
|
|
73
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { errorDefinitions } from './definitions';
|
|
2
|
+
export type ErrorTranslations = {
|
|
3
|
+
en: ErrorMessage;
|
|
4
|
+
'en-US'?: ErrorMessage;
|
|
5
|
+
fr?: ErrorMessage;
|
|
6
|
+
'fr-CA'?: ErrorMessage;
|
|
7
|
+
};
|
|
8
|
+
type ErrorMessage = (typeof errorDefinitions)[keyof typeof errorDefinitions];
|
|
9
|
+
type ErrorMessages = ErrorMessage | ErrorTranslations;
|
|
10
|
+
export declare const patternMap: Array<[string, string, ErrorMessages]>;
|
|
11
|
+
export declare const escapeKey: (key: string) => string;
|
|
12
|
+
export declare const patternRegExp: Array<[string, RegExp, ErrorMessages]>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { errorDefinitions } from './definitions';
|
|
2
|
+
const rawPatternGeneric = {
|
|
3
|
+
'User rejected the request.': errorDefinitions.UserRejected,
|
|
4
|
+
"TypeError: Cannot read properties of undefined (reading 'addresses')": errorDefinitions.UserRejected,
|
|
5
|
+
};
|
|
6
|
+
const rawPatternUnitTest = {
|
|
7
|
+
'TEST_CASE: empty': '',
|
|
8
|
+
'TEST_CASE: simple': 'simple',
|
|
9
|
+
'TEST_CASE: regex1 ${value}': 'regex1',
|
|
10
|
+
'TEST_CASE: regex2 ${value}': 'regex2 $1',
|
|
11
|
+
'Test case empty': '',
|
|
12
|
+
'Test case simple': 'simple',
|
|
13
|
+
'Test case regex1 ${value}': 'regex1',
|
|
14
|
+
'Test case regex2 ${value.get()}': 'regex2 $1',
|
|
15
|
+
'TEST_MULTI: multi1': {
|
|
16
|
+
en: 'Test English',
|
|
17
|
+
'en-US': 'Test American English',
|
|
18
|
+
fr: 'Test French',
|
|
19
|
+
'fr-CA': 'Test Canadian French',
|
|
20
|
+
},
|
|
21
|
+
'TEST_MULTI: multi2 ${value}': {
|
|
22
|
+
en: 'Test English: $1',
|
|
23
|
+
'en-US': 'Test American English: $1',
|
|
24
|
+
fr: 'Test French: $1',
|
|
25
|
+
'fr-CA': 'Test Canadian French: $1',
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const rawPatternMap = {
|
|
29
|
+
'NATIVE_SWAP: Liquidity value is too low in satoshis.': errorDefinitions.NativeSwapLiquidityValueTooLow,
|
|
30
|
+
'Unknown Error': errorDefinitions.UnknownError,
|
|
31
|
+
'Invalid Address': errorDefinitions.IndexingInProgress,
|
|
32
|
+
'Error Indexing at Block': errorDefinitions.IndexingInProgress,
|
|
33
|
+
'Valid Reservation for this Address': errorDefinitions.NoValidReservation,
|
|
34
|
+
...rawPatternGeneric,
|
|
35
|
+
...rawPatternUnitTest,
|
|
36
|
+
};
|
|
37
|
+
export const patternMap = Object.entries(rawPatternMap)
|
|
38
|
+
.filter(([key, value]) => !!value && !key.includes('${'))
|
|
39
|
+
.sort((a, b) => b[0].length - a[0].length)
|
|
40
|
+
.map(([key, value]) => [key, key.toLowerCase(), value]);
|
|
41
|
+
export const escapeKey = (key) => {
|
|
42
|
+
return key
|
|
43
|
+
.replace(/\(/g, '\\(')
|
|
44
|
+
.replace(/\)/g, '\\)')
|
|
45
|
+
.replace(/\./g, '\\.')
|
|
46
|
+
.replace(/\$\{.*?}/g, '(.+)');
|
|
47
|
+
};
|
|
48
|
+
export const patternRegExp = Object.entries(rawPatternMap)
|
|
49
|
+
.filter(([key, value]) => !!value && key.includes('${'))
|
|
50
|
+
.sort((a, b) => b[0].length - a[0].length)
|
|
51
|
+
.map(([key, value]) => [key, new RegExp(escapeKey(key), 'i'), value]);
|