@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.
Files changed (84) hide show
  1. package/.prettierrc.json +1 -1
  2. package/CHANGELOG.md +83 -0
  3. package/README.md +11 -11
  4. package/browser/consts.d.ts +2 -0
  5. package/browser/context/WalletConnectContext.d.ts +18 -0
  6. package/browser/hooks/WalletConnectHook.d.ts +2 -0
  7. package/browser/index.d.ts +4 -2
  8. package/browser/index.js +1 -1
  9. package/browser/index.js.LICENSE.txt +0 -2
  10. package/browser/provider/WalletConnectProvider.d.ts +9 -0
  11. package/browser/types.d.ts +11 -0
  12. package/browser/utils/accessibility/definitions.d.ts +2 -0
  13. package/browser/utils/accessibility/errorDecoder.d.ts +2 -0
  14. package/browser/utils/accessibility/patterns.d.ts +13 -0
  15. package/browser/wallets/controller.d.ts +29 -0
  16. package/browser/wallets/index.d.ts +4 -0
  17. package/browser/wallets/opwallet/controller.d.ts +26 -0
  18. package/browser/wallets/opwallet/interface.d.ts +5 -0
  19. package/browser/wallets/types.d.ts +34 -0
  20. package/browser/wallets/unisat/controller.d.ts +26 -0
  21. package/browser/wallets/unisat/interface.d.ts +5 -0
  22. package/build/consts.d.ts +2 -0
  23. package/build/consts.js +6 -0
  24. package/build/context/WalletConnectContext.d.ts +18 -0
  25. package/build/context/WalletConnectContext.js +2 -0
  26. package/build/hooks/WalletConnectHook.d.ts +2 -0
  27. package/build/hooks/WalletConnectHook.js +9 -0
  28. package/build/index.d.ts +4 -2
  29. package/build/index.js +4 -2
  30. package/build/provider/WalletConnectProvider.d.ts +9 -0
  31. package/build/provider/WalletConnectProvider.js +178 -0
  32. package/build/types.d.ts +11 -0
  33. package/build/types.js +1 -0
  34. package/build/utils/accessibility/definitions.d.ts +2 -0
  35. package/build/utils/accessibility/definitions.js +8 -0
  36. package/build/utils/accessibility/errorDecoder.d.ts +2 -0
  37. package/build/utils/accessibility/errorDecoder.js +73 -0
  38. package/build/utils/accessibility/patterns.d.ts +13 -0
  39. package/build/utils/accessibility/patterns.js +51 -0
  40. package/build/wallets/controller.d.ts +29 -0
  41. package/build/wallets/controller.js +205 -0
  42. package/build/wallets/index.d.ts +4 -0
  43. package/build/wallets/index.js +16 -0
  44. package/build/wallets/opwallet/controller.d.ts +26 -0
  45. package/build/wallets/opwallet/controller.js +132 -0
  46. package/build/wallets/opwallet/interface.d.ts +5 -0
  47. package/build/wallets/opwallet/interface.js +35 -0
  48. package/build/wallets/types.d.ts +34 -0
  49. package/build/wallets/types.js +1 -0
  50. package/build/wallets/unisat/controller.d.ts +26 -0
  51. package/build/wallets/unisat/controller.js +135 -0
  52. package/build/wallets/unisat/interface.d.ts +5 -0
  53. package/build/wallets/unisat/interface.js +285 -0
  54. package/eslint.config.js +23 -2
  55. package/package.json +15 -10
  56. package/src/README.md +24 -0
  57. package/src/consts.ts +8 -0
  58. package/src/context/WalletConnectContext.ts +21 -0
  59. package/src/hooks/WalletConnectHook.tsx +13 -0
  60. package/src/index.ts +5 -2
  61. package/src/provider/WalletConnectProvider.tsx +341 -0
  62. package/src/types.ts +13 -0
  63. package/src/utils/accessibility/definitions.ts +9 -0
  64. package/src/utils/accessibility/errorDecoder.ts +105 -0
  65. package/src/utils/accessibility/patterns.ts +86 -0
  66. package/src/utils/style.css +131 -0
  67. package/src/utils/theme.css +26 -0
  68. package/src/wallets/controller.ts +231 -0
  69. package/src/wallets/index.ts +22 -0
  70. package/src/wallets/opwallet/controller.ts +177 -0
  71. package/src/wallets/opwallet/interface.ts +42 -0
  72. package/src/wallets/types.ts +39 -0
  73. package/src/wallets/unisat/controller.ts +179 -0
  74. package/src/wallets/unisat/interface.ts +292 -0
  75. package/tsconfig.webpack.json +2 -6
  76. package/webpack.config.js +4 -0
  77. package/browser/WalletConnection.d.ts +0 -21
  78. package/browser/WalletProvider.d.ts +0 -25
  79. package/build/WalletConnection.d.ts +0 -21
  80. package/build/WalletConnection.js +0 -152
  81. package/build/WalletProvider.d.ts +0 -25
  82. package/build/WalletProvider.js +0 -178
  83. package/src/WalletConnection.ts +0 -210
  84. package/src/WalletProvider.tsx +0 -240
