@btc-vision/walletconnect 1.5.0 → 1.5.2
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 +10 -5
- 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
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { networks } from '@btc-vision/bitcoin';
|
|
2
|
-
import { Address, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
3
|
-
import { JSONRpcProvider } from 'opnet';
|
|
4
|
-
export var SupportedWallets;
|
|
5
|
-
(function (SupportedWallets) {
|
|
6
|
-
SupportedWallets["OP_WALLET"] = "op_wallet";
|
|
7
|
-
SupportedWallets["UNISAT"] = "unisat";
|
|
8
|
-
})(SupportedWallets || (SupportedWallets = {}));
|
|
9
|
-
export class WalletConnection {
|
|
10
|
-
constructor() {
|
|
11
|
-
this.signer = null;
|
|
12
|
-
this.walletType = null;
|
|
13
|
-
this.walletWindowInstance = null;
|
|
14
|
-
}
|
|
15
|
-
async connect(walletType) {
|
|
16
|
-
if (this.walletType === walletType)
|
|
17
|
-
return;
|
|
18
|
-
if (this.walletType)
|
|
19
|
-
this.disconnect();
|
|
20
|
-
switch (walletType) {
|
|
21
|
-
case SupportedWallets.OP_WALLET: {
|
|
22
|
-
if (window.opnet) {
|
|
23
|
-
try {
|
|
24
|
-
await window.opnet.requestAccounts();
|
|
25
|
-
this.walletType = walletType;
|
|
26
|
-
this.walletWindowInstance = window.opnet;
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
if (!error)
|
|
31
|
-
return;
|
|
32
|
-
if (error instanceof Error)
|
|
33
|
-
throw new Error(error.message);
|
|
34
|
-
if (typeof error === 'object' &&
|
|
35
|
-
'message' in error &&
|
|
36
|
-
typeof error.message === 'string') {
|
|
37
|
-
throw new Error(error.message);
|
|
38
|
-
}
|
|
39
|
-
console.warn(error);
|
|
40
|
-
throw new Error(`Unknown error, check console`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
throw new Error('OP_WALLET not found');
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
case SupportedWallets.UNISAT: {
|
|
48
|
-
if (window.unisat) {
|
|
49
|
-
try {
|
|
50
|
-
await window.unisat.requestAccounts();
|
|
51
|
-
const signer = new UnisatSigner();
|
|
52
|
-
await signer.init();
|
|
53
|
-
this.signer = signer;
|
|
54
|
-
this.walletType = walletType;
|
|
55
|
-
this.walletWindowInstance = window.unisat;
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
if (!error)
|
|
60
|
-
return;
|
|
61
|
-
if (error instanceof Error)
|
|
62
|
-
throw new Error(error.message);
|
|
63
|
-
if (typeof error === 'object' &&
|
|
64
|
-
'message' in error &&
|
|
65
|
-
typeof error.message === 'string')
|
|
66
|
-
throw new Error(error.message);
|
|
67
|
-
console.error(error);
|
|
68
|
-
throw new Error(`Unknown error, check console`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
throw new Error('Unisat not found');
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
default:
|
|
76
|
-
throw new Error('Unsupported wallet');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
disconnect() {
|
|
80
|
-
if (!this.walletWindowInstance)
|
|
81
|
-
return;
|
|
82
|
-
if (this.walletType === SupportedWallets.OP_WALLET ||
|
|
83
|
-
this.walletType === SupportedWallets.UNISAT)
|
|
84
|
-
this.walletWindowInstance.disconnect();
|
|
85
|
-
this.signer = null;
|
|
86
|
-
this.walletType = null;
|
|
87
|
-
this.walletWindowInstance = null;
|
|
88
|
-
}
|
|
89
|
-
async getAddress() {
|
|
90
|
-
if (!this.walletWindowInstance)
|
|
91
|
-
throw new Error('Wallet not connected');
|
|
92
|
-
if (this.walletType === SupportedWallets.OP_WALLET ||
|
|
93
|
-
this.walletType === SupportedWallets.UNISAT) {
|
|
94
|
-
const publicKey = await this.walletWindowInstance.getPublicKey();
|
|
95
|
-
return Address.fromString(publicKey);
|
|
96
|
-
}
|
|
97
|
-
throw new Error('Unsupported wallet');
|
|
98
|
-
}
|
|
99
|
-
async getAddressTyped() {
|
|
100
|
-
if (!this.walletWindowInstance)
|
|
101
|
-
throw new Error('Wallet not connected');
|
|
102
|
-
if (this.walletType === SupportedWallets.OP_WALLET ||
|
|
103
|
-
this.walletType === SupportedWallets.UNISAT) {
|
|
104
|
-
const accounts = await this.walletWindowInstance.getAccounts();
|
|
105
|
-
return accounts[0];
|
|
106
|
-
}
|
|
107
|
-
throw new Error('Unsupported wallet');
|
|
108
|
-
}
|
|
109
|
-
async getNetwork() {
|
|
110
|
-
if (!this.walletWindowInstance)
|
|
111
|
-
throw new Error('Wallet not connected');
|
|
112
|
-
if (this.walletType === SupportedWallets.OP_WALLET ||
|
|
113
|
-
this.walletType === SupportedWallets.UNISAT) {
|
|
114
|
-
const chain = await this.walletWindowInstance.getChain();
|
|
115
|
-
switch (chain.enum) {
|
|
116
|
-
case UnisatChainType.BITCOIN_MAINNET:
|
|
117
|
-
return networks.bitcoin;
|
|
118
|
-
case UnisatChainType.BITCOIN_TESTNET:
|
|
119
|
-
return networks.testnet;
|
|
120
|
-
case UnisatChainType.BITCOIN_REGTEST:
|
|
121
|
-
return networks.regtest;
|
|
122
|
-
case UnisatChainType.FRACTAL_BITCOIN_MAINNET:
|
|
123
|
-
return networks.bitcoin;
|
|
124
|
-
case UnisatChainType.FRACTAL_BITCOIN_TESTNET:
|
|
125
|
-
return networks.testnet;
|
|
126
|
-
default:
|
|
127
|
-
throw new Error('Unsupported network');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
throw new Error('Unsupported wallet');
|
|
131
|
-
}
|
|
132
|
-
async getProvider() {
|
|
133
|
-
if (!this.walletWindowInstance)
|
|
134
|
-
throw new Error('Wallet not connected');
|
|
135
|
-
if (this.walletType === SupportedWallets.OP_WALLET ||
|
|
136
|
-
this.walletType === SupportedWallets.UNISAT) {
|
|
137
|
-
const chain = await this.walletWindowInstance.getChain();
|
|
138
|
-
switch (chain.enum) {
|
|
139
|
-
case UnisatChainType.BITCOIN_MAINNET:
|
|
140
|
-
return new JSONRpcProvider('https://api.opnet.org', networks.bitcoin);
|
|
141
|
-
case UnisatChainType.BITCOIN_TESTNET:
|
|
142
|
-
return new JSONRpcProvider('https://testnet.opnet.org', networks.testnet);
|
|
143
|
-
case UnisatChainType.BITCOIN_REGTEST:
|
|
144
|
-
return new JSONRpcProvider('https://regtest.opnet.org', networks.regtest);
|
|
145
|
-
default:
|
|
146
|
-
throw new Error('Unsupported network');
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
throw new Error('Unsupported wallet');
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
export default WalletConnection;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Network } from '@btc-vision/bitcoin';
|
|
2
|
-
import { Address } from '@btc-vision/transaction';
|
|
3
|
-
import { AbstractRpcProvider } from 'opnet';
|
|
4
|
-
import { ReactNode } from 'react';
|
|
5
|
-
import { Signers, SupportedWallets, Wallets } from './WalletConnection';
|
|
6
|
-
export interface Account {
|
|
7
|
-
isConnected: boolean;
|
|
8
|
-
signer: Signers | null;
|
|
9
|
-
address: Address;
|
|
10
|
-
addressTyped: string;
|
|
11
|
-
network: Network;
|
|
12
|
-
provider: AbstractRpcProvider;
|
|
13
|
-
}
|
|
14
|
-
interface WalletContextType {
|
|
15
|
-
connect: (wallet: SupportedWallets, signal?: AbortSignal) => Promise<void>;
|
|
16
|
-
disconnect: () => void;
|
|
17
|
-
walletType: SupportedWallets | null;
|
|
18
|
-
walletWindowInstance: Wallets | null;
|
|
19
|
-
account: Account | null;
|
|
20
|
-
}
|
|
21
|
-
export declare const WalletProvider: ({ children }: {
|
|
22
|
-
children: ReactNode;
|
|
23
|
-
}) => import("react/jsx-runtime").JSX.Element;
|
|
24
|
-
export declare const useWallet: () => WalletContextType;
|
|
25
|
-
export {};
|
package/build/WalletProvider.js
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useEffect, useRef, useState, } from 'react';
|
|
3
|
-
import WalletConnection, { SupportedWallets } from './WalletConnection';
|
|
4
|
-
const WalletContext = createContext(undefined);
|
|
5
|
-
const MAX_RETRIES = 10;
|
|
6
|
-
const RETRY_DELAY_MS = 2000;
|
|
7
|
-
function useDocumentComplete(fn) {
|
|
8
|
-
const fired = useRef(false);
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
if (fired.current)
|
|
11
|
-
return;
|
|
12
|
-
const run = () => {
|
|
13
|
-
if (fired.current)
|
|
14
|
-
return;
|
|
15
|
-
fired.current = true;
|
|
16
|
-
fn();
|
|
17
|
-
};
|
|
18
|
-
if (document.readyState === 'complete') {
|
|
19
|
-
run();
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
const handler = () => {
|
|
23
|
-
if (document.readyState === 'complete') {
|
|
24
|
-
document.removeEventListener('readystatechange', handler);
|
|
25
|
-
run();
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
document.addEventListener('readystatechange', handler);
|
|
29
|
-
return () => document.removeEventListener('readystatechange', handler);
|
|
30
|
-
}, [fn]);
|
|
31
|
-
}
|
|
32
|
-
export const WalletProvider = ({ children }) => {
|
|
33
|
-
const [walletConnection] = useState(() => new WalletConnection());
|
|
34
|
-
const [walletType, setWalletType] = useState(null);
|
|
35
|
-
const [walletWindowInstance, setWalletWindowInstance] = useState(null);
|
|
36
|
-
const [account, setAccount] = useState(null);
|
|
37
|
-
const listeners = useRef({});
|
|
38
|
-
const disconnect = useCallback(() => {
|
|
39
|
-
const inst = walletWindowInstance;
|
|
40
|
-
if (inst) {
|
|
41
|
-
if (listeners.current.disconnect) {
|
|
42
|
-
inst.removeListener?.('disconnect', listeners.current.disconnect);
|
|
43
|
-
listeners.current.disconnect = undefined;
|
|
44
|
-
}
|
|
45
|
-
if (listeners.current.accountsChanged) {
|
|
46
|
-
inst.removeListener?.('accountsChanged', listeners.current.accountsChanged);
|
|
47
|
-
listeners.current.accountsChanged = undefined;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
walletConnection.disconnect();
|
|
51
|
-
setWalletType(null);
|
|
52
|
-
setWalletWindowInstance(null);
|
|
53
|
-
localStorage.removeItem('walletType');
|
|
54
|
-
setAccount(null);
|
|
55
|
-
}, [walletConnection, walletWindowInstance]);
|
|
56
|
-
const connect = useCallback(async (type, signal) => {
|
|
57
|
-
let attempt = 0;
|
|
58
|
-
const throwIfAborted = () => {
|
|
59
|
-
if (signal?.aborted)
|
|
60
|
-
throw new DOMException('Aborted', 'AbortError');
|
|
61
|
-
};
|
|
62
|
-
while (attempt < MAX_RETRIES) {
|
|
63
|
-
throwIfAborted();
|
|
64
|
-
try {
|
|
65
|
-
await walletConnection.connect(type);
|
|
66
|
-
if ((walletConnection.walletType !== SupportedWallets.OP_WALLET &&
|
|
67
|
-
!walletConnection.signer) ||
|
|
68
|
-
!walletConnection.walletWindowInstance) {
|
|
69
|
-
throw new Error('Wallet not fully loaded yet');
|
|
70
|
-
}
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
catch (err) {
|
|
74
|
-
attempt += 1;
|
|
75
|
-
if (attempt >= MAX_RETRIES) {
|
|
76
|
-
console.warn(`Failed to connect after ${MAX_RETRIES} attempts.`, err);
|
|
77
|
-
disconnect();
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
console.warn(`Connection attempt ${attempt} failed:`, err.message);
|
|
81
|
-
try {
|
|
82
|
-
await new Promise((res, rej) => {
|
|
83
|
-
const t = setTimeout(res, RETRY_DELAY_MS);
|
|
84
|
-
signal?.addEventListener('abort', () => {
|
|
85
|
-
clearTimeout(t);
|
|
86
|
-
rej(new DOMException('Aborted', 'AbortError'));
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
catch {
|
|
91
|
-
console.debug('Connection aborted during retry delay.');
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
throwIfAborted();
|
|
97
|
-
setWalletType(type);
|
|
98
|
-
setWalletWindowInstance(walletConnection.walletWindowInstance);
|
|
99
|
-
localStorage.setItem('walletType', type);
|
|
100
|
-
try {
|
|
101
|
-
const [signer, address, addressTyped, network, provider] = await Promise.all([
|
|
102
|
-
walletConnection.signer,
|
|
103
|
-
walletConnection.getAddress(),
|
|
104
|
-
walletConnection.getAddressTyped(),
|
|
105
|
-
walletConnection.getNetwork(),
|
|
106
|
-
walletConnection.getProvider(),
|
|
107
|
-
]);
|
|
108
|
-
setAccount({
|
|
109
|
-
isConnected: true,
|
|
110
|
-
signer,
|
|
111
|
-
address,
|
|
112
|
-
addressTyped,
|
|
113
|
-
network,
|
|
114
|
-
provider,
|
|
115
|
-
});
|
|
116
|
-
const instance = walletConnection.walletWindowInstance;
|
|
117
|
-
if (instance) {
|
|
118
|
-
const onDisconnect = () => disconnect();
|
|
119
|
-
const onAccountsChanged = async () => {
|
|
120
|
-
try {
|
|
121
|
-
const [updatedAddr, updatedAddrTyped, updatedNet, updatedProv] = await Promise.all([
|
|
122
|
-
walletConnection.getAddress(),
|
|
123
|
-
walletConnection.getAddressTyped(),
|
|
124
|
-
walletConnection.getNetwork(),
|
|
125
|
-
walletConnection.getProvider(),
|
|
126
|
-
]);
|
|
127
|
-
setAccount((prev) => prev
|
|
128
|
-
? {
|
|
129
|
-
...prev,
|
|
130
|
-
address: updatedAddr,
|
|
131
|
-
addressTyped: updatedAddrTyped,
|
|
132
|
-
network: updatedNet,
|
|
133
|
-
provider: updatedProv,
|
|
134
|
-
}
|
|
135
|
-
: prev);
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
disconnect();
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
listeners.current.disconnect = onDisconnect;
|
|
142
|
-
listeners.current.accountsChanged = onAccountsChanged;
|
|
143
|
-
instance.on('disconnect', onDisconnect);
|
|
144
|
-
instance.on('accountsChanged', onAccountsChanged);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
catch (err) {
|
|
148
|
-
console.warn('Unable to finalize wallet connection:', err);
|
|
149
|
-
disconnect();
|
|
150
|
-
}
|
|
151
|
-
}, [walletConnection, disconnect]);
|
|
152
|
-
useDocumentComplete(() => {
|
|
153
|
-
const stored = localStorage.getItem('walletType');
|
|
154
|
-
if (!stored)
|
|
155
|
-
return;
|
|
156
|
-
const controller = new AbortController();
|
|
157
|
-
connect(stored, controller.signal).catch((err) => {
|
|
158
|
-
if (err.name !== 'AbortError') {
|
|
159
|
-
console.warn('Failed to reconnect to wallet:', err);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
return () => controller.abort();
|
|
163
|
-
});
|
|
164
|
-
const ctx = {
|
|
165
|
-
connect,
|
|
166
|
-
disconnect,
|
|
167
|
-
walletType,
|
|
168
|
-
walletWindowInstance,
|
|
169
|
-
account,
|
|
170
|
-
};
|
|
171
|
-
return _jsx(WalletContext.Provider, { value: ctx, children: children });
|
|
172
|
-
};
|
|
173
|
-
export const useWallet = () => {
|
|
174
|
-
const ctx = useContext(WalletContext);
|
|
175
|
-
if (!ctx)
|
|
176
|
-
throw new Error('useWallet must be used within a WalletProvider');
|
|
177
|
-
return ctx;
|
|
178
|
-
};
|
package/src/WalletConnection.ts
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { Network, networks } from '@btc-vision/bitcoin';
|
|
2
|
-
import { Address, Unisat, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
3
|
-
import { AbstractRpcProvider, JSONRpcProvider } from 'opnet';
|
|
4
|
-
|
|
5
|
-
export enum SupportedWallets {
|
|
6
|
-
OP_WALLET = 'op_wallet',
|
|
7
|
-
UNISAT = 'unisat',
|
|
8
|
-
}
|
|
9
|
-
export type Signers = UnisatSigner;
|
|
10
|
-
export type Wallets = Unisat;
|
|
11
|
-
|
|
12
|
-
export class WalletConnection {
|
|
13
|
-
public signer: Signers | null = null; // OP_WALLET doesn't need a signer
|
|
14
|
-
public walletType: SupportedWallets | null = null;
|
|
15
|
-
public walletWindowInstance: Wallets | null = null;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @description Connect to the wallet
|
|
19
|
-
* @param {SupportedWallets} walletType
|
|
20
|
-
* @returns {Promise<void>}
|
|
21
|
-
*/
|
|
22
|
-
public async connect(walletType: SupportedWallets): Promise<void> {
|
|
23
|
-
if (this.walletType === walletType) return;
|
|
24
|
-
if (this.walletType) this.disconnect();
|
|
25
|
-
|
|
26
|
-
switch (walletType) {
|
|
27
|
-
case SupportedWallets.OP_WALLET: {
|
|
28
|
-
if (window.opnet) {
|
|
29
|
-
try {
|
|
30
|
-
await window.opnet.requestAccounts(); // Trick on OP_WALLET: force the connection popup to appear
|
|
31
|
-
|
|
32
|
-
this.walletType = walletType;
|
|
33
|
-
this.walletWindowInstance = window.opnet;
|
|
34
|
-
return;
|
|
35
|
-
} catch (error: unknown) {
|
|
36
|
-
if (!error) return;
|
|
37
|
-
|
|
38
|
-
if (error instanceof Error) throw new Error(error.message);
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
typeof error === 'object' &&
|
|
42
|
-
'message' in error &&
|
|
43
|
-
typeof error.message === 'string'
|
|
44
|
-
) {
|
|
45
|
-
throw new Error(error.message);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
console.warn(error);
|
|
49
|
-
|
|
50
|
-
throw new Error(`Unknown error, check console`);
|
|
51
|
-
}
|
|
52
|
-
} else {
|
|
53
|
-
throw new Error('OP_WALLET not found');
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
case SupportedWallets.UNISAT: {
|
|
57
|
-
if (window.unisat) {
|
|
58
|
-
try {
|
|
59
|
-
await window.unisat.requestAccounts(); // Trick on Unisat: force the connection popup to appear
|
|
60
|
-
|
|
61
|
-
const signer = new UnisatSigner();
|
|
62
|
-
await signer.init();
|
|
63
|
-
|
|
64
|
-
this.signer = signer;
|
|
65
|
-
this.walletType = walletType;
|
|
66
|
-
this.walletWindowInstance = window.unisat;
|
|
67
|
-
return;
|
|
68
|
-
} catch (error: unknown) {
|
|
69
|
-
if (!error) return;
|
|
70
|
-
|
|
71
|
-
if (error instanceof Error) throw new Error(error.message);
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
typeof error === 'object' &&
|
|
75
|
-
'message' in error &&
|
|
76
|
-
typeof error.message === 'string'
|
|
77
|
-
)
|
|
78
|
-
throw new Error(error.message);
|
|
79
|
-
|
|
80
|
-
console.error(error);
|
|
81
|
-
throw new Error(`Unknown error, check console`);
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
throw new Error('Unisat not found');
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
default:
|
|
88
|
-
throw new Error('Unsupported wallet');
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @description Disconnect from the wallet
|
|
94
|
-
* @returns {void}
|
|
95
|
-
*/
|
|
96
|
-
public disconnect(): void {
|
|
97
|
-
if (!this.walletWindowInstance) return;
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
this.walletType === SupportedWallets.OP_WALLET ||
|
|
101
|
-
this.walletType === SupportedWallets.UNISAT
|
|
102
|
-
)
|
|
103
|
-
this.walletWindowInstance.disconnect();
|
|
104
|
-
|
|
105
|
-
this.signer = null;
|
|
106
|
-
this.walletType = null;
|
|
107
|
-
this.walletWindowInstance = null;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* @description Get the address of the connected wallet
|
|
112
|
-
* @returns {Promise<Address>}
|
|
113
|
-
*/
|
|
114
|
-
public async getAddress(): Promise<Address> {
|
|
115
|
-
if (!this.walletWindowInstance) throw new Error('Wallet not connected');
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
this.walletType === SupportedWallets.OP_WALLET ||
|
|
119
|
-
this.walletType === SupportedWallets.UNISAT
|
|
120
|
-
) {
|
|
121
|
-
const publicKey = await this.walletWindowInstance.getPublicKey();
|
|
122
|
-
|
|
123
|
-
return Address.fromString(publicKey);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
throw new Error('Unsupported wallet');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* @description Get the address typed (P2TR or Native Segwit, etc.) of the connected wallet
|
|
131
|
-
* @returns {Promise<string>}
|
|
132
|
-
*/
|
|
133
|
-
public async getAddressTyped(): Promise<string> {
|
|
134
|
-
if (!this.walletWindowInstance) throw new Error('Wallet not connected');
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
this.walletType === SupportedWallets.OP_WALLET ||
|
|
138
|
-
this.walletType === SupportedWallets.UNISAT
|
|
139
|
-
) {
|
|
140
|
-
const accounts = await this.walletWindowInstance.getAccounts();
|
|
141
|
-
|
|
142
|
-
return accounts[0];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
throw new Error('Unsupported wallet');
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* @description Get the network of the connected wallet
|
|
150
|
-
* @returns {Promise<Network>}
|
|
151
|
-
*/
|
|
152
|
-
public async getNetwork(): Promise<Network> {
|
|
153
|
-
if (!this.walletWindowInstance) throw new Error('Wallet not connected');
|
|
154
|
-
|
|
155
|
-
if (
|
|
156
|
-
this.walletType === SupportedWallets.OP_WALLET ||
|
|
157
|
-
this.walletType === SupportedWallets.UNISAT
|
|
158
|
-
) {
|
|
159
|
-
const chain = await this.walletWindowInstance.getChain();
|
|
160
|
-
|
|
161
|
-
switch (chain.enum) {
|
|
162
|
-
case UnisatChainType.BITCOIN_MAINNET:
|
|
163
|
-
return networks.bitcoin;
|
|
164
|
-
case UnisatChainType.BITCOIN_TESTNET:
|
|
165
|
-
return networks.testnet;
|
|
166
|
-
case UnisatChainType.BITCOIN_REGTEST:
|
|
167
|
-
return networks.regtest;
|
|
168
|
-
case UnisatChainType.FRACTAL_BITCOIN_MAINNET:
|
|
169
|
-
return networks.bitcoin;
|
|
170
|
-
case UnisatChainType.FRACTAL_BITCOIN_TESTNET:
|
|
171
|
-
return networks.testnet;
|
|
172
|
-
default:
|
|
173
|
-
throw new Error('Unsupported network');
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
throw new Error('Unsupported wallet');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* @description Get the provider of the connected wallet
|
|
182
|
-
* @returns {Promise<AbstractRpcProvider>}
|
|
183
|
-
*/
|
|
184
|
-
public async getProvider(): Promise<AbstractRpcProvider> {
|
|
185
|
-
if (!this.walletWindowInstance) throw new Error('Wallet not connected');
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
this.walletType === SupportedWallets.OP_WALLET ||
|
|
189
|
-
this.walletType === SupportedWallets.UNISAT
|
|
190
|
-
) {
|
|
191
|
-
const chain = await this.walletWindowInstance.getChain();
|
|
192
|
-
|
|
193
|
-
switch (chain.enum) {
|
|
194
|
-
case UnisatChainType.BITCOIN_MAINNET:
|
|
195
|
-
return new JSONRpcProvider('https://api.opnet.org', networks.bitcoin);
|
|
196
|
-
case UnisatChainType.BITCOIN_TESTNET:
|
|
197
|
-
return new JSONRpcProvider('https://testnet.opnet.org', networks.testnet);
|
|
198
|
-
case UnisatChainType.BITCOIN_REGTEST:
|
|
199
|
-
return new JSONRpcProvider('https://regtest.opnet.org', networks.regtest);
|
|
200
|
-
// TODO: Add Fractal Mainnet & Testnet when available
|
|
201
|
-
default:
|
|
202
|
-
throw new Error('Unsupported network');
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
throw new Error('Unsupported wallet');
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export default WalletConnection;
|