@monygroupcorp/micro-web3 1.2.3 → 1.2.4
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/dist/micro-web3.cjs +1 -1
- package/dist/micro-web3.cjs.map +1 -1
- package/dist/micro-web3.esm.js +1 -1
- package/dist/micro-web3.esm.js.map +1 -1
- package/dist/micro-web3.umd.js +1 -1
- package/dist/micro-web3.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/services/WalletService.js +53 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monygroupcorp/micro-web3",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "A lean, reusable Web3 toolkit with components for wallet connection, IPFS, and common Web3 UI patterns.",
|
|
5
5
|
"main": "dist/micro-web3.cjs",
|
|
6
6
|
"module": "dist/micro-web3.esm.js",
|
|
@@ -56,44 +56,77 @@ class WalletService {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
|
-
* Initialize the wallet service
|
|
59
|
+
* Initialize the wallet service.
|
|
60
|
+
* Automatically reconnects to previously authorized wallets by default.
|
|
60
61
|
* @param {Object} options - Initialization options
|
|
61
|
-
* @param {boolean} options.autoReconnect -
|
|
62
|
+
* @param {boolean} [options.autoReconnect=true] - Set to false to disable auto-reconnect
|
|
62
63
|
* @returns {Promise<boolean>} True if initialized successfully
|
|
63
64
|
*/
|
|
64
65
|
async initialize(options = {}) {
|
|
66
|
+
const { autoReconnect = true } = options;
|
|
67
|
+
|
|
65
68
|
try {
|
|
66
69
|
console.log('Initializing WalletService...');
|
|
67
70
|
|
|
68
71
|
// Check if window.ethereum exists
|
|
69
|
-
if (typeof window.ethereum
|
|
70
|
-
// Log that we found a wallet provider
|
|
71
|
-
console.log('Found Ethereum provider');
|
|
72
|
-
|
|
73
|
-
// Let other components know a wallet was detected
|
|
74
|
-
this.eventBus.emit('wallet:detected');
|
|
75
|
-
|
|
76
|
-
// Check if the provider is in MetaMask compatibility mode
|
|
77
|
-
this.isMetaMask = window.ethereum.isMetaMask;
|
|
78
|
-
|
|
79
|
-
// Set up event listeners for wallet changes
|
|
80
|
-
this.setupEventListeners();
|
|
81
|
-
} else {
|
|
72
|
+
if (typeof window.ethereum === 'undefined') {
|
|
82
73
|
console.log('No Ethereum provider found');
|
|
83
74
|
this.eventBus.emit('wallet:notdetected');
|
|
75
|
+
this.isInitialized = true;
|
|
76
|
+
return true;
|
|
84
77
|
}
|
|
85
78
|
|
|
86
|
-
|
|
87
|
-
this.
|
|
79
|
+
console.log('Found Ethereum provider');
|
|
80
|
+
this.eventBus.emit('wallet:detected');
|
|
81
|
+
this.isMetaMask = window.ethereum.isMetaMask;
|
|
82
|
+
|
|
83
|
+
// Check for previously authorized accounts (standard Web3 UX pattern)
|
|
84
|
+
if (autoReconnect) {
|
|
85
|
+
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
|
|
86
|
+
|
|
87
|
+
if (accounts?.length > 0) {
|
|
88
|
+
console.log('[WalletService] Found authorized account:', accounts[0]);
|
|
89
|
+
|
|
90
|
+
// Detect wallet type and update internal state
|
|
91
|
+
const walletType = this._detectWalletType();
|
|
92
|
+
this.selectedWallet = walletType;
|
|
93
|
+
this.provider = window.ethereum;
|
|
94
|
+
this.connectedAddress = accounts[0];
|
|
95
|
+
this.connected = true;
|
|
88
96
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
// Create ethers provider and signer
|
|
98
|
+
this.ethersProvider = new ethers.providers.Web3Provider(window.ethereum, 'any');
|
|
99
|
+
this.signer = this.ethersProvider.getSigner();
|
|
100
|
+
|
|
101
|
+
// Set up event listeners
|
|
102
|
+
this.setupEventListeners();
|
|
103
|
+
|
|
104
|
+
// Store for future sessions
|
|
105
|
+
localStorage.setItem('ms2fun_lastWallet', walletType);
|
|
106
|
+
|
|
107
|
+
console.log('[WalletService] Auto-connected to:', accounts[0]);
|
|
108
|
+
|
|
109
|
+
// Emit connected event
|
|
110
|
+
this.eventBus.emit('wallet:connected', {
|
|
111
|
+
address: this.connectedAddress,
|
|
112
|
+
walletType: this.selectedWallet,
|
|
113
|
+
provider: this.provider,
|
|
114
|
+
ethersProvider: this.ethersProvider,
|
|
115
|
+
signer: this.signer
|
|
116
|
+
});
|
|
117
|
+
}
|
|
92
118
|
}
|
|
93
119
|
|
|
120
|
+
// Set up listeners even if not connected (for future connections)
|
|
121
|
+
if (!this.connected) {
|
|
122
|
+
this.setupEventListeners();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this.isInitialized = true;
|
|
94
126
|
return true;
|
|
95
127
|
} catch (error) {
|
|
96
128
|
console.error('Error initializing WalletService:', error);
|
|
129
|
+
this.isInitialized = true; // Still mark as initialized
|
|
97
130
|
throw error;
|
|
98
131
|
}
|
|
99
132
|
}
|