@@ -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,205 @@
1
+ var _a;
2
+ import { networks } from '@btc-vision/bitcoin';
3
+ import { UnisatChainType } from '@btc-vision/transaction';
4
+ import { DefaultWalletConnectNetwork } from '../consts';
5
+ import { _e } from '../utils/accessibility/errorDecoder';
6
+ class WalletController {
7
+ static isWalletInstalled(wallet) {
8
+ return this.wallets.get(wallet)?.controller?.isInstalled() || false;
9
+ }
10
+ static getWalletType() {
11
+ return _a.currentWallet?.name || null;
12
+ }
13
+ static getProvider() {
14
+ const wallet = this.currentWallet;
15
+ if (!wallet) {
16
+ return null;
17
+ }
18
+ const provider = wallet.controller.getProvider();
19
+ return provider ? new Proxy(provider, {}) : null;
20
+ }
21
+ static async getSigner() {
22
+ const wallet = this.currentWallet;
23
+ if (!wallet) {
24
+ return null;
25
+ }
26
+ return await wallet.controller.getSigner();
27
+ }
28
+ static convertChainTypeToNetwork(chainType) {
29
+ const walletNetwork = (network) => {
30
+ return { ...network, chainType: chainType };
31
+ };
32
+ switch (chainType) {
33
+ case UnisatChainType.BITCOIN_REGTEST:
34
+ return walletNetwork(networks.regtest);
35
+ case UnisatChainType.BITCOIN_TESTNET:
36
+ return walletNetwork(networks.testnet);
37
+ case UnisatChainType.BITCOIN_MAINNET:
38
+ return walletNetwork(networks.bitcoin);
39
+ case UnisatChainType.BITCOIN_TESTNET4:
40
+ case UnisatChainType.BITCOIN_SIGNET:
41
+ case UnisatChainType.FRACTAL_BITCOIN_TESTNET:
42
+ case UnisatChainType.FRACTAL_BITCOIN_MAINNET:
43
+ default:
44
+ return DefaultWalletConnectNetwork;
45
+ }
46
+ }
47
+ static async getNetwork() {
48
+ const wallet = this.currentWallet;
49
+ if (!wallet) {
50
+ return DefaultWalletConnectNetwork;
51
+ }
52
+ const chainType = await wallet.controller.getNetwork();
53
+ return this.convertChainTypeToNetwork(chainType);
54
+ }
55
+ static async getPublicKey() {
56
+ const wallet = this.currentWallet;
57
+ if (!wallet) {
58
+ return null;
59
+ }
60
+ return wallet.controller.getPublicKey();
61
+ }
62
+ static async canAutoConnect(walletName) {
63
+ const wallet = this.wallets.get(walletName);
64
+ return (wallet && (await wallet.controller.canAutoConnect())) || false;
65
+ }
66
+ static async connect(walletName) {
67
+ const wallet = this.wallets.get(walletName);
68
+ if (!wallet) {
69
+ return {
70
+ code: 404,
71
+ data: {
72
+ message: _e('Wallet not found'),
73
+ },
74
+ };
75
+ }
76
+ try {
77
+ const accounts = await wallet.controller.connect();
78
+ await this.disconnectIfWalletChanged(wallet);
79
+ this.currentWallet = wallet;
80
+ return {
81
+ code: 200,
82
+ data: accounts,
83
+ };
84
+ }
85
+ catch (error) {
86
+ return {
87
+ code: 499,
88
+ data: {
89
+ message: _e(error?.message || error),
90
+ },
91
+ };
92
+ }
93
+ }
94
+ static async disconnectIfWalletChanged(newWallet) {
95
+ const wallet = this.currentWallet;
96
+ if (wallet && wallet.name != newWallet.name) {
97
+ await this.disconnect();
98
+ this.unbindHooks();
99
+ }
100
+ }
101
+ static async disconnect() {
102
+ const wallet = this.currentWallet;
103
+ if (!wallet) {
104
+ return;
105
+ }
106
+ await wallet.controller.disconnect();
107
+ this.currentWallet = null;
108
+ }
109
+ static setAccountsChangedHook(fn) {
110
+ const wallet = this.currentWallet;
111
+ if (!wallet) {
112
+ return;
113
+ }
114
+ try {
115
+ wallet.controller.removeAccountsChangedHook();
116
+ wallet.controller.setAccountsChangedHook(fn);
117
+ }
118
+ catch (error) {
119
+ console.error('Error setting accounts changed hook:', error);
120
+ }
121
+ }
122
+ static setDisconnectHook(fn) {
123
+ const wallet = this.currentWallet;
124
+ if (!wallet) {
125
+ return;
126
+ }
127
+ try {
128
+ wallet.controller.removeDisconnectHook();
129
+ wallet.controller.setDisconnectHook(fn);
130
+ }
131
+ catch (error) {
132
+ console.error('Error setting disconnect hook:', error);
133
+ }
134
+ }
135
+ static setChainChangedHook(fn) {
136
+ const wallet = this.currentWallet;
137
+ if (!wallet) {
138
+ console.log('No current wallet to set network switch hook for');
139
+ return;
140
+ }
141
+ try {
142
+ wallet.controller.removeChainChangedHook();
143
+ wallet.controller.setChainChangedHook((chainType) => {
144
+ fn(this.convertChainTypeToNetwork(chainType));
145
+ });
146
+ }
147
+ catch (error) {
148
+ console.error('Error setting network switch hook:', error);
149
+ }
150
+ }
151
+ static removeAccountsChangedHook() {
152
+ const wallet = this.currentWallet;
153
+ if (!wallet) {
154
+ console.log('No current wallet to remove accounts changed hook from');
155
+ return;
156
+ }
157
+ try {
158
+ wallet.controller.removeAccountsChangedHook();
159
+ }
160
+ catch (error) {
161
+ console.error('Error removing accounts changed hook:', error);
162
+ }
163
+ }
164
+ static removeDisconnectHook() {
165
+ const wallet = this.currentWallet;
166
+ if (!wallet) {
167
+ console.log('No current wallet to remove disconnect hook from');
168
+ return;
169
+ }
170
+ try {
171
+ wallet.controller.removeDisconnectHook();
172
+ }
173
+ catch (error) {
174
+ console.error('Error removing disconnect hook:', error);
175
+ }
176
+ }
177
+ static removeChainChangedHook() {
178
+ const wallet = this.currentWallet;
179
+ if (!wallet) {
180
+ console.log('No current wallet to remove network change hook from');
181
+ return;
182
+ }
183
+ try {
184
+ wallet.controller.removeChainChangedHook();
185
+ }
186
+ catch (error) {
187
+ console.error('Error removing network change hook:', error);
188
+ }
189
+ }
190
+ static unbindHooks() {
191
+ this.removeDisconnectHook();
192
+ this.removeChainChangedHook();
193
+ this.removeAccountsChangedHook();
194
+ }
195
+ }
196
+ _a = WalletController;
197
+ WalletController.wallets = new Map();
198
+ WalletController.currentWallet = null;
199
+ WalletController.getWallets = () => {
200
+ return [..._a.wallets.values()];
201
+ };
202
+ WalletController.registerWallet = (wallet) => {
203
+ _a.wallets.set(wallet.name, wallet);
204
+ };
205
+ export { WalletController };
@@ -0,0 +1,4 @@
1
+ import { WalletController } from './controller';
2
+ type SupportedWallets = 'OP_WALLET' | 'UNISAT';
3
+ export { WalletController };
4
+ export type { SupportedWallets };
@@ -0,0 +1,16 @@
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
+ WalletController.registerWallet({
7
+ name: 'OP_WALLET',
8
+ icon: OPWalletLogo,
9
+ controller: new OPWallet(),
10
+ });
11
+ WalletController.registerWallet({
12
+ name: 'UNISAT',
13
+ icon: UnisatLogo,
14
+ controller: new UniSatWallet(),
15
+ });
16
+ 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,132 @@
1
+ const notInstalledError = 'OP_WALLET is not installed';
2
+ class OPWallet {
3
+ constructor() {
4
+ this._isConnected = false;
5
+ }
6
+ isInstalled() {
7
+ this.walletBase = window.opnet;
8
+ return !!this.walletBase;
9
+ }
10
+ isConnected() {
11
+ return !!this.walletBase && this._isConnected;
12
+ }
13
+ async canAutoConnect() {
14
+ const accounts = (await this.walletBase?.getAccounts()) || [];
15
+ return accounts.length > 0;
16
+ }
17
+ getChainId() {
18
+ throw new Error('Method not implemented.');
19
+ }
20
+ async connect() {
21
+ if (!this.isInstalled() || !this.walletBase) {
22
+ throw new Error(notInstalledError);
23
+ }
24
+ return this.walletBase.requestAccounts().then((accounts) => {
25
+ this._isConnected = accounts.length > 0;
26
+ return accounts;
27
+ });
28
+ }
29
+ async disconnect() {
30
+ if (!this.isInstalled() || !this.walletBase) {
31
+ throw new Error(notInstalledError);
32
+ }
33
+ return this._isConnected
34
+ ? await this.walletBase.disconnect().then(() => {
35
+ this._isConnected = false;
36
+ })
37
+ : undefined;
38
+ }
39
+ getProvider() {
40
+ return (this._isConnected && this.walletBase) || null;
41
+ }
42
+ async getSigner() {
43
+ return Promise.resolve(null);
44
+ }
45
+ getPublicKey() {
46
+ if (!this.isInstalled() || !this.walletBase) {
47
+ throw new Error(notInstalledError);
48
+ }
49
+ return this.walletBase.getPublicKey();
50
+ }
51
+ async getNetwork() {
52
+ if (!this.isInstalled() || !this.walletBase) {
53
+ throw new Error(notInstalledError);
54
+ }
55
+ const chainInfo = await this.walletBase.getChain();
56
+ if (!chainInfo) {
57
+ throw new Error('Failed to retrieve chain information');
58
+ }
59
+ return chainInfo.enum;
60
+ }
61
+ setAccountsChangedHook(fn) {
62
+ console.log('Setting account changed hook for OPWallet');
63
+ if (!this.isInstalled() || !this.walletBase) {
64
+ throw new Error(notInstalledError);
65
+ }
66
+ this.accountsChangedHookWrapper = (accounts) => {
67
+ console.log('OPWallet Account Changed Hook', accounts, accounts.length);
68
+ if (accounts.length > 0) {
69
+ fn(accounts);
70
+ }
71
+ else {
72
+ console.log('OPWallet Account Changed Hook --> Disconnect', accounts.length, !!this.disconnectHookWrapper);
73
+ this._isConnected = false;
74
+ this.disconnectHookWrapper?.();
75
+ }
76
+ };
77
+ this.walletBase.on('accountsChanged', this.accountsChangedHookWrapper);
78
+ }
79
+ removeAccountsChangedHook() {
80
+ if (!this.isInstalled() || !this.walletBase) {
81
+ throw new Error(notInstalledError);
82
+ }
83
+ if (this.accountsChangedHookWrapper) {
84
+ console.log('Removing account changed hook for OPWallet');
85
+ this.walletBase.removeListener('accountsChanged', this.accountsChangedHookWrapper);
86
+ this.accountsChangedHookWrapper = undefined;
87
+ }
88
+ }
89
+ setDisconnectHook(fn) {
90
+ console.log('Setting disconnect hook for OPWallet');
91
+ if (!this.isInstalled() || !this.walletBase) {
92
+ throw new Error(notInstalledError);
93
+ }
94
+ this.disconnectHookWrapper = () => {
95
+ console.log('OPWallet Disconnecting Hook');
96
+ fn();
97
+ };
98
+ this.walletBase.on('disconnect', this.disconnectHookWrapper);
99
+ }
100
+ removeDisconnectHook() {
101
+ if (!this.isInstalled() || !this.walletBase) {
102
+ throw new Error(notInstalledError);
103
+ }
104
+ if (this.disconnectHookWrapper) {
105
+ console.log('Removing disconnect hook for OPWallet');
106
+ this.walletBase.removeListener('disconnect', this.disconnectHookWrapper);
107
+ this.disconnectHookWrapper = undefined;
108
+ }
109
+ }
110
+ setChainChangedHook(fn) {
111
+ console.log('Setting chain changed hook for OPWallet');
112
+ if (!this.isInstalled() || !this.walletBase) {
113
+ throw new Error(notInstalledError);
114
+ }
115
+ this.chainChangedHookWrapper = (chainInfo) => {
116
+ console.log('OPWallet ChainChanged Hook', chainInfo);
117
+ fn(chainInfo.enum);
118
+ };
119
+ this.walletBase.on('chainChanged', this.chainChangedHookWrapper);
120
+ }
121
+ removeChainChangedHook() {
122
+ if (!this.isInstalled() || !this.walletBase) {
123
+ throw new Error(notInstalledError);
124
+ }
125
+ if (this.chainChangedHookWrapper) {
126
+ console.log('Removing chain changed hook for OPWallet');
127
+ this.walletBase.removeListener('chainChanged', this.chainChangedHookWrapper);
128
+ this.chainChangedHookWrapper = undefined;
129
+ }
130
+ }
131
+ }
132
+ export default OPWallet;
@@ -0,0 +1,5 @@
1
+ import type { Unisat } from '@btc-vision/transaction';
2
+ export interface OPWalletInterface extends Unisat {
3
+ disconnect: () => Promise<void>;
4
+ }
5
+ export declare const logo: string;
@@ -0,0 +1,35 @@
1
+ export const logo = 'data:image/svg+xml;base64,' +
2
+ 'PHN2ZyB3aWR0aD0iNDgxIiBoZWlnaHQ9IjExMiIgdmlld0JveD0iMCAwIDQ4MSAxMTIiIGZpbGw9' +
3
+ 'Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxnIGNsaXAtcGF0aD0i' +
4
+ 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2LjY2QzAgNzIuNDMgMjAu' +
29
+ 'ODkgOTMuMzIgNDYuNjYgOTMuMzJDNzIuNDMgOTMuMzIgOTMuMzIgNzIuNDMgOTMuMzIgNDYuNjZD' +
30
+ 'OTMuMzIgMjAuODkgNzIuNDMgMCA0Ni42NiAwWk00Ni42NiA2NC4wNUMzNy4wNiA2NC4wNSAyOS4y' +
31
+ 'NyA1Ni4yNiAyOS4yNyA0Ni42NkMyOS4yNyAzNy4wNiAzNy4wNiAyOS4yNyA0Ni42NiAyOS4yN0M1' +
32
+ 'Ni4yNiAyOS4yNyA2NC4wNSAzNy4wNiA2NC4wNSA0Ni42NkM2NC4wNSA1Ni4yNiA1Ni4yNiA2NC4w' +
33
+ 'NSA0Ni42NiA2NC4wNVoiIGZpbGw9IndoaXRlIi8+CjwvZz4KPGRlZnM+CjxjbGlwUGF0aCBpZD0i' +
34
+ 'Y2xpcDBfMF8zIj4KPHJlY3Qgd2lkdGg9IjQ4MC42IiBoZWlnaHQ9IjExMS43IiBmaWxsPSJ3aGl0' +
35
+ 'ZSIvPgo8L2NsaXBQYXRoPgo8L2RlZnM+Cjwvc3ZnPgo=';
@@ -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 @@
1
+ export {};
@@ -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;
@@ -0,0 +1,135 @@
1
+ import { UnisatSigner } from '@btc-vision/transaction';
2
+ const notInstalledError = 'UNISAT is not installed';
3
+ class UnisatWallet {
4
+ constructor() {
5
+ this._isConnected = false;
6
+ }
7
+ isInstalled() {
8
+ this.walletBase = window.unisat;
9
+ return !!this.walletBase;
10
+ }
11
+ isConnected() {
12
+ return !!this.walletBase && this._isConnected;
13
+ }
14
+ async canAutoConnect() {
15
+ const accounts = (await this.walletBase?.getAccounts()) || [];
16
+ return accounts.length > 0;
17
+ }
18
+ getProvider() {
19
+ return (this._isConnected && this.walletBase) || null;
20
+ }
21
+ async getSigner() {
22
+ const signer = new UnisatSigner();
23
+ await signer.init();
24
+ return signer;
25
+ }
26
+ getChainId() {
27
+ throw new Error('Method not implemented.');
28
+ }
29
+ async connect() {
30
+ if (!this.isInstalled() || !this.walletBase) {
31
+ throw new Error(notInstalledError);
32
+ }
33
+ return this.walletBase.requestAccounts().then((accounts) => {
34
+ this._isConnected = accounts.length > 0;
35
+ return accounts;
36
+ });
37
+ }
38
+ async disconnect() {
39
+ if (!this.isInstalled() || !this.walletBase) {
40
+ throw new Error(notInstalledError);
41
+ }
42
+ return this._isConnected
43
+ ? await this.walletBase.disconnect().then(() => {
44
+ this._isConnected = false;
45
+ })
46
+ : undefined;
47
+ }
48
+ getPublicKey() {
49
+ if (!this.isInstalled() || !this.walletBase) {
50
+ throw new Error(notInstalledError);
51
+ }
52
+ return this.walletBase.getPublicKey();
53
+ }
54
+ async getNetwork() {
55
+ if (!this.isInstalled() || !this.walletBase) {
56
+ throw new Error(notInstalledError);
57
+ }
58
+ const chainInfo = await this.walletBase.getChain();
59
+ if (!chainInfo) {
60
+ throw new Error('Failed to retrieve chain information');
61
+ }
62
+ return chainInfo.enum;
63
+ }
64
+ setAccountsChangedHook(fn) {
65
+ console.log('Setting account changed hook for Unisat');
66
+ if (!this.isInstalled() || !this.walletBase) {
67
+ throw new Error(notInstalledError);
68
+ }
69
+ this.accountsChangedHookWrapper = (accounts) => {
70
+ console.log('Unisat Account Changed Hook', accounts, accounts.length);
71
+ if (accounts.length > 0) {
72
+ fn(accounts);
73
+ }
74
+ else {
75
+ console.log('Unisat Account Changed Hook --> Disconnect', accounts.length, !!this.disconnectHookWrapper);
76
+ this._isConnected = false;
77
+ this.disconnectHookWrapper?.();
78
+ }
79
+ };
80
+ this.walletBase.on('accountsChanged', this.accountsChangedHookWrapper);
81
+ }
82
+ removeAccountsChangedHook() {
83
+ if (!this.isInstalled() || !this.walletBase) {
84
+ throw new Error(notInstalledError);
85
+ }
86
+ if (this.accountsChangedHookWrapper) {
87
+ console.log('Removing account changed hook for Unisat');
88
+ this.walletBase.removeListener('accountsChanged', this.accountsChangedHookWrapper);
89
+ this.accountsChangedHookWrapper = undefined;
90
+ }
91
+ }
92
+ setDisconnectHook(fn) {
93
+ console.log('Setting disconnect hook for Unisat');
94
+ if (!this.isInstalled() || !this.walletBase) {
95
+ throw new Error(notInstalledError);
96
+ }
97
+ this.disconnectHookWrapper = () => {
98
+ console.log('Unisat Disconnecting Hook');
99
+ fn();
100
+ };
101
+ this.walletBase.on('disconnect', this.disconnectHookWrapper);
102
+ }
103
+ removeDisconnectHook() {
104
+ if (!this.isInstalled() || !this.walletBase) {
105
+ throw new Error(notInstalledError);
106
+ }
107
+ if (this.disconnectHookWrapper) {
108
+ console.log('Removing disconnect hook for Unisat');
109
+ this.walletBase.removeListener('disconnect', this.disconnectHookWrapper);
110
+ this.disconnectHookWrapper = undefined;
111
+ }
112
+ }
113
+ setChainChangedHook(fn) {
114
+ console.log('Setting chain changed hook for Unisat');
115
+ if (!this.isInstalled() || !this.walletBase) {
116
+ throw new Error(notInstalledError);
117
+ }
118
+ this.chainChangedHookWrapper = (chainInfo) => {
119
+ console.log('Unisat ChainChanged Hook', chainInfo);
120
+ fn(chainInfo.enum);
121
+ };
122
+ this.walletBase.on('chainChanged', this.chainChangedHookWrapper);
123
+ }
124
+ removeChainChangedHook() {
125
+ if (!this.isInstalled() || !this.walletBase) {
126
+ throw new Error(notInstalledError);
127
+ }
128
+ if (this.chainChangedHookWrapper) {
129
+ console.log('Removing chain changed hook for Unisat');
130
+ this.walletBase.removeListener('chainChanged', this.chainChangedHookWrapper);
131
+ this.chainChangedHookWrapper = undefined;
132
+ }
133
+ }
134
+ }
135
+ export default UnisatWallet;
@@ -0,0 +1,5 @@
1
+ import type { Unisat } from '@btc-vision/transaction';
2
+ export interface UnisatWalletInterface extends Unisat {
3
+ disconnect: () => Promise<void>;
4
+ }
5
+ export declare const logo: string;