@monygroupcorp/micro-web3 1.2.1 → 1.2.3
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 +2 -2
- package/dist/micro-web3.esm.js.map +1 -1
- package/dist/micro-web3.umd.js +2 -2
- package/dist/micro-web3.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/services/WalletService.js +139 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monygroupcorp/micro-web3",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
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
|
|
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
|
|
291
|
-
* @
|
|
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
|
}
|
|
@@ -490,7 +566,58 @@ class WalletService {
|
|
|
490
566
|
signer: this.signer
|
|
491
567
|
};
|
|
492
568
|
}
|
|
493
|
-
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Programmatically set the connected state.
|
|
572
|
+
* Useful for migrating from manual workarounds or external connection management.
|
|
573
|
+
*
|
|
574
|
+
* @param {Object} state - The connection state to set
|
|
575
|
+
* @param {string} state.address - The connected wallet address
|
|
576
|
+
* @param {Object} state.provider - The raw provider (window.ethereum or similar)
|
|
577
|
+
* @param {Object} [state.ethersProvider] - Optional ethers Web3Provider (created if not provided)
|
|
578
|
+
* @param {Object} [state.signer] - Optional ethers signer (created if not provided)
|
|
579
|
+
* @param {string} [state.walletType='metamask'] - The wallet type identifier
|
|
580
|
+
* @param {boolean} [emitEvent=true] - Whether to emit wallet:connected event
|
|
581
|
+
* @returns {void}
|
|
582
|
+
*/
|
|
583
|
+
setConnectedState({ address, provider, ethersProvider, signer, walletType = 'metamask' }, emitEvent = true) {
|
|
584
|
+
if (!address) {
|
|
585
|
+
throw new Error('setConnectedState requires an address');
|
|
586
|
+
}
|
|
587
|
+
if (!provider) {
|
|
588
|
+
throw new Error('setConnectedState requires a provider');
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// Update internal state
|
|
592
|
+
this.provider = provider;
|
|
593
|
+
this.connectedAddress = address;
|
|
594
|
+
this.connected = true;
|
|
595
|
+
this.selectedWallet = walletType;
|
|
596
|
+
|
|
597
|
+
// Create ethers provider/signer if not provided
|
|
598
|
+
this.ethersProvider = ethersProvider || new ethers.providers.Web3Provider(provider, 'any');
|
|
599
|
+
this.signer = signer || this.ethersProvider.getSigner();
|
|
600
|
+
|
|
601
|
+
// Set up event listeners for the provider
|
|
602
|
+
this.setupEventListeners();
|
|
603
|
+
|
|
604
|
+
// Store wallet type for future auto-reconnect
|
|
605
|
+
localStorage.setItem('ms2fun_lastWallet', walletType);
|
|
606
|
+
|
|
607
|
+
console.log('[WalletService] State set programmatically:', address);
|
|
608
|
+
|
|
609
|
+
// Emit event so UI components update
|
|
610
|
+
if (emitEvent) {
|
|
611
|
+
this.eventBus.emit('wallet:connected', {
|
|
612
|
+
address: this.connectedAddress,
|
|
613
|
+
walletType: this.selectedWallet,
|
|
614
|
+
provider: this.provider,
|
|
615
|
+
ethersProvider: this.ethersProvider,
|
|
616
|
+
signer: this.signer
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
494
621
|
/**
|
|
495
622
|
* Handle network changes
|
|
496
623
|
* @private
|