@monygroupcorp/micro-web3 1.2.1 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monygroupcorp/micro-web3",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
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,39 +56,108 @@ class WalletService {
56
56
  }
57
57
 
58
58
  /**
59
- * Initialize the wallet service - just check for wallet presence
59
+ * Initialize the wallet service
60
+ * @param {Object} options - Initialization options
61
+ * @param {boolean} options.autoReconnect - If true, automatically reconnect to previously authorized wallets
62
+ * @returns {Promise<boolean>} True if initialized successfully
60
63
  */
61
- async initialize() {
64
+ async initialize(options = {}) {
62
65
  try {
63
66
  console.log('Initializing WalletService...');
64
-
67
+
65
68
  // Check if window.ethereum exists
66
69
  if (typeof window.ethereum !== 'undefined') {
67
70
  // Log that we found a wallet provider
68
71
  console.log('Found Ethereum provider');
69
-
72
+
70
73
  // Let other components know a wallet was detected
71
74
  this.eventBus.emit('wallet:detected');
72
-
75
+
73
76
  // Check if the provider is in MetaMask compatibility mode
74
77
  this.isMetaMask = window.ethereum.isMetaMask;
75
-
78
+
76
79
  // Set up event listeners for wallet changes
77
80
  this.setupEventListeners();
78
81
  } else {
79
82
  console.log('No Ethereum provider found');
80
83
  this.eventBus.emit('wallet:notdetected');
81
84
  }
82
-
85
+
83
86
  // Mark as initialized
84
87
  this.isInitialized = true;
85
-
88
+
89
+ // Auto-reconnect if requested
90
+ if (options.autoReconnect) {
91
+ await this.tryAutoConnect();
92
+ }
93
+
86
94
  return true;
87
95
  } catch (error) {
88
96
  console.error('Error initializing WalletService:', error);
89
97
  throw error;
90
98
  }
91
99
  }
100
+
101
+ /**
102
+ * Attempt to auto-connect to a previously authorized wallet.
103
+ * Uses eth_accounts (non-prompting) to check for existing authorization.
104
+ * @returns {Promise<string|null>} The connected address, or null if no authorized wallet found
105
+ */
106
+ async tryAutoConnect() {
107
+ try {
108
+ if (typeof window.ethereum === 'undefined') {
109
+ return null;
110
+ }
111
+
112
+ // eth_accounts returns already-authorized accounts without prompting
113
+ const accounts = await window.ethereum.request({ method: 'eth_accounts' });
114
+
115
+ if (!accounts || accounts.length === 0) {
116
+ console.log('[WalletService] No previously authorized accounts found');
117
+ return null;
118
+ }
119
+
120
+ console.log('[WalletService] Found authorized account:', accounts[0]);
121
+
122
+ // Detect which wallet type is active
123
+ const walletType = this._detectWalletType();
124
+ console.log('[WalletService] Detected wallet type:', walletType);
125
+
126
+ // Select and connect (this won't prompt since already authorized)
127
+ await this.selectWallet(walletType);
128
+ const address = await this.connect();
129
+
130
+ console.log('[WalletService] Auto-reconnected to:', address);
131
+ return address;
132
+
133
+ } catch (error) {
134
+ console.warn('[WalletService] Auto-connect failed:', error.message);
135
+ return null;
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Detect which wallet type is currently active based on provider flags
141
+ * @private
142
+ * @returns {string} The detected wallet type
143
+ */
144
+ _detectWalletType() {
145
+ if (typeof window.ethereum === 'undefined') return 'metamask';
146
+
147
+ // Check specific wallet flags (order matters - more specific first)
148
+ if (window.ethereum.isRabby) return 'rabby';
149
+ if (window.ethereum.isRainbow) return 'rainbow';
150
+ if (window.phantom?.ethereum) return 'phantom';
151
+
152
+ // Check localStorage for last used wallet as a hint
153
+ const lastWallet = localStorage.getItem('ms2fun_lastWallet');
154
+ if (lastWallet && this.providerMap[lastWallet]) {
155
+ return lastWallet;
156
+ }
157
+
158
+ // Default to metamask for any EIP-1193 provider
159
+ return 'metamask';
160
+ }
92
161
 
93
162
  /**
94
163
  * Format error to provide better context
@@ -287,11 +356,18 @@ class WalletService {
287
356
  }
288
357
 
289
358
  /**
290
- * Connect to the selected wallet
291
- * @returns {string} The connected address
359
+ * Connect to a wallet
360
+ * @param {string} [walletType] - Optional wallet type to connect to directly (e.g., 'metamask', 'rabby')
361
+ * If not provided, requires prior call to selectWallet()
362
+ * @returns {Promise<string>} The connected address
292
363
  */
293
- async connect() {
364
+ async connect(walletType) {
294
365
  try {
366
+ // If walletType provided, select it first
367
+ if (walletType) {
368
+ await this.selectWallet(walletType);
369
+ }
370
+
295
371
  if (!this.selectedWallet || !this.provider) {
296
372
  throw new Error('Please select a wallet first');
297
373
  }