@btc-vision/walletconnect 1.0.9 → 1.1.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/browser/WalletProvider.d.ts +8 -5
- package/browser/index.js +1 -1
- package/build/WalletProvider.d.ts +8 -5
- package/build/WalletProvider.js +44 -60
- package/package.json +17 -17
- package/src/WalletProvider.tsx +59 -77
|
@@ -3,16 +3,19 @@ import { Address } from '@btc-vision/transaction';
|
|
|
3
3
|
import { AbstractRpcProvider } from 'opnet';
|
|
4
4
|
import React from 'react';
|
|
5
5
|
import { Signers, SupportedWallets, Wallets } from './WalletConnection';
|
|
6
|
+
export interface Account {
|
|
7
|
+
isConnected: boolean;
|
|
8
|
+
signer: Signers | null;
|
|
9
|
+
address: Address;
|
|
10
|
+
network: Network;
|
|
11
|
+
provider: AbstractRpcProvider;
|
|
12
|
+
}
|
|
6
13
|
interface WalletContextType {
|
|
7
14
|
connect: (walletType: SupportedWallets) => Promise<void>;
|
|
8
15
|
disconnect: () => void;
|
|
9
|
-
isConnected: boolean;
|
|
10
|
-
signer: Signers | null;
|
|
11
16
|
walletType: SupportedWallets | null;
|
|
12
17
|
walletWindowInstance: Wallets | null;
|
|
13
|
-
|
|
14
|
-
network: Network | null;
|
|
15
|
-
provider: AbstractRpcProvider | null;
|
|
18
|
+
account: Account | null;
|
|
16
19
|
}
|
|
17
20
|
export declare const WalletProvider: React.FC<{
|
|
18
21
|
children: React.ReactNode;
|
package/build/WalletProvider.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
3
3
|
import WalletConnection, { SupportedWallets } from './WalletConnection';
|
|
4
4
|
const WalletContext = createContext(undefined);
|
|
5
5
|
export const WalletProvider = ({ children }) => {
|
|
6
6
|
const [walletConnection] = useState(new WalletConnection());
|
|
7
|
-
const [isConnected, setIsConnected] = useState(false);
|
|
8
|
-
const [signer, setSigner] = useState(null);
|
|
9
7
|
const [walletType, setWalletType] = useState(null);
|
|
10
8
|
const [walletWindowInstance, setWalletWindowInstance] = useState(null);
|
|
11
|
-
const [
|
|
12
|
-
const
|
|
13
|
-
const [provider, setProvider] = useState(null);
|
|
9
|
+
const [account, setAccount] = useState(null);
|
|
10
|
+
const registeredEvents = useRef(false);
|
|
14
11
|
useEffect(() => {
|
|
15
12
|
const storedWalletType = localStorage.getItem('walletType');
|
|
16
13
|
if (storedWalletType) {
|
|
@@ -26,77 +23,64 @@ export const WalletProvider = ({ children }) => {
|
|
|
26
23
|
!walletConnection.signer) ||
|
|
27
24
|
!walletConnection.walletWindowInstance)
|
|
28
25
|
throw new Error('Failed to connect to wallet');
|
|
29
|
-
setIsConnected(true);
|
|
30
|
-
localStorage.setItem('walletType', walletType);
|
|
31
|
-
setSigner(walletConnection.signer);
|
|
32
26
|
setWalletType(walletType);
|
|
33
27
|
setWalletWindowInstance(walletConnection.walletWindowInstance);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
28
|
+
localStorage.setItem('walletType', walletType);
|
|
29
|
+
const signer = walletConnection.signer;
|
|
30
|
+
const address = await walletConnection.getAddress();
|
|
31
|
+
const network = await walletConnection.getNetwork();
|
|
32
|
+
const provider = await walletConnection.getProvider();
|
|
33
|
+
setAccount({
|
|
34
|
+
isConnected: true,
|
|
35
|
+
signer,
|
|
36
|
+
address,
|
|
37
|
+
network,
|
|
38
|
+
provider,
|
|
39
|
+
});
|
|
37
40
|
if (walletConnection.walletType === SupportedWallets.OP_WALLET ||
|
|
38
41
|
walletConnection.walletType === SupportedWallets.UNISAT) {
|
|
39
|
-
walletConnection.walletWindowInstance
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
walletConnection.walletWindowInstance.on('accountsChanged', async () => {
|
|
43
|
-
if (!walletConnection.walletWindowInstance)
|
|
44
|
-
return;
|
|
45
|
-
try {
|
|
46
|
-
setAddress(await walletConnection.getAddress());
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
disconnect();
|
|
50
|
-
throw error;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
walletConnection.walletWindowInstance.on('chainChanged', async () => {
|
|
54
|
-
if (!walletConnection.walletWindowInstance)
|
|
55
|
-
return;
|
|
56
|
-
try {
|
|
57
|
-
setNetwork(await walletConnection.getNetwork());
|
|
58
|
-
setProvider(await walletConnection.getProvider());
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
42
|
+
const instance = walletConnection.walletWindowInstance;
|
|
43
|
+
if (instance && !registeredEvents.current) {
|
|
44
|
+
instance.on('disconnect', () => {
|
|
61
45
|
disconnect();
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
46
|
+
});
|
|
47
|
+
instance.on('accountsChanged', async () => {
|
|
48
|
+
try {
|
|
49
|
+
const updatedAddress = await walletConnection.getAddress();
|
|
50
|
+
const updatedNetwork = await walletConnection.getNetwork();
|
|
51
|
+
const updatedProvider = await walletConnection.getProvider();
|
|
52
|
+
setAccount((prevAccount) => prevAccount
|
|
53
|
+
? {
|
|
54
|
+
...prevAccount,
|
|
55
|
+
address: updatedAddress,
|
|
56
|
+
network: updatedNetwork,
|
|
57
|
+
provider: updatedProvider,
|
|
58
|
+
}
|
|
59
|
+
: prevAccount);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
disconnect();
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
registeredEvents.current = true;
|
|
67
|
+
}
|
|
77
68
|
}
|
|
78
69
|
}, [walletConnection]);
|
|
79
70
|
const disconnect = useCallback(() => {
|
|
80
71
|
walletConnection.disconnect();
|
|
81
|
-
setIsConnected(false);
|
|
82
|
-
localStorage.removeItem('walletType');
|
|
83
|
-
setSigner(null);
|
|
84
72
|
setWalletType(null);
|
|
85
73
|
setWalletWindowInstance(null);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
74
|
+
localStorage.removeItem('walletType');
|
|
75
|
+
setAccount(null);
|
|
76
|
+
registeredEvents.current = false;
|
|
89
77
|
}, [walletConnection]);
|
|
90
78
|
const value = {
|
|
91
79
|
connect,
|
|
92
80
|
disconnect,
|
|
93
|
-
isConnected,
|
|
94
|
-
signer,
|
|
95
81
|
walletType,
|
|
96
82
|
walletWindowInstance,
|
|
97
|
-
|
|
98
|
-
network,
|
|
99
|
-
provider,
|
|
83
|
+
account,
|
|
100
84
|
};
|
|
101
85
|
return _jsx(WalletContext.Provider, { value: value, children: children });
|
|
102
86
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@btc-vision/walletconnect",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"author": "impredmet",
|
|
6
6
|
"description": "The OP_NET Wallet Connect library helps your dApp connect to any compatible wallet.",
|
|
7
7
|
"engines": {
|
|
@@ -67,29 +67,29 @@
|
|
|
67
67
|
"react-dom": "^18"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@babel/core": "^7.26.
|
|
70
|
+
"@babel/core": "^7.26.10",
|
|
71
71
|
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
72
|
-
"@babel/plugin-transform-runtime": "^7.26.
|
|
73
|
-
"@babel/preset-env": "^7.26.
|
|
72
|
+
"@babel/plugin-transform-runtime": "^7.26.10",
|
|
73
|
+
"@babel/preset-env": "^7.26.9",
|
|
74
74
|
"@babel/preset-flow": "^7.25.9",
|
|
75
75
|
"@babel/preset-react": "^7.26.3",
|
|
76
76
|
"@babel/preset-typescript": "^7.26.0",
|
|
77
|
-
"@types/node": "^22.13.
|
|
78
|
-
"@types/react": "^19.0.
|
|
77
|
+
"@types/node": "^22.13.10",
|
|
78
|
+
"@types/react": "^19.0.10",
|
|
79
79
|
"@types/sha.js": "^2.4.4",
|
|
80
|
-
"eslint": "^9.
|
|
80
|
+
"eslint": "^9.22.0",
|
|
81
81
|
"gulp": "^5.0.0",
|
|
82
82
|
"gulp-cached": "^1.1.1",
|
|
83
83
|
"gulp-typescript": "^6.0.0-alpha.1",
|
|
84
84
|
"https-browserify": "^1.0.0",
|
|
85
85
|
"os-browserify": "^0.3.0",
|
|
86
|
-
"prettier": "^3.5.
|
|
86
|
+
"prettier": "^3.5.3",
|
|
87
87
|
"react": "^19.0.0",
|
|
88
88
|
"react-dom": "^19.0.0",
|
|
89
89
|
"stream-browserify": "^3.0.0",
|
|
90
90
|
"stream-http": "^3.2.0",
|
|
91
|
-
"typedoc": "^0.
|
|
92
|
-
"typescript-eslint": "^8.
|
|
91
|
+
"typedoc": "^0.28.0",
|
|
92
|
+
"typescript-eslint": "^8.26.1",
|
|
93
93
|
"webpack-cli": "^6.0.1"
|
|
94
94
|
},
|
|
95
95
|
"dependencies": {
|
|
@@ -98,11 +98,11 @@
|
|
|
98
98
|
"@btc-vision/bitcoin": "^6.3.6",
|
|
99
99
|
"@btc-vision/bitcoin-rpc": "^1.0.1",
|
|
100
100
|
"@btc-vision/logger": "^1.0.6",
|
|
101
|
-
"@btc-vision/transaction": "^1.
|
|
102
|
-
"@eslint/js": "^9.
|
|
101
|
+
"@btc-vision/transaction": "^1.3.1",
|
|
102
|
+
"@eslint/js": "^9.22.0",
|
|
103
103
|
"@noble/secp256k1": "^2.2.3",
|
|
104
104
|
"assert": "^2.1.0",
|
|
105
|
-
"babel-loader": "^
|
|
105
|
+
"babel-loader": "^10.0.0",
|
|
106
106
|
"babel-plugin-transform-import-meta": "^2.3.2",
|
|
107
107
|
"babel-preset-react": "^6.24.1",
|
|
108
108
|
"babelify": "^10.0.0",
|
|
@@ -111,16 +111,16 @@
|
|
|
111
111
|
"bip32": "^5.0.0-rc.0",
|
|
112
112
|
"browserify-zlib": "^0.2.0",
|
|
113
113
|
"buffer": "^6.0.3",
|
|
114
|
-
"ecpair": "^3.0.0
|
|
114
|
+
"ecpair": "^3.0.0",
|
|
115
115
|
"gulp-clean": "^0.4.0",
|
|
116
116
|
"gulp-eslint-new": "^2.4.0",
|
|
117
117
|
"gulp-logger-new": "^1.0.1",
|
|
118
|
-
"opnet": "^1.
|
|
118
|
+
"opnet": "^1.3.1",
|
|
119
119
|
"process": "^0.11.10",
|
|
120
120
|
"sha.js": "^2.4.11",
|
|
121
121
|
"ts-loader": "^9.5.2",
|
|
122
122
|
"ts-node": "^10.9.2",
|
|
123
|
-
"typescript": "^5.
|
|
124
|
-
"webpack": "^5.
|
|
123
|
+
"typescript": "^5.8.2",
|
|
124
|
+
"webpack": "^5.98.0"
|
|
125
125
|
}
|
|
126
126
|
}
|
package/src/WalletProvider.tsx
CHANGED
|
@@ -1,38 +1,34 @@
|
|
|
1
1
|
import { Network } from '@btc-vision/bitcoin';
|
|
2
2
|
import { Address } from '@btc-vision/transaction';
|
|
3
3
|
import { AbstractRpcProvider } from 'opnet';
|
|
4
|
-
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
|
4
|
+
import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
5
5
|
import WalletConnection, { Signers, SupportedWallets, Wallets } from './WalletConnection';
|
|
6
6
|
|
|
7
|
+
export interface Account {
|
|
8
|
+
isConnected: boolean;
|
|
9
|
+
signer: Signers | null;
|
|
10
|
+
address: Address;
|
|
11
|
+
network: Network;
|
|
12
|
+
provider: AbstractRpcProvider;
|
|
13
|
+
}
|
|
14
|
+
|
|
7
15
|
interface WalletContextType {
|
|
8
16
|
connect: (walletType: SupportedWallets) => Promise<void>;
|
|
9
17
|
disconnect: () => void;
|
|
10
|
-
|
|
11
|
-
isConnected: boolean;
|
|
12
|
-
|
|
13
|
-
signer: Signers | null;
|
|
14
18
|
walletType: SupportedWallets | null;
|
|
15
19
|
walletWindowInstance: Wallets | null;
|
|
16
|
-
|
|
17
|
-
address: Address | null;
|
|
18
|
-
network: Network | null;
|
|
19
|
-
provider: AbstractRpcProvider | null;
|
|
20
|
+
account: Account | null;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
const WalletContext = createContext<WalletContextType | undefined>(undefined);
|
|
23
24
|
|
|
24
25
|
export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
25
26
|
const [walletConnection] = useState(new WalletConnection());
|
|
26
|
-
|
|
27
|
-
const [isConnected, setIsConnected] = useState(false);
|
|
28
|
-
|
|
29
|
-
const [signer, setSigner] = useState<Signers | null>(null);
|
|
30
27
|
const [walletType, setWalletType] = useState<SupportedWallets | null>(null);
|
|
31
28
|
const [walletWindowInstance, setWalletWindowInstance] = useState<Wallets | null>(null);
|
|
29
|
+
const [account, setAccount] = useState<Account | null>(null);
|
|
32
30
|
|
|
33
|
-
const
|
|
34
|
-
const [network, setNetwork] = useState<Network | null>(null);
|
|
35
|
-
const [provider, setProvider] = useState<AbstractRpcProvider | null>(null);
|
|
31
|
+
const registeredEvents = useRef(false);
|
|
36
32
|
|
|
37
33
|
useEffect(() => {
|
|
38
34
|
const storedWalletType = localStorage.getItem('walletType') as SupportedWallets | null;
|
|
@@ -49,7 +45,7 @@ export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|
|
49
45
|
async (walletType: SupportedWallets) => {
|
|
50
46
|
await walletConnection.connect(walletType);
|
|
51
47
|
|
|
52
|
-
// OP_WALLET
|
|
48
|
+
// For wallets other than OP_WALLET, ensure that a signer is present
|
|
53
49
|
if (
|
|
54
50
|
(walletConnection.walletType !== SupportedWallets.OP_WALLET &&
|
|
55
51
|
!walletConnection.signer) ||
|
|
@@ -57,59 +53,58 @@ export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|
|
57
53
|
)
|
|
58
54
|
throw new Error('Failed to connect to wallet');
|
|
59
55
|
|
|
60
|
-
setIsConnected(true);
|
|
61
|
-
localStorage.setItem('walletType', walletType);
|
|
62
|
-
|
|
63
|
-
setSigner(walletConnection.signer);
|
|
64
56
|
setWalletType(walletType);
|
|
65
57
|
setWalletWindowInstance(walletConnection.walletWindowInstance);
|
|
58
|
+
localStorage.setItem('walletType', walletType);
|
|
66
59
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
const signer = walletConnection.signer;
|
|
61
|
+
const address = await walletConnection.getAddress();
|
|
62
|
+
const network = await walletConnection.getNetwork();
|
|
63
|
+
const provider = await walletConnection.getProvider();
|
|
64
|
+
|
|
65
|
+
setAccount({
|
|
66
|
+
isConnected: true,
|
|
67
|
+
signer,
|
|
68
|
+
address,
|
|
69
|
+
network,
|
|
70
|
+
provider,
|
|
71
|
+
});
|
|
70
72
|
|
|
71
73
|
if (
|
|
72
74
|
walletConnection.walletType === SupportedWallets.OP_WALLET ||
|
|
73
75
|
walletConnection.walletType === SupportedWallets.UNISAT
|
|
74
76
|
) {
|
|
75
|
-
walletConnection.walletWindowInstance
|
|
76
|
-
disconnect();
|
|
77
|
-
});
|
|
77
|
+
const instance = walletConnection.walletWindowInstance;
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
setAddress(await walletConnection.getAddress());
|
|
84
|
-
} catch (error) {
|
|
85
|
-
disconnect();
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
walletConnection.walletWindowInstance.on('chainChanged', async () => {
|
|
91
|
-
if (!walletConnection.walletWindowInstance) return;
|
|
92
|
-
|
|
93
|
-
try {
|
|
94
|
-
setNetwork(await walletConnection.getNetwork());
|
|
95
|
-
setProvider(await walletConnection.getProvider());
|
|
96
|
-
} catch (error) {
|
|
79
|
+
if (instance && !registeredEvents.current) {
|
|
80
|
+
instance.on('disconnect', () => {
|
|
97
81
|
disconnect();
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
instance.on('accountsChanged', async () => {
|
|
85
|
+
try {
|
|
86
|
+
const updatedAddress = await walletConnection.getAddress();
|
|
87
|
+
const updatedNetwork = await walletConnection.getNetwork();
|
|
88
|
+
const updatedProvider = await walletConnection.getProvider();
|
|
89
|
+
|
|
90
|
+
setAccount((prevAccount) =>
|
|
91
|
+
prevAccount
|
|
92
|
+
? {
|
|
93
|
+
...prevAccount,
|
|
94
|
+
address: updatedAddress,
|
|
95
|
+
network: updatedNetwork,
|
|
96
|
+
provider: updatedProvider,
|
|
97
|
+
}
|
|
98
|
+
: prevAccount,
|
|
99
|
+
);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
disconnect();
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
registeredEvents.current = true;
|
|
107
|
+
}
|
|
113
108
|
}
|
|
114
109
|
},
|
|
115
110
|
[walletConnection],
|
|
@@ -117,32 +112,19 @@ export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|
|
117
112
|
|
|
118
113
|
const disconnect = useCallback(() => {
|
|
119
114
|
walletConnection.disconnect();
|
|
120
|
-
|
|
121
|
-
setIsConnected(false);
|
|
122
|
-
localStorage.removeItem('walletType');
|
|
123
|
-
|
|
124
|
-
setSigner(null);
|
|
125
115
|
setWalletType(null);
|
|
126
116
|
setWalletWindowInstance(null);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
setProvider(null);
|
|
117
|
+
localStorage.removeItem('walletType');
|
|
118
|
+
setAccount(null);
|
|
119
|
+
registeredEvents.current = false;
|
|
131
120
|
}, [walletConnection]);
|
|
132
121
|
|
|
133
122
|
const value = {
|
|
134
123
|
connect,
|
|
135
124
|
disconnect,
|
|
136
|
-
|
|
137
|
-
isConnected,
|
|
138
|
-
|
|
139
|
-
signer,
|
|
140
125
|
walletType,
|
|
141
126
|
walletWindowInstance,
|
|
142
|
-
|
|
143
|
-
address,
|
|
144
|
-
network,
|
|
145
|
-
provider,
|
|
127
|
+
account,
|
|
146
128
|
};
|
|
147
129
|
|
|
148
130
|
return <WalletContext.Provider value={value}>{children}</WalletContext.Provider>;
|