@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
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
.wallet-connect-light-theme {
|
|
2
|
+
--background-color: #fafafa;
|
|
3
|
+
--primaryColor: #000000;
|
|
4
|
+
--border-color: #bbbbbb;
|
|
5
|
+
|
|
6
|
+
--button-background-color: #ffffff;
|
|
7
|
+
--icon-filter: invert(1) hue-rotate(180deg) !important;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.wallet-connect-dark-theme {
|
|
11
|
+
--background-color: #1e1e1e;
|
|
12
|
+
--primaryColor: #ffffff;
|
|
13
|
+
--border-color: #333333;
|
|
14
|
+
|
|
15
|
+
--button-background-color: #2a2a2a;
|
|
16
|
+
--icon-filter: unset;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.wallet-connect-moto-theme {
|
|
20
|
+
--background-color: #171239;
|
|
21
|
+
--primaryColor: #9698e2;
|
|
22
|
+
--border-color: #201b4e;
|
|
23
|
+
|
|
24
|
+
--button-background-color: #140932;
|
|
25
|
+
--icon-filter: unset;
|
|
26
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { Network, networks } from '@btc-vision/bitcoin';
|
|
2
|
+
import { Unisat, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
3
|
+
import { DefaultWalletConnectNetwork } from '../consts';
|
|
4
|
+
import { WalletConnectNetwork } from '../types';
|
|
5
|
+
import { _e } from '../utils/accessibility/errorDecoder';
|
|
6
|
+
import { SupportedWallets } from './index';
|
|
7
|
+
import type {
|
|
8
|
+
ControllerConnectAccounts,
|
|
9
|
+
ControllerErrorResponse,
|
|
10
|
+
ControllerResponse,
|
|
11
|
+
WalletConnectWallet,
|
|
12
|
+
} from './types.ts';
|
|
13
|
+
|
|
14
|
+
class WalletController {
|
|
15
|
+
private static wallets: Map<string, WalletConnectWallet> = new Map();
|
|
16
|
+
private static currentWallet: WalletConnectWallet | null = null;
|
|
17
|
+
|
|
18
|
+
static getWallets = () => {
|
|
19
|
+
return [...WalletController.wallets.values()];
|
|
20
|
+
};
|
|
21
|
+
static isWalletInstalled(wallet: string): boolean {
|
|
22
|
+
return this.wallets.get(wallet)?.controller?.isInstalled() || false;
|
|
23
|
+
}
|
|
24
|
+
static getWalletType(): SupportedWallets | null {
|
|
25
|
+
return WalletController.currentWallet?.name || null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static getProvider(): Unisat | null {
|
|
29
|
+
const wallet = this.currentWallet;
|
|
30
|
+
if (!wallet) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
// Needs to return a Proxy to be sure useEffects are triggered
|
|
34
|
+
const provider = wallet.controller.getProvider();
|
|
35
|
+
return provider ? new Proxy(provider, {}) : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static async getSigner(): Promise<UnisatSigner | null> {
|
|
39
|
+
const wallet = this.currentWallet;
|
|
40
|
+
if (!wallet) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return await wallet.controller.getSigner();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//TODO: check if we really want to return a default network here
|
|
47
|
+
// instead of null. Default is there: DefaultWalletConnectChain.network
|
|
48
|
+
static convertChainTypeToNetwork(chainType: UnisatChainType): WalletConnectNetwork {
|
|
49
|
+
const walletNetwork = (network: Network): WalletConnectNetwork => {
|
|
50
|
+
return { ...network, chainType: chainType };
|
|
51
|
+
};
|
|
52
|
+
switch (chainType) {
|
|
53
|
+
case UnisatChainType.BITCOIN_REGTEST:
|
|
54
|
+
return walletNetwork(networks.regtest);
|
|
55
|
+
case UnisatChainType.BITCOIN_TESTNET:
|
|
56
|
+
return walletNetwork(networks.testnet);
|
|
57
|
+
case UnisatChainType.BITCOIN_MAINNET:
|
|
58
|
+
return walletNetwork(networks.bitcoin);
|
|
59
|
+
|
|
60
|
+
case UnisatChainType.BITCOIN_TESTNET4:
|
|
61
|
+
case UnisatChainType.BITCOIN_SIGNET:
|
|
62
|
+
case UnisatChainType.FRACTAL_BITCOIN_TESTNET:
|
|
63
|
+
case UnisatChainType.FRACTAL_BITCOIN_MAINNET:
|
|
64
|
+
default:
|
|
65
|
+
return DefaultWalletConnectNetwork;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static async getNetwork(): Promise<WalletConnectNetwork> {
|
|
70
|
+
const wallet = this.currentWallet;
|
|
71
|
+
if (!wallet) {
|
|
72
|
+
return DefaultWalletConnectNetwork;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const chainType = await wallet.controller.getNetwork();
|
|
76
|
+
return this.convertChainTypeToNetwork(chainType);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static async getPublicKey(): Promise<string | null> {
|
|
80
|
+
const wallet = this.currentWallet;
|
|
81
|
+
if (!wallet) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
return wallet.controller.getPublicKey();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static async canAutoConnect(walletName: string) {
|
|
88
|
+
const wallet = this.wallets.get(walletName);
|
|
89
|
+
return (wallet && (await wallet.controller.canAutoConnect())) || false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static async connect(
|
|
93
|
+
walletName: string,
|
|
94
|
+
): Promise<ControllerResponse<ControllerConnectAccounts | ControllerErrorResponse>> {
|
|
95
|
+
const wallet = this.wallets.get(walletName);
|
|
96
|
+
if (!wallet) {
|
|
97
|
+
return {
|
|
98
|
+
code: 404,
|
|
99
|
+
data: {
|
|
100
|
+
message: _e('Wallet not found'),
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const accounts = await wallet.controller.connect();
|
|
106
|
+
await this.disconnectIfWalletChanged(wallet);
|
|
107
|
+
this.currentWallet = wallet;
|
|
108
|
+
return {
|
|
109
|
+
code: 200,
|
|
110
|
+
data: accounts,
|
|
111
|
+
};
|
|
112
|
+
} catch (error) {
|
|
113
|
+
return {
|
|
114
|
+
code: 499,
|
|
115
|
+
data: {
|
|
116
|
+
message: _e((error as Error)?.message || (error as string)),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static async disconnectIfWalletChanged(newWallet: WalletConnectWallet) {
|
|
123
|
+
const wallet = this.currentWallet;
|
|
124
|
+
if (wallet && wallet.name != newWallet.name) {
|
|
125
|
+
await this.disconnect();
|
|
126
|
+
this.unbindHooks();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static async disconnect(): Promise<void> {
|
|
131
|
+
const wallet = this.currentWallet;
|
|
132
|
+
if (!wallet) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
await wallet.controller.disconnect();
|
|
136
|
+
this.currentWallet = null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static setAccountsChangedHook(fn: (accounts: string[]) => void): void {
|
|
140
|
+
const wallet = this.currentWallet;
|
|
141
|
+
if (!wallet) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
wallet.controller.removeAccountsChangedHook();
|
|
146
|
+
wallet.controller.setAccountsChangedHook(fn);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.error('Error setting accounts changed hook:', error);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static setDisconnectHook(fn: () => void): void {
|
|
153
|
+
const wallet = this.currentWallet;
|
|
154
|
+
if (!wallet) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
wallet.controller.removeDisconnectHook();
|
|
159
|
+
wallet.controller.setDisconnectHook(fn);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error('Error setting disconnect hook:', error);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static setChainChangedHook(fn: (network: WalletConnectNetwork) => void): void {
|
|
166
|
+
const wallet = this.currentWallet;
|
|
167
|
+
if (!wallet) {
|
|
168
|
+
console.log('No current wallet to set network switch hook for');
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
wallet.controller.removeChainChangedHook();
|
|
173
|
+
wallet.controller.setChainChangedHook((chainType: UnisatChainType) => {
|
|
174
|
+
fn(this.convertChainTypeToNetwork(chainType));
|
|
175
|
+
});
|
|
176
|
+
} catch (error) {
|
|
177
|
+
console.error('Error setting network switch hook:', error);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static removeAccountsChangedHook(): void {
|
|
182
|
+
const wallet = this.currentWallet;
|
|
183
|
+
if (!wallet) {
|
|
184
|
+
console.log('No current wallet to remove accounts changed hook from');
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
wallet.controller.removeAccountsChangedHook();
|
|
189
|
+
} catch (error) {
|
|
190
|
+
console.error('Error removing accounts changed hook:', error);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static removeDisconnectHook(): void {
|
|
195
|
+
const wallet = this.currentWallet;
|
|
196
|
+
if (!wallet) {
|
|
197
|
+
console.log('No current wallet to remove disconnect hook from');
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
wallet.controller.removeDisconnectHook();
|
|
202
|
+
} catch (error) {
|
|
203
|
+
console.error('Error removing disconnect hook:', error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static removeChainChangedHook(): void {
|
|
208
|
+
const wallet = this.currentWallet;
|
|
209
|
+
if (!wallet) {
|
|
210
|
+
console.log('No current wallet to remove network change hook from');
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
wallet.controller.removeChainChangedHook();
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error('Error removing network change hook:', error);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
static registerWallet = (wallet: WalletConnectWallet): void => {
|
|
221
|
+
this.wallets.set(wallet.name, wallet);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
static unbindHooks(): void {
|
|
225
|
+
this.removeDisconnectHook();
|
|
226
|
+
this.removeChainChangedHook();
|
|
227
|
+
this.removeAccountsChangedHook();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export { WalletController };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { WalletController } from './controller';
|
|
2
|
+
import OPWallet from './opwallet/controller';
|
|
3
|
+
import { logo as OPWalletLogo } from './opwallet/interface';
|
|
4
|
+
import UniSatWallet from './unisat/controller';
|
|
5
|
+
import { logo as UnisatLogo } from './unisat/interface';
|
|
6
|
+
|
|
7
|
+
type SupportedWallets = 'OP_WALLET' | 'UNISAT';
|
|
8
|
+
|
|
9
|
+
WalletController.registerWallet({
|
|
10
|
+
name: 'OP_WALLET',
|
|
11
|
+
icon: OPWalletLogo,
|
|
12
|
+
controller: new OPWallet(),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
WalletController.registerWallet({
|
|
16
|
+
name: 'UNISAT',
|
|
17
|
+
icon: UnisatLogo,
|
|
18
|
+
controller: new UniSatWallet(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export { WalletController };
|
|
22
|
+
export type { SupportedWallets };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Unisat, UnisatChainInfo, UnisatChainType } from '@btc-vision/transaction';
|
|
2
|
+
import type { WalletBase } from '../types.ts';
|
|
3
|
+
import type { OPWalletInterface } from './interface';
|
|
4
|
+
|
|
5
|
+
interface OPWalletWindow extends Window {
|
|
6
|
+
opnet?: OPWalletInterface;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const notInstalledError = 'OP_WALLET is not installed';
|
|
10
|
+
|
|
11
|
+
class OPWallet implements WalletBase {
|
|
12
|
+
private walletBase: OPWalletWindow['opnet'];
|
|
13
|
+
private accountsChangedHookWrapper?: (accounts: Array<string>) => void;
|
|
14
|
+
private chainChangedHookWrapper?: (network: UnisatChainInfo) => void;
|
|
15
|
+
private disconnectHookWrapper?: () => void;
|
|
16
|
+
private _isConnected: boolean = false;
|
|
17
|
+
|
|
18
|
+
isInstalled() {
|
|
19
|
+
this.walletBase = (window as unknown as OPWalletWindow).opnet;
|
|
20
|
+
return !!this.walletBase;
|
|
21
|
+
}
|
|
22
|
+
isConnected() {
|
|
23
|
+
return !!this.walletBase && this._isConnected;
|
|
24
|
+
}
|
|
25
|
+
async canAutoConnect(): Promise<boolean> {
|
|
26
|
+
// getAccounts returns empty array if not connected,
|
|
27
|
+
// without launching connection modal window.
|
|
28
|
+
const accounts = (await this.walletBase?.getAccounts()) || [];
|
|
29
|
+
return accounts.length > 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getChainId(): void {
|
|
33
|
+
throw new Error('Method not implemented.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async connect(): Promise<string[]> {
|
|
37
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
38
|
+
throw new Error(notInstalledError);
|
|
39
|
+
}
|
|
40
|
+
return this.walletBase.requestAccounts().then((accounts: string[]) => {
|
|
41
|
+
this._isConnected = accounts.length > 0;
|
|
42
|
+
return accounts;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async disconnect() {
|
|
47
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
48
|
+
throw new Error(notInstalledError);
|
|
49
|
+
}
|
|
50
|
+
return this._isConnected
|
|
51
|
+
? await this.walletBase.disconnect().then(() => {
|
|
52
|
+
this._isConnected = false;
|
|
53
|
+
})
|
|
54
|
+
: undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getProvider(): Unisat | null {
|
|
58
|
+
return (this._isConnected && this.walletBase) || null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async getSigner(): Promise<null> {
|
|
62
|
+
return Promise.resolve(null);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getPublicKey(): Promise<string> {
|
|
66
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
67
|
+
throw new Error(notInstalledError);
|
|
68
|
+
}
|
|
69
|
+
return this.walletBase.getPublicKey();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async getNetwork(): Promise<UnisatChainType> {
|
|
73
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
74
|
+
throw new Error(notInstalledError);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const chainInfo = await this.walletBase.getChain();
|
|
78
|
+
if (!chainInfo) {
|
|
79
|
+
throw new Error('Failed to retrieve chain information');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return chainInfo.enum;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
setAccountsChangedHook(fn: (accounts: string[]) => void): void {
|
|
86
|
+
console.log('Setting account changed hook for OPWallet');
|
|
87
|
+
|
|
88
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
89
|
+
throw new Error(notInstalledError);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.accountsChangedHookWrapper = (accounts: string[]) => {
|
|
93
|
+
console.log('OPWallet Account Changed Hook', accounts, accounts.length);
|
|
94
|
+
|
|
95
|
+
if (accounts.length > 0) {
|
|
96
|
+
fn(accounts);
|
|
97
|
+
} else {
|
|
98
|
+
console.log(
|
|
99
|
+
'OPWallet Account Changed Hook --> Disconnect',
|
|
100
|
+
accounts.length,
|
|
101
|
+
!!this.disconnectHookWrapper,
|
|
102
|
+
);
|
|
103
|
+
this._isConnected = false;
|
|
104
|
+
this.disconnectHookWrapper?.();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
this.walletBase.on('accountsChanged', this.accountsChangedHookWrapper);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
removeAccountsChangedHook(): void {
|
|
112
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
113
|
+
throw new Error(notInstalledError);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.accountsChangedHookWrapper) {
|
|
117
|
+
console.log('Removing account changed hook for OPWallet');
|
|
118
|
+
this.walletBase.removeListener('accountsChanged', this.accountsChangedHookWrapper);
|
|
119
|
+
this.accountsChangedHookWrapper = undefined;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
setDisconnectHook(fn: () => void): void {
|
|
124
|
+
console.log('Setting disconnect hook for OPWallet');
|
|
125
|
+
|
|
126
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
127
|
+
throw new Error(notInstalledError);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.disconnectHookWrapper = () => {
|
|
131
|
+
console.log('OPWallet Disconnecting Hook');
|
|
132
|
+
fn();
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
this.walletBase.on('disconnect', this.disconnectHookWrapper);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
removeDisconnectHook(): void {
|
|
139
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
140
|
+
throw new Error(notInstalledError);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (this.disconnectHookWrapper) {
|
|
144
|
+
console.log('Removing disconnect hook for OPWallet');
|
|
145
|
+
this.walletBase.removeListener('disconnect', this.disconnectHookWrapper);
|
|
146
|
+
this.disconnectHookWrapper = undefined;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
setChainChangedHook(fn: (chainType: UnisatChainType) => void): void {
|
|
151
|
+
console.log('Setting chain changed hook for OPWallet');
|
|
152
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
153
|
+
throw new Error(notInstalledError);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this.chainChangedHookWrapper = (chainInfo: UnisatChainInfo) => {
|
|
157
|
+
console.log('OPWallet ChainChanged Hook', chainInfo);
|
|
158
|
+
fn(chainInfo.enum);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
this.walletBase.on('chainChanged', this.chainChangedHookWrapper);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
removeChainChangedHook(): void {
|
|
165
|
+
if (!this.isInstalled() || !this.walletBase) {
|
|
166
|
+
throw new Error(notInstalledError);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (this.chainChangedHookWrapper) {
|
|
170
|
+
console.log('Removing chain changed hook for OPWallet');
|
|
171
|
+
this.walletBase.removeListener('chainChanged', this.chainChangedHookWrapper);
|
|
172
|
+
this.chainChangedHookWrapper = undefined;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export default OPWallet;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Unisat } from '@btc-vision/transaction';
|
|
2
|
+
|
|
3
|
+
export interface OPWalletInterface extends Unisat {
|
|
4
|
+
disconnect: () => Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const logo =
|
|
8
|
+
'data:image/svg+xml;base64,' +
|
|
9
|
+
'PHN2ZyB3aWR0aD0iNDgxIiBoZWlnaHQ9IjExMiIgdmlld0JveD0iMCAwIDQ4MSAxMTIiIGZpbGw9' +
|
|
10
|
+
'Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxnIGNsaXAtcGF0aD0i' +
|
|
11
|
+
'dXJsKCNjbGlwMF8wXzMpIj4KPHBhdGggZD0iTTIzNS40IDE4LjVMMjI4LjcgNTcuMUgyMjguMkwy' +
|
|
12
|
+
'MTkuNCAxOC41SDIwOUwyMDAuNSA1Ni45SDE5OS43TDE5My4xIDE4LjVIMTc4LjFMMTkyLjIgNzQu' +
|
|
13
|
+
'OEgxOTIuM0gyMDQuOEwyMTMuNiAzOC4ySDIxNC4yTDIyMi41IDc0LjhIMjM1LjRMMjQ5LjUgMTgu' +
|
|
14
|
+
'NUgyMzUuNFoiIGZpbGw9IndoaXRlIi8+CjxwYXRoIGQ9Ik0yNjguOSAxOC41TDI0Ny41ID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yA1Ni4yNiAyOS4yNyA0Ni42NkMyOS4yNyAzNy4wNiAzNy4wNiAyOS4yNyA0Ni42NiAyOS4yN0M1' +
|
|
39
|
+
'Ni4yNiAyOS4yNyA2NC4wNSAzNy4wNiA2NC4wNSA0Ni42NkM2NC4wNSA1Ni4yNiA1Ni4yNiA2NC4w' +
|
|
40
|
+
'NSA0Ni42NiA2NC4wNVoiIGZpbGw9IndoaXRlIi8+CjwvZz4KPGRlZnM+CjxjbGlwUGF0aCBpZD0i' +
|
|
41
|
+
'Y2xpcDBfMF8zIj4KPHJlY3Qgd2lkdGg9IjQ4MC42IiBoZWlnaHQ9IjExMS43IiBmaWxsPSJ3aGl0' +
|
|
42
|
+
'ZSIvPgo8L2NsaXBQYXRoPgo8L2RlZnM+Cjwvc3ZnPgo=';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Unisat, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
|
|
2
|
+
import { SupportedWallets } from './index';
|
|
3
|
+
export type { AbstractRpcProvider } from 'opnet';
|
|
4
|
+
|
|
5
|
+
export interface WalletBase {
|
|
6
|
+
isInstalled(): boolean;
|
|
7
|
+
isConnected(): boolean;
|
|
8
|
+
canAutoConnect(): Promise<boolean>;
|
|
9
|
+
getProvider(): Unisat | null;
|
|
10
|
+
getSigner(): Promise<UnisatSigner | null>;
|
|
11
|
+
connect(): Promise<string[] | undefined>;
|
|
12
|
+
disconnect(): Promise<void>;
|
|
13
|
+
getPublicKey(): Promise<string | null>;
|
|
14
|
+
getNetwork(): Promise<UnisatChainType>;
|
|
15
|
+
setAccountsChangedHook(fn: (accounts: string[]) => void): void;
|
|
16
|
+
removeAccountsChangedHook(): void;
|
|
17
|
+
setDisconnectHook(fn: () => void): void;
|
|
18
|
+
removeDisconnectHook(): void;
|
|
19
|
+
setChainChangedHook(fn: (network: UnisatChainType) => void): void;
|
|
20
|
+
removeChainChangedHook(): void;
|
|
21
|
+
getChainId(): void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface WalletConnectWallet {
|
|
25
|
+
name: SupportedWallets;
|
|
26
|
+
icon: string;
|
|
27
|
+
controller: WalletBase;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ControllerResponse<T> {
|
|
31
|
+
code: number;
|
|
32
|
+
data?: T;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ControllerErrorResponse {
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type ControllerConnectAccounts = string[];
|